Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 1 | /* |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 2 | * ***************************************************************************** |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 3 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 4 | * Copyright 2018 Gavin D. Howard |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 5 | * |
| 6 | * Permission to use, copy, modify, and/or distribute this software for any |
| 7 | * purpose with or without fee is hereby granted. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 10 | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 | * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 14 | * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 | * PERFORMANCE OF THIS SOFTWARE. |
| 16 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 17 | * ***************************************************************************** |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 18 | * |
Gavin Howard | d2a0525 | 2018-09-27 14:00:40 -0600 | [diff] [blame] | 19 | * Common code for the lexers. |
Gavin Howard | 5715b04 | 2018-02-12 16:11:42 -0700 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 23 | #include <assert.h> |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 24 | #include <ctype.h> |
| 25 | #include <stdbool.h> |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 26 | #include <string.h> |
| 27 | |
Gavin Howard | 2949306 | 2018-03-20 19:57:37 -0600 | [diff] [blame] | 28 | #include <status.h> |
Gavin Howard | 3ba6c8d | 2018-02-15 12:23:35 -0700 | [diff] [blame] | 29 | #include <lex.h> |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 30 | #include <vm.h> |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 31 | |
Gavin Howard | ed5c831 | 2018-09-27 12:04:08 -0600 | [diff] [blame] | 32 | void bc_lex_lineComment(BcLex *l) { |
| 33 | l->t.t = BC_LEX_WHITESPACE; |
| 34 | while (l->idx < l->len && l->buffer[l->idx++] != '\n'); |
| 35 | --l->idx; |
| 36 | } |
| 37 | |
Gavin Howard | 364df3b | 2018-09-28 09:48:19 -0600 | [diff] [blame^] | 38 | void bc_lex_whitespace(BcLex *l) { |
| 39 | char c; |
| 40 | l->t.t = BC_LEX_WHITESPACE; |
| 41 | for (; (c = l->buffer[l->idx]) != '\n' && isspace(c); ++l->idx); |
| 42 | } |
| 43 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 44 | BcStatus bc_lex_number(BcLex *l, char start) { |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 45 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 46 | BcStatus s; |
| 47 | const char *buf = l->buffer + l->idx; |
| 48 | size_t len, hits, bslashes = 0, i = 0, j; |
| 49 | char c = buf[i]; |
| 50 | bool last_pt, pt = start == '.'; |
Gavin Howard | f2a4049 | 2018-03-05 11:27:29 -0700 | [diff] [blame] | 51 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 52 | last_pt = pt; |
| 53 | l->t.t = BC_LEX_NUMBER; |
Gavin Howard | 07732ec | 2018-02-27 15:40:02 -0700 | [diff] [blame] | 54 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 55 | while (c && ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || |
| 56 | (c == '.' && !pt) || (c == '\\' && buf[i + 1] == '\n'))) |
| 57 | { |
| 58 | if (c != '\\') { |
| 59 | last_pt = c == '.'; |
| 60 | pt = pt || last_pt; |
| 61 | } |
| 62 | else { |
| 63 | ++i; |
| 64 | bslashes += 1; |
| 65 | } |
Gavin Howard | 07732ec | 2018-02-27 15:40:02 -0700 | [diff] [blame] | 66 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 67 | c = buf[++i]; |
| 68 | } |
Gavin Howard | 07732ec | 2018-02-27 15:40:02 -0700 | [diff] [blame] | 69 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 70 | len = i + 1 * !last_pt - bslashes * 2; |
| 71 | if (len > BC_MAX_NUM) return BC_STATUS_EXEC_NUM_LEN; |
Gavin Howard | 07732ec | 2018-02-27 15:40:02 -0700 | [diff] [blame] | 72 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 73 | bc_vec_npop(&l->t.v, l->t.v.len); |
Gavin Howard | 0275a55 | 2018-09-27 12:08:23 -0600 | [diff] [blame] | 74 | if ((s = bc_vec_expand(&l->t.v, len + 1))) return s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 75 | if ((s = bc_vec_push(&l->t.v, 1, &start))) return s; |
Gavin Howard | a628aa2 | 2018-09-12 13:52:45 -0600 | [diff] [blame] | 76 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 77 | buf -= 1; |
| 78 | hits = 0; |
Gavin Howard | 07732ec | 2018-02-27 15:40:02 -0700 | [diff] [blame] | 79 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 80 | for (j = 1; j < len + hits * 2; ++j) { |
Gavin Howard | 07732ec | 2018-02-27 15:40:02 -0700 | [diff] [blame] | 81 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 82 | c = buf[j]; |
Gavin Howard | 07732ec | 2018-02-27 15:40:02 -0700 | [diff] [blame] | 83 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 84 | // If we have hit a backslash, skip it. We don't have |
| 85 | // to check for a newline because it's guaranteed. |
| 86 | if (hits < bslashes && c == '\\') { |
| 87 | ++hits; |
| 88 | ++j; |
| 89 | continue; |
| 90 | } |
Gavin Howard | 07732ec | 2018-02-27 15:40:02 -0700 | [diff] [blame] | 91 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 92 | if ((s = bc_vec_push(&l->t.v, 1, &c))) return s; |
| 93 | } |
Gavin Howard | 07732ec | 2018-02-27 15:40:02 -0700 | [diff] [blame] | 94 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 95 | if ((s = bc_vec_pushByte(&l->t.v, '\0'))) return s; |
| 96 | l->idx += i; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 97 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 98 | return BC_STATUS_SUCCESS; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Gavin Howard | ed5c831 | 2018-09-27 12:04:08 -0600 | [diff] [blame] | 101 | BcStatus bc_lex_init(BcLex *l, BcLexNext next) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 102 | assert(l); |
Gavin Howard | ed5c831 | 2018-09-27 12:04:08 -0600 | [diff] [blame] | 103 | l->next = next; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 104 | return bc_vec_init(&l->t.v, sizeof(uint8_t), NULL); |
Gavin Howard | 6918504 | 2018-09-10 15:46:20 -0600 | [diff] [blame] | 105 | } |
| 106 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 107 | void bc_lex_free(BcLex *l) { |
| 108 | assert(l); |
| 109 | bc_vec_free(&l->t.v); |
Gavin Howard | 6918504 | 2018-09-10 15:46:20 -0600 | [diff] [blame] | 110 | } |
| 111 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 112 | void bc_lex_file(BcLex *l, const char *file) { |
| 113 | assert(l && file); |
| 114 | l->line = 1; |
| 115 | l->newline = false; |
| 116 | l->file = file; |
Gavin Howard | 8a596d4 | 2018-01-15 15:46:01 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 119 | BcStatus bc_lex_text(BcLex *l, const char *text) { |
| 120 | assert(l && text); |
| 121 | l->buffer = text; |
| 122 | l->idx = 0; |
| 123 | l->len = strlen(text); |
| 124 | l->t.t = BC_LEX_INVALID; |
Gavin Howard | 364df3b | 2018-09-28 09:48:19 -0600 | [diff] [blame^] | 125 | return bc_lex_next(l); |
| 126 | } |
| 127 | |
| 128 | BcStatus bc_lex_next(BcLex *l) { |
| 129 | |
| 130 | BcStatus s; |
| 131 | |
| 132 | assert(l); |
| 133 | |
| 134 | if (l->t.t == BC_LEX_EOF) return BC_STATUS_LEX_EOF; |
| 135 | |
| 136 | if (l->idx == l->len) { |
| 137 | l->newline = true; |
| 138 | l->t.t = BC_LEX_EOF; |
| 139 | return BC_STATUS_SUCCESS; |
| 140 | } |
| 141 | |
| 142 | if (l->newline) { |
| 143 | ++l->line; |
| 144 | l->newline = false; |
| 145 | } |
| 146 | |
| 147 | // Loop until failure or we don't have whitespace. This |
| 148 | // is so the parser doesn't get inundated with whitespace. |
| 149 | do { |
| 150 | s = l->next(l); |
| 151 | } while (!s && l->t.t == BC_LEX_WHITESPACE); |
| 152 | |
| 153 | return s; |
Gavin Howard | 3575392 | 2018-03-21 19:22:08 -0600 | [diff] [blame] | 154 | } |