commit e54844c13e003ffc1e0b25fc806da61b0bea843a
parent 5df1c5b29ed3579abf5de5c9f07d2fcd9ed30f8f
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Thu, 9 Dec 2021 14:25:16 -0600
lex: split into separate file
Diffstat:
M | Makefile | | | 4 | ++-- |
A | lex.c | | | 126 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | lex.h | | | 1 | + |
M | main.c | | | 123 | +------------------------------------------------------------------------------ |
4 files changed, 130 insertions(+), 124 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,8 +1,8 @@
.c.o:
$(CC) -c $< -o $@
-nooc: main.o array.o util.o x64.o elf.o
- $(CC) main.o array.o x64.o util.o elf.o -o nooc
+nooc: main.o array.o util.o x64.o elf.o lex.o
+ $(CC) main.o array.o x64.o util.o elf.o lex.o -o nooc
clean:
rm -f *.o nooc
diff --git a/lex.c b/lex.c
@@ -0,0 +1,126 @@
+#include <ctype.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include "nooc.h"
+#include "util.h"
+#include "lex.h"
+
+#define ADVANCE(n) \
+ start.data += (n) ; \
+ start.len -= (n) ; \
+ col += (n) ;
+
+struct token *
+lex(struct slice start)
+{
+ size_t line = 1;
+ size_t col = 1;
+ struct token *head = calloc(1, sizeof(struct token));
+ if (!head)
+ return NULL;
+
+ struct token *cur = head;
+ while (start.len) {
+ if (isblank(*start.data)) {
+ ADVANCE(1);
+ continue;
+ }
+
+ if (*start.data == '\n') {
+ ADVANCE(1);
+ line += 1;
+ col = 1;
+ continue;
+ }
+
+ cur->line = line;
+ cur->col = col;
+
+ if (slice_cmplit(&start, "if") == 0) {
+ cur->type = TOK_IF;
+ ADVANCE(2);
+ } else if (slice_cmplit(&start, "let") == 0) {
+ cur->type = TOK_LET;
+ ADVANCE(3);
+ } else if (slice_cmplit(&start, "else") == 0) {
+ cur->type = TOK_ELSE;
+ ADVANCE(4);
+ } else if (slice_cmplit(&start, "loop") == 0) {
+ cur->type = TOK_LOOP;
+ ADVANCE(4);
+ } else if (slice_cmplit(&start, "return") == 0) {
+ cur->type = TOK_RETURN;
+ ADVANCE(6);
+ } else if (*start.data == '>') {
+ cur->type = TOK_GREATER;
+ ADVANCE(1);
+ } else if (*start.data == ',') {
+ cur->type = TOK_COMMA;
+ ADVANCE(1);
+ } else if (*start.data == '(') {
+ cur->type = TOK_LPAREN;
+ ADVANCE(1);
+ } else if (*start.data == ')') {
+ cur->type = TOK_RPAREN;
+ ADVANCE(1);
+ } else if (*start.data == '{') {
+ cur->type = TOK_LCURLY;
+ ADVANCE(1);
+ } else if (*start.data == '}') {
+ cur->type = TOK_RCURLY;
+ ADVANCE(1);
+ } else if (isdigit(*start.data)) {
+ cur->slice.data = start.data;
+ cur->slice.len = 1;
+ ADVANCE(1);
+ cur->type = TOK_NUM;
+ while (isdigit(*start.data)) {
+ ADVANCE(1);
+ cur->slice.len++;
+ }
+ } else if (*start.data == '"') {
+ ADVANCE(1);
+ cur->slice.data = start.data;
+ cur->type = TOK_STRING;
+ while (*start.data != '"') {
+ ADVANCE(1);
+ cur->slice.len++;
+ }
+
+ ADVANCE(1);
+ } else if (*start.data == '+') {
+ cur->type = TOK_PLUS;
+ ADVANCE(1);
+ } else if (*start.data == '-') {
+ cur->type = TOK_MINUS;
+ ADVANCE(1);
+ } else if (*start.data == '=') {
+ cur->type = TOK_EQUAL;
+ ADVANCE(1);
+ } else if (isalpha(*start.data)) {
+ cur->type = TOK_NAME;
+ cur->slice.data = start.data;
+ cur->slice.len = 1;
+ ADVANCE(1);
+ while (isalnum(*start.data)) {
+ ADVANCE(1);
+ cur->slice.len++;
+ }
+ } else {
+ error("invalid token", line, col);
+ }
+
+ cur->next = calloc(1, sizeof(struct token));
+
+ if (!cur->next)
+ die("lex: failed to allocate next token");
+
+ cur = cur->next;
+ }
+
+ cur->line = line;
+ cur->col = col;
+
+ return head;
+}
diff --git a/lex.h b/lex.h
@@ -0,0 +1 @@
+struct token *lex(struct slice start);
diff --git a/main.c b/main.c
@@ -16,6 +16,7 @@
#include "nooc.h"
#include "util.h"
#include "elf.h"
+#include "lex.h"
struct decls decls;
struct assgns assgns;
@@ -23,128 +24,6 @@ struct exprs exprs;
char *infile;
-#define ADVANCE(n) \
- start.data += (n) ; \
- start.len -= (n) ; \
- col += (n) ;
-
-struct token *
-lex(struct slice start)
-{
- size_t line = 1;
- size_t col = 1;
- struct token *head = calloc(1, sizeof(struct token));
- if (!head)
- return NULL;
-
- struct token *cur = head;
- while (start.len) {
- if (isblank(*start.data)) {
- ADVANCE(1);
- continue;
- }
-
- if (*start.data == '\n') {
- ADVANCE(1);
- line += 1;
- col = 1;
- continue;
- }
-
- cur->line = line;
- cur->col = col;
-
- if (slice_cmplit(&start, "if") == 0) {
- cur->type = TOK_IF;
- ADVANCE(2);
- } else if (slice_cmplit(&start, "let") == 0) {
- cur->type = TOK_LET;
- ADVANCE(3);
- } else if (slice_cmplit(&start, "else") == 0) {
- cur->type = TOK_ELSE;
- ADVANCE(4);
- } else if (slice_cmplit(&start, "loop") == 0) {
- cur->type = TOK_LOOP;
- ADVANCE(4);
- } else if (slice_cmplit(&start, "return") == 0) {
- cur->type = TOK_RETURN;
- ADVANCE(6);
- } else if (*start.data == '>') {
- cur->type = TOK_GREATER;
- ADVANCE(1);
- } else if (*start.data == ',') {
- cur->type = TOK_COMMA;
- ADVANCE(1);
- } else if (*start.data == '(') {
- cur->type = TOK_LPAREN;
- ADVANCE(1);
- } else if (*start.data == ')') {
- cur->type = TOK_RPAREN;
- ADVANCE(1);
- } else if (*start.data == '{') {
- cur->type = TOK_LCURLY;
- ADVANCE(1);
- } else if (*start.data == '}') {
- cur->type = TOK_RCURLY;
- ADVANCE(1);
- } else if (isdigit(*start.data)) {
- cur->slice.data = start.data;
- cur->slice.len = 1;
- ADVANCE(1);
- cur->type = TOK_NUM;
- while (isdigit(*start.data)) {
- ADVANCE(1);
- cur->slice.len++;
- }
- } else if (*start.data == '"') {
- ADVANCE(1);
- cur->slice.data = start.data;
- cur->type = TOK_STRING;
- while (*start.data != '"') {
- ADVANCE(1);
- cur->slice.len++;
- }
-
- ADVANCE(1);
- } else if (*start.data == '+') {
- cur->type = TOK_PLUS;
- ADVANCE(1);
- } else if (*start.data == '-') {
- cur->type = TOK_MINUS;
- ADVANCE(1);
- } else if (*start.data == '=') {
- cur->type = TOK_EQUAL;
- ADVANCE(1);
- } else if (isalpha(*start.data)) {
- cur->type = TOK_NAME;
- cur->slice.data = start.data;
- cur->slice.len = 1;
- ADVANCE(1);
- while (isalnum(*start.data)) {
- ADVANCE(1);
- cur->slice.len++;
- }
- } else {
- error("invalid token", line, col);
- }
-
- cur->next = calloc(1, sizeof(struct token));
-
- // FIXME: handle this properly
- if (!cur->next)
- return NULL;
-
- cur = cur->next;
- }
-
- cur->line = line;
- cur->col = col;
-
- return head;
-}
-
-#undef ADVANCE
-
struct data data_seg;
void