X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Ftypes%2Fnum_exp.c;h=9388ad3dcc2d12d4365cf9575951c0bb5b94ce79;hp=69d21e05d7fa5489b4152a77efddeb95550df846;hb=b1c7c64ca1d1d9dc79a59c0f612bd07e7a997a60;hpb=848d77002c2c0451ccb76b9059cb352fabbdd8bc diff --git a/src/types/num_exp.c b/src/types/num_exp.c index 69d21e0..9388ad3 100644 --- a/src/types/num_exp.c +++ b/src/types/num_exp.c @@ -36,7 +36,7 @@ struct psi_num_exp *psi_num_exp_init_ternary(token_t op, struct psi_num_exp *cond, struct psi_num_exp *truthy, struct psi_num_exp *falsy) { - struct psi_num_exp *exp = calloc(1, sizeof(*exp)); + struct psi_num_exp *exp = pecalloc(1, sizeof(*exp), 1); exp->op = op; exp->data.t.cond = cond; @@ -49,7 +49,7 @@ struct psi_num_exp *psi_num_exp_init_ternary(token_t op, struct psi_num_exp *psi_num_exp_init_binary(token_t op, struct psi_num_exp *lhs, struct psi_num_exp *rhs) { - struct psi_num_exp *exp = calloc(1, sizeof(*exp)); + struct psi_num_exp *exp = pecalloc(1, sizeof(*exp), 1); exp->op = op; exp->data.b.lhs = lhs; @@ -61,7 +61,7 @@ struct psi_num_exp *psi_num_exp_init_binary(token_t op, struct psi_num_exp *psi_num_exp_init_unary(token_t op, struct psi_num_exp *u) { - struct psi_num_exp *exp = calloc(1, sizeof(*exp)); + struct psi_num_exp *exp = pecalloc(1, sizeof(*exp), 1); exp->op = op; exp->data.u = u; @@ -71,7 +71,7 @@ struct psi_num_exp *psi_num_exp_init_unary(token_t op, struct psi_num_exp *psi_num_exp_init_num(struct psi_number *n) { - struct psi_num_exp *exp = calloc(1, sizeof(*exp)); + struct psi_num_exp *exp = pecalloc(1, sizeof(*exp), 1); exp->op = PSI_T_NUMBER; exp->data.n = n; @@ -82,7 +82,7 @@ struct psi_num_exp *psi_num_exp_init_num(struct psi_number *n) struct psi_num_exp *psi_num_exp_init_cast(struct psi_decl_type *typ, struct psi_num_exp *num) { - struct psi_num_exp *exp = calloc(1, sizeof(*exp)); + struct psi_num_exp *exp = pecalloc(1, sizeof(*exp), 1); exp->op = PSI_T_CAST; exp->data.c.typ = typ; @@ -99,7 +99,7 @@ struct psi_num_exp *psi_num_exp_copy(struct psi_num_exp *exp) return NULL; } - cpy = malloc(sizeof(*cpy)); + cpy = pemalloc(sizeof(*cpy), 1); *cpy = *exp; switch (exp->op) { @@ -159,6 +159,11 @@ struct psi_num_exp *psi_num_exp_copy(struct psi_num_exp *exp) return cpy; } +void psi_num_exp_copy_ctor(struct psi_num_exp **exp_ptr) +{ + *exp_ptr = psi_num_exp_copy(*exp_ptr); +} + void psi_num_exp_free(struct psi_num_exp **c_ptr) { if (*c_ptr) { @@ -216,9 +221,7 @@ void psi_num_exp_free(struct psi_num_exp **c_ptr) assert(0); } - if (c->token) { - free(c->token); - } + psi_token_free(&c->token); free(c); } @@ -287,25 +290,60 @@ static inline const char *psi_num_exp_op_tok(token_t op) return 0; } -void psi_num_exp_dump(int fd, struct psi_num_exp *exp) +struct psi_plist *psi_num_exp_tokens(struct psi_num_exp *exp, + struct psi_plist *list) { + struct psi_token *ntoken; + + if (!list) { + list = psi_plist_init((psi_plist_dtor) psi_token_free); + } + switch (exp->op) { case PSI_T_NUMBER: - psi_number_dump(fd, exp->data.n); + list = psi_number_tokens(exp->data.n, list); + break; + + case PSI_T_CAST: + ntoken = exp->data.c.typ->token; + ntoken = psi_token_init(PSI_T_LPAREN, "(", 1, ntoken->col-1, ntoken->line, ntoken->file); + list = psi_plist_add(list, &ntoken); + ntoken = psi_token_copy(exp->data.c.typ->token); + list = psi_plist_add(list, &ntoken); + ntoken = psi_token_init(PSI_T_RPAREN, ")", 1, ntoken->col+ntoken->text->len, ntoken->line, ntoken->file); + list = psi_plist_add(list, &ntoken); break; case PSI_T_NOT: case PSI_T_TILDE: - dprintf(fd, "%s", psi_num_exp_op_tok(exp->op)); - psi_num_exp_dump(fd, exp->data.u); + unary: + ntoken = psi_token_copy(exp->token); + list = psi_plist_add(list, &ntoken); + list = psi_num_exp_tokens(exp->data.u, list); break; case PSI_T_LPAREN: - dprintf(fd, "("); - psi_num_exp_dump(fd, exp->data.u); - dprintf(fd, ")"); + ntoken = psi_token_copy(exp->token); + list = psi_plist_add(list, &ntoken); + list = psi_num_exp_tokens(exp->data.u, list); + psi_plist_top(list, &ntoken); + ntoken = psi_token_init(PSI_T_RPAREN, ")", 1, ntoken->col+ntoken->text->len, ntoken->line, ntoken->file); + list = psi_plist_add(list, &ntoken); break; + case PSI_T_PLUS: + case PSI_T_MINUS: + if (!exp->data.b.rhs) { + goto unary; + } + /* no break */ + case PSI_T_PIPE: + case PSI_T_CARET: + case PSI_T_AMPERSAND: + case PSI_T_LSHIFT: + case PSI_T_RSHIFT: + case PSI_T_ASTERISK: + case PSI_T_SLASH: case PSI_T_OR: case PSI_T_AND: @@ -317,26 +355,91 @@ void psi_num_exp_dump(int fd, struct psi_num_exp *exp) case PSI_T_RCHEVR: case PSI_T_LCHEVR: + list = psi_num_exp_tokens(exp->data.b.lhs, list); + ntoken = psi_token_copy(exp->token); + list = psi_plist_add(list, &ntoken); + list = psi_num_exp_tokens(exp->data.b.rhs, list); + break; + + case PSI_T_IIF: + list = psi_num_exp_tokens(exp->data.t.cond, list); + ntoken = psi_token_copy(exp->token); + list = psi_plist_add(list, &ntoken); + list = psi_num_exp_tokens(exp->data.t.truthy, list); + psi_plist_top(list, &ntoken); + ntoken = psi_token_init(PSI_T_COLON, ":", 1, ntoken->col+ntoken->text->len, ntoken->line, ntoken->file); + list = psi_plist_add(list, &ntoken); + list = psi_plist_add(list, &ntoken); + list = psi_num_exp_tokens(exp->data.t.falsy, list); + break; + + default: + assert(0); + } + + return list; +} + +void psi_num_exp_dump(struct psi_dump *dump, struct psi_num_exp *exp) +{ + switch (exp->op) { + case PSI_T_NUMBER: + psi_number_dump(dump, exp->data.n); + break; + + case PSI_T_CAST: + PSI_DUMP(dump, "("); + psi_decl_type_dump(dump, exp->data.c.typ, 0); + PSI_DUMP(dump, ")"); + break; + + case PSI_T_NOT: + case PSI_T_TILDE: + unary: + PSI_DUMP(dump, "%s", psi_num_exp_op_tok(exp->op)); + psi_num_exp_dump(dump, exp->data.u); + break; + + case PSI_T_LPAREN: + PSI_DUMP(dump, "("); + psi_num_exp_dump(dump, exp->data.u); + PSI_DUMP(dump, ")"); + break; + + case PSI_T_PLUS: + case PSI_T_MINUS: + if (!exp->data.b.rhs) { + goto unary; + } + /* no break */ case PSI_T_PIPE: case PSI_T_CARET: case PSI_T_AMPERSAND: case PSI_T_LSHIFT: case PSI_T_RSHIFT: - case PSI_T_PLUS: - case PSI_T_MINUS: case PSI_T_ASTERISK: case PSI_T_SLASH: - psi_num_exp_dump(fd, exp->data.b.lhs); - dprintf(fd, " %s ", psi_num_exp_op_tok(exp->op)); - psi_num_exp_dump(fd, exp->data.b.rhs); + + case PSI_T_OR: + case PSI_T_AND: + + case PSI_T_CMP_EQ: + case PSI_T_CMP_NE: + case PSI_T_CMP_LE: + case PSI_T_CMP_GE: + case PSI_T_RCHEVR: + case PSI_T_LCHEVR: + psi_num_exp_dump(dump, exp->data.b.lhs); + PSI_DUMP(dump, " %s ", psi_num_exp_op_tok(exp->op)); + psi_num_exp_dump(dump, exp->data.b.rhs); break; case PSI_T_IIF: - psi_num_exp_dump(fd, exp->data.t.cond); - dprintf(fd, " ? "); - psi_num_exp_dump(fd, exp->data.t.truthy); - dprintf(fd, " : "); - psi_num_exp_dump(fd, exp->data.t.falsy); + psi_num_exp_dump(dump, exp->data.t.cond); + PSI_DUMP(dump, " ? "); + psi_num_exp_dump(dump, exp->data.t.truthy); + PSI_DUMP(dump, " : "); + psi_num_exp_dump(dump, exp->data.t.falsy); break; default: @@ -346,8 +449,7 @@ void psi_num_exp_dump(int fd, struct psi_num_exp *exp) } bool psi_num_exp_validate(struct psi_data *data, struct psi_num_exp *exp, - struct psi_impl *impl, struct psi_decl *cb_decl, struct psi_let_exp *current_let, - struct psi_set_exp *current_set, struct psi_decl_enum *current_enum) + struct psi_validate_scope *scope) { if (exp->op && exp->op != PSI_T_NUMBER) { switch (exp->op) { @@ -404,10 +506,16 @@ bool psi_num_exp_validate(struct psi_data *data, struct psi_num_exp *exp, exp->calc = psi_calc_bin_rshift; break; case PSI_T_PLUS: - exp->calc = psi_calc_add; + if (exp->data.b.rhs) { + exp->calc = psi_calc_add; + } break; case PSI_T_MINUS: - exp->calc = psi_calc_sub; + if (exp->data.b.rhs) { + exp->calc = psi_calc_sub; + } else { + exp->calc = psi_calc_minus; + } break; case PSI_T_ASTERISK: exp->calc = psi_calc_mul; @@ -427,19 +535,35 @@ bool psi_num_exp_validate(struct psi_data *data, struct psi_num_exp *exp, switch (exp->op) { case PSI_T_NUMBER: - return psi_number_validate(data, exp->data.n, impl, cb_decl, current_let, current_set, current_enum); + return psi_number_validate(data, exp->data.n, scope); case PSI_T_CAST: - return psi_num_exp_validate(data, exp->data.c.num, impl, cb_decl, current_let, current_set, current_enum) - && psi_decl_type_validate(data, exp->data.c.typ, 0, NULL); + return psi_num_exp_validate(data, exp->data.c.num, scope) + && psi_decl_type_validate(data, exp->data.c.typ, NULL, scope); break; case PSI_T_NOT: case PSI_T_TILDE: case PSI_T_LPAREN: - return psi_num_exp_validate(data, exp->data.u, impl, cb_decl, current_let, current_set, current_enum); + unary: + return psi_num_exp_validate(data, exp->data.u, scope); break; + case PSI_T_PLUS: + case PSI_T_MINUS: + if (!exp->data.b.rhs) { + goto unary; + } + /* no break */ + case PSI_T_PIPE: + case PSI_T_CARET: + case PSI_T_AMPERSAND: + case PSI_T_LSHIFT: + case PSI_T_RSHIFT: + case PSI_T_ASTERISK: + case PSI_T_SLASH: + case PSI_T_MODULO: + case PSI_T_OR: case PSI_T_AND: @@ -449,24 +573,13 @@ bool psi_num_exp_validate(struct psi_data *data, struct psi_num_exp *exp, case PSI_T_CMP_GE: case PSI_T_RCHEVR: case PSI_T_LCHEVR: - - case PSI_T_PIPE: - case PSI_T_CARET: - case PSI_T_AMPERSAND: - case PSI_T_LSHIFT: - case PSI_T_RSHIFT: - case PSI_T_PLUS: - case PSI_T_MINUS: - case PSI_T_ASTERISK: - case PSI_T_SLASH: - case PSI_T_MODULO: - return psi_num_exp_validate(data, exp->data.b.lhs, impl, cb_decl, current_let, current_set, current_enum) - && psi_num_exp_validate(data, exp->data.b.rhs, impl, cb_decl, current_let, current_set, current_enum); + return psi_num_exp_validate(data, exp->data.b.lhs, scope) + && psi_num_exp_validate(data, exp->data.b.rhs, scope); case PSI_T_IIF: - return psi_num_exp_validate(data, exp->data.t.cond, impl, cb_decl, current_let, current_set, current_enum) - && psi_num_exp_validate(data, exp->data.t.truthy, impl, cb_decl, current_let, current_set, current_enum) - && psi_num_exp_validate(data, exp->data.t.falsy, impl, cb_decl, current_let, current_set, current_enum); + return psi_num_exp_validate(data, exp->data.t.cond, scope) + && psi_num_exp_validate(data, exp->data.t.truthy, scope) + && psi_num_exp_validate(data, exp->data.t.falsy, scope); default: assert(0); @@ -501,7 +614,12 @@ static inline void psi_impl_val_dump(token_t t, impl_val *res, case PSI_T_DOUBLE: if (frame) PSI_DEBUG_PRINT(frame->context, " %" PRIdval, res->dval); break; - default: +#if HAVE_LONG_DOUBLE + case PSI_T_LONG_DOUBLE: + if (frame) PSI_DEBUG_PRINT(frame->context, " %" PRIldval, res->ldval); + break; +#endif + default: assert(0); } } @@ -514,7 +632,7 @@ static inline void psi_num_exp_verify_result(token_t t, impl_val *res, struct ps } static void psi_num_exp_reduce(struct psi_num_exp *exp, struct psi_plist **output_ptr, - struct psi_plist **input_ptr, struct psi_call_frame *frame, HashTable *defs) + struct psi_plist **input_ptr, struct psi_call_frame *frame, struct psi_cpp *cpp) { struct psi_plist *output = *output_ptr, *input = *input_ptr; struct element { @@ -528,14 +646,14 @@ static void psi_num_exp_reduce(struct psi_num_exp *exp, struct psi_plist **outpu switch (exp->op) { case PSI_T_NUMBER: - entry.type = psi_number_eval(exp->data.n, &entry.data.value, frame, defs); + entry.type = psi_number_eval(exp->data.n, &entry.data.value, frame, cpp, exp); output = psi_plist_add(output, &entry); break; case PSI_T_LPAREN: entry.type = exp->op; input = psi_plist_add(input, &entry); - psi_num_exp_reduce(exp->data.u, &output, &input, frame, defs); + psi_num_exp_reduce(exp->data.u, &output, &input, frame, cpp); while (psi_plist_pop(input, &entry)) { if (entry.type == PSI_T_LPAREN) { break; @@ -558,7 +676,7 @@ static void psi_num_exp_reduce(struct psi_num_exp *exp, struct psi_plist **outpu entry.type = exp->op; entry.data.cast = exp->data.c.typ; input = psi_plist_add(input, &entry); - psi_num_exp_reduce(exp->data.c.num, &output, &input, frame, defs); + psi_num_exp_reduce(exp->data.c.num, &output, &input, frame, cpp); break; case PSI_T_NOT: @@ -575,25 +693,38 @@ static void psi_num_exp_reduce(struct psi_num_exp *exp, struct psi_plist **outpu entry.type = exp->op; entry.data.calc = exp->calc; input = psi_plist_add(input, &entry); - psi_num_exp_reduce(exp->data.u, &output, &input, frame, defs); + psi_num_exp_reduce(exp->data.u, &output, &input, frame, cpp); break; case PSI_T_IIF: { impl_val cond_val = {0}; - token_t cond_typ = psi_num_exp_exec(exp->data.t.cond, &cond_val, frame, defs); + token_t cond_typ = psi_num_exp_exec(exp->data.t.cond, &cond_val, frame, cpp); psi_calc_bool_not(cond_typ, &cond_val, 0, NULL, &cond_val); if (cond_val.u8) { - psi_num_exp_reduce(exp->data.t.falsy, &output, &input, frame, defs); + psi_num_exp_reduce(exp->data.t.falsy, &output, &input, frame, cpp); } else { - psi_num_exp_reduce(exp->data.t.truthy, &output, &input, frame, defs); + psi_num_exp_reduce(exp->data.t.truthy, &output, &input, frame, cpp); } } break; + case PSI_T_MINUS: + case PSI_T_PLUS: + /* unary */ + if (!exp->data.b.rhs) { + entry.type = psi_num_exp_exec(exp->data.b.lhs, &entry.data.value, frame, cpp); + + if (exp->calc) { + entry.type = exp->calc(entry.type, &entry.data.value, 0, NULL, &entry.data.value); + } + output = psi_plist_add(output, &entry); + break; + } + /* no break */ default: - psi_num_exp_reduce(exp->data.b.lhs, &output, &input, frame, defs); + psi_num_exp_reduce(exp->data.b.lhs, &output, &input, frame, cpp); while (psi_plist_top(input, &entry)) { /* bail out if exp->op > entry.type */ if (psi_calc_oper(exp->op, entry.type) == -1) { @@ -606,7 +737,7 @@ static void psi_num_exp_reduce(struct psi_num_exp *exp, struct psi_plist **outpu entry.type = exp->op; entry.data.calc = exp->calc; input = psi_plist_add(input, &entry); - psi_num_exp_reduce(exp->data.b.rhs, &output, &input, frame, defs); + psi_num_exp_reduce(exp->data.b.rhs, &output, &input, frame, cpp); break; } @@ -615,7 +746,7 @@ static void psi_num_exp_reduce(struct psi_num_exp *exp, struct psi_plist **outpu } token_t psi_num_exp_exec(struct psi_num_exp *exp, impl_val *res, - struct psi_call_frame *frame, HashTable *defs) + struct psi_call_frame *frame, struct psi_cpp *cpp) { struct psi_plist *output, *input; struct element { @@ -630,7 +761,7 @@ token_t psi_num_exp_exec(struct psi_num_exp *exp, impl_val *res, output = psi_plist_init_ex(sizeof(entry), NULL); input = psi_plist_init_ex(sizeof(entry), NULL); - psi_num_exp_reduce(exp, &output, &input, frame, defs); + psi_num_exp_reduce(exp, &output, &input, frame, cpp); while (psi_plist_pop(input, &entry)) { if (frame) PSI_DEBUG_PRINT(frame->context, " %s", psi_num_exp_op_tok(entry.type));