Skip to content

Commit

Permalink
edge cases with preprocessing and typechecking
Browse files Browse the repository at this point in the history
  • Loading branch information
mbellotti committed Jan 15, 2023
1 parent 680e214 commit c416a46
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
33 changes: 24 additions & 9 deletions preprocess/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,20 +562,26 @@ func (p *Processor) walk(n ast.Node) (ast.Node, error) {
p.Specs[p.trail.CurrentSpec()] = NewSpecRecord()
}

order := node.Order

if p.initialPass {
pro, err = p.walk(node.Value)
if err != nil {
return node, err
}
pn := p.buildIdContext(p.trail.CurrentSpec())
pn = append(pn, node.Name)
node.Value = pro.(*ast.Identifier)
node.ProcessedName = append(pn, node.Name)
node.ProcessedName = pn

if !node.Complex && p.inGlobal {
local := strings.Join(pn[0:2], "_")
p.localIdents[local] = order
}

return node, err
}

order := node.Order

var key string
importSpec := p.Specs[node.Value.Spec] //Where the struct definition lives

Expand Down Expand Up @@ -662,6 +668,7 @@ func (p *Processor) walk(n ast.Node) (ast.Node, error) {
}
spec.UpdateStock(key, properties2)
pro.ProcessedName = pn

p.scope = oldScope
return pro, err
case "FLOW":
Expand Down Expand Up @@ -730,6 +737,7 @@ func (p *Processor) walk(n ast.Node) (ast.Node, error) {
}
spec.UpdateFlow(key, properties2)
pro.ProcessedName = pn

p.scope = oldScope
return pro, err
default:
Expand Down Expand Up @@ -879,11 +887,17 @@ func (p *Processor) walk(n ast.Node) (ast.Node, error) {
return node, fmt.Errorf("can't find a struct instance named %s", node.Parent)
}
case *ast.Identifier:
if !p.initialPass {
return node, err
}
var spec *SpecRecord

spec := p.Specs[node.Spec]
// Check to see if this is a constant from
// an import
im := p.Specs[node.Spec]
_, check := im.FetchConstant(node.Value)
if check == nil {
spec = im
} else {
spec = p.Specs[p.trail.CurrentSpec()]
}
rawid := p.buildIdContext(spec.Id())

if p.inFunc {
Expand All @@ -908,7 +922,7 @@ func (p *Processor) walk(n ast.Node) (ast.Node, error) {
return node, err
}

spec := p.Specs[node.Name.Spec]
spec := p.Specs[p.trail.CurrentSpec()]
rawid := p.buildIdContext(spec.Id())

rawid = append(rawid, node.Name.Value)
Expand Down Expand Up @@ -962,7 +976,8 @@ func (p *Processor) walk(n ast.Node) (ast.Node, error) {

// State charts tend to create endless loops by design
// short-curcuit if we've already processed this node
if alreadyNamed(branch.(ast.Nameable).RawId(), rawid) {
brName := branch.(ast.Nameable).RawId()
if brName[0] == rawid[0] {
return node, err
}

Expand Down
8 changes: 0 additions & 8 deletions preprocess/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,6 @@ func (sr *SpecRecord) UpdateComponent(name string, val map[string]ast.Node) erro
return fmt.Errorf("no component found with name %s in spec %s", name, sr.SpecName)
}

func (sr *SpecRecord) UpdateConstant(name string, val ast.Node) error {
if sr.Constants[name] != nil {
sr.Constants[name] = val
return nil
}
return fmt.Errorf("no constant found with name %s in spec %s", name, sr.SpecName)
}

func (sr *SpecRecord) UpdateVar(rawid []string, ty string, val ast.Node) error {
var err error
name := strings.Join(rawid[1:len(rawid)-1], "_")
Expand Down
17 changes: 0 additions & 17 deletions preprocess/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,6 @@ func TestSpecRecordUpdate(t *testing.T) {
if i3.Value != 2 {
t.Fatalf("property bar has incorrect value got=%d want=2", i3.Value)
}

sr.AddConstant("test4", &ast.IntegerLiteral{Value: 1})
t4, _ := sr.FetchConstant("test4")
sr.UpdateConstant("test4", &ast.IntegerLiteral{Value: 2})
t4a, _ := sr.FetchConstant("test4")
if t4 == t4a {
t.Fatal("constant not found in spec record")
}

i4, ok := t4a.(*ast.IntegerLiteral)
if !ok {
t.Fatalf("constant is incorrect type got=%T want=IntegerLiteral", t4a)
}

if i4.Value != 2 {
t.Fatalf("constant has incorrect value got=%d want=2", i4.Value)
}
}

func TestUpdateVar(t *testing.T) {
Expand Down
24 changes: 14 additions & 10 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,6 @@ func (c *Checker) isValue(exp interface{}) bool {

func (c *Checker) infer(exp interface{}) (ast.Node, error) {
switch node := exp.(type) {
/*case int64:
return &ast.Type{"INT", 1, nil}, nil
case float64:
scope := c.inferScope(node)
return &ast.Type{"FLOAT", scope, nil}, nil
case string:
return &ast.Type{"STRING", 0, nil}, nil
case bool:
return &ast.Type{"BOOL", 0, nil}, nil*/
case *ast.IntegerLiteral:
if node.InferredType == nil {
node.InferredType = &ast.Type{Type: "INT",
Expand Down Expand Up @@ -484,7 +475,17 @@ func (c *Checker) lookupType(node ast.Node) (*ast.Type, error) {
if err != nil {
return nil, fmt.Errorf("can't find node %s line:%d, col:%d", rawid, pos[0], pos[1])
}
return typeable(v), err

ret := typeable(v)
if ret == nil {
v2, err := c.typecheck(v)
if err != nil {
return nil, err
}
spec.UpdateVar(rawid, ty, v2)
return typeable(v2), err
}
return ret, err
}

func (c *Checker) inferFunction(f ast.Expression) (ast.Expression, error) {
Expand Down Expand Up @@ -549,6 +550,9 @@ func (c *Checker) inferFunction(f ast.Expression) (ast.Expression, error) {
return nil, err
}
right := typeable(nr)
if right == nil {
nr, err = c.infer(node.Right)
}

if node.Token.Type == "ASSIGN" { //In case of temp values
ty, _ := c.lookupType(node.Left)
Expand Down

0 comments on commit c416a46

Please sign in to comment.