Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 1 | #include <ctype.h> |
| 2 | #include <stdbool.h> |
| 3 | #include <stdint.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | |
Gavin Howard | f6aa329 | 2018-01-15 16:03:03 -0700 | [diff] [blame] | 8 | #include <bc/bc.h> |
| 9 | #include <bc/lex.h> |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 10 | |
| 11 | static const char* const token_type_strs[] = { |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 12 | BC_LEX_TOKEN_FOREACH(BC_LEX_GEN_STR) |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 13 | }; |
| 14 | |
| 15 | static const char* const keywords[] = { |
| 16 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 17 | "auto", |
| 18 | "break", |
| 19 | "continue", |
| 20 | "define", |
| 21 | "else", |
| 22 | "for", |
| 23 | "halt", |
| 24 | "ibase", |
| 25 | "if", |
| 26 | "last", |
| 27 | "length", |
| 28 | "limits", |
| 29 | "obase", |
| 30 | "print", |
| 31 | "quit", |
| 32 | "read", |
| 33 | "return", |
| 34 | "scale", |
| 35 | "sqrt", |
| 36 | "while", |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 37 | |
| 38 | }; |
| 39 | |
| 40 | static const uint32_t keyword_lens[] = { |
| 41 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 42 | 4, // auto |
| 43 | 5, // break |
| 44 | 8, // continue |
| 45 | 6, // define |
| 46 | 4, // else |
| 47 | 3, // for |
| 48 | 4, // halt |
| 49 | 5, // ibase |
| 50 | 2, // if |
| 51 | 4, // last |
| 52 | 6, // length |
| 53 | 6, // limits |
| 54 | 5, // obase |
| 55 | 5, // print |
| 56 | 4, // quit |
| 57 | 4, // read |
| 58 | 6, // return |
| 59 | 5, // scale |
| 60 | 4, // sqrt |
| 61 | 5, // while |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 62 | |
| 63 | }; |
| 64 | |
| 65 | static BcStatus bc_lex_token(BcLex* lex, BcLexToken* token); |
| 66 | static BcStatus bc_lex_whitespace(BcLex* lex, BcLexToken* token); |
| 67 | static BcStatus bc_lex_string(BcLex* lex, BcLexToken* token); |
| 68 | static BcStatus bc_lex_comment(BcLex* lex, BcLexToken* token); |
| 69 | static BcStatus bc_lex_number(BcLex* lex, BcLexToken* token, char start); |
| 70 | static BcStatus bc_lex_name(BcLex* lex, BcLexToken* token); |
| 71 | |
| 72 | BcStatus bc_lex_printToken(BcLexToken* token) { |
| 73 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 74 | printf("<%s", token_type_strs[token->type]); |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 75 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 76 | switch (token->type) { |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 77 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 78 | case BC_LEX_STRING: |
| 79 | case BC_LEX_NAME: |
| 80 | case BC_LEX_NUMBER: |
| 81 | printf(":%s", token->string); |
| 82 | break; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 83 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 84 | default: |
| 85 | break; |
| 86 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 87 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 88 | putchar('>'); |
| 89 | putchar('\n'); |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 90 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 91 | return BC_STATUS_SUCCESS; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Gavin Howard | 88ef8d6 | 2018-01-15 19:34:10 -0700 | [diff] [blame] | 94 | BcStatus bc_lex_init(BcLex* lex, const char* file) { |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 95 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 96 | if (lex == NULL ) { |
| 97 | return BC_STATUS_INVALID_PARAM; |
| 98 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 99 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 100 | lex->line = 1; |
| 101 | lex->newline = false; |
| 102 | lex->file = file; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 103 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 104 | return BC_STATUS_SUCCESS; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | BcStatus bc_lex_text(BcLex* lex, const char* text) { |
| 108 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 109 | if (lex == NULL || text == NULL) { |
| 110 | return BC_STATUS_INVALID_PARAM; |
| 111 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 112 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 113 | lex->buffer = text; |
| 114 | lex->idx = 0; |
| 115 | lex->len = strlen(text); |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 116 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 117 | return BC_STATUS_SUCCESS; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | BcStatus bc_lex_next(BcLex* lex, BcLexToken* token) { |
| 121 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 122 | BcStatus status; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 123 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 124 | if (lex == NULL || token == NULL) { |
| 125 | return BC_STATUS_INVALID_PARAM; |
| 126 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 127 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 128 | if (lex->idx == lex->len) { |
| 129 | token->type = BC_LEX_EOF; |
| 130 | return BC_STATUS_LEX_EOF; |
| 131 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 132 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 133 | if (lex->newline) { |
| 134 | ++lex->line; |
| 135 | lex->newline = false; |
| 136 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 137 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 138 | // Loop until failure or we don't have whitespace. This |
| 139 | // is so the parser doesn't get inundated with whitespace. |
| 140 | do { |
| 141 | status = bc_lex_token(lex, token); |
| 142 | } while (!status && token->type == BC_LEX_WHITESPACE); |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 143 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 144 | return status; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static BcStatus bc_lex_token(BcLex* lex, BcLexToken* token) { |
| 148 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 149 | BcStatus status = BC_STATUS_SUCCESS; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 150 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 151 | char c = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 152 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 153 | ++lex->idx; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 154 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 155 | char c2; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 156 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 157 | // This is the workhorse of the lexer. |
| 158 | switch (c) { |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 159 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 160 | case '\0': |
| 161 | { |
| 162 | token->type = BC_LEX_EOF; |
| 163 | break; |
| 164 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 165 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 166 | case '\t': |
| 167 | { |
| 168 | status = bc_lex_whitespace(lex, token); |
| 169 | break; |
| 170 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 171 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 172 | case '\n': |
| 173 | { |
| 174 | lex->newline = true; |
| 175 | token->type = BC_LEX_NEWLINE; |
| 176 | break; |
| 177 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 178 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 179 | case '\v': |
| 180 | case '\f': |
| 181 | case '\r': |
| 182 | case ' ': |
| 183 | { |
| 184 | status = bc_lex_whitespace(lex, token); |
| 185 | break; |
| 186 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 187 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 188 | case '!': |
| 189 | { |
| 190 | c2 = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 191 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 192 | if (c2 == '=') { |
| 193 | ++lex->idx; |
| 194 | token->type = BC_LEX_OP_REL_NOT_EQ; |
| 195 | } |
| 196 | else { |
| 197 | token->type = BC_LEX_OP_BOOL_NOT; |
| 198 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 199 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 200 | break; |
| 201 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 202 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 203 | case '"': |
| 204 | { |
| 205 | status = bc_lex_string(lex, token); |
| 206 | break; |
| 207 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 208 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 209 | case '%': |
| 210 | { |
| 211 | c2 = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 212 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 213 | if (c2 == '=') { |
| 214 | ++lex->idx; |
| 215 | token->type = BC_LEX_OP_ASSIGN_MODULUS; |
| 216 | } |
| 217 | else { |
| 218 | token->type = BC_LEX_OP_MODULUS; |
| 219 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 220 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 221 | break; |
| 222 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 223 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 224 | case '&': |
| 225 | { |
| 226 | c2 = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 227 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 228 | if (c2 == '&') { |
| 229 | ++lex->idx; |
| 230 | token->type = BC_LEX_OP_BOOL_AND; |
| 231 | } |
| 232 | else { |
| 233 | token->type = BC_LEX_INVALID; |
| 234 | status = BC_STATUS_LEX_INVALID_TOKEN; |
| 235 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 236 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 237 | break; |
| 238 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 239 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 240 | case '(': |
| 241 | { |
| 242 | token->type = BC_LEX_LEFT_PAREN; |
| 243 | break; |
| 244 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 245 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 246 | case ')': |
| 247 | { |
| 248 | token->type = BC_LEX_RIGHT_PAREN; |
| 249 | break; |
| 250 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 251 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 252 | case '*': |
| 253 | { |
| 254 | c2 = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 255 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 256 | if (c2 == '=') { |
| 257 | ++lex->idx; |
| 258 | token->type = BC_LEX_OP_ASSIGN_MULTIPLY; |
| 259 | } |
| 260 | else { |
| 261 | token->type = BC_LEX_OP_MULTIPLY; |
| 262 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 263 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 264 | break; |
| 265 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 266 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 267 | case '+': |
| 268 | { |
| 269 | c2 = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 270 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 271 | if (c2 == '=') { |
| 272 | ++lex->idx; |
| 273 | token->type = BC_LEX_OP_ASSIGN_PLUS; |
| 274 | } |
| 275 | else if (c2 == '+') { |
| 276 | ++lex->idx; |
| 277 | token->type = BC_LEX_OP_INC; |
| 278 | } |
| 279 | else { |
| 280 | token->type = BC_LEX_OP_PLUS; |
| 281 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 282 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 283 | break; |
| 284 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 285 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 286 | case ',': |
| 287 | { |
| 288 | token->type = BC_LEX_COMMA; |
| 289 | break; |
| 290 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 291 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 292 | case '-': |
| 293 | { |
| 294 | c2 = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 295 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 296 | if (c2 == '=') { |
| 297 | ++lex->idx; |
| 298 | token->type = BC_LEX_OP_ASSIGN_MINUS; |
| 299 | } |
| 300 | else if (c2 == '-') { |
| 301 | ++lex->idx; |
| 302 | token->type = BC_LEX_OP_DEC; |
| 303 | } |
| 304 | else { |
| 305 | token->type = BC_LEX_OP_MINUS; |
| 306 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 307 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 308 | break; |
| 309 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 310 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 311 | case '.': |
| 312 | { |
| 313 | status = bc_lex_number(lex, token, c); |
| 314 | break; |
| 315 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 316 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 317 | case '/': |
| 318 | { |
| 319 | c2 = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 320 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 321 | if (c2 == '=') { |
| 322 | ++lex->idx; |
| 323 | token->type = BC_LEX_OP_ASSIGN_DIVIDE; |
| 324 | } |
| 325 | else if (c2 == '*') { |
| 326 | status = bc_lex_comment(lex, token); |
| 327 | } |
| 328 | else { |
| 329 | token->type = BC_LEX_OP_DIVIDE; |
| 330 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 331 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 332 | break; |
| 333 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 334 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 335 | case '0': |
| 336 | case '1': |
| 337 | case '2': |
| 338 | case '3': |
| 339 | case '4': |
| 340 | case '5': |
| 341 | case '6': |
| 342 | case '7': |
| 343 | case '8': |
| 344 | case '9': |
| 345 | { |
| 346 | status = bc_lex_number(lex, token, c); |
| 347 | break; |
| 348 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 349 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 350 | case ';': |
| 351 | { |
| 352 | token->type = BC_LEX_SEMICOLON; |
| 353 | break; |
| 354 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 355 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 356 | case '<': |
| 357 | { |
| 358 | c2 = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 359 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 360 | if (c2 == '=') { |
| 361 | ++lex->idx; |
| 362 | token->type = BC_LEX_OP_REL_LESS_EQ; |
| 363 | } |
| 364 | else { |
| 365 | token->type = BC_LEX_OP_REL_LESS; |
| 366 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 367 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 368 | break; |
| 369 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 370 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 371 | case '=': |
| 372 | { |
| 373 | c2 = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 374 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 375 | if (c2 == '=') { |
| 376 | ++lex->idx; |
| 377 | token->type = BC_LEX_OP_REL_EQUAL; |
| 378 | } |
| 379 | else { |
| 380 | token->type = BC_LEX_OP_ASSIGN; |
| 381 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 382 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 383 | break; |
| 384 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 385 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 386 | case '>': |
| 387 | { |
| 388 | c2 = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 389 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 390 | if (c2 == '=') { |
| 391 | ++lex->idx; |
| 392 | token->type = BC_LEX_OP_REL_GREATER_EQ; |
| 393 | } |
| 394 | else { |
| 395 | token->type = BC_LEX_OP_REL_GREATER; |
| 396 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 397 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 398 | break; |
| 399 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 400 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 401 | case 'A': |
| 402 | case 'B': |
| 403 | case 'C': |
| 404 | case 'D': |
| 405 | case 'E': |
| 406 | case 'F': |
| 407 | { |
| 408 | status = bc_lex_number(lex, token, c); |
| 409 | break; |
| 410 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 411 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 412 | case '[': |
| 413 | { |
| 414 | token->type = BC_LEX_LEFT_BRACKET; |
| 415 | break; |
| 416 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 417 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 418 | case '\\': |
| 419 | { |
| 420 | status = bc_lex_whitespace(lex, token); |
| 421 | break; |
| 422 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 423 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 424 | case ']': |
| 425 | { |
| 426 | token->type = BC_LEX_RIGHT_BRACKET; |
| 427 | break; |
| 428 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 429 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 430 | case '^': |
| 431 | { |
| 432 | c2 = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 433 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 434 | if (c2 == '=') { |
| 435 | ++lex->idx; |
| 436 | token->type = BC_LEX_OP_ASSIGN_POWER; |
| 437 | } |
| 438 | else { |
| 439 | token->type = BC_LEX_OP_POWER; |
| 440 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 441 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 442 | break; |
| 443 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 444 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 445 | case 'a': |
| 446 | case 'b': |
| 447 | case 'c': |
| 448 | case 'd': |
| 449 | case 'e': |
| 450 | case 'f': |
| 451 | case 'g': |
| 452 | case 'h': |
| 453 | case 'i': |
| 454 | case 'j': |
| 455 | case 'k': |
| 456 | case 'l': |
| 457 | case 'm': |
| 458 | case 'n': |
| 459 | case 'o': |
| 460 | case 'p': |
| 461 | case 'q': |
| 462 | case 'r': |
| 463 | case 's': |
| 464 | case 't': |
| 465 | case 'u': |
| 466 | case 'v': |
| 467 | case 'w': |
| 468 | case 'x': |
| 469 | case 'y': |
| 470 | case 'z': |
| 471 | { |
| 472 | status = bc_lex_name(lex, token); |
| 473 | break; |
| 474 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 475 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 476 | case '{': |
| 477 | { |
| 478 | token->type = BC_LEX_LEFT_BRACE; |
| 479 | break; |
| 480 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 481 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 482 | case '|': |
| 483 | { |
| 484 | c2 = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 485 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 486 | if (c2 == '|') { |
| 487 | ++lex->idx; |
| 488 | token->type = BC_LEX_OP_BOOL_OR; |
| 489 | } |
| 490 | else { |
| 491 | token->type = BC_LEX_INVALID; |
| 492 | status = BC_STATUS_LEX_INVALID_TOKEN; |
| 493 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 494 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 495 | break; |
| 496 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 497 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 498 | case '}': |
| 499 | { |
| 500 | token->type = BC_LEX_RIGHT_BRACE; |
| 501 | break; |
| 502 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 503 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 504 | default: |
| 505 | { |
| 506 | token->type = BC_LEX_INVALID; |
| 507 | status = BC_STATUS_LEX_INVALID_TOKEN; |
| 508 | break; |
| 509 | } |
| 510 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 511 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 512 | return status; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static BcStatus bc_lex_whitespace(BcLex* lex, BcLexToken* token) { |
| 516 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 517 | token->type = BC_LEX_WHITESPACE; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 518 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 519 | char c = lex->buffer[lex->idx]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 520 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 521 | while ((isspace(c) && c != '\n') || c == '\\') { |
| 522 | ++lex->idx; |
| 523 | c = lex->buffer[lex->idx]; |
| 524 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 525 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 526 | return BC_STATUS_SUCCESS; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static BcStatus bc_lex_string(BcLex* lex, BcLexToken* token) { |
| 530 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 531 | uint32_t newlines; |
Gavin Howard | bdd97f0 | 2018-01-17 12:05:04 -0700 | [diff] [blame] | 532 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 533 | newlines = 0; |
Gavin Howard | bdd97f0 | 2018-01-17 12:05:04 -0700 | [diff] [blame] | 534 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 535 | token->type = BC_LEX_STRING; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 536 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 537 | size_t i = lex->idx; |
| 538 | char c = lex->buffer[i]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 539 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 540 | while (c != '"' && c != '\0') { |
Gavin Howard | bdd97f0 | 2018-01-17 12:05:04 -0700 | [diff] [blame] | 541 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 542 | if (c == '\n') { |
| 543 | ++newlines; |
| 544 | } |
Gavin Howard | bdd97f0 | 2018-01-17 12:05:04 -0700 | [diff] [blame] | 545 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 546 | c = lex->buffer[++i]; |
| 547 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 548 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 549 | if (c == '\0') { |
| 550 | lex->idx = i; |
| 551 | return BC_STATUS_LEX_NO_STRING_END; |
| 552 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 553 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 554 | size_t len = i - lex->idx; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 555 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 556 | token->string = malloc(len + 1); |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 557 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 558 | if (token->string == NULL) { |
| 559 | return BC_STATUS_MALLOC_FAIL; |
| 560 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 561 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 562 | const char* start = lex->buffer + lex->idx; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 563 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 564 | for (size_t j = 0; j < len; ++j) { |
| 565 | token->string[j] = start[j]; |
| 566 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 567 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 568 | token->string[len] = '\0'; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 569 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 570 | lex->idx = i + 1; |
| 571 | lex->line += newlines; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 572 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 573 | return BC_STATUS_SUCCESS; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static BcStatus bc_lex_comment(BcLex* lex, BcLexToken* token) { |
| 577 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 578 | uint32_t newlines; |
Gavin Howard | bdd97f0 | 2018-01-17 12:05:04 -0700 | [diff] [blame] | 579 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 580 | newlines = 0; |
Gavin Howard | bdd97f0 | 2018-01-17 12:05:04 -0700 | [diff] [blame] | 581 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 582 | token->type = BC_LEX_WHITESPACE; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 583 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 584 | ++lex->idx; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 585 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 586 | size_t i = lex->idx; |
| 587 | const char* buffer = lex->buffer; |
| 588 | char c = buffer[i]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 589 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 590 | if (c == '\n') { |
| 591 | ++newlines; |
| 592 | } |
Gavin Howard | bdd97f0 | 2018-01-17 12:05:04 -0700 | [diff] [blame] | 593 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 594 | int end = 0; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 595 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 596 | while (!end) { |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 597 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 598 | while (c != '*' && c != '\0') { |
| 599 | c = buffer[++i]; |
| 600 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 601 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 602 | if (c == '\n') { |
| 603 | ++newlines; |
| 604 | } |
Gavin Howard | bdd97f0 | 2018-01-17 12:05:04 -0700 | [diff] [blame] | 605 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 606 | if (c == '\0' || buffer[i + 1] == '\0') { |
| 607 | lex->idx = i; |
| 608 | return BC_STATUS_LEX_NO_COMMENT_END; |
| 609 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 610 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 611 | end = buffer[i + 1] == '/'; |
| 612 | i += end ? 0 : 1; |
| 613 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 614 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 615 | lex->idx = i + 2; |
| 616 | lex->line += newlines; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 617 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 618 | return BC_STATUS_SUCCESS; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | static BcStatus bc_lex_number(BcLex* lex, BcLexToken* token, char start) { |
| 622 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 623 | token->type = BC_LEX_NUMBER; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 624 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 625 | int point = start == '.'; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 626 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 627 | const char* buffer = lex->buffer + lex->idx; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 628 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 629 | size_t backslashes = 0; |
| 630 | size_t i = 0; |
| 631 | char c = buffer[i]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 632 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 633 | while (c && ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || |
| 634 | (c == '.' && !point) || (c == '\\' && buffer[i + 1] == '\n'))) |
| 635 | { |
| 636 | if (c == '\\') { |
| 637 | ++i; |
| 638 | backslashes += 1; |
| 639 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 640 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 641 | c = buffer[++i]; |
| 642 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 643 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 644 | size_t len = i + 1; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 645 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 646 | token->string = malloc(len - backslashes + 1); |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 647 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 648 | if (token->string == NULL) { |
| 649 | return BC_STATUS_MALLOC_FAIL; |
| 650 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 651 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 652 | token->string[0] = start; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 653 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 654 | const char* buf = buffer - 1; |
| 655 | size_t hits = 0; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 656 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 657 | for (size_t j = 1; j < len; ++j) { |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 658 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 659 | char c = buf[j]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 660 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 661 | // If we have hit a backslash, skip it. |
| 662 | // We don't have to check for a newline |
| 663 | // because it's guaranteed. |
| 664 | if (hits < backslashes && c == '\\') { |
| 665 | ++hits; |
| 666 | ++j; |
| 667 | continue; |
| 668 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 669 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 670 | token->string[j - (hits * 2)] = c; |
| 671 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 672 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 673 | token->string[len] = '\0'; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 674 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 675 | lex->idx += i; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 676 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 677 | return BC_STATUS_SUCCESS; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | static BcStatus bc_lex_name(BcLex* lex, BcLexToken* token) { |
| 681 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 682 | const char* buffer = lex->buffer + lex->idx - 1; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 683 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 684 | for (uint32_t i = 0; i < sizeof(keywords) / sizeof(char*); ++i) { |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 685 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 686 | if (!strncmp(buffer, keywords[i], keyword_lens[i])) { |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 687 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 688 | token->type = BC_LEX_KEY_AUTO + i; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 689 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 690 | // We need to minus one because the |
| 691 | // index has already been incremented. |
| 692 | lex->idx += keyword_lens[i] - 1; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 693 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 694 | return BC_STATUS_SUCCESS; |
| 695 | } |
| 696 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 697 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 698 | token->type = BC_LEX_NAME; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 699 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 700 | size_t i = 0; |
| 701 | char c = buffer[i]; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 702 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 703 | while ((c >= 'a' && c<= 'z') || (c >= '0' && c <= '9') || c == '_') { |
| 704 | ++i; |
| 705 | c = buffer[i]; |
| 706 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 707 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 708 | token->string = malloc(i + 1); |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 709 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 710 | if (token->string == NULL) { |
| 711 | return BC_STATUS_MALLOC_FAIL; |
| 712 | } |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 713 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 714 | strncpy(token->string, buffer, i); |
| 715 | token->string[i] = '\0'; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 716 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 717 | // Increment the index. It is minus one |
| 718 | // because it has already been incremented. |
| 719 | lex->idx += i - 1; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 720 | |
Gavin Howard | 4bc73ee | 2018-01-26 11:39:20 -0700 | [diff] [blame^] | 721 | return BC_STATUS_SUCCESS; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 722 | } |