Compare commits

..

No commits in common. 'db801bd8187a92a0e6ca81cec60e56a366f089b9' and '35ec0ece22ad1032f9f74284b0f1a06a40205937' have entirely different histories.

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

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

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

@ -1,11 +1,10 @@
(def spit (lambda (cunny) (defun spit
(miracle
"__builtins__" "print" (id cunny))))
(def input (lambda ()
(miracle (miracle
"__builtins__" "input" ("")))) "__builtins__" "print" (id $1)))
(def thesis (lambda () (defun input
(miracle
"__builtins__" "input" ("")))
(defun thesis
(spit (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) DEFINE:<3:(thesis)

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

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

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