public static List <Binding> BindValues(List <GPLCSource> subBlocks, int offset, List <string> errorLog) { int i = 0, j; List <Binding> bs = new List <Binding>(); Binding thisB = new Binding(); for (j = 0; j < subBlocks.Count; j = j + 2) { thisB.symbol = subBlocks[j].content; thisB.readIndex = i; thisB.writeIndex = offset + i; thisB.initValue = SafeArgumentHandler.ReadLiteral(subBlocks[j + 1], errorLog, 0); bs.Add(thisB); i++; } return(bs); }
private List <int> PatchCode(List <int> bytecode, List <GPLCSource> patchList, List <Binding> bs) { int i, offset, patch; List <string> errorLog = new List <string>(); for (i = 7; i < patchList.Count; i = i + 2) { offset = SafeArgumentHandler.RefToOffset(bs, patchList[i], 2, errorLog) + 1; patch = SafeArgumentHandler.ReadLiteral(patchList[i + 1], errorLog, 2); bytecode[offset] = patch; } if (errorLog.Count > 0) { foreach (string error in errorLog) { Console.Write(error); } Console.Write("\n\nCompilation aborted at data block patching stage."); Console.ReadLine(); Environment.Exit(1); } return(bytecode); }
private List <int> TransformArguments(List <GPLCSource> source, List <Binding> bs, List <int> mode, List <string> errorLog, int i) { int n; List <int> transformedArguments = new List <int>(); i++; for (n = 0; n < mode.Count; n++) { if (mode[n] == 0) { transformedArguments.Add(SafeArgumentHandler.RefToOffset(bs, source[i], 0, errorLog)); } else if (mode[n] == 1) { transformedArguments.Add(SafeArgumentHandler.RefToOffset(bs, source[i], 1, errorLog)); } else { transformedArguments.Add(SafeArgumentHandler.ReadLiteral(source[i], errorLog, 1)); } i++; } return(transformedArguments); }
public void TransformProgram(GPLCProgramIn progIn, List <string> errorLog) { int offset = 0, blockSize = 0, i, n; List <int> sigBlock = new List <int>(); List <int> codeBlock = new List <int>(); List <int> result = new List <int> { 0, 0, 0 }; string errorDetail; bool blockStart = true; for (i = 0; i < progIn.subBlocks.Count;) { if (blockStart == true && progIn.subBlocks[i].content != "--signal") { errorDetail = "\n\nError at line " + Convert.ToString(progIn.subBlocks[i].line) + " column " + Convert.ToString(progIn.subBlocks[i].column) + ". Every block of code must begin with --signal x, where x is the integer signal to be handled by the block."; errorLog.Add(errorDetail); break; } else if (blockStart == true) { try { sigBlock.Add(Convert.ToInt32(progIn.subBlocks[i + 1].content)); sigBlock.Add(offset); blockStart = false; i = i + 2; } catch (FormatException) { errorDetail = "\n\nError at line " + Convert.ToString(progIn.subBlocks[i + 1].line) + " column " + Convert.ToString(progIn.subBlocks[i + 1].column) + ". The second term in a signal handler statement must be an integer."; errorLog.Add(errorDetail); break; } } else { if (progIn.subBlocks[i].content == "if") { List <int> mode = new List <int> { 2, 0, 0, 2, 2 }; List <int> thisLine = new List <int> { 1 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 6; offset = offset + 6; blockSize = blockSize + 6; } else if (progIn.subBlocks[i].content == "chg_state") { List <int> mode = new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; List <int> thisLine = new List <int> { 2 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 10; offset = offset + 10; blockSize = blockSize + 10; } else if (progIn.subBlocks[i].content == "chg_grid") { List <int> mode = new List <int> { 0, 0, 0, 0, 0, 0, 0 }; List <int> thisLine = new List <int> { 3 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 8; offset = offset + 8; blockSize = blockSize + 8; } else if (progIn.subBlocks[i].content == "send_signal") { List <int> mode = new List <int> { 0, 0, 0, 0 }; List <int> thisLine = new List <int> { 4 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 5; offset = offset + 5; blockSize = blockSize + 5; } else if (progIn.subBlocks[i].content == "chg_value") { List <int> mode = new List <int> { 1, 0, 0, 0, 0, 0 }; List <int> thisLine = new List <int> { 5 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 7; offset = offset + 7; blockSize = blockSize + 7; } else if (progIn.subBlocks[i].content == "chg_value_") { List <int> mode = new List <int> { 2, 0, 0, 0, 0, 0 }; List <int> thisLine = new List <int> { 5 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 7; offset = offset + 7; blockSize = blockSize + 7; } else if (progIn.subBlocks[i].content == "chg_floor") { List <int> mode = new List <int> { 0, 0, 0, 0, 0, 0 }; List <int> thisLine = new List <int> { 6 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 7; offset = offset + 7; blockSize = blockSize + 7; } else if (progIn.subBlocks[i].content == "chg_ps1") { List <int> mode = new List <int> { 0, 0, 0 }; List <int> thisLine = new List <int> { 7 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 4; offset = offset + 4; blockSize = blockSize + 4; } else if (progIn.subBlocks[i].content == "chg_obj_type") { List <int> mode = new List <int> { 0, 0, 0, 0 }; List <int> thisLine = new List <int> { 8 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 5; offset = offset + 5; blockSize = blockSize + 5; } else if (progIn.subBlocks[i].content == "chg_grid_") { List <int> mode = new List <int> { 0, 0, 0, 0, 0, 0, 0 }; List <int> thisLine = new List <int> { 10 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 8; offset = offset + 8; blockSize = blockSize + 8; } else if (progIn.subBlocks[i].content == "copy_ps1") { List <int> mode = new List <int> { 1, 0, 0, 0 }; List <int> thisLine = new List <int> { 11 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 5; offset = offset + 5; blockSize = blockSize + 5; } else if (progIn.subBlocks[i].content == "copy_lstate") { List <int> mode = new List <int> { 1, 0, 0, 0, 0, 0, 0 }; List <int> thisLine = new List <int> { 12 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 8; offset = offset + 8; blockSize = blockSize + 8; } else if (progIn.subBlocks[i].content == "pass_msg") { List <int> mode = new List <int> { 0 }; List <int> thisLine = new List <int> { 13 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i + 1)); for (n = 3; n <= SafeArgumentHandler.ReadLiteral(progIn.subBlocks[i + 1], errorLog, 1); n++) { thisLine.Add(SafeArgumentHandler.ReadLiteral(progIn.subBlocks[i + n], errorLog, 1)); } codeBlock.AddRange(thisLine); i = i + n; offset = offset + n - 1; blockSize = blockSize + n - 1; } else if (progIn.subBlocks[i].content == "chg_ps0") { List <int> mode = new List <int> { 0, 0, 0 }; List <int> thisLine = new List <int> { 14 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 4; offset = offset + 4; blockSize = blockSize + 4; } else if (progIn.subBlocks[i].content == "copy_ps0") { List <int> mode = new List <int> { 1, 0, 0, 0 }; List <int> thisLine = new List <int> { 15 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 5; offset = offset + 5; blockSize = blockSize + 5; } else if (progIn.subBlocks[i].content == "binary_dice") { List <int> mode = new List <int> { 0, 0, 0, 0, 0, 2 }; List <int> thisLine = new List <int> { 16 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 7; offset = offset + 7; blockSize = blockSize + 7; } else if (progIn.subBlocks[i].content == "project_init") { List <int> mode = new List <int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0 }; List <int> thisLine = new List <int> { 17 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 14; offset = offset + 14; blockSize = blockSize + 14; } else if (progIn.subBlocks[i].content == "project_update") { List <int> mode = new List <int> { 0, 1, 0, 0, 0 }; List <int> thisLine = new List <int> { 18 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 6; offset = offset + 6; blockSize = blockSize + 6; } else if (progIn.subBlocks[i].content == "init_npc") { List <int> mode = new List <int> { 0, 0 }; List <int> thisLine = new List <int> { 19 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 3; offset = offset + 3; blockSize = blockSize + 3; } else if (progIn.subBlocks[i].content == "npc_decision") { List <int> mode = new List <int> { 1 }; List <int> thisLine = new List <int> { 20 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 2; offset = offset + 2; blockSize = blockSize + 2; } else if (progIn.subBlocks[i].content == "npc_move") { List <int> mode = new List <int> { 1 }; List <int> thisLine = new List <int> { 21 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 2; offset = offset + 2; blockSize = blockSize + 2; } else if (progIn.subBlocks[i].content == "npc_damage") { List <int> mode = new List <int> { 2 }; List <int> thisLine = new List <int> { 22 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 2; offset = offset + 2; blockSize = blockSize + 2; } else if (progIn.subBlocks[i].content == "cpede_move") { List <int> mode = new List <int> { 1, 2 }; List <int> thisLine = new List <int> { 23 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 3; offset = offset + 3; blockSize = blockSize + 3; } else if (progIn.subBlocks[i].content == "block") { List <int> mode = new List <int> { 2, 0, 0, 0 }; List <int> thisLine = new List <int> { 5, 536870910, 0 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 5; offset = offset + 7; blockSize = blockSize + 7; } else if (progIn.subBlocks[i].content == "place_light") { List <int> mode = new List <int> { 0, 0, 0, 0, 0, 0 }; List <int> thisLine = new List <int> { 9 }; thisLine.AddRange(TransformArguments(progIn.subBlocks, progIn.bs, mode, errorLog, i)); codeBlock.AddRange(thisLine); i = i + 7; offset = offset + 7; blockSize = blockSize + 7; } else if (progIn.subBlocks[i].content == "--signal") { sigBlock.Add(blockSize); blockSize = 0; blockStart = true; } else { errorDetail = "\n\nError at line " + Convert.ToString(progIn.subBlocks[i].line) + " column " + Convert.ToString(progIn.subBlocks[i].column) + ". " + progIn.subBlocks[i].content + " is an invalid GPLC op - code."; errorLog.Add(errorDetail); break; } } } List <int> dataBlock = new List <int>(AddDataBlock(progIn.bs)); sigBlock.Add(blockSize); result.AddRange(sigBlock); result.Add(536870911); result.AddRange(codeBlock); result.Add(536870911); result.AddRange(dataBlock); result[0] = result.Count - 1; GPLCProgramOut thisProg = new GPLCProgramOut(progIn.progName, result, progIn.bs); progGroup.Add(thisProg); }