tags
*.swp
lex.yy.*
-y.tab.*
\ No newline at end of file
+y.tab.*
+cerisec
+cerisec-test
+
+++ /dev/null
-#include <sclpl.h>
-
-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;
-}
#!/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
+++ /dev/null
-#include <sclpl.h>
-
-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));
-}
-#include <sclpl.h>
+#include "cerise.h"
+
void fatal(char* estr) {
perror(estr);
exit(1);
-#include <sclpl.h>
+#include "cerise.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
-#include <sclpl.h>
+#include "cerise.h"
char* ARGV0;
char* Artifact = "bin";
-#include <sclpl.h>
+#include "cerise.h"
#include <stdarg.h>
//#define TRACE
+++ /dev/null
-#include <sclpl.h>
-
-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;
-}
-
+++ /dev/null
-#include <sclpl.h>
-
-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;
- }
-}
-
+++ /dev/null
-#include <sclpl.h>
-
-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;
-}
--- /dev/null
++
+-
+/
+*
+^
+=
+:=
+!=
+<
+>
+<=
+>=
+.
+,
+;
+..
+|
+:
+(
+)
+[
+]
+{
+}
+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
+++ /dev/null
-#include <sclpl.h>
-
-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;
- }
-}
-