From b78807d60a3bb2d09ede957f5ac5d1c10c6b5273 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 16 Jul 2017 12:02:12 +1000 Subject: [PATCH] Add the "const" keyword. --- cmd/puffs-gen-c/main.go | 62 ++++++++++++++++++++++++++ doc/puffs-the-language.md | 4 +- gen/c/std/flate.c | 8 ++++ gen/c/std/gif.c | 4 ++ gen/h/std/flate.h | 2 + gen/h/std/gif.h | 2 + lang/ast/ast.go | 36 ++++++++++++++- lang/ast/string.go | 13 ++++++ lang/check/check.go | 85 ++++++++++++++++++++++++++++++++++++ lang/check/type.go | 10 +++++ lang/parse/parse.go | 56 ++++++++++++++++++++++++ lang/token/list.go | 7 +++ std/flate/codec_consts.puffs | 7 +++ 13 files changed, 292 insertions(+), 4 deletions(-) create mode 100644 std/flate/codec_consts.puffs diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 9556bd0a..54bb8570 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -245,6 +245,11 @@ func (g *gen) genHeader() error { g.printf("bool puffs_%s_status_is_error(puffs_%s_status s);\n\n", g.pkgName, g.pkgName) g.printf("const char* puffs_%s_status_string(puffs_%s_status s);\n\n", g.pkgName, g.pkgName) + g.writes("// ---------------- Public Consts\n\n") + if err := g.forEachConst(pubOnly, (*gen).writeConst); err != nil { + return err + } + g.writes("// ---------------- Structs\n\n") for _, n := range g.structList { if err := g.writeStruct(n); err != nil { @@ -312,6 +317,11 @@ func (g *gen) genImpl() error { g.printf("return i < n ? a[i] : \"%s: unknown status\";\n", g.pkgName) g.writes("}\n\n") + g.writes("// ---------------- Private Consts\n\n") + if err := g.forEachConst(priOnly, (*gen).writeConst); err != nil { + return err + } + g.writes("// ---------------- Private Constructor and Destructor Prototypes\n\n") for _, n := range g.structList { if !n.Public() { @@ -353,6 +363,22 @@ func (g *gen) genImpl() error { return nil } +func (g *gen) forEachConst(v visibility, f func(*gen, *a.Const) error) error { + for _, file := range g.files { + for _, tld := range file.TopLevelDecls() { + if tld.Kind() != a.KConst || + (v == pubOnly && tld.Raw().Flags()&a.FlagsPublic == 0) || + (v == priOnly && tld.Raw().Flags()&a.FlagsPublic != 0) { + continue + } + if err := f(g, tld.Const()); err != nil { + return err + } + } + } + return nil +} + func (g *gen) forEachFunc(v visibility, f func(*gen, *a.Func) error) error { for _, file := range g.files { for _, tld := range file.TopLevelDecls() { @@ -446,6 +472,42 @@ func (g *gen) gatherStatuses(n *a.Status) error { return nil } +func (g *gen) writeConst(n *a.Const) error { + if !n.Public() { + g.writes("static ") + } + g.writes("const ") + prefix := fmt.Sprintf("puffs_%s_", g.pkgName) + if err := g.writeCTypeName(n.XType(), prefix, n.Name().String(g.tm)); err != nil { + return err + } + g.writes(" = ") + if err := g.writeConstList(n.Value()); err != nil { + return err + } + g.writes(";\n\n") + return nil +} + +func (g *gen) writeConstList(n *a.Expr) error { + switch n.ID0().Key() { + case 0: + g.writes(n.ConstValue().String()) + case t.KeyDollar: + g.writeb('{') + for _, o := range n.Args() { + if err := g.writeConstList(o.Expr()); err != nil { + return err + } + g.writeb(',') + } + g.writeb('}') + default: + return fmt.Errorf("invalid const value %q", n.String(g.tm)) + } + return nil +} + func (g *gen) writeStruct(n *a.Struct) error { // For API/ABI compatibility, the very first field in the struct's // private_impl must be the status code. This lets the constructor callee diff --git a/doc/puffs-the-language.md b/doc/puffs-the-language.md index 553a3095..083e155e 100644 --- a/doc/puffs-the-language.md +++ b/doc/puffs-the-language.md @@ -12,9 +12,9 @@ be enclosed by curly `{}`s. There is no 'dangling else' ambiguity. ## Keywords -Puffs is a simple language, with only 23 keywords. 6 of these introduce -top-level concepts: +The Puffs language has 24 keywords. 7 of those introduce top-level concepts: +- `const` - `error` - `func` - `packageid` diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 337b1194..31d11bbd 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -115,6 +115,8 @@ bool puffs_flate_status_is_error(puffs_flate_status s); const char* puffs_flate_status_string(puffs_flate_status s); +// ---------------- Public Consts + // ---------------- Structs typedef struct { @@ -269,6 +271,12 @@ const char* puffs_flate_status_string(puffs_flate_status s) { return i < n ? a[i] : "flate: unknown status"; } +// ---------------- Private Consts + +static const uint8_t puffs_flate_code_order[19] = { + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15, +}; + // ---------------- Private Constructor and Destructor Prototypes // ---------------- Private Function Prototypes diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index a2bdb0c9..079fed97 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -113,6 +113,8 @@ bool puffs_gif_status_is_error(puffs_gif_status s); const char* puffs_gif_status_string(puffs_gif_status s); +// ---------------- Public Consts + // ---------------- Structs typedef struct { @@ -323,6 +325,8 @@ const char* puffs_gif_status_string(puffs_gif_status s) { return i < n ? a[i] : "gif: unknown status"; } +// ---------------- Private Consts + // ---------------- Private Constructor and Destructor Prototypes // ---------------- Private Function Prototypes diff --git a/gen/h/std/flate.h b/gen/h/std/flate.h index 27b74e34..183fc780 100644 --- a/gen/h/std/flate.h +++ b/gen/h/std/flate.h @@ -115,6 +115,8 @@ bool puffs_flate_status_is_error(puffs_flate_status s); const char* puffs_flate_status_string(puffs_flate_status s); +// ---------------- Public Consts + // ---------------- Structs typedef struct { diff --git a/gen/h/std/gif.h b/gen/h/std/gif.h index c979fec5..2f69ae30 100644 --- a/gen/h/std/gif.h +++ b/gen/h/std/gif.h @@ -113,6 +113,8 @@ bool puffs_gif_status_is_error(puffs_gif_status s); const char* puffs_gif_status_string(puffs_gif_status s); +// ---------------- Public Consts + // ---------------- Structs typedef struct { diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 9003fe4d..eae5a2c9 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -24,6 +24,7 @@ const ( KArg KAssert KAssign + KConst KExpr KField KFile @@ -53,6 +54,7 @@ var kindStrings = [...]string{ KArg: "KArg", KAssert: "KAssert", KAssign: "KAssign", + KConst: "KConst", KExpr: "KExpr", KField: "KField", KFile: "KFile", @@ -109,6 +111,7 @@ 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) Const() *Const { return (*Const)(n) } func (n *Node) Expr() *Expr { return (*Expr)(n) } func (n *Node) Field() *Field { return (*Field)(n) } func (n *Node) File() *File { return (*File)(n) } @@ -182,7 +185,7 @@ const MaxExprDepth = 255 // - LHS: // - MHS: // - RHS: -// - List0: function call arguments or associative op arguments +// - List0: function call args, assoc. op args or list members. // // A zero ID0 means an identifier or literal in ID1, like "foo" or "42". // @@ -203,6 +206,8 @@ const MaxExprDepth = 255 // // For selectors, like "LHS.ID1", ID0 is IDDot. // +// For lists, like "$(0, 1, 2)", ID0 is IDDollar. +// // For limits, like "limit (LHS) RHS", ID0 is IDLimit. type Expr Node @@ -463,7 +468,7 @@ const MaxTypeExprDepth = 63 // refined as "foo[LHS..MHS]". LHS and MHS are Expr's, possibly nil. For // example, the LHS for "u32[..4095]" is nil. // -// TODO: function / method types, struct types. +// TODO: function / method types, struct types, list types. type TypeExpr Node func (n *TypeExpr) Node() *Node { return (*Node)(n) } @@ -601,6 +606,33 @@ func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, message } } +// Const is "const ID1 LHS = RHS": +// - FlagsPublic is "pub" vs "pri" +// - ID1: name +// - LHS: +// - RHS: +type Const Node + +func (n *Const) Node() *Node { return (*Node)(n) } +func (n *Const) Public() bool { return n.flags&FlagsPublic != 0 } +func (n *Const) Filename() string { return n.filename } +func (n *Const) Line() uint32 { return n.line } +func (n *Const) Name() t.ID { return n.id1 } +func (n *Const) XType() *TypeExpr { return n.lhs.TypeExpr() } +func (n *Const) Value() *Expr { return n.rhs.Expr() } + +func NewConst(flags Flags, filename string, line uint32, name t.ID, xType *TypeExpr, value *Expr) *Const { + return &Const{ + kind: KConst, + flags: flags, + filename: filename, + line: line, + id1: name, + lhs: xType.Node(), + rhs: value.Node(), + } +} + // Struct is "struct ID1(List0)": // - FlagsSuspendible is "ID1" vs "ID1?" // - FlagsPublic is "pub" vs "pri" diff --git a/lang/ast/string.go b/lang/ast/string.go index 1f91b20d..d2fd9594 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -65,6 +65,19 @@ func (n *Expr) appendString(buf []byte, tm *t.Map, parenthesize bool, depth uint buf = n.lhs.Expr().appendString(buf, tm, true, depth) buf = append(buf, '.') buf = append(buf, tm.ByID(n.id1)...) + + case t.KeyDollar: + buf = append(buf, "$("...) + for i, o := range n.list0 { + if i != 0 { + buf = append(buf, ", "...) + } + buf = o.Expr().appendString(buf, tm, false, depth) + } + buf = append(buf, ')') + + case t.KeyLimit: + // TODO. } case t.FlagsUnaryOp: diff --git a/lang/check/check.go b/lang/check/check.go index ff3df163..d2135e5a 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -6,6 +6,7 @@ package check import ( "errors" "fmt" + "math/big" "github.com/google/puffs/lang/base38" @@ -55,6 +56,7 @@ func trimQuotes(s string) string { var ( typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil) typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil) + typeExprList = a.NewTypeExpr(0, t.IDDollar, nil, nil, nil) typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil) // TODO: delete this. @@ -65,6 +67,11 @@ var ( // TypeMap maps from variable names (as token IDs) to types. type TypeMap map[t.ID]*a.TypeExpr +type Const struct { + ID t.ID // ID of the const name. + Const *a.Const +} + type Func struct { QID t.QID // Qualified ID of the func name. Func *a.Func @@ -108,6 +115,7 @@ func Check(tm *t.Map, files ...*a.File) (*Checker, error) { tm: tm, reasonMap: rMap, packageID: base38.Max + 1, + consts: map[t.ID]Const{}, funcs: map[t.QID]Func{}, statuses: map[t.ID]Status{}, structs: map[t.ID]Struct{}, @@ -142,6 +150,7 @@ var phases = [...]struct { {a.KPackageID, (*Checker).checkPackageID}, {a.KUse, (*Checker).checkUse}, {a.KStatus, (*Checker).checkStatus}, + {a.KConst, (*Checker).checkConst}, {a.KStruct, (*Checker).checkStructDecl}, {a.KInvalid, (*Checker).checkStructCycles}, {a.KStruct, (*Checker).checkStructFields}, @@ -149,6 +158,7 @@ var phases = [...]struct { {a.KFunc, (*Checker).checkFuncContract}, {a.KFunc, (*Checker).checkFuncBody}, {a.KStruct, (*Checker).checkFieldMethodCollisions}, + // TODO: check consts, funcs, structs and uses for name collisions. } type Checker struct { @@ -158,6 +168,7 @@ type Checker struct { packageID uint32 otherPackageID *a.PackageID + consts map[t.ID]Const funcs map[t.QID]Func statuses map[t.ID]Status structs map[t.ID]Struct @@ -166,6 +177,7 @@ type Checker struct { } func (c *Checker) PackageID() uint32 { return c.packageID } +func (c *Checker) Consts() map[t.ID]Const { return c.consts } func (c *Checker) Funcs() map[t.QID]Func { return c.funcs } func (c *Checker) Statuses() map[t.ID]Status { return c.statuses } func (c *Checker) Structs() map[t.ID]Struct { return c.structs } @@ -228,6 +240,79 @@ func (c *Checker) checkStatus(node *a.Node) error { return nil } +func (c *Checker) checkConst(node *a.Node) error { + n := node.Const() + id := n.Name() + if other, ok := c.consts[id]; ok { + return &Error{ + Err: fmt.Errorf("check: duplicate const %q", id.String(c.tm)), + Filename: n.Filename(), + Line: n.Line(), + OtherFilename: other.Const.Filename(), + OtherLine: other.Const.Line(), + } + } + c.consts[id] = Const{ + ID: id, + Const: n, + } + + q := &checker{ + c: c, + tm: c.tm, + } + if err := q.tcheckTypeExpr(n.XType(), 0); err != nil { + return fmt.Errorf("%v in const %q", err, id.String(c.tm)) + } + if err := q.tcheckExpr(n.Value(), 0); err != nil { + return fmt.Errorf("%v in const %q", err, id.String(c.tm)) + } + + nLists := 0 + typ := n.XType() + for typ.Decorator().Key() == t.KeyOpenBracket { + if nLists == a.MaxTypeExprDepth { + return fmt.Errorf("check: type expression recursion depth too large") + } + nLists++ + typ = typ.Inner() + } + if typ.Decorator() != 0 { + return fmt.Errorf("check: invalid const type %q for %q", n.XType().String(c.tm), id.String(c.tm)) + } + nMin, nMax, err := q.bcheckTypeExpr(typ) + if err != nil { + return err + } + if nMin == nil || nMax == nil { + return fmt.Errorf("check: invalid const type %q for %q", n.XType().String(c.tm), id.String(c.tm)) + } + if err := c.checkConstElement(n.Value(), nMin, nMax, nLists); err != nil { + return fmt.Errorf("check: %v for %q", err, id.String(c.tm)) + } + n.Node().SetTypeChecked() + return nil +} + +func (c *Checker) checkConstElement(n *a.Expr, nMin *big.Int, nMax *big.Int, nLists int) error { + if nLists > 0 { + nLists-- + if n.ID0().Key() != t.KeyDollar { + return fmt.Errorf("invalid const value %q", n.String(c.tm)) + } + for _, o := range n.Args() { + if err := c.checkConstElement(o.Expr(), nMin, nMax, nLists); err != nil { + return err + } + } + return nil + } + if cv := n.ConstValue(); cv == nil || cv.Cmp(nMin) < 0 || cv.Cmp(nMax) > 0 { + return fmt.Errorf("invalid const value %q not within [%v..%v]", n.String(c.tm), nMin, nMax) + } + return nil +} + func (c *Checker) checkStructDecl(node *a.Node) error { n := node.Struct() id := n.Name() diff --git a/lang/check/type.go b/lang/check/type.go index dcdd5ac2..5f4c7864 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -438,6 +438,16 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { case t.KeyDot: return q.tcheckDot(n, depth) + case t.KeyDollar: + for _, o := range n.Args() { + o := o.Expr() + if err := q.tcheckExpr(o, depth); err != nil { + return err + } + } + n.SetMType(typeExprList) + return nil + case t.KeyLimit: lhs := n.LHS().Expr() if err := q.tcheckExpr(lhs, depth); err != nil { diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 17a672e7..351d8235 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -108,6 +108,41 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { case t.KeyPri: p.src = p.src[1:] switch p.peek1().Key() { + case t.KeyConst: + p.src = p.src[1:] + id, err := p.parseIdent() + if err != nil { + return nil, err + } + typ, err := p.parseTypeExpr() + if err != nil { + return nil, err + } + if p.peek1().Key() != t.KeyEq { + return nil, fmt.Errorf(`parse: const %q has no value at %s:%d`, + p.tm.ByID(id), p.filename, p.line()) + } + p.src = p.src[1:] + value := (*a.Expr)(nil) + // TODO: parse lists of lists. + if p.peek1().Key() == t.KeyDollar { + value, err = p.parseDollarExpr() + if err != nil { + return nil, err + } + } else { + value, err = p.parseExpr() + if err != nil { + return nil, err + } + } + if x := p.peek1().Key(); x != t.KeySemicolon { + got := p.tm.ByKey(x) + return nil, fmt.Errorf(`parse: expected (implicit) ";", got %q at %s:%d`, got, p.filename, p.line()) + } + p.src = p.src[1:] + return a.NewConst(flags, p.filename, line, id, typ, value).Node(), nil + case t.KeyFunc: p.src = p.src[1:] id0, id1, err := p.parseQualifiedIdent() @@ -660,6 +695,19 @@ func (p *parser) parseArgNode() (*a.Node, error) { return a.NewArg(name, value).Node(), nil } +func (p *parser) parseDollarExpr() (*a.Expr, error) { + if x := p.peek1().Key(); x != t.KeyDollar { + got := p.tm.ByKey(x) + return nil, fmt.Errorf(`parse: expected "$", got %q at %s:%d`, got, p.filename, p.line()) + } + p.src = p.src[1:] + args, err := p.parseList(t.KeyCloseParen, (*parser).parseExprNode) + if err != nil { + return nil, err + } + return a.NewExpr(0, t.IDDollar, 0, nil, nil, nil, args), nil +} + func (p *parser) parseLimitExpr() (*a.Expr, error) { if x := p.peek1().Key(); x != t.KeyLimit { got := p.tm.ByKey(x) @@ -687,6 +735,14 @@ func (p *parser) parseLimitExpr() (*a.Expr, error) { return a.NewExpr(0, t.IDLimit, 0, lhs.Node(), nil, rhs.Node(), nil), nil } +func (p *parser) parseExprNode() (*a.Node, error) { + n, err := p.parseExpr() + if err != nil { + return nil, err + } + return n.Node(), err +} + func (p *parser) parseExpr() (*a.Expr, error) { lhs, err := p.parseOperand() if err != nil { diff --git a/lang/token/list.go b/lang/token/list.go index 7f923c6f..2b5eaafd 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -150,6 +150,7 @@ const ( KeyQuestion = Key(IDQuestion >> KeyShift) KeyColon = Key(IDColon >> KeyShift) KeySemicolon = Key(IDSemicolon >> KeyShift) + KeyDollar = Key(IDDollar >> KeyShift) KeyEq = Key(IDEq >> KeyShift) KeyPlusEq = Key(IDPlusEq >> KeyShift) @@ -211,6 +212,7 @@ const ( KeySuspension = Key(IDSuspension >> KeyShift) KeyLimit = Key(IDLimit >> KeyShift) KeyPackageID = Key(IDPackageID >> KeyShift) + KeyConst = Key(IDConst >> KeyShift) KeyFalse = Key(IDFalse >> KeyShift) KeyTrue = Key(IDTrue >> KeyShift) @@ -312,6 +314,7 @@ const ( IDQuestion = ID(0x24<