Changeset 36375 for trunk/Ohana/src/opihi/lib.shell/convert_to_RPN.c
- Timestamp:
- Dec 10, 2013, 2:55:11 PM (13 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
-
. (modified) (1 prop)
-
Ohana (modified) (1 prop)
-
Ohana/src/opihi (modified) (1 prop)
-
Ohana/src/opihi/lib.shell/convert_to_RPN.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
trunk/Ohana
- Property svn:mergeinfo changed
/branches/eam_branches/ipp-20130904/Ohana (added) merged: 36144-36145,36150-36151,36160,36194-36195,36236,36251,36274,36286,36369
- Property svn:mergeinfo changed
-
trunk/Ohana/src/opihi
- Property svn:mergeinfo changed
/branches/eam_branches/ipp-20130904/Ohana/src/opihi (added) merged: 36144-36145,36150-36151,36160,36195,36236,36251,36274,36286,36369
- Property svn:mergeinfo changed
-
trunk/Ohana/src/opihi/lib.shell/convert_to_RPN.c
r34088 r36375 4 4 StackVar *convert_to_RPN (int argc, char **argv, int *nstack) { 5 5 6 inttype;6 StackVarType type; 7 7 int i, j, Nstack, Nop_stack, NSTACK; 8 8 StackVar *stack, *op_stack; … … 21 21 22 22 /* decide on priority of object */ 23 type = 0; 23 type = ST_NONE; 24 25 /* trinary operations */ 26 if (!strcmp (argv[i], "?")) { type = ST_TRINARY; goto gotit; } 27 if (!strcmp (argv[i], ":")) { type = ST_COMMA; goto gotit; } 28 24 29 /* unary operations */ 25 if (!strcmp (argv[i], "abs")) { type = 9; goto gotit; } 26 if (!strcmp (argv[i], "int")) { type = 9; goto gotit; } 27 if (!strcmp (argv[i], "exp")) { type = 9; goto gotit; } 28 if (!strcmp (argv[i], "ten")) { type = 9; goto gotit; } 29 if (!strcmp (argv[i], "log")) { type = 9; goto gotit; } 30 if (!strcmp (argv[i], "ln")) { type = 9; goto gotit; } 31 if (!strcmp (argv[i], "sqrt")) { type = 9; goto gotit; } 32 if (!strcmp (argv[i], "erf")) { type = 9; goto gotit; } 33 34 if (!strcmp (argv[i], "sinh")) { type = 9; goto gotit; } 35 if (!strcmp (argv[i], "cosh")) { type = 9; goto gotit; } 36 if (!strcmp (argv[i], "asinh")) { type = 9; goto gotit; } 37 if (!strcmp (argv[i], "acosh")) { type = 9; goto gotit; } 38 39 if (!strcmp (argv[i], "sin")) { type = 9; goto gotit; } 40 if (!strcmp (argv[i], "cos")) { type = 9; goto gotit; } 41 if (!strcmp (argv[i], "tan")) { type = 9; goto gotit; } 42 if (!strcmp (argv[i], "dsin")) { type = 9; goto gotit; } 43 if (!strcmp (argv[i], "dcos")) { type = 9; goto gotit; } 44 if (!strcmp (argv[i], "dtan")) { type = 9; goto gotit; } 45 if (!strcmp (argv[i], "asin")) { type = 9; goto gotit; } 46 if (!strcmp (argv[i], "acos")) { type = 9; goto gotit; } 47 if (!strcmp (argv[i], "atan")) { type = 9; goto gotit; } 48 if (!strcmp (argv[i], "dasin")) { type = 9; goto gotit; } 49 if (!strcmp (argv[i], "dacos")) { type = 9; goto gotit; } 50 if (!strcmp (argv[i], "datan")) { type = 9; goto gotit; } 51 52 if (!strcmp (argv[i], "lgamma")) { type = 9; goto gotit; } 53 54 if (!strcmp (argv[i], "rnd")) { type = 9; goto gotit; } 55 if (!strcmp (argv[i], "xramp")) { type = 9; goto gotit; } 56 if (!strcmp (argv[i], "yramp")) { type = 9; goto gotit; } 57 if (!strcmp (argv[i], "ramp")) { type = 9; goto gotit; } 58 if (!strcmp (argv[i], "zero")) { type = 9; goto gotit; } 59 if (!strcmp (argv[i], "--")) { type = 9; goto gotit; } 60 if (!strcmp (argv[i], "not")) { type = 9; goto gotit; } 61 if (!strcmp (argv[i], "isinf")) { type = 9; goto gotit; } 62 if (!strcmp (argv[i], "isnan")) { type = 9; goto gotit; } 30 if (!strcmp (argv[i], "abs")) { type = ST_UNARY; goto gotit; } 31 if (!strcmp (argv[i], "int")) { type = ST_UNARY; goto gotit; } 32 if (!strcmp (argv[i], "exp")) { type = ST_UNARY; goto gotit; } 33 if (!strcmp (argv[i], "ten")) { type = ST_UNARY; goto gotit; } 34 if (!strcmp (argv[i], "log")) { type = ST_UNARY; goto gotit; } 35 if (!strcmp (argv[i], "ln")) { type = ST_UNARY; goto gotit; } 36 if (!strcmp (argv[i], "sqrt")) { type = ST_UNARY; goto gotit; } 37 if (!strcmp (argv[i], "erf")) { type = ST_UNARY; goto gotit; } 38 if (!strcmp (argv[i], "sinh")) { type = ST_UNARY; goto gotit; } 39 if (!strcmp (argv[i], "cosh")) { type = ST_UNARY; goto gotit; } 40 if (!strcmp (argv[i], "asinh")) { type = ST_UNARY; goto gotit; } 41 if (!strcmp (argv[i], "acosh")) { type = ST_UNARY; goto gotit; } 42 if (!strcmp (argv[i], "sin")) { type = ST_UNARY; goto gotit; } 43 if (!strcmp (argv[i], "cos")) { type = ST_UNARY; goto gotit; } 44 if (!strcmp (argv[i], "tan")) { type = ST_UNARY; goto gotit; } 45 if (!strcmp (argv[i], "dsin")) { type = ST_UNARY; goto gotit; } 46 if (!strcmp (argv[i], "dcos")) { type = ST_UNARY; goto gotit; } 47 if (!strcmp (argv[i], "dtan")) { type = ST_UNARY; goto gotit; } 48 if (!strcmp (argv[i], "asin")) { type = ST_UNARY; goto gotit; } 49 if (!strcmp (argv[i], "acos")) { type = ST_UNARY; goto gotit; } 50 if (!strcmp (argv[i], "atan")) { type = ST_UNARY; goto gotit; } 51 if (!strcmp (argv[i], "dasin")) { type = ST_UNARY; goto gotit; } 52 if (!strcmp (argv[i], "dacos")) { type = ST_UNARY; goto gotit; } 53 if (!strcmp (argv[i], "datan")) { type = ST_UNARY; goto gotit; } 54 if (!strcmp (argv[i], "lgamma")) { type = ST_UNARY; goto gotit; } 55 if (!strcmp (argv[i], "rnd")) { type = ST_UNARY; goto gotit; } 56 if (!strcmp (argv[i], "xramp")) { type = ST_UNARY; goto gotit; } 57 if (!strcmp (argv[i], "yramp")) { type = ST_UNARY; goto gotit; } 58 if (!strcmp (argv[i], "ramp")) { type = ST_UNARY; goto gotit; } 59 if (!strcmp (argv[i], "zero")) { type = ST_UNARY; goto gotit; } 60 if (!strcmp (argv[i], "--")) { type = ST_UNARY; goto gotit; } 61 if (!strcmp (argv[i], "not")) { type = ST_UNARY; goto gotit; } 62 if (!strcmp (argv[i], "isinf")) { type = ST_UNARY; goto gotit; } 63 if (!strcmp (argv[i], "isnan")) { type = ST_UNARY; goto gotit; } 63 64 64 65 /* binary operations */ 65 if (!strcmp (argv[i], "^")) { type = 8; goto gotit; } 66 67 if (!strcmp (argv[i], "@")) { type = 7; goto gotit; } 68 if (!strcmp (argv[i], "/")) { type = 7; goto gotit; } 69 if (!strcmp (argv[i], "*")) { type = 7; goto gotit; } 70 if (!strcmp (argv[i], "%")) { type = 7; goto gotit; } 71 72 if (!strcmp (argv[i], "+")) { type = 6; goto gotit; } 73 if (!strcmp (argv[i], "-")) { type = 6; goto gotit; } 66 if (!strcmp (argv[i], "^")) { type = ST_POWER; goto gotit; } 67 68 if (!strcmp (argv[i], "atan2")) { type = ST_BINARY; goto gotit; } 69 if (!strcmp (argv[i], ",")) { type = ST_COMMA; goto gotit; } 70 71 if (!strcmp (argv[i], "@")) { type = ST_TIMES; goto gotit; } 72 if (!strcmp (argv[i], "/")) { type = ST_TIMES; goto gotit; } 73 if (!strcmp (argv[i], "*")) { type = ST_TIMES; goto gotit; } 74 if (!strcmp (argv[i], "%")) { type = ST_TIMES; goto gotit; } 75 76 if (!strcmp (argv[i], "+")) { type = ST_ADD; goto gotit; } 77 if (!strcmp (argv[i], "-")) { type = ST_ADD; goto gotit; } 74 78 75 if (!strcmp (argv[i], "&")) { type = 5; goto gotit; }76 if (!strcmp (argv[i], "|")) { type = 5; goto gotit; }77 78 if (!strcmp (argv[i], "<")) { type = 4; goto gotit; }79 if (!strcmp (argv[i], ">")) { type = 4; goto gotit; }80 if (!strcmp (argv[i], "==")) { type = 4; strcpy (argv[i], "E"); goto gotit; }81 if (!strcmp (argv[i], "!=")) { type = 4; strcpy (argv[i], "N"); goto gotit; }82 if (!strcmp (argv[i], "<=")) { type = 4; strcpy (argv[i], "L"); goto gotit; }83 if (!strcmp (argv[i], ">=")) { type = 4; strcpy (argv[i], "G"); goto gotit; }84 if (!strcmp (argv[i], ">>")) { type = 4; strcpy (argv[i], "U"); goto gotit; }85 if (!strcmp (argv[i], "<<")) { type = 4; strcpy (argv[i], "D"); goto gotit; }86 87 if (!strcmp (argv[i], "&&")) { type = 3; strcpy (argv[i], "A"); goto gotit; }88 if (!strcmp (argv[i], "||")) { type = 3; strcpy (argv[i], "O"); goto gotit; }89 90 if (!strcmp (argv[i], "(")) { type = 2;goto gotit; }91 if (!strcmp (argv[i], ")")) { type = 1; goto gotit; }79 if (!strcmp (argv[i], "&")) { type = ST_BITWISE; goto gotit; } 80 if (!strcmp (argv[i], "|")) { type = ST_BITWISE; goto gotit; } 81 82 if (!strcmp (argv[i], "<")) { type = ST_LOGIC; goto gotit; } 83 if (!strcmp (argv[i], ">")) { type = ST_LOGIC; goto gotit; } 84 if (!strcmp (argv[i], "==")) { type = ST_LOGIC; strcpy (argv[i], "E"); goto gotit; } 85 if (!strcmp (argv[i], "!=")) { type = ST_LOGIC; strcpy (argv[i], "N"); goto gotit; } 86 if (!strcmp (argv[i], "<=")) { type = ST_LOGIC; strcpy (argv[i], "L"); goto gotit; } 87 if (!strcmp (argv[i], ">=")) { type = ST_LOGIC; strcpy (argv[i], "G"); goto gotit; } 88 if (!strcmp (argv[i], ">>")) { type = ST_LOGIC; strcpy (argv[i], "U"); goto gotit; } 89 if (!strcmp (argv[i], "<<")) { type = ST_LOGIC; strcpy (argv[i], "D"); goto gotit; } 90 91 if (!strcmp (argv[i], "&&")) { type = ST_AND; strcpy (argv[i], "A"); goto gotit; } 92 if (!strcmp (argv[i], "||")) { type = ST_OR ; strcpy (argv[i], "O"); goto gotit; } 93 94 if (!strcmp (argv[i], "(")) { type = ST_LEFT; goto gotit; } 95 if (!strcmp (argv[i], ")")) { type = ST_RIGHT; goto gotit; } 92 96 93 97 gotit: 94 98 /* choose how to deal with object */ 95 99 switch (type) { 96 case 8: /* exponentiation: 2^2^3 = 64 != 256 (precedence is right-to-left, not left-to-right!) */100 case ST_POWER: /* exponentiation: 2^2^3 = 64 != 256 (precedence is right-to-left, not left-to-right!) */ 97 101 /* pop previous, higher operators from OP stack to stack */ 98 102 for (j = Nop_stack - 1; (j >= 0) && (op_stack[j].type > type); j--) { … … 105 109 Nop_stack ++; 106 110 break; 107 case 9: /* unary OPs */ 108 case 7: /* binary OPs */ 109 case 6: 110 case 5: 111 case 4: 112 case 3: 111 case ST_UNARY: 112 case ST_BINARY: 113 case ST_TRINARY: 114 case ST_TIMES: 115 case ST_ADD: 116 case ST_BITWISE: 117 case ST_LOGIC: 118 case ST_AND: 119 case ST_OR: 113 120 /* pop previous, higher or equal operators from OP stack to stack */ 114 121 for (j = Nop_stack - 1; (j >= 0) && (op_stack[j].type >= type); j--) { … … 121 128 Nop_stack ++; 122 129 break; 123 case 2:130 case ST_LEFT: 124 131 /* push operator on OP stack */ 125 132 assign_stack (&op_stack[Nop_stack], argv[i], type); … … 127 134 Nop_stack ++; 128 135 break; 129 case 1:136 case ST_RIGHT: 130 137 /* pop rest of operators from OP stack to stack, looking for '(' */ 131 for (j = Nop_stack - 1; (j >= 0) && (op_stack[j].type != 2); j--) {132 move_stack (&stack[Nstack], &op_stack[j]); 133 Nstack ++; 134 Nop_stack --; 135 } 136 if ((j == -1) || (op_stack[j].type != 2)) {138 for (j = Nop_stack - 1; (j >= 0) && (op_stack[j].type != ST_LEFT); j--) { 139 move_stack (&stack[Nstack], &op_stack[j]); 140 Nstack ++; 141 Nop_stack --; 142 } 143 if ((j == -1) || (op_stack[j].type != ST_LEFT)) { 137 144 push_error ("syntax error: mismatched parenthesis"); 138 145 Nstack = 0; … … 144 151 Nop_stack --; 145 152 break; 146 case 0: 153 case ST_COMMA: 154 /* pop rest of operators from OP stack to stack, looking for '(' (but do not pop the '(')*/ 155 for (j = Nop_stack - 1; (j >= 0) && (op_stack[j].type != ST_LEFT) && (op_stack[j].type != ST_TRINARY); j--) { 156 move_stack (&stack[Nstack], &op_stack[j]); 157 Nstack ++; 158 Nop_stack --; 159 } 160 break; 161 case ST_NONE: 147 162 /* place the value (number or vector/matrix name) on stack */ 148 163 /* value of 'X' is used as sentinel until we sort out values */ 149 assign_stack (&stack[Nstack], argv[i], 'X');164 assign_stack (&stack[Nstack], argv[i], ST_VALUE); 150 165 Nstack ++; 151 166 break; 167 168 default: 169 push_error ("invalid stack typ"); 170 Nstack = 0; 171 goto cleanup; 152 172 } 153 173 } 154 174 155 /* dump remaining operators on stack, checking for ' )' */175 /* dump remaining operators on stack, checking for '(' */ 156 176 for (j = Nop_stack - 1; j >= 0; j--) { 157 if (op_stack[j].type == 2) {177 if (op_stack[j].type == ST_LEFT) { 158 178 push_error ("syntax error: mismatched parenthesis"); 159 179 Nstack = 0;
Note:
See TracChangeset
for help on using the changeset viewer.
