aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-05-03 21:43:26 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-05-03 21:43:26 +0100
commite02cf21e0fc4d138c04001b1463e680faec60fe7 (patch)
tree62b16dd49f8b78201a0a59a383a1d6b4c3c074ef
parent180c71c6ae8a4e6ffdc18249c8e1bc52d151563a (diff)
downloadtr8vm-e02cf21e0fc4d138c04001b1463e680faec60fe7.tar.gz
tr8vm-e02cf21e0fc4d138c04001b1463e680faec60fe7.zip
Add .include directive
-rw-r--r--README.md3
-rw-r--r--tr8as.c52
2 files changed, 54 insertions, 1 deletions
diff --git a/README.md b/README.md
index fe9597b..f158f2e 100644
--- a/README.md
+++ b/README.md
@@ -228,6 +228,9 @@ Literal word (16-bit value).
.equ label imm
Define a label assigning an arbitrary immediate.
+.include "filename"
+Assemble the file at current position.
+
.incbin "filename"
Read the file and add the content to the output.
diff --git a/tr8as.c b/tr8as.c
index 5b525ba..30beda6 100644
--- a/tr8as.c
+++ b/tr8as.c
@@ -84,7 +84,7 @@ typedef struct
typedef struct
{
- char id[7];
+ char id[9];
uint8_t (*parse)(As *, char *);
} InstParse;
@@ -382,6 +382,55 @@ static uint8_t parse_org(As *as, char *c)
return 1;
}
+static uint8_t parse(As *as, char *c);
+
+static uint8_t parse_include(As *as, char *c)
+{
+ /* XXX: may be support longer filenames */
+ char word[MAX_ID + 1];
+ uint8_t wlen;
+ char line[MAX_LINE];
+ Location old;
+ FILE *fd;
+ uint8_t rc;
+
+ /* .include "filename" */
+
+ next_string(c, word, &wlen);
+ if (wlen == 0)
+ return error_l("Syntax error", &as->loc, "expected string");
+
+ fd = fopen(word, "rb");
+ if (!fd)
+ {
+ error_l("Failed to open include", &as->loc, word);
+ return 1;
+ }
+
+ old.filename = as->loc.filename;
+ old.line = as->loc.line;
+
+ as->loc.filename = word;
+ as->loc.line = 0;
+
+ while (fgets(line, MAX_LINE - 1, fd))
+ {
+ as->loc.line++;
+ if ((rc = parse(as, line)) == 0)
+ break;
+ }
+
+ fclose(fd);
+
+ as->loc.filename = old.filename;
+ as->loc.line = old.line;
+
+ if (!rc)
+ return error_l("Error in include", &as->loc, word);
+
+ return 1;
+}
+
static uint8_t parse_incbin(As *as, char *c)
{
/* XXX: may be support longer filenames */
@@ -1262,6 +1311,7 @@ static uint8_t parse_ld(As *as, char *c)
}
static InstParse insts[] = {
+ { ".include", parse_include },
{ ".incbin", parse_incbin },
{ ".org", parse_org },
{ ".equ", parse_equ },