summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2019-04-14 20:55:10 +1200
committerDavid Phillips <david@sighup.nz>2019-08-03 12:43:46 +1200
commitdad8bb1e347ad293d99e374e24740f207208a20d (patch)
treef65ce5d656d2e21a0da0c57ec4d84a4cf522b47e
parent4fc18fe18c7251be732dc375eff21deb70065d3e (diff)
downloadtoy-cpu-assembler-dad8bb1e347ad293d99e374e24740f207208a20d.tar.xz
Add support for // /**/ style comments
-rw-r--r--lex.c36
1 files changed, 32 insertions, 4 deletions
diff --git a/lex.c b/lex.c
index c899fd7..8bc6640 100644
--- a/lex.c
+++ b/lex.c
@@ -12,6 +12,7 @@
static const char *filename = NULL;
static size_t line;
static size_t column;
+static int context_comment;
static struct token* tokens;
static size_t tokens_count;
static char buffer[1024]; /* XXX limitation: sources must have lines < 1024 bytes */
@@ -285,6 +286,20 @@ int lex_line(void) {
struct token tok;
while (column < len) {
+ /* special case: are we still inside a block comment? */
+ if (context_comment) {
+ if ( column + 1 < len
+ && buffer[column] == '*'
+ && buffer[column + 1] == '/') {
+ context_comment = 0;
+ column += 2;
+ continue;
+ }
+ column++;
+ continue;
+ }
+
+ /* fallthrough: outside a comment */
memset(&tok, 0, sizeof(tok));
store_location(&tok);
switch (buffer[column]) {
@@ -298,12 +313,19 @@ int lex_line(void) {
case '\t':
eat_whitespace();
continue;
- /*
case '/':
- FIXME look ahead * or /
- eat_block_comment();
+ if (column + 1 < len && buffer[column + 1] == '*') {
+ column += 2;
+ context_comment = 1;
+ continue;
+ } else if (column + 1 < len && buffer[column + 1] == '/') {
+ ret = lex_eol(&tok);
+ column += 2;
+ return add_token(tok);
+ } else {
+ emit("Unexpected '/'\n");
+ }
break;
- */
case ',':
ret = lex_comma(&tok);
break;
@@ -346,6 +368,7 @@ struct token* lex(const char *filename_local, FILE *fin, size_t *len)
line = 0;
tokens = NULL;
tokens_count = 0;
+ context_comment = 0;
while (fgets(buffer, sizeof(buffer), fin)) {
column = 0;
@@ -359,6 +382,11 @@ struct token* lex(const char *filename_local, FILE *fin, size_t *len)
return NULL;
}
+ if (context_comment) {
+ emit("Error: unexpected EOF while still inside block comment\n");
+ return NULL;
+ }
+
*len = tokens_count;
return tokens;
}