1 /*
2 This file is part of GNU APL, a free implementation of the
3 ISO/IEC Standard 13751, "Programming Language APL, Extended"
4
5 Copyright (C) 2008-2015 Dr. Jürgen Sauermann
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef __ID_HH_DEFINED__
22 # error This file shall NOT be #included directly, but by #including Id.hh
23 #endif
24
25 #ifndef __TOKENENUMS_HH_DEFINED__
26 #define __TOKENENUMS_HH_DEFINED__
27
28 /**
29 The class tag of a token. Token classes < TC_MAX_PERM are permanent
30 (e.g. used in user defined functions) while higher classes are
31 temporary (used while parsing),
32 **/
33 enum TokenClass
34 {
35 // token classes.
36 //
37
38 // permanent token classes. Token of these classes can appear in
39 // the body of a defined function.
40 //
41 TC_ASSIGN = 0x01, ///< ←
42 TC_R_ARROW = 0x02, ///< →N
43 TC_L_BRACK = 0x03, ///< [ or ;
44 TC_R_BRACK = 0x04, ///< ]
45 TC_END = 0x05, ///< left end of statement
46 TC_FUN0 = 0x06, ///< niladic function
47 TC_FUN12 = 0x07, ///< ambivalent function
48 TC_INDEX = 0x08, ///< [...]
49 TC_OPER1 = 0x09, ///< monadic operator
50 TC_OPER2 = 0x0A, ///< dyadic operator
51 TC_L_PARENT = 0x0B, ///< (
52 TC_R_PARENT = 0x0C, ///< )
53 TC_RETURN = 0x0D, ///< return from defined function
54 TC_SYMBOL = 0x0E, ///< user defined name
55 TC_VALUE = 0x0F, ///< APL value
56
57 TC_MAX_PERM, ///< permanent token are < TC_MAX_PERM
58
59 TC_FUN1 = TC_FUN12, ///< monadic function
60 TC_FUN2 = TC_FUN12, ///< dyadic function
61
62 // temporary token classes. Token of these classes can appear as
63 // intermediate results during tokenization and execution
64 //
65 TC_PINDEX = 0x10, ///< partial index
66 TC_VOID = 0x11,
67 TC_MAX_PHRASE, ///< token in phrases are < TC_MAX_PHRASE
68 TC_MAX_PHRASE_2 = TC_MAX_PHRASE*TC_MAX_PHRASE, // TC_MAX_PHRASE ^ 2
69 TC_MAX_PHRASE_3 = TC_MAX_PHRASE*TC_MAX_PHRASE_2, // TC_MAX_PHRASE ^ 3
70 TC_MAX_PHRASE_4 = TC_MAX_PHRASE*TC_MAX_PHRASE_3, // TC_MAX_PHRASE ^ 4
71
72 TC_OFF = 0x12,
73 TC_SI_LEAVE = 0x13,
74 TC_LINE = 0x14,
75 TC_DIAMOND = 0x15, // ◊
76 TC_NUMERIC = 0x16, // 0-9, ¯
77 TC_SPACE = 0x17, // space, tab, CR (but not LF)
78 TC_NEWLINE = 0x18, // LF
79 TC_COLON = 0x19, // :
80 TC_QUOTE = 0x1A, // ' or "
81 TC_L_CURLY = 0x1B, // {
82 TC_R_CURLY = 0x1C, // }
83
84 TC_MASK = 0xFF,
85 TC_INVALID = 0xFF,
86
87 // short token class names for phrase table
88 //
89 SN_A = TC_VALUE,
90 SN_ASS = TC_ASSIGN,
91 SN_B = TC_VALUE,
92 SN_C = TC_INDEX,
93 SN_D = TC_OPER2,
94 SN_END = TC_END,
95 SN_F = TC_FUN12,
96 SN_G = TC_FUN12,
97 SN_GOTO = TC_R_ARROW,
98 SN_I = TC_PINDEX,
99 SN_LBRA = TC_L_BRACK,
100 SN_LPAR = TC_L_PARENT,
101 SN_M = TC_OPER1,
102 SN_N = TC_FUN0,
103 SN_RETC = TC_RETURN,
104 SN_RBRA = TC_R_BRACK,
105 SN_RPAR = TC_R_PARENT,
106 SN_V = TC_SYMBOL,
107 SN_VOID = TC_VOID,
108 SN_ = TC_INVALID
109 };
110
111 /// binding strengths between token classes
112 enum Binding_Strength
113 {
114 BS_ANY = 0,
115 BS_ASS_B = 10, ///< ← ANY : ← to what is on its right
116 BS_F_B = 20, ///< F ANY : function to its right argument
117 BS_A_F = 30, ///< ANY F : function to its left argument
118 BS_LO_OP = 40, ///< ANY OP : operator to its left function
119 BS_VAL_VAL = 50, ///< VEC VEC : vector to vector
120 BS_OP_RO = 60, ///< OP ANY : (dyadic) operator to its right function
121 BS_V_ASS = 70, ///< ANY ← : ← to what is on its left
122 BS_ANY_BRA = 80 ///< ANY [] : [] to what is on its left
123 };
124
125 /**
126 The value type of a token
127 **/
128 enum TokenValueType
129 {
130 // token value types. The token value type defines the type of the
131 // token in the union 'value'.
132 // Type union member
133 TV_MASK = 0xFF00,
134 TV_NONE = 0x0000, // value not used
135 TV_CHAR = 0x0100, // Unicode .char_val
136 TV_INT = 0x0200, // uint64_t .int_val;
137 TV_FLT = 0x0300, // APL_Float .flt_val;
138 TV_CPX = 0x0400, // cdouble .complex_val;
139 TV_SYM = 0x0500, // Symbol * .sym_ptr;
140 TV_LIN = 0x0600, // Function_Line .fun_line;
141 TV_VAL = 0x0700, // Value_P .apl_val;
142 TV_INDEX = 0x0800, // IndexExpr * .index_val;
143 TV_FUN = 0x0900, // Function * .function;
144 };
145
146 /**
147 A token tag. It is comprised of 3 fields: a 16 bit Id, an 8 bit
148 TokenValueType, and an 8 bit Token class.
149
150 Bit: 31............................16 15.............8 7-..............0
151 --------------------------------------------------------------------
152 | Id | TokenValueType | TokenClass |
153 --------------------------------------------------------------------
154 **/
155 enum TokenTag
156 {
157
158 #define TD(tok, tc, tv, id) tok = tc | tv | (id << 16),
159 #include "Token.def"
160
161 TOK_FUN1 = TOK_FUN2,
162
163 };
164
165 inline void
Hswap(TokenTag & t1,TokenTag & t2)166 Hswap(TokenTag & t1, TokenTag & t2)
167 { const TokenTag tmp = t1; t1 = t2; t2 = tmp; }
168
169 #endif // __TOKENENUMS_HH_DEFINED__
170