Add the "const" keyword.

This commit is contained in:
Nigel Tao committed 2017-07-16 12:02:12 +10:00
1 parent 04a41987f0
commit b78807d60a
13 files changed
+292 -4

No files matched your search

+62
View File
@@ -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
+2 -2
View File
@@ -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`
+8
View File
@@ -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
+4
View File
@@ -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
+2
View File
@@ -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 {
+2
View File
@@ -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 {
+34 -2
View File
@@ -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: <nil|Expr>
// - MHS: <nil|Expr>
// - RHS: <nil|Expr|TypeExpr>
// - List0: <Arg|Expr> function call arguments or associative op arguments
// - List0: <Arg|Expr> 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: <TypeExpr>
// - RHS: <Expr>
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"
+13
View File
@@ -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:
+85
View File
@@ -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()
+10
View File
@@ -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 {
+56
View File
@@ -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 {
+7
View File
@@ -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<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDColon = ID(0x25<<KeyShift | FlagsTightLeft | FlagsTightRight)
IDSemicolon = ID(0x26<<KeyShift | FlagsTightLeft)
IDDollar = ID(0x27<<KeyShift | FlagsTightRight)
IDEq = ID(0x30<<KeyShift | FlagsAssign)
IDPlusEq = ID(0x31<<KeyShift | FlagsAssign)
@@ -373,6 +376,7 @@ const (
IDSuspension = ID(0x74<<KeyShift | FlagsOther)
IDLimit = ID(0x75<<KeyShift | FlagsOther)
IDPackageID = ID(0x76<<KeyShift | FlagsOther)
IDConst = ID(0x77<<KeyShift | FlagsOther)
IDFalse = ID(0x80<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
IDTrue = ID(0x81<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
@@ -491,6 +495,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeyQuestion: {"?", IDQuestion},
KeyColon: {":", IDColon},
KeySemicolon: {";", IDSemicolon},
KeyDollar: {"$", IDDollar},
KeyEq: {"=", IDEq},
KeyPlusEq: {"+=", IDPlusEq},
@@ -555,6 +560,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
KeySuspension: {"suspension", IDSuspension},
KeyLimit: {"limit", IDLimit},
KeyPackageID: {"packageid", IDPackageID},
KeyConst: {"const", IDConst},
KeyFalse: {"false", IDFalse},
KeyTrue: {"true", IDTrue},
@@ -630,6 +636,7 @@ var squiggles = [256]ID{
'?': IDQuestion,
':': IDColon,
';': IDSemicolon,
'$': IDDollar,
}
type suffixLexer struct {
+7
View File
@@ -0,0 +1,7 @@
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
// code_order is defined in the RFC section 3.2.7.
pri const code_order[19] u8[..18] = $(
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
)