Last active
August 29, 2015 14:13
-
-
Save keyle/b7dd1aa5397c93978649 to your computer and use it in GitHub Desktop.
toy programming language, lispish and concise.
This file contains hidden or 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
| # werp.py :: toy programming language, lispish and concise. | |
| tokenize = lambda chars: chars.replace('(', ' ( ').replace(')', ' ) ').split() | |
| def read_from_tokens(tokens): | |
| if len(tokens) == 0: | |
| raise SyntaxError('Unexpected EOF') | |
| token = tokens.pop(0) | |
| if token == "(": | |
| L = [] | |
| while tokens[0] != ')': L.append(read_from_tokens(tokens)) | |
| tokens.pop(0) # final ) pop | |
| return L | |
| elif token == ')': raise SyntaxError('unexpected `)`') | |
| else: return atom(token) | |
| def atom(token): | |
| try: return int(token) | |
| except ValueError: | |
| try: return float(token) | |
| except ValueError: | |
| return Symbol(token) | |
| parse = lambda program: read_from_tokens(tokenize(program)) | |
| Symbol = str | |
| List = list | |
| Number = (int, float) | |
| Env = dict | |
| def standard_env(): | |
| import math, operator as op | |
| env = Env() | |
| env.update(vars(math)) # sin, cos, etc. | |
| env.update({ | |
| '+':op.add, '-':op.sub, '*':op.mul, '/':op.div, | |
| '>':op.gt, '<':op.lt, '>=':op.ge, '<=':op.le, '==':op.eq, | |
| 'abs': abs, | |
| 'append': op.add, | |
| 'apply': apply, | |
| '>>': lambda *x : x[-1], | |
| 'car': lambda x: x[0], | |
| 'cdr': lambda x: x[1:], | |
| 'cons': lambda x,y: [x] + y, | |
| 'eq?': op.is_, | |
| 'equal?': op.eq, | |
| 'length': len, | |
| 'list': lambda *x: list(x), | |
| 'list?': lambda x: isinstance(x, list), | |
| 'map': map, | |
| 'max': max, | |
| 'min': min, | |
| 'not': op.not_, | |
| 'null?': lambda x: x==[], | |
| 'number?': lambda x: isinstance(x, Number), | |
| 'procedure?':callable, | |
| 'space': ' ', | |
| 'round': round, | |
| 'symbol?': lambda x: isinstance(x, Symbol) | |
| }) | |
| return env | |
| global_env = standard_env() | |
| def eval(x, env = global_env): | |
| if isinstance(x, Symbol): | |
| return env[x] | |
| elif not isinstance(x, List): | |
| return x | |
| elif x[0] == '`' or x[0] == 'string': | |
| (_, exp) = x | |
| return exp | |
| elif x[0] == 'if': | |
| (_, test, _then, _else) = x | |
| exp = (_then if eval(test, env) else _else) | |
| return eval(exp, env) | |
| elif x[0] == 'var': | |
| (_, _var, exp) = x | |
| env[_var] = eval(exp, env) | |
| else: | |
| proc = eval(x[0], env) | |
| args = [eval(arg, env) for arg in x[1:]] | |
| return proc(*args) | |
| run = lambda prg: eval(parse(prg)) | |
| print run("(>> (var r 10) (* pi (* r r)))") # 314.159265359 | |
| print run("(if (> (* 11 11) 120) (* 7 6) (` oops))") # 42 | |
| print run("(>> (var h (` hello)) (var w (` world)) (+ h (+ space w)))") # hello world | |
| print run(""" | |
| (>> | |
| (var h (` hello)) | |
| (var w (` world)) | |
| (+ h (+ space w)) | |
| ) | |
| """) # hello world | |
| print run(""" | |
| (>> | |
| (var p 1) | |
| (if (== p 1) (string yes) (string no)) | |
| ) | |
| """) # yes | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment