import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import java.util.function.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.math.*;
import static x30_pkg.x30_util.DynamicObject;
import java.util.jar.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.geom.*;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import javax.swing.Timer;
import java.text.*;
import java.text.NumberFormat;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import java.awt.datatransfer.StringSelection;
import javax.swing.undo.UndoManager;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
import java.nio.charset.Charset;
import javax.swing.Icon;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.nio.file.Path;
class main {
public static void main(final String[] args) throws Exception {
test_leftArrowScript();
}
// include functions that scripts may want to use
static class test_leftArrowScript_Tester { Object value = "123"; }
static void test_leftArrowScript() {
final class _C_1 { String countLoop(int n) {
return " i <- 0; while lessThan i " + n + " { i <- plus i 1 }; i ";
} } final _C_1 __2 = new _C_1();
testFunctionValues(script -> leftArrowVerbose((String) script),
// int primitive
" 5 ", 5,
" -5 ", -5,
// function definition & call
"\r\n def double x {\r\n Math multiplyExact x 2\r\n }\r\n \r\n double 10\r\n ", 20,
// new object with constructor arguments
" new Pair \"hello\" \"world\" ", pair("hello", "world"),
// double primitive
" str 1.5e2 ", "150.0",
// [not implemented, too complicated]
// last = result of last statement
// [[ plus 1 2; Math addExact 5 last ]], 8,
// get static field
" Color black ", Color.black,
// get instance field,
" tester <- new test_leftArrowScript_Tester; tester value ", "123",
// while loop
__2.countLoop(5), 5,
// if statement
" if lessThan 0 1 { \"ok\" } ", "ok",
" if lessThan 1 0 { \"ok\" } ", null,
// for each
" l <- ll 2 3\r\n sum <- 0\r\n for number in l { sum <- plus sum number }\r\n sum\r\n ", 5,
// some bug
"\r\n img <- newImage 10 10\r\n r <- randomRect img 10 10\r\n ", rect(0, 0, 10, 10));
// Test that ping is called at least once per loop iteration
var script1 = leftArrowParse(__2.countLoop(5));
var script2 = leftArrowParse(__2.countLoop(6));
long pingCount1 = countPingCalls(() -> script1.get());
long pingCount2 = countPingCalls(() -> script2.get());
printVars("pingCount1", pingCount1, "pingCount2", pingCount2);
assertTrue(pingCount2 > pingCount1);
print("Left-arrow script tested OK");
}
// params = input, output, input, output, ...
static void testFunctionValues(IF1 function, Object... params) {
for (int i = 0; i+1 < l(params); i += 2) {
Object in = params[i], expected = params[i+1];
testFunctionValue(function, in, expected);
}
}
static Object leftArrowVerbose(String script) {
GazelleV_LeftArrowScriptParser parser = new GazelleV_LeftArrowScriptParser();
parser.allowTheWorld();
var parsedScript = parser.parse(script);
parser.printFunctionDefs();
print(parsedScript);
return parsedScript.get();
}
static Pair pair(A a, B b) {
return new Pair(a, b);
}
static Pair pair(A a) {
return new Pair(a, a);
}
static Rect rect(int x, int y, int w, int h) {
return new Rect(x, y, w, h);
}
static Rect rect(Pt p, int w, int h) {
return new Rect(p.x, p.y, w, h);
}
static Rect rect(int w, int h) {
return new Rect(0, 0, w, h);
}
static GazelleV_LeftArrowScript.Script leftArrowParse(String script) {
GazelleV_LeftArrowScriptParser parser = new GazelleV_LeftArrowScriptParser();
parser.allowTheWorld();
return parser.parse(script);
}
static long countPingCalls(Runnable r) {
AtomicLong counter = new AtomicLong();
{
tempPingSource(new PingSource(() -> { incAtomicLong(counter); return false; }));
{ if (r != null) r.run(); }
}
return counter.get();
}
// Use like this: printVars(+x, +y);
// Or: printVars("bla", +x);
// Or: printVars bla(, +x);
static void printVars(Object... params) {
printVars_str(params);
}
static void assertTrue(Object o) {
if (!(eq(o, true) /*|| isTrue(pcallF(o))*/))
throw fail(str(o));
}
static boolean assertTrue(String msg, boolean b) {
if (!b)
throw fail(msg);
return b;
}
static boolean assertTrue(boolean b) {
if (!b)
throw fail("oops");
return b;
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static boolean printAlsoToSystemOut = true;
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal