Skip to content

Commit 4004703

Browse files
committed
Исправлен вызов пользовательской функции
1 parent cf40faa commit 4004703

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/com/annimon/ownlang/lib/UserDefinedFunction.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,21 @@ public String getArgsName(int index) {
2828
}
2929

3030
@Override
31-
public Value execute(Value... args) {
31+
public Value execute(Value... values) {
32+
final int size = values.length;
33+
if (size != getArgsCount()) throw new RuntimeException("Args count mismatch");
34+
3235
try {
36+
Variables.push();
37+
for (int i = 0; i < size; i++) {
38+
Variables.set(getArgsName(i), values[i]);
39+
}
3340
body.execute();
3441
return NumberValue.ZERO;
3542
} catch (ReturnStatement rt) {
3643
return rt.getResult();
44+
} finally {
45+
Variables.pop();
3746
}
3847
}
3948

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,7 @@ public Value eval() {
4040
for (int i = 0; i < size; i++) {
4141
values[i] = arguments.get(i).eval();
4242
}
43-
44-
final Function function = getFunction(name);
45-
if (function instanceof UserDefinedFunction) {
46-
final UserDefinedFunction userFunction = (UserDefinedFunction) function;
47-
if (size != userFunction.getArgsCount()) throw new RuntimeException("Args count mismatch");
48-
49-
Variables.push();
50-
for (int i = 0; i < size; i++) {
51-
Variables.set(userFunction.getArgsName(i), values[i]);
52-
}
53-
final Value result = userFunction.execute(values);
54-
Variables.pop();
55-
return result;
56-
}
57-
return function.execute(values);
43+
return getFunction(name).execute(values);
5844
}
5945

6046
private Function getFunction(String key) {

0 commit comments

Comments
 (0)