commit 1bcf48b

zzo38  ·  2024-03-06 23:14:09 +0000 UTC
parent 6704b7a
Implement command-line switches of assembler.
2 files changed,  +25, -11
+8, -0
 1@@ -13,6 +13,14 @@ This is optional, and if specified, then it will write a list of labels
 2 (including unused labels too) and their addresses to this file. (This is a
 3 nonstandard feature.)
 4 
 5+Switches are also allowed if preceding the other arguments:
 6+
 7+ -a will set the starting address to 0x0100.
 8+
 9+ -i followed by a token will parse that token before parsing the input
10+ file. This must be a single token, and not the beginning of a macro or a
11+ comment. This switch may occur multiple times.
12+
13 
14 === Tokens ===
15 
+17, -11
 1@@ -5,6 +5,7 @@ exit
 2 
 3 #include <stdio.h>
 4 #include <string.h>
 5+#include <unistd.h>
 6 
 7 /*
 8 Copyright (c) 2021-2023 Devine Lu Linvega, Andrew Alderwick
 9@@ -918,25 +919,30 @@ int
10 main(int argc, char *argv[])
11 {
12 	FILE *src, *dst;
13-	if(argc==2 && argv[1][0]=='@') printf("Program:%u Macro:%u Label:%u Reference:%u ExpItem:%u\n"
14-	 ,(int)sizeof(Program),(int)sizeof(Macro),(int)sizeof(Label),(int)sizeof(Reference),(int)sizeof(ExpItem));
15-	if(argc < 3)
16-		return !error("usage", "input.tal output.rom [output.nam]");
17-	if(!(src = fopen(argv[1], "r")))
18-		return !error("Invalid input", argv[1]);
19+	int c;
20+	while((c=getopt(argc,argv,"+Yai:"))>0) switch(c) {
21+	  case 'Y': printf("Program:%u Macro:%u Label:%u Reference:%u ExpItem:%u\n"
22+	   ,(int)sizeof(Program),(int)sizeof(Macro),(int)sizeof(Label),(int)sizeof(Reference),(int)sizeof(ExpItem)); break;
23+	  case 'a': p.ptr=0x100; break;
24+	  case 'i': if(!parse(optarg,stdin)) return !error("Assembly", "Cannot parse command-line arguments"); break;
25+	}
26+	if(argc < optind+2)
27+		return !error("usage", "[switches] input.tal output.rom [output.nam]");
28+	if(!(src = fopen(argv[optind], "r")))
29+		return !error("Invalid input", argv[optind]);
30 	if(!assemble(src))
31 		return !error("Assembly", "Failed to assemble rom.");
32-	if(!(dst = fopen(argv[2], "wb")))
33-		return !error("Invalid output", argv[2]);
34+	if(!(dst = fopen(argv[optind+1], "wb")))
35+		return !error("Invalid output", argv[optind+1]);
36 	if(p.length <= TRIM)
37 		error("Assembly", "Output rom is empty.");
38 	else
39 		fwrite(p.data + TRIM, p.length - TRIM, 1, dst);
40 	if(p.xlen) send_expanded(dst);
41-	review(argv[2]);
42-	if(argc>3 && argv[3][0]) {
43+	review(argv[optind+1]);
44+	if(argc>optind+2 && argv[optind+2][0]) {
45 	  fclose(dst);
46-	  if(!(dst=fopen(argv[3],"w"))) return !error("Invalid name output",argv[3]);
47+	  if(!(dst=fopen(argv[optind+2],"w"))) return !error("Invalid name output",argv[3]);
48 	  send_name_output(dst);
49 	}
50 	return 0;