diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 26cd930d..5beb262c 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -21,6 +21,7 @@ type Kind uint32 const ( KInvalid = Kind(iota) + KArg KAssert KAssign KExpr @@ -47,6 +48,7 @@ func (k Kind) String() string { var kindStrings = [...]string{ KInvalid: "KInvalid", + KArg: "KArg", KAssert: "KAssert", KAssign: "KAssign", KExpr: "KExpr", @@ -93,6 +95,7 @@ func (n *Node) Kind() Kind { return n.kind } func (n *Node) TypeChecked() bool { return n.flags&FlagsTypeChecked != 0 } func (n *Node) SetTypeChecked() { n.flags |= FlagsTypeChecked } +func (n *Node) Arg() *Arg { return (*Arg)(n) } func (n *Node) Assert() *Assert { return (*Assert)(n) } func (n *Node) Assign() *Assign { return (*Assign)(n) } func (n *Node) Expr() *Expr { return (*Expr)(n) } @@ -159,11 +162,11 @@ const MaxExprDepth = 255 // Expr is an expression, such as "i", "+j" or "k + l[m(n, o)].p": // - FlagsSuspendible is "f(x)" vs "f?(x)" // - ID0: <0|operator|IDOpenParen|IDOpenBracket|IDColon|IDDot> -// - ID1: <0|identifier name|literal> +// - ID1: <0|ident|literal> // - LHS: // - MHS: // - RHS: -// - List0: function call arguments or associative op arguments +// - List0: function call arguments or associative op arguments // // A zero ID0 means an identifier or literal in ID1, like "foo" or "42". // @@ -212,20 +215,43 @@ func NewExpr(flags Flags, operator t.ID, nameLiteralSelector t.ID, lhs *Node, mh } } -// Assert is "assert RHS", "pre RHS" or "post RHS": +// Assert is "assert RHS via ID1(args)", "pre etc" or "post etc": // - ID0: +// - ID1: reason // - RHS: +// - List0: reason arguments type Assert Node func (n *Assert) Node() *Node { return (*Node)(n) } func (n *Assert) Keyword() t.ID { return n.id0 } +func (n *Assert) Reason() t.ID { return n.id1 } func (n *Assert) Condition() *Expr { return n.rhs.Expr() } +func (n *Assert) Args() []*Node { return n.list0 } -func NewAssert(keyword t.ID, condition *Expr) *Assert { +func NewAssert(keyword t.ID, condition *Expr, reason t.ID, args []*Node) *Assert { return &Assert{ - kind: KAssert, - id0: keyword, - rhs: condition.Node(), + kind: KAssert, + id0: keyword, + id1: reason, + rhs: condition.Node(), + list0: args, + } +} + +// Arg is "name:value". +// - ID1: name +// - RHS: value +type Arg Node + +func (n *Arg) Node() *Node { return (*Node)(n) } +func (n *Arg) Name() t.ID { return n.id1 } +func (n *Arg) Value() *Expr { return n.rhs.Expr() } + +func NewArg(name t.ID, value *Expr) *Arg { + return &Arg{ + kind: KArg, + id1: name, + rhs: value.Node(), } } diff --git a/lang/ast/string.go b/lang/ast/string.go index d52fbf51..a7621a73 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -41,7 +41,9 @@ func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool, depth uin if i != 0 { buf = append(buf, ", "...) } - buf = o.Expr().appendString(buf, m, false, depth) + buf = append(buf, m.ByID(o.Arg().Name())...) + buf = append(buf, ':') + buf = o.Arg().Value().appendString(buf, m, false, depth) } buf = append(buf, ')') diff --git a/lang/ast/string_test.go b/lang/ast/string_test.go index fa168836..cb0699d1 100644 --- a/lang/ast/string_test.go +++ b/lang/ast/string_test.go @@ -17,17 +17,17 @@ func TestString(t *testing.T) { "x", "f()", - "f(i)", - "f(i, j)", - "f(i, j) + 1", - "f(i, j)(k)", - "f(i, j)(k, l, m + 2) + 3", + "f(a:i)", + "f(a:i, b:j)", + "f(a:i, b:j) + 1", + "f(a:i, b:j)(c:k)", + "f(a:i, b:j)(c:k, d:l, e:m + 2) + 3", "x[i]", "x[i][j]", "x[i:j]", - "x[i](j)", - "x(i)[j]", + "x[i](arg:j)", + "x(arg:i)[j]", "x.y", "x.y.z.a", @@ -44,7 +44,7 @@ func TestString(t *testing.T) { "x * ((a / b) - (i / j))", "x + y + z", - "x + (i * j.k[l] * (-m << 4) * (n & o(p, q[:r.s + 5]))) + z", + "x + (i * j.k[l] * (-m << 4) * (n & o(o0:p, o1:q[:r.s + 5]))) + z", "x as bool", "x as u32", diff --git a/lang/check/type.go b/lang/check/type.go index 4677ed17..560524fb 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -136,8 +136,13 @@ func (c *checker) tcheckAssert(n *a.Assert) error { 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)) } + for _, a := range n.Args() { + if err := c.tcheckExpr(a.Arg().Value(), 0); err != nil { + return err + } + a.SetTypeChecked() + } // TODO: check that there are no side effects. - // TODO: check the actual assertion. return nil } diff --git a/lang/parse/parse.go b/lang/parse/parse.go index ed0318fb..0f342418 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -404,11 +404,25 @@ func (p *parser) parseAssertNode() (*a.Node, error) { switch x := p.peek1(); x.Key() { case t.KeyAssert, t.KeyPre, t.KeyPost: p.src = p.src[1:] - rhs, err := p.parseExpr() + condition, err := p.parseExpr() if err != nil { return nil, err } - return a.NewAssert(x, rhs).Node(), nil + reason, args := t.ID(0), []*a.Node(nil) + if p.peek1().Key() == t.KeyVia { + p.src = p.src[1:] + reason = p.peek1() + if !reason.IsStrLiteral() { + got := p.m.ByID(reason) + return nil, fmt.Errorf(`parse: expected string literal, got %q at %s:%d`, got, p.filename, p.line()) + } + p.src = p.src[1:] + args, err = p.parseList(t.KeyCloseParen, (*parser).parseArgNode) + if err != nil { + return nil, err + } + } + return a.NewAssert(x, condition, reason, args).Node(), nil } return nil, fmt.Errorf(`parse: expected "assert", "pre" or "post" at %s:%d`, p.filename, p.line()) } @@ -542,9 +556,21 @@ func (p *parser) parseIf() (*a.If, error) { return a.NewIf(condition, elseIf, bodyIfTrue, bodyIfFalse), nil } -func (p *parser) parseExprNode() (*a.Node, error) { - n, err := p.parseExpr() - return n.Node(), err +func (p *parser) parseArgNode() (*a.Node, error) { + name, err := p.parseIdent() + if err != nil { + return nil, err + } + if x := p.peek1().Key(); x != t.KeyColon { + got := p.m.ByKey(x) + return nil, fmt.Errorf(`parse: expected ":", got %q at %s:%d`, got, p.filename, p.line()) + } + p.src = p.src[1:] + value, err := p.parseExpr() + if err != nil { + return nil, err + } + return a.NewArg(name, value).Node(), nil } func (p *parser) parseExpr() (*a.Expr, error) { @@ -645,10 +671,7 @@ func (p *parser) parseOperand() (*a.Expr, error) { fallthrough case t.KeyOpenParen: - args, err := p.parseList(t.KeyCloseParen, func(p *parser) (*a.Node, error) { - n, err := p.parseExpr() - return n.Node(), err - }) + args, err := p.parseList(t.KeyCloseParen, (*parser).parseArgNode) if err != nil { return nil, err } diff --git a/lang/token/list.go b/lang/token/list.go index 39e013c7..b280c97d 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -194,6 +194,7 @@ const ( KeyIdeal = Key(IDIdeal >> KeyShift) KeyPre = Key(IDPre >> KeyShift) KeyPost = Key(IDPost >> KeyShift) + KeyVia = Key(IDVia >> KeyShift) KeyFalse = Key(IDFalse >> KeyShift) KeyTrue = Key(IDTrue >> KeyShift) @@ -317,6 +318,7 @@ const ( IDIdeal = ID(0x6C<