Skip to content

Commit cb116d1

Browse files
committed
fixed a but in equality operator
1 parent 5075056 commit cb116d1

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

docs/examples.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ <h2>Code Examples</h2>
3838

3939
# Fibonacci
4040
fibonacci = fun(n) {
41-
if n <= 1 then n
41+
if n <= 1 then return = n
4242
fibonacci(n - 1) + fibonacci(n - 2)
4343
}
44-
print(fibonacci(10)) # Outputs 55
44+
print(fibonacci(10)) # output 55
4545
</code>
4646
</pre>
4747
</section>

evaluator.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ func (e *Evaluator) evaluate(exp *Expr, env *Environment) interface{} {
124124
return env.Set(exp.Left.Value.(string), e.evaluate(exp.Right, env), exp)
125125

126126
case Binary:
127-
return applyBinaryOp(exp.Operator,
128-
e.evaluate(exp.Left, env),
129-
e.evaluate(exp.Right, env), exp)
127+
a := e.evaluate(exp.Left, env)
128+
b := e.evaluate(exp.Right, env)
129+
result := applyBinaryOp(exp.Operator, a, b, exp)
130+
return result
130131

131132
case Unary:
132133
return applyUnaryOp(exp.Operator,
@@ -378,9 +379,9 @@ func applyBinaryOp(op string, a, b interface{}, exp *Expr) interface{} {
378379
case ">=":
379380
return num(a) >= num(b)
380381
case "==":
381-
return a == b
382+
return num(a) == num(b)
382383
case "!=":
383-
return a != b
384+
return num(a) != num(b)
384385
default:
385386
Error(exp, "Can't apply operator %s", op)
386387
return nil

example/test.rune

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
factorial = fun(n) {
2+
if n == 0 then return = 1
3+
return = n * factorial(n - 1)
4+
}
5+
println(factorial(5)) # Outputs 120
6+
7+
exit()
8+
9+
fibonacci = fun(n) {
10+
if n <= 1 then return = n
11+
fibonacci(n - 1) + fibonacci(n - 2)
12+
}
13+
print(fibonacci(10)) # output 55
114

215
a = 10
316

0 commit comments

Comments
 (0)