Allow scalar constants in type expressions

This commit is contained in:
Nigel Tao committed 2019-02-02 07:53:59 +11:00
1 parent 278200f560
commit 8bbdcc4ce1
7 files changed
+58 -18

No files matched your search

+17 -4
View File
@@ -318,10 +318,11 @@ type gen struct {
// generated C code (due to line numbers changing) when editing Wuffs code.
genlinenum bool
statusList []status
statusMap map[t.QID]status
structList []*a.Struct
structMap map[t.QID]*a.Struct
scalarConstsMap map[t.QID]*a.Const
statusList []status
statusMap map[t.QID]status
structList []*a.Struct
structMap map[t.QID]*a.Struct
currFunk funk
funks map[t.QQID]funk
@@ -350,6 +351,11 @@ func (g *gen) generate() ([]byte, error) {
}
}
g.scalarConstsMap = map[t.QID]*a.Const{}
if err := g.forEachConst(b, bothPubPri, (*gen).gatherScalarConsts); err != nil {
return nil, err
}
// Make a topologically sorted list of structs.
unsortedStructs := []*a.Struct(nil)
for _, file := range g.files {
@@ -667,6 +673,13 @@ func (g *gen) addStatus(qid t.QID, msg string, public bool) error {
return nil
}
func (g *gen) gatherScalarConsts(b *buffer, n *a.Const) error {
if cv := n.Value().ConstValue(); cv != nil {
g.scalarConstsMap[n.QID()] = n
}
return nil
}
func (g *gen) writeConst(b *buffer, n *a.Const) error {
if n.Public() {
b.writes("WUFFS_BASE__MAYBE_STATIC ")
+3
View File
@@ -75,6 +75,9 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, depth uint32) error {
return fmt.Errorf("unrecognized status %s", n.StatusQID().Str(g.tm))
}
} else if c, ok := g.scalarConstsMap[t.QID{0, n.Ident()}]; ok {
b.writes(c.Value().ConstValue().String())
} else {
if n.GlobalIdent() {
b.writes(g.pkgPrefix)