From: Michael Wallner Date: Tue, 13 Oct 2015 08:12:11 +0000 (+0200) Subject: flush X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=commitdiff_plain;h=9e0411db099e71409f0ee8a8f464aa34ac65c778 flush --- diff --git a/idl/Makefile b/idl/Makefile index e17b526..6534ffe 100644 --- a/idl/Makefile +++ b/idl/Makefile @@ -1,12 +1,14 @@ CFLAGS += -g -D$(shell uname | tr a-z A-Z) -SOURCES := parser_proc.c parser.c lexer.c validator.c compiler.c main.c -MAKEHEADERS := $(addsuffix .[ch], $(basename $(SOURCES))) +SOURCES := parser_proc.c parser.c validator.c compiler.c main.c GENERATED_FILES := $(addsuffix .h, $(basename $(SOURCES))) .PHONY: all -all: headers main +all: main +## +# The lemon parser generator +## GENERATED_FILES += lempar.c lempar.c: curl -sSo $@ "http://www.sqlite.org/src/raw/tool/lempar.c?name=3617143ddb9b176c3605defe6a9c798793280120" @@ -18,15 +20,24 @@ lemon.c: GENERATED_FILES += lemon lemon: lemon.c | lempar.c +## +# Main test program +## GENERATED_FILES += main main: $(SOURCES) +## +# Parser proc +## GENERATED_FILES += parser_proc.c parser_proc.c: parser_proc.y lemon - ./lemon -c $@ + ./lemon -c $< -GENERATED_FILES += lexer.c -lexer.c: lexer.re +## +# Parser with lexer +## +GENERATED_FILES += parser.c +parser.c: parser.re re2c -o $@ $< .PHONY: clean diff --git a/idl/compiler.c b/idl/compiler.c index 7250836..02f3867 100644 --- a/idl/compiler.c +++ b/idl/compiler.c @@ -2,17 +2,13 @@ #include "compiler.h" -#if INTERFACE -typedef struct PSI_Compiler { - decl_typedefs *defs; - decls *decls; - impls *impls; - char *lib; - char *fn; -} PSI_Compiler; -#endif - PSI_Compiler *PSI_CompilerInit(PSI_Compiler *C, PSI_Validator *V) { + if (!C) { + C = malloc(sizeof(*C)); + } + memset(C, 0, sizeof(*C)); + PSI_DataExchange((PSI_Data *) C, (PSI_Data *) V); + return C; } diff --git a/idl/compiler.h b/idl/compiler.h index cbf6039..c5e447f 100644 --- a/idl/compiler.h +++ b/idl/compiler.h @@ -9,7 +9,8 @@ typedef struct PSI_Compiler { impls *impls; char *lib; char *fn; -}; +} PSI_Compiler; + PSI_Compiler *PSI_CompilerInit(PSI_Compiler *C, PSI_Validator *V); #endif diff --git a/idl/idl.h b/idl/idl.h deleted file mode 100644 index f98e1cd..0000000 --- a/idl/idl.h +++ /dev/null @@ -1,644 +0,0 @@ -#ifndef _PSI_TYPES_H -#define _PSI_TYPES_H - -#include -#include - -#include "parser_proc.h" - -typedef int token_t; - -typedef struct PSI_Token { - token_t type; - unsigned line; - size_t size; - char text[1]; -} PSI_Token; - -static inline PSI_Token *PSI_TokenAlloc(PSI_Lexer *L, token_t t) { - PSI_Token *T; - size_t token_len; - - if (L->cur <= L->tok) { - return NULL; - } - - token_len = L->cur - L->tok; - - T = malloc(sizeof(*T) + token_len); - T->type = t; - T->line = L->line; - T->size = token_len; - T->text[token_len] = 0; - memcpy(T->text, L->tok, token_len); - - return T; -} - -typedef struct decl_type { - char *name; - token_t type; - struct decl_type *real; -} decl_type; - -static inline decl_type *init_decl_type(token_t type, char *name) { - decl_type *t = malloc(sizeof(*t)); - t->type = type; - t->name = strdup(name); - return t; -} - -static inline void free_decl_type(decl_type *type) { - free(type->name); - free(type); -} - -typedef struct decl_typedef { - char *alias; - decl_type *type; -} decl_typedef; - -static inline decl_typedef *init_decl_typedef(char *name, decl_type *type) { - decl_typedef *t = malloc(sizeof(*t)); - t->alias = strdup(name); - t->type = type; - return t; -} - -static inline void free_decl_typedef(decl_typedef *t) { - free(t->alias); - free_decl_type(t->type); - free(t); -} - -typedef struct decl_typedefs { - size_t count; - decl_typedef **list; -} decl_typedefs; - -static decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_typedef *def) { - if (!defs) { - defs = calloc(1, sizeof(*defs)); - } - defs->list = realloc(defs->list, ++defs->count * sizeof(*defs->list)); - defs->list[defs->count-1] = def; - return defs; -} - -static void free_decl_typedefs(decl_typedefs *defs) { - size_t i; - - for (i = 0; i < defs->count; ++i) { - free_decl_typedef(defs->list[i]); - } - free(defs->list); - free(defs); -} - -typedef struct decl_var { - char *name; - unsigned pointer_level; -} decl_var; - -static inline decl_var *init_decl_var(char *name, unsigned pl) { - decl_var *v = malloc(sizeof(*v)); - v->name = (char *) strdup((const char *) name); - v->pointer_level = pl; - return v; -} - -static inline void free_decl_var(decl_var *var) { - free(var->name); - free(var); -} - -typedef struct decl_arg { - decl_type *type; - decl_var *var; -} decl_arg; - -static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) { - decl_arg *arg = malloc(sizeof(*arg)); - arg->type = type; - arg->var = var; - return arg; -} - -static inline void free_decl_arg(decl_arg *arg) { - free_decl_type(arg->type); - free_decl_var(arg->var); - free(arg); -} - -typedef struct decl_vars { - decl_var **vars; - size_t count; -} decl_vars; - -static inline decl_vars *init_decl_vars(decl_var *var) { - decl_vars *vars = malloc(sizeof(*vars)); - vars->count = 1; - vars->vars = malloc(sizeof(*vars->vars)); - vars->vars[0] = var; - return vars; -} - -static inline decl_vars *add_decl_var(decl_vars *vars, decl_var *var) { - vars->vars = realloc(vars->vars, ++vars->count * sizeof(*vars->vars)); - vars->vars[vars->count-1] = var; - return vars; -} - -static inline void free_decl_vars(decl_vars *vars) { - size_t i; - - for (i = 0; i < vars->count; ++i) { - free_decl_var(vars->vars[i]); - } - free(vars->vars); - free(vars); -} - -typedef struct decl_args { - decl_arg **args; - size_t count; -} decl_args; - -static inline decl_args *init_decl_args(decl_arg *arg) { - decl_args *args = malloc(sizeof(*args)); - args->count = 1; - args->args = malloc(sizeof(*args->args)); - args->args[0] = arg; - return args; -} - -static inline decl_args *add_decl_arg(decl_args *args, decl_arg *arg) { - args->args = realloc(args->args, ++args->count * sizeof(*args->args)); - args->args[args->count-1] = arg; - return args; -} - -static inline void free_decl_args(decl_args *args) { - size_t i; - - for (i = 0; i < args->count; ++i) { - free_decl_arg(args->args[i]); - } - free(args->args); - free(args); -} - -typedef struct decl_abi { - char *convention; -} decl_abi; - -static inline decl_abi *init_decl_abi(char *convention) { - decl_abi *abi = malloc(sizeof(*abi)); - abi->convention = strdup(convention); - return abi; -} - -static inline void free_decl_abi(decl_abi *abi) { - free(abi->convention); - free(abi); -} - -typedef struct decl { - decl_abi *abi; - decl_arg *func; - decl_args *args; - void *dlptr; -} decl; - -static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) { - decl *d = malloc(sizeof(*d)); - d->abi = abi; - d->func = func; - d->args = args; - return d; -} - -static inline void free_decl(decl *d) { - free_decl_abi(d->abi); - free_decl_arg(d->func); - free_decl_args(d->args); - free(d); -} - -typedef struct decls { - size_t count; - decl **list; -} decls; - -static inline decls *add_decl(decls *decls, decl *decl) { - if (!decls) { - decls = calloc(1, sizeof(*decls)); - } - decls->list = realloc(decls->list, ++decls->count * sizeof(*decls->list)); - decls->list[decls->count-1] = decl; - return decls; -} - -static inline void free_decls(decls *decls) { - size_t i; - - for (i = 0; i < decls->count; ++i) { - free_decl(decls->list[i]); - } - free(decls->list); - free(decls); -} - -typedef struct impl_type { - char *name; - token_t type; -} impl_type; - -static inline impl_type *init_impl_type(token_t type, char *name) { - impl_type *t = malloc(sizeof(*t)); - - t->type = type; - t->name = (char *) strdup((const char *) name); - return t; -} - -static inline void free_impl_type(impl_type *type) { - free(type->name); - free(type); -} - -typedef struct impl_var { - char *name; - unsigned reference:1; -} impl_var; - -static inline impl_var *init_impl_var(char *name, int is_reference) { - impl_var *var = malloc(sizeof(*var)); - var->name = (char *) strdup((const char *) name); - var->reference = is_reference; - return var; -} - -static inline void free_impl_var(impl_var *var) { - free(var->name); - free(var); -} - -typedef struct impl_def_val { - token_t type; - union { - int64_t digits; - double decimals; - } v; - unsigned is_null:1; -} impl_def_val; - -static inline impl_def_val *init_impl_def_val() { - impl_def_val *def = malloc(sizeof(*def)); - def->type = 0; - def->is_null = 1; - return def; -} - -static inline void free_impl_def_val(impl_def_val *def) { - free(def); -} - -typedef struct impl_arg { - impl_type *type; - impl_var *var; - impl_def_val *def; -} impl_arg; - -static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) { - impl_arg *arg = malloc(sizeof(*arg)); - arg->type = type; - arg->var = var; - arg->def = def; - return arg; -} - -static inline void free_impl_arg(impl_arg *arg) { - free_impl_type(arg->type); - free_impl_var(arg->var); - if (arg->def) { - free_impl_def_val(arg->def); - } - free(arg); -} - -typedef struct impl_args { - impl_arg **args; - size_t count; -} impl_args; - -static inline impl_args *init_impl_args(impl_arg *arg) { - impl_args *args = malloc(sizeof(*args)); - args->args = malloc(sizeof(*args->args)); - if (arg) { - args->count = 1; - args->args[0] = arg; - } else { - args->count = 0; - args->args = NULL; - } - return args; -} - -static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) { - args->args = realloc(args->args, ++args->count * sizeof(*args->args)); - args->args[args->count-1] = arg; - return args; -} - -static inline void free_impl_args(impl_args *args) { - size_t i; - - for (i = 0; i < args->count; ++i) { - free_impl_arg(args->args[i]); - } - free(args->args); - free(args); -} - -typedef struct impl_func { - char *name; - impl_args *args; - impl_type *return_type; -} impl_func; - -static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type) { - impl_func *func = malloc(sizeof(*func)); - func->name = strdup(name); - func->args = args ? args : init_impl_args(NULL); - func->return_type = type; - return func; -} - -static inline void free_impl_func(impl_func *f) { - free_impl_type(f->return_type); - free_impl_args(f->args); - free(f->name); - free(f); -} - -typedef struct let_func { - token_t type; - char *name; -} let_func; - -static inline let_func *init_let_func(token_t type, char *name) { - let_func *func = malloc(sizeof(*func)); - func->type = type; - func->name = (char *) strdup((const char *) name); - return func; -} - -static inline void free_let_func(let_func *func) { - free(func->name); - free(func); -} - -typedef struct let_value { - let_func *func; - impl_var *var; - unsigned null_pointer_ref:1; -} let_value; - -static inline let_value *init_let_value(let_func *func, impl_var *var, int null_pointer_ref) { - let_value *val = malloc(sizeof(*val)); - val->null_pointer_ref = null_pointer_ref; - val->func = func; - val->var = var; - return val; -} - -static inline void free_let_value(let_value *val) { - if (val->func) { - free_let_func(val->func); - } - if (val->var) { - free_impl_var(val->var); - } - free(val); -} - -typedef struct let_stmt { - decl_var *var; - let_value *val; -} let_stmt; - -static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) { - let_stmt *let = malloc(sizeof(*let)); - let->var = var; - let->val = val; - return let; -} - -static inline void free_let_stmt(let_stmt *stmt) { - free_decl_var(stmt->var); - free_let_value(stmt->val); - free(stmt); -} - -typedef struct set_func { - token_t type; - char *name; -} set_func; - -static inline set_func *init_set_func(token_t type, char *name) { - set_func *func = malloc(sizeof(*func)); - func->type = type; - func->name = (char *) strdup((const char *) name); - return func; -} - -static inline void free_set_func(set_func *func) { - free(func->name); - free(func); -} - -typedef struct set_value { - set_func *func; - decl_vars *vars; -} set_value; - -static inline set_value *init_set_value(set_func *func, decl_vars *vars) { - set_value *val = malloc(sizeof(*val)); - val->func = func; - val->vars = vars; - return val; -} - -static inline void free_set_value(set_value *val) { - free_set_func(val->func); - free_decl_vars(val->vars); - free(val); -} - -typedef struct set_stmt { - impl_var *var; - set_value *val; -} set_stmt; - -static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) { - set_stmt *set = malloc(sizeof(*set)); - set->var = var; - set->val = val; - return set; -} - -static inline void free_set_stmt(set_stmt *set) { - free_impl_var(set->var); - free_set_value(set->val); - free(set); -} - -typedef struct ret_stmt { - set_func *func; - decl_var *decl; -} ret_stmt; - -static inline ret_stmt *init_ret_stmt(set_func *func, decl_var *decl) { - ret_stmt *ret = malloc(sizeof(*ret)); - ret->func = func; - ret->decl = decl; - return ret; -} - -static inline void free_ret_stmt(ret_stmt *ret) { - free_set_func(ret->func); - free_decl_var(ret->decl); - free(ret); -} - -typedef struct impl_stmt { - token_t type; - union { - let_stmt *let; - set_stmt *set; - ret_stmt *ret; - void *ptr; - } s; -} impl_stmt; - -static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) { - impl_stmt *stmt = malloc(sizeof(*stmt)); - stmt->type = type; - stmt->s.ptr = ptr; - return stmt; -} - -static inline void free_impl_stmt(impl_stmt *stmt) { - switch (stmt->type) { - case PSI_T_LET: - free_let_stmt(stmt->s.let); - break; - case PSI_T_SET: - free_set_stmt(stmt->s.set); - break; - case PSI_T_RET: - free_ret_stmt(stmt->s.ret); - break; - } - free(stmt); -} - -typedef struct impl_stmts { - impl_stmt **stmts; - size_t count; -} impl_stmts; - -static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) { - impl_stmts *stmts = malloc(sizeof(*stmts)); - stmts->count = 1; - stmts->stmts = malloc(sizeof(*stmts->stmts)); - stmts->stmts[0] = stmt; - return stmts; -} - -static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) { - stmts->stmts = realloc(stmts->stmts, ++stmts->count * sizeof(*stmts->stmts)); - stmts->stmts[stmts->count-1] = stmt; - return stmts; -} - -static inline void free_impl_stmts(impl_stmts *stmts) { - size_t i; - - for (i = 0; i < stmts->count; ++i) { - free_impl_stmt(stmts->stmts[i]); - } - free(stmts->stmts); - free(stmts); -} - -typedef struct impl { - impl_func *func; - impl_stmts *stmts; -} impl; - -static inline impl *init_impl(impl_func *func, impl_stmts *stmts) { - impl *i = malloc(sizeof(*i)); - i->func = func; - i->stmts = stmts; - return i; -} - -static inline void free_impl(impl *impl) { - free_impl_func(impl->func); - free_impl_stmts(impl->stmts); - free(impl); -} - -typedef struct impls { - size_t count; - impl **list; -} impls; - -static impls *add_impl(impls *impls, impl *impl) { - if (!impls) { - impls = calloc(1, sizeof(*impls)); - } - impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list)); - impls->list[impls->count-1] = impl; - return impls; -} - -static void free_impls(impls *impls) { - size_t i; - - for (i = 0; i < impls->count; ++i) { - free_impl(impls->list[i]); - } - free(impls->list); - free(impls); -} - -typedef struct PSI_Data { - decl_typedefs *defs; - decls *decls; - impls *impls; - char *lib; - char *fn; -} PSI_Data; - -static inline void PSI_DataExchange(PSI_Data *dest, PSI_Data *src) { - memcpy(dest, src, sizeof(*dest)); - memset(src, 0, sizeof(*src)); -} - -static inline void PSI_DataDtor(PSI_Data *data) { - if (data->defs) { - free_decl_typedefs(data->defs); - } - if (data->decls) { - free_decls(data->decls); - } - if (data->impls) { - free_impls(data->impls); - } - if (data->fn) { - free(data->fn); - } -} diff --git a/idl/lexer.h b/idl/lexer.h deleted file mode 100644 index c41bd1d..0000000 --- a/idl/lexer.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef _PSI_LEXER_H -#define _PSI_LEXER_H - -#ifndef BSIZE -# define BSIZE 256 -#endif - -typedef struct PSI_Lexer { - -} PSI_Lexer; - -size_t PSI_LexerFill(PSI_Lexer *L, size_t n); -void PSI_LexerDtor(PSI_Lexer *L); -void PSI_LexerFree(PSI_Lexer **L); -PSI_Lexer *PSI_LexerInit(PSI_Lexer *L, const char *filename); -token_t PSI_LexerScan(PSI_Lexer *L); - -#endif diff --git a/idl/lexer.re b/idl/lexer.re deleted file mode 100644 index 91c044a..0000000 --- a/idl/lexer.re +++ /dev/null @@ -1,2 +0,0 @@ - -#include "lexer.h" diff --git a/idl/main.c b/idl/main.c index b132bbd..b4eb459 100644 --- a/idl/main.c +++ b/idl/main.c @@ -2,58 +2,34 @@ #include #include -static int TRACE; - -static void loop(PSI_Lexer *L, void *P) -{ - token_t t; - PSI_Token *T = NULL; - - if (TRACE) { - PSI_ParserTrace(stdout, "> "); - } - - while (-1 != (t = PSI_LexerScan(L))) { - if (!(T = PSI_TokenAlloc(L, t))) { - break; - } - - if (TRACE) { - printf("# Token: <%s>(%d)\n", T->text, t); - } +#include "parser.h" +#include "validator.h" - PSI_Parser(P, t, T, L); - } - PSI_Parser(P, 0, T, L); -} +static int TRACE; int main(int argc, char *argv[]) { - PSI_Lexer L; PSI_Parser P; PSI_Validator V; TRACE = !!getenv("TRACE"); - if (!PSI_LexerInit(&L, argv[1])) { - perror("Failed to init lexer"); - return 1; - } - if (!PSI_ParserInit(&P)) { + if (!PSI_ParserInit(&P, argv[1])) { perror("Failer to init parser"); return 1; } - while (PSI_ParserParse(&p, &L)); - - PSI_ParserFree(P, free); + while (-1 != PSI_ParserScan(&P)) { + PSI_ParserParse(&P, PSI_TokenAlloc(&P)); + }; + PSI_ParserParse(&P, NULL); - if (!PSI_ValidatorInit(&V, &L)) { + if (!PSI_ValidatorInit(&V, &P)) { perror("Failed to init validator"); return 2; } - PSI_LexerDtor(&L); + PSI_ParserDtor(&P); if (PSI_ValidatorValidate(&V)) { printf("Whoa! VALID.\n"); diff --git a/idl/parser.re b/idl/parser.re index 7b7b1c5..5a06023 100644 --- a/idl/parser.re +++ b/idl/parser.re @@ -2,8 +2,13 @@ #include #include "parser.h" +#include "parser_proc.h" -PSI_Parser *PSI_ParserInit(PSI_Parser *P) +void *PSI_ParserProcAlloc(void*(unsigned long)); +void PSI_ParserProcFree(void*, void(*)(void*)); +void PSI_ParserProc(void *, token_t, PSI_Token *, PSI_Parser *); + +PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename) { FILE *fp; @@ -31,6 +36,8 @@ PSI_Parser *PSI_ParserInit(PSI_Parser *P) P->proc = PSI_ParserProcAlloc(malloc); PSI_ParserFill(P, 0); + + return P; } size_t PSI_ParserFill(PSI_Parser *P, size_t n) @@ -68,43 +75,25 @@ size_t PSI_ParserFill(PSI_Parser *P, size_t n) return P->lim - P->cur; } -int PSI_ParserParse(PSI_Parser *P) +void PSI_ParserParse(PSI_Parser *P, PSI_Token *T) { - PSI_Token *T = NULL; - - if (-1 == PSI_ParserScan(P)) { + if (T) { + PSI_ParserProc(P->proc, T->type, T, P); + } else { PSI_ParserProc(P->proc, 0, NULL, P); - return 0; - } - if (!(T = PSI_TokenAlloc(P, t))) { - return 0; } - - PSI_ParserProc(P->proc, P->num, T, P); - return 1; } void PSI_ParserDtor(PSI_Parser *P) { PSI_ParserProcFree(P->proc, free); + if (P->fp) { fclose(P->fp); } - if (P->fn) { - free(P->fn); - } - if (P->lib) { - free(P->lib); - } - if (P->defs) { - free_decl_typedefs(P->defs); - } - if (P->decls) { - free_decls(P->decls); - } - if (P->impls) { - free_impls(P->impls); - } + + PSI_DataDtor((PSI_Data *) P); + memset(P, 0, sizeof(*P)); } @@ -127,7 +116,8 @@ void PSI_ParserFree(PSI_Parser **P) #define RETURN(t) do { \ P->num = t; \ return t; \ -} +} while(1) + token_t PSI_ParserScan(PSI_Parser *P) { for (;;) { @@ -138,7 +128,7 @@ token_t PSI_ParserScan(PSI_Parser *P) re2c:define:YYCURSOR = P->cur; re2c:define:YYLIMIT = P->lim; re2c:define:YYMARKER = P->mrk; - re2c:define:YYFILL = "{ if (!fill(P,@@)) RETURN(-1); }"; + re2c:define:YYFILL = "{ if (!PSI_ParserFill(P,@@)) RETURN(-1); }"; re2c:yyfill:parameter = 0; B = [^a-zA-Z0-9_]; diff --git a/idl/parser.y b/idl/parser.y deleted file mode 100644 index de0de4a..0000000 --- a/idl/parser.y +++ /dev/null @@ -1,340 +0,0 @@ -%include { -#include -#include -#include -#include - -#include "parser.h" - -static void syntax_error(const char *fn, size_t ln, const char *msg, ...) { - fprintf(stderr, "WARNING: Syntax error on line %zu in '%s'%s", ln, fn, msg ? ": ": "\n"); - if (msg) { - va_list argv; - - va_start(argv, msg); - vfprintf(stderr, msg, argv); - va_end(argv); - } -} - -} - -%name PSI_Parser -%token_prefix PSI_T_ -%token_type {PSI_Token *} -%token_destructor {free($$);} -%extra_argument {PSI_Lexer *L} -/* TOKEN is defined inside syntax_error */ -%syntax_error { - syntax_error(L->fn, L->line, "Unexpected token '%s'.\n", TOKEN->text); -} -file ::= blocks. - -blocks ::= block. -blocks ::= blocks block. - -block ::= COMMENT. - -block ::= LIB(T) QUOTED_STRING(libname) EOS. { - if (L->lib) { - syntax_error(L->fn, T->line, "Extra 'lib %s' statement has no effect.\n", libname->text); - } else { - L->lib = strndup(libname->text + 1, libname->size - 2); - } - free(libname); - free(T); -} - -block ::= decl(decl). { - L->decls = add_decl(L->decls, decl); -} -block ::= impl(impl). { - L->impls = add_impl(L->impls, impl); -} -block ::= decl_typedef(def). { - L->defs = add_decl_typedef(L->defs, def); -} - -%type decl_typedef {decl_typedef*} -decl_typedef(def) ::= TYPEDEF NAME(ALIAS) decl_type(type) EOS. { - def = init_decl_typedef(ALIAS->text, type); - free(ALIAS); -} - -%type decl {decl*} -decl(decl) ::= decl_abi(abi) decl_arg(func) LPAREN decl_args(args) RPAREN EOS. { - decl = init_decl(abi, func, args); -} - -%type decl_abi {decl_abi*} -decl_abi(abi) ::= NAME(T). { - abi = init_decl_abi(T->text); -} - -%type decl_var {decl_var*} -decl_var(var) ::= NAME(T). { - var = init_decl_var(T->text, 0); - free(T); -} -decl_var(var) ::= pointers(p) NAME(T). { - var = init_decl_var(T->text, p); - free(T); -} - -%type decl_vars {decl_vars*} -decl_vars(vars) ::= decl_var(var). { - vars = init_decl_vars(var); -} -decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). { - vars = add_decl_var(vars_, var); -} - -%type decl_arg {decl_arg*} -decl_arg(arg) ::= decl_type(type) decl_var(var). { - arg = init_decl_arg(type, var); -} - -%type decl_args {decl_args*} -decl_args(args) ::= decl_arg(arg). { - args = init_decl_args(arg); -} -decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). { - args = add_decl_arg(args_, arg); -} - -%type decl_type {decl_type*} -decl_type(type_) ::= VOID(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} -decl_type(type_) ::= INT(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} -decl_type(type_) ::= FLOAT(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} -decl_type(type_) ::= DOUBLE(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} -decl_type(type_) ::= SINT8(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} -decl_type(type_) ::= UINT8(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} -decl_type(type_) ::= SINT16(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} -decl_type(type_) ::= UINT16(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} -decl_type(type_) ::= SINT32(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} -decl_type(type_) ::= UINT32(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} -decl_type(type_) ::= SINT64(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} -decl_type(type_) ::= UINT64(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} -decl_type(type_) ::= NAME(T). { - type_ = init_decl_type(T->type, T->text); - free(T); -} - -%type impl {impl*} -impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. { - impl = init_impl(func, stmts); -} - -%type impl_func {impl_func*} -impl_func(func) ::= FUNCTION NSNAME(NAME) LPAREN impl_args(args) RPAREN COLON impl_type(type). { - func = init_impl_func(NAME->text, args, type); - free(NAME); -} -impl_func(func) ::= FUNCTION NSNAME(NAME) LPAREN RPAREN COLON impl_type(type). { - func = init_impl_func(NAME->text, NULL, type); - free(NAME); -} - -%type impl_def_val {impl_def_val*} -impl_def_val(def) ::= NULL. { - /* FIXME */ - def = init_impl_def_val(); -} -impl_def_val(def) ::= number. { - /* FIXME */ - def = init_impl_def_val(); -} - -%type impl_var {impl_var*} -impl_var(var) ::= DOLLAR NAME(T). { - var = init_impl_var(T->text, 0); - free(T); -} -impl_var(var) ::= REFERENCE DOLLAR NAME(T). { - var = init_impl_var(T->text, 1); - free(T); -} - -%type impl_arg {impl_arg*} -impl_arg(arg) ::= impl_type(type) impl_var(var). { - arg = init_impl_arg(type, var, NULL); -} -impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). { - arg = init_impl_arg(type, var, def); -} - -%type impl_args {impl_args*} -impl_args(args) ::= impl_arg(arg). { - args = init_impl_args(arg); -} -impl_args(args) ::= impl_args(args_) COMMA impl_arg(arg). { - args = add_impl_arg(args_, arg); -} - -%type impl_stmts {impl_stmts*} -impl_stmts(stmts) ::= impl_stmt(stmt). { - stmts = init_impl_stmts(stmt); -} -impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). { - stmts = add_impl_stmt(stmts_, stmt); -} - -%type impl_stmt {impl_stmt*} -impl_stmt(stmt) ::= let_stmt(let). { - stmt = init_impl_stmt(PSI_T_LET, let); -} -impl_stmt(stmt) ::= set_stmt(set). { - stmt = init_impl_stmt(PSI_T_SET, set); -} -impl_stmt(stmt) ::= ret_stmt(ret). { - stmt = init_impl_stmt(PSI_T_RET, ret); -} - -%type let_stmt {let_stmt*} -let_stmt(let) ::= LET decl_var(var) EQUALS let_value(val) EOS. { - let = init_let_stmt(var, val); -} - -%type let_value {let_value*} -let_value(val) ::= let_func(func) LPAREN impl_var(var) RPAREN. { - val = init_let_value(func, var, 0); -} -let_value(val) ::= REFERENCE NULL. { - val = init_let_value(NULL, NULL, 1); -} -let_value(val) ::= NULL. { - val = init_let_value(NULL, NULL, 0); -} - -%type let_func {let_func*} -let_func(func) ::= STRVAL(T). { - func = init_let_func(T->type, T->text); - free(T); -} -let_func(func) ::= INTVAL(T). { - func = init_let_func(T->type, T->text); - free(T); -} -let_func(func) ::= FLOATVAL(T). { - func = init_let_func(T->type, T->text); - free(T); -} -let_func(func) ::= BOOLVAL(T). { - func = init_let_func(T->type, T->text); - free(T); -} - -%type set_stmt {set_stmt*} -set_stmt(set) ::= SET impl_var(var) EQUALS set_value(val) EOS. { - set = init_set_stmt(var, val); -} - -%type set_value {set_value*} -set_value(val) ::= set_func(func) LPAREN decl_vars(vars) RPAREN. { - val = init_set_value(func, vars); -} - -%type set_func {set_func*} -set_func(func) ::= TO_STRING(T). { - func = init_set_func(T->type, T->text); - free(T); -} -set_func(func) ::= TO_INT(T). { - func = init_set_func(T->type, T->text); - free(T); -} -set_func(func) ::= TO_FLOAT(T). { - func = init_set_func(T->type, T->text); - free(T); -} -set_func(func) ::= TO_BOOL(T). { - func = init_set_func(T->type, T->text); - free(T); -} - -%type ret_stmt {ret_stmt*} -ret_stmt(ret) ::= RET set_func(func) LPAREN decl_var(var) RPAREN EOS. { - ret = init_ret_stmt(func, var); -} - -%type impl_type {impl_type*} -impl_type(type_) ::= VOID(T). { - type_ = init_impl_type(T->type, T->text); - free(T); -} -impl_type(type_) ::= MIXED(T). { - type_ = init_impl_type(T->type, T->text); - free(T); -} -impl_type(type_) ::= BOOL(T). { - type_ = init_impl_type(T->type, T->text); - free(T); -} -impl_type(type_) ::= INT(T). { - type_ = init_impl_type(T->type, T->text); - free(T); -} -impl_type(type_) ::= FLOAT(T). { - type_ = init_impl_type(T->type, T->text); - free(T); -} -impl_type(type_) ::= STRING(T). { - type_ = init_impl_type(T->type, T->text); - free(T); -} -impl_type(type_) ::= ARRAY(T). { - type_ = init_impl_type(T->type, T->text); - free(T); -} - -digits ::= DIGIT. -digits ::= digits DIGIT. -decimals ::= digits DOT digits. -decimals ::= DOT digits. -decimals ::= digits DOT. -number ::= digits. -number ::= PLUS digits. -number ::= MINUS digits. -number ::= decimals. -number ::= MINUS decimals. -number ::= PLUS decimals. - -%type pointers {unsigned} -pointers(p) ::= POINTER. {++p;} -pointers(p) ::= pointers(P) POINTER. {p = ++P;} diff --git a/idl/parser_proc.h b/idl/parser_proc.h new file mode 100644 index 0000000..3be3c66 --- /dev/null +++ b/idl/parser_proc.h @@ -0,0 +1,50 @@ +#define PSI_T_COMMENT 1 +#define PSI_T_LIB 2 +#define PSI_T_QUOTED_STRING 3 +#define PSI_T_EOS 4 +#define PSI_T_TYPEDEF 5 +#define PSI_T_NAME 6 +#define PSI_T_LPAREN 7 +#define PSI_T_RPAREN 8 +#define PSI_T_COMMA 9 +#define PSI_T_VOID 10 +#define PSI_T_INT 11 +#define PSI_T_FLOAT 12 +#define PSI_T_DOUBLE 13 +#define PSI_T_SINT8 14 +#define PSI_T_UINT8 15 +#define PSI_T_SINT16 16 +#define PSI_T_UINT16 17 +#define PSI_T_SINT32 18 +#define PSI_T_UINT32 19 +#define PSI_T_SINT64 20 +#define PSI_T_UINT64 21 +#define PSI_T_LBRACE 22 +#define PSI_T_RBRACE 23 +#define PSI_T_FUNCTION 24 +#define PSI_T_NSNAME 25 +#define PSI_T_COLON 26 +#define PSI_T_NULL 27 +#define PSI_T_DOLLAR 28 +#define PSI_T_REFERENCE 29 +#define PSI_T_EQUALS 30 +#define PSI_T_LET 31 +#define PSI_T_STRVAL 32 +#define PSI_T_INTVAL 33 +#define PSI_T_FLOATVAL 34 +#define PSI_T_BOOLVAL 35 +#define PSI_T_SET 36 +#define PSI_T_TO_STRING 37 +#define PSI_T_TO_INT 38 +#define PSI_T_TO_FLOAT 39 +#define PSI_T_TO_BOOL 40 +#define PSI_T_RET 41 +#define PSI_T_MIXED 42 +#define PSI_T_BOOL 43 +#define PSI_T_STRING 44 +#define PSI_T_ARRAY 45 +#define PSI_T_DIGIT 46 +#define PSI_T_DOT 47 +#define PSI_T_PLUS 48 +#define PSI_T_MINUS 49 +#define PSI_T_POINTER 50 diff --git a/idl/parser_proc.y b/idl/parser_proc.y index 3ad10fb..47de153 100644 --- a/idl/parser_proc.y +++ b/idl/parser_proc.y @@ -4,6 +4,8 @@ #include #include +#include "parser.h" + static void syntax_error(const char *fn, size_t ln, const char *msg, ...) { fprintf(stderr, "WARNING: Syntax error on line %zu in '%s'%s", ln, fn, msg ? ": ": "\n"); if (msg) { @@ -20,10 +22,10 @@ %token_prefix PSI_T_ %token_type {PSI_Token *} %token_destructor {free($$);} -%extra_argument {PSI_Lexer *L} +%extra_argument {PSI_Parser *P} /* TOKEN is defined inside syntax_error */ %syntax_error { - syntax_error(L->fn, L->line, "Unexpected token '%s'.\n", TOKEN->text); + syntax_error(P->fn, P->line, "Unexpected token '%s'.\n", TOKEN->text); } file ::= blocks. @@ -33,23 +35,23 @@ blocks ::= blocks block. block ::= COMMENT. block ::= LIB(T) QUOTED_STRING(libname) EOS. { - if (L->lib) { - syntax_error(L->fn, T->line, "Extra 'lib %s' statement has no effect.\n", libname->text); + if (P->lib) { + syntax_error(P->fn, T->line, "Extra 'lib %s' statement has no effect.\n", libname->text); } else { - L->lib = strndup(libname->text + 1, libname->size - 2); + P->lib = strndup(libname->text + 1, libname->size - 2); } free(libname); free(T); } block ::= decl(decl). { - L->decls = add_decl(L->decls, decl); + P->decls = add_decl(P->decls, decl); } block ::= impl(impl). { - L->impls = add_impl(L->impls, impl); + P->impls = add_impl(P->impls, impl); } block ::= decl_typedef(def). { - L->defs = add_decl_typedef(L->defs, def); + P->defs = add_decl_typedef(P->defs, def); } %type decl_typedef {decl_typedef*} diff --git a/idl/types.h b/idl/types.h deleted file mode 100644 index e3a0ead..0000000 --- a/idl/types.h +++ /dev/null @@ -1,617 +0,0 @@ - -typedef int token_t; - -typedef struct PSI_Token { - token_t type; - unsigned line; - size_t size; - char text[1]; -} PSI_Token; - -typedef struct decl_type { - char *name; - token_t type; - struct decl_type *real; -} decl_type; - -static inline decl_type *init_decl_type(token_t type, char *name) { - decl_type *t = malloc(sizeof(*t)); - t->type = type; - t->name = strdup(name); - return t; -} - -static inline void free_decl_type(decl_type *type) { - free(type->name); - free(type); -} - -typedef struct decl_typedef { - char *alias; - decl_type *type; -} decl_typedef; - -static inline decl_typedef *init_decl_typedef(char *name, decl_type *type) { - decl_typedef *t = malloc(sizeof(*t)); - t->alias = strdup(name); - t->type = type; - return t; -} - -static inline void free_decl_typedef(decl_typedef *t) { - free(t->alias); - free_decl_type(t->type); - free(t); -} - -typedef struct decl_typedefs { - size_t count; - decl_typedef **list; -} decl_typedefs; - -static decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_typedef *def) { - if (!defs) { - defs = calloc(1, sizeof(*defs)); - } - defs->list = realloc(defs->list, ++defs->count * sizeof(*defs->list)); - defs->list[defs->count-1] = def; - return defs; -} - -static void free_decl_typedefs(decl_typedefs *defs) { - size_t i; - - for (i = 0; i < defs->count; ++i) { - free_decl_typedef(defs->list[i]); - } - free(defs->list); - free(defs); -} - -typedef struct decl_var { - char *name; - unsigned pointer_level; -} decl_var; - -static inline decl_var *init_decl_var(char *name, unsigned pl) { - decl_var *v = malloc(sizeof(*v)); - v->name = (char *) strdup((const char *) name); - v->pointer_level = pl; - return v; -} - -static inline void free_decl_var(decl_var *var) { - free(var->name); - free(var); -} - -typedef struct decl_arg { - decl_type *type; - decl_var *var; -} decl_arg; - -static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) { - decl_arg *arg = malloc(sizeof(*arg)); - arg->type = type; - arg->var = var; - return arg; -} - -static inline void free_decl_arg(decl_arg *arg) { - free_decl_type(arg->type); - free_decl_var(arg->var); - free(arg); -} - -typedef struct decl_vars { - decl_var **vars; - size_t count; -} decl_vars; - -static inline decl_vars *init_decl_vars(decl_var *var) { - decl_vars *vars = malloc(sizeof(*vars)); - vars->count = 1; - vars->vars = malloc(sizeof(*vars->vars)); - vars->vars[0] = var; - return vars; -} - -static inline decl_vars *add_decl_var(decl_vars *vars, decl_var *var) { - vars->vars = realloc(vars->vars, ++vars->count * sizeof(*vars->vars)); - vars->vars[vars->count-1] = var; - return vars; -} - -static inline void free_decl_vars(decl_vars *vars) { - size_t i; - - for (i = 0; i < vars->count; ++i) { - free_decl_var(vars->vars[i]); - } - free(vars->vars); - free(vars); -} - -typedef struct decl_args { - decl_arg **args; - size_t count; -} decl_args; - -static inline decl_args *init_decl_args(decl_arg *arg) { - decl_args *args = malloc(sizeof(*args)); - args->count = 1; - args->args = malloc(sizeof(*args->args)); - args->args[0] = arg; - return args; -} - -static inline decl_args *add_decl_arg(decl_args *args, decl_arg *arg) { - args->args = realloc(args->args, ++args->count * sizeof(*args->args)); - args->args[args->count-1] = arg; - return args; -} - -static inline void free_decl_args(decl_args *args) { - size_t i; - - for (i = 0; i < args->count; ++i) { - free_decl_arg(args->args[i]); - } - free(args->args); - free(args); -} - -typedef struct decl_abi { - char *convention; -} decl_abi; - -static inline decl_abi *init_decl_abi(char *convention) { - decl_abi *abi = malloc(sizeof(*abi)); - abi->convention = strdup(convention); - return abi; -} - -static inline void free_decl_abi(decl_abi *abi) { - free(abi->convention); - free(abi); -} - -typedef struct decl { - decl_abi *abi; - decl_arg *func; - decl_args *args; - void *dlptr; -} decl; - -static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) { - decl *d = malloc(sizeof(*d)); - d->abi = abi; - d->func = func; - d->args = args; - return d; -} - -static inline void free_decl(decl *d) { - free_decl_abi(d->abi); - free_decl_arg(d->func); - free_decl_args(d->args); - free(d); -} - -typedef struct decls { - size_t count; - decl **list; -} decls; - -static inline decls *add_decl(decls *decls, decl *decl) { - if (!decls) { - decls = calloc(1, sizeof(*decls)); - } - decls->list = realloc(decls->list, ++decls->count * sizeof(*decls->list)); - decls->list[decls->count-1] = decl; - return decls; -} - -static inline void free_decls(decls *decls) { - size_t i; - - for (i = 0; i < decls->count; ++i) { - free_decl(decls->list[i]); - } - free(decls->list); - free(decls); -} - -typedef struct impl_type { - char *name; - token_t type; -} impl_type; - -static inline impl_type *init_impl_type(token_t type, char *name) { - impl_type *t = malloc(sizeof(*t)); - - t->type = type; - t->name = (char *) strdup((const char *) name); - return t; -} - -static inline void free_impl_type(impl_type *type) { - free(type->name); - free(type); -} - -typedef struct impl_var { - char *name; - unsigned reference:1; -} impl_var; - -static inline impl_var *init_impl_var(char *name, int is_reference) { - impl_var *var = malloc(sizeof(*var)); - var->name = (char *) strdup((const char *) name); - var->reference = is_reference; - return var; -} - -static inline void free_impl_var(impl_var *var) { - free(var->name); - free(var); -} - -typedef struct impl_def_val { - token_t type; - union { - int64_t digits; - double decimals; - } v; - unsigned is_null:1; -} impl_def_val; - -static inline impl_def_val *init_impl_def_val() { - impl_def_val *def = malloc(sizeof(*def)); - def->type = 0; - def->is_null = 1; - return def; -} - -static inline void free_impl_def_val(impl_def_val *def) { - free(def); -} - -typedef struct impl_arg { - impl_type *type; - impl_var *var; - impl_def_val *def; -} impl_arg; - -static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) { - impl_arg *arg = malloc(sizeof(*arg)); - arg->type = type; - arg->var = var; - arg->def = def; - return arg; -} - -static inline void free_impl_arg(impl_arg *arg) { - free_impl_type(arg->type); - free_impl_var(arg->var); - if (arg->def) { - free_impl_def_val(arg->def); - } - free(arg); -} - -typedef struct impl_args { - impl_arg **args; - size_t count; -} impl_args; - -static inline impl_args *init_impl_args(impl_arg *arg) { - impl_args *args = malloc(sizeof(*args)); - args->args = malloc(sizeof(*args->args)); - if (arg) { - args->count = 1; - args->args[0] = arg; - } else { - args->count = 0; - args->args = NULL; - } - return args; -} - -static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) { - args->args = realloc(args->args, ++args->count * sizeof(*args->args)); - args->args[args->count-1] = arg; - return args; -} - -static inline void free_impl_args(impl_args *args) { - size_t i; - - for (i = 0; i < args->count; ++i) { - free_impl_arg(args->args[i]); - } - free(args->args); - free(args); -} - -typedef struct impl_func { - char *name; - impl_args *args; - impl_type *return_type; -} impl_func; - -static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type) { - impl_func *func = malloc(sizeof(*func)); - func->name = strdup(name); - func->args = args ? args : init_impl_args(NULL); - func->return_type = type; - return func; -} - -static inline void free_impl_func(impl_func *f) { - free_impl_type(f->return_type); - free_impl_args(f->args); - free(f->name); - free(f); -} - -typedef struct let_func { - token_t type; - char *name; -} let_func; - -static inline let_func *init_let_func(token_t type, char *name) { - let_func *func = malloc(sizeof(*func)); - func->type = type; - func->name = (char *) strdup((const char *) name); - return func; -} - -static inline void free_let_func(let_func *func) { - free(func->name); - free(func); -} - -typedef struct let_value { - let_func *func; - impl_var *var; - unsigned null_pointer_ref:1; -} let_value; - -static inline let_value *init_let_value(let_func *func, impl_var *var, int null_pointer_ref) { - let_value *val = malloc(sizeof(*val)); - val->null_pointer_ref = null_pointer_ref; - val->func = func; - val->var = var; - return val; -} - -static inline void free_let_value(let_value *val) { - if (val->func) { - free_let_func(val->func); - } - if (val->var) { - free_impl_var(val->var); - } - free(val); -} - -typedef struct let_stmt { - decl_var *var; - let_value *val; -} let_stmt; - -static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) { - let_stmt *let = malloc(sizeof(*let)); - let->var = var; - let->val = val; - return let; -} - -static inline void free_let_stmt(let_stmt *stmt) { - free_decl_var(stmt->var); - free_let_value(stmt->val); - free(stmt); -} - -typedef struct set_func { - token_t type; - char *name; -} set_func; - -static inline set_func *init_set_func(token_t type, char *name) { - set_func *func = malloc(sizeof(*func)); - func->type = type; - func->name = (char *) strdup((const char *) name); - return func; -} - -static inline void free_set_func(set_func *func) { - free(func->name); - free(func); -} - -typedef struct set_value { - set_func *func; - decl_vars *vars; -} set_value; - -static inline set_value *init_set_value(set_func *func, decl_vars *vars) { - set_value *val = malloc(sizeof(*val)); - val->func = func; - val->vars = vars; - return val; -} - -static inline void free_set_value(set_value *val) { - free_set_func(val->func); - free_decl_vars(val->vars); - free(val); -} - -typedef struct set_stmt { - impl_var *var; - set_value *val; -} set_stmt; - -static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) { - set_stmt *set = malloc(sizeof(*set)); - set->var = var; - set->val = val; - return set; -} - -static inline void free_set_stmt(set_stmt *set) { - free_impl_var(set->var); - free_set_value(set->val); - free(set); -} - -typedef struct ret_stmt { - set_func *func; - decl_var *decl; -} ret_stmt; - -static inline ret_stmt *init_ret_stmt(set_func *func, decl_var *decl) { - ret_stmt *ret = malloc(sizeof(*ret)); - ret->func = func; - ret->decl = decl; - return ret; -} - -static inline void free_ret_stmt(ret_stmt *ret) { - free_set_func(ret->func); - free_decl_var(ret->decl); - free(ret); -} - -typedef struct impl_stmt { - token_t type; - union { - let_stmt *let; - set_stmt *set; - ret_stmt *ret; - void *ptr; - } s; -} impl_stmt; - -static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) { - impl_stmt *stmt = malloc(sizeof(*stmt)); - stmt->type = type; - stmt->s.ptr = ptr; - return stmt; -} - -static inline void free_impl_stmt(impl_stmt *stmt) { - switch (stmt->type) { - case PSI_T_LET: - free_let_stmt(stmt->s.let); - break; - case PSI_T_SET: - free_set_stmt(stmt->s.set); - break; - case PSI_T_RET: - free_ret_stmt(stmt->s.ret); - break; - } - free(stmt); -} - -typedef struct impl_stmts { - impl_stmt **stmts; - size_t count; -} impl_stmts; - -static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) { - impl_stmts *stmts = malloc(sizeof(*stmts)); - stmts->count = 1; - stmts->stmts = malloc(sizeof(*stmts->stmts)); - stmts->stmts[0] = stmt; - return stmts; -} - -static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) { - stmts->stmts = realloc(stmts->stmts, ++stmts->count * sizeof(*stmts->stmts)); - stmts->stmts[stmts->count-1] = stmt; - return stmts; -} - -static inline void free_impl_stmts(impl_stmts *stmts) { - size_t i; - - for (i = 0; i < stmts->count; ++i) { - free_impl_stmt(stmts->stmts[i]); - } - free(stmts->stmts); - free(stmts); -} - -typedef struct impl { - impl_func *func; - impl_stmts *stmts; -} impl; - -static inline impl *init_impl(impl_func *func, impl_stmts *stmts) { - impl *i = malloc(sizeof(*i)); - i->func = func; - i->stmts = stmts; - return i; -} - -static inline void free_impl(impl *impl) { - free_impl_func(impl->func); - free_impl_stmts(impl->stmts); - free(impl); -} - -typedef struct impls { - size_t count; - impl **list; -} impls; - -static impls *add_impl(impls *impls, impl *impl) { - if (!impls) { - impls = calloc(1, sizeof(*impls)); - } - impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list)); - impls->list[impls->count-1] = impl; - return impls; -} - -static void free_impls(impls *impls) { - size_t i; - - for (i = 0; i < impls->count; ++i) { - free_impl(impls->list[i]); - } - free(impls->list); - free(impls); -} - -typedef struct PSI_Data { - decl_typedefs *defs; - decls *decls; - impls *impls; - char *lib; - char *fn; -} PSI_Data; - -EXPORT static inline void PSI_DataExchange(PSI_Data *dest, PSI_Data *src) { - memcpy(dest, src, sizeof(*dest)); - memset(src, 0, sizeof(*src)); -} - -EXPORT static inline void PSI_DataDtor(PSI_Data *data) { - if (data->defs) { - free_decl_typedefs(data->defs); - } - if (data->decls) { - free_decls(data->decls); - } - if (data->impls) { - free_impls(data->impls); - } - if (data->fn) { - free(data->fn); - } -} diff --git a/idl/validator.c b/idl/validator.c index 54e6468..87951eb 100644 --- a/idl/validator.c +++ b/idl/validator.c @@ -6,32 +6,21 @@ #include "validator.h" -PSI_Validator *PSI_ValidatorInit(PSI_Validator *V, PSI_Lexer *L) +PSI_Validator *PSI_ValidatorInit(PSI_Validator *V, PSI_Parser *P) { if (!V) { V = malloc(sizeof(*V)); } memset(V, 0, sizeof(*V)); - PSI_DataExchange((PSI_Data *) V, (PSI_Data *) L); + PSI_DataExchange((PSI_Data *) V, (PSI_Data *) P); return V; } void PSI_ValidatorDtor(PSI_Validator *V) { - if (V->defs) { - free_decl_typedefs(V->defs); - } - if (V->decls) { - free_decls(V->decls); - } - if (V->impls) { - free_impls(V->impls); - } - if (V->fn) { - free(V->fn); - } + PSI_DataDtor((PSI_Data *) V); memset(V, 0, sizeof(*V)); } @@ -212,6 +201,9 @@ static inline int validate_impl_func(PSI_Validator *V, impl *impl, impl_func *fu } return 1; } +static inline int validate_impl_stmts(PSI_Validator *V, impl *impl, impl_stmts *stmts) { + return 1; +} static inline int validate_impl(PSI_Validator *V, impl *impl) { if (!validate_impl_func(V, impl, impl->func)) { return 0; diff --git a/idl/validator.h b/idl/validator.h index 630f967..f96111c 100644 --- a/idl/validator.h +++ b/idl/validator.h @@ -1,7 +1,7 @@ #ifndef _PSI_VALIDATOR_H #define _PSI_VALIDATOR_H -#include "lexer.h" +#include "parser.h" typedef struct PSI_Validator { decl_typedefs *defs; @@ -12,7 +12,7 @@ typedef struct PSI_Validator { void *dlopened; } PSI_Validator; -PSI_Validator *PSI_ValidatorInit(PSI_Validator *V, PSI_Lexer *L); +PSI_Validator *PSI_ValidatorInit(PSI_Validator *V, PSI_Parser *P); int PSI_ValidatorValidate(PSI_Validator *V); void PSI_ValidatorFree(PSI_Validator **V); void PSI_ValidatorDtor(PSI_Validator *V);