From 6a32a7bc5f6320902cd5c2910a1353a0f7039237 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Thu, 23 Nov 2023 17:56:35 +0100 Subject: 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. --- parser/decl.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'parser/decl.go') diff --git a/parser/decl.go b/parser/decl.go index 782b57a..54a2989 100644 --- a/parser/decl.go +++ b/parser/decl.go @@ -4,12 +4,15 @@ import ( "errors" "go/constant" "go/token" + "reflect" "strings" "github.com/gnolang/parscan/lang" "github.com/gnolang/parscan/scanner" ) +var nilValue = reflect.ValueOf(nil) + func (p *Parser) ParseConst(in Tokens) (out Tokens, err error) { if len(in) < 2 { return out, errors.New("missing expression") @@ -51,8 +54,8 @@ func (p *Parser) parseConstLine(in Tokens) (out Tokens, err error) { if errors.Is(err, MissingTypeErr) { for _, lt := range decl.Split(lang.Comma) { vars = append(vars, lt[0].Str) - // TODO: compute type from rhs - p.addSym(unsetAddr, strings.TrimPrefix(p.scope+"/"+lt[0].Str, "/"), nil, symConst, nil, false) + name := strings.TrimPrefix(p.scope+"/"+lt[0].Str, "/") + p.addSym(unsetAddr, name, nilValue, symConst, nil, false) } } else { return out, err @@ -75,7 +78,7 @@ func (p *Parser) parseConstLine(in Tokens) (out Tokens, err error) { kind: symConst, index: unsetAddr, cval: cval, - value: constValue(cval), + value: reflect.ValueOf(constValue(cval)), local: p.funcScope != "", used: true, } @@ -222,7 +225,7 @@ func (p *Parser) parseTypeLine(in Tokens) (out Tokens, err error) { if err != nil { return out, err } - p.addSym(unsetAddr, in[0].Str, nil, symType, typ, p.funcScope != "") + p.addSym(unsetAddr, in[0].Str, reflect.New(typ).Elem(), symType, typ, p.funcScope != "") return out, err } @@ -257,8 +260,13 @@ func (p *Parser) parseVarLine(in Tokens) (out Tokens, err error) { if errors.Is(err, MissingTypeErr) { for _, lt := range decl.Split(lang.Comma) { vars = append(vars, lt[0].Str) - // TODO: compute type from rhs - p.addSym(unsetAddr, strings.TrimPrefix(p.scope+"/"+lt[0].Str, "/"), nil, symVar, nil, false) + name := strings.TrimPrefix(p.scope+"/"+lt[0].Str, "/") + if p.funcScope == "" { + p.addSym(unsetAddr, name, nilValue, symVar, nil, false) + continue + } + p.addSym(p.framelen[p.funcScope], name, nilValue, symVar, nil, false) + p.framelen[p.funcScope]++ } } else { return out, err -- cgit v1.2.3