Skip to content

Commit cd7382b

Browse files
committed
Получение функций из переменных
1 parent cf51c55 commit cd7382b

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.annimon.ownlang.parser.ast;
22

33
import com.annimon.ownlang.lib.Function;
4+
import com.annimon.ownlang.lib.FunctionValue;
45
import com.annimon.ownlang.lib.Functions;
6+
import static com.annimon.ownlang.lib.Functions.isExists;
57
import com.annimon.ownlang.lib.UserDefinedFunction;
68
import com.annimon.ownlang.lib.Value;
79
import com.annimon.ownlang.lib.Variables;
@@ -39,7 +41,7 @@ public Value eval() {
3941
values[i] = arguments.get(i).eval();
4042
}
4143

42-
final Function function = Functions.get(name);
44+
final Function function = getFunction(name);
4345
if (function instanceof UserDefinedFunction) {
4446
final UserDefinedFunction userFunction = (UserDefinedFunction) function;
4547
if (size != userFunction.getArgsCount()) throw new RuntimeException("Args count mismatch");
@@ -55,6 +57,15 @@ public Value eval() {
5557
return function.execute(values);
5658
}
5759

60+
private Function getFunction(String key) {
61+
if (Functions.isExists(key)) return Functions.get(key);
62+
if (Variables.isExists(key)) {
63+
final Value value = Variables.get(key);
64+
if (value instanceof FunctionValue) return ((FunctionValue)value).getValue();
65+
}
66+
throw new RuntimeException("Unknown function " + key);
67+
}
68+
5869
@Override
5970
public void accept(Visitor visitor) {
6071
visitor.visit(this);

0 commit comments

Comments
 (0)