Last active
August 29, 2015 14:11
-
-
Save SamuraiT/63755b8a80f75557f08b to your computer and use it in GitHub Desktop.
lisp interpreter for arithmetic operations
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
def read_from_tokens(tokens): | |
if len(tokens) == 0: raise SyntaxError("unexpected EOF while reading") | |
token = tokens.pop(0) | |
if token == '(': | |
L = [] | |
while tokens[0] != ')': | |
L.append(read_from_tokens(tokens)) | |
print(L) | |
tokens.pop(0) # pop off ')' | |
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) | |
Symbol = str | |
List = list | |
Number = (int, float) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment