mirror of
https://github.com/Open-Argon/argon-v2.git
synced 2026-10-28 09:44:03 +00:00
fix bugs in maths module (more bug fixes to come), and add indexing to function.
This commit is contained in:
1 parent
cbb51bb308
commit
1cb3e37e47
10 files changed
+264
-115
No files matched your search
@@ -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):
|
||||
|
||||
```javascript
|
||||
|
||||
@@ -28,12 +28,17 @@ sub sqrt(x) [
|
||||
if x equals 0 [
|
||||
return 0
|
||||
]
|
||||
i = 0
|
||||
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
|
||||
if abs(guess - last_guess) < 1e-5 [
|
||||
return guess
|
||||
]
|
||||
last_guess = guess
|
||||
i = i + 1
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
@@ -1,36 +1,30 @@
|
||||
# trigonometry
|
||||
# a sub library of maths
|
||||
|
||||
from 'functions' import abs, sqrt
|
||||
from 'functions' import abs, sqrt, mod
|
||||
|
||||
# trigonometry
|
||||
|
||||
sub sin(theta) [
|
||||
x = ((theta)/180) mod 1
|
||||
result = 0-(((x - 0.5)**2) * 4) + 1
|
||||
if ((theta div 180) div 1) % 2 != 0 [
|
||||
result = result * -1
|
||||
result = -(mod(theta divided by 180, 1) minus .5) to the power of 2 times 4 plus 1
|
||||
if ((theta div 180) div 1) mod 2 is not equal to 0 [
|
||||
result = result times -1
|
||||
]
|
||||
return result
|
||||
]
|
||||
]
|
||||
|
||||
sub cos(theta) [
|
||||
return sin(theta + 90)
|
||||
return sin(theta plus 90)
|
||||
]
|
||||
|
||||
sub tan(theta) [
|
||||
return sin(theta) / cos(theta)
|
||||
return sin(theta) divided by cos(theta)
|
||||
]
|
||||
|
||||
# inverse trigonometry
|
||||
|
||||
sub asin(result) [
|
||||
theta = result
|
||||
theta = (sqrt((0-(result - 1))/4)+0.5)
|
||||
theta = (theta * 180) mod 180
|
||||
if result < 0 [
|
||||
theta = (0-theta) - 180
|
||||
]
|
||||
return theta
|
||||
return (sqrt(abs(-(result minus 1) divided by 4)) plus .5) times 180
|
||||
]
|
||||
|
||||
sub acos(result) [
|
||||
@@ -38,5 +32,5 @@ sub acos(result) [
|
||||
]
|
||||
|
||||
sub atan(result) [
|
||||
return asin(result) * acos(result)
|
||||
return
|
||||
]
|
||||
+3
-2
@@ -8,11 +8,12 @@ type opperator struct {
|
||||
|
||||
type variable struct {
|
||||
variable string
|
||||
splices []any
|
||||
line int
|
||||
}
|
||||
|
||||
type funcCallType struct {
|
||||
name string
|
||||
name variable
|
||||
args []any
|
||||
line int
|
||||
}
|
||||
@@ -48,7 +49,7 @@ type ifstatement struct {
|
||||
|
||||
type setVariable struct {
|
||||
TYPE string
|
||||
variable string
|
||||
variable variable
|
||||
value any
|
||||
line int
|
||||
}
|
||||
|
||||
+5
-5
@@ -1,5 +1,5 @@
|
||||
try [
|
||||
error "wow"
|
||||
] catch [
|
||||
log('lol, error:', err)
|
||||
]
|
||||
from 'maths/trigonometry' import tan, atan
|
||||
|
||||
x = tan(19)
|
||||
y = atan(x)
|
||||
log(x, tan(y), y)
|
||||
@@ -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) {
|
||||
translated, err := translate(str)
|
||||
fmt.Println(origin, ":", translated)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -124,7 +124,10 @@ func runprocess(codeseg any, origin string, vargroups []map[string]variableValue
|
||||
return resp, ty
|
||||
case whileLoop:
|
||||
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{})
|
||||
for boolean(resp) {
|
||||
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" {
|
||||
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
|
||||
case ifstatement:
|
||||
vari := append(vargroups, map[string]variableValue{})
|
||||
iff := codeseg
|
||||
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) {
|
||||
ty, val, _ := run(iff.statments[i].code, origin, vari)
|
||||
return val, ty
|
||||
@@ -151,7 +160,10 @@ func runprocess(codeseg any, origin string, vargroups []map[string]variableValue
|
||||
ty, val, _ := run(iff.FALSE, origin, vari)
|
||||
return val, ty
|
||||
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))
|
||||
if err != nil {
|
||||
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) {
|
||||
var variables map[string]variableValue
|
||||
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]
|
||||
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
|
||||
for variables[callable.name].TYPE != "func" && variables[callable.name].TYPE != "init_function" && fmt.Sprint(reflect.TypeOf(variables[callable.name].VAL)) == "main.variable" {
|
||||
callable = funcCallType{name: variables[callable.name].VAL.(variable).variable, args: callable.args, line: callable.line}
|
||||
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.variable].VAL.(variable), args: callable.args, line: callable.line}
|
||||
}
|
||||
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]
|
||||
break
|
||||
}
|
||||
}
|
||||
if variables[callable.name].TYPE != "func" && variables[callable.name].TYPE != "init_function" {
|
||||
return ("'" + call.name + "' is not a function: " + origin + ":" + fmt.Sprint(call.line+1)), "error"
|
||||
if variables[callable.name.variable].TYPE != "func" && variables[callable.name.variable].TYPE != "init_function" {
|
||||
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{}
|
||||
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)
|
||||
}
|
||||
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 {
|
||||
return err, "error"
|
||||
}
|
||||
@@ -241,8 +254,11 @@ func callFunc(call funcCallType, vargroups []map[string]variableValue, origin st
|
||||
} else {
|
||||
argvars := make(map[string]variableValue)
|
||||
for i := 0; i < len(callable.args); i++ {
|
||||
resp, _ := runprocess(callable.args[i], origin, vargroups)
|
||||
name := variables[callable.name].VAL.(setFunction).args[i]
|
||||
resp, ty := runprocess(callable.args[i], origin, vargroups)
|
||||
if ty == "error" {
|
||||
return resp, ty
|
||||
}
|
||||
name := variables[callable.name.variable].VAL.(setFunction).args[i]
|
||||
argvars[name] = variableValue{
|
||||
VAL: resp,
|
||||
TYPE: "var",
|
||||
@@ -250,8 +266,8 @@ func callFunc(call funcCallType, vargroups []map[string]variableValue, origin st
|
||||
FUNC: false,
|
||||
}
|
||||
}
|
||||
ty, val, _ := run(variables[callable.name].VAL.(setFunction).code, origin, append(vargroups, modules[variables[callable.name].origin], argvars))
|
||||
if ty != "return" && ty != nil {
|
||||
ty, val, _ := run(variables[callable.name.variable].VAL.(setFunction).code, origin, append(vargroups, modules[variables[callable.name.variable].origin], argvars))
|
||||
if ty != "return" && ty != "error" && ty != nil {
|
||||
return fmt.Sprint(ty) + " is not allowed in function: " + origin + ":" + fmt.Sprint(callable.line+1), "error"
|
||||
}
|
||||
return val, "value"
|
||||
@@ -262,7 +278,7 @@ func setVariableVal(x setVariable, vargroups [](map[string]variableValue), origi
|
||||
var variable map[string]variableValue = nil
|
||||
if x.TYPE == "preset" {
|
||||
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]
|
||||
break
|
||||
}
|
||||
@@ -270,34 +286,40 @@ func setVariableVal(x setVariable, vargroups [](map[string]variableValue), origi
|
||||
} else {
|
||||
variable = vargroups[len(vargroups)-1]
|
||||
}
|
||||
if variable[x.variable].EXISTS == nil {
|
||||
resp, _ := runop(x.value, origin, vargroups)
|
||||
if variable[x.variable.variable].EXISTS == nil {
|
||||
resp, ty := runop(x.value, origin, vargroups)
|
||||
if ty == "error" {
|
||||
return resp
|
||||
}
|
||||
var TYPE = x.TYPE
|
||||
if TYPE == "preset" {
|
||||
TYPE = "var"
|
||||
}
|
||||
vargroups[len(vargroups)-1][x.variable] = variableValue{
|
||||
vargroups[len(vargroups)-1][x.variable.variable] = variableValue{
|
||||
TYPE: TYPE,
|
||||
EXISTS: true,
|
||||
VAL: resp,
|
||||
origin: origin,
|
||||
FUNC: false,
|
||||
}
|
||||
} else if variable[x.variable].TYPE == "var" {
|
||||
resp, _ := runop(x.value, origin, vargroups)
|
||||
} else if variable[x.variable.variable].TYPE == "var" {
|
||||
resp, ty := runop(x.value, origin, vargroups)
|
||||
if ty == "error" {
|
||||
return resp
|
||||
}
|
||||
var TYPE = x.TYPE
|
||||
if TYPE == "preset" {
|
||||
TYPE = "var"
|
||||
}
|
||||
variable[x.variable] = variableValue{
|
||||
variable[x.variable.variable] = variableValue{
|
||||
TYPE: TYPE,
|
||||
EXISTS: variable[x.variable].EXISTS,
|
||||
EXISTS: variable[x.variable.variable].EXISTS,
|
||||
VAL: resp,
|
||||
origin: origin,
|
||||
FUNC: variable[x.variable].FUNC,
|
||||
FUNC: variable[x.variable.variable].FUNC,
|
||||
}
|
||||
} 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
|
||||
}
|
||||
@@ -394,7 +416,10 @@ opperationloop:
|
||||
for i := 0; i < len(opperation.vals); i++ {
|
||||
switch opperation.t {
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
@@ -406,27 +431,39 @@ opperationloop:
|
||||
}
|
||||
}
|
||||
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) {
|
||||
output = x
|
||||
break opperationloop
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
output = xiny(output, x.([]any))
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
output = !xiny(output, x.([]interface{}))
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
@@ -442,7 +479,10 @@ opperationloop:
|
||||
output = (out <= in)
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
@@ -458,7 +498,10 @@ opperationloop:
|
||||
output = (out >= in)
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
@@ -474,7 +517,10 @@ opperationloop:
|
||||
output = (out < in)
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
@@ -490,21 +536,30 @@ opperationloop:
|
||||
output = (out > in)
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
output = (output != x)
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
output = (output == x)
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
@@ -520,7 +575,10 @@ opperationloop:
|
||||
output = (out - in)
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
@@ -531,7 +589,10 @@ opperationloop:
|
||||
output = val
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
@@ -547,7 +608,10 @@ opperationloop:
|
||||
output = (out * in)
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
@@ -563,7 +627,10 @@ opperationloop:
|
||||
output = math.Floor(out / in)
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
@@ -579,7 +646,10 @@ opperationloop:
|
||||
output = math.Mod(out, in)
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
@@ -595,7 +665,10 @@ opperationloop:
|
||||
output = (out / in)
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
@@ -611,7 +684,10 @@ opperationloop:
|
||||
output = math.Pow(out, 1/in)
|
||||
}
|
||||
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 {
|
||||
output = x
|
||||
} else {
|
||||
|
||||
@@ -23,7 +23,7 @@ func shell() {
|
||||
val, _ := ArgonInput("... ")
|
||||
tempstr = val.(string)
|
||||
}
|
||||
if openCompile.MatchString(tempstr) && !setVariableCompile.MatchString(tempstr) {
|
||||
if opensIndent(tempstr) {
|
||||
indent--
|
||||
} else if closeCompile.MatchString(tempstr) {
|
||||
indent++
|
||||
|
||||
@@ -19,3 +19,7 @@ incompleted:
|
||||
- books (objects)
|
||||
- add del to delete variables
|
||||
- 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
@@ -8,22 +8,22 @@ import (
|
||||
|
||||
var anyAndNewline = "((.)|(\\n))"
|
||||
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 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 subCompile = makeRegex("( *)(sub( )+" + variableOnly + "\\(" + anyAndNewline + "*\\)( )+\\[" + anyAndNewline + "*)( *)")
|
||||
var ifCompile = makeRegex("( *)(if( )+" + anyAndNewline + "+( )+\\[" + anyAndNewline + "*)( *)")
|
||||
var elseifCompile = makeRegex("( *)(\\]( )+else( )+if( )+" + anyAndNewline + "+( )+\\[" + anyAndNewline + "*)( *)")
|
||||
var openCompile = makeRegex("( *)((.)+( )+" + anyAndNewline + "+(\\(" + anyAndNewline + "+\\))?( )+\\[" + anyAndNewline + "*)")
|
||||
var elseCompile = makeRegex("( *)\\]( )+else( )+\\[" + anyAndNewline + "*( *)")
|
||||
var closeCompile = makeRegex("( *)\\]( *)")
|
||||
var switchCloseCompile = makeRegex("( *)" + anyAndNewline + "*\\]" + anyAndNewline + "*\\[" + anyAndNewline + "*( *)")
|
||||
var importCompile = makeRegex("( *)((import " + anyAndNewline + "+)|(from( +)" + anyAndNewline + "+( +)import( +)(( *)" + variableOnly + "( *)(\\,( *)" + variableOnly + "( *))*)))( *)")
|
||||
var bracketsCompile = makeRegex("( *)\\(" + anyAndNewline + "*\\)( *)")
|
||||
var functionCompile = makeRegex("( *)" + variableOnly + "\\(" + anyAndNewline + "*\\)( *)")
|
||||
var variableTextCompile = makeRegex("( *)" + variableOnly + "( *)")
|
||||
var variableTextCompile = makeRegex("( *)" + variableWithIndexes + "( *)")
|
||||
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 returnStatementCompile = makeRegex("( *)(return(( )+" + anyAndNewline + "*)?)( *)")
|
||||
var itemsCompiled = makeRegex("( *)(\\[" + anyAndNewline + "*\\])( *)")
|
||||
@@ -32,6 +32,7 @@ var continueStatementCompile = makeRegex("( *)continue( *)")
|
||||
var skipCompile = makeRegex("( *)(#(.*))?")
|
||||
var tryCompile = makeRegex("( *)(try( )+\\[" + anyAndNewline + "*)( *)")
|
||||
var catchCompile = makeRegex("( *)(\\]( )+catch( )+\\[" + anyAndNewline + "*)( *)")
|
||||
var negetiveCompile = makeRegex("( *)(\\-" + anyAndNewline + "+)( *)")
|
||||
var processfunc func(codeseg code) (any, bool, any)
|
||||
var linefunc func(i int, codearray []code) (any, int, any)
|
||||
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() {
|
||||
processfunc = translateprocess
|
||||
linefunc = translateline
|
||||
@@ -199,10 +204,11 @@ func unescape(str string) string {
|
||||
output = append(output, 39)
|
||||
case 92:
|
||||
output = append(output, 92)
|
||||
case 117:
|
||||
if i+4 < stringlen {
|
||||
i += 4
|
||||
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])))
|
||||
case 85:
|
||||
val, err := strconv.Unquote(`"\U` + str[i+1:i+9] + `"`)
|
||||
if err == nil {
|
||||
output = append(output, val...)
|
||||
i += 8
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -280,14 +286,14 @@ var getCodeInIndent = func(i int, codearray []code, TYPE string) ([]code, int, a
|
||||
i++
|
||||
for {
|
||||
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))) {
|
||||
indent--
|
||||
if indent < 0 {
|
||||
break
|
||||
}
|
||||
} else if openCompile.MatchString(codearray[i].code) && !switchCloseCompile.MatchString(codearray[i].code) {
|
||||
} else if opensIndent(codearray[i].code) {
|
||||
indent++
|
||||
}
|
||||
result = append(result, codearray[i])
|
||||
@@ -306,13 +312,10 @@ var translateprocess = func(codeseg code) (any, bool, any) {
|
||||
current := 0
|
||||
for k := 0; k < len(split); k++ {
|
||||
currentstr := strings.Join(split[current:k], opperators[i][j])
|
||||
val, worked, err := processfunc(code{
|
||||
val, worked, _ := processfunc(code{
|
||||
code: currentstr,
|
||||
line: codeseg.line,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if worked {
|
||||
vals = append(vals, val)
|
||||
current = k
|
||||
@@ -335,8 +338,6 @@ var translateprocess = func(codeseg code) (any, bool, any) {
|
||||
vals: vals,
|
||||
line: codeseg.line,
|
||||
}, 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) {
|
||||
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) {
|
||||
str := strings.Trim(codeseg.code, " ")
|
||||
bracketsplit := strings.SplitN(str, "(", 2)
|
||||
@@ -355,7 +373,11 @@ var translateprocess = func(codeseg code) (any, bool, any) {
|
||||
}
|
||||
if worked {
|
||||
return funcCallType{
|
||||
name: funcname,
|
||||
name: variable{
|
||||
variable: funcname,
|
||||
splices: []any{},
|
||||
line: codeseg.line,
|
||||
},
|
||||
args: paramstranslated,
|
||||
line: codeseg.line,
|
||||
}, true, nil
|
||||
@@ -389,6 +411,7 @@ var translateprocess = func(codeseg code) (any, bool, any) {
|
||||
} else if variableTextCompile.MatchString(codeseg.code) {
|
||||
return variable{
|
||||
variable: strings.Trim(codeseg.code, " "),
|
||||
splices: []any{},
|
||||
line: codeseg.line,
|
||||
}, true, nil
|
||||
}
|
||||
@@ -436,7 +459,7 @@ var translateline = func(i int, codearray []code) (any, int, any) {
|
||||
if len(firstSplit) > 1 {
|
||||
TYPE = firstSplit[0]
|
||||
}
|
||||
variable := firstSplit[len(firstSplit)-1]
|
||||
vari := firstSplit[len(firstSplit)-1]
|
||||
resp, worked, err := translateprocess(code{code: VAR[1],
|
||||
line: codeseg.line})
|
||||
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 setVariable{
|
||||
TYPE: TYPE,
|
||||
variable: variable,
|
||||
value: resp,
|
||||
line: codeseg.line,
|
||||
TYPE: TYPE,
|
||||
variable: variable{
|
||||
variable: vari,
|
||||
splices: []any{},
|
||||
line: codeseg.line,
|
||||
},
|
||||
value: resp,
|
||||
line: codeseg.line,
|
||||
}, i + 1, nil
|
||||
} else if whileCompile.MatchString(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
|
||||
}
|
||||
i = x
|
||||
codeoutput = append(codeoutput, resp)
|
||||
|
||||
if resp != nil {
|
||||
codeoutput = append(codeoutput, resp)
|
||||
}
|
||||
}
|
||||
|
||||
return whileLoop{
|
||||
@@ -498,7 +528,10 @@ var translateline = func(i int, codearray []code) (any, int, any) {
|
||||
return nil, 0, err
|
||||
}
|
||||
i = x
|
||||
codeoutput = append(codeoutput, resp)
|
||||
|
||||
if resp != nil {
|
||||
codeoutput = append(codeoutput, resp)
|
||||
}
|
||||
}
|
||||
catchdata, x, err := getCodeInIndent(x-1, codearray, "none")
|
||||
if err != nil {
|
||||
@@ -512,7 +545,10 @@ var translateline = func(i int, codearray []code) (any, int, any) {
|
||||
return nil, 0, err
|
||||
}
|
||||
i = x
|
||||
catchoutput = append(catchoutput, resp)
|
||||
|
||||
if resp != nil {
|
||||
catchoutput = append(catchoutput, resp)
|
||||
}
|
||||
}
|
||||
return tryType{
|
||||
code: codeoutput,
|
||||
@@ -545,7 +581,9 @@ var translateline = func(i int, codearray []code) (any, int, any) {
|
||||
return nil, 0, err
|
||||
}
|
||||
i = x
|
||||
codeoutput = append(codeoutput, resp)
|
||||
if resp != nil {
|
||||
codeoutput = append(codeoutput, resp)
|
||||
}
|
||||
}
|
||||
ifstatementcode := []iftype{
|
||||
{
|
||||
@@ -579,7 +617,9 @@ var translateline = func(i int, codearray []code) (any, int, any) {
|
||||
return nil, 0, err
|
||||
}
|
||||
i = x
|
||||
codeoutput = append(codeoutput, resp)
|
||||
if resp != nil {
|
||||
codeoutput = append(codeoutput, resp)
|
||||
}
|
||||
}
|
||||
ifstatementcode = append(ifstatementcode,
|
||||
iftype{
|
||||
@@ -603,7 +643,9 @@ var translateline = func(i int, codearray []code) (any, int, any) {
|
||||
return nil, 0, err
|
||||
}
|
||||
i = x
|
||||
elsecode = append(elsecode, resp)
|
||||
if resp != nil {
|
||||
elsecode = append(elsecode, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ifstatement{
|
||||
@@ -635,7 +677,9 @@ var translateline = func(i int, codearray []code) (any, int, any) {
|
||||
return nil, 0, err
|
||||
}
|
||||
i = x
|
||||
codeoutput = append(codeoutput, resp)
|
||||
if resp != nil {
|
||||
codeoutput = append(codeoutput, resp)
|
||||
}
|
||||
}
|
||||
return setFunction{
|
||||
name: functionname,
|
||||
@@ -647,29 +691,36 @@ var translateline = func(i int, codearray []code) (any, int, any) {
|
||||
var toImport any = nil
|
||||
var path any
|
||||
if strings.HasPrefix(str, "import") {
|
||||
p, worked, err := processfunc(code{code: str[7:], line: codeseg.line})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
path = p
|
||||
if !worked {
|
||||
return nil, 0, "invalid import path on line " + fmt.Sprint(codeseg.line+1)
|
||||
if variableonlyCompile.MatchString(strings.Trim(str[7:], " ")) {
|
||||
path = strings.Trim(str[7:], " ")
|
||||
} else {
|
||||
p, worked, err := processfunc(code{code: str[7:], line: codeseg.line})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
path = p
|
||||
if !worked {
|
||||
return nil, 0, "invalid import path on line " + fmt.Sprint(codeseg.line+1)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
info := strings.SplitN(str[5:], " import ", 2)
|
||||
p, worked, err := processfunc(code{code: info[0], line: codeseg.line})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
path = p
|
||||
if !worked {
|
||||
return nil, 0, "invalid import path on line " + fmt.Sprint(codeseg.line+1)
|
||||
if variableonlyCompile.MatchString(strings.Trim(info[0], " ")) {
|
||||
path = strings.Trim(info[0], " ")
|
||||
} else {
|
||||
p, worked, err := processfunc(code{code: info[0], line: codeseg.line})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
path = p
|
||||
if !worked {
|
||||
return nil, 0, "invalid import path on line " + fmt.Sprint(codeseg.line+1)
|
||||
}
|
||||
}
|
||||
toImport, err = getParamNames(info[1], codeseg.line)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
}
|
||||
return importType{
|
||||
path: path,
|
||||
@@ -706,7 +757,9 @@ var translate = func(str string) ([]any, any) {
|
||||
return nil, err
|
||||
}
|
||||
i = x
|
||||
output = append(output, resp)
|
||||
if resp != nil {
|
||||
output = append(output, resp)
|
||||
}
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
Reference in new issue
Block a user