Raise built-in ident capacity from 96 to 768

This commit is contained in:
Nigel Tao committed 2018-05-05 09:23:10 +10:00
1 parent 1702c1e303
commit e8eeee371a
8 files changed
+129 -105

No files matched your search

+20 -9
View File
@@ -432,7 +432,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error { func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
op := n.Operator() op := n.Operator()
opName := cOpNames[0xFF&op] opName := cOpName(op)
if opName == "" { if opName == "" {
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm)) return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
} }
@@ -462,7 +462,7 @@ func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp p
return g.writeExprAs(b, n.LHS().Expr(), n.RHS().TypeExpr(), rp, depth) return g.writeExprAs(b, n.LHS().Expr(), n.RHS().TypeExpr(), rp, depth)
default: default:
opName = cOpNames[0xFF&op] opName = cOpName(op)
if opName == "" { if opName == "" {
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm)) return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
} }
@@ -501,7 +501,7 @@ func (g *gen) writeExprAs(b *buffer, lhs *a.Expr, rhs *a.TypeExpr, rp replacemen
func (g *gen) writeExprAssociativeOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error { func (g *gen) writeExprAssociativeOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
op := n.Operator() op := n.Operator()
opName := cOpNames[0xFF&op] opName := cOpName(op)
if opName == "" { if opName == "" {
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm)) return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
} }
@@ -628,7 +628,18 @@ var cTypeNames = [...]string{
t.IDIOWriter: "wuffs_base__io_writer", t.IDIOWriter: "wuffs_base__io_writer",
} }
var cOpNames = [256]string{ const noSuchCOperator = " no_such_C_operator "
func cOpName(x t.ID) string {
if x < t.ID(len(cOpNames)) {
if s := cOpNames[x]; s != "" {
return s
}
}
return noSuchCOperator
}
var cOpNames = [...]string{
t.IDEq: " = ", t.IDEq: " = ",
t.IDPlusEq: " += ", t.IDPlusEq: " += ",
t.IDMinusEq: " -= ", t.IDMinusEq: " -= ",
@@ -642,8 +653,8 @@ var cOpNames = [256]string{
t.IDPercentEq: " %= ", t.IDPercentEq: " %= ",
t.IDTildeModPlusEq: " += ", t.IDTildeModPlusEq: " += ",
t.IDTildeModMinusEq: " -= ", t.IDTildeModMinusEq: " -= ",
t.IDTildeSatPlusEq: " no_such_C_operator ", t.IDTildeSatPlusEq: noSuchCOperator,
t.IDTildeSatMinusEq: " no_such_C_operator ", t.IDTildeSatMinusEq: noSuchCOperator,
t.IDXUnaryPlus: " + ", t.IDXUnaryPlus: " + ",
t.IDXUnaryMinus: " - ", t.IDXUnaryMinus: " - ",
@@ -663,8 +674,8 @@ var cOpNames = [256]string{
t.IDXBinaryPercent: " % ", t.IDXBinaryPercent: " % ",
t.IDXBinaryTildeModPlus: " + ", t.IDXBinaryTildeModPlus: " + ",
t.IDXBinaryTildeModMinus: " - ", t.IDXBinaryTildeModMinus: " - ",
t.IDXBinaryTildeSatPlus: " no_such_C_operator ", t.IDXBinaryTildeSatPlus: noSuchCOperator,
t.IDXBinaryTildeSatMinus: " no_such_C_operator ", t.IDXBinaryTildeSatMinus: noSuchCOperator,
t.IDXBinaryNotEq: " != ", t.IDXBinaryNotEq: " != ",
t.IDXBinaryLessThan: " < ", t.IDXBinaryLessThan: " < ",
t.IDXBinaryLessEq: " <= ", t.IDXBinaryLessEq: " <= ",
@@ -673,7 +684,7 @@ var cOpNames = [256]string{
t.IDXBinaryGreaterThan: " > ", t.IDXBinaryGreaterThan: " > ",
t.IDXBinaryAnd: " && ", t.IDXBinaryAnd: " && ",
t.IDXBinaryOr: " || ", t.IDXBinaryOr: " || ",
t.IDXBinaryAs: " no_such_C_operator ", t.IDXBinaryAs: noSuchCOperator,
t.IDXAssociativePlus: " + ", t.IDXAssociativePlus: " + ",
t.IDXAssociativeStar: " * ", t.IDXAssociativeStar: " * ",
+1 -1
View File
@@ -402,7 +402,7 @@ func (g *gen) writeFuncImplArgChecks(b *buffer, n *a.Func) error {
return nil return nil
} }
var numTypeBounds = [256][2]*big.Int{ var numTypeBounds = [...][2]*big.Int{
t.IDI8: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)}, t.IDI8: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)},
t.IDI16: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)}, t.IDI16: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)},
t.IDI32: {big.NewInt(-1 << 31), big.NewInt(1<<31 - 1)}, t.IDI32: {big.NewInt(-1 << 31), big.NewInt(1<<31 - 1)},
+1 -1
View File
@@ -116,7 +116,7 @@ func (g *gen) writeStatementAssign(b *buffer, n *a.Assign, depth uint32) error {
opName, tilde = ",", true opName, tilde = ",", true
default: default:
opName = cOpNames[0xFF&op] opName = cOpName(op)
if opName == "" { if opName == "" {
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm)) return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
} }
+13 -4
View File
@@ -42,7 +42,7 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32)
if n.id0.IsXOp() { if n.id0.IsXOp() {
switch { switch {
case n.id0.IsXUnaryOp(): case n.id0.IsXUnaryOp():
buf = append(buf, opStrings[0xFF&n.id0]...) buf = append(buf, opString(n.id0)...)
buf = n.rhs.Expr().appendStr(buf, tm, true, depth) buf = n.rhs.Expr().appendStr(buf, tm, true, depth)
case n.id0.IsXBinaryOp(): case n.id0.IsXBinaryOp():
@@ -50,7 +50,7 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32)
buf = append(buf, '(') buf = append(buf, '(')
} }
buf = n.lhs.Expr().appendStr(buf, tm, true, depth) buf = n.lhs.Expr().appendStr(buf, tm, true, depth)
buf = append(buf, opStrings[0xFF&n.id0]...) buf = append(buf, opString(n.id0)...)
if n.id0 == t.IDXBinaryAs { if n.id0 == t.IDXBinaryAs {
buf = append(buf, n.rhs.TypeExpr().Str(tm)...) buf = append(buf, n.rhs.TypeExpr().Str(tm)...)
} else { } else {
@@ -64,7 +64,7 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32)
if parenthesize { if parenthesize {
buf = append(buf, '(') buf = append(buf, '(')
} }
op := opStrings[0xFF&n.id0] op := opString(n.id0)
for i, o := range n.list0 { for i, o := range n.list0 {
if i != 0 { if i != 0 {
buf = append(buf, op...) buf = append(buf, op...)
@@ -152,7 +152,16 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32)
return buf return buf
} }
var opStrings = [256]string{ func opString(x t.ID) string {
if x < t.ID(len(opStrings)) {
if s := opStrings[x]; s != "" {
return s
}
}
return " no_such_Wuffs_operator "
}
var opStrings = [...]string{
t.IDXUnaryPlus: "+", t.IDXUnaryPlus: "+",
t.IDXUnaryMinus: "-", t.IDXUnaryMinus: "-",
t.IDXUnaryNot: "not ", t.IDXUnaryNot: "not ",
+1 -1
View File
@@ -22,7 +22,7 @@ import (
t "github.com/google/wuffs/lang/token" t "github.com/google/wuffs/lang/token"
) )
var numTypeBounds = [256][2]*big.Int{ var numTypeBounds = [...][2]*big.Int{
t.IDI8: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)}, t.IDI8: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)},
t.IDI16: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)}, t.IDI16: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)},
t.IDI32: {big.NewInt(-1 << 31), big.NewInt(1<<31 - 1)}, t.IDI32: {big.NewInt(-1 << 31), big.NewInt(1<<31 - 1)},
+1 -1
View File
@@ -278,7 +278,7 @@ func (q *checker) optimizeIOMethodAdvance(n *a.Expr, receiver *a.Expr, advance *
}) })
} }
var ioMethodAdvances = [256]*big.Int{ var ioMethodAdvances = [...]*big.Int{
t.IDReadU8: one, t.IDReadU8: one,
t.IDReadU16BE: two, t.IDReadU16BE: two,
t.IDReadU16LE: two, t.IDReadU16LE: two,
+2 -2
View File
@@ -846,7 +846,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
n.SetConstValue(ncv) n.SetConstValue(ncv)
} }
if comparisonOps[0xFF&op] { if (op < t.ID(len(comparisonOps))) && comparisonOps[op] {
n.SetMType(typeExprBool) n.SetMType(typeExprBool)
} else if !lTyp.IsIdeal() { } else if !lTyp.IsIdeal() {
n.SetMType(lTyp.Unrefined()) n.SetMType(lTyp.Unrefined())
@@ -1036,7 +1036,7 @@ swtch:
return nil return nil
} }
var comparisonOps = [256]bool{ var comparisonOps = [...]bool{
t.IDXBinaryNotEq: true, t.IDXBinaryNotEq: true,
t.IDXBinaryLessThan: true, t.IDXBinaryLessThan: true,
t.IDXBinaryLessEq: true, t.IDXBinaryLessEq: true,
+90 -86
View File
@@ -30,28 +30,28 @@ type ID uint32
func (x ID) Str(m *Map) string { return m.ByID(x) } func (x ID) Str(m *Map) string { return m.ByID(x) }
func (x ID) AmbiguousForm() ID { func (x ID) AmbiguousForm() ID {
if x >= nBuiltInIDs { if x >= ID(len(ambiguousForms)) {
return 0 return 0
} }
return ambiguousForms[x] return ambiguousForms[x]
} }
func (x ID) UnaryForm() ID { func (x ID) UnaryForm() ID {
if x >= nBuiltInIDs { if x >= ID(len(unaryForms)) {
return 0 return 0
} }
return unaryForms[x] return unaryForms[x]
} }
func (x ID) BinaryForm() ID { func (x ID) BinaryForm() ID {
if x >= nBuiltInIDs { if x >= ID(len(binaryForms)) {
return 0 return 0
} }
return binaryForms[x] return binaryForms[x]
} }
func (x ID) AssociativeForm() ID { func (x ID) AssociativeForm() ID {
if x >= nBuiltInIDs { if x >= ID(len(associativeForms)) {
return 0 return 0
} }
return associativeForms[x] return associativeForms[x]
@@ -180,20 +180,24 @@ type Token struct {
// nBuiltInIDs is the number of built-in IDs. The packing is: // nBuiltInIDs is the number of built-in IDs. The packing is:
// - Zero is invalid. // - Zero is invalid.
// - [0x01, 0x0F] are squiggly punctuation, such as "(", ")" and ";". // - [ 0x01, 0x0F] are squiggly punctuation, such as "(", ")" and ";".
// - [0x10, 0x1F] are squiggly assignments, such as "=" and "+=". // - [ 0x10, 0x1F] are squiggly assignments, such as "=" and "+=".
// - [0x20, 0x3F] are operators, such as "+", "==" and "not". // - [ 0x20, 0x3F] are operators, such as "+", "==" and "not".
// - [0x40, 0x6F] are x-ops (disambiguation forms), e.g. unary vs binary "+". // - [ 0x40, 0x6F] are x-ops (disambiguation forms): unary vs binary "+".
// - [0x70, 0x8F] are keywords, such as "if" and "return". // - [ 0x70, 0x8F] are keywords, such as "if" and "return".
// - [0x90, 0x93] are type modifiers, such as "ptr" and "slice". // - [ 0x90, 0x93] are type modifiers, such as "ptr" and "slice".
// - [0x94, 0x9F] are literals, such as "false" and "true". // - [ 0x94, 0x9F] are literals, such as "false" and "true".
// - [0xA0, 0xFF] are identifiers, such as "bool", "u32" and "read_u8". // - [ 0xA0, 0xFF] are reserved.
// - [0x100, 0x3FF] are identifiers, such as "bool", "u32" and "read_u8".
// //
// "Squiggly" means a sequence of non-alpha-numeric characters, such as "+" and // "Squiggly" means a sequence of non-alpha-numeric characters, such as "+" and
// "&=". Roughly speaking, their IDs range in [0x01, 0x4F], or disambiguation // "&=". Roughly speaking, their IDs range in [0x01, 0x3F], or disambiguation
// forms range in [0xD0, 0xFF], but vice versa does not necessarily hold. For // forms range in [0x40, 0x6F], but vice versa does not necessarily hold. For
// example, the "and" operator is not "squiggly" but it is within [0x01, 0x4F]. // example, the "and" operator is not "squiggly" but it is within [0x01, 0x3F].
const nBuiltInIDs = ID(256) const (
nBuiltInSymbolicIDs = ID(0x70) // 112
nBuiltInIDs = ID(0x400) // 1024
)
const ( const (
IDInvalid = ID(0) IDInvalid = ID(0)
@@ -378,77 +382,77 @@ const (
) )
const ( const (
minBuiltInIdent = 0xA0 minBuiltInIdent = 0x100
minNumTypeOrIdeal = 0xA0 minNumTypeOrIdeal = 0x100
minNumType = 0xA0 minNumType = 0x100
maxNumType = 0xA7 maxNumType = 0x107
maxNumTypeOrIdeal = 0xA8 maxNumTypeOrIdeal = 0x108
maxBuiltInIdent = 0xFF maxBuiltInIdent = 0x3FF
IDI8 = ID(0xA0) IDI8 = ID(0x100)
IDI16 = ID(0xA1) IDI16 = ID(0x101)
IDI32 = ID(0xA2) IDI32 = ID(0x102)
IDI64 = ID(0xA3) IDI64 = ID(0x103)
IDU8 = ID(0xA4) IDU8 = ID(0x104)
IDU16 = ID(0xA5) IDU16 = ID(0x105)
IDU32 = ID(0xA6) IDU32 = ID(0x106)
IDU64 = ID(0xA7) IDU64 = ID(0x107)
// It is important that IDDoubleZ is right next to the IDI8..IDU64 block. // It is important that IDDoubleZ is right next to the IDI8..IDU64 block.
// See the ID.IsNumTypeOrIdeal method. // See the ID.IsNumTypeOrIdeal method.
IDDoubleZ = ID(0xA8) IDDoubleZ = ID(0x108)
IDDiamond = ID(0xA9) IDDiamond = ID(0x110)
IDUnderscore = ID(0xAA) IDUnderscore = ID(0x120)
IDThis = ID(0xAB) IDThis = ID(0x121)
IDIn = ID(0xAC) IDIn = ID(0x122)
IDOut = ID(0xAD) IDOut = ID(0x123)
IDCapitalT = ID(0xAE) IDCapitalT = ID(0x124)
IDBase = ID(0xAF) IDBase = ID(0x125)
IDMark = ID(0xB0) IDMark = ID(0x180)
IDReadU8 = ID(0xB1) IDReadU8 = ID(0x181)
IDReadU16BE = ID(0xB2) IDReadU16BE = ID(0x182)
IDReadU16LE = ID(0xB3) IDReadU16LE = ID(0x183)
IDReadU32BE = ID(0xB4) IDReadU32BE = ID(0x184)
IDReadU32LE = ID(0xB5) IDReadU32LE = ID(0x185)
IDReadU64BE = ID(0xB6) IDReadU64BE = ID(0x186)
IDReadU64LE = ID(0xB7) IDReadU64LE = ID(0x187)
IDSinceMark = ID(0xB8) IDSinceMark = ID(0x188)
IDWriteU8 = ID(0xB9) IDWriteU8 = ID(0x189)
IDWriteU16BE = ID(0xBA) IDWriteU16BE = ID(0x18A)
IDWriteU16LE = ID(0xBB) IDWriteU16LE = ID(0x18B)
IDWriteU32BE = ID(0xBC) IDWriteU32BE = ID(0x18C)
IDWriteU32LE = ID(0xBD) IDWriteU32LE = ID(0x18D)
IDWriteU64BE = ID(0xBE) IDWriteU64BE = ID(0x18E)
IDWriteU64LE = ID(0xBF) IDWriteU64LE = ID(0x18F)
IDIsError = ID(0xC0) IDIsError = ID(0x200)
IDIsOK = ID(0xC1) IDIsOK = ID(0x201)
IDIsSuspension = ID(0xC2) IDIsSuspension = ID(0x202)
IDCopyFromHistory32 = ID(0xC3) IDCopyFromHistory32 = ID(0x203)
IDCopyFromReader32 = ID(0xC4) IDCopyFromReader32 = ID(0x204)
IDCopyFromSlice = ID(0xC5) IDCopyFromSlice = ID(0x205)
IDCopyFromSlice32 = ID(0xC6) IDCopyFromSlice32 = ID(0x206)
IDSkip32 = ID(0xC7) IDSkip32 = ID(0x207)
IDSkip64 = ID(0xC8) IDSkip64 = ID(0x208)
IDLength = ID(0xC9) IDLength = ID(0x209)
IDAvailable = ID(0xCA) IDAvailable = ID(0x20A)
IDPrefix = ID(0xCB) IDPrefix = ID(0x20B)
IDSuffix = ID(0xCC) IDSuffix = ID(0x20C)
IDSetLimit = ID(0xCD) IDSetLimit = ID(0x20D)
IDLowBits = ID(0xCE) IDLowBits = ID(0x20E)
IDHighBits = ID(0xCF) IDHighBits = ID(0x20F)
IDUnreadU8 = ID(0xD0) IDUnreadU8 = ID(0x210)
IDUnroll = ID(0xD1) IDUnroll = ID(0x211)
IDEmptyStruct = ID(0xF8) IDEmptyStruct = ID(0x308)
IDBool = ID(0xF9) IDBool = ID(0x309)
IDStatus = ID(0xFA) IDStatus = ID(0x30A)
IDIOReader = ID(0xFB) IDIOReader = ID(0x30B)
IDIOWriter = ID(0xFC) IDIOWriter = ID(0x30C)
IDImageConfig = ID(0xFD) IDImageConfig = ID(0x30D)
) )
var builtInsByID = [nBuiltInIDs]string{ var builtInsByID = [nBuiltInIDs]string{
@@ -645,7 +649,7 @@ func init() {
} }
// squiggles are built-in IDs that aren't alpha-numeric. // squiggles are built-in IDs that aren't alpha-numeric.
var squiggles = [nBuiltInIDs]ID{ var squiggles = [256]ID{
'(': IDOpenParen, '(': IDOpenParen,
')': IDCloseParen, ')': IDCloseParen,
'[': IDOpenBracket, '[': IDOpenBracket,
@@ -670,7 +674,7 @@ type suffixLexer struct {
// //
// The order of the []suffixLexer elements matters. The first match wins. Since // The order of the []suffixLexer elements matters. The first match wins. Since
// we want to lex greedily, longer suffixes should be earlier in the slice. // we want to lex greedily, longer suffixes should be earlier in the slice.
var lexers = [nBuiltInIDs][]suffixLexer{ var lexers = [256][]suffixLexer{
'.': { '.': {
{".", IDDotDot}, {".", IDDotDot},
{"", IDDot}, {"", IDDot},
@@ -739,7 +743,7 @@ var lexers = [nBuiltInIDs][]suffixLexer{
}, },
} }
var ambiguousForms = [nBuiltInIDs]ID{ var ambiguousForms = [nBuiltInSymbolicIDs]ID{
IDXUnaryPlus: IDPlus, IDXUnaryPlus: IDPlus,
IDXUnaryMinus: IDMinus, IDXUnaryMinus: IDMinus,
IDXUnaryNot: IDNot, IDXUnaryNot: IDNot,
@@ -791,8 +795,8 @@ func init() {
// IDPlus: IDXUnaryPlus, // IDPlus: IDXUnaryPlus,
// and this function implicitly addes entries like: // and this function implicitly addes entries like:
// IDXUnaryPlus: IDXUnaryPlus, // IDXUnaryPlus: IDXUnaryPlus,
func addXForms(table *[nBuiltInIDs]ID) { func addXForms(table *[nBuiltInSymbolicIDs]ID) {
implicitEntries := [nBuiltInIDs]bool{} implicitEntries := [nBuiltInSymbolicIDs]bool{}
for _, y := range table { for _, y := range table {
if y != 0 { if y != 0 {
implicitEntries[y] = true implicitEntries[y] = true
@@ -805,7 +809,7 @@ func addXForms(table *[nBuiltInIDs]ID) {
} }
} }
var unaryForms = [nBuiltInIDs]ID{ var unaryForms = [nBuiltInSymbolicIDs]ID{
IDPlus: IDXUnaryPlus, IDPlus: IDXUnaryPlus,
IDMinus: IDXUnaryMinus, IDMinus: IDXUnaryMinus,
IDNot: IDXUnaryNot, IDNot: IDXUnaryNot,
@@ -813,7 +817,7 @@ var unaryForms = [nBuiltInIDs]ID{
IDDeref: IDXUnaryDeref, IDDeref: IDXUnaryDeref,
} }
var binaryForms = [nBuiltInIDs]ID{ var binaryForms = [nBuiltInSymbolicIDs]ID{
IDPlusEq: IDXBinaryPlus, IDPlusEq: IDXBinaryPlus,
IDMinusEq: IDXBinaryMinus, IDMinusEq: IDXBinaryMinus,
IDStarEq: IDXBinaryStar, IDStarEq: IDXBinaryStar,
@@ -855,7 +859,7 @@ var binaryForms = [nBuiltInIDs]ID{
IDAs: IDXBinaryAs, IDAs: IDXBinaryAs,
} }
var associativeForms = [nBuiltInIDs]ID{ var associativeForms = [nBuiltInSymbolicIDs]ID{
IDPlus: IDXAssociativePlus, IDPlus: IDXAssociativePlus,
IDStar: IDXAssociativeStar, IDStar: IDXAssociativeStar,
IDAmp: IDXAssociativeAmp, IDAmp: IDXAssociativeAmp,