Skip to content

Commit 5cada73

Browse files
committed
Небольшие исправления
1 parent 4eabeca commit 5cada73

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/main/java/com/annimon/ownlang/parser/Parser.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,15 @@ private Statement doWhileStatement() {
219219

220220
private Statement forStatement() {
221221
int foreachIndex = lookMatch(0, TokenType.LPAREN) ? 1 : 0;
222-
if (lookMatch(foreachIndex, TokenType.WORD) && lookMatch(foreachIndex + 1, TokenType.COLON)) {
222+
if (lookMatch(foreachIndex, TokenType.WORD)
223+
&& lookMatch(foreachIndex + 1, TokenType.COLON)) {
223224
// for v : arr || for (v : arr)
224225
return foreachArrayStatement();
225226
}
226-
if (lookMatch(foreachIndex, TokenType.WORD) && lookMatch(foreachIndex + 1, TokenType.COMMA)
227-
&& lookMatch(foreachIndex + 2, TokenType.WORD) && lookMatch(foreachIndex + 3, TokenType.COLON)) {
227+
if (lookMatch(foreachIndex, TokenType.WORD)
228+
&& lookMatch(foreachIndex + 1, TokenType.COMMA)
229+
&& lookMatch(foreachIndex + 2, TokenType.WORD)
230+
&& lookMatch(foreachIndex + 3, TokenType.COLON)) {
228231
// for key, value : arr || for (key, value : arr)
229232
return foreachMapStatement();
230233
}
@@ -242,23 +245,29 @@ && lookMatch(foreachIndex + 2, TokenType.WORD) && lookMatch(foreachIndex + 3, To
242245
}
243246

244247
private ForeachArrayStatement foreachArrayStatement() {
248+
// for x : arr
245249
boolean optParentheses = match(TokenType.LPAREN);
246250
final String variable = consume(TokenType.WORD).getText();
247251
consume(TokenType.COLON);
248252
final Expression container = expression();
249-
if (optParentheses) consume(TokenType.RPAREN); // close opt parentheses
253+
if (optParentheses) {
254+
consume(TokenType.RPAREN); // close opt parentheses
255+
}
250256
final Statement statement = statementOrBlock();
251257
return new ForeachArrayStatement(variable, container, statement);
252258
}
253259

254260
private ForeachMapStatement foreachMapStatement() {
261+
// for k, v : map
255262
boolean optParentheses = match(TokenType.LPAREN);
256263
final String key = consume(TokenType.WORD).getText();
257264
consume(TokenType.COMMA);
258265
final String value = consume(TokenType.WORD).getText();
259266
consume(TokenType.COLON);
260267
final Expression container = expression();
261-
if (optParentheses) consume(TokenType.RPAREN); // close opt parentheses
268+
if (optParentheses) {
269+
consume(TokenType.RPAREN); // close opt parentheses
270+
}
262271
final Statement statement = statementOrBlock();
263272
return new ForeachMapStatement(key, value, container, statement);
264273
}
@@ -440,13 +449,14 @@ private Expression assignment() {
440449
}
441450

442451
private Expression assignmentStrict() {
452+
// x[0].prop += ...
443453
final int position = pos;
444454
final Expression targetExpr = qualifiedName();
445455
if (!(targetExpr instanceof Accessible)) {
446456
pos = position;
447457
return null;
448458
}
449-
459+
450460
final TokenType currentType = get(0).getType();
451461
if (!ASSIGN_OPERATORS.containsKey(currentType)) {
452462
pos = position;
@@ -696,13 +706,15 @@ private Expression primary() {
696706
}
697707

698708
if (match(TokenType.COLONCOLON)) {
709+
// ::method reference
699710
final String functionName = consume(TokenType.WORD).getText();
700711
return new FunctionReferenceExpression(functionName);
701712
}
702713
if (match(TokenType.MATCH)) {
703714
return match();
704715
}
705716
if (match(TokenType.DEF)) {
717+
// anonymous function def(args) ...
706718
final Arguments arguments = arguments();
707719
final Statement statement = statementBody();
708720
return new ValueExpression(new UserDefinedFunction(arguments, statement));
@@ -818,14 +830,18 @@ private Number createNumber(String text, int radix) {
818830

819831
private Token consume(TokenType type) {
820832
final Token current = get(0);
821-
if (type != current.getType()) throw new ParseException("Token " + current + " doesn't match " + type);
833+
if (type != current.getType()) {
834+
throw new ParseException("Token " + current + " doesn't match " + type);
835+
}
822836
pos++;
823837
return current;
824838
}
825839

826840
private boolean match(TokenType type) {
827841
final Token current = get(0);
828-
if (type != current.getType()) return false;
842+
if (type != current.getType()) {
843+
return false;
844+
}
829845
pos++;
830846
return true;
831847
}

src/main/java/com/annimon/ownlang/parser/ast/FunctionalExpression.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public Value eval() {
4343
final Function f = consumeFunction(functionExpr);
4444
CallStack.enter(functionExpr.toString(), f);
4545
try {
46-
return f.execute(values);
46+
final Value result = f.execute(values);
47+
CallStack.exit();
48+
return result;
4749
} catch (ArgumentsMismatchException | TypeException | VariableDoesNotExistsException ex) {
4850
throw new RuntimeException(ex.getMessage() + " in function " + functionExpr, ex);
49-
} finally {
50-
CallStack.exit();
5151
}
5252
}
5353

0 commit comments

Comments
 (0)