/* e.g. overlay <- ScreenOverlay bounds <- rightScreenBounds overlay bounds bounds overlay show */ sclass GazelleV_LeftArrowScriptParser > SimpleLeftToRightParser { new Script script; new LinkedHashSet 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 { sclass Script extends SynchronizedList 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 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 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; } }