Skip to content

Commit

Permalink
types and preprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
mbellotti committed Apr 14, 2023
1 parent f8252fe commit 4cf1223
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
1 change: 1 addition & 0 deletions listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,7 @@ func (l *FaultListener) ExitForStmt(c *parser.ForStmtContext) {

case *ast.IntegerLiteral:
rounds = x
block2 = &ast.BlockStatement{}

default:
panic(fmt.Sprintf("top of stack not a block statement or integer: line %d col %d type %T", c.GetStart().GetLine(), c.GetStart().GetColumn(), init))
Expand Down
10 changes: 10 additions & 0 deletions preprocess/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,13 @@ func (p *Processor) walk(n ast.Node) (ast.Node, error) {
node.Expression = pro.(ast.Expression)
return node, err
case *ast.ForStatement:
for i, v := range node.Inits.Statements {
pro, err = p.walk(v)
if err != nil {
return node, err
}
node.Inits.Statements[i] = pro.(ast.Statement)
}
for i, v := range node.Body.Statements {
pro, err = p.walk(v)
if err != nil {
Expand Down Expand Up @@ -488,6 +495,9 @@ func (p *Processor) walk(n ast.Node) (ast.Node, error) {

l, err := p.walk(node.Left)
if err != nil {
if node.Token.Type == "ASSIGN" {
return node, fmt.Errorf("illegal assignment %s", node.String())
}
return node, err
}

Expand Down
10 changes: 4 additions & 6 deletions preprocess/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ func TestRunInstances(t *testing.T) {
},
};
for 5 run {
f = new fl;
for 5 init{f = new fl;} run {
f.fizz;
}
`
Expand All @@ -252,7 +251,7 @@ func TestRunInstances(t *testing.T) {
t.Fatalf("flow f returns the wrong number of properties got=%d want=3", len(fl))
}

pc := process.Processed.Statements[3].(*ast.ForStatement).Body.Statements[1].(*ast.ParallelFunctions).Expressions[0].(*ast.ParameterCall)
pc := process.Processed.Statements[3].(*ast.ForStatement).Body.Statements[0].(*ast.ParallelFunctions).Expressions[0].(*ast.ParameterCall)
if pc.IdString() != "test1_f_fizz" {
t.Fatalf("flow not correctly named in runblock got=%s", pc.IdString())
}
Expand All @@ -274,7 +273,7 @@ func TestRunInstances(t *testing.T) {
t.Fatalf("flow property value not correctly named in runblock got=%s", buzz.Properties["foo"].Value.(ast.Nameable).IdString())
}

run := process.Processed.Statements[3].(*ast.ForStatement).Body.Statements[0].(*ast.ExpressionStatement).Expression.(*ast.StructInstance)
run := process.Processed.Statements[3].(*ast.ForStatement).Inits.Statements[0].(*ast.ExpressionStatement).Expression.(*ast.StructInstance)
for k, v := range run.Properties {
if k == "fizz" {
foo := v.Value.(*ast.FunctionLiteral).Body.Statements[0].(*ast.ExpressionStatement).Expression.(*ast.InfixExpression).Left.(*ast.ParameterCall).IdString()
Expand Down Expand Up @@ -382,8 +381,7 @@ func TestUnknowns(t *testing.T) {
},
};
for 5 run {
t = new test;
for 5 init{t = new test;} run {
t.bar;
};
`
Expand Down
28 changes: 19 additions & 9 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ var COMPARE = map[string]bool{
}

type Checker struct {
SpecStructs map[string]*preprocess.SpecRecord
Constants map[string]map[string]ast.Node
Instances map[string]*ast.StructInstance
inStock string
temps map[string]*ast.Type
Checked *ast.Spec
SpecStructs map[string]*preprocess.SpecRecord
Constants map[string]map[string]ast.Node
Instances map[string]*ast.StructInstance
inStock string
temps map[string]*ast.Type
Checked *ast.Spec
Preprocesser *preprocess.Processor
}

Expand Down Expand Up @@ -196,15 +196,25 @@ func (c *Checker) typecheck(n ast.Node) (ast.Node, error) {
return node, err

case *ast.ForStatement:
var st []ast.Statement
var st1 []ast.Statement
var st2 []ast.Statement
for _, v := range node.Inits.Statements {
tnode, err = c.typecheck(v)
if err != nil {
return node, err
}
st1 = append(st1, tnode.(ast.Statement))
}
node.Inits.Statements = st1

for _, v := range node.Body.Statements {
tnode, err = c.typecheck(v)
if err != nil {
return node, err
}
st = append(st, tnode.(ast.Statement))
st2 = append(st2, tnode.(ast.Statement))
}
node.Body.Statements = st
node.Body.Statements = st2
return node, err
case *ast.StartStatement:
return node, err
Expand Down
21 changes: 9 additions & 12 deletions types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,10 +910,9 @@ func TestSwapError(t *testing.T) {
},
};
for 1 run {
f2 = new f1;
f2.x = 2.3;
}
for 1 init{f2 = new f1;
f2.x = 2.3;
} run {}
`

_, err := prepTest(test, true)
Expand Down Expand Up @@ -942,10 +941,9 @@ func TestSwapError2(t *testing.T) {
},
};
for 1 run {
f2 = new f1;
f2.x = new s2;
}
for 1 init{f2 = new f1;
f2.x = new s2;
} run { }
`

_, err := prepTest(test, true)
Expand Down Expand Up @@ -974,10 +972,9 @@ func TestSwapError3(t *testing.T) {
},
};
for 1 run {
f2 = new f1;
for 1 init{f2 = new f1;
s = new s2;
f2.x = s;
f2.x = s;} run {
}
`

Expand All @@ -1002,4 +999,4 @@ func prepTest(test string, specType bool) (*Checker, error) {
ty := NewTypeChecker(pre)
_, err := ty.Check(pre.Processed)
return ty, err
}
}

0 comments on commit 4cf1223

Please sign in to comment.