Skip to content

Commit fd0b8bd

Browse files
committed
Добавлен оператор ссылки на фукнцию ::
1 parent 29a80a7 commit fd0b8bd

File tree

8 files changed

+51
-4
lines changed

8 files changed

+51
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/dist/

program.own

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,4 @@ print "\n"
9090
print function(map["+"], 4, 5)
9191
print "\n"
9292
foreach(map, def(op, func) = echo (4, op, 5, "=", func(4,5)))
93+
foreach(arr, ::echo)

src/com/annimon/ownlang/parser/Lexer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public final class Lexer {
4545
OPERATORS.put("<=", TokenType.LTEQ);
4646
OPERATORS.put(">=", TokenType.GTEQ);
4747

48+
OPERATORS.put("::", TokenType.COLONCOLON);
49+
4850
OPERATORS.put("&&", TokenType.AMPAMP);
4951
OPERATORS.put("||", TokenType.BARBAR);
5052

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ private Expression primary() {
426426
if (match(TokenType.TEXT)) {
427427
return new ValueExpression(current.getText());
428428
}
429+
if (match(TokenType.COLONCOLON)) {
430+
final String functionName = consume(TokenType.WORD).getText();
431+
return new FunctionReferenceExpression(functionName);
432+
}
429433
if (match(TokenType.DEF)) {
430434
consume(TokenType.LPAREN);
431435
final List<String> argNames = new ArrayList<>();

src/com/annimon/ownlang/parser/TokenType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public enum TokenType {
5151

5252
QUESTION, // ?
5353
COLON, // :
54-
54+
COLONCOLON, // ::
55+
5556
LPAREN, // (
5657
RPAREN, // )
5758
LBRACKET, // [
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.annimon.ownlang.parser.ast;
2+
3+
import com.annimon.ownlang.lib.*;
4+
5+
/**
6+
*
7+
* @author aNNiMON
8+
*/
9+
public final class FunctionReferenceExpression implements Expression {
10+
11+
public final String name;
12+
13+
public FunctionReferenceExpression(String name) {
14+
this.name = name;
15+
}
16+
17+
@Override
18+
public FunctionValue eval() {
19+
return new FunctionValue(Functions.get(name));
20+
}
21+
22+
@Override
23+
public void accept(Visitor visitor) {
24+
visitor.visit(this);
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "::" + name;
30+
}
31+
}

src/com/annimon/ownlang/parser/ast/Visitor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public interface Visitor {
1818
void visit(DoWhileStatement s);
1919
void visit(ForStatement s);
2020
void visit(FunctionDefineStatement s);
21+
void visit(FunctionReferenceExpression e);
2122
void visit(FunctionStatement s);
2223
void visit(FunctionalExpression s);
2324
void visit(IfStatement s);

src/com/annimon/ownlang/parser/visitors/AbstractVisitor.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.annimon.ownlang.parser.ast.*;
44

5+
import java.util.Map;
6+
57
/**
68
*
79
* @author aNNiMON
@@ -79,6 +81,10 @@ public void visit(FunctionDefineStatement s) {
7981
s.body.accept(this);
8082
}
8183

84+
@Override
85+
public void visit(FunctionReferenceExpression e) {
86+
}
87+
8288
@Override
8389
public void visit(FunctionStatement s) {
8490
s.function.accept(this);
@@ -102,9 +108,9 @@ public void visit(IfStatement s) {
102108

103109
@Override
104110
public void visit(MapExpression s) {
105-
for (Expression key : s.elements.keySet()) {
106-
key.accept(this);
107-
s.elements.get(key).accept(this);
111+
for (Map.Entry<Expression, Expression> entry : s.elements.entrySet()) {
112+
entry.getKey().accept(this);
113+
entry.getValue().accept(this);
108114
}
109115
}
110116

0 commit comments

Comments
 (0)