From 5cada737b506600eca8923e097d1ba9134968976 Mon Sep 17 00:00:00 2001 From: Victor Date: Sat, 5 Jan 2019 20:37:15 +0200 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C=D1=88?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/annimon/ownlang/parser/Parser.java | 32 ++++++++++++++----- .../parser/ast/FunctionalExpression.java | 6 ++-- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/annimon/ownlang/parser/Parser.java b/src/main/java/com/annimon/ownlang/parser/Parser.java index 58b35ae..23b2a21 100644 --- a/src/main/java/com/annimon/ownlang/parser/Parser.java +++ b/src/main/java/com/annimon/ownlang/parser/Parser.java @@ -219,12 +219,15 @@ public final class Parser { private Statement forStatement() { int foreachIndex = lookMatch(0, TokenType.LPAREN) ? 1 : 0; - if (lookMatch(foreachIndex, TokenType.WORD) && lookMatch(foreachIndex + 1, TokenType.COLON)) { + if (lookMatch(foreachIndex, TokenType.WORD) + && lookMatch(foreachIndex + 1, TokenType.COLON)) { // for v : arr || for (v : arr) return foreachArrayStatement(); } - if (lookMatch(foreachIndex, TokenType.WORD) && lookMatch(foreachIndex + 1, TokenType.COMMA) - && lookMatch(foreachIndex + 2, TokenType.WORD) && lookMatch(foreachIndex + 3, TokenType.COLON)) { + if (lookMatch(foreachIndex, TokenType.WORD) + && lookMatch(foreachIndex + 1, TokenType.COMMA) + && lookMatch(foreachIndex + 2, TokenType.WORD) + && lookMatch(foreachIndex + 3, TokenType.COLON)) { // for key, value : arr || for (key, value : arr) return foreachMapStatement(); } @@ -242,23 +245,29 @@ public final class Parser { } private ForeachArrayStatement foreachArrayStatement() { + // for x : arr boolean optParentheses = match(TokenType.LPAREN); final String variable = consume(TokenType.WORD).getText(); consume(TokenType.COLON); final Expression container = expression(); - if (optParentheses) consume(TokenType.RPAREN); // close opt parentheses + if (optParentheses) { + consume(TokenType.RPAREN); // close opt parentheses + } final Statement statement = statementOrBlock(); return new ForeachArrayStatement(variable, container, statement); } private ForeachMapStatement foreachMapStatement() { + // for k, v : map boolean optParentheses = match(TokenType.LPAREN); final String key = consume(TokenType.WORD).getText(); consume(TokenType.COMMA); final String value = consume(TokenType.WORD).getText(); consume(TokenType.COLON); final Expression container = expression(); - if (optParentheses) consume(TokenType.RPAREN); // close opt parentheses + if (optParentheses) { + consume(TokenType.RPAREN); // close opt parentheses + } final Statement statement = statementOrBlock(); return new ForeachMapStatement(key, value, container, statement); } @@ -440,13 +449,14 @@ public final class Parser { } private Expression assignmentStrict() { + // x[0].prop += ... final int position = pos; final Expression targetExpr = qualifiedName(); if (!(targetExpr instanceof Accessible)) { pos = position; return null; } - + final TokenType currentType = get(0).getType(); if (!ASSIGN_OPERATORS.containsKey(currentType)) { pos = position; @@ -696,6 +706,7 @@ public final class Parser { } if (match(TokenType.COLONCOLON)) { + // ::method reference final String functionName = consume(TokenType.WORD).getText(); return new FunctionReferenceExpression(functionName); } @@ -703,6 +714,7 @@ public final class Parser { return match(); } if (match(TokenType.DEF)) { + // anonymous function def(args) ... final Arguments arguments = arguments(); final Statement statement = statementBody(); return new ValueExpression(new UserDefinedFunction(arguments, statement)); @@ -818,14 +830,18 @@ public final class Parser { private Token consume(TokenType type) { final Token current = get(0); - if (type != current.getType()) throw new ParseException("Token " + current + " doesn't match " + type); + if (type != current.getType()) { + throw new ParseException("Token " + current + " doesn't match " + type); + } pos++; return current; } private boolean match(TokenType type) { final Token current = get(0); - if (type != current.getType()) return false; + if (type != current.getType()) { + return false; + } pos++; return true; } diff --git a/src/main/java/com/annimon/ownlang/parser/ast/FunctionalExpression.java b/src/main/java/com/annimon/ownlang/parser/ast/FunctionalExpression.java index 103ca47..eb4c5c4 100644 --- a/src/main/java/com/annimon/ownlang/parser/ast/FunctionalExpression.java +++ b/src/main/java/com/annimon/ownlang/parser/ast/FunctionalExpression.java @@ -43,11 +43,11 @@ public final class FunctionalExpression extends InterruptableNode implements Exp final Function f = consumeFunction(functionExpr); CallStack.enter(functionExpr.toString(), f); try { - return f.execute(values); + final Value result = f.execute(values); + CallStack.exit(); + return result; } catch (ArgumentsMismatchException | TypeException | VariableDoesNotExistsException ex) { throw new RuntimeException(ex.getMessage() + " in function " + functionExpr, ex); - } finally { - CallStack.exit(); } }