mirror of
https://github.com/Open-Argon/argon-v3.git
synced 2025-12-06 08:56:07 +00:00
add functions and variables name
This commit is contained in:
76
src/call.go
Normal file
76
src/call.go
Normal 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}
|
||||
}
|
||||
Reference in New Issue
Block a user