Skip to content

Commit d0b79f5

Browse files
committed
add syntax checking
1 parent a7ea1c1 commit d0b79f5

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

gnovm/pkg/gnolang/gotypecheck.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gnolang
22

33
import (
44
"bytes"
5+
"errors"
56
"go/ast"
67
"go/format"
78
"go/parser"
@@ -141,7 +142,10 @@ func (g *gnoImporter) parseCheckMemPackage(mpkg *gnovm.MemPackage, fmt bool) (*t
141142
deleteOldIdents(delFunc, f)
142143
}
143144

144-
filterCrossing(f)
145+
if err := filterCrossing(f); err != nil {
146+
errs = multierr.Append(errs, err)
147+
continue
148+
}
145149

146150
// enforce formatting
147151
if fmt {
@@ -181,12 +185,16 @@ func deleteOldIdents(idents map[string]func(), f *ast.File) {
181185
}
182186
}
183187

184-
func filterCrossing(f *ast.File) {
188+
func filterCrossing(f *ast.File) (err error) {
185189
astutil.Apply(f, nil, func(c *astutil.Cursor) bool {
186190
switch n := c.Node().(type) {
187191
case *ast.ExprStmt:
188192
if ce, ok := n.X.(*ast.CallExpr); ok {
189193
if id, ok := ce.Fun.(*ast.Ident); ok && id.Name == "crossing" {
194+
// Validate syntax.
195+
if len(ce.Args) != 0 {
196+
err = errors.New("crossing called with non empty parameters")
197+
}
190198
// Delete statement 'crossing()'.
191199
c.Delete()
192200
}
@@ -195,12 +203,15 @@ func filterCrossing(f *ast.File) {
195203
if id, ok := n.Fun.(*ast.Ident); ok && id.Name == "cross" {
196204
// Replace expression 'cross(x)' by 'x'.
197205
var a ast.Node
198-
if len(n.Args) > 0 {
206+
if len(n.Args) == 1 {
199207
a = n.Args[0]
208+
} else {
209+
err = errors.New("cross called with invalid parameters")
200210
}
201211
c.Replace(a)
202212
}
203213
}
204214
return true
205215
})
216+
return err
206217
}

0 commit comments

Comments
 (0)