def test_lambda(std_env: Environment) -> None: source = '(lambda (a b) (if (>= a b) a b))' func = evaluate(parse(source), std_env) assert func.parms == ['a', 'b'] assert func.body == [['if', ['>=', 'a', 'b'], 'a', 'b']] assert func.env is std_env assert func(1, 2) == 2 assert func(3, 2) == 3
def test_begin(std_env: Environment) -> None: source = """ (begin (define x (* 2 3)) (* x 7) ) """ got = evaluate(parse(source), std_env) assert got == 42
def test_invocation_user_procedure(std_env: Environment) -> None: source = """ (begin (define max (lambda (a b) (if (>= a b) a b))) (max 22 11) ) """ got = evaluate(parse(source), std_env) assert got == 22
def test_define_function(std_env: Environment) -> None: source = '(define (max a b) (if (>= a b) a b))' got = evaluate(parse(source), std_env) assert got is None max_fn = std_env['max'] assert max_fn.parms == ['a', 'b'] assert max_fn.body == [['if', ['>=', 'a', 'b'], 'a', 'b']] assert max_fn.env is std_env assert max_fn(1, 2) == 2 assert max_fn(3, 2) == 3
def callJSON(func): global r20functions if func.get('type')=='lisp': if lis: lis.eval(lis.parse(str(func['content']))) else: sendMsgToR20('Cannot execute lisp function, lisp functions rely on lispy2 by Peter Norvig, available here: http://norvig.com/lispy2.html') else: r20functions=reload(r20functions) r20functions.getFunc(func['name'])(func)
def callJSON(func): global r20functions if func.get('type') == 'lisp': if lis: lis.eval(lis.parse(str(func['content']))) else: sendMsgToR20( 'Cannot execute lisp function, lisp functions rely on lispy2 by Peter Norvig, available here: http://norvig.com/lispy2.html' ) else: r20functions = reload(r20functions) r20functions.getFunc(func['name'])(func)
def test(task_name, func_string, subset='train'): with open(f'ARC/data/training/{task_name}.json') as f: j = json.load(f) prog = parse(func_string) correct = True for i, t in enumerate(j[subset]): input_grid = np.array(t['input']) prog_with_input = ['define', 'grid', input_grid, prog] pred = eval(prog_with_input) #print(pred) # vis(pred) target = np.array(t['output']) correct &= match(pred, target) return correct
def test_invocation_builtin_car(std_env: Environment) -> None: source = '(car (quote (11 22 33)))' got = evaluate(parse(source), std_env) assert got == 11
def test_invocation_builtin_append(std_env: Environment) -> None: source = '(append (quote (a b)) (quote (c d)))' got = evaluate(parse(source), std_env) assert got == ['a', 'b', 'c', 'd']
def test_define_exp(self): self.assertEqual(parse('(begin (define r 3) (+ r 5))'), ['begin', ['define', 'r', 3], ['+', 'r', 5]])
def test_even_more_more_nested_exp(self): self.assertEqual( parse('(begin (+ (+ 3 6) (- 3 2) (+ (- 1 1) 3)))'), ['begin', ['+', ['+', 3, 6], ['-', 3, 2], ['+', ['-', 1, 1], 3]]])
def test_evaluate_if_false(std_env: Environment) -> None: source = '(if 0 no-such-thing 20)' expected = 20 got = evaluate(parse(source), std_env) assert got == expected
def test_define(std_env: Environment) -> None: source = '(define answer (* 6 7))' got = evaluate(parse(source), std_env) assert got is None assert std_env['answer'] == 42
def mainloop(statements): for statement in statements: if len(statement) != 0: tokens = parse(statement) print(eval(tokens))
def test_parse_multiple_expr(self): self.assertEqual(list(parse('(1 2)(3)')), [[1, 2], [3]])
def test_parse(source: str, expected: Expression) -> None: got = parse(source) assert got == expected
def test_evaluate_variable() -> None: env: Environment = dict(x=10) source = 'x' expected = 10 got = evaluate(parse(source), env) assert got == expected
def test_evaluate_variable(): env = dict(x=10) source = 'x' expected = 10 got = evaluate(parse(source), env) assert got == expected
def test_evaluate(source, expected): got = evaluate(parse(source), global_env_for_first_test) assert got == expected
def test_parse(source: str, expected): got = parse(source) assert got == expected
def test_invocation_builtin_map(std_env: Environment) -> None: source = '(map (lambda (x) (* x 2)) (quote (1 2 3))))' got = evaluate(parse(source), std_env) assert got == [2, 4, 6]
def test_nested_exp(self): self.assertEqual(parse('(begin (+ 1 (- 3 2)))'), ['begin', ['+', 1, ['-', 3, 2]]])
def test_evaluate_literal(std_env): source = '3.3' expected = 3.3 got = evaluate(parse(source), std_env) assert got == expected
def test_parse_single_expr(self): self.assertEqual(list(parse('(1 2 3)')), [[1, 2, 3]])
def test_evaluate(source: str, expected: Optional[Expression]) -> None: got = evaluate(parse(source), global_env_for_first_test) assert got == expected
def test_evaluate_quote(std_env: Environment) -> None: source = '(quote (1.1 is not 1))' expected = [1.1, 'is', 'not', 1] got = evaluate(parse(source), std_env) assert got == expected
def test_evaluate_literal(std_env: Environment) -> None: source = '3.3' expected = 3.3 got = evaluate(parse(source), std_env) assert got == expected
def test_evaluate_if_true(std_env: Environment) -> None: source = '(if 1 10 no-such-thing)' expected = 10 got = evaluate(parse(source), std_env) assert got == expected
def notes(self, world, *args): if not self.__dict__.get('note_store'): self.note_store = [] self.save() if len(args) == 0: if self.note_store: for n, note in enumerate(self.note_store): print("note #" + str(n) + "\n" + note.split('\n')[0]) else: print("No notes.") elif len(args) > 0: if str(args[0]).lower() in ['new', 'create', 'write', 'add']: self.writenote() return if str(args[0]).lower() in ['delete', 'del', 'rm']: try: del self.note_store[int(args[1])] self.save() print('Deleted.') except ValueError: print("Bad note ID.") except IndexError: print("Can't find that note ID") return if str(args[0]).lower() in ['run']: try: for line in self.note_store[int(args[1])].split('\n'): val = lis.eval(lis.parse(line)) if val: print(lis.lispstr(val)) print() except IndexError: print("Can't find that note ID") return if str(args[0]).lower() in ['drop']: try: dropped_note = self.note_store[int(args[1])] del self.note_store[int(args[1])] self.save() room_data = get_room(self.x, self.y) if not room_data.get('data'): room_data['data'] = [] room_data['data'].append(dropped_note) put_room(self.x, self.y, room_data) except ValueError: raise # print("Bad note ID.") except IndexError: raise # print("Can't find that note ID") return elif type(args[0]) == int: try: print( "note #" + str(args[0]) + " " + self.note_store[int(args[0])] ) return except ValueError: print("Bad note ID.") return except IndexError: print("Can't find that note ID") return
def test_simple_exp(self): self.assertEqual(parse('(begin (+ 1 2))'), ['begin', ['+', 1, 2]])