Compare commits

...

10 Commits

  1. 46
      lib.py
  2. 1
      lib/aids.klambda
  3. 2
      lib/booleans.klambda
  4. 15
      lib/kokoro.klambda
  5. 8
      lib/lists.klambda
  6. 8
      lib/loop.klambda
  7. 4
      test.klambda

@ -6,10 +6,9 @@
def execute(program):
import traceback, copy
def _execute(ctx, ids, fns):
def _execute(ctx, ids):
import sys, functools
lids = copy.copy(ids)
lfns = copy.copy(fns)
def _ident(name):
return ("identifier", name)
def _destr(t):
@ -55,20 +54,19 @@ def execute(program):
#print("abba", ctx, subs)
if ctx[0] == _ident("defun"):
fns[ctx[1]] = ctx[2]
return fns[ctx[1]]
if ctx[0] == _ident("lambda"):
return ("lambda", (ctx[1], ctx[2]))
elif ctx[0] == _ident("cond"):
return _execute(ctx[2], lids, lfns) if _truthy(_execute(ctx[1], lids, lfns)) else _execute(ctx[3], lids, lfns)
return _execute(ctx[2], lids) if _truthy(_execute(ctx[1], lids)) else _execute(ctx[3], lids)
subs = _fixarr(list(map(lambda a: _execute(a, lids, lfns), ctx)))
subs = _fixarr(list(map(lambda a: _execute(a, lids), ctx)))
if ctx[0][1][0] == "$":
return subs
elif ctx[0][1] == "id":
return subs[1] if len(subs[1:]) == 1 else subs[1:]
elif ctx[0] == _ident("miracle"):
return _box(eval(_destr(subs[1]))[_destr(subs[2])](*[i[1] for i in _fixarr(subs[3])]))
return _box(eval(_destr(subs[1]))[_destr(subs[2])](*[(i if type(i) == type([]) else i[1]) for i in _fixarr(subs[3])]))
elif ctx[0] == _ident("def"):
ids[ctx[1]] = subs[2]
return ids[ctx[1]]
@ -83,7 +81,7 @@ def execute(program):
elif ctx[0] == _ident("%"):
return (subs[1][0], subs[1][1]%subs[2][1])
elif ctx[0] == _ident("!"):
return ("number", 0.0 if subs[1] else 1.0)
return ("number", 0.0 if _truthy(subs[1]) else 1.0)
elif ctx[0] == _ident("=="):
return ("number", 1.0 if subs[1] == subs[2] else 0.0)
elif ctx[0] == _ident("="):
@ -95,41 +93,37 @@ def execute(program):
elif ctx[0] == _ident("conv"):
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], lids, lfns)
ret = _execute(subs[1], lids)
for statement in subs[2:]:
ret = _execute(statement, lids, lfns)
ret = _execute(statement, lids)
return ret
elif ctx[0] == _ident("at"):
return subs[2][int(subs[1][1])]
elif ctx[0] == _ident("insert"):
subs[3].insert(int(subs[1][1]), subs[2])
return subs[3]
elif ctx[0] in fns:
#print(subs)
prototype = fns[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, lids, lfns)
else:
return subs
#print(f"{subs[0]} is not a valid function")
return _execute(subs, lids)
elif ctx[0][0] == "lambda":
prototype = ctx[0][1][1]
for idx, arg in enumerate(ctx[0][1][0]):
prototype = _recursereplace(prototype, arg, ctx[1:][idx])
return _execute(prototype, lids)
else:
#print("base", ctx)
if type(ctx) == type([]):
return list(
map(lambda a: _execute(a, lids, lfns), ctx)
map(lambda a: _execute(a, lids), ctx)
)
return ctx
idspace = {}
funcspace = {}
for strand in program:
try: _execute(strand, idspace, funcspace)
try: _execute(strand, idspace)
# _execute(strand)
except Exception as e:
print("failed in", strand, "with", e)
_execute(strand, idspace, funcspace)
_execute(strand, idspace)
#input()

@ -4,3 +4,4 @@
INCLUDE:./loop.klambda
INCLUDE:./lists.klambda
INCLUDE:./kokoro.klambda
INCLUDE:./booleans.klambda

@ -0,0 +1,2 @@
(def true 1)
(def false 1)

@ -1,10 +1,11 @@
(defun spit
(def spit (lambda (cunny)
(miracle
"__builtins__" "print" (id cunny))))
(def input (lambda ()
(miracle
"__builtins__" "print" (id $1)))
(defun input
(miracle
"__builtins__" "input" ("")))
(defun thesis
"__builtins__" "input" (""))))
(def thesis (lambda ()
(spit
("this language belongs to makise kurisu. there are many like it, but this one is hers.")))
("this language belongs to makise kurisu. there are many like it, but this one is hers."))))
DEFINE:<3:(thesis)

@ -1,6 +1,6 @@
(defun length
(def length (lambda (cunny)
(miracle
"__builtins__" "len" ((id $1))))
"__builtins__" "len" (cunny))))
(defun append
(insert (length $2) $1 $2))
(def append (lambda (item, victim)
(insert (length victim) item victim)))

@ -1,8 +1,8 @@
DEFINE:loop:(defun loop\
DEFINE:loop:(def loop (lambda (counter max)\
(all\
statement\
(cond (= $1 $2)\
$1\
(loop (+ $1 1) $2))))\
(cond (= counter max)\
counter\
(loop (+ counter 1) max)))))\
(loop start end)\
:start:end:statement

@ -15,9 +15,9 @@ INCLUDE:./lib/aids.klambda
"adult (hag)"
"not adult (uoh)"))
loop:0:100:(spit (cond (% $1 15) (cond (% $1 5) (cond (% $1 3) $1 "fizz") "buzz") "fizzbuzz"))
loop:0:100:(spit (cond (% counter 15) (cond (% counter 5) (cond (% counter 3) counter "fizz") "buzz") "fizzbuzz"))
(def moeblob ("moe" "moe~" "kyun!!"))
loop:0:(length moeblob):(spit (at $1 moeblob))
loop:0:(- (length moeblob) 1):(spit (at counter moeblob))
(thesis)
Loading…
Cancel
Save