blob: d62f50934ed4f445c6d3f9d20c92629990ccdaa5 [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;
53 return BC_STATUS_PARSE_NO_COMMENT_END;
54 }
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;
97 if (len > BC_MAX_NUM) return BC_STATUS_EXEC_NUM_LEN;
Gavin Howard07732ec2018-02-27 15:40:02 -070098
Gavin Howard63738202018-09-26 15:34:20 -060099 bc_vec_npop(&l->t.v, l->t.v.len);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600100 bc_vec_expand(&l->t.v, len + 1);
101 bc_vec_push(&l->t.v, &start);
Gavin Howarda628aa22018-09-12 13:52:45 -0600102
Gavin Howardc39fd492018-10-04 10:07:03 -0600103 for (buf -= 1, j = 1; j < len + hits * 2; ++j) {
Gavin Howard07732ec2018-02-27 15:40:02 -0700104
Gavin Howard63738202018-09-26 15:34:20 -0600105 c = buf[j];
Gavin Howard07732ec2018-02-27 15:40:02 -0700106
Gavin Howard63738202018-09-26 15:34:20 -0600107 // If we have hit a backslash, skip it. We don't have
108 // to check for a newline because it's guaranteed.
109 if (hits < bslashes && c == '\\') {
110 ++hits;
111 ++j;
112 continue;
113 }
Gavin Howard07732ec2018-02-27 15:40:02 -0700114
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600115 bc_vec_push(&l->t.v, &c);
Gavin Howard63738202018-09-26 15:34:20 -0600116 }
Gavin Howard07732ec2018-02-27 15:40:02 -0700117
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600118 bc_vec_pushByte(&l->t.v, '\0');
Gavin Howard53eba8b2018-10-31 15:14:37 -0600119 l->i += i;
Gavin Howard8a596d42018-01-15 15:46:01 -0700120
Gavin Howard63738202018-09-26 15:34:20 -0600121 return BC_STATUS_SUCCESS;
Gavin Howard8a596d42018-01-15 15:46:01 -0700122}
123
Gavin Howard8412ba82018-10-04 13:03:24 -0600124BcStatus bc_lex_name(BcLex *l) {
125
Gavin Howard88c25302018-10-17 13:32:23 -0600126 size_t i = 0;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600127 const char *buf = l->buf + l->i - 1;
Gavin Howard88c25302018-10-17 13:32:23 -0600128 char c = buf[i];
Gavin Howard8412ba82018-10-04 13:03:24 -0600129
130 l->t.t = BC_LEX_NAME;
Gavin Howard8412ba82018-10-04 13:03:24 -0600131
Gavin Howard9a4b6cd2018-10-23 15:13:30 -0600132 while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i];
Gavin Howard8412ba82018-10-04 13:03:24 -0600133
134 if (i > BC_MAX_STRING) return BC_STATUS_EXEC_NAME_LEN;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600135 bc_vec_string(&l->t.v, i, buf);
Gavin Howard8412ba82018-10-04 13:03:24 -0600136
137 // Increment the index. We minus 1 because it has already been incremented.
Gavin Howard53eba8b2018-10-31 15:14:37 -0600138 l->i += i - 1;
Gavin Howard8412ba82018-10-04 13:03:24 -0600139
140 return BC_STATUS_SUCCESS;
141}
142
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600143void bc_lex_init(BcLex *l, BcLexNext next) {
Gavin Howard63738202018-09-26 15:34:20 -0600144 assert(l);
Gavin Howarded5c8312018-09-27 12:04:08 -0600145 l->next = next;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600146 bc_vec_init(&l->t.v, sizeof(char), NULL);
Gavin Howard69185042018-09-10 15:46:20 -0600147}
148
Gavin Howard63738202018-09-26 15:34:20 -0600149void bc_lex_free(BcLex *l) {
150 assert(l);
151 bc_vec_free(&l->t.v);
Gavin Howard69185042018-09-10 15:46:20 -0600152}
153
Gavin Howard63738202018-09-26 15:34:20 -0600154void bc_lex_file(BcLex *l, const char *file) {
155 assert(l && file);
156 l->line = 1;
157 l->newline = false;
Gavin Howardc39fd492018-10-04 10:07:03 -0600158 l->f = file;
Gavin Howard8a596d42018-01-15 15:46:01 -0700159}
160
Gavin Howard364df3b2018-09-28 09:48:19 -0600161BcStatus bc_lex_next(BcLex *l) {
162
163 BcStatus s;
164
165 assert(l);
166
Gavin Howard890d0c02018-10-30 16:34:50 -0600167 l->t.last = l->t.t;
Gavin Howard1bc09122018-12-11 11:59:54 -0700168 if (l->t.last == BC_LEX_EOF) return BC_STATUS_PARSE_EOF;
Gavin Howard364df3b2018-09-28 09:48:19 -0600169
Gavin Howardc39fd492018-10-04 10:07:03 -0600170 l->line += l->newline;
Gavin Howard9a4b6cd2018-10-23 15:13:30 -0600171 l->t.t = BC_LEX_EOF;
Gavin Howardc39fd492018-10-04 10:07:03 -0600172
Gavin Howard53eba8b2018-10-31 15:14:37 -0600173 l->newline = (l->i == l->len);
Gavin Howard890d0c02018-10-30 16:34:50 -0600174 if (l->newline) return BC_STATUS_SUCCESS;
Gavin Howard364df3b2018-09-28 09:48:19 -0600175
Gavin Howard364df3b2018-09-28 09:48:19 -0600176 // Loop until failure or we don't have whitespace. This
177 // is so the parser doesn't get inundated with whitespace.
Gavin Howard53eba8b2018-10-31 15:14:37 -0600178 do {
179 s = l->next(l);
180 } while (!s && l->t.t == BC_LEX_WHITESPACE);
Gavin Howard364df3b2018-09-28 09:48:19 -0600181
182 return s;
Gavin Howard35753922018-03-21 19:22:08 -0600183}
Gavin Howardc9a9c472018-10-02 17:23:01 -0600184
185BcStatus bc_lex_text(BcLex *l, const char *text) {
186 assert(l && text);
Gavin Howard890d0c02018-10-30 16:34:50 -0600187 l->buf = text;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600188 l->i = 0;
Gavin Howardc9a9c472018-10-02 17:23:01 -0600189 l->len = strlen(text);
190 l->t.t = l->t.last = BC_LEX_INVALID;
191 return bc_lex_next(l);
192}