If can't local variable not find in compilation, assume it's a method call.
@@ -13,14 +13,10 @@ type LocalTable struct {
count int
}
-func (lt *LocalTable) Get(v string) int {
+func (lt *LocalTable) Get(v string) (int, bool) {
i, ok := lt.store[v]
- if !ok {
- panic(fmt.Errorf("Can't find %s in local table.", v))
- }
-
- return i
+ return i, ok
}
func (lt *LocalTable) Set(val string) int {
@@ -148,8 +144,19 @@ func (cg *CodeGenerator) compileDefStmt(stmt *ast.DefStatement, scope *Scope) {
func (cg *CodeGenerator) compileExpression(is *InstructionSet, exp ast.Expression, scope *Scope) {
switch exp := exp.(type) {
case *ast.Identifier:
- value := fmt.Sprintf("%d", scope.LocalTable.Get(exp.Value))
+ index, ok := scope.LocalTable.Get(exp.Value)
+
+ // it's local variable
+ if ok {
+ value := fmt.Sprintf("%d", index)
is.Define("getlocal", value)
+ return
+ }
+
+ // otherwise it's a method call
+ is.Define("putself")
+ is.Define("send", exp.Value, 0)
+
case *ast.Constant:
is.Define("getconstant", exp.Value)
case *ast.InstanceVariable:
