validation
[m6w6/ext-psi] / src / types / num_exp.c
index c211f45e8c568b09752d526a2d9b42f3cd05fe0d..69d21e05d7fa5489b4152a77efddeb95550df846 100644 (file)
 #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)
@@ -60,11 +73,24 @@ struct psi_num_exp *psi_num_exp_init_num(struct psi_number *n)
 {
        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;
@@ -77,10 +103,15 @@ struct psi_num_exp *psi_num_exp_copy(struct psi_num_exp *exp)
        *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:
@@ -111,6 +142,12 @@ struct psi_num_exp *psi_num_exp_copy(struct psi_num_exp *exp)
                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);
        }
@@ -130,9 +167,15 @@ void psi_num_exp_free(struct psi_num_exp **c_ptr)
                *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:
@@ -163,6 +206,12 @@ void psi_num_exp_free(struct psi_num_exp **c_ptr)
                        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);
                }
@@ -184,6 +233,8 @@ static inline const char *psi_num_exp_op_tok(token_t op)
                return "~";
        case PSI_T_LPAREN:
                return "(";
+       case PSI_T_CAST:
+               return "(cast)";
 
        case PSI_T_PIPE:
                return "|";
@@ -227,6 +278,9 @@ static inline const char *psi_num_exp_op_tok(token_t op)
        case PSI_T_LCHEVR:
                return "<";
 
+       case PSI_T_IIF:
+               return "?";
+
        default:
                assert(0);
        }
@@ -236,7 +290,7 @@ static inline const char *psi_num_exp_op_tok(token_t op)
 void psi_num_exp_dump(int fd, struct psi_num_exp *exp)
 {
        switch (exp->op) {
-       case 0:
+       case PSI_T_NUMBER:
                psi_number_dump(fd, exp->data.n);
                break;
 
@@ -277,6 +331,14 @@ void psi_num_exp_dump(int fd, struct psi_num_exp *exp)
                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);
        }
@@ -287,20 +349,20 @@ 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)
 {
-       if (exp->op) {
+       if (exp->op && exp->op != PSI_T_NUMBER) {
                switch (exp->op) {
                case PSI_T_NOT:
-                       exp->calc = psi_calc_not;
+                       exp->calc = psi_calc_bool_not;
                        break;
                case PSI_T_TILDE:
                        exp->calc = psi_calc_bin_not;
                        break;
 
                case PSI_T_OR:
-                       exp->calc = psi_calc_or;
+                       exp->calc = psi_calc_bool_or;
                        break;
                case PSI_T_AND:
-                       exp->calc = psi_calc_and;
+                       exp->calc = psi_calc_bool_and;
                        break;
                case PSI_T_CMP_EQ:
                        exp->calc = psi_calc_cmp_eq;
@@ -321,7 +383,9 @@ bool psi_num_exp_validate(struct psi_data *data, struct psi_num_exp *exp,
                        exp->calc = psi_calc_cmp_gt;
                        break;
 
+               case PSI_T_CAST:
                case PSI_T_LPAREN:
+               case PSI_T_IIF:
                        break;
 
                case PSI_T_PIPE:
@@ -362,9 +426,14 @@ bool psi_num_exp_validate(struct psi_data *data, struct psi_num_exp *exp,
        }
 
        switch (exp->op) {
-       case 0:
+       case PSI_T_NUMBER:
                return psi_number_validate(data, exp->data.n, impl, cb_decl, current_let, current_set, current_enum);
 
+       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);
+               break;
+
        case PSI_T_NOT:
        case PSI_T_TILDE:
        case PSI_T_LPAREN:
@@ -393,6 +462,12 @@ bool psi_num_exp_validate(struct psi_data *data, struct psi_num_exp *exp,
        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);
+
+       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);
+
        default:
                assert(0);
        }
@@ -438,25 +513,8 @@ static inline void psi_num_exp_verify_result(token_t t, impl_val *res, struct ps
        if (frame) PSI_DEBUG_PRINT(frame->context, "%s", "\n");
 }
 
-static inline int psi_num_exp_op_cmp(token_t op1, token_t op2)
-{
-       if (PSI_T_LPAREN == op2) {
-               return -1;
-       } else if (PSI_T_LPAREN == op1) {
-               return 1;
-       } else if (op1 == op2) {
-               return 0;
-       } else if (!op1) {
-               return 1;
-       } else if (!op2) {
-               return -1;
-       }
-
-       return psi_token_oper_cmp(op1, op2);
-}
-
 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)
+               struct psi_plist **input_ptr, struct psi_call_frame *frame, HashTable *defs)
 {
        struct psi_plist *output = *output_ptr, *input = *input_ptr;
        struct element {
@@ -464,19 +522,20 @@ static void psi_num_exp_reduce(struct psi_num_exp *exp, struct psi_plist **outpu
                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);
+       case PSI_T_NUMBER:
+               entry.type = psi_number_eval(exp->data.n, &entry.data.value, frame, defs);
                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);
+               psi_num_exp_reduce(exp->data.u, &output, &input, frame, defs);
                while (psi_plist_pop(input, &entry)) {
                        if (entry.type == PSI_T_LPAREN) {
                                break;
@@ -486,11 +545,27 @@ static void psi_num_exp_reduce(struct psi_num_exp *exp, struct psi_plist **outpu
                }
                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)) {
                        /* bail out if exp->op >= entry.type */
-                       if (psi_num_exp_op_cmp(exp->op, entry.type) != 1) {
+                       if (psi_calc_oper(exp->op, entry.type) != 1) {
                                break;
                        }
                        psi_plist_pop(input, NULL);
@@ -500,14 +575,28 @@ 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);
+               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;
 
        default:
-               psi_num_exp_reduce(exp->data.b.lhs, &output, &input, frame);
+               psi_num_exp_reduce(exp->data.b.lhs, &output, &input, frame, defs);
                while (psi_plist_top(input, &entry)) {
                        /* bail out if exp->op > entry.type */
-                       if (psi_num_exp_op_cmp(exp->op, entry.type) == -1) {
+                       if (psi_calc_oper(exp->op, entry.type) == -1) {
                                break;
                        }
                        psi_plist_pop(input, NULL);
@@ -517,7 +606,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);
+               psi_num_exp_reduce(exp->data.b.rhs, &output, &input, frame, defs);
                break;
        }
 
@@ -526,7 +615,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)
+               struct psi_call_frame *frame, HashTable *defs)
 {
        struct psi_plist *output, *input;
        struct element {
@@ -534,13 +623,14 @@ token_t psi_num_exp_exec(struct psi_num_exp *exp, impl_val *res,
                union {
                        impl_val value;
                        psi_calc calc;
+                       struct psi_decl_type *cast;
                } data;
        } entry, lhs, rhs;
 
        output = psi_plist_init_ex(sizeof(entry), NULL);
        input = psi_plist_init_ex(sizeof(entry), NULL);
 
-       psi_num_exp_reduce(exp, &output, &input, frame);
+       psi_num_exp_reduce(exp, &output, &input, frame, defs);
 
        while (psi_plist_pop(input, &entry)) {
                if (frame) PSI_DEBUG_PRINT(frame->context, " %s", psi_num_exp_op_tok(entry.type));
@@ -554,6 +644,17 @@ token_t psi_num_exp_exec(struct psi_num_exp *exp, impl_val *res,
                        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);