package org.mozartoz.truffle;

import org.mozartoz.truffle.nodes.OzRootNode;
import org.mozartoz.truffle.translator.Translator;

import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.vm.PolyglotEngine;

public class Main {

	public static final PolyglotEngine ENGINE = PolyglotEngine.newBuilder().build();

	public static void main(String[] args) {
		eval("{Show 3 + 2}");
		eval("local A=3 B=2 in {Show A + B} end");
		eval("local fun {Inc Arg} Arg + 1 end in {Show {Inc 1}} end");
		eval("local Fact in\nfun {Fact N} if N == 0 then 1 else N * {Fact N-1} end end\n{Show {Fact 30}}\nend");
		eval("local L in L = [1 2 3] {Show L} {Show L.1} {Show L.2} end");
		eval("local A in A=1+2 {Show A} end");
		eval("local A B in A=B B=42 {Show B} {Show A} end");

		eval("local Add in fun {Add A B} A + B end {Show {Add 2 4}} end");
		eval("local H=5 V T in T=V V=[6 7 8] {Show H|T} end");
		eval("local H=5 V T in V=T {Show H|T} {proc {$} V=[6 7 8] end} {Show H|T} end");
		eval("local fun {Binder A} A=43 unit end X R in R={Binder X} {Show X} end");

		eval("local Tree=tree(left:1 right:2) in {Show Tree} {Show Tree.left} end");

		eval("local F=functor\n import\n Property(get)\n export\n Return\n define\n Return=42\n end in {Show F} end");

		eval("local fun {MakeList N} if N>0 then _|{MakeList N-1} else nil end end in {Show {MakeList 3}} end");

		eval("local fun {Append Xs Ys} case Xs of nil then Ys [] X|Xr then X|{Append Xr Ys} end end in {Show {Append [1 2] [3 4]}} end");

		eval("local fun {Member X Ys} case Ys of nil then false [] Y|Yr then X==Y orelse {Member X Yr} end end in {Show {Member nil [1 2 3]}} end");
	}

	private static void eval(String code) {
		// ENGINE.eval(Source.fromText(code, "test.oz").withMimeType(OzLanguage.MIME_TYPE));
		parseAndExecute(code);
	}

	public static void parseAndExecute(String code) {
		execute(new Translator().parseAndTranslate(code));
	}

	private static Object execute(OzRootNode rootNode) {
		return Truffle.getRuntime().createCallTarget(rootNode).call();
	}

}

@@ -24,16 +24,66 @@
 uffle;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0A%0A
 import o
@@ -120,16 +120,16 @@
 otNode;%0A
-
 import o
@@ -207,24 +207,69 @@
 pi.Truffle;%0A
+import com.oracle.truffle.api.source.Source;%0A
 import com.o
@@ -410,16 +410,78 @@
 ild();%0A%0A
+%09public static final String PROJECT_ROOT = getProjectRoot();%0A%0A
 %09public 
@@ -511,16 +511,35 @@
 %5B%5D args)
+ throws IOException
  %7B%0A%09%09eva
@@ -1806,16 +1806,109 @@
 %7D end%22);
+%0A%0A%09%09Source source = Source.fromFileName(PROJECT_ROOT + %22/test.oz%22);%0A%09%09eval(source.getCode());
 %0A%09%7D%0A%0A%09pr
@@ -2292,10 +2292,213 @@
 ();%0A%09%7D%0A%0A
+%09private static String getProjectRoot() %7B%0A%09%09String thisFile = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();%0A%09%09String path = new File(thisFile).getParent();%0A%09%09return path;%0A%09%7D%0A%0A
 %7D%0A

read from file