From 27a1abe1603ce1e0d0b008c0351318d7893abae2 Mon Sep 17 00:00:00 2001 From: Ugric Date: Sat, 11 Mar 2023 15:26:25 +0000 Subject: [PATCH] about to start implimentation of if statments, and add not --- src/dowraps.go | 2 +- src/ifstatment.go | 1 + src/jumpStatments.go | 18 +++++++----------- src/not.go | 40 ++++++++++++++++++++++++++++++++++++++++ src/run.go | 6 ++++-- src/translate.go | 4 +++- src/variable.go | 3 +++ test.ar | 10 ++++++---- 8 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 src/ifstatment.go create mode 100644 src/not.go diff --git a/src/dowraps.go b/src/dowraps.go index 21d98da..046d7da 100644 --- a/src/dowraps.go +++ b/src/dowraps.go @@ -56,7 +56,7 @@ func runDoWrap(d dowrap, stack stack, stacklevel int) (any, ArErr) { return nil, err } switch x := val.(type) { - case PassBackJumpStatment: + case Return: return x, ArErr{} } } diff --git a/src/ifstatment.go b/src/ifstatment.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/src/ifstatment.go @@ -0,0 +1 @@ +package main diff --git a/src/jumpStatments.go b/src/jumpStatments.go index 32f670b..9261dbb 100644 --- a/src/jumpStatments.go +++ b/src/jumpStatments.go @@ -4,16 +4,14 @@ import "strings" var returnCompile = makeRegex(`( *)return( +)(.|\n)+`) -type CallJumpStatment struct { - TYPE string +type CallReturn struct { value any line int code string path string } -type PassBackJumpStatment struct { - TYPE string +type Return struct { value any line int code string @@ -24,15 +22,14 @@ func isReturn(code UNPARSEcode) bool { return returnCompile.MatchString(code.code) } -func parseReturn(code UNPARSEcode, index int, codeline []UNPARSEcode) (CallJumpStatment, bool, ArErr, int) { +func parseReturn(code UNPARSEcode, index int, codeline []UNPARSEcode) (CallReturn, bool, ArErr, int) { resp, worked, err, i := translateVal(UNPARSEcode{ code: strings.TrimSpace(code.code)[6:], realcode: code.realcode, line: code.line, path: code.path, }, index, codeline, 1) - return CallJumpStatment{ - TYPE: "return", + return CallReturn{ value: resp, line: code.line, code: code.realcode, @@ -40,7 +37,7 @@ func parseReturn(code UNPARSEcode, index int, codeline []UNPARSEcode) (CallJumpS }, worked, err, i } -func runJumpStatment(code CallJumpStatment, stack stack, stacklevel int) (any, ArErr) { +func runReturn(code CallReturn, stack stack, stacklevel int) (any, ArErr) { var val any if code.value != nil { v, err := runVal(code.value, stack, stacklevel+1) @@ -49,8 +46,7 @@ func runJumpStatment(code CallJumpStatment, stack stack, stacklevel int) (any, A } val = v } - return PassBackJumpStatment{ - TYPE: code.TYPE, + return Return{ value: val, line: code.line, code: code.code, @@ -60,7 +56,7 @@ func runJumpStatment(code CallJumpStatment, stack stack, stacklevel int) (any, A func openJump(resp any) any { switch x := resp.(type) { - case PassBackJumpStatment: + case Return: return x.value default: return resp diff --git a/src/not.go b/src/not.go new file mode 100644 index 0000000..1f2bee9 --- /dev/null +++ b/src/not.go @@ -0,0 +1,40 @@ +package main + +import "strings" + +var notCompiled = makeRegex(`( *)not (.|\n)+`) + +type not struct { + value any + line int + code string + path string +} + +func isnot(code UNPARSEcode) bool { + return notCompiled.MatchString(code.code) +} + +func parseNot(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine int) (any, bool, ArErr, int) { + trimmed := strings.TrimSpace(code.code) + trimmed = trimmed[4:] + + val, worked, err, step := translateVal(UNPARSEcode{ + code: trimmed, + realcode: code.realcode, + line: code.line, + path: code.path, + }, index, codelines, isLine) + return not{ + value: val, + line: code.line, + code: code.realcode, + path: code.path, + }, worked, err, step +} + +func runNot(n not, stack stack, stacklevel int) (bool, ArErr) { + val, err := runVal(n.value, stack, stacklevel+1) + boolean := !anyToBool(val) + return boolean, err +} diff --git a/src/run.go b/src/run.go index cd5cf2e..73bda09 100644 --- a/src/run.go +++ b/src/run.go @@ -55,10 +55,12 @@ func runVal(line any, stack stack, stacklevel int) (any, ArErr) { return runOperation(x, stack, stacklevel+1) case dowrap: return runDoWrap(x, stack, stacklevel+1) - case CallJumpStatment: - return runJumpStatment(x, stack, stacklevel+1) + case CallReturn: + return runReturn(x, stack, stacklevel+1) case ArDelete: return runDelete(x, stack, stacklevel+1) + case not: + return runNot(x, stack, stacklevel+1) } fmt.Println("unreachable", reflect.TypeOf(line)) panic("unreachable") diff --git a/src/translate.go b/src/translate.go index beb8a9e..d749a7d 100644 --- a/src/translate.go +++ b/src/translate.go @@ -40,6 +40,8 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i if worked { return bracket, worked, err, step } + } else if isnot(code) { + return parseNot(code, index, codelines, isLine) } if isSetVariable(code) { setvar, worked, err, step := parseSetVariable(code, index, codelines, isLine) @@ -97,7 +99,7 @@ func translate(codelines []UNPARSEcode) ([]any, ArErr) { } val, _, err, step := translateVal(codelines[i], i, codelines, 2) switch val.(type) { - case CallJumpStatment: + case CallReturn: return nil, ArErr{"Runtime Error", "Jump statment at top level", codelines[i].line, codelines[i].path, codelines[i].realcode, true} } i += step diff --git a/src/variable.go b/src/variable.go index c257b30..9f51019 100644 --- a/src/variable.go +++ b/src/variable.go @@ -27,6 +27,9 @@ var blockedVariableNames = map[string]bool{ "false": true, "null": true, "delete": true, + "not": true, + "and": true, + "or": true, } type accessVariable struct { diff --git a/test.ar b/test.ar index ecab21b..4ab8c54 100644 --- a/test.ar +++ b/test.ar @@ -1,13 +1,15 @@ -let ln(x) = do +let maths = map() + +maths.ln(x) = do let n = 1e10 return n * ((x^(1/n)) - 1) let __ln10cache = ln(10) -let log(x) = do +maths.log(x) = do return ln(x) / __ln10cache -let logN(n,x) = do +maths.logN(n,x) = do return ln(x) / ln(n) -term.log(log(1000)) \ No newline at end of file +term.log(maths.log(10000), array(maths)) \ No newline at end of file