Add set index in list, and update lib+examples+tests

This commit is contained in:
Neemek 2026-08-20 13:44:10 +02:00
parent 09ac16a42d
commit 3bccc227ba
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
12 changed files with 117 additions and 60 deletions

View file

@ -977,6 +977,41 @@ func (c *Compiler) compileAssignFromStack(to Node, sig TypeSignature, declare bo
}
return sig, nil
case IndexNodeType:
if declare {
return nil, c.error(fmt.Sprintf("cannot declare an indexed item"), to)
}
n := to.(*IndexNode)
ssig, err := c.compile(n.source)
if err != nil {
return nil, err
}
isig, err := c.compile(n.index)
if err != nil {
return nil, err
}
if isig.Type() != TypeInteger {
return nil, c.error(fmt.Sprintf("cannot index into list with non-integer (%s)", isig), n.index)
}
switch ssig.Type() {
case TypeList:
lsig := ssig.(*ListSignature)
if !c.typeMatches(sig, lsig.Contents) {
return nil, c.error(fmt.Sprintf("cannot assign value of type %s to list with items of type %s", sig, lsig.Contents), to)
}
c.add(InstructionSetIndexList)
default:
return nil, c.error(fmt.Sprintf("cannot set index of %s", ssig.Type()), n.source)
}
return sig, nil
default:
return nil, c.error(fmt.Sprintf("cannot assign to %s", to.Type()), to)
}