Skip to content

Commit

Permalink
wip support struct field selection
Browse files Browse the repository at this point in the history
  • Loading branch information
hidetatz committed Sep 6, 2023
1 parent 267d95e commit a3da295
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
34 changes: 23 additions & 11 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,22 +656,34 @@ func procSelector(mod *module, n *ndSelector) (procResult, shibaErr) {
return nil, err
}

// currently selector typ must be mod.
// In the future this should support struct/field.
if selector.typ != tMod {
return nil, &errSimple{msg: fmt.Sprintf("selector %s is not a module", selector), l: n.token().loc}
}
if selector.typ == tMod {
target, err := procAsObj(selector.mod, n.target)
if err != nil {
return nil, err
}

target, err := procAsObj(selector.mod, n.target)
if err != nil {
return nil, err
if target != nil {
return &prObj{o: target}, nil
}

return nil, nil
}

if target != nil {
return &prObj{o: target}, nil
if selector.typ == tStruct {
field, ok := n.target.(*ndIdent)
if !ok {
return nil, &errSimple{msg: fmt.Sprintf("%s must be an identifier", n.target), l: n.token().loc}
}

f, ok := selector.fields[field.ident]
if !ok {
return nil, &errSimple{msg: fmt.Sprintf("unknown field name %s in %s", field.ident, selector), l: n.token().loc}
}

return &prObj{o: f}, nil
}

return nil, nil
return nil, &errSimple{msg: fmt.Sprintf("selector %s is not a module", selector), l: n.token().loc}
}

func procFuncall(mod *module, n *ndFuncall) (procResult, shibaErr) {
Expand Down
3 changes: 2 additions & 1 deletion tests/struct.sb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct person{
}

p = person{name: "alice", age: 3}
print(p)
as("alice", p.name)
as(3, p.age)

print("struct test succeeded")

0 comments on commit a3da295

Please sign in to comment.