Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

66
LINES

< > BotCompany Repo | #1035190 // ShuntingYardParser_v2 [parses things like "1+2*(3-4)"] - reimplementation over ShuntingYardCore

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (9353L/52K).

sclass ShuntingYardParser_v2 > ShuntingYardCore is Steppable {
  // input
  LS tok;
  int iTok = 1;
  
  // transitional
  int iLiteralStart;

  *() {}
  *(S s) { this(noTok(s)); }
  *(LS *tok) {}
  
  run { stepAll(this); }
  
  public bool step() {
    if (iTok >= l(tok)) {
      finishLiteral();
      handleEOT();
      false;
    }
    
    S t = tok.get(iTok);
    bool space = isSpacer(t);

    if (!space) {
      if (isOperatorOrBracket(t)) {
        finishLiteral();
        handleToken(t);
      } else
        if (iLiteralStart == 0)
          iLiteralStart = iTok;
    }

    iTok += 2;
    true;
  }
  
  @Override int precedence(S op) {
    if (eqOneOf(op, "*", "/")) ret 2;
    if (eqOneOf(op, "+", "-")) ret 1;
    ret 0;
  }
  
  bool isSpacer(S t) {
    ret emptyAfterTrim(t);
  }
  
  @Override bool isOperator(S t) {
    ret !isSpacer(t)
      && !startsWithLetterOrDigit(t) && !eq(t, ".");
  }
  
  void finishLiteral {
    if (iLiteralStart == 0) ret;
    S s = joinSubList(tok, iLiteralStart, iTok-1);
    if (!isSpacer(s))
      handleToken(s);
    iLiteralStart = 0;
  }
  
  // returns list that can be processed with CalcRPN
  LS get() {
    run();
    ret outputQueue;
  }
}

Author comment

Began life as a copy of #1032659

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035190
Snippet name: ShuntingYardParser_v2 [parses things like "1+2*(3-4)"] - reimplementation over ShuntingYardCore
Eternal ID of this version: #1035190/2
Text MD5: f75aff3ba56bd16906433835093791b2
Transpilation MD5: dd3d644bf3c61432a79fca72168f1c3d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-08 18:38:27
Source code size: 1269 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 142 / 216
Version history: 1 change(s)
Referenced in: [show references]