X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fparser_proc.y;h=15407e0df03bbc1bc5281a1b9978eaf48e493c41;hb=1cc295bbc317d62e6ba254a0dcfb00c5a7a5baaa;hp=8ada20fa4a688e0b430ff3e0279e0e709d3de9ef;hpb=9bcb1df0786a8193d65949c857baaba2f4296e84;p=m6w6%2Fext-psi diff --git a/src/parser_proc.y b/src/parser_proc.y index 8ada20f..15407e0 100644 --- a/src/parser_proc.y +++ b/src/parser_proc.y @@ -6,27 +6,56 @@ #include "plist.h" #include "parser.h" + +/* rename lemon symbols, works better than DEF(%name) */ +#define ParseAlloc psi_parser_proc_init_ex +#define Parse psi_parser_proc_parse +#define ParseTrace psi_parser_proc_trace +#define ParseFree psi_parser_proc_free_ex + +/* fwd decls */ +void *ParseAlloc(void *(*mallocProc)(size_t)); +void ParseFree(void *p, void (*freeProc)(void*)); + +/* wrappers */ +void *psi_parser_proc_init(void) +{ + return ParseAlloc(malloc); +} + +void psi_parser_proc_free(void **parser_proc) +{ + if (*parser_proc) { + ParseFree(*parser_proc, free); + *parser_proc = NULL; + } +} + } -%name psi_parser_proc_ %token_prefix PSI_T_ %token_type {struct psi_token *} %token_destructor {free($$);} %default_destructor {(void)P;} %extra_argument {struct psi_parser *P} %syntax_error { ++P->errors; if (TOKEN && TOKEN->type != PSI_T_EOF) { psi_error(PSI_WARNING, TOKEN->file, TOKEN->line, "PSI syntax error: Unexpected token '%s' at pos %u", TOKEN->text, TOKEN->col); } else { psi_error(PSI_WARNING, P->file.fn, P->line, "PSI syntax error: Unexpected end of input"); } } -%nonassoc NAME. -%left PLUS MINUS. -%left ASTERISK SLASH. -%nonassoc AMPERSAND. -%fallback NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING COUNT. %token_class const_type_token BOOL INT FLOAT STRING. %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME. %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING. -%token_class num_exp_token NUMBER NSNAME. -%token_class num_exp_op_token PLUS MINUS ASTERISK SLASH. +%token_class number_token NUMBER NSNAME. +%token_class num_exp_binary_op_token PIPE CARET AMPERSAND LSHIFT RSHIFT PLUS MINUS ASTERISK SLASH MODULO. +%token_class num_exp_unary_op_token TILDE NOT PLUS MINUS. %token_class let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL COUNT. %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID. %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE. +%nonassoc NAME. +%right NOT TILDE. +%left PIPE. +%left CARET. +%left AMPERSAND. +%left LSHIFT RSHIFT. +%left PLUS MINUS. +%left ASTERISK SLASH MODULO. +%fallback NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING COUNT. %type decl_enum {struct psi_decl_enum *} %destructor decl_enum {psi_decl_enum_free(&$$);} %type decl_enum_items {struct psi_plist*} @@ -96,6 +125,8 @@ %destructor impl_stmts {psi_plist_free($$);} %type impl_stmt {struct psi_token**} %destructor impl_stmt {psi_impl_stmt_free(&$$);} +%type number {struct psi_number*} +%destructor number {psi_number_free(&$$);} %type num_exp {struct psi_num_exp*} %destructor num_exp {psi_num_exp_free(&$$);} %type let_stmt {struct psi_let_stmt*} @@ -132,7 +163,7 @@ %destructor free_exp {psi_free_exp_free(&$$);} %type impl_type {struct psi_impl_type*} %destructor impl_type {psi_impl_type_free(&$$);} -%type reference {char} +%type reference {bool} %type indirection {unsigned} %type pointers {unsigned} file ::= blocks. @@ -696,19 +727,29 @@ impl_stmt(i) ::= set_stmt(s). { impl_stmt(i) ::= free_stmt(f). { i = (struct psi_token**) f; } -num_exp(exp) ::= num_exp_token(tok). { - exp = psi_num_exp_init(tok->type, tok->text); +number(exp) ::= number_token(tok). { + exp = psi_number_init(tok->type, tok->text); exp->token = tok; } -num_exp(exp) ::= decl_var(var). { - exp = psi_num_exp_init(PSI_T_NAME, var); +number(exp) ::= decl_var(var). { + exp = psi_number_init(PSI_T_NAME, var); exp->token = psi_token_copy(var->token); } -num_exp(exp) ::= num_exp(exp_) num_exp_op_token(operator_) num_exp(operand_). { - exp_->op = operator_->type; - exp_->operand = operand_; - exp = exp_; - free(operator_); +num_exp(exp) ::= number(num). { + exp = psi_num_exp_init_num(num); + exp->token = psi_token_copy(num->token); +} +num_exp(exp) ::= LPAREN(L) num_exp(exp_) RPAREN. { + exp = psi_num_exp_init_unary(PSI_T_LPAREN, exp_); + exp->token = L; +} +num_exp(exp) ::= num_exp(lhs_) num_exp_binary_op_token(OP) num_exp(rhs_). { + exp = psi_num_exp_init_binary(OP->type, lhs_, rhs_); + exp->token = OP; +} +num_exp(exp) ::= num_exp_unary_op_token(OP) num_exp(exp_). { + exp = psi_num_exp_init_unary(OP->type, exp_); + exp->token = OP; } let_exp(val) ::= NULL. { val = psi_let_exp_init(PSI_LET_NULL, NULL); @@ -835,8 +876,9 @@ return_stmt(ret) ::= RETURN(T) set_func(func) EOS. { ret = psi_return_stmt_init(psi_set_exp_init(PSI_SET_FUNC, func)); ret->token = T; } -free_stmt(free) ::= FREE free_exps(calls) EOS. { +free_stmt(free) ::= FREE(T) free_exps(calls) EOS. { free = psi_free_stmt_init(calls); + free->token = T; } free_exps(calls) ::= free_exp(call). { calls = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_free_exp_free), @@ -850,10 +892,10 @@ free_exp(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. { call->token = F; } reference(r) ::= . { - r = 0; + r = false; } reference(r) ::= AMPERSAND. { - r = 1; + r = true; } indirection(i) ::= .{ i = 0;