num_exp: re-parseable dumps
[m6w6/ext-psi] / src / types / num_exp.c
index aba528853b6ec4f7208ee8c18089d3365bb20106..c211f45e8c568b09752d526a2d9b42f3cd05fe0d 100644 (file)
@@ -87,6 +87,16 @@ struct psi_num_exp *psi_num_exp_copy(struct psi_num_exp *exp)
                cpy->data.u = psi_num_exp_copy(exp->data.u);
                break;
 
+       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:
+
        case PSI_T_PIPE:
        case PSI_T_CARET:
        case PSI_T_AMPERSAND:
@@ -129,6 +139,16 @@ void psi_num_exp_free(struct psi_num_exp **c_ptr)
                        psi_num_exp_free(&c->data.u);
                        break;
 
+               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:
+
                case PSI_T_PIPE:
                case PSI_T_CARET:
                case PSI_T_AMPERSAND:
@@ -155,39 +175,57 @@ void psi_num_exp_free(struct psi_num_exp **c_ptr)
        }
 }
 
-static inline wint_t psi_num_exp_op_tok(token_t op)
+static inline const char *psi_num_exp_op_tok(token_t op)
 {
        switch (op) {
        case PSI_T_NOT:
-               return L'!';
+               return "!";
        case PSI_T_TILDE:
-               return L'~';
+               return "~";
        case PSI_T_LPAREN:
-               return L'(';
+               return "(";
 
        case PSI_T_PIPE:
-               return L'|';
+               return "|";
        case PSI_T_CARET:
-               return L'^';
+               return "^";
        case PSI_T_AMPERSAND:
-               return L'&';
+               return "&";
 
        case PSI_T_LSHIFT:
-               return L'«';
+               return "<<";
        case PSI_T_RSHIFT:
-               return L'»';
+               return ">>";
 
        case PSI_T_PLUS:
-               return L'+';
+               return "+";
        case PSI_T_MINUS:
-               return L'-';
+               return "-";
 
        case PSI_T_ASTERISK:
-               return L'*';
+               return "*";
        case PSI_T_SLASH:
-               return L'/';
+               return "/";
        case PSI_T_MODULO:
-               return L'%';
+               return "%";
+
+       case PSI_T_OR:
+               return "||";
+       case PSI_T_AND:
+               return "&&";
+
+       case PSI_T_CMP_EQ:
+               return "==";
+       case PSI_T_CMP_NE:
+               return "!=";
+       case PSI_T_CMP_LE:
+               return "<=";
+       case PSI_T_CMP_GE:
+               return ">=";
+       case PSI_T_RCHEVR:
+               return ">";
+       case PSI_T_LCHEVR:
+               return "<";
 
        default:
                assert(0);
@@ -204,7 +242,7 @@ void psi_num_exp_dump(int fd, struct psi_num_exp *exp)
 
        case PSI_T_NOT:
        case PSI_T_TILDE:
-               dprintf(fd, "%lc", psi_num_exp_op_tok(exp->op));
+               dprintf(fd, "%s", psi_num_exp_op_tok(exp->op));
                psi_num_exp_dump(fd, exp->data.u);
                break;
 
@@ -214,6 +252,17 @@ void psi_num_exp_dump(int fd, struct psi_num_exp *exp)
                dprintf(fd, ")");
                break;
 
+
+       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:
+
        case PSI_T_PIPE:
        case PSI_T_CARET:
        case PSI_T_AMPERSAND:
@@ -224,7 +273,7 @@ void psi_num_exp_dump(int fd, struct psi_num_exp *exp)
        case PSI_T_ASTERISK:
        case PSI_T_SLASH:
                psi_num_exp_dump(fd, exp->data.b.lhs);
-               dprintf(fd, " %lc ", psi_num_exp_op_tok(exp->op));
+               dprintf(fd, " %s ", psi_num_exp_op_tok(exp->op));
                psi_num_exp_dump(fd, exp->data.b.rhs);
                break;
 
@@ -247,6 +296,31 @@ bool psi_num_exp_validate(struct psi_data *data, struct psi_num_exp *exp,
                        exp->calc = psi_calc_bin_not;
                        break;
 
+               case PSI_T_OR:
+                       exp->calc = psi_calc_or;
+                       break;
+               case PSI_T_AND:
+                       exp->calc = psi_calc_and;
+                       break;
+               case PSI_T_CMP_EQ:
+                       exp->calc = psi_calc_cmp_eq;
+                       break;
+               case PSI_T_CMP_NE:
+                       exp->calc = psi_calc_cmp_ne;
+                       break;
+               case PSI_T_CMP_LE:
+                       exp->calc = psi_calc_cmp_le;
+                       break;
+               case PSI_T_CMP_GE:
+                       exp->calc = psi_calc_cmp_ge;
+                       break;
+               case PSI_T_LCHEVR:
+                       exp->calc = psi_calc_cmp_lt;
+                       break;
+               case PSI_T_RCHEVR:
+                       exp->calc = psi_calc_cmp_gt;
+                       break;
+
                case PSI_T_LPAREN:
                        break;
 
@@ -297,6 +371,16 @@ bool psi_num_exp_validate(struct psi_data *data, struct psi_num_exp *exp,
                return psi_num_exp_validate(data, exp->data.u, impl, cb_decl, current_let, current_set, current_enum);
                break;
 
+       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:
+
        case PSI_T_PIPE:
        case PSI_T_CARET:
        case PSI_T_AMPERSAND:
@@ -346,6 +430,7 @@ static inline void psi_impl_val_dump(token_t t, impl_val *res,
                assert(0);
        }
 }
+
 static inline void psi_num_exp_verify_result(token_t t, impl_val *res, struct psi_call_frame *frame)
 {
        if (frame) PSI_DEBUG_PRINT(frame->context, "%s", " = ");
@@ -353,6 +438,23 @@ 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)
 {
@@ -379,7 +481,7 @@ static void psi_num_exp_reduce(struct psi_num_exp *exp, struct psi_plist **outpu
                        if (entry.type == PSI_T_LPAREN) {
                                break;
                        }
-                       if (frame) PSI_DEBUG_PRINT(frame->context, " %lc", psi_num_exp_op_tok(entry.type));
+                       if (frame) PSI_DEBUG_PRINT(frame->context, " %s", psi_num_exp_op_tok(entry.type));
                        output = psi_plist_add(output, &entry);
                }
                break;
@@ -392,7 +494,7 @@ static void psi_num_exp_reduce(struct psi_num_exp *exp, struct psi_plist **outpu
                                break;
                        }
                        psi_plist_pop(input, NULL);
-                       if (frame) PSI_DEBUG_PRINT(frame->context, " %lc", psi_num_exp_op_tok(entry.type));
+                       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;
@@ -409,7 +511,7 @@ static void psi_num_exp_reduce(struct psi_num_exp *exp, struct psi_plist **outpu
                                break;
                        }
                        psi_plist_pop(input, NULL);
-                       if (frame) PSI_DEBUG_PRINT(frame->context, " %lc", psi_num_exp_op_tok(entry.type));
+                       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;
@@ -441,7 +543,7 @@ token_t psi_num_exp_exec(struct psi_num_exp *exp, impl_val *res,
        psi_num_exp_reduce(exp, &output, &input, frame);
 
        while (psi_plist_pop(input, &entry)) {
-               if (frame) PSI_DEBUG_PRINT(frame->context, " %lc", psi_num_exp_op_tok(entry.type));
+               if (frame) PSI_DEBUG_PRINT(frame->context, " %s", psi_num_exp_op_tok(entry.type));
                output = psi_plist_add(output, &entry);
        }
        if (frame) PSI_DEBUG_PRINT(frame->context, "%s", "\n");
@@ -455,7 +557,7 @@ token_t psi_num_exp_exec(struct psi_num_exp *exp, impl_val *res,
                case PSI_T_NOT:
                case PSI_T_TILDE:
                        psi_plist_pop(input, &rhs);
-                       if (frame) PSI_DEBUG_PRINT(frame->context, " %lc", psi_num_exp_op_tok(entry.type));
+                       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 = entry.data.calc(rhs.type, &rhs.data.value, 0, NULL, &entry.data.value);
@@ -463,6 +565,16 @@ token_t psi_num_exp_exec(struct psi_num_exp *exp, impl_val *res,
                        psi_num_exp_verify_result(entry.type, &entry.data.value, frame);
                        break;
 
+               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:
+
                case PSI_T_PIPE:
                case PSI_T_CARET:
                case PSI_T_AMPERSAND:
@@ -477,7 +589,7 @@ token_t psi_num_exp_exec(struct psi_num_exp *exp, impl_val *res,
                        psi_plist_pop(input, &lhs);
 
                        psi_impl_val_dump(lhs.type, &lhs.data.value, frame);
-                       if (frame) PSI_DEBUG_PRINT(frame->context, " %lc", psi_num_exp_op_tok(entry.type));
+                       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 = entry.data.calc(