impliment do wraps

This commit is contained in:
2023-03-11 01:43:37 +00:00
parent 6dcb996f61
commit 597be4e0df
15 changed files with 193 additions and 64 deletions

View File

@@ -24,7 +24,7 @@ func parseBrackets(code UNPARSEcode, index int, codeline []UNPARSEcode) (bracket
realcode: code.realcode, realcode: code.realcode,
line: code.line, line: code.line,
path: code.path, path: code.path,
}, index, codeline, false) }, index, codeline, 0)
return brackets{ return brackets{
VAL: resp, VAL: resp,
line: code.line, line: code.line,

View File

@@ -1,7 +1,5 @@
package main package main
import "github.com/wadey/go-rounding"
var vars = scope{} var vars = scope{}
func init() { func init() {
@@ -67,35 +65,10 @@ func init() {
} }
return nil, ArErr{TYPE: "TypeError", message: "Cannot create array from '" + typeof(a[0]) + "'", EXISTS: true} return nil, ArErr{TYPE: "TypeError", message: "Cannot create array from '" + typeof(a[0]) + "'", EXISTS: true}
}} }}
vars["round"] = builtinFunc{"round", func(a ...any) (any, ArErr) { vars["maths"] = maths
if len(a) == 0 {
return nil, ArErr{TYPE: "round", message: "round takes 1 argument",
EXISTS: true}
}
precision := newNumber()
if len(a) > 1 {
switch x := a[1].(type) {
case number:
if !x.IsInt() {
return nil, ArErr{TYPE: "TypeError", message: "Cannot round to '" + typeof(a[1]) + "'", EXISTS: true}
}
precision = x
default:
return nil, ArErr{TYPE: "TypeError", message: "Cannot round to '" + typeof(a[1]) + "'", EXISTS: true}
}
}
switch x := a[0].(type) {
case number:
return rounding.Round(newNumber().Set(x), int(precision.Num().Int64()), rounding.HalfUp), ArErr{}
}
return nil, ArErr{TYPE: "TypeError", message: "Cannot round '" + typeof(a[0]) + "'", EXISTS: true}
}}
vars["time"] = ArTime vars["time"] = ArTime
vars["PI"] = PI vars["PI"] = PI
vars["π"] = PI vars["π"] = PI
vars["e"] = e vars["e"] = e
sqrt := builtinFunc{"sqrt", ArgonSqrt}
vars["sqrt"] = sqrt
vars["thread"] = builtinFunc{"thread", ArThread} vars["thread"] = builtinFunc{"thread", ArThread}
} }

View File

@@ -38,7 +38,7 @@ func parseCall(code UNPARSEcode, index int, codelines []UNPARSEcode) (any, bool,
} }
continue continue
} }
resp, worked, _, _ := translateVal(UNPARSEcode{code: name, realcode: code.realcode, line: index + 1, path: code.path}, index, codelines, false) resp, worked, _, _ := translateVal(UNPARSEcode{code: name, realcode: code.realcode, line: index + 1, path: code.path}, index, codelines, 0)
if !worked { if !worked {
if i == len(splitby)-1 { if i == len(splitby)-1 {
return nil, false, ArErr{"Syntax Error", "invalid callable", code.line, code.path, code.realcode, true}, 1 return nil, false, ArErr{"Syntax Error", "invalid callable", code.line, code.path, code.realcode, true}, 1
@@ -94,6 +94,10 @@ func runCall(c call, stack stack) (any, ArErr) {
level[param] = args[i] level[param] = args[i]
} }
resp, err := runVal(x.run, append(x.stack, level)) resp, err := runVal(x.run, append(x.stack, level))
switch x := resp.(type) {
case PassBackJumpStatment:
resp = x.value
}
return resp, err return resp, err
} }
return nil, ArErr{"Runtime Error", "type '" + typeof(callable) + "' is not callable", c.line, c.path, c.code, true} return nil, ArErr{"Runtime Error", "type '" + typeof(callable) + "' is not callable", c.line, c.path, c.code, true}

View File

@@ -21,7 +21,7 @@ func parseComment(code UNPARSEcode, index int, codelines []UNPARSEcode) (any, bo
for i := 0; i < len(split)-1; i++ { for i := 0; i < len(split)-1; i++ {
temp = append(temp, split[i]) temp = append(temp, split[i])
joined := strings.Join(temp, "#") joined := strings.Join(temp, "#")
resp, worked, _, s := translateVal(UNPARSEcode{code: joined, realcode: code.realcode, line: code.line, path: code.path}, index, codelines, true) resp, worked, _, s := translateVal(UNPARSEcode{code: joined, realcode: code.realcode, line: code.line, path: code.path}, index, codelines, 0)
step += s - 1 step += s - 1
if worked { if worked {
return resp, true, ArErr{}, step return resp, true, ArErr{}, step

64
src/dowraps.go Normal file
View File

@@ -0,0 +1,64 @@
package main
import (
"strings"
)
var dowrapCompile = makeRegex("( )*do( )*")
type dowrap struct {
run []any
line int
path string
code string
}
func isDoWrap(code UNPARSEcode) bool {
return dowrapCompile.MatchString(code.code)
}
func parseDoWrap(code UNPARSEcode, index int, codelines []UNPARSEcode) (any, bool, ArErr, int) {
currentindent := len(code.code) - len(strings.TrimLeft(code.code, " "))
var setindent int = -1
var i = index + 1
translated := []any{}
for i < len(codelines) {
if isBlank(codelines[i]) {
i++
continue
}
indent := len(codelines[i].code) - len(strings.TrimLeft(codelines[i].code, " "))
if setindent == -1 {
setindent = indent
}
if indent <= currentindent {
break
}
if indent != setindent {
return nil, false, ArErr{"Syntax Error", "invalid indent", code.line, code.path, code.realcode, true}, 1
}
val, _, err, step := translateVal(codelines[i], i, codelines, 2)
i += step
if err.EXISTS {
return nil, false, err, i - index
}
translated = append(translated, val)
}
return dowrap{run: translated, line: code.line, path: code.path, code: code.realcode}, true, ArErr{}, i - index
}
func runDoWrap(d dowrap, stack stack) (any, ArErr) {
newstack := append(stack, scope{})
for _, v := range d.run {
val, err := runVal(v, newstack)
if err.EXISTS {
return nil, err
}
switch x := val.(type) {
case PassBackJumpStatment:
return x, ArErr{}
}
}
return nil, ArErr{}
}

View File

@@ -16,7 +16,7 @@ type factorial struct {
func parseFactorial(code UNPARSEcode, index int, codeline []UNPARSEcode) (factorial, bool, ArErr, int) { func parseFactorial(code UNPARSEcode, index int, codeline []UNPARSEcode) (factorial, bool, ArErr, int) {
trim := strings.TrimSpace(code.code) trim := strings.TrimSpace(code.code)
trim = trim[:len(trim)-1] trim = trim[:len(trim)-1]
val, success, err, i := translateVal(UNPARSEcode{code: trim, realcode: code.realcode, line: 1, path: ""}, 0, []UNPARSEcode{}, false) val, success, err, i := translateVal(UNPARSEcode{code: trim, realcode: code.realcode, line: 1, path: ""}, 0, []UNPARSEcode{}, 0)
if !success { if !success {
return factorial{}, false, err, i return factorial{}, false, err, i
} }

59
src/jumpStatments.go Normal file
View File

@@ -0,0 +1,59 @@
package main
import "strings"
var returnCompile = makeRegex(`( *)return( +)(.|\n)+`)
type CallJumpStatment struct {
TYPE string
value any
line int
code string
path string
}
type PassBackJumpStatment struct {
TYPE string
value any
line int
code string
path string
}
func isReturn(code UNPARSEcode) bool {
return returnCompile.MatchString(code.code)
}
func parseReturn(code UNPARSEcode, index int, codeline []UNPARSEcode) (CallJumpStatment, 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, 0)
return CallJumpStatment{
TYPE: "return",
value: resp,
line: code.line,
code: code.realcode,
path: code.path,
}, worked, err, i
}
func runJumpStatment(code CallJumpStatment, stack stack) (any, ArErr) {
var val any
if code.value != nil {
v, err := runVal(code.value, stack)
if err.EXISTS {
return nil, err
}
val = v
}
return PassBackJumpStatment{
TYPE: code.TYPE,
value: val,
line: code.line,
code: code.code,
path: code.path,
}, ArErr{}
}

View File

@@ -18,7 +18,7 @@ func getValuesFromLetter(str string, splitstr string, index int, codelines []UNP
arguments = append(arguments, nil) arguments = append(arguments, nil)
temp = []string{} temp = []string{}
} else { } else {
resp, worked, _, _ := translateVal(UNPARSEcode{code: test, realcode: codelines[index].realcode, line: index + 1, path: codelines[index].path}, index, codelines, false) resp, worked, _, _ := translateVal(UNPARSEcode{code: test, realcode: codelines[index].realcode, line: index + 1, path: codelines[index].path}, index, codelines, 0)
if worked { if worked {
arguments = append(arguments, resp) arguments = append(arguments, resp)
temp = []string{} temp = []string{}

View File

@@ -403,7 +403,7 @@ func mapGetParse(code UNPARSEcode, index int, codelines []UNPARSEcode) (ArMapGet
split := strings.Split(trim, ".") split := strings.Split(trim, ".")
start := strings.Join(split[:len(split)-1], ".") start := strings.Join(split[:len(split)-1], ".")
key := split[len(split)-1] key := split[len(split)-1]
resp, worked, err, i := translateVal(UNPARSEcode{code: start, realcode: code.realcode, line: code.line, path: code.path}, index, codelines, false) resp, worked, err, i := translateVal(UNPARSEcode{code: start, realcode: code.realcode, line: code.line, path: code.path}, index, codelines, 0)
if !worked { if !worked {
return ArMapGet{}, false, err, i return ArMapGet{}, false, err, i
} }
@@ -444,7 +444,7 @@ func indexGetParse(code UNPARSEcode, index int, codelines []UNPARSEcode) (ArMapG
true, true,
}, 1 }, 1
} }
tival, worked, err, i := translateVal(UNPARSEcode{code: ti, realcode: code.realcode, line: code.line, path: code.path}, index, codelines, false) tival, worked, err, i := translateVal(UNPARSEcode{code: ti, realcode: code.realcode, line: code.line, path: code.path}, index, codelines, 0)
if !worked { if !worked {
if i == len(split)-1 { if i == len(split)-1 {
return ArMapGet{}, false, err, i return ArMapGet{}, false, err, i

View File

@@ -21,7 +21,7 @@ func parseNegative(code UNPARSEcode, index int, codeline []UNPARSEcode) (negativ
realcode: code.realcode, realcode: code.realcode,
line: code.line, line: code.line,
path: code.path, path: code.path,
}, index, codeline, false) }, index, codeline, 0)
return negative{ return negative{
VAL: resp, VAL: resp,
line: code.line, line: code.line,

View File

@@ -70,7 +70,7 @@ func parseOperations(code UNPARSEcode, index int, codelines []UNPARSEcode) (oper
realcode: code.realcode, realcode: code.realcode,
line: code.line, line: code.line,
path: code.path, path: code.path,
}, index, codelines, false) }, index, codelines, 0)
if success { if success {
totalindex += respindex - 1 totalindex += respindex - 1
@@ -87,7 +87,7 @@ func parseOperations(code UNPARSEcode, index int, codelines []UNPARSEcode) (oper
realcode: code.realcode, realcode: code.realcode,
line: code.line, line: code.line,
path: code.path, path: code.path,
}, index, codelines, false) }, index, codelines, 0)
if success { if success {
totalindex += respindex - 1 totalindex += respindex - 1
values = append(values, resp) values = append(values, resp)

View File

@@ -50,6 +50,10 @@ func runVal(line any, stack stack) (any, ArErr) {
return runVal(x.VAL, stack) return runVal(x.VAL, stack)
case operationType: case operationType:
return runOperation(x, stack) return runOperation(x, stack)
case dowrap:
return runDoWrap(x, stack)
case CallJumpStatment:
return runJumpStatment(x, stack)
case ArDelete: case ArDelete:
return runDelete(x, stack) return runDelete(x, stack)
} }

View File

@@ -1,5 +1,7 @@
package main package main
import "strings"
type UNPARSEcode struct { type UNPARSEcode struct {
code string code string
realcode string realcode string
@@ -8,21 +10,31 @@ type UNPARSEcode struct {
} }
// returns (number | string | nil), success, error, step // returns (number | string | nil), success, error, step
func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine bool) (any, bool, ArErr, int) { func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine int) (any, bool, ArErr, int) {
if isLine { if isLine == 2 {
if isBlank(code) { if isDeleteVariable(code) {
return nil, true, ArErr{}, 1
} else if isDeleteVariable(code) {
return parseDelete(code, index, codelines) return parseDelete(code, index, codelines)
} else if isComment(code) { } else if isComment(code) {
resp, worked, err, step := parseComment(code, index, codelines) resp, worked, err, step := parseComment(code, index, codelines)
if worked { if worked {
return resp, worked, err, step return resp, worked, err, step
} }
} else if isReturn(code) {
return parseReturn(code, index, codelines)
} }
} }
if isLine >= 1 {
if isDoWrap(code) {
return parseDoWrap(code, index, codelines)
}
}
if isLine == 2 {
isLine = 1
}
if isBrackets(code) { if isBrackets(code) {
bracket, worked, err, step := parseBrackets(code, index, codelines) bracket, worked, err, step := parseBrackets(code, index, codelines)
if worked { if worked {
@@ -30,13 +42,13 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine b
} }
} }
if isSetVariable(code) { if isSetVariable(code) {
setvar, worked, err, step := parseSetVariable(code, index, codelines) setvar, worked, err, step := parseSetVariable(code, index, codelines, isLine)
if worked { if worked {
return setvar, worked, err, step return setvar, worked, err, step
} }
} }
if isAutoAsignVariable(code) { if isAutoAsignVariable(code) {
setvar, worked, err, step := parseAutoAsignVariable(code, index, codelines) setvar, worked, err, step := parseAutoAsignVariable(code, index, codelines, isLine)
if worked { if worked {
return setvar, worked, err, step return setvar, worked, err, step
} }
@@ -75,14 +87,19 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine b
func translate(codelines []UNPARSEcode) ([]any, ArErr) { func translate(codelines []UNPARSEcode) ([]any, ArErr) {
translated := []any{} translated := []any{}
for i := 0; i < len(codelines); { for i := 0; i < len(codelines); {
val, _, err, step := translateVal(codelines[i], i, codelines, true) if isBlank(codelines[i]) {
i++
continue
}
currentindent := len(codelines[i].code) - len(strings.TrimLeft(codelines[i].code, " "))
if currentindent != 0 {
return nil, ArErr{"Syntax Error", "invalid indent", codelines[i].line, codelines[i].path, codelines[i].realcode, true}
}
val, _, err, step := translateVal(codelines[i], i, codelines, 2)
i += step i += step
if err.EXISTS { if err.EXISTS {
return nil, err return nil, err
} }
if val == nil {
continue
}
translated = append(translated, val) translated = append(translated, val)
} }
return translated, ArErr{} return translated, ArErr{}

View File

@@ -65,7 +65,7 @@ func isVariable(code UNPARSEcode) bool {
func parseVariable(code UNPARSEcode) (accessVariable, bool, ArErr, int) { func parseVariable(code UNPARSEcode) (accessVariable, bool, ArErr, int) {
name := strings.TrimSpace(code.code) name := strings.TrimSpace(code.code)
return accessVariable{name: name, code: code.code, line: code.line, path: code.path}, true, ArErr{}, 1 return accessVariable{name: name, code: code.realcode, line: code.line, path: code.path}, true, ArErr{}, 1
} }
func readVariable(v accessVariable, stack stack) (any, ArErr) { func readVariable(v accessVariable, stack stack) (any, ArErr) {
@@ -112,13 +112,13 @@ func nameToTranslated(code UNPARSEcode, index int, lines []UNPARSEcode) (any, bo
realcode: code.realcode, realcode: code.realcode,
line: code.line, line: code.line,
path: code.path, path: code.path,
}, index, lines, false) }, index, lines, 0)
return setFunction{toset: value, params: params}, success, err, i return setFunction{toset: value, params: params}, success, err, i
} }
return translateVal(code, index, lines, false) return translateVal(code, index, lines, 0)
} }
func parseSetVariable(code UNPARSEcode, index int, lines []UNPARSEcode) (setVariable, bool, ArErr, int) { func parseSetVariable(code UNPARSEcode, index int, lines []UNPARSEcode, isLine int) (setVariable, bool, ArErr, int) {
trim := strings.TrimSpace(code.code) trim := strings.TrimSpace(code.code)
equalsplit := strings.SplitN(trim, "=", 2) equalsplit := strings.SplitN(trim, "=", 2)
spacesplit := strings.SplitN(equalsplit[0], " ", 2) spacesplit := strings.SplitN(equalsplit[0], " ", 2)
@@ -128,9 +128,9 @@ func parseSetVariable(code UNPARSEcode, index int, lines []UNPARSEcode) (setVari
if blockedVariableNames[name] { if blockedVariableNames[name] {
return setVariable{}, false, ArErr{"Naming Error", "\"" + name + "\" is a reserved keyword", code.line, code.path, code.realcode, true}, 1 return setVariable{}, false, ArErr{"Naming Error", "\"" + name + "\" is a reserved keyword", code.line, code.path, code.realcode, true}, 1
} }
toset, success, err, i := nameToTranslated(UNPARSEcode{code: name, realcode: code.realcode, line: code.line, path: code.path}, index, lines) toset, success, err, namei := nameToTranslated(UNPARSEcode{code: name, realcode: code.realcode, line: code.line, path: code.path}, index, lines)
if err.EXISTS { if err.EXISTS {
return setVariable{}, success, err, i return setVariable{}, success, err, namei
} }
switch toset.(type) { switch toset.(type) {
case accessVariable: case accessVariable:
@@ -145,14 +145,14 @@ func parseSetVariable(code UNPARSEcode, index int, lines []UNPARSEcode) (setVari
default: default:
return setVariable{}, false, ArErr{"Type Error", "can't set for non variable, did you mean '=='?", code.line, code.path, code.realcode, true}, 1 return setVariable{}, false, ArErr{"Type Error", "can't set for non variable, did you mean '=='?", code.line, code.path, code.realcode, true}, 1
} }
value, success, err, i := translateVal(UNPARSEcode{code: equalsplit[1], realcode: code.realcode, line: code.line, path: code.path}, index, lines, false) value, success, err, i := translateVal(UNPARSEcode{code: equalsplit[1], realcode: code.realcode, line: code.line, path: code.path}, index, lines, isLine)
if !success { if !success {
return setVariable{}, false, err, i return setVariable{}, false, err, i
} }
return setVariable{TYPE: "let", toset: toset, value: value, function: function, params: params, line: code.line, code: code.code, path: code.path}, true, ArErr{}, i return setVariable{TYPE: "let", toset: toset, value: value, function: function, params: params, line: code.line, code: code.code, path: code.path}, true, ArErr{}, i + namei - 1
} }
func parseAutoAsignVariable(code UNPARSEcode, index int, lines []UNPARSEcode) (setVariable, bool, ArErr, int) { func parseAutoAsignVariable(code UNPARSEcode, index int, lines []UNPARSEcode, isLine int) (setVariable, bool, ArErr, int) {
trim := strings.TrimSpace(code.code) trim := strings.TrimSpace(code.code)
equalsplit := strings.SplitN(trim, "=", 2) equalsplit := strings.SplitN(trim, "=", 2)
name := strings.TrimSpace(equalsplit[0]) name := strings.TrimSpace(equalsplit[0])
@@ -161,9 +161,9 @@ func parseAutoAsignVariable(code UNPARSEcode, index int, lines []UNPARSEcode) (s
if blockedVariableNames[name] { if blockedVariableNames[name] {
return setVariable{}, false, ArErr{"Naming Error", "\"" + name + "\" is a reserved keyword", code.line, code.path, code.realcode, true}, 1 return setVariable{}, false, ArErr{"Naming Error", "\"" + name + "\" is a reserved keyword", code.line, code.path, code.realcode, true}, 1
} }
toset, success, err, i := nameToTranslated(UNPARSEcode{code: name, realcode: code.realcode, line: code.line, path: code.path}, index, lines) toset, success, err, namei := nameToTranslated(UNPARSEcode{code: name, realcode: code.realcode, line: code.line, path: code.path}, index, lines)
if err.EXISTS { if err.EXISTS {
return setVariable{}, success, err, i return setVariable{}, success, err, namei
} }
switch toset.(type) { switch toset.(type) {
case accessVariable: case accessVariable:
@@ -177,11 +177,11 @@ func parseAutoAsignVariable(code UNPARSEcode, index int, lines []UNPARSEcode) (s
default: default:
return setVariable{}, false, ArErr{"Type Error", "can't set for non variable, did you mean '=='?", code.line, code.path, code.realcode, true}, 1 return setVariable{}, false, ArErr{"Type Error", "can't set for non variable, did you mean '=='?", code.line, code.path, code.realcode, true}, 1
} }
value, success, err, i := translateVal(UNPARSEcode{code: equalsplit[1], realcode: code.realcode, line: code.line, path: code.path}, index, lines, false) value, success, err, i := translateVal(UNPARSEcode{code: equalsplit[1], realcode: code.realcode, line: code.line, path: code.path}, index, lines, isLine)
if !success { if !success {
return setVariable{}, false, err, i return setVariable{}, false, err, i
} }
return setVariable{TYPE: "auto", toset: toset, value: value, function: function, params: params, line: code.line, code: code.code, path: code.path}, true, ArErr{}, i return setVariable{TYPE: "auto", toset: toset, value: value, function: function, params: params, line: code.line, code: code.code, path: code.path}, true, ArErr{}, i + namei - 1
} }
func setVariableValue(v setVariable, stack stack) (any, ArErr) { func setVariableValue(v setVariable, stack stack) (any, ArErr) {
@@ -193,6 +193,10 @@ func setVariableValue(v setVariable, stack stack) (any, ArErr) {
if err.EXISTS { if err.EXISTS {
return nil, err return nil, err
} }
switch x := respp.(type) {
case PassBackJumpStatment:
respp = x.value
}
resp = respp resp = respp
} }
@@ -238,7 +242,7 @@ func parseDelete(code UNPARSEcode, index int, lines []UNPARSEcode) (ArDelete, bo
if blockedVariableNames[name] { if blockedVariableNames[name] {
return ArDelete{}, false, ArErr{"Naming Error", "\"" + name + "\" is a reserved keyword", code.line, code.path, code.realcode, true}, 1 return ArDelete{}, false, ArErr{"Naming Error", "\"" + name + "\" is a reserved keyword", code.line, code.path, code.realcode, true}, 1
} }
toset, success, err, i := translateVal(UNPARSEcode{code: name, realcode: code.realcode, line: code.line, path: code.path}, index, lines, false) toset, success, err, i := translateVal(UNPARSEcode{code: name, realcode: code.realcode, line: code.line, path: code.path}, index, lines, 0)
if !success { if !success {
return ArDelete{}, false, err, i return ArDelete{}, false, err, i

View File

@@ -1 +1,5 @@
range(1e10) f(x) = do
x = x + 1
x = x + 1
return x
term.log(f(1))