Skip to content

Commit d2a159b

Browse files
committed
Исправлено некорректное обновление переменных внутри функций
1 parent 3c6571c commit d2a159b

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public Value execute(Value... values) {
4444
try {
4545
Variables.push();
4646
for (int i = 0; i < size; i++) {
47-
Variables.set(getArgsName(i), values[i]);
47+
Variables.define(getArgsName(i), values[i]);
4848
}
4949
// Optional args if exists
5050
for (int i = size; i < totalArgsCount; i++) {
5151
final Argument arg = arguments.get(i);
52-
Variables.set(arg.getName(), arg.getValueExpr().eval());
52+
Variables.define(arg.getName(), arg.getValueExpr().eval());
5353
}
5454
body.execute();
5555
return NumberValue.ZERO;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private void execute(ArrayValue array) {
4141
for (int i = 0; i < size; i++) {
4242
final String variable = variables.get(i);
4343
if (variable != null) {
44-
Variables.set(variable, array.get(i));
44+
Variables.define(variable, array.get(i));
4545
}
4646
}
4747
}
@@ -50,7 +50,7 @@ private void execute(MapValue map) {
5050
for (Map.Entry<Value, Value> entry : map) {
5151
final String variable = variables.get(i);
5252
if (variable != null) {
53-
Variables.set(variable, new ArrayValue(
53+
Variables.define(variable, new ArrayValue(
5454
new Value[] { entry.getKey(), entry.getValue() }
5555
));
5656
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public Value eval() {
4747
return evalResult(p.result);
4848
}
4949
} else {
50-
Variables.set(pattern.variable, value);
50+
Variables.define(pattern.variable, value);
5151
if (optMatches(p)) {
5252
final Value result = evalResult(p.result);;
5353
Variables.remove(pattern.variable);
@@ -84,7 +84,7 @@ private boolean matchListPattern(ArrayValue array, ListPattern p) {
8484

8585
case 1: // match arr { case [x]: x = arr ... }
8686
final String variable = parts.get(0);
87-
Variables.set(variable, array);
87+
Variables.define(variable, array);
8888
if (optMatches(p)) {
8989
return true;
9090
}
@@ -107,7 +107,7 @@ private boolean matchListPattern(ArrayValue array, ListPattern p) {
107107
private boolean matchListPatternEqualsSize(ListPattern p, List<String> parts, int partsSize, ArrayValue array) {
108108
// Set variables
109109
for (int i = 0; i < partsSize; i++) {
110-
Variables.set(parts.get(i), array.get(i));
110+
Variables.define(parts.get(i), array.get(i));
111111
}
112112
if (optMatches(p)) {
113113
// Clean up will be provided after evaluate result
@@ -124,14 +124,14 @@ private boolean matchListPatternWithTail(ListPattern p, List<String> parts, int
124124
// Set element variables
125125
final int lastPart = partsSize - 1;
126126
for (int i = 0; i < lastPart; i++) {
127-
Variables.set(parts.get(i), array.get(i));
127+
Variables.define(parts.get(i), array.get(i));
128128
}
129129
// Set tail variable
130130
final ArrayValue tail = new ArrayValue(arraySize - partsSize + 1);
131131
for (int i = lastPart; i < arraySize; i++) {
132132
tail.set(i - lastPart, array.get(i));
133133
}
134-
Variables.set(parts.get(lastPart), tail);
134+
Variables.define(parts.get(lastPart), tail);
135135
// Check optional condition
136136
if (optMatches(p)) {
137137
// Clean up will be provided after evaluate result

tests.own

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def testTypes() {
3232
assertSameType(0, 0.0)
3333
}
3434

35-
def testFail() {
35+
/*def testFail() {
3636
assertTrue(false)
37-
}
37+
}*/
3838

3939
def testScope() {
4040
x = 5
@@ -47,4 +47,13 @@ def testScope() {
4747
assertEquals(15, x)
4848
}
4949

50+
def testFibonacci() {
51+
def fib(n) {
52+
if n < 2 return n
53+
return fib(n-2) + fib(n-1)
54+
}
55+
assertEquals(3, fib(4))
56+
assertEquals(21, fib(8))
57+
}
58+
5059
println runTests()

0 commit comments

Comments
 (0)