diff --git a/lib.py b/lib.py index 7c99e0c..1a18502 100644 --- a/lib.py +++ b/lib.py @@ -82,7 +82,6 @@ def execute(program): elif ctx[0] == _ident("<"): return ("number", 1.0 if subs[1][1] < subs[2][1] else 0.0) elif ctx[0] == _ident("conv"): - print(subs) return (subs[1][1], float(subs[2][1]) if subs[1][1] == "number" else str(subs[2][1])) elif ctx[0] == _ident("all"): ret = _execute(subs[1]) @@ -90,11 +89,13 @@ def execute(program): ret = _execute(statement) return ret elif ctx[0] in funcspace: + #print(subs) prototype = funcspace[ctx[0]] for idx, arg in enumerate(subs[1:]): idx += 1 prototype = _recursereplace(prototype, ("identifier", f"${idx}"), arg) #print(f"${idx}", prototype) + #print(prototype) return _execute(prototype) else: print("no such function", ctx[0]) @@ -108,12 +109,11 @@ def execute(program): ) return ctx for strand in program: -# try: _execute(strand) - _execute(strand) -# except Exception as e: -# print("failed in", strand, "with", e) -# traceback.print_stack() -# exit() + try: _execute(strand) +# _execute(strand) + except Exception as e: + print("failed in", strand, "with", e) + _execute(strand) #input() diff --git a/lib/aids.klambda b/lib/aids.klambda index be7097a..bf325cf 100644 --- a/lib/aids.klambda +++ b/lib/aids.klambda @@ -1,6 +1,8 @@ : get it? because std? : i'm hilarious. +INCLUDE:./lib/loop.klambda + (defun spit (miracle "builtins" "print" (id $1)) diff --git a/lib/loop.klambda b/lib/loop.klambda new file mode 100644 index 0000000..84770f4 --- /dev/null +++ b/lib/loop.klambda @@ -0,0 +1,11 @@ +DEFINE:loop:(defun loop\ + (all\ + statement\ + (cond (= $1 $2)\ + $1\ + (loop (+ $1 1) $2)\ + )\ + )\ +)\ +(loop start end)\ +:start:end:statement \ No newline at end of file diff --git a/main.py b/main.py index b5152d2..d271128 100644 --- a/main.py +++ b/main.py @@ -10,17 +10,35 @@ reps = ["(", ")", "\"", ","] f = open(sys.argv[1]).read() def expand(file): - tmp = file.split("\n") + tmp = file.replace("\\\n", "").split("\n") + defs = {} for idx, i in enumerate(tmp): + parts = i.split(":") if len(i) < 1: continue elif i[:8] == "INCLUDE:": - tmp[idx] = expand(open(i[8:]).read()) + tdefs, tmp[idx] = expand(open(i[8:]).read()) + defs.update(tdefs) + elif i[:7] == "DEFINE:": + parts = parts[1:] + defs[parts[0]] = (parts[2:], parts[1].replace("\t", " ")) + tmp.pop(idx) elif i[0] == ":": tmp.pop(idx) - return "\n".join(tmp) - -f = expand(f) + elif i.split(":")[0] in defs.keys(): + parts = i.split(":") + args, exp = defs[parts[0]] + + for arg_idx, arg in enumerate(parts[1:]): + exp = exp.replace( + args[arg_idx], arg + ) + #print(exp, "exp") + tmp[idx] = exp + #print(i, "\nsep") + return (defs, "\n".join(tmp)) + +f = expand(f)[1] f = f.replace(" ", " RDANEELOLIVAW ") # spacer diff --git a/test.klambda b/test.klambda index cd07566..89cf061 100644 --- a/test.klambda +++ b/test.klambda @@ -4,33 +4,21 @@ INCLUDE:./lib/aids.klambda (spit ("lol swej" (+ 9000 1) a) ) -(spit ((cond (= "10" (+ 1 9)) "same" "not same"))) +(spit (cond (= "10" (+ 1 9)) "same" "not same")) (cond ("", (+ 0 1)) - (id spit ("one")) - (id spit ("zero")) + (id spit "one") + (id spit "zero") ) -(spit ("how old are you? > ")) +(spit "how old are you? > ") (def age (conv number (input))) -(spit - ( - (cond (> age 18) - "adult (hag)" - "not adult (uoh)" - ) - ) -) - -(defun loop - (all - (spit ($1)) - (cond (= $1 $2) - $1 - (loop (+ $1 1) $2) - ) +(spit + (cond (> age 18) + "adult (hag)" + "not adult (uoh)" ) ) -(loop 0 5) +loop:0:100:(spit (cond (% $1 15) (cond (% $1 5) (cond (% $1 3) $1 "fizz") "buzz") "fizzbuzz")) (thesis) \ No newline at end of file