summaryrefslogtreecommitdiff
path: root/parser/compiler.go
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2024-06-19 13:55:21 +0200
committerMarc Vertes <mvertes@free.fr>2024-06-19 13:55:21 +0200
commit70625002b6b3ba280c700636ed8314f20e1384a7 (patch)
tree8218f793c1fc140fc8354f1cfac162d47c424d57 /parser/compiler.go
parent1a2b2cb565ebf701f43012e1fce5552398d622a9 (diff)
fix (parser): don't panic if assign of define untyped value
In case of defining or assigning to untyped value, the type has to be taken from the source value instead of the target value. The vm test coverage has also been slightly improved.
Diffstat (limited to 'parser/compiler.go')
-rw-r--r--parser/compiler.go17
1 files changed, 13 insertions, 4 deletions
diff --git a/parser/compiler.go b/parser/compiler.go
index 3b9a32d..aa16888 100644
--- a/parser/compiler.go
+++ b/parser/compiler.go
@@ -61,13 +61,14 @@ func (c *Compiler) Codegen(tokens Tokens) (err error) {
case lang.String:
s := t.Block()
+ v := vm.Value{Data: reflect.ValueOf(s), Type: vm.TypeOf(s)}
i, ok := c.strings[s]
if !ok {
i = len(c.Data)
- c.Data = append(c.Data, vm.ValueOf(s))
+ c.Data = append(c.Data, v)
c.strings[s] = i
}
- push(&symbol{kind: symConst, value: vm.ValueOf(s)})
+ push(&symbol{kind: symConst, value: v})
emit(int64(t.Pos), vm.Dup, int64(i))
case lang.Add:
@@ -144,7 +145,11 @@ func (c *Compiler) Codegen(tokens Tokens) (err error) {
// TODO: support assignment to local, composite objects.
st := tokens[i-1]
l := len(c.Data)
- typ := pop().Type
+ d := pop()
+ typ := d.Type
+ if typ == nil {
+ typ = d.value.Type
+ }
v := vm.NewValue(typ)
c.addSym(l, st.Str, v, symVar, typ, false)
c.Data = append(c.Data, v)
@@ -160,7 +165,11 @@ func (c *Compiler) Codegen(tokens Tokens) (err error) {
if !ok {
return fmt.Errorf("symbol not found: %s", st.Str)
}
- typ := pop().Type
+ d := pop()
+ typ := d.Type
+ if typ == nil {
+ typ = d.value.Type
+ }
if s.Type == nil {
s.Type = typ
s.value = vm.NewValue(typ)