aboutsummaryrefslogtreecommitdiff
path: root/tr8as.c
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 /tr8as.c
parent180c71c6ae8a4e6ffdc18249c8e1bc52d151563a (diff)
downloadtr8vm-e02cf21e0fc4d138c04001b1463e680faec60fe7.tar.gz
tr8vm-e02cf21e0fc4d138c04001b1463e680faec60fe7.zip
Add .include directive
Diffstat (limited to 'tr8as.c')
-rw-r--r--tr8as.c52
1 files changed, 51 insertions, 1 deletions
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 },