commit 580a89c
Devine Lu Linvega
·
2024-11-14 23:59:33 +0000 UTC
parent fca4234
(uxnasm) Expanded condensed functions
1 files changed,
+70,
-22
+70,
-22
1@@ -14,6 +14,15 @@ WITH REGARD TO THIS SOFTWARE.
2 /* clang-format off */
3
4 #define PAGE 0x0100
5+#define ishex(x) (shex(x) >= 0)
6+#define isopc(x) (findopcode(x) || scmp(x, "BRK", 4))
7+#define isinvalid(x) (!x[0] || ishex(x) || isopc(x) || find(runes, x[0]) >= 0)
8+#define writeshort(x) (writebyte(x >> 8, ctx) && writebyte(x & 0xff, ctx))
9+#define findlabel(x) finditem(x, labels, labels_len)
10+#define findmacro(x) finditem(x, macros, macro_len)
11+#define error_top(id, msg) !printf("%s: %s\n", id, msg)
12+#define error_asm(id) !printf("%s: %s in @%s, %s:%d.\n", id, token, scope, ctx->path, ctx->line)
13+#define error_ref(id) !printf("%s: %s, %s:%d\n", id, r->name, r->data, r->line)
14
15 typedef unsigned char Uint8;
16 typedef signed char Sint8;
17@@ -37,26 +46,66 @@ static char ops[][4] = {
18 "ADD", "SUB", "MUL", "DIV", "AND", "ORA", "EOR", "SFT"
19 };
20
21-static int find(char *s, char t) { int i = 0; char c; while((c = *s++)) { if(c == t) return i; i++; } return -1; }
22-static int shex(char *s) { int d, n = 0; char c; while((c = *s++)) { d = find(hexad, c); if(d < 0) return d; n = n << 4, n |= d; } return n; }
23-static int scmp(char *a, char *b, int len) { int i = 0; while(a[i] == b[i]) if(!a[i] || ++i >= len) return 1; return 0; }
24-static char *copy(char *src, char *dst, char c) { while(*src && *src != c) *dst++ = *src++; *dst++ = 0; return dst; }
25-static char *save(char *s, char c) { char *o = dictnext; while((*dictnext++ = *s++) && *s); *dictnext++ = c; return o; }
26-static char *join(char *a, char j, char *b) { char *res = dictnext; save(a, j), save(b, 0); return res; }
27+/* clang-format on */
28
29-#define ishex(x) (shex(x) >= 0)
30-#define isopc(x) (findopcode(x) || scmp(x, "BRK", 4))
31-#define isinvalid(x) (!x[0] || ishex(x) || isopc(x) || find(runes, x[0]) >= 0)
32-#define writeshort(x) (writebyte(x >> 8, ctx) && writebyte(x & 0xff, ctx))
33-#define findlabel(x) finditem(x, labels, labels_len)
34-#define findmacro(x) finditem(x, macros, macro_len)
35-#define error_top(id, msg) !printf("%s: %s\n", id, msg)
36-#define error_asm(id) !printf("%s: %s in @%s, %s:%d.\n", id, token, scope, ctx->path, ctx->line)
37-#define error_ref(id) !printf("%s: %s, %s:%d\n", id, r->name, r->data, r->line)
38+static int
39+find(char *s, char t)
40+{
41+ int i = 0;
42+ char c;
43+ while((c = *s++)) {
44+ if(c == t) return i;
45+ i++;
46+ }
47+ return -1;
48+}
49
50-/* clang-format on */
51+static int
52+shex(char *s)
53+{
54+ int d, n = 0;
55+ char c;
56+ while((c = *s++)) {
57+ d = find(hexad, c);
58+ if(d < 0) return d;
59+ n = n << 4, n |= d;
60+ }
61+ return n;
62+}
63
64-static int parse(char *w, FILE *f, Context *ctx);
65+static int
66+scmp(char *a, char *b, int len)
67+{
68+ int i = 0;
69+ while(a[i] == b[i])
70+ if(!a[i] || ++i >= len) return 1;
71+ return 0;
72+}
73+
74+static char *
75+copy(char *src, char *dst, char c)
76+{
77+ while(*src && *src != c) *dst++ = *src++;
78+ *dst++ = 0;
79+ return dst;
80+}
81+
82+static char *
83+save(char *s, char c)
84+{
85+ char *o = dictnext;
86+ while((*dictnext++ = *s++) && *s);
87+ *dictnext++ = c;
88+ return o;
89+}
90+
91+static char *
92+join(char *a, char j, char *b)
93+{
94+ char *res = dictnext;
95+ save(a, j), save(b, 0);
96+ return res;
97+}
98
99 static char *
100 push(char *s, char c)
101@@ -119,6 +168,8 @@ walkcomment(FILE *f, Context *ctx)
102 return error_asm("Comment incomplete");
103 }
104
105+static int parse(char *w, FILE *f, Context *ctx);
106+
107 static int
108 walkmacro(Item *m, Context *ctx)
109 {
110@@ -415,10 +466,7 @@ main(int argc, char *argv[])
111 {
112 ptr = PAGE;
113 copy("on-reset", scope, 0);
114- if(argc == 2 && scmp(argv[1], "-v", 2)) return !printf("Uxnasm - Uxntal Assembler, 29 Aug 2024.\n");
115+ if(argc == 2 && scmp(argv[1], "-v", 2)) return !printf("Uxnasm - Uxntal Assembler, 14 Nov 2024.\n");
116 if(argc != 3) return error_top("usage", "uxnasm [-v] input.tal output.rom");
117- if(!assemble(argv[1])) return 1;
118- if(!resolve(argv[2])) return 1;
119- if(!build(argv[2])) return 1;
120- return 0;
121+ return !assemble(argv[1]) || !resolve(argv[2]) || !build(argv[2]);
122 }