add functions and variables name

This commit is contained in:
2023-02-25 23:34:15 +00:00
parent 636101f1fa
commit 6ef6e051e6
23 changed files with 414 additions and 97 deletions

76
src/call.go Normal file
View File

@@ -0,0 +1,76 @@
package main
import (
"strings"
)
var callCompile = makeRegex("( *).+\\(.*\\)( *)")
type call struct {
callable any
args []any
code string
line int
}
func isCall(code UNPARSEcode) bool {
return callCompile.MatchString(code.code)
}
func parseCall(code UNPARSEcode, index int, codelines []UNPARSEcode) (any, bool, ArErr, int) {
trim := strings.TrimSpace(code.code)
trim = trim[:len(trim)-1]
splitby := strings.Split(trim, "(")
var works bool
var callable any
var arguments []any
for i := 1; i < len(splitby); i++ {
name := strings.Join(splitby[0:i], "(")
argstr := strings.Join(splitby[i:], "(")
args, success, argserr := getValuesFromCommas(argstr, index, codelines)
arguments = args
if !success {
if i == len(splitby)-1 {
return nil, false, argserr, 1
}
continue
}
resp, worked, _, _ := translateVal(UNPARSEcode{code: name, realcode: code.realcode, line: index + 1, path: code.path}, index, codelines, false)
if !worked {
if i == len(splitby)-1 {
return nil, false, ArErr{"Syntax Error", "invalid callable", code.line, code.path, code.realcode, true}, 1
}
continue
}
works = true
callable = resp
break
}
if !works {
return nil, false, ArErr{"Syntax Error", "invalid call", code.line, code.path, code.realcode, true}, 1
}
return call{callable: callable, args: arguments, line: code.line, code: code.code}, true, ArErr{}, 1
}
func runCall(c call, stack []map[string]variableValue) (any, ArErr) {
callable, err := runVal(c.callable, stack)
if err.EXISTS {
return nil, err
}
args := []any{}
for _, arg := range c.args {
resp, err := runVal(arg, stack)
if err.EXISTS {
return nil, err
}
args = append(args, resp)
}
switch x := callable.(type) {
case builtinFunc:
return x.FUNC(args...)
case Callable:
return nil, ArErr{"Runtime Error", "cannot call a class", c.line, "", c.code, true}
}
return nil, ArErr{"Runtime Error", typeof(callable) + "' is not callable", c.line, "", c.code, true}
}