Skip to content

Commit 3b5ac5b

Browse files
mvertesjaekwon
authored andcommitted
add syntax checking
1 parent fdcc3f9 commit 3b5ac5b

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 {
@@ -183,12 +187,16 @@ func deleteOldIdents(idents map[string]func(), f *ast.File) {
183187
}
184188
}
185189

186-
func filterCrossing(f *ast.File) {
190+
func filterCrossing(f *ast.File) (err error) {
187191
astutil.Apply(f, nil, func(c *astutil.Cursor) bool {
188192
switch n := c.Node().(type) {
189193
case *ast.ExprStmt:
190194
if ce, ok := n.X.(*ast.CallExpr); ok {
191195
if id, ok := ce.Fun.(*ast.Ident); ok && id.Name == "crossing" {
196+
// Validate syntax.
197+
if len(ce.Args) != 0 {
198+
err = errors.New("crossing called with non empty parameters")
199+
}
192200
// Delete statement 'crossing()'.
193201
c.Delete()
194202
}
@@ -197,12 +205,15 @@ func filterCrossing(f *ast.File) {
197205
if id, ok := n.Fun.(*ast.Ident); ok && id.Name == "cross" {
198206
// Replace expression 'cross(x)' by 'x'.
199207
var a ast.Node
200-
if len(n.Args) > 0 {
208+
if len(n.Args) == 1 {
201209
a = n.Args[0]
210+
} else {
211+
err = errors.New("cross called with invalid parameters")
202212
}
203213
c.Replace(a)
204214
}
205215
}
206216
return true
207217
})
218+
return err
208219
}

0 commit comments

Comments
 (0)