compiler: allow to declare global variables in multiple files

Traverse and count globals across all used files.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2020-07-28 10:59:21 +03:00
parent babd84ec10
commit 7009417325
6 changed files with 36 additions and 8 deletions

View file

@ -28,20 +28,18 @@ func (c *codegen) newGlobal(name string) {
// traverseGlobals visits and initializes global variables.
// and returns number of variables initialized.
func (c *codegen) traverseGlobals(fs ...*ast.File) int {
func (c *codegen) traverseGlobals() int {
var n int
for _, f := range fs {
c.ForEachFile(func(f *ast.File) {
n += countGlobals(f)
}
})
if n != 0 {
if n > 255 {
c.prog.BinWriter.Err = errors.New("too many global variables")
return 0
}
emit.Instruction(c.prog.BinWriter, opcode.INITSSLOT, []byte{byte(n)})
for _, f := range fs {
c.convertGlobals(f)
}
c.ForEachFile(c.convertGlobals)
}
return n
}