Добавлен оператор ссылки на фукнцию ::

This commit is contained in:
Victor 2016-01-08 21:58:20 +02:00
parent 29a80a7bfe
commit fd0b8bd817
8 changed files with 51 additions and 4 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/dist/

View File

@ -90,3 +90,4 @@ print "\n"
print function(map["+"], 4, 5) print function(map["+"], 4, 5)
print "\n" print "\n"
foreach(map, def(op, func) = echo (4, op, 5, "=", func(4,5))) foreach(map, def(op, func) = echo (4, op, 5, "=", func(4,5)))
foreach(arr, ::echo)

View File

@ -45,6 +45,8 @@ public final class Lexer {
OPERATORS.put("<=", TokenType.LTEQ); OPERATORS.put("<=", TokenType.LTEQ);
OPERATORS.put(">=", TokenType.GTEQ); OPERATORS.put(">=", TokenType.GTEQ);
OPERATORS.put("::", TokenType.COLONCOLON);
OPERATORS.put("&&", TokenType.AMPAMP); OPERATORS.put("&&", TokenType.AMPAMP);
OPERATORS.put("||", TokenType.BARBAR); OPERATORS.put("||", TokenType.BARBAR);

View File

@ -426,6 +426,10 @@ public final class Parser {
if (match(TokenType.TEXT)) { if (match(TokenType.TEXT)) {
return new ValueExpression(current.getText()); return new ValueExpression(current.getText());
} }
if (match(TokenType.COLONCOLON)) {
final String functionName = consume(TokenType.WORD).getText();
return new FunctionReferenceExpression(functionName);
}
if (match(TokenType.DEF)) { if (match(TokenType.DEF)) {
consume(TokenType.LPAREN); consume(TokenType.LPAREN);
final List<String> argNames = new ArrayList<>(); final List<String> argNames = new ArrayList<>();

View File

@ -51,6 +51,7 @@ public enum TokenType {
QUESTION, // ? QUESTION, // ?
COLON, // : COLON, // :
COLONCOLON, // ::
LPAREN, // ( LPAREN, // (
RPAREN, // ) RPAREN, // )

View File

@ -0,0 +1,31 @@
package com.annimon.ownlang.parser.ast;
import com.annimon.ownlang.lib.*;
/**
*
* @author aNNiMON
*/
public final class FunctionReferenceExpression implements Expression {
public final String name;
public FunctionReferenceExpression(String name) {
this.name = name;
}
@Override
public FunctionValue eval() {
return new FunctionValue(Functions.get(name));
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
@Override
public String toString() {
return "::" + name;
}
}

View File

@ -18,6 +18,7 @@ public interface Visitor {
void visit(DoWhileStatement s); void visit(DoWhileStatement s);
void visit(ForStatement s); void visit(ForStatement s);
void visit(FunctionDefineStatement s); void visit(FunctionDefineStatement s);
void visit(FunctionReferenceExpression e);
void visit(FunctionStatement s); void visit(FunctionStatement s);
void visit(FunctionalExpression s); void visit(FunctionalExpression s);
void visit(IfStatement s); void visit(IfStatement s);

View File

@ -2,6 +2,8 @@ package com.annimon.ownlang.parser.visitors;
import com.annimon.ownlang.parser.ast.*; import com.annimon.ownlang.parser.ast.*;
import java.util.Map;
/** /**
* *
* @author aNNiMON * @author aNNiMON
@ -79,6 +81,10 @@ public abstract class AbstractVisitor implements Visitor {
s.body.accept(this); s.body.accept(this);
} }
@Override
public void visit(FunctionReferenceExpression e) {
}
@Override @Override
public void visit(FunctionStatement s) { public void visit(FunctionStatement s) {
s.function.accept(this); s.function.accept(this);
@ -102,9 +108,9 @@ public abstract class AbstractVisitor implements Visitor {
@Override @Override
public void visit(MapExpression s) { public void visit(MapExpression s) {
for (Expression key : s.elements.keySet()) { for (Map.Entry<Expression, Expression> entry : s.elements.entrySet()) {
key.accept(this); entry.getKey().accept(this);
s.elements.get(key).accept(this); entry.getValue().accept(this);
} }
} }