From e02cf21e0fc4d138c04001b1463e680faec60fe7 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Wed, 3 May 2023 21:43:26 +0100 Subject: Add .include directive --- tr8as.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) (limited to 'tr8as.c') 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 }, -- cgit v1.2.3