basic define macros

master
cynic 3 years ago
parent 6beeb3beba
commit 9ea2fa85b0
  1. 14
      lib.py
  2. 2
      lib/aids.klambda
  3. 11
      lib/loop.klambda
  4. 28
      main.py
  5. 28
      test.klambda

@ -82,7 +82,6 @@ def execute(program):
elif ctx[0] == _ident("<"): elif ctx[0] == _ident("<"):
return ("number", 1.0 if subs[1][1] < subs[2][1] else 0.0) return ("number", 1.0 if subs[1][1] < subs[2][1] else 0.0)
elif ctx[0] == _ident("conv"): 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])) return (subs[1][1], float(subs[2][1]) if subs[1][1] == "number" else str(subs[2][1]))
elif ctx[0] == _ident("all"): elif ctx[0] == _ident("all"):
ret = _execute(subs[1]) ret = _execute(subs[1])
@ -90,11 +89,13 @@ def execute(program):
ret = _execute(statement) ret = _execute(statement)
return ret return ret
elif ctx[0] in funcspace: elif ctx[0] in funcspace:
#print(subs)
prototype = funcspace[ctx[0]] prototype = funcspace[ctx[0]]
for idx, arg in enumerate(subs[1:]): for idx, arg in enumerate(subs[1:]):
idx += 1 idx += 1
prototype = _recursereplace(prototype, ("identifier", f"${idx}"), arg) prototype = _recursereplace(prototype, ("identifier", f"${idx}"), arg)
#print(f"${idx}", prototype) #print(f"${idx}", prototype)
#print(prototype)
return _execute(prototype) return _execute(prototype)
else: else:
print("no such function", ctx[0]) print("no such function", ctx[0])
@ -108,12 +109,11 @@ def execute(program):
) )
return ctx return ctx
for strand in program: for strand in program:
# try: _execute(strand) try: _execute(strand)
_execute(strand) # _execute(strand)
# except Exception as e: except Exception as e:
# print("failed in", strand, "with", e) print("failed in", strand, "with", e)
# traceback.print_stack() _execute(strand)
# exit()
#input() #input()

@ -1,6 +1,8 @@
: get it? because std? : get it? because std?
: i'm hilarious. : i'm hilarious.
INCLUDE:./lib/loop.klambda
(defun spit (defun spit
(miracle (miracle
"builtins" "print" (id $1)) "builtins" "print" (id $1))

@ -0,0 +1,11 @@
DEFINE:loop:(defun loop\
(all\
statement\
(cond (= $1 $2)\
$1\
(loop (+ $1 1) $2)\
)\
)\
)\
(loop start end)\
:start:end:statement

@ -10,17 +10,35 @@ reps = ["(", ")", "\"", ","]
f = open(sys.argv[1]).read() f = open(sys.argv[1]).read()
def expand(file): def expand(file):
tmp = file.split("\n") tmp = file.replace("\\\n", "").split("\n")
defs = {}
for idx, i in enumerate(tmp): for idx, i in enumerate(tmp):
parts = i.split(":")
if len(i) < 1: if len(i) < 1:
continue continue
elif i[:8] == "INCLUDE:": 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] == ":": elif i[0] == ":":
tmp.pop(idx) tmp.pop(idx)
return "\n".join(tmp) elif i.split(":")[0] in defs.keys():
parts = i.split(":")
f = expand(f) 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 f = f.replace(" ", " RDANEELOLIVAW ") # spacer

@ -4,33 +4,21 @@ INCLUDE:./lib/aids.klambda
(spit (spit
("lol swej" (+ 9000 1) a) ("lol swej" (+ 9000 1) a)
) )
(spit ((cond (= "10" (+ 1 9)) "same" "not same"))) (spit (cond (= "10" (+ 1 9)) "same" "not same"))
(cond ("", (+ 0 1)) (cond ("", (+ 0 1))
(id spit ("one")) (id spit "one")
(id spit ("zero")) (id spit "zero")
) )
(spit ("how old are you? > ")) (spit "how old are you? > ")
(def age (conv number (input))) (def age (conv number (input)))
(spit (spit
( (cond (> age 18)
(cond (> age 18) "adult (hag)"
"adult (hag)" "not adult (uoh)"
"not adult (uoh)"
)
) )
) )
(defun loop loop:0:100:(spit (cond (% $1 15) (cond (% $1 5) (cond (% $1 3) $1 "fizz") "buzz") "fizzbuzz"))
(all
(spit ($1))
(cond (= $1 $2)
$1
(loop (+ $1 1) $2)
)
)
)
(loop 0 5)
(thesis) (thesis)
Loading…
Cancel
Save