mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 08:44:20 +03:00
Небольшие исправления
This commit is contained in:
parent
4eabeca2ac
commit
5cada737b5
@ -219,12 +219,15 @@ public final class Parser {
|
|||||||
|
|
||||||
private Statement forStatement() {
|
private Statement forStatement() {
|
||||||
int foreachIndex = lookMatch(0, TokenType.LPAREN) ? 1 : 0;
|
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)
|
// for v : arr || for (v : arr)
|
||||||
return foreachArrayStatement();
|
return foreachArrayStatement();
|
||||||
}
|
}
|
||||||
if (lookMatch(foreachIndex, TokenType.WORD) && lookMatch(foreachIndex + 1, TokenType.COMMA)
|
if (lookMatch(foreachIndex, TokenType.WORD)
|
||||||
&& lookMatch(foreachIndex + 2, TokenType.WORD) && lookMatch(foreachIndex + 3, TokenType.COLON)) {
|
&& lookMatch(foreachIndex + 1, TokenType.COMMA)
|
||||||
|
&& lookMatch(foreachIndex + 2, TokenType.WORD)
|
||||||
|
&& lookMatch(foreachIndex + 3, TokenType.COLON)) {
|
||||||
// for key, value : arr || for (key, value : arr)
|
// for key, value : arr || for (key, value : arr)
|
||||||
return foreachMapStatement();
|
return foreachMapStatement();
|
||||||
}
|
}
|
||||||
@ -242,23 +245,29 @@ public final class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ForeachArrayStatement foreachArrayStatement() {
|
private ForeachArrayStatement foreachArrayStatement() {
|
||||||
|
// for x : arr
|
||||||
boolean optParentheses = match(TokenType.LPAREN);
|
boolean optParentheses = match(TokenType.LPAREN);
|
||||||
final String variable = consume(TokenType.WORD).getText();
|
final String variable = consume(TokenType.WORD).getText();
|
||||||
consume(TokenType.COLON);
|
consume(TokenType.COLON);
|
||||||
final Expression container = expression();
|
final Expression container = expression();
|
||||||
if (optParentheses) consume(TokenType.RPAREN); // close opt parentheses
|
if (optParentheses) {
|
||||||
|
consume(TokenType.RPAREN); // close opt parentheses
|
||||||
|
}
|
||||||
final Statement statement = statementOrBlock();
|
final Statement statement = statementOrBlock();
|
||||||
return new ForeachArrayStatement(variable, container, statement);
|
return new ForeachArrayStatement(variable, container, statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ForeachMapStatement foreachMapStatement() {
|
private ForeachMapStatement foreachMapStatement() {
|
||||||
|
// for k, v : map
|
||||||
boolean optParentheses = match(TokenType.LPAREN);
|
boolean optParentheses = match(TokenType.LPAREN);
|
||||||
final String key = consume(TokenType.WORD).getText();
|
final String key = consume(TokenType.WORD).getText();
|
||||||
consume(TokenType.COMMA);
|
consume(TokenType.COMMA);
|
||||||
final String value = consume(TokenType.WORD).getText();
|
final String value = consume(TokenType.WORD).getText();
|
||||||
consume(TokenType.COLON);
|
consume(TokenType.COLON);
|
||||||
final Expression container = expression();
|
final Expression container = expression();
|
||||||
if (optParentheses) consume(TokenType.RPAREN); // close opt parentheses
|
if (optParentheses) {
|
||||||
|
consume(TokenType.RPAREN); // close opt parentheses
|
||||||
|
}
|
||||||
final Statement statement = statementOrBlock();
|
final Statement statement = statementOrBlock();
|
||||||
return new ForeachMapStatement(key, value, container, statement);
|
return new ForeachMapStatement(key, value, container, statement);
|
||||||
}
|
}
|
||||||
@ -440,6 +449,7 @@ public final class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Expression assignmentStrict() {
|
private Expression assignmentStrict() {
|
||||||
|
// x[0].prop += ...
|
||||||
final int position = pos;
|
final int position = pos;
|
||||||
final Expression targetExpr = qualifiedName();
|
final Expression targetExpr = qualifiedName();
|
||||||
if (!(targetExpr instanceof Accessible)) {
|
if (!(targetExpr instanceof Accessible)) {
|
||||||
@ -696,6 +706,7 @@ public final class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (match(TokenType.COLONCOLON)) {
|
if (match(TokenType.COLONCOLON)) {
|
||||||
|
// ::method reference
|
||||||
final String functionName = consume(TokenType.WORD).getText();
|
final String functionName = consume(TokenType.WORD).getText();
|
||||||
return new FunctionReferenceExpression(functionName);
|
return new FunctionReferenceExpression(functionName);
|
||||||
}
|
}
|
||||||
@ -703,6 +714,7 @@ public final class Parser {
|
|||||||
return match();
|
return match();
|
||||||
}
|
}
|
||||||
if (match(TokenType.DEF)) {
|
if (match(TokenType.DEF)) {
|
||||||
|
// anonymous function def(args) ...
|
||||||
final Arguments arguments = arguments();
|
final Arguments arguments = arguments();
|
||||||
final Statement statement = statementBody();
|
final Statement statement = statementBody();
|
||||||
return new ValueExpression(new UserDefinedFunction(arguments, statement));
|
return new ValueExpression(new UserDefinedFunction(arguments, statement));
|
||||||
@ -818,14 +830,18 @@ public final class Parser {
|
|||||||
|
|
||||||
private Token consume(TokenType type) {
|
private Token consume(TokenType type) {
|
||||||
final Token current = get(0);
|
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++;
|
pos++;
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean match(TokenType type) {
|
private boolean match(TokenType type) {
|
||||||
final Token current = get(0);
|
final Token current = get(0);
|
||||||
if (type != current.getType()) return false;
|
if (type != current.getType()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
pos++;
|
pos++;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -43,11 +43,11 @@ public final class FunctionalExpression extends InterruptableNode implements Exp
|
|||||||
final Function f = consumeFunction(functionExpr);
|
final Function f = consumeFunction(functionExpr);
|
||||||
CallStack.enter(functionExpr.toString(), f);
|
CallStack.enter(functionExpr.toString(), f);
|
||||||
try {
|
try {
|
||||||
return f.execute(values);
|
final Value result = f.execute(values);
|
||||||
|
CallStack.exit();
|
||||||
|
return result;
|
||||||
} catch (ArgumentsMismatchException | TypeException | VariableDoesNotExistsException ex) {
|
} catch (ArgumentsMismatchException | TypeException | VariableDoesNotExistsException ex) {
|
||||||
throw new RuntimeException(ex.getMessage() + " in function " + functionExpr, ex);
|
throw new RuntimeException(ex.getMessage() + " in function " + functionExpr, ex);
|
||||||
} finally {
|
|
||||||
CallStack.exit();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user