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:
56
src/variable.go
Normal file
56
src/variable.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var variableCompile = makeRegex(`([a-zA-Z_])([a-zA-Z0-9_])*`)
|
||||
|
||||
var blockedVariableNames = map[string]bool{
|
||||
"if": true,
|
||||
"else": true,
|
||||
"elif": true,
|
||||
"while": true,
|
||||
"for": true,
|
||||
"break": true,
|
||||
"continue": true,
|
||||
"return": true,
|
||||
"let": true,
|
||||
"import": true,
|
||||
"from": true,
|
||||
"do": true,
|
||||
}
|
||||
|
||||
type variableValue struct {
|
||||
VAL any
|
||||
EXISTS any
|
||||
origin string
|
||||
}
|
||||
|
||||
type accessVariable struct {
|
||||
name string
|
||||
line int
|
||||
code string
|
||||
path string
|
||||
}
|
||||
|
||||
func isVariable(code UNPARSEcode) bool {
|
||||
return variableCompile.MatchString(code.code)
|
||||
}
|
||||
|
||||
func parseVariable(code UNPARSEcode) (accessVariable, bool, ArErr, int) {
|
||||
name := strings.TrimSpace(code.code)
|
||||
if blockedVariableNames[name] {
|
||||
return accessVariable{}, false, ArErr{"Naming Error", "Naming Error: \"" + name + "\" is a reserved keyword", code.line, code.path, code.realcode, true}, 1
|
||||
}
|
||||
return accessVariable{name: name, code: code.code, line: code.line}, true, ArErr{}, 1
|
||||
}
|
||||
|
||||
func readVariable(v accessVariable, stack []map[string]variableValue) (any, ArErr) {
|
||||
for i := len(stack) - 1; i >= 0; i-- {
|
||||
if val, ok := stack[i][v.name]; ok {
|
||||
return val.VAL, ArErr{}
|
||||
}
|
||||
}
|
||||
return nil, ArErr{"Runtime Error", "variable \"" + v.name + "\" does not exist", v.line, v.path, v.code, true}
|
||||
}
|
||||
Reference in New Issue
Block a user