mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 16:52:18 +01:00
Add pkg to ast Const and Status nodes
This commit is contained in:
1 parent
33efa88e54
commit
10e4936f08
5 files changed
+73
-46
No files matched your search
@@ -470,7 +470,7 @@ func (g *gen) sizeof(typ *a.TypeExpr) (uint32, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *gen) gatherStatuses(b *buffer, n *a.Status) error {
|
func (g *gen) gatherStatuses(b *buffer, n *a.Status) error {
|
||||||
raw := n.Message().Str(g.tm)
|
raw := n.QID()[1].Str(g.tm)
|
||||||
msg, ok := t.Unescape(raw)
|
msg, ok := t.Unescape(raw)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("bad status message %q", raw)
|
return fmt.Errorf("bad status message %q", raw)
|
||||||
@@ -485,7 +485,7 @@ func (g *gen) gatherStatuses(b *buffer, n *a.Status) error {
|
|||||||
keyword: n.Keyword(),
|
keyword: n.Keyword(),
|
||||||
}
|
}
|
||||||
g.statusList = append(g.statusList, s)
|
g.statusList = append(g.statusList, s)
|
||||||
g.statusMap[n.Message()] = s
|
g.statusMap[n.QID()[1]] = s
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -494,7 +494,7 @@ func (g *gen) writeConst(b *buffer, n *a.Const) error {
|
|||||||
b.writes("static ")
|
b.writes("static ")
|
||||||
}
|
}
|
||||||
b.writes("const ")
|
b.writes("const ")
|
||||||
if err := g.writeCTypeName(b, n.XType(), g.pkgPrefix, n.Name().Str(g.tm)); err != nil {
|
if err := g.writeCTypeName(b, n.XType(), g.pkgPrefix, n.QID()[1].Str(g.tm)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
b.writes(" = ")
|
b.writes(" = ")
|
||||||
|
|||||||
+8
-1
@@ -249,6 +249,13 @@ func (h *genHelper) genWuffs(dirname string, qualifiedFilenames []string) error
|
|||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
for _, n := range f.TopLevelDecls() {
|
for _, n := range f.TopLevelDecls() {
|
||||||
switch n.Kind() {
|
switch n.Kind() {
|
||||||
|
case a.KConst:
|
||||||
|
n := n.Const()
|
||||||
|
if !n.Public() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return fmt.Errorf("TODO: genWuffs for consts")
|
||||||
|
|
||||||
case a.KFunc:
|
case a.KFunc:
|
||||||
n := n.Func()
|
n := n.Func()
|
||||||
if !n.Public() {
|
if !n.Public() {
|
||||||
@@ -286,7 +293,7 @@ func (h *genHelper) genWuffs(dirname string, qualifiedFilenames []string) error
|
|||||||
if !n.Public() {
|
if !n.Public() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Fprintf(out, "pub %s %s\n", n.Keyword().Str(&h.tm), n.Message().Str(&h.tm))
|
fmt.Fprintf(out, "pub %s %s\n", n.Keyword().Str(&h.tm), n.QID().Str(&h.tm))
|
||||||
|
|
||||||
case a.KStruct:
|
case a.KStruct:
|
||||||
n := n.Struct()
|
n := n.Struct()
|
||||||
|
|||||||
+23
-6
@@ -15,6 +15,7 @@
|
|||||||
package ast
|
package ast
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
t "github.com/google/wuffs/lang/token"
|
t "github.com/google/wuffs/lang/token"
|
||||||
@@ -138,7 +139,7 @@ type Node struct {
|
|||||||
// Arg . . name Arg
|
// Arg . . name Arg
|
||||||
// Assert keyword reason . Assert
|
// Assert keyword reason . Assert
|
||||||
// Assign operator . . Assign
|
// Assign operator . . Assign
|
||||||
// Const . . name Const
|
// Const . pkg name Const
|
||||||
// Expr operator literal/ident . Expr
|
// Expr operator literal/ident . Expr
|
||||||
// Field . . name Field
|
// Field . . name Field
|
||||||
// File . . . File
|
// File . . . File
|
||||||
@@ -148,7 +149,7 @@ type Node struct {
|
|||||||
// Jump keyword label . Jump
|
// Jump keyword label . Jump
|
||||||
// PackageID . literal . PackageID
|
// PackageID . literal . PackageID
|
||||||
// Ret keyword . . Ret
|
// Ret keyword . . Ret
|
||||||
// Status keyword literal . Status
|
// Status keyword pkg literal Status
|
||||||
// Struct . . name Struct
|
// Struct . . name Struct
|
||||||
// TypeExpr decorator . name TypeExpr
|
// TypeExpr decorator . name TypeExpr
|
||||||
// Use . literal . Use
|
// Use . literal . Use
|
||||||
@@ -234,6 +235,20 @@ func (n *Raw) SubLists() [3][]*Node { return [3][]*Node{n.list0, n.lis
|
|||||||
|
|
||||||
func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l }
|
func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l }
|
||||||
|
|
||||||
|
func (n *Raw) SetPackage(tm *t.Map, pkg t.ID) error {
|
||||||
|
return n.Node().Walk(func(o *Node) error {
|
||||||
|
switch o.Kind() {
|
||||||
|
case KConst, KStatus:
|
||||||
|
if o.id1 != 0 {
|
||||||
|
return fmt.Errorf(`invalid SetPackage(%q) call: old package: got %q, want ""`,
|
||||||
|
pkg.Str(tm), o.id1.Str(tm))
|
||||||
|
}
|
||||||
|
o.id1 = pkg
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// MaxExprDepth is an advisory limit for an Expr's recursion depth.
|
// MaxExprDepth is an advisory limit for an Expr's recursion depth.
|
||||||
const MaxExprDepth = 255
|
const MaxExprDepth = 255
|
||||||
|
|
||||||
@@ -731,7 +746,8 @@ func NewFunc(flags Flags, filename string, line uint32, receiver t.ID, name t.ID
|
|||||||
// Status is "error ID1" or "suspension ID1":
|
// Status is "error ID1" or "suspension ID1":
|
||||||
// - FlagsPublic is "pub" vs "pri"
|
// - FlagsPublic is "pub" vs "pri"
|
||||||
// - ID0: <IDError|IDSuspension>
|
// - ID0: <IDError|IDSuspension>
|
||||||
// - ID1: message
|
// - ID1: <0|pkg> (set by calling SetPackage)
|
||||||
|
// - ID2: message
|
||||||
type Status Node
|
type Status Node
|
||||||
|
|
||||||
func (n *Status) Node() *Node { return (*Node)(n) }
|
func (n *Status) Node() *Node { return (*Node)(n) }
|
||||||
@@ -739,7 +755,7 @@ func (n *Status) Public() bool { return n.flags&FlagsPublic != 0 }
|
|||||||
func (n *Status) Filename() string { return n.filename }
|
func (n *Status) Filename() string { return n.filename }
|
||||||
func (n *Status) Line() uint32 { return n.line }
|
func (n *Status) Line() uint32 { return n.line }
|
||||||
func (n *Status) Keyword() t.ID { return n.id0 }
|
func (n *Status) Keyword() t.ID { return n.id0 }
|
||||||
func (n *Status) Message() t.ID { return n.id1 }
|
func (n *Status) QID() t.QID { return t.QID{n.id1, n.id2} }
|
||||||
|
|
||||||
func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, message t.ID) *Status {
|
func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, message t.ID) *Status {
|
||||||
return &Status{
|
return &Status{
|
||||||
@@ -748,12 +764,13 @@ func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, message
|
|||||||
filename: filename,
|
filename: filename,
|
||||||
line: line,
|
line: line,
|
||||||
id0: keyword,
|
id0: keyword,
|
||||||
id1: message,
|
id2: message,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Const is "const ID1 LHS = RHS":
|
// Const is "const ID1 LHS = RHS":
|
||||||
// - FlagsPublic is "pub" vs "pri"
|
// - FlagsPublic is "pub" vs "pri"
|
||||||
|
// - ID1: <0|pkg> (set by calling SetPackage)
|
||||||
// - ID2: name
|
// - ID2: name
|
||||||
// - LHS: <TypeExpr>
|
// - LHS: <TypeExpr>
|
||||||
// - RHS: <Expr>
|
// - RHS: <Expr>
|
||||||
@@ -763,7 +780,7 @@ func (n *Const) Node() *Node { return (*Node)(n) }
|
|||||||
func (n *Const) Public() bool { return n.flags&FlagsPublic != 0 }
|
func (n *Const) Public() bool { return n.flags&FlagsPublic != 0 }
|
||||||
func (n *Const) Filename() string { return n.filename }
|
func (n *Const) Filename() string { return n.filename }
|
||||||
func (n *Const) Line() uint32 { return n.line }
|
func (n *Const) Line() uint32 { return n.line }
|
||||||
func (n *Const) Name() t.ID { return n.id2 }
|
func (n *Const) QID() t.QID { return t.QID{n.id1, n.id2} }
|
||||||
func (n *Const) XType() *TypeExpr { return n.lhs.TypeExpr() }
|
func (n *Const) XType() *TypeExpr { return n.lhs.TypeExpr() }
|
||||||
func (n *Const) Value() *Expr { return n.rhs.Expr() }
|
func (n *Const) Value() *Expr { return n.rhs.Expr() }
|
||||||
|
|
||||||
|
|||||||
+35
-33
@@ -61,7 +61,7 @@ func (e *Error) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Const struct {
|
type Const struct {
|
||||||
ID t.ID // ID of the const name.
|
QID t.QID // Qualified ID of the const name.
|
||||||
Const *a.Const
|
Const *a.Const
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ type Func struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Status struct {
|
type Status struct {
|
||||||
ID t.ID // ID of the status message.
|
QID t.QID // Qualified ID of the status message.
|
||||||
Status *a.Status
|
Status *a.Status
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,9 +82,9 @@ type Struct struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type usee struct {
|
type usee struct {
|
||||||
consts map[t.ID]*a.Const
|
consts map[t.QID]*a.Const
|
||||||
funcs map[t.QID]*a.Func
|
funcs map[t.QID]*a.Func
|
||||||
statuses map[t.ID]*a.Status
|
statuses map[t.QID]*a.Status
|
||||||
structs map[t.ID]*a.Struct
|
structs map[t.ID]*a.Struct
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,9 +116,9 @@ func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte,
|
|||||||
resolveUse: resolveUse,
|
resolveUse: resolveUse,
|
||||||
reasonMap: rMap,
|
reasonMap: rMap,
|
||||||
packageID: base38.Max + 1,
|
packageID: base38.Max + 1,
|
||||||
consts: map[t.ID]Const{},
|
consts: map[t.QID]Const{},
|
||||||
funcs: map[t.QID]Func{},
|
funcs: map[t.QID]Func{},
|
||||||
statuses: map[t.ID]Status{},
|
statuses: map[t.QID]Status{},
|
||||||
structs: map[t.ID]Struct{},
|
structs: map[t.ID]Struct{},
|
||||||
usees: map[t.ID]*usee{},
|
usees: map[t.ID]*usee{},
|
||||||
}
|
}
|
||||||
@@ -176,9 +176,9 @@ type Checker struct {
|
|||||||
packageID uint32
|
packageID uint32
|
||||||
otherPackageID *a.PackageID
|
otherPackageID *a.PackageID
|
||||||
|
|
||||||
consts map[t.ID]Const
|
consts map[t.QID]Const
|
||||||
funcs map[t.QID]Func
|
funcs map[t.QID]Func
|
||||||
statuses map[t.ID]Status
|
statuses map[t.QID]Status
|
||||||
structs map[t.ID]Struct
|
structs map[t.ID]Struct
|
||||||
|
|
||||||
// usees are the packages referred to in the `use "foo/bar"` lines. The
|
// usees are the packages referred to in the `use "foo/bar"` lines. The
|
||||||
@@ -190,11 +190,11 @@ type Checker struct {
|
|||||||
unsortedStructs []*a.Struct
|
unsortedStructs []*a.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) Consts() map[t.QID]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.QID]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 }
|
||||||
|
|
||||||
func (c *Checker) checkPackageID(node *a.Node) error {
|
func (c *Checker) checkPackageID(node *a.Node) error {
|
||||||
n := node.PackageID()
|
n := node.PackageID()
|
||||||
@@ -270,22 +270,25 @@ func (c *Checker) checkUse(node *a.Node) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
u := &usee{
|
u := &usee{
|
||||||
consts: map[t.ID]*a.Const{},
|
consts: map[t.QID]*a.Const{},
|
||||||
funcs: map[t.QID]*a.Func{},
|
funcs: map[t.QID]*a.Func{},
|
||||||
statuses: map[t.ID]*a.Status{},
|
statuses: map[t.QID]*a.Status{},
|
||||||
structs: map[t.ID]*a.Struct{},
|
structs: map[t.ID]*a.Struct{},
|
||||||
}
|
}
|
||||||
for _, n := range f.TopLevelDecls() {
|
for _, n := range f.TopLevelDecls() {
|
||||||
|
if err := n.Raw().SetPackage(c.tm, base); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
switch n.Kind() {
|
switch n.Kind() {
|
||||||
case a.KConst:
|
case a.KConst:
|
||||||
n := n.Const()
|
n := n.Const()
|
||||||
u.consts[n.Name()] = n
|
u.consts[n.QID()] = n
|
||||||
case a.KFunc:
|
case a.KFunc:
|
||||||
n := n.Func()
|
n := n.Func()
|
||||||
u.funcs[n.QID()] = n
|
u.funcs[n.QID()] = n
|
||||||
case a.KStatus:
|
case a.KStatus:
|
||||||
n := n.Status()
|
n := n.Status()
|
||||||
u.statuses[n.Message()] = n
|
u.statuses[n.QID()] = n
|
||||||
case a.KStruct:
|
case a.KStruct:
|
||||||
n := n.Struct()
|
n := n.Struct()
|
||||||
u.structs[n.Name()] = n
|
u.structs[n.Name()] = n
|
||||||
@@ -297,19 +300,18 @@ func (c *Checker) checkUse(node *a.Node) error {
|
|||||||
|
|
||||||
func (c *Checker) checkStatus(node *a.Node) error {
|
func (c *Checker) checkStatus(node *a.Node) error {
|
||||||
n := node.Status()
|
n := node.Status()
|
||||||
id := n.Message()
|
qid := n.QID()
|
||||||
if other, ok := c.statuses[id]; ok {
|
if other, ok := c.statuses[qid]; ok {
|
||||||
s, _ := t.Unescape(id.Str(c.tm))
|
|
||||||
return &Error{
|
return &Error{
|
||||||
Err: fmt.Errorf("check: duplicate status %q", s),
|
Err: fmt.Errorf("check: duplicate status %s", qid.Str(c.tm)),
|
||||||
Filename: n.Filename(),
|
Filename: n.Filename(),
|
||||||
Line: n.Line(),
|
Line: n.Line(),
|
||||||
OtherFilename: other.Status.Filename(),
|
OtherFilename: other.Status.Filename(),
|
||||||
OtherLine: other.Status.Line(),
|
OtherLine: other.Status.Line(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.statuses[id] = Status{
|
c.statuses[qid] = Status{
|
||||||
ID: id,
|
QID: qid,
|
||||||
Status: n,
|
Status: n,
|
||||||
}
|
}
|
||||||
n.Node().SetTypeChecked()
|
n.Node().SetTypeChecked()
|
||||||
@@ -318,18 +320,18 @@ func (c *Checker) checkStatus(node *a.Node) error {
|
|||||||
|
|
||||||
func (c *Checker) checkConst(node *a.Node) error {
|
func (c *Checker) checkConst(node *a.Node) error {
|
||||||
n := node.Const()
|
n := node.Const()
|
||||||
id := n.Name()
|
qid := n.QID()
|
||||||
if other, ok := c.consts[id]; ok {
|
if other, ok := c.consts[qid]; ok {
|
||||||
return &Error{
|
return &Error{
|
||||||
Err: fmt.Errorf("check: duplicate const %q", id.Str(c.tm)),
|
Err: fmt.Errorf("check: duplicate const %s", qid.Str(c.tm)),
|
||||||
Filename: n.Filename(),
|
Filename: n.Filename(),
|
||||||
Line: n.Line(),
|
Line: n.Line(),
|
||||||
OtherFilename: other.Const.Filename(),
|
OtherFilename: other.Const.Filename(),
|
||||||
OtherLine: other.Const.Line(),
|
OtherLine: other.Const.Line(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.consts[id] = Const{
|
c.consts[qid] = Const{
|
||||||
ID: id,
|
QID: qid,
|
||||||
Const: n,
|
Const: n,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,10 +340,10 @@ func (c *Checker) checkConst(node *a.Node) error {
|
|||||||
tm: c.tm,
|
tm: c.tm,
|
||||||
}
|
}
|
||||||
if err := q.tcheckTypeExpr(n.XType(), 0); err != nil {
|
if err := q.tcheckTypeExpr(n.XType(), 0); err != nil {
|
||||||
return fmt.Errorf("%v in const %q", err, id.Str(c.tm))
|
return fmt.Errorf("%v in const %s", err, qid.Str(c.tm))
|
||||||
}
|
}
|
||||||
if err := q.tcheckExpr(n.Value(), 0); err != nil {
|
if err := q.tcheckExpr(n.Value(), 0); err != nil {
|
||||||
return fmt.Errorf("%v in const %q", err, id.Str(c.tm))
|
return fmt.Errorf("%v in const %s", err, qid.Str(c.tm))
|
||||||
}
|
}
|
||||||
|
|
||||||
nLists := 0
|
nLists := 0
|
||||||
@@ -354,17 +356,17 @@ func (c *Checker) checkConst(node *a.Node) error {
|
|||||||
typ = typ.Inner()
|
typ = typ.Inner()
|
||||||
}
|
}
|
||||||
if typ.Decorator() != 0 {
|
if typ.Decorator() != 0 {
|
||||||
return fmt.Errorf("check: invalid const type %q for %q", n.XType().Str(c.tm), id.Str(c.tm))
|
return fmt.Errorf("check: invalid const type %q for %s", n.XType().Str(c.tm), qid.Str(c.tm))
|
||||||
}
|
}
|
||||||
nMin, nMax, err := q.bcheckTypeExpr(typ)
|
nMin, nMax, err := q.bcheckTypeExpr(typ)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if nMin == nil || nMax == nil {
|
if nMin == nil || nMax == nil {
|
||||||
return fmt.Errorf("check: invalid const type %q for %q", n.XType().Str(c.tm), id.Str(c.tm))
|
return fmt.Errorf("check: invalid const type %q for %s", n.XType().Str(c.tm), qid.Str(c.tm))
|
||||||
}
|
}
|
||||||
if err := c.checkConstElement(n.Value(), nMin, nMax, nLists); err != nil {
|
if err := c.checkConstElement(n.Value(), nMin, nMax, nLists); err != nil {
|
||||||
return fmt.Errorf("check: %v for %q", err, id.Str(c.tm))
|
return fmt.Errorf("check: %v for %s", err, qid.Str(c.tm))
|
||||||
}
|
}
|
||||||
n.Node().SetTypeChecked()
|
n.Node().SetTypeChecked()
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+4
-3
@@ -394,14 +394,15 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if c, ok := q.c.consts[id1]; ok {
|
if c, ok := q.c.consts[t.QID{0, id1}]; ok {
|
||||||
// TODO: check somewhere that a global ident (i.e. a const) is
|
// TODO: check somewhere that a global ident (i.e. a const) is
|
||||||
// not directly in the LHS of an assignment.
|
// not directly in the LHS of an assignment.
|
||||||
n.SetGlobalIdent()
|
n.SetGlobalIdent()
|
||||||
n.SetMType(c.Const.XType())
|
n.SetMType(c.Const.XType())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// TODO: look for other (global) names: funcs, structs.
|
// TODO: look for other (global) names: consts, funcs, statuses,
|
||||||
|
// structs from used packages.
|
||||||
return fmt.Errorf("check: unrecognized identifier %q", id1.Str(q.tm))
|
return fmt.Errorf("check: unrecognized identifier %q", id1.Str(q.tm))
|
||||||
}
|
}
|
||||||
switch id1.Key() {
|
switch id1.Key() {
|
||||||
@@ -522,7 +523,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
|||||||
case t.KeyError, t.KeyStatus, t.KeySuspension:
|
case t.KeyError, t.KeyStatus, t.KeySuspension:
|
||||||
nominalKeyword := n.ID0()
|
nominalKeyword := n.ID0()
|
||||||
declaredKeyword := t.ID(0)
|
declaredKeyword := t.ID(0)
|
||||||
if s, ok := q.c.statuses[n.ID1()]; ok {
|
if s, ok := q.c.statuses[t.QID{0, n.ID1()}]; ok {
|
||||||
declaredKeyword = s.Status.Keyword()
|
declaredKeyword = s.Status.Keyword()
|
||||||
} else {
|
} else {
|
||||||
msg, _ := t.Unescape(n.ID1().Str(q.tm))
|
msg, _ := t.Unescape(n.ID1().Str(q.tm))
|
||||||
|
|||||||
Reference in new issue
Block a user