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).

1  
sclass ShuntingYardParser_v2 > ShuntingYardCore is Steppable {
2  
  // input
3  
  LS tok;
4  
  int iTok = 1;
5  
  
6  
  // transitional
7  
  int iLiteralStart;
8  
9  
  *() {}
10  
  *(S s) { this(noTok(s)); }
11  
  *(LS *tok) {}
12  
  
13  
  run { stepAll(this); }
14  
  
15  
  public bool step() {
16  
    if (iTok >= l(tok)) {
17  
      finishLiteral();
18  
      handleEOT();
19  
      false;
20  
    }
21  
    
22  
    S t = tok.get(iTok);
23  
    bool space = isSpacer(t);
24  
25  
    if (!space) {
26  
      if (isOperatorOrBracket(t)) {
27  
        finishLiteral();
28  
        handleToken(t);
29  
      } else
30  
        if (iLiteralStart == 0)
31  
          iLiteralStart = iTok;
32  
    }
33  
34  
    iTok += 2;
35  
    true;
36  
  }
37  
  
38  
  @Override int precedence(S op) {
39  
    if (eqOneOf(op, "*", "/")) ret 2;
40  
    if (eqOneOf(op, "+", "-")) ret 1;
41  
    ret 0;
42  
  }
43  
  
44  
  bool isSpacer(S t) {
45  
    ret emptyAfterTrim(t);
46  
  }
47  
  
48  
  @Override bool isOperator(S t) {
49  
    ret !isSpacer(t)
50  
      && !startsWithLetterOrDigit(t) && !eq(t, ".");
51  
  }
52  
  
53  
  void finishLiteral {
54  
    if (iLiteralStart == 0) ret;
55  
    S s = joinSubList(tok, iLiteralStart, iTok-1);
56  
    if (!isSpacer(s))
57  
      handleToken(s);
58  
    iLiteralStart = 0;
59  
  }
60  
  
61  
  // returns list that can be processed with CalcRPN
62  
  LS get() {
63  
    run();
64  
    ret outputQueue;
65  
  }
66  
}

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: 143 / 217
Version history: 1 change(s)
Referenced in: [show references]