implemented return by rewriting interpreter result

This commit is contained in:
Moritz Gmeiner 2024-08-28 22:55:15 +02:00
commit dee73dea20
16 changed files with 191 additions and 239 deletions

View file

@ -1,4 +0,0 @@
// Note: This is just for the expression evaluating chapter which evaluates an
// expression directly.
(5 - (3 - 1)) + -1
// expect: 2

View file

@ -1,3 +0,0 @@
// Note: This is just for the expression parsing chapter which prints the AST.
(5 - (3 - 1)) + -1
// expect: (+ (group (- 5.0 (group (- 3.0 1.0)))) (- 1.0))

View file

@ -1,6 +0,0 @@
fun fib(n) {
if (n < 2) return n;
return fib(n - 2) + fib(n - 1);
}
print fib(35); // expect: 9227465

View file

@ -1,20 +1,7 @@
$ for lox_file in *.lox **/*.lox; do
> mlox $lox_file || echo $lox_file failed
> done
unexpected_character.lox failed
string/unterminated.lox failed
file empty_file.lox
$ mlox empty_file.lox
file fib.lox
$ mlox fib.lox
9227465
file precedence.lox
$ mlox precedence.lox
14
@ -364,14 +351,6 @@ file continue/while.lox
9
file expressions/evaluate.lox
$ mlox expressions/evaluate.lox
file expressions/parse.lox
$ mlox expressions/parse.lox
file field/call_function_field.lox
$ mlox field/call_function_field.lox
@ -476,7 +455,8 @@ $ mlox for/return_closure.lox
file for/return_inside.lox
$ mlox for/return_inside.lox
$ mlox for/return_inside.lox
i
file for/scope.lox
@ -510,7 +490,21 @@ file for/statement_initializer.lox
file for/syntax.lox
$ mlox for/syntax.lox
$ mlox for/syntax.lox
1
2
3
0
1
2
done
0
1
0
1
2
0
1
file for/var_in_body.lox
@ -538,8 +532,9 @@ file function/extra_arguments.lox
file function/local_mutual_recursion.lox
$ mlox function/local_mutual_recursion.lox
$ mlox function/local_mutual_recursion.lox
RuntimeError at line 4, column 11: name "isOdd" is not defined
[1]
file function/local_recursion.lox
$ mlox function/local_recursion.lox
@ -559,15 +554,27 @@ file function/missing_comma_in_parameters.lox
file function/mutual_recursion.lox
$ mlox function/mutual_recursion.lox
$ mlox function/mutual_recursion.lox
true
true
file function/nested_call_with_arguments.lox
$ mlox function/nested_call_with_arguments.lox
$ mlox function/nested_call_with_arguments.lox
hello world
file function/parameters.lox
$ mlox function/parameters.lox
$ mlox function/parameters.lox
0
1
3
6
10
15
21
28
36
file function/print.lox
@ -577,7 +584,8 @@ file function/print.lox
file function/recursion.lox
$ mlox function/recursion.lox
$ mlox function/recursion.lox
21
file function/too_many_arguments.lox
@ -1079,23 +1087,29 @@ $ mlox regression/394.lox
file return/after_else.lox
$ mlox return/after_else.lox
$ mlox return/after_else.lox
ok
file return/after_if.lox
$ mlox return/after_if.lox
$ mlox return/after_if.lox
ok
file return/after_while.lox
$ mlox return/after_while.lox
$ mlox return/after_while.lox
ok
file return/at_top_level.lox
$ mlox return/at_top_level.lox
$ mlox return/at_top_level.lox
found 1 ParserError:
ParserError at line 1, column 0: Can use return only in functions
[1]
file return/in_function.lox
$ mlox return/in_function.lox
$ mlox return/in_function.lox
ok
file return/in_method.lox
@ -1103,31 +1117,8 @@ $ mlox return/in_method.lox
file return/return_nil_if_no_value.lox
$ mlox return/return_nil_if_no_value.lox
file scanning/identifiers.lox
$ mlox scanning/identifiers.lox
file scanning/keywords.lox
$ mlox scanning/keywords.lox
file scanning/numbers.lox
$ mlox scanning/numbers.lox
file scanning/punctuators.lox
$ mlox scanning/punctuators.lox
file scanning/strings.lox
$ mlox scanning/strings.lox
file scanning/whitespace.lox
$ mlox scanning/whitespace.lox
$ mlox return/return_nil_if_no_value.lox
nil
file string/error_after_multiline.lox
@ -1403,7 +1394,8 @@ $ mlox while/return_closure.lox
file while/return_inside.lox
$ mlox while/return_inside.lox
$ mlox while/return_inside.lox
i
file while/syntax.lox

View file

@ -1,12 +0,0 @@
andy formless fo _ _123 _abc ab123
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_
// expect: IDENTIFIER andy null
// expect: IDENTIFIER formless null
// expect: IDENTIFIER fo null
// expect: IDENTIFIER _ null
// expect: IDENTIFIER _123 null
// expect: IDENTIFIER _abc null
// expect: IDENTIFIER ab123 null
// expect: IDENTIFIER abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_ null
// expect: EOF null

View file

@ -1,18 +0,0 @@
and class else false for fun if nil or return super this true var while
// expect: AND and null
// expect: CLASS class null
// expect: ELSE else null
// expect: FALSE false null
// expect: FOR for null
// expect: FUN fun null
// expect: IF if null
// expect: NIL nil null
// expect: OR or null
// expect: RETURN return null
// expect: SUPER super null
// expect: THIS this null
// expect: TRUE true null
// expect: VAR var null
// expect: WHILE while null
// expect: EOF null

View file

@ -1,12 +0,0 @@
123
123.456
.456
123.
// expect: NUMBER 123 123.0
// expect: NUMBER 123.456 123.456
// expect: DOT . null
// expect: NUMBER 456 456.0
// expect: NUMBER 123 123.0
// expect: DOT . null
// expect: EOF null

View file

@ -1,21 +0,0 @@
(){};,+-*!===<=>=!=<>/.
// expect: LEFT_PAREN ( null
// expect: RIGHT_PAREN ) null
// expect: LEFT_BRACE { null
// expect: RIGHT_BRACE } null
// expect: SEMICOLON ; null
// expect: COMMA , null
// expect: PLUS + null
// expect: MINUS - null
// expect: STAR * null
// expect: BANG_EQUAL != null
// expect: EQUAL_EQUAL == null
// expect: LESS_EQUAL <= null
// expect: GREATER_EQUAL >= null
// expect: BANG_EQUAL != null
// expect: LESS < null
// expect: GREATER > null
// expect: SLASH / null
// expect: DOT . null
// expect: EOF null

View file

@ -1,6 +0,0 @@
""
"string"
// expect: STRING ""
// expect: STRING "string" string
// expect: EOF null

View file

@ -1,12 +0,0 @@
space tabs newlines
end
// expect: IDENTIFIER space null
// expect: IDENTIFIER tabs null
// expect: IDENTIFIER newlines null
// expect: IDENTIFIER end null
// expect: EOF null