diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 3c731473..a1759627 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -209,16 +209,19 @@ func NewExpr(flags Flags, operator t.ID, nameLiteralSelector t.ID, lhs *Node, mh } } -// Assert is "assert RHS": +// Assert is "assert RHS", "pre RHS" or "post RHS": +// - ID0: // - RHS: type Assert Node func (n *Assert) Node() *Node { return (*Node)(n) } +func (n *Assert) Keyword() t.ID { return n.id0 } func (n *Assert) Condition() *Expr { return n.rhs.Expr() } -func NewAssert(condition *Expr) *Assert { +func NewAssert(keyword t.ID, condition *Expr) *Assert { return &Assert{ kind: KAssert, + id0: keyword, rhs: condition.Node(), } } @@ -282,17 +285,20 @@ func NewField(name t.ID, xType *TypeExpr) *Field { // While is "while { List1 }" or "while LHS { List1 }": // - LHS: +// - List0: asserts // - List1: <*> loop body type While Node func (n *While) Node() *Node { return (*Node)(n) } func (n *While) Condition() *Expr { return n.lhs.Expr() } +func (n *While) Asserts() []*Node { return n.list0 } func (n *While) Body() []*Node { return n.list1 } -func NewWhile(condition *Expr, body []*Node) *While { +func NewWhile(condition *Expr, asserts []*Node, body []*Node) *While { return &While{ kind: KWhile, lhs: condition.Node(), + list0: asserts, list1: body, } } @@ -403,6 +409,7 @@ func NewTypeExpr(pkgOrDec t.ID, name t.ID, arrayLengthInclMin *Expr, exclMax *Ex // - ID1: name // - LHS: in-parameters // - RHS: out-parameters +// - List0: asserts // - List1: <*> function body type Func Node @@ -415,9 +422,10 @@ func (n *Func) Receiver() t.ID { return n.id0 } func (n *Func) Name() t.ID { return n.id1 } func (n *Func) In() *Struct { return n.lhs.Struct() } func (n *Func) Out() *Struct { return n.rhs.Struct() } +func (n *Func) Asserts() []*Node { return n.list0 } func (n *Func) Body() []*Node { return n.list1 } -func NewFunc(flags Flags, filename string, line uint32, receiver t.ID, name t.ID, in *Struct, out *Struct, body []*Node) *Func { +func NewFunc(flags Flags, filename string, line uint32, receiver t.ID, name t.ID, in *Struct, out *Struct, asserts []*Node, body []*Node) *Func { return &Func{ kind: KFunc, flags: flags, @@ -427,6 +435,7 @@ func NewFunc(flags Flags, filename string, line uint32, receiver t.ID, name t.ID id1: name, lhs: in.Node(), rhs: out.Node(), + list0: asserts, list1: body, } } diff --git a/lang/check/check.go b/lang/check/check.go index 28d08bd8..85131bb0 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -79,6 +79,7 @@ var phases = [...]struct { {a.KUse, (*Checker).checkUse}, {a.KStruct, (*Checker).checkStruct}, {a.KFunc, (*Checker).checkFuncSignature}, + {a.KFunc, (*Checker).checkFuncContract}, {a.KFunc, (*Checker).checkFuncBody}, } @@ -168,6 +169,23 @@ func (c *Checker) checkFuncSignature(n *a.Node) error { return nil } +func (c *Checker) checkFuncContract(n *a.Node) error { + as := n.Func().Asserts() + if len(as) == 0 { + return nil + } + tc := &typeChecker{ + c: c, + idMap: c.idMap, + } + for _, a := range as { + if err := tc.checkAssert(a.Assert()); err != nil { + return err + } + } + return nil +} + func (c *Checker) checkFuncBody(n *a.Node) error { f := n.Func() tc := &typeChecker{ diff --git a/lang/check/check_test.go b/lang/check/check_test.go index 0b054181..0527edce 100644 --- a/lang/check/check_test.go +++ b/lang/check/check_test.go @@ -68,7 +68,9 @@ func TestCheck(t *testing.T) { var p i32 var q i32[0:8] - while p == q { + while p == q, + post p != q, + { } } `) + "\n" diff --git a/lang/check/type.go b/lang/check/type.go index f7279b02..acf50077 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -53,17 +53,7 @@ func (c *typeChecker) checkStatement(n *a.Node) error { switch n.Kind() { case a.KAssert: - o := n.Assert() - cond := o.Condition() - if err := c.checkExpr(cond); err != nil { - return err - } - if !cond.MType().Eq(TypeExprBoolean) { - return fmt.Errorf("check: assert condition %q, of type %q, does not have a boolean type", - cond.String(c.idMap), cond.MType().String(c.idMap)) - } - // TODO: check the actual assertion. - return nil + return c.checkAssert(n.Assert()) case a.KAssign: return c.checkAssign(n.Assign()) @@ -89,6 +79,11 @@ func (c *typeChecker) checkStatement(n *a.Node) error { cond.String(c.idMap), cond.MType().String(c.idMap)) } } + for _, m := range o.Asserts() { + if err := c.checkAssert(m.Assert()); err != nil { + return err + } + } for _, m := range o.Body() { if err := c.checkStatement(m); err != nil { return err @@ -142,6 +137,19 @@ func (c *typeChecker) checkStatement(n *a.Node) error { return fmt.Errorf("check: unrecognized ast.Kind (%d) for checkStatement", n.Kind()) } +func (c *typeChecker) checkAssert(n *a.Assert) error { + cond := n.Condition() + if err := c.checkExpr(cond); err != nil { + return err + } + if !cond.MType().Eq(TypeExprBoolean) { + return fmt.Errorf("check: assert condition %q, of type %q, does not have a boolean type", + cond.String(c.idMap), cond.MType().String(c.idMap)) + } + // TODO: check the actual assertion. + return nil +} + func (c *typeChecker) checkAssign(n *a.Assign) error { lhs := n.LHS() rhs := n.RHS() diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 556a013e..d03ec068 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -91,14 +91,22 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { flags |= a.FlagsSuspendible p.src = p.src[1:] } - inFields, err := p.parseList((*parser).parseFieldNode) + inFields, err := p.parseList(t.IDCloseParen, (*parser).parseFieldNode) if err != nil { return nil, err } - outFields, err := p.parseList((*parser).parseFieldNode) + outFields, err := p.parseList(t.IDCloseParen, (*parser).parseFieldNode) if err != nil { return nil, err } + asserts := []*a.Node(nil) + if p.peekID() == t.IDComma { + p.src = p.src[1:] + asserts, err = p.parseList(t.IDOpenCurly, (*parser).parseAssertNode) + if err != nil { + return nil, err + } + } body, err := p.parseBlock() if err != nil { return nil, err @@ -110,7 +118,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { p.src = p.src[1:] in := a.NewStruct(0, p.filename, line, t.IDIn, inFields) out := a.NewStruct(0, p.filename, line, t.IDOut, outFields) - return a.NewFunc(flags, p.filename, line, id0, id1, in, out, body).Node(), nil + return a.NewFunc(flags, p.filename, line, id0, id1, in, out, asserts, body).Node(), nil case t.IDStruct: flags := a.Flags(0) @@ -127,7 +135,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { flags |= a.FlagsSuspendible p.src = p.src[1:] } - fields, err := p.parseList((*parser).parseFieldNode) + fields, err := p.parseList(t.IDCloseParen, (*parser).parseFieldNode) if err != nil { return nil, err } @@ -189,17 +197,21 @@ func (p *parser) parseIdent() (t.ID, error) { return x.ID, nil } -func (p *parser) parseList(parseElem func(*parser) (*a.Node, error)) ([]*a.Node, error) { - if x := p.peekID(); x != t.IDOpenParen { - got := p.m.ByID(x) - return nil, fmt.Errorf("parse: expected \"(\", got %q at %s:%d", got, p.filename, p.line()) +func (p *parser) parseList(stop t.ID, parseElem func(*parser) (*a.Node, error)) ([]*a.Node, error) { + if stop == t.IDCloseParen { + if x := p.peekID(); x != t.IDOpenParen { + return nil, fmt.Errorf(`parse: expected "(", got %q at %s:%d`, + p.m.ByID(x), p.filename, p.line()) + } + p.src = p.src[1:] } - p.src = p.src[1:] ret := []*a.Node(nil) for len(p.src) > 0 { - if p.src[0].ID == t.IDCloseParen { - p.src = p.src[1:] + if p.src[0].ID == stop { + if stop == t.IDCloseParen { + p.src = p.src[1:] + } return ret, nil } @@ -210,17 +222,19 @@ func (p *parser) parseList(parseElem func(*parser) (*a.Node, error)) ([]*a.Node, ret = append(ret, elem) switch x := p.peekID(); x { - case t.IDCloseParen: - p.src = p.src[1:] + case stop: + if stop == t.IDCloseParen { + p.src = p.src[1:] + } return ret, nil case t.IDComma: p.src = p.src[1:] default: - got := p.m.ByID(x) - return nil, fmt.Errorf("parse: expected \")\", got %q at %s:%d", got, p.filename, p.line()) + return nil, fmt.Errorf("parse: expected %q, got %q at %s:%d", + p.m.ByID(stop), p.m.ByID(x), p.filename, p.line()) } } - return nil, fmt.Errorf("parse: expected \")\" at %s:%d", p.filename, p.line()) + return nil, fmt.Errorf("parse: expected %q at %s:%d", p.m.ByID(stop), p.filename, p.line()) } func (p *parser) parseFieldNode() (*a.Node, error) { @@ -359,6 +373,19 @@ func (p *parser) parseBlock() ([]*a.Node, error) { return nil, fmt.Errorf("parse: expected \"}\" at %s:%d", p.filename, p.line()) } +func (p *parser) parseAssertNode() (*a.Node, error) { + switch x := p.peekID(); x { + case t.IDAssert, t.IDPre, t.IDPost: + p.src = p.src[1:] + rhs, err := p.parseExpr() + if err != nil { + return nil, err + } + return a.NewAssert(x, rhs).Node(), nil + } + return nil, fmt.Errorf(`parse: expected "assert", "pre" or "post" at %s:%d`, p.filename, p.line()) +} + func (p *parser) parseStatement() (*a.Node, error) { line := uint32(0) if len(p.src) > 0 { @@ -373,13 +400,8 @@ func (p *parser) parseStatement() (*a.Node, error) { func (p *parser) parseStatement1() (*a.Node, error) { switch x := p.peekID(); x { - case t.IDAssert: - p.src = p.src[1:] - rhs, err := p.parseExpr() - if err != nil { - return nil, err - } - return a.NewAssert(rhs).Node(), nil + case t.IDAssert, t.IDPre, t.IDPost: + return p.parseAssertNode() case t.IDBreak: p.src = p.src[1:] @@ -391,18 +413,31 @@ func (p *parser) parseStatement1() (*a.Node, error) { case t.IDWhile: p.src = p.src[1:] - condition, err := (*a.Expr)(nil), error(nil) - if p.peekID() != t.IDOpenCurly { + condition, asserts, err := (*a.Expr)(nil), []*a.Node(nil), error(nil) + switch p.peekID() { + case t.IDOpenCurly: + // No-op. + default: condition, err = p.parseExpr() if err != nil { return nil, err } + if p.peekID() != t.IDComma { + break + } + fallthrough + case t.IDComma: + p.src = p.src[1:] + asserts, err = p.parseList(t.IDOpenCurly, (*parser).parseAssertNode) + if err != nil { + return nil, err + } } body, err := p.parseBlock() if err != nil { return nil, err } - return a.NewWhile(condition, body).Node(), nil + return a.NewWhile(condition, asserts, body).Node(), nil case t.IDIf: o, err := p.parseIf() @@ -592,7 +627,7 @@ func (p *parser) parseOperand() (*a.Expr, error) { fallthrough case t.IDOpenParen: - args, err := p.parseList(func(p *parser) (*a.Node, error) { + args, err := p.parseList(t.IDCloseParen, func(p *parser) (*a.Node, error) { n, err := p.parseExpr() return n.Node(), err }) diff --git a/lang/render/render.go b/lang/render/render.go index 5877590c..8d93a120 100644 --- a/lang/render/render.go +++ b/lang/render/render.go @@ -88,7 +88,7 @@ func Render(w io.Writer, m *token.IDMap, src []token.Token, comments []string) ( indentAdjustment := 0 if lineTokens[0].ID.IsClose() { indentAdjustment-- - } else if hanging { + } else if hanging && lineTokens[0].ID != token.IDOpenCurly { indentAdjustment++ } buf = appendTabs(buf, indent+indentAdjustment) diff --git a/lang/token/list.go b/lang/token/list.go index e82f8a74..342b6300 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -192,6 +192,8 @@ const ( IDUse = ID(0x6A<> KeyShift: {"use", IDUse}, IDVar >> KeyShift: {"var", IDVar}, IDIdeal >> KeyShift: {"ideal", IDIdeal}, + IDPre >> KeyShift: {"pre", IDPre}, + IDPost >> KeyShift: {"post", IDPost}, IDFalse >> KeyShift: {"false", IDFalse}, IDTrue >> KeyShift: {"true", IDTrue},