package orc.run.porce;

import com.oracle.truffle.api.frame.FrameSlot;
import com.oracle.truffle.api.frame.VirtualFrame;

public class Read {
	private Read() {
	}

	public static class Constant extends Expression {
		private final Object value;

		public Constant(Object value) {
			this.value = value;
			// TODO Auto-generated constructor stub
		}

		public Object execute(VirtualFrame frame) {
			return value;
		}

		public static Constant create(Object v) {
			return new Constant(v);
		}
	}

	public static class Local extends Expression {
		private final FrameSlot slot;

		public Local(FrameSlot slot) {
			this.slot = slot;
		}

		public Object execute(VirtualFrame frame) {
			// TODO: Change to getObject
			Object value = frame.getValue(slot);
			return value;
		}

		public static Local create(FrameSlot slot) {
			assert slot != null;
			return new Local(slot);
		}
	}

	public static class Argument extends Expression {
		private final int index;

		public Argument(int index) {
			this.index = index;
		}

		public Object execute(VirtualFrame frame) {
			Object value = frame.getArguments()[index];
			return value;
		}

		public static Argument create(int index) {
			assert index >= 0;
			return new Argument(index + 1);
		}
	}

	public static class Closure extends Expression {
		private final int index;

		public Closure(int index) {
			this.index = index;
		}

		public Object execute(VirtualFrame frame) {
			Object value = ((Object[]) frame.getArguments()[0])[index];
			return value;
		}

		public static Closure create(int index) {
			assert index >= 0;
			return new Closure(index);
		}
	}
}

@@ -64,16 +64,76 @@
 meSlot;%0A
+import com.oracle.truffle.api.frame.FrameSlotTypeException;%0A
 import c
@@ -744,76 +744,159 @@
 %0A%09%09%09
-// TODO: Change to getObject%0A%09%09%09Object value = frame.getValue(slot);
+Object value;%0A%09%09%09try %7B%0A%09%09%09%09value = frame.getObject(slot);%0A%09%09%09%7D catch (FrameSlotTypeException e) %7B%0A%09%09%09%09throw InternalPorcEError.typeError(this, e);%0A%09%09%09%7D
 %0A%09%09%09

Remove Frame.getValue call.