compiler: manage variables in a separate varScope struct

Abstract var scope management from the funcScope.
This commit is contained in:
Evgenii Stratonikov 2020-06-29 16:45:27 +03:00
parent 8065114da6
commit 1ee4acbdbc
3 changed files with 72 additions and 20 deletions

View file

@ -166,10 +166,9 @@ func (c *codegen) emitStoreStructField(i int) {
// according to current scope.
func (c *codegen) getVarIndex(name string) (varType, int) {
if c.scope != nil {
if i, ok := c.scope.arguments[name]; ok {
return varArgument, i
} else if i, ok := c.scope.locals[name]; ok {
return varLocal, i
vt, val := c.scope.vars.getVarIndex(name)
if val >= 0 {
return vt, val
}
}
if i, ok := c.globals[name]; ok {
@ -311,6 +310,9 @@ func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl) {
emit.Instruction(c.prog.BinWriter, opcode.INITSLOT, []byte{byte(sizeLoc), byte(sizeArg)})
}
f.vars.newScope()
defer f.vars.dropScope()
// We need to handle methods, which in Go, is just syntactic sugar.
// The method receiver will be passed in as first argument.
// We check if this declaration has a receiver and load it into scope.