summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2019-08-04 00:20:08 +1200
committerDavid Phillips <david@sighup.nz>2019-08-04 12:01:13 +1200
commit8f182d5db075f4f4b939725065596f49bbd0d0d4 (patch)
treed140dfb27b57743ee860d74aff8fa76a46328899
parent9aa02bebf295ce9436451e0ce85db7717a6c9f81 (diff)
downloadtoy-cpu-assembler-8f182d5db075f4f4b939725065596f49bbd0d0d4.tar.xz
lex: empty file is equivalent to EOL
lex() returning NULL is used to convey an error case but having not allocated any tokens in the case of an empty input file isn't an error case. This patch causes lex to treat an empty token stream after successfully examining a file as just a single EOL token. This is a fair approximation of an empty file for this assembler's purposes, and results in the correct behaviour of an empty output file.
-rw-r--r--assembler.c11
-rw-r--r--lex.c7
-rw-r--r--test/emul/001-empty.asm0
-rw-r--r--test/full-pipeline/010-empty.asm0
4 files changed, 18 insertions, 0 deletions
diff --git a/assembler.c b/assembler.c
index 9ffdb85..7ae699d 100644
--- a/assembler.c
+++ b/assembler.c
@@ -7,6 +7,9 @@
#include "instruction.h"
#include "output/output_bin.h"
+//#define DEBUG
+#include "debug.h"
+
void print_help(const char *argv0)
{
fprintf(stderr, "Syntax: %s <in.asm> <out.bin>\n", argv0);
@@ -45,12 +48,14 @@ int main(int argc, char **argv)
perror("fopen");
return error_ret;
}
+ debug("Opened fin\n");
if ((fout = fopen(path_out, "wb")) == NULL) {
fprintf(stderr, "Error opening %s: ", path_out);
perror("fopen");
return error_ret;
}
+ debug("Opened fout\n");
/****/
struct token *tokens = NULL;
size_t tok_count = 0;
@@ -58,6 +63,8 @@ int main(int argc, char **argv)
if ((tokens = lex(path_in, fin, &tok_count)) == NULL)
return error_ret;
+ debug("Lexed.\n");
+
/* FIXME package these things into `tok_result`, `parse_result` etc */
struct instruction *insts;
size_t insts_count;
@@ -66,6 +73,8 @@ int main(int argc, char **argv)
if ((ret = parse(path_in, fin, &labels, &labels_count, tokens, tok_count, &insts, &insts_count)))
return error_ret && ret;
+ debug("Parsed.\n");
+
/* FIXME insert pass for sanity checking identifiers, sizes of values */
/* FIXME insert optional pass for optimisation */
@@ -73,5 +82,7 @@ int main(int argc, char **argv)
if ((ret = output_bin(fout, labels, labels_count, insts, insts_count)))
return error_ret && ret;
+ debug("Output.\n");
+
return 0;
}
diff --git a/lex.c b/lex.c
index 6fe78b3..949425e 100644
--- a/lex.c
+++ b/lex.c
@@ -406,6 +406,13 @@ struct token* lex(const char *filename_local, FILE *fin, size_t *len)
return NULL;
}
+ /* no tokens? just an EOL then */
+ if (tokens_count == 0) {
+ struct token eol = {0};
+ lex_eol(&eol);
+ add_token(eol);
+ }
+
*len = tokens_count;
return tokens;
}
diff --git a/test/emul/001-empty.asm b/test/emul/001-empty.asm
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/emul/001-empty.asm
diff --git a/test/full-pipeline/010-empty.asm b/test/full-pipeline/010-empty.asm
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/full-pipeline/010-empty.asm