spacepaste

  1.  
  2. diff --git a/nolang/lexer.py b/nolang/lexer.py
  3. index d6cb4ae..5378560 100644
  4. --- a/nolang/lexer.py
  5. +++ b/nolang/lexer.py
  6. @@ -4,8 +4,10 @@ from rply import LexerGenerator
  7. RULES = [
  8. ('INTEGER', r'\d+'),
  9. ('PLUS', r'\+'),
  10. + ('MINUS', r'\-'),
  11. ('LT', r'\<'),
  12. - ('EQUALS', r'='),
  13. + ('EQ', r'=='),
  14. + ('ASSIGN', r'='),
  15. ('FUNCTION', r'function'),
  16. ('RETURN', r'return'),
  17. ('VAR', r'var'),
  18. diff --git a/nolang/parser.py b/nolang/parser.py
  19. index ac2f8f3..d075e6f 100644
  20. --- a/nolang/parser.py
  21. +++ b/nolang/parser.py
  22. @@ -80,8 +80,8 @@ def get_parser():
  23. def var_decl_identifier(state, p):
  24. return ast.VarDeclPartial([p[1].getstr()] + p[2].get_names())
  25. - @pg.production('statement : IDENTIFIER EQUALS expression SEMICOLON')
  26. - def statement_identifier_equals_expr(state, p):
  27. + @pg.production('statement : IDENTIFIER ASSIGN expression SEMICOLON')
  28. + def statement_identifier_assign_expr(state, p):
  29. return ast.Assignment(p[0].getstr(), p[2])
  30. @pg.production('statement : RETURN expression SEMICOLON')
  31. @@ -140,12 +140,14 @@ def get_parser():
  32. return p[1]
  33. @pg.production('expression : expression PLUS expression')
  34. + @pg.production('expression : expression MINUS expression')
  35. def expression_plus_expression(state, p):
  36. - return ast.BinOp('+', p[0], p[2])
  37. + return ast.BinOp(p[1].getstr(), p[0], p[2])
  38. @pg.production('expression : expression LT expression')
  39. + @pg.production('expression : expression EQ expression')
  40. def expression_lt_expression(state, p):
  41. - return ast.BinOp('<', p[0], p[2])
  42. + return ast.BinOp(p[1].getstr(), p[0], p[2])
  43. @pg.production('expression_list : ')
  44. def expression_list_empty(state, p):
  45. diff --git a/tests/test_interpreter.py b/tests/test_interpreter.py
  46. index aeb81b7..162589b 100644
  47. --- a/tests/test_interpreter.py
  48. +++ b/tests/test_interpreter.py
  49. @@ -123,8 +123,16 @@ class TestInterpreter(BaseTest):
  50. ''')
  51. assert self.space.int_w(w_res) == 4
  52. - # def test_recursive_call(self):
  53. - # w_res = self.interpret('''
  54. - # function fib(n) {
  55. - # if n == 0 or n == 1
  56. - # }
  57. \ No newline at end of file
  58. + def test_recursive_call(self):
  59. + w_res = self.interpret('''
  60. + function fib(n) {
  61. + if n == 0 or n == 1 {
  62. + return 1;
  63. + }
  64. + return fib(n - 1) + fib(n - 2);
  65. + }
  66. + function main() {
  67. + return fib(5);
  68. + }
  69. + ''')
  70. + assert self.space.int_w(w_res) == 8
  71.