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

56
LINES

< > BotCompany Repo | #1035189 // ShuntingYardCore - handles operator precences while parsing [abandoned version]

JavaX fragment (include)

1  
// TODO: unary operators
2  
sclass ShuntingYardCore<Op> {
3  
  new L<Operator> operatorStack;
4  
  
5  
  void handleEOT() {
6  
    while (nempty(operatorStack)) {
7  
      var o = popLast(operatorStack);
8  
      assertNequals(o, "(");
9  
      outputQueue.add(o);
10  
    }
11  
  }
12  
13  
  void handleToken(S token) {
14  
    if (iTok >= l(tok)) {
15  
      false;
16  
    }
17  
    
18  
    S t = tok.get(iTok);
19  
    bool op = isOperator(t), space = isSpacer(t);
20  
    if (op && space) warn("Operators should not be spacers");
21  
    
22  
    if (eq(t, "(")) {
23  
      finishLiteral();
24  
      operatorStack.add(t);
25  
    } else if (eq(t, ")")) {
26  
      finishLiteral();
27  
      while (!eq(last(operatorStack), "(")) {
28  
        assertNempty(operatorStack);
29  
        outputQueue.add(popLast(operatorStack));
30  
      }
31  
      popLast(operatorStack);
32  
    } else if (op) {
33  
      finishLiteral();
34  
      S o1 = t, o2;
35  
      while (!eqOneOf(o2 = last(operatorStack), "(", null)
36  
        && (precedence(o2) > precedence(o1)
37  
        ||  precedence(o2) == precedence(o1) && isLeftAssociative(o1))) {
38  
          ifdef ShuntingYardParser_debug
39  
            print("popLast");
40  
          endifdef
41  
          outputQueue.add(popLast(operatorStack));
42  
        }
43  
        
44  
      operatorStack.add(o1);
45  
    }
46  
    
47  
    true;
48  
  }
49  
  
50  
  // return 0 for non-operator
51  
  int precedence(Op op) {
52  
    ret 1;
53  
  }
54  
  
55  
  swappable bool isLeftAssociative(Op op) { true; }
56  
}

Author comment

Began life as a copy of #1034914

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035189
Snippet name: ShuntingYardCore - handles operator precences while parsing [abandoned version]
Eternal ID of this version: #1035189/1
Text MD5: 5f66cde9ccfa658d331f73beda17b664
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-08 17:50:46
Source code size: 1410 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 113 / 125
Referenced in: [show references]