mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 00:55:11 +01:00
Add the "const" keyword.
This commit is contained in:
1 parent
04a41987f0
commit
b78807d60a
13 files changed
+292
-4
No files matched your search
@@ -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("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.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")
|
g.writes("// ---------------- Structs\n\n")
|
||||||
for _, n := range g.structList {
|
for _, n := range g.structList {
|
||||||
if err := g.writeStruct(n); err != nil {
|
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.printf("return i < n ? a[i] : \"%s: unknown status\";\n", g.pkgName)
|
||||||
g.writes("}\n\n")
|
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")
|
g.writes("// ---------------- Private Constructor and Destructor Prototypes\n\n")
|
||||||
for _, n := range g.structList {
|
for _, n := range g.structList {
|
||||||
if !n.Public() {
|
if !n.Public() {
|
||||||
@@ -353,6 +363,22 @@ func (g *gen) genImpl() error {
|
|||||||
return nil
|
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 {
|
func (g *gen) forEachFunc(v visibility, f func(*gen, *a.Func) error) error {
|
||||||
for _, file := range g.files {
|
for _, file := range g.files {
|
||||||
for _, tld := range file.TopLevelDecls() {
|
for _, tld := range file.TopLevelDecls() {
|
||||||
@@ -446,6 +472,42 @@ func (g *gen) gatherStatuses(n *a.Status) error {
|
|||||||
return nil
|
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 {
|
func (g *gen) writeStruct(n *a.Struct) error {
|
||||||
// For API/ABI compatibility, the very first field in the struct's
|
// For API/ABI compatibility, the very first field in the struct's
|
||||||
// private_impl must be the status code. This lets the constructor callee
|
// private_impl must be the status code. This lets the constructor callee
|
||||||
|
|||||||
@@ -12,9 +12,9 @@ be enclosed by curly `{}`s. There is no 'dangling else' ambiguity.
|
|||||||
|
|
||||||
## Keywords
|
## Keywords
|
||||||
|
|
||||||
Puffs is a simple language, with only 23 keywords. 6 of these introduce
|
The Puffs language has 24 keywords. 7 of those introduce top-level concepts:
|
||||||
top-level concepts:
|
|
||||||
|
|
||||||
|
- `const`
|
||||||
- `error`
|
- `error`
|
||||||
- `func`
|
- `func`
|
||||||
- `packageid`
|
- `packageid`
|
||||||
|
|||||||
@@ -115,6 +115,8 @@ bool puffs_flate_status_is_error(puffs_flate_status s);
|
|||||||
|
|
||||||
const char* puffs_flate_status_string(puffs_flate_status s);
|
const char* puffs_flate_status_string(puffs_flate_status s);
|
||||||
|
|
||||||
|
// ---------------- Public Consts
|
||||||
|
|
||||||
// ---------------- Structs
|
// ---------------- Structs
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -269,6 +271,12 @@ const char* puffs_flate_status_string(puffs_flate_status s) {
|
|||||||
return i < n ? a[i] : "flate: unknown status";
|
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 Constructor and Destructor Prototypes
|
||||||
|
|
||||||
// ---------------- Private Function Prototypes
|
// ---------------- Private Function Prototypes
|
||||||
|
|||||||
@@ -113,6 +113,8 @@ bool puffs_gif_status_is_error(puffs_gif_status s);
|
|||||||
|
|
||||||
const char* puffs_gif_status_string(puffs_gif_status s);
|
const char* puffs_gif_status_string(puffs_gif_status s);
|
||||||
|
|
||||||
|
// ---------------- Public Consts
|
||||||
|
|
||||||
// ---------------- Structs
|
// ---------------- Structs
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -323,6 +325,8 @@ const char* puffs_gif_status_string(puffs_gif_status s) {
|
|||||||
return i < n ? a[i] : "gif: unknown status";
|
return i < n ? a[i] : "gif: unknown status";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------------- Private Consts
|
||||||
|
|
||||||
// ---------------- Private Constructor and Destructor Prototypes
|
// ---------------- Private Constructor and Destructor Prototypes
|
||||||
|
|
||||||
// ---------------- Private Function Prototypes
|
// ---------------- Private Function Prototypes
|
||||||
|
|||||||
@@ -115,6 +115,8 @@ bool puffs_flate_status_is_error(puffs_flate_status s);
|
|||||||
|
|
||||||
const char* puffs_flate_status_string(puffs_flate_status s);
|
const char* puffs_flate_status_string(puffs_flate_status s);
|
||||||
|
|
||||||
|
// ---------------- Public Consts
|
||||||
|
|
||||||
// ---------------- Structs
|
// ---------------- Structs
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
@@ -113,6 +113,8 @@ bool puffs_gif_status_is_error(puffs_gif_status s);
|
|||||||
|
|
||||||
const char* puffs_gif_status_string(puffs_gif_status s);
|
const char* puffs_gif_status_string(puffs_gif_status s);
|
||||||
|
|
||||||
|
// ---------------- Public Consts
|
||||||
|
|
||||||
// ---------------- Structs
|
// ---------------- Structs
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
+34
-2
@@ -24,6 +24,7 @@ const (
|
|||||||
KArg
|
KArg
|
||||||
KAssert
|
KAssert
|
||||||
KAssign
|
KAssign
|
||||||
|
KConst
|
||||||
KExpr
|
KExpr
|
||||||
KField
|
KField
|
||||||
KFile
|
KFile
|
||||||
@@ -53,6 +54,7 @@ var kindStrings = [...]string{
|
|||||||
KArg: "KArg",
|
KArg: "KArg",
|
||||||
KAssert: "KAssert",
|
KAssert: "KAssert",
|
||||||
KAssign: "KAssign",
|
KAssign: "KAssign",
|
||||||
|
KConst: "KConst",
|
||||||
KExpr: "KExpr",
|
KExpr: "KExpr",
|
||||||
KField: "KField",
|
KField: "KField",
|
||||||
KFile: "KFile",
|
KFile: "KFile",
|
||||||
@@ -109,6 +111,7 @@ func (n *Node) SetTypeChecked() { n.flags |= FlagsTypeChecked }
|
|||||||
func (n *Node) Arg() *Arg { return (*Arg)(n) }
|
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) Const() *Const { return (*Const)(n) }
|
||||||
func (n *Node) Expr() *Expr { return (*Expr)(n) }
|
func (n *Node) Expr() *Expr { return (*Expr)(n) }
|
||||||
func (n *Node) Field() *Field { return (*Field)(n) }
|
func (n *Node) Field() *Field { return (*Field)(n) }
|
||||||
func (n *Node) File() *File { return (*File)(n) }
|
func (n *Node) File() *File { return (*File)(n) }
|
||||||
@@ -182,7 +185,7 @@ const MaxExprDepth = 255
|
|||||||
// - LHS: <nil|Expr>
|
// - LHS: <nil|Expr>
|
||||||
// - MHS: <nil|Expr>
|
// - MHS: <nil|Expr>
|
||||||
// - RHS: <nil|Expr|TypeExpr>
|
// - 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".
|
// 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 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.
|
// For limits, like "limit (LHS) RHS", ID0 is IDLimit.
|
||||||
type Expr Node
|
type Expr Node
|
||||||
|
|
||||||
@@ -463,7 +468,7 @@ const MaxTypeExprDepth = 63
|
|||||||
// refined as "foo[LHS..MHS]". LHS and MHS are Expr's, possibly nil. For
|
// refined as "foo[LHS..MHS]". LHS and MHS are Expr's, possibly nil. For
|
||||||
// example, the LHS for "u32[..4095]" is nil.
|
// 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
|
type TypeExpr Node
|
||||||
|
|
||||||
func (n *TypeExpr) Node() *Node { return (*Node)(n) }
|
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)":
|
// Struct is "struct ID1(List0)":
|
||||||
// - FlagsSuspendible is "ID1" vs "ID1?"
|
// - FlagsSuspendible is "ID1" vs "ID1?"
|
||||||
// - FlagsPublic is "pub" vs "pri"
|
// - FlagsPublic is "pub" vs "pri"
|
||||||
|
|||||||
@@ -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 = n.lhs.Expr().appendString(buf, tm, true, depth)
|
||||||
buf = append(buf, '.')
|
buf = append(buf, '.')
|
||||||
buf = append(buf, tm.ByID(n.id1)...)
|
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:
|
case t.FlagsUnaryOp:
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package check
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
|
||||||
"github.com/google/puffs/lang/base38"
|
"github.com/google/puffs/lang/base38"
|
||||||
|
|
||||||
@@ -55,6 +56,7 @@ func trimQuotes(s string) string {
|
|||||||
var (
|
var (
|
||||||
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
|
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
|
||||||
typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, 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)
|
typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil)
|
||||||
|
|
||||||
// TODO: delete this.
|
// TODO: delete this.
|
||||||
@@ -65,6 +67,11 @@ var (
|
|||||||
// TypeMap maps from variable names (as token IDs) to types.
|
// TypeMap maps from variable names (as token IDs) to types.
|
||||||
type TypeMap map[t.ID]*a.TypeExpr
|
type TypeMap map[t.ID]*a.TypeExpr
|
||||||
|
|
||||||
|
type Const struct {
|
||||||
|
ID t.ID // ID of the const name.
|
||||||
|
Const *a.Const
|
||||||
|
}
|
||||||
|
|
||||||
type Func struct {
|
type Func struct {
|
||||||
QID t.QID // Qualified ID of the func name.
|
QID t.QID // Qualified ID of the func name.
|
||||||
Func *a.Func
|
Func *a.Func
|
||||||
@@ -108,6 +115,7 @@ func Check(tm *t.Map, files ...*a.File) (*Checker, error) {
|
|||||||
tm: tm,
|
tm: tm,
|
||||||
reasonMap: rMap,
|
reasonMap: rMap,
|
||||||
packageID: base38.Max + 1,
|
packageID: base38.Max + 1,
|
||||||
|
consts: map[t.ID]Const{},
|
||||||
funcs: map[t.QID]Func{},
|
funcs: map[t.QID]Func{},
|
||||||
statuses: map[t.ID]Status{},
|
statuses: map[t.ID]Status{},
|
||||||
structs: map[t.ID]Struct{},
|
structs: map[t.ID]Struct{},
|
||||||
@@ -142,6 +150,7 @@ var phases = [...]struct {
|
|||||||
{a.KPackageID, (*Checker).checkPackageID},
|
{a.KPackageID, (*Checker).checkPackageID},
|
||||||
{a.KUse, (*Checker).checkUse},
|
{a.KUse, (*Checker).checkUse},
|
||||||
{a.KStatus, (*Checker).checkStatus},
|
{a.KStatus, (*Checker).checkStatus},
|
||||||
|
{a.KConst, (*Checker).checkConst},
|
||||||
{a.KStruct, (*Checker).checkStructDecl},
|
{a.KStruct, (*Checker).checkStructDecl},
|
||||||
{a.KInvalid, (*Checker).checkStructCycles},
|
{a.KInvalid, (*Checker).checkStructCycles},
|
||||||
{a.KStruct, (*Checker).checkStructFields},
|
{a.KStruct, (*Checker).checkStructFields},
|
||||||
@@ -149,6 +158,7 @@ var phases = [...]struct {
|
|||||||
{a.KFunc, (*Checker).checkFuncContract},
|
{a.KFunc, (*Checker).checkFuncContract},
|
||||||
{a.KFunc, (*Checker).checkFuncBody},
|
{a.KFunc, (*Checker).checkFuncBody},
|
||||||
{a.KStruct, (*Checker).checkFieldMethodCollisions},
|
{a.KStruct, (*Checker).checkFieldMethodCollisions},
|
||||||
|
// TODO: check consts, funcs, structs and uses for name collisions.
|
||||||
}
|
}
|
||||||
|
|
||||||
type Checker struct {
|
type Checker struct {
|
||||||
@@ -158,6 +168,7 @@ type Checker struct {
|
|||||||
packageID uint32
|
packageID uint32
|
||||||
otherPackageID *a.PackageID
|
otherPackageID *a.PackageID
|
||||||
|
|
||||||
|
consts map[t.ID]Const
|
||||||
funcs map[t.QID]Func
|
funcs map[t.QID]Func
|
||||||
statuses map[t.ID]Status
|
statuses map[t.ID]Status
|
||||||
structs map[t.ID]Struct
|
structs map[t.ID]Struct
|
||||||
@@ -166,6 +177,7 @@ type Checker struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checker) PackageID() uint32 { return c.packageID }
|
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) Funcs() map[t.QID]Func { return c.funcs }
|
||||||
func (c *Checker) Statuses() map[t.ID]Status { return c.statuses }
|
func (c *Checker) Statuses() map[t.ID]Status { return c.statuses }
|
||||||
func (c *Checker) Structs() map[t.ID]Struct { return c.structs }
|
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
|
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 {
|
func (c *Checker) checkStructDecl(node *a.Node) error {
|
||||||
n := node.Struct()
|
n := node.Struct()
|
||||||
id := n.Name()
|
id := n.Name()
|
||||||
|
|||||||
@@ -438,6 +438,16 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
|||||||
case t.KeyDot:
|
case t.KeyDot:
|
||||||
return q.tcheckDot(n, depth)
|
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:
|
case t.KeyLimit:
|
||||||
lhs := n.LHS().Expr()
|
lhs := n.LHS().Expr()
|
||||||
if err := q.tcheckExpr(lhs, depth); err != nil {
|
if err := q.tcheckExpr(lhs, depth); err != nil {
|
||||||
|
|||||||
@@ -108,6 +108,41 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) {
|
|||||||
case t.KeyPri:
|
case t.KeyPri:
|
||||||
p.src = p.src[1:]
|
p.src = p.src[1:]
|
||||||
switch p.peek1().Key() {
|
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:
|
case t.KeyFunc:
|
||||||
p.src = p.src[1:]
|
p.src = p.src[1:]
|
||||||
id0, id1, err := p.parseQualifiedIdent()
|
id0, id1, err := p.parseQualifiedIdent()
|
||||||
@@ -660,6 +695,19 @@ func (p *parser) parseArgNode() (*a.Node, error) {
|
|||||||
return a.NewArg(name, value).Node(), nil
|
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) {
|
func (p *parser) parseLimitExpr() (*a.Expr, error) {
|
||||||
if x := p.peek1().Key(); x != t.KeyLimit {
|
if x := p.peek1().Key(); x != t.KeyLimit {
|
||||||
got := p.tm.ByKey(x)
|
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
|
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) {
|
func (p *parser) parseExpr() (*a.Expr, error) {
|
||||||
lhs, err := p.parseOperand()
|
lhs, err := p.parseOperand()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -150,6 +150,7 @@ const (
|
|||||||
KeyQuestion = Key(IDQuestion >> KeyShift)
|
KeyQuestion = Key(IDQuestion >> KeyShift)
|
||||||
KeyColon = Key(IDColon >> KeyShift)
|
KeyColon = Key(IDColon >> KeyShift)
|
||||||
KeySemicolon = Key(IDSemicolon >> KeyShift)
|
KeySemicolon = Key(IDSemicolon >> KeyShift)
|
||||||
|
KeyDollar = Key(IDDollar >> KeyShift)
|
||||||
|
|
||||||
KeyEq = Key(IDEq >> KeyShift)
|
KeyEq = Key(IDEq >> KeyShift)
|
||||||
KeyPlusEq = Key(IDPlusEq >> KeyShift)
|
KeyPlusEq = Key(IDPlusEq >> KeyShift)
|
||||||
@@ -211,6 +212,7 @@ const (
|
|||||||
KeySuspension = Key(IDSuspension >> KeyShift)
|
KeySuspension = Key(IDSuspension >> KeyShift)
|
||||||
KeyLimit = Key(IDLimit >> KeyShift)
|
KeyLimit = Key(IDLimit >> KeyShift)
|
||||||
KeyPackageID = Key(IDPackageID >> KeyShift)
|
KeyPackageID = Key(IDPackageID >> KeyShift)
|
||||||
|
KeyConst = Key(IDConst >> KeyShift)
|
||||||
|
|
||||||
KeyFalse = Key(IDFalse >> KeyShift)
|
KeyFalse = Key(IDFalse >> KeyShift)
|
||||||
KeyTrue = Key(IDTrue >> KeyShift)
|
KeyTrue = Key(IDTrue >> KeyShift)
|
||||||
@@ -312,6 +314,7 @@ const (
|
|||||||
IDQuestion = ID(0x24<<KeyShift | FlagsTightLeft | FlagsTightRight)
|
IDQuestion = ID(0x24<<KeyShift | FlagsTightLeft | FlagsTightRight)
|
||||||
IDColon = ID(0x25<<KeyShift | FlagsTightLeft | FlagsTightRight)
|
IDColon = ID(0x25<<KeyShift | FlagsTightLeft | FlagsTightRight)
|
||||||
IDSemicolon = ID(0x26<<KeyShift | FlagsTightLeft)
|
IDSemicolon = ID(0x26<<KeyShift | FlagsTightLeft)
|
||||||
|
IDDollar = ID(0x27<<KeyShift | FlagsTightRight)
|
||||||
|
|
||||||
IDEq = ID(0x30<<KeyShift | FlagsAssign)
|
IDEq = ID(0x30<<KeyShift | FlagsAssign)
|
||||||
IDPlusEq = ID(0x31<<KeyShift | FlagsAssign)
|
IDPlusEq = ID(0x31<<KeyShift | FlagsAssign)
|
||||||
@@ -373,6 +376,7 @@ const (
|
|||||||
IDSuspension = ID(0x74<<KeyShift | FlagsOther)
|
IDSuspension = ID(0x74<<KeyShift | FlagsOther)
|
||||||
IDLimit = ID(0x75<<KeyShift | FlagsOther)
|
IDLimit = ID(0x75<<KeyShift | FlagsOther)
|
||||||
IDPackageID = ID(0x76<<KeyShift | FlagsOther)
|
IDPackageID = ID(0x76<<KeyShift | FlagsOther)
|
||||||
|
IDConst = ID(0x77<<KeyShift | FlagsOther)
|
||||||
|
|
||||||
IDFalse = ID(0x80<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
IDFalse = ID(0x80<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
||||||
IDTrue = ID(0x81<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
IDTrue = ID(0x81<<KeyShift | FlagsLiteral | FlagsImplicitSemicolon)
|
||||||
@@ -491,6 +495,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
|
|||||||
KeyQuestion: {"?", IDQuestion},
|
KeyQuestion: {"?", IDQuestion},
|
||||||
KeyColon: {":", IDColon},
|
KeyColon: {":", IDColon},
|
||||||
KeySemicolon: {";", IDSemicolon},
|
KeySemicolon: {";", IDSemicolon},
|
||||||
|
KeyDollar: {"$", IDDollar},
|
||||||
|
|
||||||
KeyEq: {"=", IDEq},
|
KeyEq: {"=", IDEq},
|
||||||
KeyPlusEq: {"+=", IDPlusEq},
|
KeyPlusEq: {"+=", IDPlusEq},
|
||||||
@@ -555,6 +560,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
|
|||||||
KeySuspension: {"suspension", IDSuspension},
|
KeySuspension: {"suspension", IDSuspension},
|
||||||
KeyLimit: {"limit", IDLimit},
|
KeyLimit: {"limit", IDLimit},
|
||||||
KeyPackageID: {"packageid", IDPackageID},
|
KeyPackageID: {"packageid", IDPackageID},
|
||||||
|
KeyConst: {"const", IDConst},
|
||||||
|
|
||||||
KeyFalse: {"false", IDFalse},
|
KeyFalse: {"false", IDFalse},
|
||||||
KeyTrue: {"true", IDTrue},
|
KeyTrue: {"true", IDTrue},
|
||||||
@@ -630,6 +636,7 @@ var squiggles = [256]ID{
|
|||||||
'?': IDQuestion,
|
'?': IDQuestion,
|
||||||
':': IDColon,
|
':': IDColon,
|
||||||
';': IDSemicolon,
|
';': IDSemicolon,
|
||||||
|
'$': IDDollar,
|
||||||
}
|
}
|
||||||
|
|
||||||
type suffixLexer struct {
|
type suffixLexer struct {
|
||||||
|
|||||||
@@ -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,
|
||||||
|
)
|
||||||
Reference in new issue
Block a user