Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package emulator.editor;
- import static emulator.memory.Memory.Access.StructureAccess.simpleStructureAccess;
- import java.awt.Color;
- import java.util.*;
- import java.util.Map.Entry;
- import emulator.*;
- import emulator.Hypervisor.Call;
- import emulator.code.*;
- import emulator.code.Code.Decl;
- import emulator.code.Operation.OperationType;
- import emulator.editor.CodeIn.CodeInDef;
- import emulator.editor.CodeIn.CodeInDef.EditToken;
- import emulator.editor.Context.CodeContext;
- import emulator.editor.Context.CodeTreeContext;
- import emulator.editor.Context.TreePosition;
- import emulator.io.Video2d;
- import emulator.memory.*;
- import emulator.memory.Identifier.Property;
- import emulator.memory.Identifier.Stack;
- import emulator.memory.Memory.Access;
- import emulator.memory.Memory.Access.StackAccess;
- import emulator.memory.Memory.Access.StructureAccess;
- public class CodeOut {
- public static final LinkedHashMap<Operation.OperationType, CodeOutDef> codeOut = new LinkedHashMap<Operation.OperationType, CodeOut.CodeOutDef>();
- public static void initCodeOut() {
- // Control
- codeOut.put(Operation.OperationType.Control.OP_SEQUENCE, new CodeOutDef(
- new DisplayScript() {
- public void exec(CodeContext context, CodeTreeContext treeContext) {
- //...
- }
- },
- new ExecuteScript[] {
- new ExecuteScript() { // 0
- public void exec(CodeContext context, Call call) {
- int size2 = call.declInitList.size();
- if(call.step2 >= size2) {
- call.step2 = 0;
- call.step = 2;
- }
- else {
- Code.Decl decl = call.declInitList.get(call.step2);
- assert decl.initType == Code.Decl.InitType.EXPRESSION;
- Code code2 = (Code) Memory.Process.resolveExecuteItem(context.process, decl.initExpression)[0];
- Hypervisor.pushExecuteExpression(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code2), true));
- }
- }
- },
- new ExecuteScript() { // 1
- public void exec(CodeContext context, Call call) {
- int size2 = call.declInitList.size();
- assert call.step2 < size2;
- {
- Code.Decl decl = call.declInitList.get(call.step2);
- assert decl.initType == Code.Decl.InitType.EXPRESSION;
- Object[] access2 = Memory.Process.resolveItem(context.process, decl.access);
- access2[0] = Hypervisor.popEvaluate(context.threadStackContext);
- }
- call.step2++;
- call.step = 0;
- }
- },
- new ExecuteScript() { // 2
- public void exec(CodeContext context, Call call) {
- int size2 = ((Structure.List.Vector) Memory.Process.resolveExecuteItem(context.process, context.code.codeList.get(new Identifier.Property("code")))[0]).size();
- if(call.step2 >= size2) {
- call.step = -1;
- }
- else {
- Code code2 = (Code) Memory.Process.resolveExecuteItem(context.process, new Access.MultipleAccess(context.code.codeList.get(new Identifier.Property("code")), new StructureAccess(new Identifier.Property("index", call.step2))))[0];
- Hypervisor.pushExecute(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code2), false));
- call.step2++;
- call.step = 2;
- }
- }
- }
- }
- ));
- codeOut.put(Operation.OperationType.Control.OP_ENVIRONMENT, new CodeOutDef(
- new DisplayScript() {
- //...
- },
- new ExecuteScript[] {
- new ExecuteScript() {
- public void exec(CodeContext context, Call call) {
- }
- }
- }
- ));
- codeOut.put(Operation.OperationType.Control.OP_IF, new CodeOutDef(
- // TODO expression syntax
- // TODO switch syntax
- // TODO fall through (, inner break)
- new DisplayScript() {
- public void exec(CodeContext context, CodeTreeContext treeContext) {
- //...
- }
- },
- new ExecuteScript[] {
- new ExecuteScript() { // 0
- public void exec(CodeContext context, Call call) {
- int size2 = ((Structure.List.Vector) Memory.Process.resolveExecuteItem(context.process, context.code.dataList.get(new Identifier.Property("cond")))[0]).size();
- assert call.step2 < size2;
- Code code1 = (Code) Memory.Process.resolveExecuteItem(context.process, new Access.MultipleAccess(context.code.dataList.get(new Identifier.Property("cond")), new StructureAccess(new Identifier.Property("index", call.step2))))[0];
- Hypervisor.pushExecuteExpression(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code1), true));
- }
- },
- new ExecuteScript() { // 1
- public void exec(CodeContext context, Call call) {
- int size2 = ((Structure.List.Vector) Memory.Process.resolveExecuteItem(context.process, context.code.dataList.get(new Identifier.Property("cond")))[0]).size();
- int size3 = ((Structure.List.Vector) Memory.Process.resolveExecuteItem(context.process, context.code.codeList.get(new Identifier.Property("code")))[0]).size();
- assert call.step2 < size2;
- assert call.step2 < size3;
- boolean expression1 = (Boolean) Hypervisor.popEvaluate(context.threadStackContext);
- if(expression1) {
- Code code2 = (Code) Memory.Process.resolveExecuteItem(context.process, new Access.MultipleAccess(context.code.codeList.get(new Identifier.Property("code")), new StructureAccess(new Identifier.Property("index", call.step2))))[0];
- Hypervisor.pushExecutePassthrough(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code2), call.executeAsExpression), call);
- call.step = -1;
- }
- else {
- call.step2++;
- if(call.step2 < size2) {
- call.step = 0;
- }
- }
- }
- },
- new ExecuteScript() { // 2
- public void exec(CodeContext context, Call call) {
- int size3 = ((Structure.List.Vector) Memory.Process.resolveExecuteItem(context.process, context.code.codeList.get(new Identifier.Property("code")))[0]).size();
- if(call.step2 < size3) {
- Code code2 = (Code) Memory.Process.resolveExecuteItem(context.process, new Access.MultipleAccess(context.code.codeList.get(new Identifier.Property("code")), new StructureAccess(new Identifier.Property("index", call.step2))))[0];
- Hypervisor.pushExecutePassthrough(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code2), call.executeAsExpression), call);
- }
- call.step = -1;
- }
- }
- }
- ));
- codeOut.put(Operation.OperationType.Control.OP_FOR_STEP, new CodeOutDef(
- new DisplayScript() {
- public void exec(CodeContext context, CodeTreeContext treeContext) {
- //...
- }
- },
- new ExecuteScript[] {
- // OP_WHILE + step + decl
- // TODO provide iterator
- new ExecuteScript() { // 0
- public void exec(CodeContext context, Call call) {
- Code code1 = (Code) Memory.Process.resolveExecuteItem(context.process, context.code.dataList.get(new Identifier.Property("cond")))[0];
- Hypervisor.pushExecuteExpression(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code1), true));
- }
- },
- new ExecuteScript() { // 1
- public void exec(CodeContext context, Call call) {
- boolean expression1 = (Boolean) Hypervisor.popEvaluate(context.threadStackContext);
- if(expression1) {
- Code code2 = (Code) Memory.Process.resolveExecuteItem(context.process, context.code.codeList.get(new Identifier.Property("code")))[0];
- Hypervisor.pushExecutePassthrough(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code2), call.executeAsExpression), call);
- call.step2++;
- }
- else {
- call.step = -1;
- }
- }
- },
- new ExecuteScript() { // 2
- public void exec(CodeContext context, Call call) {
- Code code1 = (Code) Memory.Process.resolveExecuteItem(context.process, context.code.codeList.get(new Identifier.Property("step")))[0];
- Hypervisor.pushExecute(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code1), false));
- call.step = 0;
- }
- }
- }
- ));
- codeOut.put(Operation.OperationType.Control.OP_FOR_LIST, new CodeOutDef(
- new DisplayScript() {
- public void exec(CodeContext context, CodeTreeContext treeContext) {
- //...
- }
- },
- new ExecuteScript[] {
- // TODO provide iterator
- new ExecuteScript() {
- public void exec(CodeContext context, Call call) {
- }
- }
- }
- ));
- codeOut.put(Operation.OperationType.Control.OP_FOR_LOOP, new CodeOutDef(
- new DisplayScript() {
- public void exec(CodeContext context, CodeTreeContext treeContext) {
- //...
- }
- },
- new ExecuteScript[] {
- // TODO provide iterator
- new ExecuteScript() {
- public void exec(CodeContext context, Call call) {
- }
- }
- }
- ));
- codeOut.put(Operation.OperationType.Control.OP_WHILE, new CodeOutDef(
- new DisplayScript() {
- public void exec(CodeContext context, CodeTreeContext treeContext) {
- }
- },
- new ExecuteScript[] {
- // OP_IF as loop
- // TODO provide iterator
- new ExecuteScript() { // 0
- public void exec(CodeContext context, Call call) {
- if(call.step2 == 0 && context.code.propertyList.containsKey(new Identifier.Property("do while"))) {
- }
- else {
- Code code1 = (Code) Memory.Process.resolveExecuteItem(context.process, context.code.dataList.get(new Identifier.Property("cond")))[0];
- Hypervisor.pushExecuteExpression(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code1), true));
- }
- }
- },
- new ExecuteScript() { // 1
- public void exec(CodeContext context, Call call) {
- boolean expression1 = (Boolean) Hypervisor.popEvaluate(context.threadStackContext);
- if(expression1) {
- Code code2 = (Code) Memory.Process.resolveExecuteItem(context.process, context.code.codeList.get(new Identifier.Property("code")))[0];
- Hypervisor.pushExecutePassthrough(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code2), call.executeAsExpression), call);
- call.step2++;
- call.step = 0;
- }
- else {
- call.step = -1;
- }
- }
- }
- }
- ));
- codeOut.put(Operation.OperationType.Control.OP_INTERRUPT, new CodeOutDef(
- new DisplayScript() {
- public void exec(CodeContext context, CodeTreeContext treeContext) {
- }
- },
- new ExecuteScript[] {
- }
- ));
- codeOut.put(Operation.OperationType.Control.OP_ASSERT, new CodeOutDef(
- new DisplayScript() {
- public void exec(CodeContext context, CodeTreeContext treeContext) {
- }
- },
- new ExecuteScript[] {
- new ExecuteScript() { // 0
- public void exec(CodeContext context, Call call) {
- Code code1 = (Code) Memory.Process.resolveExecuteItem(context.process, context.code.dataList.get(new Identifier.Property("cond")))[0];
- Hypervisor.pushExecuteExpression(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code1), true));
- }
- },
- new ExecuteScript() { // 1
- public void exec(CodeContext context, Call call) {
- boolean expression1 = (Boolean) Hypervisor.popEvaluate(context.threadStackContext);
- if(!expression1) {
- // TODO
- //Code code2 = (Code) Memory.Process.resolveExecuteItem(context.process, context.code.codeList.get(new Identifier.Property("code")))[0];
- //Hypervisor.pushExecutePassthrough(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code2), call.executeAsExpression), call);
- }
- call.step = -1;
- }
- }
- }
- ));
- // Expression
- codeOut.put(Operation.OperationType.Expression.OP_ACCESS, new CodeOutDef(
- new DisplayScript() {
- public void exec(CodeContext context, CodeTreeContext treeContext) {
- TreePosition treePosition = new TreePosition(treeContext);
- displayAccess(context, treeContext, treePosition);
- treePosition.applySize();
- }
- },
- new ExecuteScript[] {
- new ExecuteScript() {
- public void exec(CodeContext context, Call call) {
- Access access1 = (Access) Memory.Process.resolveItem(context.process, context.code.dataList.get(new Identifier.Property("source")))[0];
- Object expression1 = Memory.Process.resolveItem(context.process, access1)[0];
- Hypervisor.pushEvaluate(context.threadStackContext, expression1);
- }
- }
- }
- ));
- codeOut.put(Operation.OperationType.Expression.OP_ASSIGN, new CodeOutDef(
- new DisplayScript() {
- public void exec(CodeContext context, CodeTreeContext treeContext) {
- }
- },
- new ExecuteScript[] {
- new ExecuteScript() {
- public void exec(CodeContext context, Call call) {
- Code code1 = (Code) Memory.Process.resolveExecuteItem(context.process, context.code.dataList.get(new Identifier.Property("source")))[0];
- Hypervisor.pushExecuteExpression(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code1), true));
- }
- },
- new ExecuteScript() {
- public void exec(CodeContext context, Call call) {
- Access access1 = (Access) Memory.Process.resolveItem(context.process, context.code.dataList.get(new Identifier.Property("destination")))[0];
- Object[] access2 = Memory.Process.resolveItem(context.process, access1);
- access2[0] = Hypervisor.getEvaluate(context.threadStackContext); // keep on stack
- }
- }
- }
- ));
- codeOut.put(Operation.OperationType.Expression.OP_ASSIGN_OP, new CodeOutDef(
- new DisplayScript() {
- public void exec(CodeContext context, CodeTreeContext treeContext) {
- }
- },
- new ExecuteScript[] {
- new ExecuteScript() {
- public void exec(CodeContext context, Call call) {
- Code code1 = (Code) Memory.Process.resolveExecuteItem(context.process, context.code.dataList.get(new Identifier.Property("source")))[0];
- Hypervisor.pushExecuteExpression(context.threadStackContext, new Call(new CodeContext.CodeContextNew(context, code1), true));
- // TODO push evaluate and execute operator
- }
- },
- new ExecuteScript() {
- public void exec(CodeContext context, Call call) {
- Access access1 = (Access) Memory.Process.resolveItem(context.process, context.code.dataList.get(new Identifier.Property("destination")))[0];
- Object[] access2 = Memory.Process.resolveItem(context.process, access1);
- access2[0] = Hypervisor.getEvaluate(context.threadStackContext); // keep on stack
- }
- }
- }
- ));
- codeOut.put(Operation.OperationType.Expression.OP_TRIGGER, new CodeOutDef(
- new DisplayScript() {
- public void exec(CodeContext context, CodeTreeContext treeContext) {
- //...
- }
- },
- new ExecuteScript[] {
- }
- ));
- // Arithmetic
- {
- CodeOutDef codeOutDef1 = new CodeOutDef(
- CodeOutGroup.multipleExpressionDisplayScript,
- CodeOutGroup.multipleArithmeticExecuteScript
- );
- codeOut.put(Operation.OperationType.Arithmetic.OP_ADD, codeOutDef1);
- codeOut.put(Operation.OperationType.Arithmetic.OP_MUL, codeOutDef1);
- }
- {
- CodeOutDef codeOutDef1 = new CodeOutDef(
- CodeOutGroup.binaryExpressionDisplayScript,
- CodeOutGroup.binaryArithmeticExecuteScript
- );
- codeOut.put(Operation.OperationType.Arithmetic.OP_SUB, codeOutDef1);
- codeOut.put(Operation.OperationType.Arithmetic.OP_DIV, codeOutDef1);
- codeOut.put(Operation.OperationType.Arithmetic.OP_MOD, codeOutDef1);
- }
- {
- CodeOutDef codeOutDef1 = new CodeOutDef(
- CodeOutGroup.unaryExpressionDisplayScript,
- CodeOutGroup.unaryArithmeticExecuteScript
- );
- codeOut.put(Operation.OperationType.Arithmetic.OP_NEGATE, codeOutDef1);
- }
- codeOut.put(Operation.OperationType.Arithmetic.OP_INC, new CodeOutDef(
- CodeOutGroup.unaryExpressionDisplayScript,
- new ExecuteScript[] {
- new ExecuteScript() {
- public void exec(CodeContext context, Call call) {
- //TODO resolve access and execute (single step)
- // expect access
- //assert code1.get(0).operationType == Operation.OperationType.Expression.OP_ACCESS;
- }
- }
- }
- ));
- codeOut.put(Operation.OperationType.Arithmetic.OP_DEC, new CodeOutDef(
- CodeOutGroup.unaryExpressionDisplayScript,
- new ExecuteScript[] {
- new ExecuteScript() {
- public void exec(CodeContext context, Call call) {
- //TODO resolve access and execute (single step)
- // expect access
- }
- }
- }
- ));
- // Bitwise
- {
- CodeOutDef codeOutDef1 = new CodeOutDef(
- CodeOutGroup.multipleExpressionDisplayScript,
- CodeOutGroup.multipleBitwiseExecuteScript
- );
- codeOut.put(Operation.OperationType.Bitwise.OP_AND, codeOutDef1);
- codeOut.put(Operation.OperationType.Bitwise.OP_OR, codeOutDef1);
- codeOut.put(Operation.OperationType.Bitwise.OP_XOR, codeOutDef1);
- }
- {
- CodeOutDef codeOutDef1 = new CodeOutDef(
- CodeOutGroup.binaryExpressionDisplayScript,
- CodeOutGroup.binaryBitwiseExecuteScript
- );
- codeOut.put(Operation.OperationType.Bitwise.OP_SHR, codeOutDef1);
- codeOut.put(Operation.OperationType.Bitwise.OP_SHL, codeOutDef1);
- codeOut.put(Operation.OperationType.Bitwise.OP_ROR, codeOutDef1);
- codeOut.put(Operation.OperationType.Bitwise.OP_ROL, codeOutDef1);
- }
- {
- CodeOutDef codeOutDef1 = new CodeOutDef(
- CodeOutGroup.unaryExpressionDisplayScript,
- CodeOutGroup.unaryBitwiseExecuteScript
- );
- codeOut.put(Operation.OperationType.Bitwise.OP_COMPLEMENT, codeOutDef1);
- }
- // Logic
- {
- CodeOutDef codeOutDef1 = new CodeOutDef(
- CodeOutGroup.multipleExpressionDisplayScript,
- CodeOutGroup.multipleLogicExecuteScript
- );
- codeOut.put(Operation.OperationType.Logic.OP_AND, codeOutDef1);
- codeOut.put(Operation.OperationType.Logic.OP_OR, codeOutDef1);
- codeOut.put(Operation.OperationType.Logic.OP_XOR, codeOutDef1);
- }
- {
- CodeOutDef codeOutDef1 = new CodeOutDef(
- CodeOutGroup.binaryExpressionDisplayScript,
- CodeOutGroup.binaryLogicExecuteScript
- );
- codeOut.put(Operation.OperationType.Logic.OP_EQ, codeOutDef1);
- codeOut.put(Operation.OperationType.Logic.OP_NEQ, codeOutDef1);
- codeOut.put(Operation.OperationType.Logic.OP_GT, codeOutDef1);
- codeOut.put(Operation.OperationType.Logic.OP_GTE, codeOutDef1);
- codeOut.put(Operation.OperationType.Logic.OP_LT, codeOutDef1);
- codeOut.put(Operation.OperationType.Logic.OP_LTE, codeOutDef1);
- }
- {
- CodeOutDef codeOutDef1 = new CodeOutDef(
- CodeOutGroup.unaryExpressionDisplayScript,
- CodeOutGroup.unaryLogicExecuteScript
- );
- codeOut.put(Operation.OperationType.Logic.OP_NOT, codeOutDef1);
- }
- }
- //...
- public static class CodeOutDef {
- public DisplayScript displayScript;
- public ExecuteScript[] executeScript;
- public CodeOutDef(DisplayScript displayScript, ExecuteScript[] executeScript) {
- this.displayScript = displayScript;
- this.executeScript = executeScript;
- };
- }
- public static abstract class DisplayScript {
- public abstract void exec(CodeContext context, CodeTreeContext treeContext);
- }
- public static abstract class ExecuteScript {
- public abstract void exec(CodeContext context, Call call);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment