diff --git a/calc.go b/calc.go index d9d2275..82bda08 100644 --- a/calc.go +++ b/calc.go @@ -259,7 +259,7 @@ func (c *Calc) Eval(line string) { if contains(c.LuaFunctions, item) { // user provided custom lua functions - c.luafunc(item) + c.EvalLuaFunction(item) continue } @@ -393,6 +393,10 @@ func (c *Calc) DoFuncall(funcname string) error { return R.Err } + // don't forget to backup! + c.stack.Backup() + + // "pop" if batch { // get rid of stack c.stack.Clear() @@ -442,7 +446,7 @@ func (c *Calc) Debug(msg string) { } } -func (c *Calc) luafunc(funcname string) { +func (c *Calc) EvalLuaFunction(funcname string) { // called from calc loop var x float64 var err error diff --git a/calc_test.go b/calc_test.go index b8f48c3..a5e0958 100644 --- a/calc_test.go +++ b/calc_test.go @@ -20,6 +20,8 @@ package main import ( "fmt" "testing" + + lua "github.com/yuin/gopher-lua" ) func TestCommentsAndWhitespace(t *testing.T) { @@ -104,6 +106,7 @@ func TestCalc(t *testing.T) { exp float64 batch bool }{ + // ops { name: "plus", cmd: `15 15 +`, @@ -144,6 +147,8 @@ func TestCalc(t *testing.T) { cmd: `400 20 %+`, exp: 480, }, + + // math tests { name: "mod", cmd: `9 2 mod`, @@ -164,6 +169,20 @@ func TestCalc(t *testing.T) { cmd: `6 4 dim`, exp: 2, }, + + // constants tests + { + name: "pitimes2", + cmd: `Pi 2 *`, + exp: 6.283185307179586, + }, + { + name: "pi+sqrt2", + cmd: `Pi Sqrt2 +`, + exp: 4.555806215962888, + }, + + // batch tests { name: "batch-sum", cmd: `2 2 2 2 sum`, @@ -194,6 +213,34 @@ func TestCalc(t *testing.T) { exp: 5, batch: true, }, + + // stack tests + { + name: "use-vars", + cmd: `10 >TEN clear 5 = 0; i-- { + s.linklist.PushFront(items[i]) + } }