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

117
LINES

< > BotCompany Repo | #1033980 // GazelleV_LeftArrowScriptParser [backup]

JavaX fragment (include)

1  
/* e.g.
2  
3  
  overlay <- ScreenOverlay
4  
  bounds <- rightScreenBounds
5  
  overlay bounds bounds
6  
  overlay show
7  
*/
8  
9  
sclass GazelleV_LeftArrowScriptParser > SimpleLeftToRightParser {
10  
  new Script script;
11  
  new LinkedHashSet<S> knownVars;
12  
  
13  
  // object can be a class
14  
  srecord MethodOnObject(O object, S method) {}
15  
  
16  
  interface Evaluable {
17  
    public O get(VarContext ctx default new);
18  
  }
19  
  
20  
  // We're using SynchronizedList as a list wrapper
21  
  //sclass Script extends ArrayList<Evaluable> {
22  
  sclass Script extends SynchronizedList<Evaluable> is Evaluable {
23  
    *() { super(new L); }
24  
    
25  
    public O get(VarContext ctx) {
26  
      O result = null;
27  
      for (step : this)
28  
        result = step.get(ctx);
29  
      ret result;
30  
    }
31  
  }
32  
  
33  
  srecord noeq Assignment(S var, Evaluable expression) is Evaluable {
34  
    public O get(VarContext ctx) {
35  
      O o = expression.get(ctx);
36  
      ctx.set(var, o);
37  
      ret o;
38  
    }
39  
  }
40  
  
41  
  srecord noeq NewObject(Class c) is Evaluable {
42  
    public O get(VarContext ctx) {
43  
      ret callConstructor(c);
44  
    }
45  
  }
46  
  
47  
  srecord noeq GetVar(S var) is Evaluable {
48  
    public O get(VarContext ctx) {
49  
      ret ctx.get(var);
50  
    }
51  
  }
52  
  
53  
  srecord noeq Const(O value) is Evaluable {
54  
    public O get(VarContext ctx) {
55  
      ret value;
56  
    }
57  
  }
58  
  
59  
  srecord CallMethod(Evaluable target, S methodName, L<Evaluable> args) is Evaluable {
60  
    public O get(VarContext ctx) {
61  
      ret call(target.get(ctx), methodName, mapToArray(args, arg -> arg.get(ctx)));
62  
    }
63  
  }
64  
  
65  
  Script parse(S text) {
66  
    setText(text);
67  
    while (mainLoop()) {
68  
      S t = token();
69  
      if (isIdentifier(t) && eq(token(1), "<") && eq(token(2), "-")) {
70  
        next(3);
71  
        knownVars.add(t);
72  
        script.add(new Assignment(t, parseExpr()));
73  
      } else
74  
        script.add(parseExpr());
75  
    }
76  
    ret script;
77  
  }
78  
  
79  
  Evaluable parseOptionalExpression() {
80  
    if (lineBreak()) null;
81  
    ret parseExpr();
82  
  }
83  
  
84  
  Evaluable parseExpr() {
85  
    if (atEnd()) null;
86  
    S t = tpp();
87  
    print(+t);
88  
    
89  
    if (knownVars.contains(t))
90  
      ret parseCall(new GetVar(t));
91  
      
92  
    O o = findExternalObject(t);
93  
    if (o == null)
94  
      fail("Unknown object: " + t);
95  
    else if (o cast Class)
96  
      ret new NewObject(o);
97  
    else if (o cast MethodOnObject)
98  
      ret new CallMethod(new Const(o.object), o.method, parseExpressions());
99  
    else
100  
      ret parseCall(new Const(o));
101  
  }
102  
  
103  
  L<Evaluable> parseExpressions() {
104  
    ret collectWhileNotNull(-> parseOptionalExpression());
105  
  }
106  
  
107  
  Evaluable parseCall(Evaluable target) {
108  
    if (atEndOrLineBreak()) ret target;
109  
    
110  
    S methodName = tpp();
111  
    var args = parseExpressions();
112  
    ret new CallMethod(target, methodName, args);
113  
  }
114  
  
115  
  // can return MethodOnObject
116  
  swappable O findExternalObject(S name) { null; }
117  
}

Author comment

Began life as a copy of #1033976

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1033980
Snippet name: GazelleV_LeftArrowScriptParser [backup]
Eternal ID of this version: #1033980/1
Text MD5: d29bd5ca72aa81b2a559af24fa4eb77f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-15 05:17:02
Source code size: 2892 bytes / 117 lines
Pitched / IR pitched: No / No
Views / Downloads: 111 / 127
Referenced in: [show references]