fix bugs in maths module (more bug fixes to come), and add indexing to function.

This commit is contained in:
Ugric committed 2022-07-09 18:51:29 +02:00
1 parent cbb51bb308
commit 1cb3e37e47
10 files changed
+264 -115

No files matched your search

+15
View File
@@ -80,6 +80,21 @@ while (yes) [
] ]
``` ```
Import Modules:
- import the whole module
```javascript
import 'maths'
log(pi)
```
- import specific functions and variables from the module
```javascript
from 'maths' import sqrt, sin, cos, tan, pi, e
log(pi)
```
Items (Lists or Arrays): Items (Lists or Arrays):
```javascript ```javascript
+5
View File
@@ -28,12 +28,17 @@ sub sqrt(x) [
if x equals 0 [ if x equals 0 [
return 0 return 0
] ]
i = 0
while yes [ while yes [
if i > 10000 [
error "sqrt: could not find a solution for the square root of " + x
]
guess = (last_guess + x / last_guess) / 2 guess = (last_guess + x / last_guess) / 2
if abs(guess - last_guess) < 1e-5 [ if abs(guess - last_guess) < 1e-5 [
return guess return guess
] ]
last_guess = guess last_guess = guess
i = i + 1
] ]
] ]
+10 -16
View File
@@ -1,36 +1,30 @@
# trigonometry # trigonometry
# a sub library of maths # a sub library of maths
from 'functions' import abs, sqrt from 'functions' import abs, sqrt, mod
# trigonometry
sub sin(theta) [ sub sin(theta) [
x = ((theta)/180) mod 1 result = -(mod(theta divided by 180, 1) minus .5) to the power of 2 times 4 plus 1
result = 0-(((x - 0.5)**2) * 4) + 1 if ((theta div 180) div 1) mod 2 is not equal to 0 [
if ((theta div 180) div 1) % 2 != 0 [ result = result times -1
result = result * -1
] ]
return result return result
] ]
sub cos(theta) [ sub cos(theta) [
return sin(theta + 90) return sin(theta plus 90)
] ]
sub tan(theta) [ sub tan(theta) [
return sin(theta) / cos(theta) return sin(theta) divided by cos(theta)
] ]
# inverse trigonometry # inverse trigonometry
sub asin(result) [ sub asin(result) [
theta = result return (sqrt(abs(-(result minus 1) divided by 4)) plus .5) times 180
theta = (sqrt((0-(result - 1))/4)+0.5)
theta = (theta * 180) mod 180
if result < 0 [
theta = (0-theta) - 180
]
return theta
] ]
sub acos(result) [ sub acos(result) [
@@ -38,5 +32,5 @@ sub acos(result) [
] ]
sub atan(result) [ sub atan(result) [
return asin(result) * acos(result) return
] ]
+3 -2
View File
@@ -8,11 +8,12 @@ type opperator struct {
type variable struct { type variable struct {
variable string variable string
splices []any
line int line int
} }
type funcCallType struct { type funcCallType struct {
name string name variable
args []any args []any
line int line int
} }
@@ -48,7 +49,7 @@ type ifstatement struct {
type setVariable struct { type setVariable struct {
TYPE string TYPE string
variable string variable variable
value any value any
line int line int
} }
+5 -5
View File
@@ -1,5 +1,5 @@
try [ from 'maths/trigonometry' import tan, atan
error "wow"
] catch [ x = tan(19)
log('lol, error:', err) y = atan(x)
] log(x, tan(y), y)
+1
View File
@@ -56,6 +56,7 @@ func importMod(realpath string, origin string) (map[string]variableValue, any) {
func runStr(str string, origin string, variables map[string]variableValue) ([][]any, any) { func runStr(str string, origin string, variables map[string]variableValue) ([][]any, any) {
translated, err := translate(str) translated, err := translate(str)
fmt.Println(origin, ":", translated)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+122 -46
View File
@@ -124,7 +124,10 @@ func runprocess(codeseg any, origin string, vargroups []map[string]variableValue
return resp, ty return resp, ty
case whileLoop: case whileLoop:
whileloop := codeseg whileloop := codeseg
resp, _ := runop(whileloop.condition, origin, vargroups) resp, ty := runop(whileloop.condition, origin, vargroups)
if ty == "error" {
return resp, ty
}
vari := append(vargroups, map[string]variableValue{}) vari := append(vargroups, map[string]variableValue{})
for boolean(resp) { for boolean(resp) {
ty, val, _ := run(whileloop.code, origin, vari) ty, val, _ := run(whileloop.code, origin, vari)
@@ -135,14 +138,20 @@ func runprocess(codeseg any, origin string, vargroups []map[string]variableValue
} else if ty == "return" || ty == "error" { } else if ty == "return" || ty == "error" {
return val, ty return val, ty
} }
resp, _ = runop(whileloop.condition, origin, vargroups) resp, ty = runop(whileloop.condition, origin, vargroups)
if ty == "error" {
return resp, ty
}
} }
return nil, nil return nil, nil
case ifstatement: case ifstatement:
vari := append(vargroups, map[string]variableValue{}) vari := append(vargroups, map[string]variableValue{})
iff := codeseg iff := codeseg
for i := 0; i < len(iff.statments); i++ { for i := 0; i < len(iff.statments); i++ {
resp, _ := runop(iff.statments[i].condition, origin, vargroups) resp, ty := runop(iff.statments[i].condition, origin, vargroups)
if ty == "error" {
return resp, ty
}
if boolean(resp) { if boolean(resp) {
ty, val, _ := run(iff.statments[i].code, origin, vari) ty, val, _ := run(iff.statments[i].code, origin, vari)
return val, ty return val, ty
@@ -151,7 +160,10 @@ func runprocess(codeseg any, origin string, vargroups []map[string]variableValue
ty, val, _ := run(iff.FALSE, origin, vari) ty, val, _ := run(iff.FALSE, origin, vari)
return val, ty return val, ty
case importType: case importType:
resp, _ := runop(codeseg.path, origin, vargroups) resp, ty := runop(codeseg.path, origin, vargroups)
if ty == "error" {
return resp, ty
}
importvars, err := importMod(resp.(string), filepath.Dir(origin)) importvars, err := importMod(resp.(string), filepath.Dir(origin))
if err != nil { if err != nil {
return err, "error" return err, "error"
@@ -202,28 +214,29 @@ func runprocess(codeseg any, origin string, vargroups []map[string]variableValue
func callFunc(call funcCallType, vargroups []map[string]variableValue, origin string) (any, any) { func callFunc(call funcCallType, vargroups []map[string]variableValue, origin string) (any, any) {
var variables map[string]variableValue var variables map[string]variableValue
for i := len(vargroups) - 1; i >= 0; i-- { for i := len(vargroups) - 1; i >= 0; i-- {
if vargroups[i][call.name].EXISTS != nil { if vargroups[i][call.name.variable].EXISTS != nil {
variables = vargroups[i] variables = vargroups[i]
break break
} }
} }
if variables[call.name].EXISTS == nil {
return ("undecared function '" + call.name + "': " + origin + ":" + fmt.Sprint(call.line+1)), "error" if variables[call.name.variable].EXISTS == nil {
return ("undecared function '" + call.name.variable + "': " + origin + ":" + fmt.Sprint(call.line+1)), "error"
} }
callable := call callable := call
for variables[callable.name].TYPE != "func" && variables[callable.name].TYPE != "init_function" && fmt.Sprint(reflect.TypeOf(variables[callable.name].VAL)) == "main.variable" { for variables[callable.name.variable].TYPE != "func" && variables[callable.name.variable].TYPE != "init_function" && fmt.Sprint(reflect.TypeOf(variables[callable.name.variable].VAL)) == "main.variable" {
callable = funcCallType{name: variables[callable.name].VAL.(variable).variable, args: callable.args, line: callable.line} callable = funcCallType{name: variables[callable.name.variable].VAL.(variable), args: callable.args, line: callable.line}
} }
for i := len(vargroups) - 1; i >= 0; i-- { for i := len(vargroups) - 1; i >= 0; i-- {
if vargroups[i][callable.name].EXISTS != nil { if vargroups[i][callable.name.variable].EXISTS != nil {
variables = vargroups[i] variables = vargroups[i]
break break
} }
} }
if variables[callable.name].TYPE != "func" && variables[callable.name].TYPE != "init_function" { if variables[callable.name.variable].TYPE != "func" && variables[callable.name.variable].TYPE != "init_function" {
return ("'" + call.name + "' is not a function: " + origin + ":" + fmt.Sprint(call.line+1)), "error" return ("'" + call.name.variable + "' is not a function: " + origin + ":" + fmt.Sprint(call.line+1)), "error"
} }
if variables[callable.name].FUNC { if variables[callable.name.variable].FUNC {
argvals := []any{} argvals := []any{}
for i := 0; i < len(callable.args); i++ { for i := 0; i < len(callable.args); i++ {
@@ -233,7 +246,7 @@ func callFunc(call funcCallType, vargroups []map[string]variableValue, origin st
} }
argvals = append(argvals, resp) argvals = append(argvals, resp)
} }
val, err := variables[callable.name].VAL.(func(...any) (any, any))(argvals...) val, err := variables[callable.name.variable].VAL.(func(...any) (any, any))(argvals...)
if err != nil { if err != nil {
return err, "error" return err, "error"
} }
@@ -241,8 +254,11 @@ func callFunc(call funcCallType, vargroups []map[string]variableValue, origin st
} else { } else {
argvars := make(map[string]variableValue) argvars := make(map[string]variableValue)
for i := 0; i < len(callable.args); i++ { for i := 0; i < len(callable.args); i++ {
resp, _ := runprocess(callable.args[i], origin, vargroups) resp, ty := runprocess(callable.args[i], origin, vargroups)
name := variables[callable.name].VAL.(setFunction).args[i] if ty == "error" {
return resp, ty
}
name := variables[callable.name.variable].VAL.(setFunction).args[i]
argvars[name] = variableValue{ argvars[name] = variableValue{
VAL: resp, VAL: resp,
TYPE: "var", TYPE: "var",
@@ -250,8 +266,8 @@ func callFunc(call funcCallType, vargroups []map[string]variableValue, origin st
FUNC: false, FUNC: false,
} }
} }
ty, val, _ := run(variables[callable.name].VAL.(setFunction).code, origin, append(vargroups, modules[variables[callable.name].origin], argvars)) ty, val, _ := run(variables[callable.name.variable].VAL.(setFunction).code, origin, append(vargroups, modules[variables[callable.name.variable].origin], argvars))
if ty != "return" && ty != nil { if ty != "return" && ty != "error" && ty != nil {
return fmt.Sprint(ty) + " is not allowed in function: " + origin + ":" + fmt.Sprint(callable.line+1), "error" return fmt.Sprint(ty) + " is not allowed in function: " + origin + ":" + fmt.Sprint(callable.line+1), "error"
} }
return val, "value" return val, "value"
@@ -262,7 +278,7 @@ func setVariableVal(x setVariable, vargroups [](map[string]variableValue), origi
var variable map[string]variableValue = nil var variable map[string]variableValue = nil
if x.TYPE == "preset" { if x.TYPE == "preset" {
for i := len(vargroups) - 1; i >= 0; i-- { for i := len(vargroups) - 1; i >= 0; i-- {
if vargroups[i][x.variable].EXISTS != nil { if vargroups[i][x.variable.variable].EXISTS != nil {
variable = vargroups[i] variable = vargroups[i]
break break
} }
@@ -270,34 +286,40 @@ func setVariableVal(x setVariable, vargroups [](map[string]variableValue), origi
} else { } else {
variable = vargroups[len(vargroups)-1] variable = vargroups[len(vargroups)-1]
} }
if variable[x.variable].EXISTS == nil { if variable[x.variable.variable].EXISTS == nil {
resp, _ := runop(x.value, origin, vargroups) resp, ty := runop(x.value, origin, vargroups)
if ty == "error" {
return resp
}
var TYPE = x.TYPE var TYPE = x.TYPE
if TYPE == "preset" { if TYPE == "preset" {
TYPE = "var" TYPE = "var"
} }
vargroups[len(vargroups)-1][x.variable] = variableValue{ vargroups[len(vargroups)-1][x.variable.variable] = variableValue{
TYPE: TYPE, TYPE: TYPE,
EXISTS: true, EXISTS: true,
VAL: resp, VAL: resp,
origin: origin, origin: origin,
FUNC: false, FUNC: false,
} }
} else if variable[x.variable].TYPE == "var" { } else if variable[x.variable.variable].TYPE == "var" {
resp, _ := runop(x.value, origin, vargroups) resp, ty := runop(x.value, origin, vargroups)
if ty == "error" {
return resp
}
var TYPE = x.TYPE var TYPE = x.TYPE
if TYPE == "preset" { if TYPE == "preset" {
TYPE = "var" TYPE = "var"
} }
variable[x.variable] = variableValue{ variable[x.variable.variable] = variableValue{
TYPE: TYPE, TYPE: TYPE,
EXISTS: variable[x.variable].EXISTS, EXISTS: variable[x.variable.variable].EXISTS,
VAL: resp, VAL: resp,
origin: origin, origin: origin,
FUNC: variable[x.variable].FUNC, FUNC: variable[x.variable.variable].FUNC,
} }
} else { } else {
return ("cannot edit " + variable[x.variable].TYPE + " variable: " + origin + ":" + fmt.Sprint(x.line+1)) return ("cannot edit " + variable[x.variable.variable].TYPE + " variable: " + origin + ":" + fmt.Sprint(x.line+1))
} }
return nil return nil
} }
@@ -394,7 +416,10 @@ opperationloop:
for i := 0; i < len(opperation.vals); i++ { for i := 0; i < len(opperation.vals); i++ {
switch opperation.t { switch opperation.t {
case 0: case 0:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
@@ -406,27 +431,39 @@ opperationloop:
} }
} }
case 1: case 1:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if boolean(x) { if boolean(x) {
output = x output = x
break opperationloop break opperationloop
} }
case 2: case 2:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
output = xiny(output, x.([]any)) output = xiny(output, x.([]any))
} }
case 3: case 3:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
output = !xiny(output, x.([]interface{})) output = !xiny(output, x.([]interface{}))
} }
case 4: case 4:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
@@ -442,7 +479,10 @@ opperationloop:
output = (out <= in) output = (out <= in)
} }
case 5: case 5:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
@@ -458,7 +498,10 @@ opperationloop:
output = (out >= in) output = (out >= in)
} }
case 6: case 6:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
@@ -474,7 +517,10 @@ opperationloop:
output = (out < in) output = (out < in)
} }
case 7: case 7:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
@@ -490,21 +536,30 @@ opperationloop:
output = (out > in) output = (out > in)
} }
case 8: case 8:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
output = (output != x) output = (output != x)
} }
case 9: case 9:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
output = (output == x) output = (output == x)
} }
case 11: case 11:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
@@ -520,7 +575,10 @@ opperationloop:
output = (out - in) output = (out - in)
} }
case 10: case 10:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
@@ -531,7 +589,10 @@ opperationloop:
output = val output = val
} }
case 12: case 12:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
@@ -547,7 +608,10 @@ opperationloop:
output = (out * in) output = (out * in)
} }
case 14: case 14:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
@@ -563,7 +627,10 @@ opperationloop:
output = math.Floor(out / in) output = math.Floor(out / in)
} }
case 13: case 13:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
@@ -579,7 +646,10 @@ opperationloop:
output = math.Mod(out, in) output = math.Mod(out, in)
} }
case 15: case 15:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
@@ -595,7 +665,10 @@ opperationloop:
output = (out / in) output = (out / in)
} }
case 16: case 16:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
@@ -611,7 +684,10 @@ opperationloop:
output = math.Pow(out, 1/in) output = math.Pow(out, 1/in)
} }
case 17: case 17:
x, _ := runop(opperation.vals[i], origin, vargroups) x, ty := runop(opperation.vals[i], origin, vargroups)
if ty == "error" {
return x, ty
}
if output == nil { if output == nil {
output = x output = x
} else { } else {
+1 -1
View File
@@ -23,7 +23,7 @@ func shell() {
val, _ := ArgonInput("... ") val, _ := ArgonInput("... ")
tempstr = val.(string) tempstr = val.(string)
} }
if openCompile.MatchString(tempstr) && !setVariableCompile.MatchString(tempstr) { if opensIndent(tempstr) {
indent-- indent--
} else if closeCompile.MatchString(tempstr) { } else if closeCompile.MatchString(tempstr) {
indent++ indent++
+4
View File
@@ -19,3 +19,7 @@ incompleted:
- books (objects) - books (objects)
- add del to delete variables - add del to delete variables
- add origin to translate.go - add origin to translate.go
- add edit variable assignment
- allow negetive variables without needing 0 in front (e.g. -x instead of 0-x)
- add index and key to variable assignment and reading
- fix atan function in maths module
+98 -45
View File
@@ -8,22 +8,22 @@ import (
var anyAndNewline = "((.)|(\\n))" var anyAndNewline = "((.)|(\\n))"
var variableOnly = "([a-zA-Z_])([a-zA-Z0-9_])*" var variableOnly = "([a-zA-Z_])([a-zA-Z0-9_])*"
var variableWithIndexes = variableOnly + "(\\[" + anyAndNewline + "+\\]|\\.variableOnly)*"
var stringCompile = makeRegex("(( *)\"((\\\\([a-z\\\"'`]))|[^\\\"])*\"( *))|(( *)'((\\\\([a-z\\'\"`]))|[^\\'])*'( *))|(( *)(`(\\\\[a-z\\\"'`\\n]|[^\\`])*`)( *))") var stringCompile = makeRegex("(( *)\"((\\\\([a-z\\\"'`]))|[^\\\"])*\"( *))|(( *)'((\\\\([a-z\\'\"`]))|[^\\'])*'( *))|(( *)(`(\\\\[a-z\\\"'`\\n]|[^\\`])*`)( *))")
var numberCompile = makeRegex("( *)(\\-)?(([0-9]+(\\.[0-9]+)?)(e((\\-|\\+)?([0-9]+(\\.[0-9]+)?)))?)( *)") var numberCompile = makeRegex("( *)(\\-)?(([0-9]*(\\.[0-9]+)?)(e((\\-|\\+)?([0-9]+(\\.[0-9]+)?)))?)( *)")
var whileCompile = makeRegex("( *)(while( )+" + anyAndNewline + "+( )+\\[" + anyAndNewline + "*)( *)") var whileCompile = makeRegex("( *)(while( )+" + anyAndNewline + "+( )+\\[" + anyAndNewline + "*)( *)")
var subCompile = makeRegex("( *)(sub( )+" + variableOnly + "\\(" + anyAndNewline + "*\\)( )+\\[" + anyAndNewline + "*)( *)") var subCompile = makeRegex("( *)(sub( )+" + variableOnly + "\\(" + anyAndNewline + "*\\)( )+\\[" + anyAndNewline + "*)( *)")
var ifCompile = makeRegex("( *)(if( )+" + anyAndNewline + "+( )+\\[" + anyAndNewline + "*)( *)") var ifCompile = makeRegex("( *)(if( )+" + anyAndNewline + "+( )+\\[" + anyAndNewline + "*)( *)")
var elseifCompile = makeRegex("( *)(\\]( )+else( )+if( )+" + anyAndNewline + "+( )+\\[" + anyAndNewline + "*)( *)") var elseifCompile = makeRegex("( *)(\\]( )+else( )+if( )+" + anyAndNewline + "+( )+\\[" + anyAndNewline + "*)( *)")
var openCompile = makeRegex("( *)((.)+( )+" + anyAndNewline + "+(\\(" + anyAndNewline + "+\\))?( )+\\[" + anyAndNewline + "*)")
var elseCompile = makeRegex("( *)\\]( )+else( )+\\[" + anyAndNewline + "*( *)") var elseCompile = makeRegex("( *)\\]( )+else( )+\\[" + anyAndNewline + "*( *)")
var closeCompile = makeRegex("( *)\\]( *)") var closeCompile = makeRegex("( *)\\]( *)")
var switchCloseCompile = makeRegex("( *)" + anyAndNewline + "*\\]" + anyAndNewline + "*\\[" + anyAndNewline + "*( *)") var switchCloseCompile = makeRegex("( *)" + anyAndNewline + "*\\]" + anyAndNewline + "*\\[" + anyAndNewline + "*( *)")
var importCompile = makeRegex("( *)((import " + anyAndNewline + "+)|(from( +)" + anyAndNewline + "+( +)import( +)(( *)" + variableOnly + "( *)(\\,( *)" + variableOnly + "( *))*)))( *)") var importCompile = makeRegex("( *)((import " + anyAndNewline + "+)|(from( +)" + anyAndNewline + "+( +)import( +)(( *)" + variableOnly + "( *)(\\,( *)" + variableOnly + "( *))*)))( *)")
var bracketsCompile = makeRegex("( *)\\(" + anyAndNewline + "*\\)( *)") var bracketsCompile = makeRegex("( *)\\(" + anyAndNewline + "*\\)( *)")
var functionCompile = makeRegex("( *)" + variableOnly + "\\(" + anyAndNewline + "*\\)( *)") var functionCompile = makeRegex("( *)" + variableOnly + "\\(" + anyAndNewline + "*\\)( *)")
var variableTextCompile = makeRegex("( *)" + variableOnly + "( *)") var variableTextCompile = makeRegex("( *)" + variableWithIndexes + "( *)")
var variableonlyCompile = makeRegex(variableOnly) var variableonlyCompile = makeRegex(variableOnly)
var setVariableCompile = makeRegex("( *)(((const|var) (" + variableOnly + "( *))=( *).+)|(( *)" + variableOnly + "( *))(( *)=( *).+))( *)") var setVariableCompile = makeRegex("( *)(((const|var) (" + variableOnly + "( *))=( *)" + anyAndNewline + "+)|(( *)" + variableOnly + "( *))(( *)=( *)" + anyAndNewline + "+))( *)")
var callErrorCompile = makeRegex("( *)(error(( )+" + anyAndNewline + "+))( *)") var callErrorCompile = makeRegex("( *)(error(( )+" + anyAndNewline + "+))( *)")
var returnStatementCompile = makeRegex("( *)(return(( )+" + anyAndNewline + "*)?)( *)") var returnStatementCompile = makeRegex("( *)(return(( )+" + anyAndNewline + "*)?)( *)")
var itemsCompiled = makeRegex("( *)(\\[" + anyAndNewline + "*\\])( *)") var itemsCompiled = makeRegex("( *)(\\[" + anyAndNewline + "*\\])( *)")
@@ -32,6 +32,7 @@ var continueStatementCompile = makeRegex("( *)continue( *)")
var skipCompile = makeRegex("( *)(#(.*))?") var skipCompile = makeRegex("( *)(#(.*))?")
var tryCompile = makeRegex("( *)(try( )+\\[" + anyAndNewline + "*)( *)") var tryCompile = makeRegex("( *)(try( )+\\[" + anyAndNewline + "*)( *)")
var catchCompile = makeRegex("( *)(\\]( )+catch( )+\\[" + anyAndNewline + "*)( *)") var catchCompile = makeRegex("( *)(\\]( )+catch( )+\\[" + anyAndNewline + "*)( *)")
var negetiveCompile = makeRegex("( *)(\\-" + anyAndNewline + "+)( *)")
var processfunc func(codeseg code) (any, bool, any) var processfunc func(codeseg code) (any, bool, any)
var linefunc func(i int, codearray []code) (any, int, any) var linefunc func(i int, codearray []code) (any, int, any)
var opperators = [][]string{ var opperators = [][]string{
@@ -119,6 +120,10 @@ var opperators = [][]string{
"**", "**",
}} }}
func opensIndent(str string) bool {
return tryCompile.MatchString(str) || ifCompile.MatchString(str) || whileCompile.MatchString(str) || subCompile.MatchString(str)
}
func init() { func init() {
processfunc = translateprocess processfunc = translateprocess
linefunc = translateline linefunc = translateline
@@ -199,10 +204,11 @@ func unescape(str string) string {
output = append(output, 39) output = append(output, 39)
case 92: case 92:
output = append(output, 92) output = append(output, 92)
case 117: case 85:
if i+4 < stringlen { val, err := strconv.Unquote(`"\U` + str[i+1:i+9] + `"`)
i += 4 if err == nil {
output = append(output, byte(int(str[i])*16*16*16+int(str[i+1])*16*16+int(str[i+2])*16+int(str[i+3]))) output = append(output, val...)
i += 8
} }
} }
} }
@@ -280,14 +286,14 @@ var getCodeInIndent = func(i int, codearray []code, TYPE string) ([]code, int, a
i++ i++
for { for {
if i == len(codearray) { if i == len(codearray) {
return nil, 0, ("invalid opening brackets starting on line " + fmt.Sprint(codearray[i].line+1) + ": " + start.code) return nil, 0, ("invalid opening brackets starting on line " + fmt.Sprint(start.line+1) + ": " + start.code)
} }
if (TYPE != "try" && closeCompile.MatchString(codearray[i].code) && !setVariableCompile.MatchString(codearray[i].code) && !switchCloseCompile.MatchString(codearray[i].code)) || (TYPE == "if" && (elseCompile.MatchString(codearray[i].code) || elseifCompile.MatchString(codearray[i].code)) || (TYPE == "try" && catchCompile.MatchString(codearray[i].code))) { if (TYPE != "try" && closeCompile.MatchString(codearray[i].code) && !setVariableCompile.MatchString(codearray[i].code) && !switchCloseCompile.MatchString(codearray[i].code)) || (TYPE == "if" && (elseCompile.MatchString(codearray[i].code) || elseifCompile.MatchString(codearray[i].code)) || (TYPE == "try" && catchCompile.MatchString(codearray[i].code))) {
indent-- indent--
if indent < 0 { if indent < 0 {
break break
} }
} else if openCompile.MatchString(codearray[i].code) && !switchCloseCompile.MatchString(codearray[i].code) { } else if opensIndent(codearray[i].code) {
indent++ indent++
} }
result = append(result, codearray[i]) result = append(result, codearray[i])
@@ -306,13 +312,10 @@ var translateprocess = func(codeseg code) (any, bool, any) {
current := 0 current := 0
for k := 0; k < len(split); k++ { for k := 0; k < len(split); k++ {
currentstr := strings.Join(split[current:k], opperators[i][j]) currentstr := strings.Join(split[current:k], opperators[i][j])
val, worked, err := processfunc(code{ val, worked, _ := processfunc(code{
code: currentstr, code: currentstr,
line: codeseg.line, line: codeseg.line,
}) })
if err != nil {
return nil, false, err
}
if worked { if worked {
vals = append(vals, val) vals = append(vals, val)
current = k current = k
@@ -335,8 +338,6 @@ var translateprocess = func(codeseg code) (any, bool, any) {
vals: vals, vals: vals,
line: codeseg.line, line: codeseg.line,
}, true, nil }, true, nil
} else if len(opperators) == i+1 {
return nil, false, nil
} }
} }
} }
@@ -344,6 +345,23 @@ var translateprocess = func(codeseg code) (any, bool, any) {
} }
if stringCompile.MatchString(codeseg.code) { if stringCompile.MatchString(codeseg.code) {
return unescape(stringencode(codeseg.code)), true, nil return unescape(stringencode(codeseg.code)), true, nil
} else if negetiveCompile.MatchString(codeseg.code) {
str := strings.Trim(codeseg.code, " ")
val, worked, err := processfunc(code{
code: str[1:],
line: codeseg.line,
})
if err != nil {
return val, worked, err
}
if !worked {
return nil, false, "invalid value on line " + fmt.Sprint(codeseg.line+1)
}
return opperator{
t: 11,
vals: []any{0, val},
line: codeseg.line,
}, true, nil
} else if functionCompile.MatchString(codeseg.code) { } else if functionCompile.MatchString(codeseg.code) {
str := strings.Trim(codeseg.code, " ") str := strings.Trim(codeseg.code, " ")
bracketsplit := strings.SplitN(str, "(", 2) bracketsplit := strings.SplitN(str, "(", 2)
@@ -355,7 +373,11 @@ var translateprocess = func(codeseg code) (any, bool, any) {
} }
if worked { if worked {
return funcCallType{ return funcCallType{
name: funcname, name: variable{
variable: funcname,
splices: []any{},
line: codeseg.line,
},
args: paramstranslated, args: paramstranslated,
line: codeseg.line, line: codeseg.line,
}, true, nil }, true, nil
@@ -389,6 +411,7 @@ var translateprocess = func(codeseg code) (any, bool, any) {
} else if variableTextCompile.MatchString(codeseg.code) { } else if variableTextCompile.MatchString(codeseg.code) {
return variable{ return variable{
variable: strings.Trim(codeseg.code, " "), variable: strings.Trim(codeseg.code, " "),
splices: []any{},
line: codeseg.line, line: codeseg.line,
}, true, nil }, true, nil
} }
@@ -436,7 +459,7 @@ var translateline = func(i int, codearray []code) (any, int, any) {
if len(firstSplit) > 1 { if len(firstSplit) > 1 {
TYPE = firstSplit[0] TYPE = firstSplit[0]
} }
variable := firstSplit[len(firstSplit)-1] vari := firstSplit[len(firstSplit)-1]
resp, worked, err := translateprocess(code{code: VAR[1], resp, worked, err := translateprocess(code{code: VAR[1],
line: codeseg.line}) line: codeseg.line})
if err != nil { if err != nil {
@@ -446,10 +469,14 @@ var translateline = func(i int, codearray []code) (any, int, any) {
return nil, 0, ("invalid value on line " + fmt.Sprint(codeseg.line+1) + ": " + VAR[1]) return nil, 0, ("invalid value on line " + fmt.Sprint(codeseg.line+1) + ": " + VAR[1])
} }
return setVariable{ return setVariable{
TYPE: TYPE, TYPE: TYPE,
variable: variable, variable: variable{
value: resp, variable: vari,
line: codeseg.line, splices: []any{},
line: codeseg.line,
},
value: resp,
line: codeseg.line,
}, i + 1, nil }, i + 1, nil
} else if whileCompile.MatchString(codeseg.code) { } else if whileCompile.MatchString(codeseg.code) {
str := strings.Trim(codeseg.code, " ") str := strings.Trim(codeseg.code, " ")
@@ -477,7 +504,10 @@ var translateline = func(i int, codearray []code) (any, int, any) {
return nil, 0, err return nil, 0, err
} }
i = x i = x
codeoutput = append(codeoutput, resp)
if resp != nil {
codeoutput = append(codeoutput, resp)
}
} }
return whileLoop{ return whileLoop{
@@ -498,7 +528,10 @@ var translateline = func(i int, codearray []code) (any, int, any) {
return nil, 0, err return nil, 0, err
} }
i = x i = x
codeoutput = append(codeoutput, resp)
if resp != nil {
codeoutput = append(codeoutput, resp)
}
} }
catchdata, x, err := getCodeInIndent(x-1, codearray, "none") catchdata, x, err := getCodeInIndent(x-1, codearray, "none")
if err != nil { if err != nil {
@@ -512,7 +545,10 @@ var translateline = func(i int, codearray []code) (any, int, any) {
return nil, 0, err return nil, 0, err
} }
i = x i = x
catchoutput = append(catchoutput, resp)
if resp != nil {
catchoutput = append(catchoutput, resp)
}
} }
return tryType{ return tryType{
code: codeoutput, code: codeoutput,
@@ -545,7 +581,9 @@ var translateline = func(i int, codearray []code) (any, int, any) {
return nil, 0, err return nil, 0, err
} }
i = x i = x
codeoutput = append(codeoutput, resp) if resp != nil {
codeoutput = append(codeoutput, resp)
}
} }
ifstatementcode := []iftype{ ifstatementcode := []iftype{
{ {
@@ -579,7 +617,9 @@ var translateline = func(i int, codearray []code) (any, int, any) {
return nil, 0, err return nil, 0, err
} }
i = x i = x
codeoutput = append(codeoutput, resp) if resp != nil {
codeoutput = append(codeoutput, resp)
}
} }
ifstatementcode = append(ifstatementcode, ifstatementcode = append(ifstatementcode,
iftype{ iftype{
@@ -603,7 +643,9 @@ var translateline = func(i int, codearray []code) (any, int, any) {
return nil, 0, err return nil, 0, err
} }
i = x i = x
elsecode = append(elsecode, resp) if resp != nil {
elsecode = append(elsecode, resp)
}
} }
} }
return ifstatement{ return ifstatement{
@@ -635,7 +677,9 @@ var translateline = func(i int, codearray []code) (any, int, any) {
return nil, 0, err return nil, 0, err
} }
i = x i = x
codeoutput = append(codeoutput, resp) if resp != nil {
codeoutput = append(codeoutput, resp)
}
} }
return setFunction{ return setFunction{
name: functionname, name: functionname,
@@ -647,29 +691,36 @@ var translateline = func(i int, codearray []code) (any, int, any) {
var toImport any = nil var toImport any = nil
var path any var path any
if strings.HasPrefix(str, "import") { if strings.HasPrefix(str, "import") {
p, worked, err := processfunc(code{code: str[7:], line: codeseg.line}) if variableonlyCompile.MatchString(strings.Trim(str[7:], " ")) {
if err != nil { path = strings.Trim(str[7:], " ")
return nil, 0, err } else {
} p, worked, err := processfunc(code{code: str[7:], line: codeseg.line})
path = p if err != nil {
if !worked { return nil, 0, err
return nil, 0, "invalid import path on line " + fmt.Sprint(codeseg.line+1) }
path = p
if !worked {
return nil, 0, "invalid import path on line " + fmt.Sprint(codeseg.line+1)
}
} }
} else { } else {
info := strings.SplitN(str[5:], " import ", 2) info := strings.SplitN(str[5:], " import ", 2)
p, worked, err := processfunc(code{code: info[0], line: codeseg.line}) if variableonlyCompile.MatchString(strings.Trim(info[0], " ")) {
if err != nil { path = strings.Trim(info[0], " ")
return nil, 0, err } else {
} p, worked, err := processfunc(code{code: info[0], line: codeseg.line})
path = p if err != nil {
if !worked { return nil, 0, err
return nil, 0, "invalid import path on line " + fmt.Sprint(codeseg.line+1) }
path = p
if !worked {
return nil, 0, "invalid import path on line " + fmt.Sprint(codeseg.line+1)
}
} }
toImport, err = getParamNames(info[1], codeseg.line) toImport, err = getParamNames(info[1], codeseg.line)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
} }
return importType{ return importType{
path: path, path: path,
@@ -706,7 +757,9 @@ var translate = func(str string) ([]any, any) {
return nil, err return nil, err
} }
i = x i = x
output = append(output, resp) if resp != nil {
output = append(output, resp)
}
} }
return output, nil return output, nil
} }