From: Michael Wallner Date: Tue, 13 Oct 2015 07:41:06 +0000 (+0200) Subject: flush X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=69008ba2a7733358974c8985caa2e5bcb1182fe1;p=m6w6%2Fext-psi flush --- diff --git a/idl/Makefile b/idl/Makefile index ccc4bf6..e17b526 100644 --- a/idl/Makefile +++ b/idl/Makefile @@ -1,16 +1,12 @@ CFLAGS += -g -D$(shell uname | tr a-z A-Z) -SOURCES := parser.c lexer.c validator.c compiler.c main.c -HEADERS := $(addsuffix .[ch], $(basename $(SOURCES))) +SOURCES := parser_proc.c parser.c lexer.c validator.c compiler.c main.c +MAKEHEADERS := $(addsuffix .[ch], $(basename $(SOURCES))) GENERATED_FILES := $(addsuffix .h, $(basename $(SOURCES))) .PHONY: all all: headers main -.PHONY: headers -headers: makeheaders $(SOURCES) - ./makeheaders $(HEADERS) types.h - GENERATED_FILES += lempar.c lempar.c: curl -sSo $@ "http://www.sqlite.org/src/raw/tool/lempar.c?name=3617143ddb9b176c3605defe6a9c798793280120" @@ -22,30 +18,17 @@ lemon.c: GENERATED_FILES += lemon lemon: lemon.c | lempar.c -GENERATED_FILES += makeheaders.c -makeheaders.c: - curl -sSo makeheaders.c "https://www.fossil-scm.org/xfer/raw/src/makeheaders.c?name=c42268ec69aae42b8dd10471fd73377d789873ca" -#http://www.hwaci.com/sw/mkhdr/makeheaders.c - -GENERATED_FILES += makeheaders -makeheaders: makeheaders.c - GENERATED_FILES += main main: $(SOURCES) -GENERATED_FILES += parser.c -parser.c: parser.y lemon - ./lemon -c -m $< +GENERATED_FILES += parser_proc.c +parser_proc.c: parser_proc.y lemon + ./lemon -c $@ GENERATED_FILES += lexer.c lexer.c: lexer.re re2c -o $@ $< -%.h: headers - -%.c: %.h - touch $@ - .PHONY: clean clean: -echo rm -f $(GENERATED_FILES) diff --git a/idl/compiler.h b/idl/compiler.h new file mode 100644 index 0000000..cbf6039 --- /dev/null +++ b/idl/compiler.h @@ -0,0 +1,15 @@ +#ifndef _PSI_COMPILER_H +#define _PSI_COMPILER_H + +#include "validator.h" + +typedef struct PSI_Compiler { + decl_typedefs *defs; + decls *decls; + impls *impls; + char *lib; + char *fn; +}; +PSI_Compiler *PSI_CompilerInit(PSI_Compiler *C, PSI_Validator *V); + +#endif diff --git a/idl/idl.h b/idl/idl.h new file mode 100644 index 0000000..f98e1cd --- /dev/null +++ b/idl/idl.h @@ -0,0 +1,644 @@ +#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 new file mode 100644 index 0000000..c41bd1d --- /dev/null +++ b/idl/lexer.h @@ -0,0 +1,18 @@ +#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 index 4c9e0fe..91c044a 100644 --- a/idl/lexer.re +++ b/idl/lexer.re @@ -1,244 +1,2 @@ -#include -#include -#include -#include -#include #include "lexer.h" - -#if INTERFACE -/*!max:re2c*/ -#define BSIZE 256 - -typedef struct PSI_Lexer { - decl_typedefs *defs; - decls *decls; - impls *impls; - char *lib; - char *fn; - FILE *fp; - size_t line; - char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE]; -} PSI_Lexer; -#endif - -#if BSIZE < YYMAXFILL -# error BSIZE must be greater than YYMAXFILL -#endif - -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; -} - -size_t PSI_LexerFill(PSI_Lexer *L, size_t n) -{ - // printf("+ Fill: n=%zu\n", n); - if (!n) { - L->cur = L->tok = L->lim = L->mrk = L->buf; - L->eof = NULL; - } - - if (!L->eof) { - size_t consumed = L->tok - L->buf; - size_t reserved = L->lim - L->tok; - size_t available = BSIZE - reserved; - size_t didread; - - if (consumed) { - memmove(L->buf, L->tok, reserved); - L->tok -= consumed; - L->cur -= consumed; - L->lim -= consumed; - L->mrk -= consumed; - } - - didread = fread(L->lim, 1, available, L->fp); - L->lim += didread; - if (didread < available) { - L->eof = L->lim; - } - - // printf("+ Fill: consumed=%zu reserved=%zu available=%zu didread=%zu\n", - // consumed, reserved, available, didread); - } - // printf("+ Fill: avail=%zu\n", L->lim - L->cur); - return L->lim - L->cur; -} - -void PSI_LexerDtor(PSI_Lexer *L) -{ - if (L->fp) { - fclose(L->fp); - } - if (L->fn) { - free(L->fn); - } - if (L->lib) { - free(L->lib); - } - if (L->defs) { - free_decl_typedefs(L->defs); - } - if (L->decls) { - free_decls(L->decls); - } - if (L->impls) { - free_impls(L->impls); - } - memset(L, 0, sizeof(*L)); -} - -void PSI_LexerFree(PSI_Lexer **L) -{ - if (*L) { - PSI_LexerDtor(*L); - free(*L); - *L = NULL; - } -} - -PSI_Lexer *PSI_LexerInit(PSI_Lexer *L, const char *filename) -{ - FILE *fp = fopen(filename, "r"); - - if (!fp) { - perror(filename); - return NULL; - } - - if (!L) { - L = malloc(sizeof(*L)); - } - memset(L, 0, sizeof(*L)); - - L->fp = fp; - L->fn = strdup(filename); - L->line = 1; - - PSI_LexerFill(L, 0); - - return L; -} - - -token_t PSI_LexerScan(PSI_Lexer *L) -{ - for (;;) { - L->tok = L->cur; - /*!re2c - re2c:indent:top = 2; - re2c:define:YYCTYPE = "unsigned char"; - re2c:define:YYCURSOR = L->cur; - re2c:define:YYLIMIT = L->lim; - re2c:define:YYMARKER = L->mrk; - re2c:define:YYFILL = "{ if (!PSI_LexerFill(L,@@)) return -1; }"; - re2c:yyfill:parameter = 0; - - B = [^a-zA-Z0-9_]; - W = [a-zA-Z0-9_]; - NAME = W+; - NSNAME = (NAME)? ("\\" NAME)+; - QUOTED_STRING = "\"" ([^\"])+ "\""; - NULL = 'NULL'; - MIXED = 'mixed'; - VOID = 'void'; - BOOL = 'bool'; - INT = 'int'; - FLOAT = 'float'; - DOUBLE = 'double'; - SINT8 = 'sint8'; - UINT8 = 'uint8'; - SINT16 = 'sint16'; - UINT16 = 'uint16'; - SINT32 = 'sint32'; - UINT32 = 'uint32'; - SINT64 = 'sint64'; - UINT64 = 'uint64'; - STRING = 'string'; - ARRAY = 'array'; - FUNCTION = 'function'; - TYPEDEF = 'typedef'; - LIB = 'lib'; - LET = 'let'; - SET = 'set'; - RET = 'ret'; - STRVAL = 'strval'; - INTVAL = 'intval'; - FLOATVAL = 'floatval'; - BOOLVAL = 'boolval'; - TO_STRING = 'to_string'; - TO_INT = 'to_int'; - TO_FLOAT = 'to_float'; - TO_BOOL = 'to_bool'; - - "#" .* "\n" { ++L->line; return PSI_T_COMMENT;} - "(" {return PSI_T_LPAREN;} - ")" {return PSI_T_RPAREN;} - ";" {return PSI_T_EOS;} - "," {return PSI_T_COMMA;} - ":" {return PSI_T_COLON;} - "{" {return PSI_T_LBRACE;} - "}" {return PSI_T_RBRACE;} - "." {return PSI_T_DOT;} - "=" {return PSI_T_EQUALS;} - "$" {return PSI_T_DOLLAR;} - "*" {return PSI_T_POINTER;} - "&" {return PSI_T_REFERENCE;} - [\r\n] { ++L->line; continue; } - [\t ]+ { continue; } - NULL {return PSI_T_NULL;} - MIXED {return PSI_T_MIXED;} - VOID {return PSI_T_VOID;} - BOOL {return PSI_T_BOOL;} - INT {return PSI_T_INT;} - FLOAT {return PSI_T_FLOAT;} - DOUBLE {return PSI_T_DOUBLE;} - SINT8 {return PSI_T_SINT8;} - UINT8 {return PSI_T_UINT8;} - SINT16 {return PSI_T_SINT16;} - UINT16 {return PSI_T_UINT16;} - SINT32 {return PSI_T_SINT32;} - UINT32 {return PSI_T_UINT32;} - SINT64 {return PSI_T_SINT64;} - UINT64 {return PSI_T_UINT64;} - STRING {return PSI_T_STRING;} - ARRAY {return PSI_T_ARRAY;} - FUNCTION {return PSI_T_FUNCTION;} - TYPEDEF {return PSI_T_TYPEDEF;} - LIB {return PSI_T_LIB;} - LET {return PSI_T_LET;} - SET {return PSI_T_SET;} - RET {return PSI_T_RET;} - STRVAL {return PSI_T_STRVAL;} - INTVAL {return PSI_T_INTVAL;} - FLOATVAL {return PSI_T_FLOATVAL;} - BOOLVAL {return PSI_T_BOOLVAL;} - TO_STRING {return PSI_T_TO_STRING;} - TO_INT {return PSI_T_TO_INT;} - TO_FLOAT {return PSI_T_TO_FLOAT;} - TO_BOOL {return PSI_T_TO_BOOL;} - [0-9] {return PSI_T_DIGIT;} - NAME {return PSI_T_NAME;} - NSNAME {return PSI_T_NSNAME;} - QUOTED_STRING {return PSI_T_QUOTED_STRING;} - */ - } - return -1; -} diff --git a/idl/main.c b/idl/main.c index c4664be..b132bbd 100644 --- a/idl/main.c +++ b/idl/main.c @@ -2,8 +2,6 @@ #include #include -#include "main.h" - static int TRACE; static void loop(PSI_Lexer *L, void *P) @@ -32,8 +30,8 @@ static void loop(PSI_Lexer *L, void *P) int main(int argc, char *argv[]) { PSI_Lexer L; + PSI_Parser P; PSI_Validator V; - void *P = PSI_ParserAlloc(malloc); TRACE = !!getenv("TRACE"); @@ -41,8 +39,12 @@ int main(int argc, char *argv[]) perror("Failed to init lexer"); return 1; } + if (!PSI_ParserInit(&P)) { + perror("Failer to init parser"); + return 1; + } - loop(&L, P); + while (PSI_ParserParse(&p, &L)); PSI_ParserFree(P, free); diff --git a/idl/old/compiler.h b/idl/old/compiler.h new file mode 100644 index 0000000..1dce838 --- /dev/null +++ b/idl/old/compiler.h @@ -0,0 +1,19 @@ +#ifndef _PSI_COMPILER_H +#define _PSI_COMPILER_H + +#include "types.h" +#include "validator.h" + +typedef struct PSI_Compiler { + decl_typedefs *defs; + decls *decls; + impls *impls; + char *lib; + char *fn; +} PSI_Compiler; + +PSI_Compiler *PSI_CompilerInit(PSI_Compiler *C, PSI_Validator *V); +void PSI_CompilerDtor(PSI_Compiler *C); +void PSI_CompilerFree(PSI_Compiler **C); + +#endif diff --git a/idl/old/lexer.h b/idl/old/lexer.h new file mode 100644 index 0000000..1e955a7 --- /dev/null +++ b/idl/old/lexer.h @@ -0,0 +1,17 @@ +#ifndef _PSI_LEXER_H +#define _PSI_LEXER_H + +#include "types.h" + +#define BSIZE 256 + + +PSI_Lexer *PSI_LexerInit(PSI_Lexer *L, const char *filename); +size_t PSI_LexerFill(PSI_Lexer *L, size_t n); +token_t PSI_LexerScan(PSI_Lexer *L); +void PSI_LexerDtor(PSI_Lexer *L); +void PSI_LexerFree(PSI_Lexer **L); + +PSI_Token *PSI_TokenAlloc(PSI_Lexer *L, token_t t); + +#endif diff --git a/idl/old/validator.h b/idl/old/validator.h new file mode 100644 index 0000000..0b8a279 --- /dev/null +++ b/idl/old/validator.h @@ -0,0 +1,21 @@ +#ifndef _PSI_VALIDATOR_H +#define _PSI_VALIDATOR_H + +#include "types.h" +#include "lexer.h" + +typedef struct PSI_Validator { + decl_typedefs *defs; + decls *decls; + impls *impls; + char *lib; + char *fn; + void *dlopened; +} PSI_Validator; + +PSI_Validator *PSI_ValidatorInit(PSI_Validator *V, PSI_Lexer *L); +int PSI_ValidatorValidate(PSI_Validator *V); +void PSI_ValidatorDtor(PSI_Validator *V); +void PSI_ValidatorFree(PSI_Validator **V); + +#endif diff --git a/idl/parser.re b/idl/parser.re new file mode 100644 index 0000000..7b7b1c5 --- /dev/null +++ b/idl/parser.re @@ -0,0 +1,234 @@ +#include +#include + +#include "parser.h" + +PSI_Parser *PSI_ParserInit(PSI_Parser *P) +{ + FILE *fp; + + if (!P) { + P = malloc(sizeof(*P)); + } + memset(P, 0, sizeof(*P)); + + fp = fopen(filename, "r"); + + if (!fp) { + perror(filename); + return NULL; + } + + if (!P) { + P = malloc(sizeof(*P)); + } + memset(P, 0, sizeof(*P)); + + P->fp = fp; + P->fn = strdup(filename); + P->line = 1; + + P->proc = PSI_ParserProcAlloc(malloc); + + PSI_ParserFill(P, 0); +} + +size_t PSI_ParserFill(PSI_Parser *P, size_t n) +{ + // printf("+ Fill: n=%zu\n", n); + if (!n) { + P->cur = P->tok = P->lim = P->mrk = P->buf; + P->eof = NULL; + } + + if (!P->eof) { + size_t consumed = P->tok - P->buf; + size_t reserved = P->lim - P->tok; + size_t available = BSIZE - reserved; + size_t didread; + + if (consumed) { + memmove(P->buf, P->tok, reserved); + P->tok -= consumed; + P->cur -= consumed; + P->lim -= consumed; + P->mrk -= consumed; + } + + didread = fread(P->lim, 1, available, P->fp); + P->lim += didread; + if (didread < available) { + P->eof = P->lim; + } + + // printf("+ Fill: consumed=%zu reserved=%zu available=%zu didread=%zu\n", + // consumed, reserved, available, didread); + } + // printf("+ Fill: avail=%zu\n", P->lim - P->cur); + return P->lim - P->cur; +} + +int PSI_ParserParse(PSI_Parser *P) +{ + PSI_Token *T = NULL; + + if (-1 == PSI_ParserScan(P)) { + 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); + } + memset(P, 0, sizeof(*P)); +} + +void PSI_ParserFree(PSI_Parser **P) +{ + if (*P) { + PSI_ParserDtor(*P); + free(*P); + *P = NULL; + } +} + +/*!max:re2c*/ +#define BSIZE 256 + +#if BSIZE < YYMAXFILL +# error BSIZE must be greater than YYMAXFILL +#endif + +#define RETURN(t) do { \ + P->num = t; \ + return t; \ +} +token_t PSI_ParserScan(PSI_Parser *P) +{ + for (;;) { + P->tok = P->cur; + /*!re2c + re2c:indent:top = 2; + re2c:define:YYCTYPE = "unsigned char"; + re2c:define:YYCURSOR = P->cur; + re2c:define:YYLIMIT = P->lim; + re2c:define:YYMARKER = P->mrk; + re2c:define:YYFILL = "{ if (!fill(P,@@)) RETURN(-1); }"; + re2c:yyfill:parameter = 0; + + B = [^a-zA-Z0-9_]; + W = [a-zA-Z0-9_]; + NAME = W+; + NSNAME = (NAME)? ("\\" NAME)+; + QUOTED_STRING = "\"" ([^\"])+ "\""; + NULL = 'NULL'; + MIXED = 'mixed'; + VOID = 'void'; + BOOL = 'bool'; + INT = 'int'; + FLOAT = 'float'; + DOUBLE = 'double'; + SINT8 = 'sint8'; + UINT8 = 'uint8'; + SINT16 = 'sint16'; + UINT16 = 'uint16'; + SINT32 = 'sint32'; + UINT32 = 'uint32'; + SINT64 = 'sint64'; + UINT64 = 'uint64'; + STRING = 'string'; + ARRAY = 'array'; + FUNCTION = 'function'; + TYPEDEF = 'typedef'; + LIB = 'lib'; + LET = 'let'; + SET = 'set'; + RET = 'ret'; + STRVAL = 'strval'; + INTVAL = 'intval'; + FLOATVAL = 'floatval'; + BOOLVAL = 'boolval'; + TO_STRING = 'to_string'; + TO_INT = 'to_int'; + TO_FLOAT = 'to_float'; + TO_BOOL = 'to_bool'; + + "#" .* "\n" { ++P->line; RETURN(PSI_T_COMMENT);} + "(" {RETURN(PSI_T_LPAREN);} + ")" {RETURN(PSI_T_RPAREN);} + ";" {RETURN(PSI_T_EOS);} + "," {RETURN(PSI_T_COMMA);} + ":" {RETURN(PSI_T_COLON);} + "{" {RETURN(PSI_T_LBRACE);} + "}" {RETURN(PSI_T_RBRACE);} + "." {RETURN(PSI_T_DOT);} + "=" {RETURN(PSI_T_EQUALS);} + "$" {RETURN(PSI_T_DOLLAR);} + "*" {RETURN(PSI_T_POINTER);} + "&" {RETURN(PSI_T_REFERENCE);} + [\r\n] { ++P->line; continue; } + [\t ]+ { continue; } + NULL {RETURN(PSI_T_NULL);} + MIXED {RETURN(PSI_T_MIXED);} + VOID {RETURN(PSI_T_VOID);} + BOOL {RETURN(PSI_T_BOOL);} + INT {RETURN(PSI_T_INT);} + FLOAT {RETURN(PSI_T_FLOAT);} + DOUBLE {RETURN(PSI_T_DOUBLE);} + SINT8 {RETURN(PSI_T_SINT8);} + UINT8 {RETURN(PSI_T_UINT8);} + SINT16 {RETURN(PSI_T_SINT16);} + UINT16 {RETURN(PSI_T_UINT16);} + SINT32 {RETURN(PSI_T_SINT32);} + UINT32 {RETURN(PSI_T_UINT32);} + SINT64 {RETURN(PSI_T_SINT64);} + UINT64 {RETURN(PSI_T_UINT64);} + STRING {RETURN(PSI_T_STRING);} + ARRAY {RETURN(PSI_T_ARRAY);} + FUNCTION {RETURN(PSI_T_FUNCTION);} + TYPEDEF {RETURN(PSI_T_TYPEDEF);} + LIB {RETURN(PSI_T_LIB);} + LET {RETURN(PSI_T_LET);} + SET {RETURN(PSI_T_SET);} + RET {RETURN(PSI_T_RET);} + STRVAL {RETURN(PSI_T_STRVAL);} + INTVAL {RETURN(PSI_T_INTVAL);} + FLOATVAL {RETURN(PSI_T_FLOATVAL);} + BOOLVAL {RETURN(PSI_T_BOOLVAL);} + TO_STRING {RETURN(PSI_T_TO_STRING);} + TO_INT {RETURN(PSI_T_TO_INT);} + TO_FLOAT {RETURN(PSI_T_TO_FLOAT);} + TO_BOOL {RETURN(PSI_T_TO_BOOL);} + [0-9] {RETURN(PSI_T_DIGIT);} + NAME {RETURN(PSI_T_NAME);} + NSNAME {RETURN(PSI_T_NSNAME);} + QUOTED_STRING {RETURN(PSI_T_QUOTED_STRING);} + */ + } + return -1; +} diff --git a/idl/parser_proc.y b/idl/parser_proc.y new file mode 100644 index 0000000..3ad10fb --- /dev/null +++ b/idl/parser_proc.y @@ -0,0 +1,337 @@ +%include { +#include +#include +#include +#include + + 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_ParserProc +%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/validator.c b/idl/validator.c index e81d923..54e6468 100644 --- a/idl/validator.c +++ b/idl/validator.c @@ -6,17 +6,6 @@ #include "validator.h" -#if INTERFACE -typedef struct PSI_Validator { - decl_typedefs *defs; - decls *decls; - impls *impls; - char *lib; - char *fn; - void *dlopened; -} PSI_Validator; -#endif - PSI_Validator *PSI_ValidatorInit(PSI_Validator *V, PSI_Lexer *L) { if (!V) { diff --git a/idl/validator.h b/idl/validator.h new file mode 100644 index 0000000..630f967 --- /dev/null +++ b/idl/validator.h @@ -0,0 +1,20 @@ +#ifndef _PSI_VALIDATOR_H +#define _PSI_VALIDATOR_H + +#include "lexer.h" + +typedef struct PSI_Validator { + decl_typedefs *defs; + decls *decls; + impls *impls; + char *lib; + char *fn; + void *dlopened; +} PSI_Validator; + +PSI_Validator *PSI_ValidatorInit(PSI_Validator *V, PSI_Lexer *L); +int PSI_ValidatorValidate(PSI_Validator *V); +void PSI_ValidatorFree(PSI_Validator **V); +void PSI_ValidatorDtor(PSI_Validator *V); + +#endif