File tree Expand file tree Collapse file tree 2 files changed +18
-8
lines changed
src/com/annimon/ownlang/parser Expand file tree Collapse file tree 2 files changed +18
-8
lines changed Original file line number Diff line number Diff line change @@ -64,18 +64,18 @@ print "\n\n"
64
64
array = newarray(2, 2, 2, 2)
65
65
print array
66
66
67
- add = def(a,b) return a+b
68
- sub = def(a,b) return a-b
69
- mul = def(a,b) return a*b
70
- div = def(a,b) return a/b
71
- cube = def(x) return x*mul(x, x)
67
+ add = def(a,b) = a+b
68
+ sub = def(a,b) = a-b
69
+ mul = def(a,b) = a*b
70
+ div = def(a,b) = a/b
71
+ cube = def(x) = x*mul(x, x)
72
72
print "\n\n"
73
73
print mul(8, 5)
74
74
print "\n"
75
75
print cube(2)
76
76
77
77
functions = [add, sub, mul, div]
78
- def function(f, a, b) return f(a, b)
78
+ def function(f, a, b) = f(a, b)
79
79
for i = 0, i < 4, i = i + 1 {
80
80
print "\n"
81
81
print functions[i]
Original file line number Diff line number Diff line change @@ -141,6 +141,10 @@ private FunctionDefineStatement functionDefine() {
141
141
argNames .add (consume (TokenType .WORD ).getText ());
142
142
match (TokenType .COMMA );
143
143
}
144
+ if (lookMatch (0 , TokenType .EQ )) {
145
+ match (TokenType .EQ );
146
+ return new FunctionDefineStatement (name , argNames , new ReturnStatement (expression ()));
147
+ }
144
148
final Statement body = statementOrBlock ();
145
149
return new FunctionDefineStatement (name , argNames , body );
146
150
}
@@ -411,8 +415,14 @@ private Expression primary() {
411
415
argNames .add (consume (TokenType .WORD ).getText ());
412
416
match (TokenType .COMMA );
413
417
}
414
- final Statement body = statementOrBlock ();
415
- return new ValueExpression (new UserDefinedFunction (argNames , body ));
418
+ Statement statement ;
419
+ if (lookMatch (0 , TokenType .EQ )) {
420
+ match (TokenType .EQ );
421
+ statement = new ReturnStatement (expression ());
422
+ } else {
423
+ statement = statementOrBlock ();
424
+ }
425
+ return new ValueExpression (new UserDefinedFunction (argNames , statement ));
416
426
}
417
427
if (match (TokenType .LPAREN )) {
418
428
Expression result = expression ();
You can’t perform that action at this time.
0 commit comments