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. 30
      test.klambda

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

@ -1,6 +1,8 @@
: get it? because std?
: i'm hilarious.
INCLUDE:./lib/loop.klambda
(defun spit
(miracle
"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()
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

@ -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)
Loading…
Cancel
Save