blob: 7a1a7003438e1c8a725cd13a291fe70db18ca0e4 [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;
34 while (l->idx < l->len && l->buffer[l->idx++] != '\n');
35 --l->idx;
36}
37
Gavin Howard364df3b2018-09-28 09:48:19 -060038void 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 Howard63738202018-09-26 15:34:20 -060044BcStatus bc_lex_number(BcLex *l, char start) {
Gavin Howard8a596d42018-01-15 15:46:01 -070045
Gavin Howard63738202018-09-26 15:34:20 -060046 BcStatus s;
47 const char *buf = l->buffer + l->idx;
Gavin Howardc39fd492018-10-04 10:07:03 -060048 size_t len, hits = 0, bslashes = 0, i = 0, j;
Gavin Howard63738202018-09-26 15:34:20 -060049 char c = buf[i];
50 bool last_pt, pt = start == '.';
Gavin Howardf2a40492018-03-05 11:27:29 -070051
Gavin Howard63738202018-09-26 15:34:20 -060052 last_pt = pt;
53 l->t.t = BC_LEX_NUMBER;
Gavin Howard07732ec2018-02-27 15:40:02 -070054
Gavin Howard63738202018-09-26 15:34:20 -060055 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 Howard07732ec2018-02-27 15:40:02 -070066
Gavin Howard63738202018-09-26 15:34:20 -060067 c = buf[++i];
68 }
Gavin Howard07732ec2018-02-27 15:40:02 -070069
Gavin Howard63738202018-09-26 15:34:20 -060070 len = i + 1 * !last_pt - bslashes * 2;
71 if (len > BC_MAX_NUM) return BC_STATUS_EXEC_NUM_LEN;
Gavin Howard07732ec2018-02-27 15:40:02 -070072
Gavin Howard63738202018-09-26 15:34:20 -060073 bc_vec_npop(&l->t.v, l->t.v.len);
Gavin Howard0275a552018-09-27 12:08:23 -060074 if ((s = bc_vec_expand(&l->t.v, len + 1))) return s;
Gavin Howard90198862018-09-29 03:37:47 -060075 if ((s = bc_vec_push(&l->t.v, &start))) return s;
Gavin Howarda628aa22018-09-12 13:52:45 -060076
Gavin Howardc39fd492018-10-04 10:07:03 -060077 for (buf -= 1, j = 1; j < len + hits * 2; ++j) {
Gavin Howard07732ec2018-02-27 15:40:02 -070078
Gavin Howard63738202018-09-26 15:34:20 -060079 c = buf[j];
Gavin Howard07732ec2018-02-27 15:40:02 -070080
Gavin Howard63738202018-09-26 15:34:20 -060081 // If we have hit a backslash, skip it. We don't have
82 // to check for a newline because it's guaranteed.
83 if (hits < bslashes && c == '\\') {
84 ++hits;
85 ++j;
86 continue;
87 }
Gavin Howard07732ec2018-02-27 15:40:02 -070088
Gavin Howard90198862018-09-29 03:37:47 -060089 if ((s = bc_vec_push(&l->t.v, &c))) return s;
Gavin Howard63738202018-09-26 15:34:20 -060090 }
Gavin Howard07732ec2018-02-27 15:40:02 -070091
Gavin Howard63738202018-09-26 15:34:20 -060092 if ((s = bc_vec_pushByte(&l->t.v, '\0'))) return s;
93 l->idx += i;
Gavin Howard8a596d42018-01-15 15:46:01 -070094
Gavin Howard63738202018-09-26 15:34:20 -060095 return BC_STATUS_SUCCESS;
Gavin Howard8a596d42018-01-15 15:46:01 -070096}
97
Gavin Howard8412ba82018-10-04 13:03:24 -060098BcStatus bc_lex_name(BcLex *l) {
99
100 BcStatus s;
Gavin Howard88c25302018-10-17 13:32:23 -0600101 size_t i = 0;
Gavin Howard8412ba82018-10-04 13:03:24 -0600102 const char *buf = l->buffer + l->idx - 1;
Gavin Howard88c25302018-10-17 13:32:23 -0600103 char c = buf[i];
Gavin Howard8412ba82018-10-04 13:03:24 -0600104
105 l->t.t = BC_LEX_NAME;
Gavin Howard8412ba82018-10-04 13:03:24 -0600106
Gavin Howard9a4b6cd2018-10-23 15:13:30 -0600107 while ((c >= 'a' && c <= 'z') || isdigit(c) || c == '_') c = buf[++i];
Gavin Howard8412ba82018-10-04 13:03:24 -0600108
109 if (i > BC_MAX_STRING) return BC_STATUS_EXEC_NAME_LEN;
110 if ((s = bc_vec_string(&l->t.v, i, buf))) return s;
111
112 // Increment the index. We minus 1 because it has already been incremented.
113 l->idx += i - 1;
114
115 return BC_STATUS_SUCCESS;
116}
117
Gavin Howarded5c8312018-09-27 12:04:08 -0600118BcStatus bc_lex_init(BcLex *l, BcLexNext next) {
Gavin Howard63738202018-09-26 15:34:20 -0600119 assert(l);
Gavin Howarded5c8312018-09-27 12:04:08 -0600120 l->next = next;
Gavin Howard63738202018-09-26 15:34:20 -0600121 return bc_vec_init(&l->t.v, sizeof(uint8_t), NULL);
Gavin Howard69185042018-09-10 15:46:20 -0600122}
123
Gavin Howard63738202018-09-26 15:34:20 -0600124void bc_lex_free(BcLex *l) {
125 assert(l);
126 bc_vec_free(&l->t.v);
Gavin Howard69185042018-09-10 15:46:20 -0600127}
128
Gavin Howard63738202018-09-26 15:34:20 -0600129void bc_lex_file(BcLex *l, const char *file) {
130 assert(l && file);
131 l->line = 1;
132 l->newline = false;
Gavin Howardc39fd492018-10-04 10:07:03 -0600133 l->f = file;
Gavin Howard8a596d42018-01-15 15:46:01 -0700134}
135
Gavin Howard364df3b2018-09-28 09:48:19 -0600136BcStatus bc_lex_next(BcLex *l) {
137
138 BcStatus s;
139
140 assert(l);
141
Gavin Howard6d632e92018-09-28 11:26:23 -0600142 if ((l->t.last = l->t.t) == BC_LEX_EOF) return BC_STATUS_LEX_EOF;
Gavin Howard364df3b2018-09-28 09:48:19 -0600143
Gavin Howardc39fd492018-10-04 10:07:03 -0600144 l->line += l->newline;
Gavin Howard9a4b6cd2018-10-23 15:13:30 -0600145 l->t.t = BC_LEX_EOF;
Gavin Howardc39fd492018-10-04 10:07:03 -0600146
Gavin Howard9a4b6cd2018-10-23 15:13:30 -0600147 if ((l->newline = (l->idx == l->len))) return BC_STATUS_SUCCESS;
Gavin Howard364df3b2018-09-28 09:48:19 -0600148
Gavin Howard364df3b2018-09-28 09:48:19 -0600149 // Loop until failure or we don't have whitespace. This
150 // is so the parser doesn't get inundated with whitespace.
Gavin Howard48641a52018-09-28 11:37:47 -0600151 while (!(s = l->next(l)) && l->t.t == BC_LEX_WHITESPACE);
Gavin Howard364df3b2018-09-28 09:48:19 -0600152
153 return s;
Gavin Howard35753922018-03-21 19:22:08 -0600154}
Gavin Howardc9a9c472018-10-02 17:23:01 -0600155
156BcStatus bc_lex_text(BcLex *l, const char *text) {
157 assert(l && text);
158 l->buffer = text;
159 l->idx = 0;
160 l->len = strlen(text);
161 l->t.t = l->t.last = BC_LEX_INVALID;
162 return bc_lex_next(l);
163}