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)

/* e.g.

  overlay <- ScreenOverlay
  bounds <- rightScreenBounds
  overlay bounds bounds
  overlay show
*/

sclass GazelleV_LeftArrowScriptParser > SimpleLeftToRightParser {
  new Script script;
  new LinkedHashSet<S> knownVars;
  
  // object can be a class
  srecord MethodOnObject(O object, S method) {}
  
  interface Evaluable {
    public O get(VarContext ctx default new);
  }
  
  // We're using SynchronizedList as a list wrapper
  //sclass Script extends ArrayList<Evaluable> {
  sclass Script extends SynchronizedList<Evaluable> is Evaluable {
    *() { super(new L); }
    
    public O get(VarContext ctx) {
      O result = null;
      for (step : this)
        result = step.get(ctx);
      ret result;
    }
  }
  
  srecord noeq Assignment(S var, Evaluable expression) is Evaluable {
    public O get(VarContext ctx) {
      O o = expression.get(ctx);
      ctx.set(var, o);
      ret o;
    }
  }
  
  srecord noeq NewObject(Class c) is Evaluable {
    public O get(VarContext ctx) {
      ret callConstructor(c);
    }
  }
  
  srecord noeq GetVar(S var) is Evaluable {
    public O get(VarContext ctx) {
      ret ctx.get(var);
    }
  }
  
  srecord noeq Const(O value) is Evaluable {
    public O get(VarContext ctx) {
      ret value;
    }
  }
  
  srecord CallMethod(Evaluable target, S methodName, L<Evaluable> args) is Evaluable {
    public O get(VarContext ctx) {
      ret call(target.get(ctx), methodName, mapToArray(args, arg -> arg.get(ctx)));
    }
  }
  
  Script parse(S text) {
    setText(text);
    while (mainLoop()) {
      S t = token();
      if (isIdentifier(t) && eq(token(1), "<") && eq(token(2), "-")) {
        next(3);
        knownVars.add(t);
        script.add(new Assignment(t, parseExpr()));
      } else
        script.add(parseExpr());
    }
    ret script;
  }
  
  Evaluable parseOptionalExpression() {
    if (lineBreak()) null;
    ret parseExpr();
  }
  
  Evaluable parseExpr() {
    if (atEnd()) null;
    S t = tpp();
    print(+t);
    
    if (knownVars.contains(t))
      ret parseCall(new GetVar(t));
      
    O o = findExternalObject(t);
    if (o == null)
      fail("Unknown object: " + t);
    else if (o cast Class)
      ret new NewObject(o);
    else if (o cast MethodOnObject)
      ret new CallMethod(new Const(o.object), o.method, parseExpressions());
    else
      ret parseCall(new Const(o));
  }
  
  L<Evaluable> parseExpressions() {
    ret collectWhileNotNull(-> parseOptionalExpression());
  }
  
  Evaluable parseCall(Evaluable target) {
    if (atEndOrLineBreak()) ret target;
    
    S methodName = tpp();
    var args = parseExpressions();
    ret new CallMethod(target, methodName, args);
  }
  
  // can return MethodOnObject
  swappable O findExternalObject(S name) { null; }
}

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: 113 / 129
Referenced in: [show references]