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

99
LINES

< > BotCompany Repo | #1033981 // GazelleV_LeftArrowScript [LIVE]

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

Libraryless. Click here for Pure Java version (6351L/35K).

// See GazelleV_LeftArrowScriptParser

sclass GazelleV_LeftArrowScript {
  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 {
    Map<S, FunctionDef> functionDefs;
    
    *() { super(new L); }
    
    public O get(VarContext ctx) {
      O result = null;
      for (step : this)
        result = step.get(ctx);
      ret result;
    }
    
    toString { ret pnlToLines(this); }
    
    FunctionDef getFunction(S name) { ret mapGet(functionDefs, name); }
  } // end of Script
  
  srecord noeq FunctionDef(S name, LS args, Evaluable body) {
    public O call(VarContext ctx, O... args) {
      var ctx2 = new VarContext(ctx);
      int n = min(l(args), l(this.args));
      for i to n:
        ctx2.put(this.args.get(i), args[i]);
      print ifdef GazelleV_LeftArrowScript_debug(ctx2 := ctx2.vars);
      ret body.get(ctx2);
    }
  }
  
  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;
    }
    
    toString { ret var + " <- " + expression; }
  }
  
  persistable sclass NewObject is Evaluable {
    Class c;
    L<Evaluable> args;
    
    *(Class *c) {}
    *(Class *c, L<Evaluable> *args) {}
    
    public O get(VarContext ctx) {
      ret callConstructor(c, mapToArray(args, arg -> arg.get(ctx)));
    }
    
    toString { ret "new " + formatFunctionCall(className(c), args); }
  }
  
  srecord noeq CallFunction(FunctionDef f, L<Evaluable> args) is Evaluable {
    public O get(VarContext ctx) {
      ret f.call(ctx, mapToArray(args, a -> a.get(ctx));
    }
    
    toString { ret formatFunctionCall(f.name, args); }
  }
  
  srecord noeq GetVar(S var) is Evaluable {
    public O get(VarContext ctx) {
      ret ctx.get(var);
    }
    
    toString { ret var; }
  }
  
  srecord noeq Const(O value) is Evaluable {
    public O get(VarContext ctx) {
      ret value;
    }
    
    toString { ret strOrClassName(value); }
  }
  
  srecord noeq GetStaticField(Field field) is Evaluable {
    public O get(VarContext ctx) ctex {
      ret field.get(null);
    }
  }
  
  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)));
    }
    
    toString { ret target + "." + formatFunctionCall(methodName, args); }
  }
}

Author comment

Began life as a copy of #1033976

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033981
Snippet name: GazelleV_LeftArrowScript [LIVE]
Eternal ID of this version: #1033981/28
Text MD5: 1d5a7034e362acbabb2b163b759fd791
Transpilation MD5: 2ccf3e023cd269628a7c5ab2e74fcf2c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-27 14:51:20
Source code size: 2699 bytes / 99 lines
Pitched / IR pitched: No / No
Views / Downloads: 73 / 195
Version history: 27 change(s)
Referenced in: [show references]