diff --git a/compiler.go b/compiler.go index 53cc7d38..82b86d03 100644 --- a/compiler.go +++ b/compiler.go @@ -664,11 +664,23 @@ func (c *Compiler) compileAssign( if depth == 0 && exists { return c.errorf(node, "'%s' redeclared in this block", ident) } - symbol = c.symbolTable.Define(ident) + symbol = c.symbolTable.Define(ident, rhs[0]) } else { if !exists { return c.errorf(node, "unresolved reference '%s'", ident) + } else { + symbol, ok := c.symbolTable.store[ident] + if ok { + switch symbol.Expr.(type) { + case *parser.IntLit, *parser.FloatLit, *parser.CharLit: + break + default: + return c.errorf(node, "invalid operation: ++/-- (non-numeric type)") + + } + } } + } // +=, -=, *=, /= diff --git a/symbol_table.go b/symbol_table.go index 73aaad3b..4caf0ca7 100644 --- a/symbol_table.go +++ b/symbol_table.go @@ -1,5 +1,7 @@ package tengo +import "github.com/d5/tengo/v2/parser" + // SymbolScope represents a symbol scope. type SymbolScope string @@ -17,6 +19,7 @@ type Symbol struct { Scope SymbolScope Index int LocalAssigned bool // if the local symbol is assigned at least once + Expr parser.Expr } // SymbolTable represents a symbol table. @@ -38,7 +41,7 @@ func NewSymbolTable() *SymbolTable { } // Define adds a new symbol in the current scope. -func (t *SymbolTable) Define(name string) *Symbol { +func (t *SymbolTable) Define(name string, expr ...parser.Expr) *Symbol { symbol := &Symbol{Name: name, Index: t.nextIndex()} t.numDefinition++ @@ -58,6 +61,9 @@ func (t *SymbolTable) Define(name string) *Symbol { } else { symbol.Scope = ScopeLocal } + if len(expr) > 0 { + symbol.Expr = expr[0] + } t.store[name] = symbol t.updateMaxDefs(symbol.Index + 1) return symbol