Created
April 10, 2015 21:34
-
-
Save gkbrk/3bca0aa8466c1bb34ca4 to your computer and use it in GitHub Desktop.
Language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import string | |
import sys | |
class gkexpr(list): | |
def a(self): | |
pass | |
def lex(stream): | |
while True: | |
char = stream.read(1).decode("ascii") | |
if char == "(": | |
expr = gkexpr() | |
while True: | |
lexed = lex(stream) | |
if lexed: | |
expr.append(lexed) | |
else: | |
return expr | |
elif char == ")" or char == "": | |
return None | |
elif char in [str(x) for x in range(10)]: | |
stream.seek(-1, io.SEEK_CUR) | |
gknum = "" | |
while stream.read(1).decode("ascii") in [str(x) for x in range(10)]: | |
stream.seek(-1, io.SEEK_CUR) | |
gknum += stream.read(1).decode("ascii") | |
else: | |
stream.seek(-1, io.SEEK_CUR) | |
return int(gknum) | |
elif char in string.ascii_lowercase: | |
stream.seek(-1, io.SEEK_CUR) | |
gkstr = "" | |
while stream.read(1).decode("ascii") in string.ascii_lowercase: | |
stream.seek(-1, io.SEEK_CUR) | |
gkstr += stream.read(1).decode("ascii") | |
else: | |
stream.seek(-1, io.SEEK_CUR) | |
return gkstr | |
else: | |
continue | |
def run(gkcode, variables, functions): | |
print("{}:{}".format(gkcode, variables)) | |
if isinstance(gkcode, gkexpr): | |
for index, gkelement in enumerate(gkcode): | |
if isinstance(gkelement, gkexpr): | |
gkcode[index] = run(gkelement, variables, functions) | |
return functions[gkcode[0]](*gkcode[1:]) | |
elif isinstance(gkcode, list): | |
print("a") | |
def run_code(code, variables, functions): | |
stream = io.BytesIO(code.encode("ascii")) | |
while True: | |
lexed = lex(stream) | |
if lexed is not None: | |
run(lexed, variables, functions) | |
else: | |
return | |
variables = {} | |
functions = {} | |
def setfunc(variables, x, y): | |
variables[x] = y | |
def printfunc(variables, x): | |
if isinstance(x, int): | |
print(x) | |
elif isinstance(x, str): | |
if x in variables: | |
print(variables[x]) | |
else: | |
print(x) | |
elif isinstance(x, list): | |
print(x) | |
def forloop(variables, functions, forlist, code): | |
for a in forlist: | |
expr = gkexpr() | |
for codeelement in code: | |
expr.append(codeelement) | |
run(expr, variables, functions) | |
def flatten_code(code): | |
if isinstance(code, gkexpr): | |
flatcode = [] | |
for codeelement in code: | |
flatcode.append(flatten_code(codeelement)) | |
return code | |
else: | |
return code | |
def foreachloop(variables, functions, forlist, code): | |
#code = flatten_code(code) | |
operator = code[0] | |
expr = gkexpr() | |
for x in operator: | |
print(x) | |
expr.append(x) | |
for a in forlist[1:]: | |
variables["loopkey"] = a | |
run(expr, variables, functions) | |
# map :: (a -> b) -> [a] -> [b] | |
# map(lambda: x -> x + 2, [1,2,3]) = [3,4,5] | |
def myMap(f, xs): | |
if not xs: | |
return [] | |
head = xs[0] | |
transformed_head = list(f(head)) | |
return transformed_head.append(myMap(f, xs[1:])) | |
functions["print"] = lambda x: printfunc(variables, x) | |
functions["sum"] = lambda x, y: x + y | |
functions["times"] = lambda x, y: x * y | |
functions["pow"] = lambda x, y: x**y | |
functions["set"] = lambda x, y: setfunc(variables, x, y) | |
functions["range"] = lambda x: list(range(x)) | |
functions["for"] = lambda x, y: forloop(variables, functions, y, x) | |
functions["foreach"] = lambda x, y: foreachloop(variables, functions, y, x) | |
functions["code"] = lambda *args: flatten_code(args) | |
functions["intostring"] = lambda *args: " ".join(args) | |
functions["getarg"] = lambda x: variables["loopkey"] | |
run_code(open(sys.argv[1]).read(), variables, functions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(set ten (sum 5 5)) | |
(print ten) | |
(for (code print (sum 12 (getarg 1))) (range 10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
neat