blob: ad69ba59b131497a751d03313460743e6e4c8364 [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 Howard50c8c2d2018-12-27 11:58:34 -070032BcStatus bc_lex_invalidChar(BcLex *l, char c) {
33 l->t = BC_LEX_INVALID;
Gavin Howard03cd1122018-12-31 14:08:15 -070034 return bc_lex_verr(l, BC_ERROR_PARSE_CHAR, c);
Gavin Howard50c8c2d2018-12-27 11:58:34 -070035}
36
Gavin Howarded5c8312018-09-27 12:04:08 -060037void bc_lex_lineComment(BcLex *l) {
Gavin Howardad477312018-12-24 15:51:35 -070038 l->t = BC_LEX_WHITESPACE;
Gavin Howard9a0d2e52018-12-28 13:43:01 -070039 while (l->i < l->len && l->buf[l->i] != '\n') ++l->i;
Gavin Howarded5c8312018-09-27 12:04:08 -060040}
41
Gavin Howard52446f22018-12-13 11:39:22 -070042BcStatus bc_lex_comment(BcLex *l) {
43
44 size_t i, nlines = 0;
45 const char *buf = l->buf;
46 bool end = false;
47 char c;
48
Gavin Howardad477312018-12-24 15:51:35 -070049 l->t = BC_LEX_WHITESPACE;
Gavin Howard52446f22018-12-13 11:39:22 -070050
51 for (i = ++l->i; !end; i += !end) {
52
Gavin Howard1ab22d22019-01-03 13:32:17 -070053 for (; (c = buf[i]) && c != '*'; ++i) nlines += (c == '\n');
Gavin Howard52446f22018-12-13 11:39:22 -070054
Gavin Howard1ab22d22019-01-03 13:32:17 -070055 if (!c || buf[i + 1] == '\0') {
Gavin Howard52446f22018-12-13 11:39:22 -070056 l->i = i;
Gavin Howard03cd1122018-12-31 14:08:15 -070057 return bc_lex_err(l, BC_ERROR_PARSE_COMMENT);
Gavin Howard52446f22018-12-13 11:39:22 -070058 }
59
60 end = buf[i + 1] == '/';
61 }
62
63 l->i = i + 2;
64 l->line += nlines;
65
66 return BC_STATUS_SUCCESS;
67}
68
Gavin Howard364df3b2018-09-28 09:48:19 -060069void bc_lex_whitespace(BcLex *l) {
70 char c;
Gavin Howardad477312018-12-24 15:51:35 -070071 l->t = BC_LEX_WHITESPACE;
Gavin Howard53eba8b2018-10-31 15:14:37 -060072 for (c = l->buf[l->i]; c != '\n' && isspace(c); c = l->buf[++l->i]);
Gavin Howard364df3b2018-09-28 09:48:19 -060073}
74
Gavin Howard63738202018-09-26 15:34:20 -060075BcStatus bc_lex_number(BcLex *l, char start) {
Gavin Howard8a596d42018-01-15 15:46:01 -070076
Gavin Howard53eba8b2018-10-31 15:14:37 -060077 const char *buf = l->buf + l->i;
Gavin Howard8dd307e2019-01-08 23:05:19 -070078 size_t i;
Gavin Howard1ab22d22019-01-03 13:32:17 -070079 char last_valid, c;
Gavin Howard94f14102019-01-11 09:39:57 -070080 bool last_pt, pt = (start == '.');
Gavin Howardf2a40492018-03-05 11:27:29 -070081
Gavin Howardad477312018-12-24 15:51:35 -070082 l->t = BC_LEX_NUMBER;
Gavin Howard196f7db2018-12-29 21:41:54 -070083 last_valid = BC_IS_BC ? 'Z' : 'F';
Gavin Howard07732ec2018-02-27 15:40:02 -070084
Gavin Howardad477312018-12-24 15:51:35 -070085 bc_vec_npop(&l->str, l->str.len);
Gavin Howardad477312018-12-24 15:51:35 -070086 bc_vec_push(&l->str, &start);
Gavin Howarda628aa22018-09-12 13:52:45 -060087
Gavin Howard94f14102019-01-11 09:39:57 -070088 for (i = 0; (c = buf[i]) && (BC_LEX_NUM_CHAR(c, last_valid, pt) ||
Gavin Howard8dd307e2019-01-08 23:05:19 -070089 (c == '\\' && buf[i + 1] == '\n')); ++i)
90 {
Gavin Howard25f60882019-01-11 09:20:51 -070091 if (c == '\\') {
92
93 if (buf[i + 1] == '\n') {
94
95 i += 2;
96
97 // Make sure to eat whitespace at the beginning of the line.
98 while(isspace(buf[i]) && buf[i] != '\n') ++i;
99
100 c = buf[i];
101
Gavin Howard94f14102019-01-11 09:39:57 -0700102 if (!BC_LEX_NUM_CHAR(c, last_valid, pt)) break;
Gavin Howard25f60882019-01-11 09:20:51 -0700103 }
104 else break;
Gavin Howard63738202018-09-26 15:34:20 -0600105 }
Gavin Howard8dd307e2019-01-08 23:05:19 -0700106
Gavin Howard94f14102019-01-11 09:39:57 -0700107 last_pt = (c == '.');
Gavin Howard25f60882019-01-11 09:20:51 -0700108 if (pt && last_pt) break;
Gavin Howard94f14102019-01-11 09:39:57 -0700109 pt = pt || last_pt;
Gavin Howard07732ec2018-02-27 15:40:02 -0700110
Gavin Howardad477312018-12-24 15:51:35 -0700111 bc_vec_push(&l->str, &c);
Gavin Howard63738202018-09-26 15:34:20 -0600112 }
Gavin Howard07732ec2018-02-27 15:40:02 -0700113
Gavin Howard94f14102019-01-11 09:39:57 -0700114 if (l->str.len - pt > BC_MAX_NUM)
Gavin Howard8dd307e2019-01-08 23:05:19 -0700115 return bc_lex_verr(l, BC_ERROR_EXEC_NUM_LEN, BC_MAX_NUM);
116
Gavin Howardad477312018-12-24 15:51:35 -0700117 bc_vec_pushByte(&l->str, '\0');
Gavin Howard53eba8b2018-10-31 15:14:37 -0600118 l->i += i;
Gavin Howard8a596d42018-01-15 15:46:01 -0700119
Gavin Howard63738202018-09-26 15:34:20 -0600120 return BC_STATUS_SUCCESS;
Gavin Howard8a596d42018-01-15 15:46:01 -0700121}
122
Gavin Howard8412ba82018-10-04 13:03:24 -0600123BcStatus bc_lex_name(BcLex *l) {
124
Gavin Howard88c25302018-10-17 13:32:23 -0600125 size_t i = 0;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600126 const char *buf = l->buf + l->i - 1;
Gavin Howard88c25302018-10-17 13:32:23 -0600127 char c = buf[i];
Gavin Howard8412ba82018-10-04 13:03:24 -0600128
Gavin Howardad477312018-12-24 15:51:35 -0700129 l->t = BC_LEX_NAME;
Gavin Howard8412ba82018-10-04 13:03:24 -0600130
Gavin Howard9a4b6cd2018-10-23 15:13:30 -0600131 while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i];
Gavin Howard8412ba82018-10-04 13:03:24 -0600132
Gavin Howardecc7fc22018-12-31 13:21:47 -0700133 if (i > BC_MAX_NAME)
Gavin Howard03cd1122018-12-31 14:08:15 -0700134 return bc_lex_verr(l, BC_ERROR_EXEC_NAME_LEN, BC_MAX_NAME);
Gavin Howard7536dcf2018-12-15 19:27:09 -0700135
Gavin Howardad477312018-12-24 15:51:35 -0700136 bc_vec_string(&l->str, i, buf);
Gavin Howard8412ba82018-10-04 13:03:24 -0600137
138 // Increment the index. We minus 1 because it has already been incremented.
Gavin Howard53eba8b2018-10-31 15:14:37 -0600139 l->i += i - 1;
Gavin Howard8412ba82018-10-04 13:03:24 -0600140
141 return BC_STATUS_SUCCESS;
142}
143
Gavin Howard48354e82019-01-02 18:15:56 -0700144void bc_lex_init(BcLex *l) {
Gavin Howard63738202018-09-26 15:34:20 -0600145 assert(l);
Gavin Howardad477312018-12-24 15:51:35 -0700146 bc_vec_init(&l->str, 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);
Gavin Howardad477312018-12-24 15:51:35 -0700151 bc_vec_free(&l->str);
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;
Gavin Howard7536dcf2018-12-15 19:27:09 -0700157 vm->file = file;
Gavin Howard8a596d42018-01-15 15:46:01 -0700158}
159
Gavin Howard364df3b2018-09-28 09:48:19 -0600160BcStatus bc_lex_next(BcLex *l) {
161
162 BcStatus s;
163
164 assert(l);
165
Gavin Howardad477312018-12-24 15:51:35 -0700166 l->last = l->t;
Gavin Howard7de4f222019-01-10 15:59:08 -0700167 l->line += (l->i != 0 && l->buf[l->i] == '\n');
Gavin Howard7536dcf2018-12-15 19:27:09 -0700168
Gavin Howard03cd1122018-12-31 14:08:15 -0700169 if (l->last == BC_LEX_EOF) return bc_lex_err(l, BC_ERROR_PARSE_EOF);
Gavin Howard364df3b2018-09-28 09:48:19 -0600170
Gavin Howardad477312018-12-24 15:51:35 -0700171 l->t = BC_LEX_EOF;
Gavin Howardc39fd492018-10-04 10:07:03 -0600172
Gavin Howardf9b86ee2018-12-28 13:18:27 -0700173 if (l->i == l->len) return BC_STATUS_SUCCESS;
Gavin Howard364df3b2018-09-28 09:48:19 -0600174
Gavin Howard364df3b2018-09-28 09:48:19 -0600175 // Loop until failure or we don't have whitespace. This
176 // is so the parser doesn't get inundated with whitespace.
Gavin Howard53eba8b2018-10-31 15:14:37 -0600177 do {
Gavin Howard48354e82019-01-02 18:15:56 -0700178 s = vm->next(l);
Gavin Howardad477312018-12-24 15:51:35 -0700179 } while (!s && l->t == BC_LEX_WHITESPACE);
Gavin Howard364df3b2018-09-28 09:48:19 -0600180
181 return s;
Gavin Howard35753922018-03-21 19:22:08 -0600182}
Gavin Howardc9a9c472018-10-02 17:23:01 -0600183
184BcStatus bc_lex_text(BcLex *l, const char *text) {
185 assert(l && text);
Gavin Howard890d0c02018-10-30 16:34:50 -0600186 l->buf = text;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600187 l->i = 0;
Gavin Howardc9a9c472018-10-02 17:23:01 -0600188 l->len = strlen(text);
Gavin Howardad477312018-12-24 15:51:35 -0700189 l->t = l->last = BC_LEX_INVALID;
Gavin Howardc9a9c472018-10-02 17:23:01 -0600190 return bc_lex_next(l);
191}