about to start implimentation of if statments, and add not

This commit is contained in:
2023-03-11 15:26:25 +00:00
parent 3a449dec63
commit 27a1abe160
8 changed files with 65 additions and 19 deletions

View File

@@ -56,7 +56,7 @@ func runDoWrap(d dowrap, stack stack, stacklevel int) (any, ArErr) {
return nil, err return nil, err
} }
switch x := val.(type) { switch x := val.(type) {
case PassBackJumpStatment: case Return:
return x, ArErr{} return x, ArErr{}
} }
} }

1
src/ifstatment.go Normal file
View File

@@ -0,0 +1 @@
package main

View File

@@ -4,16 +4,14 @@ import "strings"
var returnCompile = makeRegex(`( *)return( +)(.|\n)+`) var returnCompile = makeRegex(`( *)return( +)(.|\n)+`)
type CallJumpStatment struct { type CallReturn struct {
TYPE string
value any value any
line int line int
code string code string
path string path string
} }
type PassBackJumpStatment struct { type Return struct {
TYPE string
value any value any
line int line int
code string code string
@@ -24,15 +22,14 @@ func isReturn(code UNPARSEcode) bool {
return returnCompile.MatchString(code.code) 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{ resp, worked, err, i := translateVal(UNPARSEcode{
code: strings.TrimSpace(code.code)[6:], code: strings.TrimSpace(code.code)[6:],
realcode: code.realcode, realcode: code.realcode,
line: code.line, line: code.line,
path: code.path, path: code.path,
}, index, codeline, 1) }, index, codeline, 1)
return CallJumpStatment{ return CallReturn{
TYPE: "return",
value: resp, value: resp,
line: code.line, line: code.line,
code: code.realcode, code: code.realcode,
@@ -40,7 +37,7 @@ func parseReturn(code UNPARSEcode, index int, codeline []UNPARSEcode) (CallJumpS
}, worked, err, i }, 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 var val any
if code.value != nil { if code.value != nil {
v, err := runVal(code.value, stack, stacklevel+1) v, err := runVal(code.value, stack, stacklevel+1)
@@ -49,8 +46,7 @@ func runJumpStatment(code CallJumpStatment, stack stack, stacklevel int) (any, A
} }
val = v val = v
} }
return PassBackJumpStatment{ return Return{
TYPE: code.TYPE,
value: val, value: val,
line: code.line, line: code.line,
code: code.code, code: code.code,
@@ -60,7 +56,7 @@ func runJumpStatment(code CallJumpStatment, stack stack, stacklevel int) (any, A
func openJump(resp any) any { func openJump(resp any) any {
switch x := resp.(type) { switch x := resp.(type) {
case PassBackJumpStatment: case Return:
return x.value return x.value
default: default:
return resp return resp

40
src/not.go Normal file
View File

@@ -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
}

View File

@@ -55,10 +55,12 @@ func runVal(line any, stack stack, stacklevel int) (any, ArErr) {
return runOperation(x, stack, stacklevel+1) return runOperation(x, stack, stacklevel+1)
case dowrap: case dowrap:
return runDoWrap(x, stack, stacklevel+1) return runDoWrap(x, stack, stacklevel+1)
case CallJumpStatment: case CallReturn:
return runJumpStatment(x, stack, stacklevel+1) return runReturn(x, stack, stacklevel+1)
case ArDelete: case ArDelete:
return runDelete(x, stack, stacklevel+1) return runDelete(x, stack, stacklevel+1)
case not:
return runNot(x, stack, stacklevel+1)
} }
fmt.Println("unreachable", reflect.TypeOf(line)) fmt.Println("unreachable", reflect.TypeOf(line))
panic("unreachable") panic("unreachable")

View File

@@ -40,6 +40,8 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i
if worked { if worked {
return bracket, worked, err, step return bracket, worked, err, step
} }
} else if isnot(code) {
return parseNot(code, index, codelines, isLine)
} }
if isSetVariable(code) { if isSetVariable(code) {
setvar, worked, err, step := parseSetVariable(code, index, codelines, isLine) 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) val, _, err, step := translateVal(codelines[i], i, codelines, 2)
switch val.(type) { 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} return nil, ArErr{"Runtime Error", "Jump statment at top level", codelines[i].line, codelines[i].path, codelines[i].realcode, true}
} }
i += step i += step

View File

@@ -27,6 +27,9 @@ var blockedVariableNames = map[string]bool{
"false": true, "false": true,
"null": true, "null": true,
"delete": true, "delete": true,
"not": true,
"and": true,
"or": true,
} }
type accessVariable struct { type accessVariable struct {

10
test.ar
View File

@@ -1,13 +1,15 @@
let ln(x) = do let maths = map()
maths.ln(x) = do
let n = 1e10 let n = 1e10
return n * ((x^(1/n)) - 1) return n * ((x^(1/n)) - 1)
let __ln10cache = ln(10) let __ln10cache = ln(10)
let log(x) = do maths.log(x) = do
return ln(x) / __ln10cache return ln(x) / __ln10cache
let logN(n,x) = do maths.logN(n,x) = do
return ln(x) / ln(n) return ln(x) / ln(n)
term.log(log(1000)) term.log(maths.log(10000), array(maths))