fix if result typing + fix indexing

This commit is contained in:
Neemek 2026-07-13 21:29:57 +02:00
parent 0bc8b2ef55
commit 7cb2be8415
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
2 changed files with 38 additions and 15 deletions

View file

@ -437,8 +437,10 @@ func (c *Compiler) compile(tree Node) (TypeSignature, error) {
c.putU16(jumpOverElse, uint16(c.ip-jumpOverElse-2))
if dot.Equal(ot) {
if dot.Contains(ot) {
return dot, nil
} else if ot.Contains(dot) {
return ot, nil
}
return &CompositeSignature{
@ -745,6 +747,15 @@ func (c *Compiler) compile(tree Node) (TypeSignature, error) {
st = c.resolveType(st)
it, err := c.compile(n.index)
if err != nil {
return nil, err
}
if it.Type() != TypeInteger {
return nil, c.error(fmt.Sprintf("can only index with integers (got %s, nice try)", it), n.index)
}
var output TypeSignature
switch st.Type() {
case TypeList:
@ -762,7 +773,7 @@ func (c *Compiler) compile(tree Node) (TypeSignature, error) {
in := n.index.(*IntegerNode)
i := in.value.Int64()
if i < 0 || i >= int64(len(sig.Contents)) {
if i < 0 || int64(len(sig.Contents)) <= i {
return nil, c.error(fmt.Sprintf("cannot index outside of tuple (%d items)", len(sig.Contents)), n.source)
}
@ -780,19 +791,15 @@ func (c *Compiler) compile(tree Node) (TypeSignature, error) {
}
}
}
c.add(InstructionIndexTuple)
case TypeString:
output = &StringSignature{}
c.add(InstructionIndexString)
default:
return nil, c.error(fmt.Sprintf("cannot index into value of type %s", st), n.source)
}
it, err := c.compile(n.index)
if err != nil {
return nil, err
}
if it.Type() != TypeInteger {
return nil, c.error(fmt.Sprintf("can only index with integers (got %s, nice try)", it), n.index)
}
return output, nil
case BreakpointNodeType: