add debug

This commit is contained in:
2023-06-13 23:18:54 +01:00
parent 8443b20949
commit ec53db45d5
9 changed files with 98 additions and 71 deletions

46
app.py
View File

@@ -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!

2
run
View File

@@ -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 # 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 "$@" __ARGON_DEBUG__=true go run ./src "$@"

View File

@@ -93,6 +93,7 @@ func runCall(c call, stack stack, stacklevel int) (any, ArErr) {
} }
switch x := callable.(type) { switch x := callable.(type) {
case builtinFunc: case builtinFunc:
debugPrintln(x.name, args)
resp, err := x.FUNC(args...) resp, err := x.FUNC(args...)
resp = AnyToArValid(resp) resp = AnyToArValid(resp)
if err.EXISTS { if err.EXISTS {
@@ -108,6 +109,7 @@ func runCall(c call, stack stack, stacklevel int) (any, ArErr) {
} }
return resp, err return resp, err
case Callable: case Callable:
debugPrintln(x.name, args)
if len(x.params) != len(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} 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) { func builtinCall(callable any, args []any) (any, ArErr) {
debugPrintln(callable, args)
switch x := callable.(type) { switch x := callable.(type) {
case builtinFunc: case builtinFunc:

View File

@@ -1,6 +1,6 @@
package main package main
var websiteLang = "https://argon.wbell.dev/" var website = "https://argon.wbell.dev/"
var docs = "https://argon.wbell.dev/docs/" var docs = "https://argon.wbell.dev/docs/"
var mainrepo = "https://github.com/Open-Argon/argon-v3" var mainrepo = "https://github.com/Open-Argon/argon-v3"
var mainissuesPage = "https://github.com/Open-Argon/argon-v3/issues" var mainissuesPage = "https://github.com/Open-Argon/argon-v3/issues"

21
src/debug.go Normal file
View File

@@ -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...)
}()
}
}

View File

@@ -15,10 +15,15 @@ func newscope() ArObject {
} }
func main() { func main() {
debugPrintln("In debug mode...")
if !debug {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
fmt.Println("There was a fundamental error in argon v3 that caused it to crash.") fmt.Println("There was a fundamental error in argon v3 that caused it to crash.")
fmt.Println() fmt.Println()
fmt.Println("website:", website)
fmt.Println("docs:", docs)
fmt.Println()
if fork { if fork {
fmt.Println("This is a fork of Open-Argon. Please report this to the fork's maintainer.") 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 repo:", forkrepo)
@@ -35,6 +40,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
}() }()
}
initRandom() initRandom()
garbageCollect() garbageCollect()
global := makeGlobal() global := makeGlobal()

View File

@@ -22,13 +22,13 @@ func isMap(code UNPARSEcode) bool {
func parseMap(code UNPARSEcode) (any, UNPARSEcode) { func parseMap(code UNPARSEcode) (any, UNPARSEcode) {
trimmed := strings.Trim(code.code, " ") trimmed := strings.Trim(code.code, " ")
trimmed = trimmed[1 : len(trimmed)-1] trimmed = trimmed[1 : len(trimmed)-1]
fmt.Println(trimmed) debugPrintln(trimmed)
return nil, UNPARSEcode{} return nil, UNPARSEcode{}
} }
func Map(m anymap) ArObject { func Map(m anymap) ArObject {
var mutex = sync.RWMutex{} var mutex = sync.RWMutex{}
return ArObject{ obj := ArObject{
obj: anymap{ obj: anymap{
"__value__": m, "__value__": m,
"__name__": "map", "__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
} }

View File

@@ -129,7 +129,6 @@ func compareValues(o operationType, stack stack, stacklevel int) (bool, ArErr) {
stack, stack,
stacklevel+1, stacklevel+1,
) )
resp2 = ArValidToAny(resp2)
if err.EXISTS { if err.EXISTS {
return false, err return false, err
} }
@@ -557,12 +556,12 @@ func calcNotIn(o operationType, stack stack, stacklevel int) (any, ArErr) {
if err.EXISTS { if err.EXISTS {
return false, err return false, err
} }
if x, ok := resp2.(ArObject); ok { if x, ok := resp.(ArObject); ok {
if y, ok := x.obj["__NotContains__"]; ok { if y, ok := x.obj["__NotContains__"]; ok {
return runCall( return runCall(
call{ call{
y, y,
[]any{resp}, []any{resp2},
o.code, o.code,
o.line, o.line,
o.path, 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) { 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" { if typeof(a) == "number" && typeof(b) == "number" {
return a.(number).Cmp(b.(number)) == 0, ArErr{} return a.(number).Cmp(b.(number)) == 0, ArErr{}
} else if x, ok := a.(ArObject); ok { } else if x, ok := a.(ArObject); ok {
@@ -678,7 +678,6 @@ func calcMod(o operationType, stack stack, stacklevel int) (any, ArErr) {
stack, stack,
stacklevel+1, stacklevel+1,
) )
resp = ArValidToAny(resp)
if err.EXISTS { if err.EXISTS {
return nil, err return nil, err
} }

1
test.ar Normal file
View File

@@ -0,0 +1 @@
term.log(null)