From: Michael D. Lowis Date: Thu, 15 Apr 2021 20:25:13 +0000 (-0400) Subject: added build script, removed unnecessary files, and changed name X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=273fa5398ccccf298fa1b1dc27bf02cc5ed00f31;p=proto%2Fobnc.git added build script, removed unnecessary files, and changed name --- diff --git a/.gitignore b/.gitignore index 33e0039..3cb8f46 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,7 @@ lex.yyTest tags *.swp lex.yy.* -y.tab.* \ No newline at end of file +y.tab.* +cerisec +cerisec-test + diff --git a/cerise/ast.c b/cerise/ast.c deleted file mode 100644 index a3fd9d4..0000000 --- a/cerise/ast.c +++ /dev/null @@ -1,193 +0,0 @@ -#include - -static AST* ast(ASTType type) { - AST* tree = emalloc(sizeof(AST)); - memset(tree, 0, sizeof(AST)); - tree->nodetype = type; - return tree; -} - -AST* String(char* val) { - AST* node = ast(AST_STRING); - node->value.text = val; - return node; -} - -char* string_value(AST* val) { - assert(val->nodetype == AST_STRING); - return val->value.text; -} - -AST* Char(int val) { - AST* node = ast(AST_CHAR); - node->value.integer = val; - return node; -} - -uint32_t char_value(AST* val) { - assert(val->nodetype == AST_CHAR); - return val->value.integer; -} - -AST* Integer(int val) { - AST* node = ast(AST_INT); - node->value.integer = val; - return node; -} - -intptr_t integer_value(AST* val) { - assert(val->nodetype == AST_INT); - return val->value.integer; -} - -AST* Float(double val) { - AST* node = ast(AST_FLOAT); - node->value.floating = val; - return node; -} - -double float_value(AST* val) { - assert(val->nodetype == AST_FLOAT); - return val->value.floating; -} - -AST* Bool(bool val) { - AST* node = ast(AST_BOOL); - node->value.integer = val; - return node; -} - -bool bool_value(AST* val) { - assert(val->nodetype == AST_BOOL); - return val->value.integer; -} - -AST* Ident(char* val) { - AST* node = ast(AST_IDENT); - node->value.text = strdup(val); - return node; -} - -char* ident_value(AST* val) { - assert(val->nodetype == AST_IDENT); - return val->value.text; -} - -AST* Var(char* name, AST* value, AST* type, int flags) { - (void)type; - AST* node = ast(AST_VAR); - node->value.var.name = name; - node->value.var.value = value; - node->value.var.flags = flags; - return node; -} - -char* var_name(AST* var) { - assert(var->nodetype == AST_VAR); - return var->value.var.name; -} - -AST* var_value(AST* var) { - assert(var->nodetype == AST_VAR); - return var->value.var.value; -} - -bool var_flagset(AST* var, int mask) { - assert(var->nodetype == AST_VAR); - return ((var->value.var.flags & mask) == mask); -} - -AST* Func(AST* args, AST* body, AST* type) -{ - (void)type; - AST* node = ast(AST_FUNC); - node->value.nodes[0] = args; - node->value.nodes[1] = body; - return node; -} - -AST* func_args(AST* func) { - assert(func->nodetype == AST_FUNC); - return func->value.nodes[0]; -} - -AST* func_body(AST* func) { - assert(func->nodetype == AST_FUNC); - return func->value.nodes[1]; -} - -AST* ExpList(void) { - AST* node = ast(AST_EXPLIST); - node->value.explist.nexprs = 0; - node->value.explist.exprs = 0; - return node; -} - -AST** explist_get(AST* explist, size_t* nexprs) { - assert(explist->nodetype == AST_EXPLIST); - *nexprs = explist->value.explist.nexprs; - return explist->value.explist.exprs; -} - -void explist_append(AST* explist, AST* expr) { - assert(explist->nodetype == AST_EXPLIST); - explist->value.explist.nexprs += 1; - explist->value.explist.exprs = realloc(explist->value.explist.exprs, explist->value.explist.nexprs * sizeof(AST*)); - explist->value.explist.exprs[explist->value.explist.nexprs-1] = expr; -} - -void explist_prepend(AST* explist, AST* expr) { - assert(explist->nodetype == AST_EXPLIST); - explist->value.explist.nexprs++; - explist->value.explist.exprs = realloc(explist->value.explist.exprs, explist->value.explist.nexprs * sizeof(AST*)); - memmove(explist->value.explist.exprs+1, explist->value.explist.exprs, explist->value.explist.nexprs-1); - explist->value.explist.exprs[0] = expr; -} - -AST* If(AST* cond, AST* b1, AST* b2) { - AST* node = ast(AST_IF); - node->value.nodes[0] = cond; - node->value.nodes[1] = b1; - node->value.nodes[2] = b2; - return node; -} - -AST* if_cond(AST* ifexp) { - assert(ifexp->nodetype == AST_IF); - return ifexp->value.nodes[0]; -} - -AST* if_then(AST* ifexp) { - assert(ifexp->nodetype == AST_IF); - return ifexp->value.nodes[1]; -} - -AST* if_else(AST* ifexp) { - assert(ifexp->nodetype == AST_IF); - return ifexp->value.nodes[2]; -} - -AST* Apply(AST* func, AST* args) { - AST* node = ast(AST_APPLY); - node->value.nodes[0] = func; - node->value.nodes[1] = args; - return node; -} - -AST* apply_func(AST* apply) { - assert(apply->nodetype == AST_APPLY); - return apply->value.nodes[0]; -} - -AST* apply_args(AST* apply) { - assert(apply->nodetype == AST_APPLY); - return apply->value.nodes[1]; -} - -AST* OpCall(int oper, AST* left, AST* right) { - AST* node = ast(AST_OPER); - node->value.op.oper = oper; - node->value.op.left = left; - node->value.op.right = right; - return node; -} diff --git a/cerise/build.sh b/cerise/build.sh index c5d2f06..dec3fbf 100755 --- a/cerise/build.sh +++ b/cerise/build.sh @@ -1,3 +1,5 @@ #!/bin/sh ctags -R & -cc -Wall -Wextra -Werror --std=c99 -o sclpl -I. *.c +cc -g -D CERISE_TESTS -Wall -Wextra -Werror --std=c99 -o cerisec-test *.c tests/*.c \ + && ./cerisec-test \ + && cc -g -Wall -Wextra -Werror --std=c99 -o cerisec *.c diff --git a/cerise/sclpl.h b/cerise/cerise.h similarity index 100% rename from cerise/sclpl.h rename to cerise/cerise.h diff --git a/cerise/codegen.c b/cerise/codegen.c deleted file mode 100644 index a625f09..0000000 --- a/cerise/codegen.c +++ /dev/null @@ -1,12 +0,0 @@ -#include - -void codegen_init(Parser* p) { - sym_add(&(p->syms), SF_TYPEDEF, "void", VoidType()); - sym_add(&(p->syms), SF_TYPEDEF, "bool", UIntType(1u)); - sym_add(&(p->syms), SF_TYPEDEF, "byte", UIntType(8u)); - sym_add(&(p->syms), SF_TYPEDEF, "int", IntType(64u)); - sym_add(&(p->syms), SF_TYPEDEF, "uint", UIntType(64u)); - sym_add(&(p->syms), SF_TYPEDEF, "float", FloatType(32u)); - sym_add(&(p->syms), SF_TYPEDEF, "string", - ArrayOf(sym_get(&(p->syms), "byte")->type, -1)); -} diff --git a/cerise/emalloc.c b/cerise/emalloc.c index 751a40f..b5203a7 100644 --- a/cerise/emalloc.c +++ b/cerise/emalloc.c @@ -1,4 +1,5 @@ -#include +#include "cerise.h" + void fatal(char* estr) { perror(estr); exit(1); diff --git a/cerise/lex.c b/cerise/lex.c index 648295e..0adffdf 100644 --- a/cerise/lex.c +++ b/cerise/lex.c @@ -1,4 +1,4 @@ -#include +#include "cerise.h" #include #include #include diff --git a/cerise/main.c b/cerise/main.c index d1beeeb..8e85037 100644 --- a/cerise/main.c +++ b/cerise/main.c @@ -1,4 +1,4 @@ -#include +#include "cerise.h" char* ARGV0; char* Artifact = "bin"; diff --git a/cerise/parser.c b/cerise/parser.c index 3ed1869..826c710 100644 --- a/cerise/parser.c +++ b/cerise/parser.c @@ -1,4 +1,4 @@ -#include +#include "cerise.h" #include //#define TRACE diff --git a/cerise/pkg.c b/cerise/pkg.c deleted file mode 100644 index 0bed7ff..0000000 --- a/cerise/pkg.c +++ /dev/null @@ -1,23 +0,0 @@ -#include - -void pkg_add_require(Package* p, char* req) -{ - Require* rq = malloc(sizeof(Require)); - rq->path = strdup(req); - rq->next = p->requires; - p->requires = rq; -} - -void pkg_add_provide(Package* p, char* exp) -{ - Provide* pr = malloc(sizeof(Provide)); - pr->name = strdup(exp); - pr->next = p->provides; - p->provides = pr; -} - -void pkg_add_definition(Package* p, AST* ast) -{ - (void)p, (void)ast; -} - diff --git a/cerise/pprint.c b/cerise/pprint.c deleted file mode 100644 index 8a9705c..0000000 --- a/cerise/pprint.c +++ /dev/null @@ -1,217 +0,0 @@ -#include - -static void indent(FILE* file, int depth) { - fprintf(file, "\n"); - if (depth) fprintf(file, "%*c", depth * 2, ' '); -} - -static const char* token_type_to_string(int type) { - #define TOK(name) case (name): return #name - switch(type) { - TOK(T_NONE); TOK(T_ERROR); TOK(T_END_FILE); - TOK(T_REQUIRES); TOK(T_PROVIDES); TOK(T_LET); TOK(T_VAR); - TOK(T_FUN); TOK(T_TYPE); TOK(T_STRUCT); TOK(T_UNION); - TOK(T_RETURN); TOK(T_IF); TOK(T_ELSE); TOK(T_ID); - TOK(T_CHAR); TOK(T_INT); TOK(T_FLOAT); TOK(T_BOOL); - TOK(T_STRING); - case '{': return "T_LBRACE"; - case '}': return "T_RBRACE"; - case '[': return "T_LBRACK"; - case ']': return "T_RBRACK"; - case '(': return "T_LPAR"; - case ')': return "T_RPAR"; - case ',': return "T_COMMA"; - case ':': return "T_COLON"; - case '&': return "T_AMP"; - case '\'': return "T_SQUOTE"; - case '"': return "T_DQUOTE"; - default: return "???"; - } - #undef TOK -} - -static void print_char(FILE* file, char ch) { - int i; - static const char* lookup_table[5] = { - " \0space", - "\n\0newline", - "\r\0return", - "\t\0tab", - "\v\0vtab" - }; - for(i = 0; i < 5; i++) { - if (ch == lookup_table[i][0]) { - fprintf(file, "\\%s", &(lookup_table[i][2])); - break; - } - } - if (i == 5) fprintf(file, "\\%c", ch); -} - -static void pprint_token_type(FILE* file, Tok* token) { - if (token->type > 256) - fprintf(file, "%s", token_type_to_string(token->type)); - else - fprintf(file, "%c", token->type); -} - -static void pprint_token_value(FILE* file, Tok* token) { - #define TOK(name) case (name): fprintf(file, "%s", #name); break - switch(token->type) { - /* value tokens */ - case T_STRING: fprintf(file, "\"%s\"", token->text); break; - case T_ID: fprintf(file, "%s", token->text); break; - case T_CHAR: print_char(file, token->value.integer); break; - case T_INT: fprintf(file, "%lld", token->value.integer); break; - case T_FLOAT: fprintf(file, "%f", token->value.floating); break; - case T_BOOL: fprintf(file, "%s", (token->value.integer)?"true":"false"); break; - - /* keyword tokens */ - TOK(T_NONE); TOK(T_ERROR); TOK(T_END_FILE); - TOK(T_REQUIRES); TOK(T_PROVIDES); TOK(T_LET); TOK(T_VAR); - TOK(T_FUN); TOK(T_TYPE); TOK(T_STRUCT); TOK(T_UNION); TOK(T_RETURN); - TOK(T_IF); TOK(T_ELSE); - - /* evertything else */ - default: - fprintf(file, "???"); - break; - } - #undef TOK -} - -void pprint_token(FILE* file, Tok* token, bool print_loc) -{ - if (print_loc) { - fprintf(file, "%zu:", token->offset); - } - pprint_token_type(file, token); - if (token->type > 256) { - fprintf(file, ":"); - pprint_token_value(file, token); - } - fprintf(file, "\n"); -} - -/*****************************************************************************/ - -static const char* tree_type_to_string(ASTType type) { - switch(type) { - case AST_STRING: return "T_STRING"; - case AST_SYMBOL: return "T_SYMBOL"; - case AST_IDENT: return "T_ID"; - case AST_CHAR: return "T_CHAR"; - case AST_INT: return "T_INT"; - case AST_FLOAT: return "T_FLOAT"; - case AST_BOOL: return "T_BOOL"; - default: return "???"; - } -} - -static void pprint_literal(FILE* file, AST* tree, int depth) -{ - (void)depth; - fprintf(file, "%s:", tree_type_to_string(tree->nodetype)); - switch(tree->nodetype) { - case AST_STRING: fprintf(file, "\"%s\"", string_value(tree)); break; - case AST_IDENT: fprintf(file, "%s", ident_value(tree)); break; - case AST_CHAR: fprintf(file, "%c", char_value(tree)); break; - case AST_INT: fprintf(file, "%ld", integer_value(tree)); break; - case AST_FLOAT: fprintf(file, "%lf", float_value(tree)); break; - case AST_BOOL: - fprintf(file, "%s", bool_value(tree) ? "true" : "false"); - break; - default: fprintf(file, "???"); - } -} - -static char* getvartype(AST* tree) { - if (var_flagset(tree, SF_CONSTANT)) - return "let"; - else if (var_flagset(tree, SF_TYPEDEF)) - return "typedef"; - else - return "var"; -} - -void pprint_fargs(FILE* file, AST* tree) { - size_t nargs = 0; - AST** args = explist_get(tree, &nargs); - fprintf(file, "("); - for (size_t i = 0; i < nargs; i++) { - fprintf(file, "("); - fprintf(file, "%s : type", var_name(args[i])); - fprintf(file, ") "); - } - fprintf(file, ")"); -} - -void pprint_block(FILE* file, AST* tree, int depth) { - if (!tree) return; - size_t nexprs = 0; - AST** exprs = explist_get(tree, &nexprs); - for (size_t i = 0; i < nexprs; i++) { - indent(file, depth); - pprint_tree(file, exprs[i], depth); - } -} - -void pprint_branch(FILE* file, AST* tree, int depth) { - indent(file, depth); - pprint_tree(file, tree, depth); -} - -void pprint_ifexpr(FILE* file, AST* tree, int depth) { - fprintf(file, "(if "); - pprint_tree(file, if_cond(tree), depth); - pprint_branch(file, if_then(tree), depth+1); - pprint_branch(file, if_else(tree), depth+1); - fprintf(file, ")"); -} - -void pprint_apply(FILE* file, AST* tree, int depth) { - fprintf(file, "(apply "); - pprint_tree(file, apply_func(tree), depth); - size_t nexprs = 0; - AST** exprs = explist_get(apply_args(tree), &nexprs); - for (size_t i = 0; i < nexprs; i++) { - indent(file, depth+1); - pprint_tree(file, exprs[i], depth+1); - } - fprintf(file, ")"); -} - -void pprint_tree(FILE* file, AST* tree, int depth) { - if (tree == NULL) return; - switch (tree->nodetype) { - case AST_VAR: - fprintf(file, "(%s %s ", getvartype(tree), var_name(tree)); - pprint_tree(file, var_value(tree), depth); - fprintf(file, ")"); - break; - - case AST_FUNC: - pprint_fargs(file, func_args(tree)); - pprint_block(file, func_body(tree), depth+1); - break; - - case AST_EXPLIST: - fprintf(file, "(block"); - pprint_block(file, tree, depth+1); - fprintf(file, ")"); - break; - - case AST_IF: - pprint_ifexpr(file, tree, depth); - break; - - case AST_APPLY: - pprint_apply(file, tree, depth); - break; - - default: - pprint_literal(file, tree, depth); - break; - } -} - diff --git a/cerise/sclpl b/cerise/sclpl deleted file mode 100755 index 6fd7fbd..0000000 Binary files a/cerise/sclpl and /dev/null differ diff --git a/cerise/syms.c b/cerise/syms.c deleted file mode 100644 index 6a8f0e2..0000000 --- a/cerise/syms.c +++ /dev/null @@ -1,22 +0,0 @@ -#include - -static Sym* mksym(int flags, char* name, Type* type, Sym* next) { - Sym* sym = emalloc(sizeof(Sym)); - sym->flags = flags; - sym->name = name; - sym->type = type; - sym->next = next; - return sym; -} - -void sym_add(SymTable* syms, int flags, char* name, Type* type) { - syms->syms = mksym(flags, name, type, syms->syms); -} - -Sym* sym_get(SymTable* syms, char* name) { - Sym* sym = syms->syms; - for (; sym; sym = sym->next) - if (!strcmp(sym->name, name)) - return sym; - return NULL; -} diff --git a/cerise/tests/tokens.txt b/cerise/tests/tokens.txt new file mode 100644 index 0000000..b9068f3 --- /dev/null +++ b/cerise/tests/tokens.txt @@ -0,0 +1,58 @@ ++ +- +/ +* +^ += +:= +!= +< +> +<= +>= +. +, +; +.. +| +: +( +) +[ +] +{ +} +and +array +begin +by +case +const +div +do +else +elsif +end +false +for +if +import +is +mod +module +nil +not +of +or +pointer +procedure +record +repeat +return +then +to +true +type +until +var +while diff --git a/cerise/types.c b/cerise/types.c deleted file mode 100644 index cfaae4c..0000000 --- a/cerise/types.c +++ /dev/null @@ -1,59 +0,0 @@ -#include - -static Type* mktype(Kind kind) { - Type* type = emalloc(sizeof(Type)); - memset(type, 0, sizeof(Type)); - type->kind = kind; - return type; -} - -Type* VoidType(void) { - return mktype(VOID); -} - -Type* IntType(size_t nbits) { - Type* type = mktype(INT); - type->value.bits = nbits; - return type; -} - -Type* UIntType(size_t nbits) { - Type* type = mktype(UINT); - type->value.bits = nbits; - return type; -} - -Type* FloatType(size_t nbits) { - Type* type = mktype(FLOAT); - type->value.bits = nbits; - return type; -} - -Type* ArrayOf(Type* elemtype, size_t count) { - Type* type = mktype(ARRAY); - type->value.array.type = elemtype; - type->value.array.count = count; - return type; -} - -Type* RefTo(Type* type) { - (void)type; - return NULL; -} - -Type* PtrTo(Type* type) { - (void)type; - return NULL; -} - -bool types_equal(Type* type1, Type* type2) { - if (type1->kind != type2->kind) return false; - switch (type1->kind) { - case ARRAY: - return (types_equal(type1->value.array.type, type2->value.array.type) && - (type1->value.array.count == type2->value.array.count)); - default: - return true; - } -} -