From 5208ca5d7f34e07762d1e7922ec8d31ddac51a1e Mon Sep 17 00:00:00 2001 From: mryouse Date: Thu, 19 May 2022 23:48:52 +0000 Subject: remove redundant arity checks --- interpreter.py | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) (limited to 'interpreter.py') diff --git a/interpreter.py b/interpreter.py index f342caa..e8c0271 100644 --- a/interpreter.py +++ b/interpreter.py @@ -102,8 +102,6 @@ GLOBALS.register("and", Function(interpretAnd)) def interpretEq(expr): # equal - if len(expr.args) != 2: - raise Exception("'eq?' needs two arguments") first = evaluate(expr.args[0]) second = evaluate(expr.args[1]) return first == second @@ -111,8 +109,6 @@ def interpretEq(expr): GLOBALS.register("eq?", Function(interpretEq, 2)) def interpretComparison(expr): - if len(expr.args) != 2: - raise Exception("comparisons have two operands") left = evaluate(expr.args[0]) if type(left) not in (int, float): raise Exception("'left' must be a number") @@ -155,8 +151,6 @@ GLOBALS.register("-", Function(interpretTerm)) def interpretFactor(expr): if expr.symbol.name == "/": - if len(expr.args) != 2: - raise Exception("'/' requires two operands") num = evaluate(expr.args[0]) if type(num) not in (int, float): raise Exception("numerator must be a number") @@ -182,8 +176,6 @@ GLOBALS.register("*", Function(interpretFactor)) GLOBALS.register("/", Function(interpretFactor, 2)) def interpretNot(expr): - if len(expr.args) != 1: - raise Exception("'not' takes one operand") res = evaluate(expr.args[0]) if res not in (True, False): raise Exception("'not' only works on booleans") @@ -193,8 +185,6 @@ GLOBALS.register("not", Function(interpretNot, 1)) def interpretIf(expr): # if cond t-branch [f-branch] - if len(expr.args) not in (2, 3): - raise Exception("too many or too few operands") cond = evaluate(expr.args[0]) if cond not in (True, False): raise Exception("'if' condition must be boolean") @@ -207,21 +197,16 @@ def interpretIf(expr): GLOBALS.register("if", Function(interpretIf, 2, 3)) def interpretPrint(expr): - if len(expr.args) == 1: - ev = evaluate(expr.args[0]) - if not isinstance(ev, str): - raise Exception("can only 'print' strings") - print(ev) - else: - raise Exception("'print' takes one argument") + ev = evaluate(expr.args[0]) + if not isinstance(ev, str): + raise Exception("can only 'print' strings") + print(ev) return None # print returns nothing GLOBALS.register("print", Function(interpretPrint, 1)) def interpretDef(expr, env): - if len(expr.args) != 2: - raise Exception("'def' requires a name and an expression") if not isinstance(expr.args[0], Expr.Symbol): raise Exception("'def' requires a string literal as a name") name = expr.args[0].name # NOTE: we are not evaluating the name!! -- cgit v1.2.3