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
}
switch x := val.(type) {
case PassBackJumpStatment:
case Return:
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)+`)
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

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)
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")

View File

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

View File

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