Add secret structs
This commit is contained in:
parent
c60b21bf3c
commit
853b8e505e
1
ast.h
1
ast.h
@ -225,6 +225,7 @@ struct ast_s {
|
||||
const char *name;
|
||||
arg_ast_t *fields;
|
||||
ast_t *namespace;
|
||||
bool secret:1;
|
||||
} StructDef;
|
||||
struct {
|
||||
const char *name;
|
||||
|
40
compile.c
40
compile.c
@ -289,23 +289,31 @@ CORD compile(env_t *env, ast_t *ast)
|
||||
}
|
||||
CORD_appendf(&env->types, "};\n");
|
||||
|
||||
CORD_appendf(&env->funcs,
|
||||
"CORD %s__cord(%s_t *obj, bool use_color) {\n"
|
||||
"\tif (!obj) return \"%s\";\n"
|
||||
"\treturn CORD_asprintf(use_color ? \"\\x1b[0;1m%s\\x1b[m(", def->name, def->name, def->name, def->name);
|
||||
for (arg_ast_t *field = def->fields; field; field = field->next) {
|
||||
CORD_appendf(&env->funcs, "%s=\\x1b[35m%%r\\x1b[m", field->name);
|
||||
if (field->next) env->funcs = CORD_cat(env->funcs, ", ");
|
||||
CORD cord_func = CORD_asprintf("CORD %s__cord(%s_t *obj, bool use_color) {\n"
|
||||
"\tif (!obj) return \"%s\";\n", def->name, def->name, def->name);
|
||||
|
||||
if (def->secret) {
|
||||
CORD_appendf(&cord_func, "\treturn use_color ? \"\\x1b[0;1m%s\\x1b[m(\\x1b[2m...\\x1b[m)\" : \"%s(...)\";\n}",
|
||||
def->name, def->name);
|
||||
} else {
|
||||
CORD_appendf(&cord_func, "\treturn CORD_asprintf(use_color ? \"\\x1b[0;1m%s\\x1b[m(", def->name);
|
||||
for (arg_ast_t *field = def->fields; field; field = field->next) {
|
||||
CORD_appendf(&cord_func, "%s=\\x1b[35m%%r\\x1b[m", field->name);
|
||||
if (field->next) cord_func = CORD_cat(cord_func, ", ");
|
||||
}
|
||||
CORD_appendf(&cord_func, ")\" : \"%s(", def->name);
|
||||
for (arg_ast_t *field = def->fields; field; field = field->next) {
|
||||
CORD_appendf(&cord_func, "%s=%%r", field->name);
|
||||
if (field->next) cord_func = CORD_cat(cord_func, ", ");
|
||||
}
|
||||
cord_func = CORD_cat(cord_func, ")\"");
|
||||
for (arg_ast_t *field = def->fields; field; field = field->next)
|
||||
CORD_appendf(&cord_func, ", __cord(obj->%s)", field->name);
|
||||
cord_func = CORD_cat(cord_func, ");\n}");
|
||||
}
|
||||
CORD_appendf(&env->funcs, ")\" : \"%s(", def->name);
|
||||
for (arg_ast_t *field = def->fields; field; field = field->next) {
|
||||
CORD_appendf(&env->funcs, "%s=%%r", field->name);
|
||||
if (field->next) env->funcs = CORD_cat(env->funcs, ", ");
|
||||
}
|
||||
env->funcs = CORD_cat(env->funcs, ")\"");
|
||||
for (arg_ast_t *field = def->fields; field; field = field->next)
|
||||
CORD_appendf(&env->funcs, ", __cord(obj->%s)", field->name);
|
||||
env->funcs = CORD_cat(env->funcs, ");\n}");
|
||||
|
||||
env->funcs = CORD_cat(env->funcs, cord_func);
|
||||
|
||||
return CORD_EMPTY;
|
||||
}
|
||||
case EnumDef: {
|
||||
|
@ -89,7 +89,7 @@ int main(int argc, char *argv[])
|
||||
if (getenv("LDLIBS"))
|
||||
ldlibs = heap_strf("%s %s", ldlibs, getenv("LDLIBS"));
|
||||
|
||||
const char *run = heap_strf("tcc %s %s -run -", cflags, ldlibs);
|
||||
const char *run = heap_strf("tcc -run %s %s -", cflags, ldlibs);
|
||||
FILE *cc = popen(run, "w");
|
||||
CORD_put(program, cc);
|
||||
fclose(cc);
|
||||
|
17
parse.c
17
parse.c
@ -1400,6 +1400,21 @@ PARSER(parse_struct_def) {
|
||||
|
||||
arg_ast_t *fields = parse_args(ctx, &pos, false);
|
||||
|
||||
whitespace(&pos);
|
||||
bool secret = false;
|
||||
if (match(&pos, ";")) { // Extra flags
|
||||
for (bool done = false; !done; done = true) {
|
||||
whitespace(&pos);
|
||||
if (match_word(&pos, "secret")) {
|
||||
secret = true;
|
||||
done = false;
|
||||
}
|
||||
whitespace(&pos);
|
||||
match(&pos, ",");
|
||||
match(&pos, ";");
|
||||
}
|
||||
}
|
||||
|
||||
expect_closing(ctx, &pos, ")", "I wasn't able to parse the rest of this struct");
|
||||
|
||||
const char *ns_pos = pos;
|
||||
@ -1412,7 +1427,7 @@ PARSER(parse_struct_def) {
|
||||
}
|
||||
if (!namespace)
|
||||
namespace = NewAST(ctx->file, pos, pos, Block, .statements=NULL);
|
||||
return NewAST(ctx->file, start, pos, StructDef, .name=name, .fields=fields, .namespace=namespace);
|
||||
return NewAST(ctx->file, start, pos, StructDef, .name=name, .fields=fields, .namespace=namespace, .secret=secret);
|
||||
}
|
||||
|
||||
ast_t *parse_enum_def(parse_ctx_t *ctx, const char *pos) {
|
||||
|
Loading…
Reference in New Issue
Block a user