#include "call.h"
#include "calc.h"
+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));
+
+ exp->op = op;
+ exp->data.t.cond = cond;
+ exp->data.t.truthy = truthy;
+ exp->data.t.falsy = falsy;
+
+ return exp;
+}
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));
+ exp->op = PSI_T_NUMBER;
exp->data.n = n;
return exp;
}
+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));
+
+ exp->op = PSI_T_CAST;
+ exp->data.c.typ = typ;
+ exp->data.c.num = num;
+
+ return exp;
+}
+
struct psi_num_exp *psi_num_exp_copy(struct psi_num_exp *exp)
{
struct psi_num_exp *cpy;
*cpy = *exp;
switch (exp->op) {
- case 0:
+ case PSI_T_NUMBER:
cpy->data.n = psi_number_copy(exp->data.n);
break;
+ case PSI_T_CAST:
+ cpy->data.c.typ = psi_decl_type_copy(exp->data.c.typ);
+ cpy->data.c.num = psi_num_exp_copy(exp->data.c.num);
+ break;
+
case PSI_T_NOT:
case PSI_T_TILDE:
case PSI_T_LPAREN:
cpy->data.b.rhs = psi_num_exp_copy(exp->data.b.rhs);
break;
+ case PSI_T_IIF:
+ cpy->data.t.cond = psi_num_exp_copy(exp->data.t.cond);
+ cpy->data.t.truthy = psi_num_exp_copy(exp->data.t.truthy);
+ cpy->data.t.falsy = psi_num_exp_copy(exp->data.t.falsy);
+ break;
+
default:
assert(0);
}
*c_ptr = NULL;
switch (c->op) {
- case 0:
+ case PSI_T_NUMBER:
psi_number_free(&c->data.n);
break;
+
+ case PSI_T_CAST:
+ psi_decl_type_free(&c->data.c.typ);
+ psi_num_exp_free(&c->data.c.num);
+ break;
+
case PSI_T_NOT:
case PSI_T_TILDE:
case PSI_T_LPAREN:
psi_num_exp_free(&c->data.b.rhs);
break;
+ case PSI_T_IIF:
+ psi_num_exp_free(&c->data.t.cond);
+ psi_num_exp_free(&c->data.t.truthy);
+ psi_num_exp_free(&c->data.t.falsy);
+ break;
+
default:
assert(0);
}
return "~";
case PSI_T_LPAREN:
return "(";
+ case PSI_T_CAST:
+ return "(cast)";
case PSI_T_PIPE:
return "|";
case PSI_T_LCHEVR:
return "<";
+ case PSI_T_IIF:
+ return "?";
+
default:
assert(0);
}
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 0:
- psi_number_dump(fd, exp->data.n);
+ case PSI_T_NUMBER:
+ 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->size, 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->size, 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:
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->size, 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(int fd, struct psi_num_exp *exp)
+{
+ switch (exp->op) {
+ case PSI_T_NUMBER:
+ psi_number_dump(fd, exp->data.n);
+ break;
+
+ case PSI_T_CAST:
+ dprintf(fd, "(");
+ psi_decl_type_dump(1, exp->data.c.typ, 0);
+ dprintf(fd, ")");
+ break;
+
+ case PSI_T_NOT:
+ case PSI_T_TILDE:
+ unary:
+ dprintf(fd, "%s", psi_num_exp_op_tok(exp->op));
+ psi_num_exp_dump(fd, exp->data.u);
+ break;
+
+ case PSI_T_LPAREN:
+ dprintf(fd, "(");
+ psi_num_exp_dump(fd, exp->data.u);
+ dprintf(fd, ")");
+ 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:
+
+ 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(fd, exp->data.b.lhs);
dprintf(fd, " %s ", psi_num_exp_op_tok(exp->op));
psi_num_exp_dump(fd, 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);
+ break;
+
default:
assert(0);
}
}
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) {
+ if (exp->op && exp->op != PSI_T_NUMBER) {
switch (exp->op) {
case PSI_T_NOT:
exp->calc = psi_calc_bool_not;
exp->calc = psi_calc_cmp_gt;
break;
+ case PSI_T_CAST:
case PSI_T_LPAREN:
+ case PSI_T_IIF:
break;
case PSI_T_PIPE:
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;
}
switch (exp->op) {
- case 0:
- return psi_number_validate(data, exp->data.n, impl, cb_decl, current_let, current_set, current_enum);
+ case PSI_T_NUMBER:
+ return psi_number_validate(data, exp->data.n, scope);
+
+ case PSI_T_CAST:
+ 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:
case PSI_T_CMP_GE:
case PSI_T_RCHEVR:
case PSI_T_LCHEVR:
+ 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, scope)
+ && psi_num_exp_validate(data, exp->data.t.truthy, scope)
+ && psi_num_exp_validate(data, exp->data.t.falsy, scope);
- 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);
default:
assert(0);
}
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);
}
}
union {
impl_val value;
psi_calc calc;
+ struct psi_decl_type *cast;
} data;
} entry;
switch (exp->op) {
- case 0:
- entry.type = psi_number_eval(exp->data.n, &entry.data.value, frame, defs);
+ case PSI_T_NUMBER:
+ entry.type = psi_number_eval(exp->data.n, &entry.data.value, frame, defs, exp);
output = psi_plist_add(output, &entry);
break;
}
break;
+ case PSI_T_CAST:
+ while (psi_plist_top(input, &entry)) {
+ /* bail out if exp->op >= entry.type */
+ if (psi_calc_oper(exp->op, entry.type) != 1) {
+ break;
+ }
+ psi_plist_pop(input, NULL);
+ if (frame) PSI_DEBUG_PRINT(frame->context, " %s", psi_num_exp_op_tok(entry.type));
+ output = psi_plist_add(output, &entry);
+ }
+ 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);
+ break;
+
case PSI_T_NOT:
case PSI_T_TILDE:
while (psi_plist_top(input, &entry)) {
psi_num_exp_reduce(exp->data.u, &output, &input, frame, defs);
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);
+
+ 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);
+ } else {
+ psi_num_exp_reduce(exp->data.t.truthy, &output, &input, frame, defs);
+ }
+ }
+ 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, defs);
+
+ 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);
while (psi_plist_top(input, &entry)) {
union {
impl_val value;
psi_calc calc;
+ struct psi_decl_type *cast;
} data;
} entry, lhs, rhs;
input = psi_plist_add(input, &entry);
break;
+ case PSI_T_CAST:
+ psi_plist_pop(input, &rhs);
+ if (frame) PSI_DEBUG_PRINT(frame->context, " %s", psi_num_exp_op_tok(entry.type));
+ psi_impl_val_dump(rhs.type, &rhs.data.value, frame);
+
+ entry.type = psi_decl_type_get_real(entry.data.cast)->type;
+ psi_calc_cast(rhs.type, &rhs.data.value, entry.type, &entry.data.value);
+ input = psi_plist_add(input, &entry);
+ psi_num_exp_verify_result(entry.type, &entry.data.value, frame);
+ break;
+
case PSI_T_NOT:
case PSI_T_TILDE:
psi_plist_pop(input, &rhs);