Commit 2a7d848e authored by Hugo Moreau's avatar Hugo Moreau
Browse files

add pass triggering errors for use of non-integer variable

parent ae943e13
......@@ -65,6 +65,7 @@ func compileTo(path string, src []byte, file *os.File) (string, error) {
// Apply all transformations.
meta := transform.NewMeta(fset, info)
res := transform.ApplyAll(f, meta, []transform.Transform{
&transform.TypeChecker{},
&transform.NormalizeDeclarations{},
&transform.FunctionDefs{},
&transform.LocalVariableAssignments{},
......
package main
import "fmt"
func main() {
n := 1.0
fmt.Println(n)
}
package transform
import (
"fmt"
"go/ast"
"go/token"
"os"
)
/*
The TypeChecker check if there is other types, variable must be declared
as integer.
*/
type TypeChecker struct {
}
// Pre traversal check if there is use of variable other than integers.
func (t *TypeChecker) Pre(meta *Meta, v *Visitor) bool {
c := v.Cursor()
switch node := c.Node().(type) {
case *ast.AssignStmt:
for i := 0; i < len(node.Rhs); i++ {
switch rhn := node.Rhs[i].(type) {
case *ast.BasicLit:
if rhn.Kind != token.INT {
fmt.Println("Values must be integer.")
fmt.Println("Identified type :", rhn.Kind)
os.Exit(1)
}
}
}
}
return true
}
func (TypeChecker) Post(meta *Meta, v *Visitor) bool {
return true
}
package transform
import (
"fmt"
"go/ast"
"go/token"
"os"
)
/*
......@@ -47,15 +45,6 @@ func (t *LocalVariableAssignments) Pre(meta *Meta, v *Visitor) bool {
newLHS = append(newLHS, lhn)
}
}
for i := 0; i < len(node.Rhs); i++ {
switch rhn := node.Rhs[i].(type) {
case *ast.BasicLit:
if rhn.Kind != token.INT {
fmt.Println("Non-integer values are not supported")
os.Exit(1)
}
}
}
c.Replace(&ast.AssignStmt{
Lhs: newLHS,
// Replace declarations by assignments
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment