Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed/inc dec #356

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")

}
}
}

}

// +=, -=, *=, /=
Expand Down
8 changes: 7 additions & 1 deletion symbol_table.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tengo

import "github.com/d5/tengo/v2/parser"

// SymbolScope represents a symbol scope.
type SymbolScope string

Expand All @@ -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.
Expand All @@ -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++

Expand All @@ -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
Expand Down