From ec53db45d5f7e3db3ac0e3c29a2180117a137c24 Mon Sep 17 00:00:00 2001 From: William Bell Date: Tue, 13 Jun 2023 23:18:54 +0100 Subject: [PATCH] add debug --- app.py | 46 ---------------------------------------------- run | 2 +- src/call.go | 3 +++ src/config.go | 2 +- src/debug.go | 21 +++++++++++++++++++++ src/main.go | 40 +++++++++++++++++++++++----------------- src/map.go | 47 +++++++++++++++++++++++++++++++++++++++++++++-- src/operations.go | 7 +++---- test.ar | 1 + 9 files changed, 98 insertions(+), 71 deletions(-) delete mode 100644 app.py create mode 100644 src/debug.go create mode 100644 test.ar diff --git a/app.py b/app.py deleted file mode 100644 index 65ede69..0000000 --- a/app.py +++ /dev/null @@ -1,46 +0,0 @@ -def interpret(code): - memory = {} - pointer = 0 - code_ptr = 0 - loops = [] - - while code_ptr < len(code): - command = code[code_ptr] - - if command == '>': - pointer += 1 - elif command == '<': - pointer -= 1 - elif command == '+': - if pointer not in memory: - memory[pointer] = 0 - memory[pointer] += 1 - elif command == '-': - if pointer not in memory: - memory[pointer] = 0 - memory[pointer] -= 1 - elif command == '.': - print(chr(memory.get(pointer, 0)), end='') - elif command == ',': - memory[pointer] = ord(input()) - elif command == '[': - if memory.get(pointer, 0) == 0: - loop_depth = 1 - while loop_depth > 0: - code_ptr += 1 - if code[code_ptr] == '[': - loop_depth += 1 - elif code[code_ptr] == ']': - loop_depth -= 1 - else: - loops.append(code_ptr) - elif command == ']': - if memory.get(pointer, 0) != 0: - code_ptr = loops[-1] - else: - loops.pop() - code_ptr += 1 - -# Example usage -interpret("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.") -# Output: Hello World! diff --git a/run b/run index 496b1a7..2b3c5e1 100755 --- a/run +++ b/run @@ -1,2 +1,2 @@ # run the go run command passing the path to the main.go file, with the working directory set to the bin folder. pass in the arguments -go run ./src "$@" \ No newline at end of file +__ARGON_DEBUG__=true go run ./src "$@" \ No newline at end of file diff --git a/src/call.go b/src/call.go index 7604cd6..1c9b968 100644 --- a/src/call.go +++ b/src/call.go @@ -93,6 +93,7 @@ func runCall(c call, stack stack, stacklevel int) (any, ArErr) { } switch x := callable.(type) { case builtinFunc: + debugPrintln(x.name, args) resp, err := x.FUNC(args...) resp = AnyToArValid(resp) if err.EXISTS { @@ -108,6 +109,7 @@ func runCall(c call, stack stack, stacklevel int) (any, ArErr) { } return resp, err case Callable: + debugPrintln(x.name, args) if len(x.params) != len(args) { return nil, ArErr{"Runtime Error", "expected " + fmt.Sprint(len(x.params)) + " arguments, got " + fmt.Sprint(len(args)), c.line, c.path, c.code, true} } @@ -122,6 +124,7 @@ func runCall(c call, stack stack, stacklevel int) (any, ArErr) { } func builtinCall(callable any, args []any) (any, ArErr) { + debugPrintln(callable, args) switch x := callable.(type) { case builtinFunc: diff --git a/src/config.go b/src/config.go index 1aa3275..8f4c0ed 100644 --- a/src/config.go +++ b/src/config.go @@ -1,6 +1,6 @@ package main -var websiteLang = "https://argon.wbell.dev/" +var website = "https://argon.wbell.dev/" var docs = "https://argon.wbell.dev/docs/" var mainrepo = "https://github.com/Open-Argon/argon-v3" var mainissuesPage = "https://github.com/Open-Argon/argon-v3/issues" diff --git a/src/debug.go b/src/debug.go new file mode 100644 index 0000000..f6d2566 --- /dev/null +++ b/src/debug.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "os" +) + +var debug = os.Getenv("__ARGON_DEBUG__") == "true" + +func debugPrintln(a ...interface{}) { + if debug { + go func() { + defer func() { + if r := recover(); r != nil { + fmt.Println("debugPrintln: panic:", r) + } + }() + fmt.Println(a...) + }() + } +} diff --git a/src/main.go b/src/main.go index 3dcbe80..6f6ff0c 100644 --- a/src/main.go +++ b/src/main.go @@ -15,26 +15,32 @@ func newscope() ArObject { } func main() { - defer func() { - if r := recover(); r != nil { - fmt.Println("There was a fundamental error in argon v3 that caused it to crash.") - fmt.Println() - if fork { - fmt.Println("This is a fork of Open-Argon. Please report this to the fork's maintainer.") - fmt.Println("Fork repo:", forkrepo) - fmt.Println("Fork issue page:", forkissuesPage) + debugPrintln("In debug mode...") + if !debug { + defer func() { + if r := recover(); r != nil { + fmt.Println("There was a fundamental error in argon v3 that caused it to crash.") fmt.Println() - } else { - fmt.Println("Please report this to the Open-Argon team.") - fmt.Println("Main repo:", mainrepo) - fmt.Println("Issue page:", mainissuesPage) + fmt.Println("website:", website) + fmt.Println("docs:", docs) fmt.Println() + if fork { + fmt.Println("This is a fork of Open-Argon. Please report this to the fork's maintainer.") + fmt.Println("Fork repo:", forkrepo) + fmt.Println("Fork issue page:", forkissuesPage) + fmt.Println() + } else { + fmt.Println("Please report this to the Open-Argon team.") + fmt.Println("Main repo:", mainrepo) + fmt.Println("Issue page:", mainissuesPage) + fmt.Println() + } + fmt.Println("please include the following information:") + fmt.Println("panic:", r) + os.Exit(1) } - fmt.Println("please include the following information:") - fmt.Println("panic:", r) - os.Exit(1) - } - }() + }() + } initRandom() garbageCollect() global := makeGlobal() diff --git a/src/map.go b/src/map.go index c975ffb..4547213 100644 --- a/src/map.go +++ b/src/map.go @@ -22,13 +22,13 @@ func isMap(code UNPARSEcode) bool { func parseMap(code UNPARSEcode) (any, UNPARSEcode) { trimmed := strings.Trim(code.code, " ") trimmed = trimmed[1 : len(trimmed)-1] - fmt.Println(trimmed) + debugPrintln(trimmed) return nil, UNPARSEcode{} } func Map(m anymap) ArObject { var mutex = sync.RWMutex{} - return ArObject{ + obj := ArObject{ obj: anymap{ "__value__": m, "__name__": "map", @@ -168,4 +168,47 @@ func Map(m anymap) ArObject { }, }, } + obj.obj["__Equal__"] = builtinFunc{ + "__Equal__", + func(args ...any) (any, ArErr) { + debugPrintln("Equal", args) + if len(args) != 1 { + return nil, ArErr{ + TYPE: "TypeError", + message: "expected 1 argument, got " + fmt.Sprint(len(args)), + EXISTS: true, + } + } + if typeof(args[0]) != "map" { + return false, ArErr{} + } + a := ArValidToAny(args[0]).(anymap) + mutex.RLock() + if len(m) != len(a) { + mutex.RUnlock() + return false, ArErr{} + } + for k, v := range m { + debugPrintln(k, v) + if _, ok := a[k]; !ok { + mutex.RUnlock() + return false, ArErr{} + } + val, err := runOperation(operationType{ + operation: 9, + values: []any{v, a[k]}, + }, stack{}, 0) + if err.EXISTS { + return val, err + } + if !anyToBool(val) { + mutex.RUnlock() + return false, ArErr{} + } + } + mutex.RUnlock() + return true, ArErr{} + }, + } + return obj } diff --git a/src/operations.go b/src/operations.go index 9df0132..2b614ae 100644 --- a/src/operations.go +++ b/src/operations.go @@ -129,7 +129,6 @@ func compareValues(o operationType, stack stack, stacklevel int) (bool, ArErr) { stack, stacklevel+1, ) - resp2 = ArValidToAny(resp2) if err.EXISTS { return false, err } @@ -557,12 +556,12 @@ func calcNotIn(o operationType, stack stack, stacklevel int) (any, ArErr) { if err.EXISTS { return false, err } - if x, ok := resp2.(ArObject); ok { + if x, ok := resp.(ArObject); ok { if y, ok := x.obj["__NotContains__"]; ok { return runCall( call{ y, - []any{resp}, + []any{resp2}, o.code, o.line, o.path, @@ -651,6 +650,7 @@ func notequals(a any, b any, o operationType, stack stack, stacklevel int) (bool } func equals(a any, b any, o operationType, stack stack, stacklevel int) (bool, ArErr) { + debugPrintln("equals", a, b) if typeof(a) == "number" && typeof(b) == "number" { return a.(number).Cmp(b.(number)) == 0, ArErr{} } else if x, ok := a.(ArObject); ok { @@ -678,7 +678,6 @@ func calcMod(o operationType, stack stack, stacklevel int) (any, ArErr) { stack, stacklevel+1, ) - resp = ArValidToAny(resp) if err.EXISTS { return nil, err } diff --git a/test.ar b/test.ar new file mode 100644 index 0000000..3f3085d --- /dev/null +++ b/test.ar @@ -0,0 +1 @@ +term.log(null) \ No newline at end of file