commit f67f7f40616e0526bb00467d3ca0d5459504bff4
parent e7a1268b5c7412c80fbeb684403ea5a44eaa9b53
Author: Brian Swetland <swetland@frotz.net>
Date: Thu, 12 Oct 2023 18:13:39 -0700
compiler0: more robust and uniform namespacing
Types are now t$<name>
Array Types are now t$<name>$<size>
Constants are still c$<name>
Variables are still v$<name>
Diffstat:
4 files changed, 32 insertions(+), 32 deletions(-)
diff --git a/compiler0.c b/compiler0.c
@@ -944,7 +944,7 @@ void parse_primary_expr(void) {
require(tOPAREN);
String *typename = parse_name("type name");
require(tCPAREN);
- emit_impl("calloc(1,sizeof(%s_t))", typename->text);
+ emit_impl("calloc(1,sizeof(t$%s))", typename->text);
return;
} else if (ctx.tok == tIDN) {
parse_ident();
@@ -1058,8 +1058,8 @@ Type *parse_struct_type(String *name) {
Type *rectype = type_make(name, TYPE_STRUCT, nil, nil, 0);
scope_push(SCOPE_STRUCT);
require(tOBRACE);
- emit_type("typedef struct %s_t %s_t;\n", name->text, name->text);
- emit_type("struct %s_t {\n", name->text);
+ emit_type("typedef struct t$%s t$%s;\n", name->text, name->text);
+ emit_type("struct t$%s {\n", name->text);
while (true) {
if (ctx.tok == tCBRACE) {
next();
@@ -1069,7 +1069,7 @@ Type *parse_struct_type(String *name) {
bool ptr = (ctx.tok == tSTAR);
if (ptr) next();
Type *type = parse_type(false);
- emit_type(" %s_t %s%s;\n", type->name->text, ptr ? "*" : "", fname->text);
+ emit_type(" t$%s %s%s;\n", type->name->text, ptr ? "*" : "", fname->text);
Symbol *sym = symbol_make(fname, type);
sym->kind = ptr ? SYMBOL_PTR : SYMBOL_FLD;
if (ctx.tok != tCBRACE) {
@@ -1098,9 +1098,9 @@ Type *parse_array_type(void) {
}
// TODO: slices
char tmp[256];
- sprintf(tmp, "%s_a%u", type->of->name->text, nelem);
+ sprintf(tmp, "%s$%u", type->of->name->text, nelem);
type->name = string_make(tmp, strlen(tmp));
- emit_type("typedef %s_t %s_t[%u];\n", type->of->name->text, type->name->text, nelem);
+ emit_type("typedef t$%s t$%s[%u];\n", type->of->name->text, type->name->text, nelem);
return type;
}
@@ -1278,19 +1278,19 @@ void parse_var(void) {
if (ctx.tok == tOBRACE) {
next();
if (type->kind == TYPE_STRUCT) {
- emit_impl("%s_t $$%s = {\n", type->name->text, name->text);
+ emit_impl("t$%s $$%s = {\n", type->name->text, name->text);
parse_struct_init(var);
- emit_impl("\n};\n%s_t *$%s = &$$%s;\n",
+ emit_impl("\n};\nt$%s *$%s = &$$%s;\n",
type->name->text, name->text, name->text);
} else if (type->kind == TYPE_ARRAY) {
- emit_impl("%s_t $%s = {\n", type->name->text, name->text);
+ emit_impl("t$%s $%s = {\n", type->name->text, name->text);
parse_array_init(var);
emit_impl("\n};\n");
} else {
error("type %s cannot be initialized with {} expr", type->name->text);
}
} else {
- emit_impl("%s_t %s$%s = ", type->name->text,
+ emit_impl("t$%s %s$%s = ", type->name->text,
(type->kind == TYPE_STRUCT) ? "*" : "",
name->text);
parse_expr();
@@ -1298,9 +1298,9 @@ void parse_var(void) {
}
} else {
if (type->kind == TYPE_ARRAY) {
- emit_impl("%s_t $%s = { 0, };\n", type->name->text, name->text);
+ emit_impl("t$%s $%s = { 0, };\n", type->name->text, name->text);
} else {
- emit_impl("%s_t %s$%s = 0;", type->name->text,
+ emit_impl("t$%s %s$%s = 0;", type->name->text,
(type->kind == TYPE_STRUCT) ? "*" : "",
name->text);
}
@@ -1400,14 +1400,14 @@ void parse_function(void) {
rtype = parse_type(false);
}
- emit_decl("%s_t fn_%s(", rtype->name->text, fname->text);
- emit_impl("%s_t fn_%s(", rtype->name->text, fname->text);
+ emit_decl("t$%s fn_%s(", rtype->name->text, fname->text);
+ emit_impl("t$%s fn_%s(", rtype->name->text, fname->text);
for (Symbol *s = ctx.scope->first; s != nil; s = s->next) {
- emit_decl("%s_t %s$%s%s",
+ emit_decl("t$%s %s$%s%s",
s->type->name->text,
s->type->kind == TYPE_STRUCT ? "*" : "",
s->name->text, s->next ? ", " : "");
- emit_impl("%s_t %s$%s%s",
+ emit_impl("t$%s %s$%s%s",
s->type->name->text,
s->type->kind == TYPE_STRUCT ? "*" : "",
s->name->text, s->next ? ", " : "");
diff --git a/inc/builtin.type.h b/inc/builtin.type.h
@@ -1,13 +1,13 @@
#include <stdint.h>
-typedef void void_t;
+typedef void t$void;
-typedef uint32_t u32_t;
-typedef int32_t i32_t;
-typedef uint16_t u16_t;
-typedef int16_t i16_t;
-typedef uint8_t u8_t;
-typedef int8_t i8_t;
+typedef uint32_t t$u32;
+typedef int32_t t$i32;
+typedef uint16_t t$u16;
+typedef int16_t t$i16;
+typedef uint8_t t$u8;
+typedef int8_t t$i8;
-typedef uint8_t *u8_a_t;
+typedef uint8_t *t$u8$;
diff --git a/inc/library.impl.c b/inc/library.impl.c
@@ -8,7 +8,7 @@ void fn__hexout_(int x) {
printf("D %08x\n", x);
}
-void fn_writes(int fd, u8_a_t s) {
+void fn_writes(int fd, t$u8$ s) {
write(fd, (void*)s, strlen((void*) s));
}
void fn_writex(int fd, int n) {
@@ -17,12 +17,12 @@ void fn_writex(int fd, int n) {
write(fd, tmp, strlen(tmp));
}
void fn_writec(int fd, int n) {
- u8_t x = n;
+ t$u8 x = n;
if (write(fd, &x, 1) != 1) {}
}
int fn_readc(int fd) {
- u8_t x;
+ t$u8 x;
if (read(fd, &x, 1) == 1) {
return x;
} else {
diff --git a/inc/library.impl.h b/inc/library.impl.h
@@ -1,8 +1,8 @@
#include <stdio.h>
-void fn__hexout_(i32_t x);
+void fn__hexout_(t$i32 x);
-void fn_writes(i32_t fd, u8_a_t s);
-void fn_writex(i32_t fd, i32_t n);
-void fn_writec(i32_t fd, i32_t c);
-i32_t fn_readc(i32_t fd);
+void fn_writes(t$i32 fd, t$u8$ s);
+void fn_writex(t$i32 fd, t$i32 n);
+void fn_writec(t$i32 fd, t$i32 c);
+t$i32 fn_readc(t$i32 fd);