diff options
| author | Marc Vertes <mvertes@free.fr> | 2023-11-23 17:56:35 +0100 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2023-11-24 09:12:46 +0100 |
| commit | 6a32a7bc5f6320902cd5c2910a1353a0f7039237 (patch) | |
| tree | 0fcce51e4d4f54d48d57a5dda8a896a35264f68b /parser/symbol.go | |
| parent | c548093d79edece3c1cbb7e4dc03d92fe45b1cc7 (diff) | |
parser: fix allocation of local variables
A 'New' instruction is added in VM to manage initialisation
of typed variables in the stack. The instantiated type symbols
are now added to global data. Accessing and setting values by
address is now working.
Diffstat (limited to 'parser/symbol.go')
| -rw-r--r-- | parser/symbol.go | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/parser/symbol.go b/parser/symbol.go index 7edcef3..6c5ab9b 100644 --- a/parser/symbol.go +++ b/parser/symbol.go @@ -22,12 +22,12 @@ const unsetAddr = -65535 type symbol struct { kind symKind - index int // address of symbol in frame - value any - cval constant.Value - Type reflect.Type - local bool // if true address is relative to local frame, otherwise global - used bool + index int // address of symbol in frame + value reflect.Value // + cval constant.Value // + Type reflect.Type // + local bool // if true address is relative to local frame, otherwise global + used bool // } func symtype(s *symbol) reflect.Type { @@ -37,11 +37,13 @@ func symtype(s *symbol) reflect.Type { return reflect.TypeOf(s.value) } -func (p *Parser) AddSym(i int, name string, v any) { p.addSym(i, name, v, symValue, nil, false) } +func (p *Parser) AddSym(i int, name string, v reflect.Value) { + p.addSym(i, name, v, symValue, nil, false) +} -func (p *Parser) addSym(i int, name string, v any, k symKind, t reflect.Type, local bool) { +func (p *Parser) addSym(i int, name string, v reflect.Value, k symKind, t reflect.Type, local bool) { name = strings.TrimPrefix(name, "/") - p.symbols[name] = &symbol{kind: k, index: i, local: local, value: v, Type: t, used: true} + p.symbols[name] = &symbol{kind: k, index: i, local: local, value: v, Type: t} } // getSym searches for an existing symbol starting from the deepest scope. @@ -72,9 +74,9 @@ func initUniverse() map[string]*symbol { "nil": {index: unsetAddr}, "iota": {kind: symConst, index: unsetAddr}, - "true": {index: unsetAddr, value: true, Type: reflect.TypeOf(true)}, - "false": {index: unsetAddr, value: false, Type: reflect.TypeOf(false)}, + "true": {index: unsetAddr, value: reflect.ValueOf(true), Type: reflect.TypeOf(true)}, + "false": {index: unsetAddr, value: reflect.ValueOf(false), Type: reflect.TypeOf(false)}, - "println": {index: unsetAddr, value: func(v ...any) { fmt.Println(v...) }}, + "println": {index: unsetAddr, value: reflect.ValueOf(func(v ...any) { fmt.Println(v...) })}, } } |
