mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 16:52:18 +01:00
Add via keyword and named args.
This commit is contained in:
1 parent
9719fab535
commit
06eaadb1d6
7 files changed
+86
-27
No files matched your search
+33
-7
@@ -21,6 +21,7 @@ type Kind uint32
|
|||||||
const (
|
const (
|
||||||
KInvalid = Kind(iota)
|
KInvalid = Kind(iota)
|
||||||
|
|
||||||
|
KArg
|
||||||
KAssert
|
KAssert
|
||||||
KAssign
|
KAssign
|
||||||
KExpr
|
KExpr
|
||||||
@@ -47,6 +48,7 @@ func (k Kind) String() string {
|
|||||||
var kindStrings = [...]string{
|
var kindStrings = [...]string{
|
||||||
KInvalid: "KInvalid",
|
KInvalid: "KInvalid",
|
||||||
|
|
||||||
|
KArg: "KArg",
|
||||||
KAssert: "KAssert",
|
KAssert: "KAssert",
|
||||||
KAssign: "KAssign",
|
KAssign: "KAssign",
|
||||||
KExpr: "KExpr",
|
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) TypeChecked() bool { return n.flags&FlagsTypeChecked != 0 }
|
||||||
func (n *Node) SetTypeChecked() { n.flags |= FlagsTypeChecked }
|
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) Assert() *Assert { return (*Assert)(n) }
|
||||||
func (n *Node) Assign() *Assign { return (*Assign)(n) }
|
func (n *Node) Assign() *Assign { return (*Assign)(n) }
|
||||||
func (n *Node) Expr() *Expr { return (*Expr)(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":
|
// Expr is an expression, such as "i", "+j" or "k + l[m(n, o)].p":
|
||||||
// - FlagsSuspendible is "f(x)" vs "f?(x)"
|
// - FlagsSuspendible is "f(x)" vs "f?(x)"
|
||||||
// - ID0: <0|operator|IDOpenParen|IDOpenBracket|IDColon|IDDot>
|
// - ID0: <0|operator|IDOpenParen|IDOpenBracket|IDColon|IDDot>
|
||||||
// - ID1: <0|identifier name|literal>
|
// - ID1: <0|ident|literal>
|
||||||
// - LHS: <nil|Expr>
|
// - LHS: <nil|Expr>
|
||||||
// - MHS: <nil|Expr>
|
// - MHS: <nil|Expr>
|
||||||
// - RHS: <nil|Expr|TypeExpr>
|
// - RHS: <nil|Expr|TypeExpr>
|
||||||
// - List0: <Expr> function call arguments or associative op arguments
|
// - List0: <Arg|Expr> function call arguments or associative op arguments
|
||||||
//
|
//
|
||||||
// A zero ID0 means an identifier or literal in ID1, like "foo" or "42".
|
// 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: <IDAssert|IDPre|IDPost>
|
// - ID0: <IDAssert|IDPre|IDPost>
|
||||||
|
// - ID1: <string literal> reason
|
||||||
// - RHS: <Expr>
|
// - RHS: <Expr>
|
||||||
|
// - List0: <Arg> reason arguments
|
||||||
type Assert Node
|
type Assert Node
|
||||||
|
|
||||||
func (n *Assert) Node() *Node { return (*Node)(n) }
|
func (n *Assert) Node() *Node { return (*Node)(n) }
|
||||||
func (n *Assert) Keyword() t.ID { return n.id0 }
|
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) 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{
|
return &Assert{
|
||||||
kind: KAssert,
|
kind: KAssert,
|
||||||
id0: keyword,
|
id0: keyword,
|
||||||
rhs: condition.Node(),
|
id1: reason,
|
||||||
|
rhs: condition.Node(),
|
||||||
|
list0: args,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Arg is "name:value".
|
||||||
|
// - ID1: <ident> name
|
||||||
|
// - RHS: <Expr> 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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -41,7 +41,9 @@ func (n *Expr) appendString(buf []byte, m *t.IDMap, parenthesize bool, depth uin
|
|||||||
if i != 0 {
|
if i != 0 {
|
||||||
buf = append(buf, ", "...)
|
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, ')')
|
buf = append(buf, ')')
|
||||||
|
|
||||||
|
|||||||
@@ -17,17 +17,17 @@ func TestString(t *testing.T) {
|
|||||||
"x",
|
"x",
|
||||||
|
|
||||||
"f()",
|
"f()",
|
||||||
"f(i)",
|
"f(a:i)",
|
||||||
"f(i, j)",
|
"f(a:i, b:j)",
|
||||||
"f(i, j) + 1",
|
"f(a:i, b:j) + 1",
|
||||||
"f(i, j)(k)",
|
"f(a:i, b:j)(c:k)",
|
||||||
"f(i, j)(k, l, m + 2) + 3",
|
"f(a:i, b:j)(c:k, d:l, e:m + 2) + 3",
|
||||||
|
|
||||||
"x[i]",
|
"x[i]",
|
||||||
"x[i][j]",
|
"x[i][j]",
|
||||||
"x[i:j]",
|
"x[i:j]",
|
||||||
"x[i](j)",
|
"x[i](arg:j)",
|
||||||
"x(i)[j]",
|
"x(arg:i)[j]",
|
||||||
|
|
||||||
"x.y",
|
"x.y",
|
||||||
"x.y.z.a",
|
"x.y.z.a",
|
||||||
@@ -44,7 +44,7 @@ func TestString(t *testing.T) {
|
|||||||
"x * ((a / b) - (i / j))",
|
"x * ((a / b) - (i / j))",
|
||||||
|
|
||||||
"x + y + z",
|
"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 bool",
|
||||||
"x as u32",
|
"x as u32",
|
||||||
|
|||||||
+6
-1
@@ -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",
|
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))
|
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 that there are no side effects.
|
||||||
// TODO: check the actual assertion.
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+32
-9
@@ -404,11 +404,25 @@ func (p *parser) parseAssertNode() (*a.Node, error) {
|
|||||||
switch x := p.peek1(); x.Key() {
|
switch x := p.peek1(); x.Key() {
|
||||||
case t.KeyAssert, t.KeyPre, t.KeyPost:
|
case t.KeyAssert, t.KeyPre, t.KeyPost:
|
||||||
p.src = p.src[1:]
|
p.src = p.src[1:]
|
||||||
rhs, err := p.parseExpr()
|
condition, err := p.parseExpr()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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())
|
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
|
return a.NewIf(condition, elseIf, bodyIfTrue, bodyIfFalse), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *parser) parseExprNode() (*a.Node, error) {
|
func (p *parser) parseArgNode() (*a.Node, error) {
|
||||||
n, err := p.parseExpr()
|
name, err := p.parseIdent()
|
||||||
return n.Node(), err
|
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) {
|
func (p *parser) parseExpr() (*a.Expr, error) {
|
||||||
@@ -645,10 +671,7 @@ func (p *parser) parseOperand() (*a.Expr, error) {
|
|||||||
fallthrough
|
fallthrough
|
||||||
|
|
||||||
case t.KeyOpenParen:
|
case t.KeyOpenParen:
|
||||||
args, err := p.parseList(t.KeyCloseParen, func(p *parser) (*a.Node, error) {
|
args, err := p.parseList(t.KeyCloseParen, (*parser).parseArgNode)
|
||||||
n, err := p.parseExpr()
|
|
||||||
return n.Node(), err
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -194,6 +194,7 @@ const (
|
|||||||
KeyIdeal = Key(IDIdeal >> KeyShift)
|
KeyIdeal = Key(IDIdeal >> KeyShift)
|
||||||
KeyPre = Key(IDPre >> KeyShift)
|
KeyPre = Key(IDPre >> KeyShift)
|
||||||
KeyPost = Key(IDPost >> KeyShift)
|
KeyPost = Key(IDPost >> KeyShift)
|
||||||
|
KeyVia = Key(IDVia >> KeyShift)
|
||||||
|
|
||||||
KeyFalse = Key(IDFalse >> KeyShift)
|
KeyFalse = Key(IDFalse >> KeyShift)
|
||||||
KeyTrue = Key(IDTrue >> KeyShift)
|
KeyTrue = Key(IDTrue >> KeyShift)
|
||||||
@@ -317,6 +318,7 @@ const (
|
|||||||
IDIdeal = ID(0x6C<<KeyShift | FlagsOther)
|
IDIdeal = ID(0x6C<<KeyShift | FlagsOther)
|
||||||
IDPre = ID(0x6D<<KeyShift | FlagsOther)
|
IDPre = ID(0x6D<<KeyShift | FlagsOther)
|
||||||
IDPost = ID(0x6E<<KeyShift | FlagsOther)
|
IDPost = ID(0x6E<<KeyShift | FlagsOther)
|
||||||
|
IDVia = ID(0x6F<<KeyShift | FlagsOther)
|
||||||
|
|
||||||
IDFalse = ID(0x90<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
IDFalse = ID(0x90<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
||||||
IDTrue = ID(0x91<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
IDTrue = ID(0x91<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
||||||
@@ -446,6 +448,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
|
|||||||
KeyIdeal: {"ideal", IDIdeal},
|
KeyIdeal: {"ideal", IDIdeal},
|
||||||
KeyPre: {"pre", IDPre},
|
KeyPre: {"pre", IDPre},
|
||||||
KeyPost: {"post", IDPost},
|
KeyPost: {"post", IDPost},
|
||||||
|
KeyVia: {"via", IDVia},
|
||||||
|
|
||||||
KeyFalse: {"false", IDFalse},
|
KeyFalse: {"false", IDFalse},
|
||||||
KeyTrue: {"true", IDTrue},
|
KeyTrue: {"true", IDTrue},
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)() {
|
|||||||
// TODO: implement.
|
// TODO: implement.
|
||||||
|
|
||||||
while n_bits < width {
|
while n_bits < width {
|
||||||
assert n_bits < 12
|
assert n_bits < 12 via "a < b: a < c <= b"(c:width)
|
||||||
// TODO.
|
// TODO.
|
||||||
n_bits += 8
|
n_bits += 8
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user