Add set index in list, and update lib+examples+tests
This commit is contained in:
parent
8d12c0bdf8
commit
0f905a7fea
12 changed files with 117 additions and 60 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue