::SHE+ILA::'s PEMDAS stack parser

Elegant Figure in Purple Gown in Grand Setting
17
0
  • Squishy Plushie's avatar Artist
    Squishy Pl...
  • Prompt
    Read prompt
  • DDG Model
    DaVinci2
  • Access
    Public
  • Created
    2h ago
  • Try

More about ::SHE+ILA::'s PEMDAS stack parser

To implement the Evaluate() opcode we use a stack, onto which we push and pop the various PEMDAS operators (+, - , *, .div., .mod) and numeric values. (PEMDAS stands for Parentheses, Exponentiation, Multiply, Divide, Add, Subtract, and specifies the order in which these ops are done in a general arithmetic expression). Here's an extract from the stack parsing algorithm :
StackParse <:: [hier1, hier2, hold, stack](srce) {
[1] stack <:: EMPTY;
[2] obj <:: EMPTY;
[3] hold <:: EMPTY;
[4] READ:: srce <:: ("{", srce, "}");
[5] REPEAT:: (OPR) [? srce[1] .in. operators ?]; // go to OPR if srce[1] is an operator;
[6] obj < srce[1] ; // Push srce[1] onto head of obj;
[7] srce => dummy; // Discard the first element of srce;
[8] TESTHIER; // go to label TESTHIER;
[9] OPR:: (L1) [? .nor./ srce[1] .eq. ")}" ?]; // go to L1 if srce[1] is neither right paren nor right brace;
[10] (E1) [? .nor./ stack[1] .eq. "({" ?]; // go to E1 if stack [1] is neither left paren nor left brace;
[11] MV1A2:: srce => dummy; // Discard the first element of srce;
[12] stack => dummy; // Pop and discard first element of stack;
[13] TESTHIER; // go to label TESTHIER;
[14] E1:: throw ("*** ( or [ missing!"); // get out of here;
[15] // this line removed as redundant;
[16] L1:: (L2) [? srce[1] .ne. "]" ?]; // go to L2 if srce[1] isn't a right square bracket;
[17] (E2) [? stack[1] .ne. "!" ?]; // go to E2 if top of stack isn't a "!";
[18] obj <:: (stack[1], hold[1], obj);
[19] hold => dummy; // discard;
[20] stack => dummy; // Pop and discard;
[21] MV1A2; // go to label MV1A2;
.... TO BE CONTINUED :) ....
Sorry bout the lack of structured programming, lol (i.e the use of "go to" and labels) :)

Comments


Loading Dream Comments...

Discover more dreams from this artist