Skip to content
This repository has been archived by the owner on Oct 5, 2019. It is now read-only.

Commit

Permalink
Finish kvalle#2.
Browse files Browse the repository at this point in the history
  • Loading branch information
codecop committed May 24, 2017
1 parent 1582a14 commit d77c6bf
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion diylang/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,36 @@

def evaluate(ast, env):
"""Evaluate an Abstract Syntax Tree in the specified environment."""
raise NotImplementedError("DIY")
if (is_boolean(ast)):
return ast
if (is_integer(ast)):
return ast

if len(ast) == 2 and ast[0] == 'quote':
return ast[1]

if len(ast) == 2 and ast[0] == 'atom':
return is_atom(evaluate(ast[1], env))

if len(ast) == 3 and ast[0] == 'eq':
left = evaluate(ast[1], env)
right = evaluate(ast[2], env)
if is_list(left) or is_list(right):
return False
return left == right

if len(ast) == 3 and ast[0] == 'mod':
left = _eval_num(ast[1], env)
right = _eval_num(ast[2], env)
return left % right

if len(ast) == 3 and ast[0] in ['+', '-', '/', '*', '>']:
left = _eval_num(ast[1], env)
right = _eval_num(ast[2], env)
return eval(str(left) + ast[0] + str(right))


def _eval_num(ast, env):
if not is_integer(ast):
raise DiyLangError(str(ast))
return evaluate(ast, env)

0 comments on commit d77c6bf

Please sign in to comment.