diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-05-03 21:43:26 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-05-03 21:43:26 +0100 |
commit | e02cf21e0fc4d138c04001b1463e680faec60fe7 (patch) | |
tree | 62b16dd49f8b78201a0a59a383a1d6b4c3c074ef | |
parent | 180c71c6ae8a4e6ffdc18249c8e1bc52d151563a (diff) | |
download | tr8vm-e02cf21e0fc4d138c04001b1463e680faec60fe7.tar.gz tr8vm-e02cf21e0fc4d138c04001b1463e680faec60fe7.zip |
Add .include directive
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | tr8as.c | 52 |
2 files changed, 54 insertions, 1 deletions
@@ -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. @@ -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 }, |