blob: 05e40dcd369335752ef88a8acc36d760c4076b9a [file] [log] [blame]
Gavin Howard5715b042018-02-12 16:11:42 -07001/*
Gavin Howardb5904bf2018-02-20 13:28:18 -07002 * *****************************************************************************
Gavin Howard5715b042018-02-12 16:11:42 -07003 *
Gavin Howardb5904bf2018-02-20 13:28:18 -07004 * Copyright 2018 Gavin D. Howard
Gavin Howard5715b042018-02-12 16:11:42 -07005 *
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 Howardb5904bf2018-02-20 13:28:18 -070017 * *****************************************************************************
Gavin Howard5715b042018-02-12 16:11:42 -070018 *
Gavin Howardd2a05252018-09-27 14:00:40 -060019 * Common code for the lexers.
Gavin Howard5715b042018-02-12 16:11:42 -070020 *
21 */
22
Gavin Howard27fdfb92018-03-21 07:56:59 -060023#include <assert.h>
Gavin Howard8a596d42018-01-15 15:46:01 -070024#include <ctype.h>
25#include <stdbool.h>
Gavin Howard8a596d42018-01-15 15:46:01 -070026#include <string.h>
27
Gavin Howard29493062018-03-20 19:57:37 -060028#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070029#include <lex.h>
Gavin Howardd5551672018-09-22 19:52:42 -060030#include <vm.h>
Gavin Howard8a596d42018-01-15 15:46:01 -070031
Gavin Howarded5c8312018-09-27 12:04:08 -060032void bc_lex_lineComment(BcLex *l) {
33 l->t.t = BC_LEX_WHITESPACE;
Gavin Howard53eba8b2018-10-31 15:14:37 -060034 while (l->i < l->len && l->buf[l->i++] != '\n');
35 --l->i;
Gavin Howarded5c8312018-09-27 12:04:08 -060036}
37
Gavin Howard52446f22018-12-13 11:39:22 -070038BcStatus bc_lex_comment(BcLex *l) {
39
40 size_t i, nlines = 0;
41 const char *buf = l->buf;
42 bool end = false;
43 char c;
44
45 l->t.t = BC_LEX_WHITESPACE;
46
47 for (i = ++l->i; !end; i += !end) {
48
49 for (c = buf[i]; c != '*' && c != 0; c = buf[++i]) nlines += c == '\n';
50
51 if (c == 0 || buf[i + 1] == '\0') {
52 l->i = i;
Gavin Howard7536dcf2018-12-15 19:27:09 -070053 return bc_vm_error(BC_ERROR_PARSE_BAD_COMMENT, l->line);
Gavin Howard52446f22018-12-13 11:39:22 -070054 }
55
56 end = buf[i + 1] == '/';
57 }
58
59 l->i = i + 2;
60 l->line += nlines;
61
62 return BC_STATUS_SUCCESS;
63}
64
Gavin Howard364df3b2018-09-28 09:48:19 -060065void bc_lex_whitespace(BcLex *l) {
66 char c;
67 l->t.t = BC_LEX_WHITESPACE;
Gavin Howard53eba8b2018-10-31 15:14:37 -060068 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
Gavin Howard364df3b2018-09-28 09:48:19 -060069}
70
Gavin Howard63738202018-09-26 15:34:20 -060071BcStatus bc_lex_number(BcLex *l, char start) {
Gavin Howard8a596d42018-01-15 15:46:01 -070072
Gavin Howard53eba8b2018-10-31 15:14:37 -060073 const char *buf = l->buf + l->i;
Gavin Howardc39fd492018-10-04 10:07:03 -060074 size_t len, hits = 0, bslashes = 0, i = 0, j;
Gavin Howard63738202018-09-26 15:34:20 -060075 char c = buf[i];
76 bool last_pt, pt = start == '.';
Gavin Howardf2a40492018-03-05 11:27:29 -070077
Gavin Howard63738202018-09-26 15:34:20 -060078 last_pt = pt;
79 l->t.t = BC_LEX_NUMBER;
Gavin Howard07732ec2018-02-27 15:40:02 -070080
Gavin Howard53eba8b2018-10-31 15:14:37 -060081 while (c != 0 && (isdigit(c) || (c >= 'A' && c <= 'F') ||
Gavin Howard63738202018-09-26 15:34:20 -060082 (c == '.' && !pt) || (c == '\\' && buf[i + 1] == '\n')))
83 {
84 if (c != '\\') {
85 last_pt = c == '.';
86 pt = pt || last_pt;
87 }
88 else {
89 ++i;
90 bslashes += 1;
91 }
Gavin Howard07732ec2018-02-27 15:40:02 -070092
Gavin Howard63738202018-09-26 15:34:20 -060093 c = buf[++i];
94 }
Gavin Howard07732ec2018-02-27 15:40:02 -070095
Gavin Howard63738202018-09-26 15:34:20 -060096 len = i + 1 * !last_pt - bslashes * 2;
Gavin Howard7536dcf2018-12-15 19:27:09 -070097
98 if (len > BC_MAX_NUM) return bc_vm_error(BC_ERROR_EXEC_NUM_LEN, l->line);
Gavin Howard07732ec2018-02-27 15:40:02 -070099
Gavin Howard63738202018-09-26 15:34:20 -0600100 bc_vec_npop(&l->t.v, l->t.v.len);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600101 bc_vec_expand(&l->t.v, len + 1);
102 bc_vec_push(&l->t.v, &start);
Gavin Howarda628aa22018-09-12 13:52:45 -0600103
Gavin Howardc39fd492018-10-04 10:07:03 -0600104 for (buf -= 1, j = 1; j < len + hits * 2; ++j) {
Gavin Howard07732ec2018-02-27 15:40:02 -0700105
Gavin Howard63738202018-09-26 15:34:20 -0600106 c = buf[j];
Gavin Howard07732ec2018-02-27 15:40:02 -0700107
Gavin Howard63738202018-09-26 15:34:20 -0600108 // If we have hit a backslash, skip it. We don't have
109 // to check for a newline because it's guaranteed.
110 if (hits < bslashes && c == '\\') {
111 ++hits;
112 ++j;
113 continue;
114 }
Gavin Howard07732ec2018-02-27 15:40:02 -0700115
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600116 bc_vec_push(&l->t.v, &c);
Gavin Howard63738202018-09-26 15:34:20 -0600117 }
Gavin Howard07732ec2018-02-27 15:40:02 -0700118
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600119 bc_vec_pushByte(&l->t.v, '\0');
Gavin Howard53eba8b2018-10-31 15:14:37 -0600120 l->i += i;
Gavin Howard8a596d42018-01-15 15:46:01 -0700121
Gavin Howard63738202018-09-26 15:34:20 -0600122 return BC_STATUS_SUCCESS;
Gavin Howard8a596d42018-01-15 15:46:01 -0700123}
124
Gavin Howard8412ba82018-10-04 13:03:24 -0600125BcStatus bc_lex_name(BcLex *l) {
126
Gavin Howard88c25302018-10-17 13:32:23 -0600127 size_t i = 0;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600128 const char *buf = l->buf + l->i - 1;
Gavin Howard88c25302018-10-17 13:32:23 -0600129 char c = buf[i];
Gavin Howard8412ba82018-10-04 13:03:24 -0600130
131 l->t.t = BC_LEX_NAME;
Gavin Howard8412ba82018-10-04 13:03:24 -0600132
Gavin Howard9a4b6cd2018-10-23 15:13:30 -0600133 while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i];
Gavin Howard8412ba82018-10-04 13:03:24 -0600134
Gavin Howard7536dcf2018-12-15 19:27:09 -0700135 if (i > BC_MAX_STRING) return bc_vm_error(BC_ERROR_EXEC_NAME_LEN, l->line);
136
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600137 bc_vec_string(&l->t.v, i, buf);
Gavin Howard8412ba82018-10-04 13:03:24 -0600138
139 // Increment the index. We minus 1 because it has already been incremented.
Gavin Howard53eba8b2018-10-31 15:14:37 -0600140 l->i += i - 1;
Gavin Howard8412ba82018-10-04 13:03:24 -0600141
142 return BC_STATUS_SUCCESS;
143}
144
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600145void bc_lex_init(BcLex *l, BcLexNext next) {
Gavin Howard63738202018-09-26 15:34:20 -0600146 assert(l);
Gavin Howarded5c8312018-09-27 12:04:08 -0600147 l->next = next;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600148 bc_vec_init(&l->t.v, sizeof(char), NULL);
Gavin Howard69185042018-09-10 15:46:20 -0600149}
150
Gavin Howard63738202018-09-26 15:34:20 -0600151void bc_lex_free(BcLex *l) {
152 assert(l);
153 bc_vec_free(&l->t.v);
Gavin Howard69185042018-09-10 15:46:20 -0600154}
155
Gavin Howard63738202018-09-26 15:34:20 -0600156void bc_lex_file(BcLex *l, const char *file) {
157 assert(l && file);
158 l->line = 1;
159 l->newline = false;
Gavin Howard7536dcf2018-12-15 19:27:09 -0700160 vm->file = file;
Gavin Howard8a596d42018-01-15 15:46:01 -0700161}
162
Gavin Howard364df3b2018-09-28 09:48:19 -0600163BcStatus bc_lex_next(BcLex *l) {
164
165 BcStatus s;
166
167 assert(l);
168
Gavin Howard890d0c02018-10-30 16:34:50 -0600169 l->t.last = l->t.t;
Gavin Howard7536dcf2018-12-15 19:27:09 -0700170
171 if (l->t.last == BC_LEX_EOF) return bc_vm_error(BC_ERROR_PARSE_EOF, l->line);
Gavin Howard364df3b2018-09-28 09:48:19 -0600172
Gavin Howardc39fd492018-10-04 10:07:03 -0600173 l->line += l->newline;
Gavin Howard9a4b6cd2018-10-23 15:13:30 -0600174 l->t.t = BC_LEX_EOF;
Gavin Howardc39fd492018-10-04 10:07:03 -0600175
Gavin Howard53eba8b2018-10-31 15:14:37 -0600176 l->newline = (l->i == l->len);
Gavin Howard890d0c02018-10-30 16:34:50 -0600177 if (l->newline) return BC_STATUS_SUCCESS;
Gavin Howard364df3b2018-09-28 09:48:19 -0600178
Gavin Howard364df3b2018-09-28 09:48:19 -0600179 // Loop until failure or we don't have whitespace. This
180 // is so the parser doesn't get inundated with whitespace.
Gavin Howard53eba8b2018-10-31 15:14:37 -0600181 do {
182 s = l->next(l);
183 } while (!s && l->t.t == BC_LEX_WHITESPACE);
Gavin Howard364df3b2018-09-28 09:48:19 -0600184
185 return s;
Gavin Howard35753922018-03-21 19:22:08 -0600186}
Gavin Howardc9a9c472018-10-02 17:23:01 -0600187
188BcStatus bc_lex_text(BcLex *l, const char *text) {
189 assert(l && text);
Gavin Howard890d0c02018-10-30 16:34:50 -0600190 l->buf = text;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600191 l->i = 0;
Gavin Howardc9a9c472018-10-02 17:23:01 -0600192 l->len = strlen(text);
193 l->t.t = l->t.last = BC_LEX_INVALID;
194 return bc_lex_next(l);
195}