Simplify token.builtInsByID

This commit is contained in:
Nigel Tao committed 2018-04-15 18:15:56 +10:00
1 parent e2716e4365
commit c9161bafaf
2 files changed
+142 -146

No files matched your search

+141 -145
View File
@@ -381,111 +381,107 @@ const (
IDIsMarked = ID(0xD1) IDIsMarked = ID(0xD1)
) )
// TODO: the struct{string, ID} can now just be a string. var builtInsByID = [nBuiltInIDs]string{
var builtInsByID = [nBuiltInIDs]struct { IDOpenParen: "(",
name string IDCloseParen: ")",
id ID IDOpenBracket: "[",
}{ IDCloseBracket: "]",
IDOpenParen: {"(", IDOpenParen}, IDOpenCurly: "{",
IDCloseParen: {")", IDCloseParen}, IDCloseCurly: "}",
IDOpenBracket: {"[", IDOpenBracket},
IDCloseBracket: {"]", IDCloseBracket},
IDOpenCurly: {"{", IDOpenCurly},
IDCloseCurly: {"}", IDCloseCurly},
IDDot: {".", IDDot}, IDDot: ".",
IDDotDot: {"..", IDDotDot}, IDDotDot: "..",
IDComma: {",", IDComma}, IDComma: ",",
IDExclam: {"!", IDExclam}, IDExclam: "!",
IDQuestion: {"?", IDQuestion}, IDQuestion: "?",
IDColon: {":", IDColon}, IDColon: ":",
IDSemicolon: {";", IDSemicolon}, IDSemicolon: ";",
IDDollar: {"$", IDDollar}, IDDollar: "$",
IDEq: {"=", IDEq}, IDEq: "=",
IDPlusEq: {"+=", IDPlusEq}, IDPlusEq: "+=",
IDMinusEq: {"-=", IDMinusEq}, IDMinusEq: "-=",
IDStarEq: {"*=", IDStarEq}, IDStarEq: "*=",
IDSlashEq: {"/=", IDSlashEq}, IDSlashEq: "/=",
IDShiftLEq: {"<<=", IDShiftLEq}, IDShiftLEq: "<<=",
IDShiftREq: {">>=", IDShiftREq}, IDShiftREq: ">>=",
IDAmpEq: {"&=", IDAmpEq}, IDAmpEq: "&=",
IDAmpHatEq: {"&^=", IDAmpHatEq}, IDAmpHatEq: "&^=",
IDPipeEq: {"|=", IDPipeEq}, IDPipeEq: "|=",
IDHatEq: {"^=", IDHatEq}, IDHatEq: "^=",
IDPercentEq: {"%=", IDPercentEq}, IDPercentEq: "%=",
IDTildePlusEq: {"~+=", IDTildePlusEq}, IDTildePlusEq: "~+=",
IDPlus: {"+", IDPlus}, IDPlus: "+",
IDMinus: {"-", IDMinus}, IDMinus: "-",
IDStar: {"*", IDStar}, IDStar: "*",
IDSlash: {"/", IDSlash}, IDSlash: "/",
IDShiftL: {"<<", IDShiftL}, IDShiftL: "<<",
IDShiftR: {">>", IDShiftR}, IDShiftR: ">>",
IDAmp: {"&", IDAmp}, IDAmp: "&",
IDAmpHat: {"&^", IDAmpHat}, IDAmpHat: "&^",
IDPipe: {"|", IDPipe}, IDPipe: "|",
IDHat: {"^", IDHat}, IDHat: "^",
IDPercent: {"%", IDPercent}, IDPercent: "%",
IDTildePlus: {"~+", IDPercent}, IDTildePlus: "~+",
IDNotEq: {"!=", IDNotEq}, IDNotEq: "!=",
IDLessThan: {"<", IDLessThan}, IDLessThan: "<",
IDLessEq: {"<=", IDLessEq}, IDLessEq: "<=",
IDEqEq: {"==", IDEqEq}, IDEqEq: "==",
IDGreaterEq: {">=", IDGreaterEq}, IDGreaterEq: ">=",
IDGreaterThan: {">", IDGreaterThan}, IDGreaterThan: ">",
IDAnd: {"and", IDAnd}, IDAnd: "and",
IDOr: {"or", IDOr}, IDOr: "or",
IDNot: {"not", IDNot}, IDNot: "not",
IDAs: {"as", IDAs}, IDAs: "as",
IDRef: {"ref", IDRef}, IDRef: "ref",
IDDeref: {"deref", IDDeref}, IDDeref: "deref",
IDFunc: {"func", IDFunc}, IDFunc: "func",
IDAssert: {"assert", IDAssert}, IDAssert: "assert",
IDWhile: {"while", IDWhile}, IDWhile: "while",
IDIf: {"if", IDIf}, IDIf: "if",
IDElse: {"else", IDElse}, IDElse: "else",
IDReturn: {"return", IDReturn}, IDReturn: "return",
IDBreak: {"break", IDBreak}, IDBreak: "break",
IDContinue: {"continue", IDContinue}, IDContinue: "continue",
IDStruct: {"struct", IDStruct}, IDStruct: "struct",
IDUse: {"use", IDUse}, IDUse: "use",
IDVar: {"var", IDVar}, IDVar: "var",
IDPre: {"pre", IDPre}, IDPre: "pre",
IDInv: {"inv", IDInv}, IDInv: "inv",
IDPost: {"post", IDPost}, IDPost: "post",
IDVia: {"via", IDVia}, IDVia: "via",
IDPub: {"pub", IDPub}, IDPub: "pub",
IDPri: {"pri", IDPri}, IDPri: "pri",
IDError: {"error", IDError}, IDError: "error",
IDSuspension: {"suspension", IDSuspension}, IDSuspension: "suspension",
IDPackageID: {"packageid", IDPackageID}, IDPackageID: "packageid",
IDConst: {"const", IDConst}, IDConst: "const",
IDTry: {"try", IDTry}, IDTry: "try",
IDIterate: {"iterate", IDIterate}, IDIterate: "iterate",
IDYield: {"yield", IDYield}, IDYield: "yield",
IDArray: {"array", IDArray}, IDArray: "array",
IDNptr: {"nptr", IDNptr}, IDNptr: "nptr",
IDPtr: {"ptr", IDPtr}, IDPtr: "ptr",
IDSlice: {"slice", IDSlice}, IDSlice: "slice",
IDFalse: {"false", IDFalse}, IDFalse: "false",
IDTrue: {"true", IDTrue}, IDTrue: "true",
IDZero: {"0", IDZero}, IDZero: "0",
// Change MaxIntBits if a future update adds an i128 or u128 type. // Change MaxIntBits if a future update adds an i128 or u128 type.
IDI8: {"i8", IDI8}, IDI8: "i8",
IDI16: {"i16", IDI16}, IDI16: "i16",
IDI32: {"i32", IDI32}, IDI32: "i32",
IDI64: {"i64", IDI64}, IDI64: "i64",
IDU8: {"u8", IDU8}, IDU8: "u8",
IDU16: {"u16", IDU16}, IDU16: "u16",
IDU32: {"u32", IDU32}, IDU32: "u32",
IDU64: {"u64", IDU64}, IDU64: "u64",
// IDDoubleZ and IDDiamond are never returned by the tokenizer, as the // IDDoubleZ and IDDiamond are never returned by the tokenizer, as the
// tokenizer rejects non-ASCII input. // tokenizer rejects non-ASCII input.
@@ -497,68 +493,68 @@ var builtInsByID = [nBuiltInIDs]struct {
// represent an ideal integer type (in mathematical terms, the integer ring // represent an ideal integer type (in mathematical terms, the integer ring
// ℤ), as opposed to a realized integer type whose range is restricted. For // ℤ), as opposed to a realized integer type whose range is restricted. For
// example, the base.u16 type is restricted to [0x0000, 0xFFFF]. // example, the base.u16 type is restricted to [0x0000, 0xFFFF].
IDDoubleZ: {"ℤ", IDDoubleZ}, // U+2124 DOUBLE-STRUCK CAPITAL Z IDDoubleZ: "ℤ", // U+2124 DOUBLE-STRUCK CAPITAL Z
// IDDiamond is used by the type checker as a dummy-valued built-in ID to // IDDiamond is used by the type checker as a dummy-valued built-in ID to
// represent a generic type. // represent a generic type.
IDDiamond: {"◊", IDDiamond}, // U+25C7 WHITE DIAMOND IDDiamond: "◊", // U+25C7 WHITE DIAMOND
IDUnderscore: {"_", IDUnderscore}, IDUnderscore: "_",
IDThis: {"this", IDThis}, IDThis: "this",
IDIn: {"in", IDIn}, IDIn: "in",
IDOut: {"out", IDOut}, IDOut: "out",
IDSLICE: {"SLICE", IDSLICE}, IDSLICE: "SLICE",
IDBase: {"base", IDBase}, IDBase: "base",
IDBool: {"bool", IDBool}, IDBool: "bool",
IDIOReader: {"io_reader", IDIOReader}, IDIOReader: "io_reader",
IDIOWriter: {"io_writer", IDIOWriter}, IDIOWriter: "io_writer",
IDStatus: {"status", IDStatus}, IDStatus: "status",
IDImageConfig: {"image_config", IDImageConfig}, IDImageConfig: "image_config",
IDMark: {"mark", IDMark}, IDMark: "mark",
IDReadU8: {"read_u8", IDReadU8}, IDReadU8: "read_u8",
IDReadU16BE: {"read_u16be", IDReadU16BE}, IDReadU16BE: "read_u16be",
IDReadU16LE: {"read_u16le", IDReadU16LE}, IDReadU16LE: "read_u16le",
IDReadU32BE: {"read_u32be", IDReadU32BE}, IDReadU32BE: "read_u32be",
IDReadU32LE: {"read_u32le", IDReadU32LE}, IDReadU32LE: "read_u32le",
IDReadU64BE: {"read_u64be", IDReadU64BE}, IDReadU64BE: "read_u64be",
IDReadU64LE: {"read_u64le", IDReadU64LE}, IDReadU64LE: "read_u64le",
IDSinceMark: {"since_mark", IDSinceMark}, IDSinceMark: "since_mark",
IDWriteU8: {"write_u8", IDWriteU8}, IDWriteU8: "write_u8",
IDWriteU16BE: {"write_u16be", IDWriteU16BE}, IDWriteU16BE: "write_u16be",
IDWriteU16LE: {"write_u16le", IDWriteU16LE}, IDWriteU16LE: "write_u16le",
IDWriteU32BE: {"write_u32be", IDWriteU32BE}, IDWriteU32BE: "write_u32be",
IDWriteU32LE: {"write_u32le", IDWriteU32LE}, IDWriteU32LE: "write_u32le",
IDWriteU64BE: {"write_u64be", IDWriteU64BE}, IDWriteU64BE: "write_u64be",
IDWriteU64LE: {"write_u64le", IDWriteU64LE}, IDWriteU64LE: "write_u64le",
IDIsError: {"is_error", IDIsError}, IDIsError: "is_error",
IDIsOK: {"is_ok", IDIsOK}, IDIsOK: "is_ok",
IDIsSuspension: {"is_suspension", IDIsSuspension}, IDIsSuspension: "is_suspension",
IDCopyFromHistory32: {"copy_from_history32", IDCopyFromHistory32}, IDCopyFromHistory32: "copy_from_history32",
IDCopyFromReader32: {"copy_from_reader32", IDCopyFromReader32}, IDCopyFromReader32: "copy_from_reader32",
IDCopyFromSlice: {"copy_from_slice", IDCopyFromSlice}, IDCopyFromSlice: "copy_from_slice",
IDCopyFromSlice32: {"copy_from_slice32", IDCopyFromSlice32}, IDCopyFromSlice32: "copy_from_slice32",
IDSkip32: {"skip32", IDSkip32}, IDSkip32: "skip32",
IDSkip64: {"skip64", IDSkip64}, IDSkip64: "skip64",
IDLength: {"length", IDLength}, IDLength: "length",
IDAvailable: {"available", IDAvailable}, IDAvailable: "available",
IDPrefix: {"prefix", IDPrefix}, IDPrefix: "prefix",
IDSuffix: {"suffix", IDSuffix}, IDSuffix: "suffix",
IDLimit: {"limit", IDLimit}, IDLimit: "limit",
IDLowBits: {"low_bits", IDLowBits}, IDLowBits: "low_bits",
IDHighBits: {"high_bits", IDHighBits}, IDHighBits: "high_bits",
IDUnreadU8: {"unread_u8", IDUnreadU8}, IDUnreadU8: "unread_u8",
IDIsMarked: {"is_marked", IDIsMarked}, IDIsMarked: "is_marked",
} }
var builtInsByName = map[string]ID{} var builtInsByName = map[string]ID{}
func init() { func init() {
for _, x := range builtInsByID { for i, name := range builtInsByID {
if x.name != "" { if name != "" {
builtInsByName[x.name] = x.id builtInsByName[name] = ID(i)
} }
} }
} }
+1 -1
View File
@@ -72,7 +72,7 @@ func (m *Map) ByName(name string) ID {
func (m *Map) ByID(x ID) string { func (m *Map) ByID(x ID) string {
if x < nBuiltInIDs { if x < nBuiltInIDs {
return builtInsByID[x].name return builtInsByID[x]
} }
x -= nBuiltInIDs x -= nBuiltInIDs
if uint(x) < uint(len(m.byID)) { if uint(x) < uint(len(m.byID)) {