internal void OpenBlock(BlockType blocktype, string filename, int line_number, int column_number) { AddInstruction(BytecodeInstruction.NewLabel("block_start_" + maxBlock.ToString(), filename, line_number, column_number)); openBlocks.Push(maxBlock); openBlockTypes.Push(blocktype); ++maxBlock; }
internal void CloseBlock(string filename, int line_number, int column_number) { AddInstruction(BytecodeInstruction.NewLabel("block_end_" + openBlocks.Pop().ToString(), filename, line_number, column_number)); int num = (int)openBlockTypes.Pop(); }
private static void ParseString(string s, BytecodeProgram bytecodeProgram, string filename, int lineNumber, int columnNumber, bool isMiniMaestro) { try { if (LooksLikeLiteral(s)) { Decimal num; if (s.StartsWith("0X")) { num = (Decimal)long.Parse(s.Substring(2), NumberStyles.HexNumber); if (num > new Decimal((int)ushort.MaxValue) || num < new Decimal(0)) { throw new Exception("Value " + s + " is not in the allowed range of " + (object)(ushort)0 + " to " + (object)ushort.MaxValue + "."); } if ((Decimal)(ushort)num != num) { throw new Exception("Value " + s + " must be an integer."); } } else { num = Decimal.Parse(s); if (num > new Decimal((int)short.MaxValue) || num < new Decimal((int)short.MinValue)) { throw new Exception("Value " + s + " is not in the allowed range of " + (object)short.MinValue + " to " + (object)short.MaxValue + "."); } if ((Decimal)(short)num != num) { throw new Exception("Value " + s + " must be an integer."); } } int literal = (int)(short)(long)(num % new Decimal((int)ushort.MaxValue)); bytecodeProgram.AddLiteral(literal, filename, lineNumber, columnNumber, isMiniMaestro); return; } } catch (Exception ex) { throw new Exception("Error parsing " + s + ": " + ex.ToString()); } if (s == Keyword.GOTO.ToString()) { mode = Mode.GOTO; } else if (s == Keyword.SUB.ToString()) { mode = Mode.SUBROUTINE; } else { Match match = Regex.Match(s, "(.*):$"); if (match.Success) { bytecodeProgram.AddInstruction(BytecodeInstruction.NewLabel("USER_" + match.Groups[1].ToString(), filename, lineNumber, columnNumber)); } else if (s == Keyword.BEGIN.ToString()) { bytecodeProgram.OpenBlock(BlockType.BEGIN, filename, lineNumber, columnNumber); } else if (s == Keyword.WHILE.ToString()) { if (bytecodeProgram.GetCurrentBlockType() != BlockType.BEGIN) { throw new Exception("WHILE must be inside a BEGIN...REPEAT block"); } bytecodeProgram.AddInstruction(BytecodeInstruction.NewConditionalJumpToLabel(bytecodeProgram.GetCurrentBlockEndLabel(), filename, lineNumber, columnNumber)); } else if (s == Keyword.REPEAT.ToString()) { try { if (bytecodeProgram.GetCurrentBlockType() != BlockType.BEGIN) { throw new Exception("REPEAT must end a BEGIN...REPEAT block"); } bytecodeProgram.AddInstruction(BytecodeInstruction.NewJumpToLabel(bytecodeProgram.GetCurrentBlockStartLabel(), filename, lineNumber, columnNumber)); bytecodeProgram.CloseBlock(filename, lineNumber, columnNumber); } catch (InvalidOperationException) { throw new Exception(filename + ":" + (object)lineNumber + ":" + (object)columnNumber + ": Found REPEAT without a corresponding BEGIN"); } } else if (s == Keyword.IF.ToString()) { bytecodeProgram.OpenBlock(BlockType.IF, filename, lineNumber, columnNumber); bytecodeProgram.AddInstruction(BytecodeInstruction.NewConditionalJumpToLabel(bytecodeProgram.GetCurrentBlockEndLabel(), filename, lineNumber, columnNumber)); } else if (s == Keyword.ENDIF.ToString()) { try { if (bytecodeProgram.GetCurrentBlockType() != BlockType.IF && bytecodeProgram.GetCurrentBlockType() != BlockType.ELSE) { throw new Exception("ENDIF must end an IF...ENDIF or an IF...ELSE...ENDIF block."); } bytecodeProgram.CloseBlock(filename, lineNumber, columnNumber); } catch (InvalidOperationException) { throw new Exception(filename + ":" + (object)lineNumber + ":" + (object)columnNumber + ": Found ENDIF without a corresponding IF"); } } else if (s == Keyword.ELSE.ToString()) { try { if (bytecodeProgram.GetCurrentBlockType() != BlockType.IF) { throw new Exception("ELSE must be part of an IF...ELSE...ENDIF block."); } bytecodeProgram.AddInstruction(BytecodeInstruction.NewJumpToLabel(bytecodeProgram.GetNextBlockEndLabel(), filename, lineNumber, columnNumber)); bytecodeProgram.CloseBlock(filename, lineNumber, columnNumber); bytecodeProgram.OpenBlock(BlockType.ELSE, filename, lineNumber, columnNumber); } catch (InvalidOperationException) { throw new Exception(filename + ":" + (object)lineNumber + ":" + (object)columnNumber + ": Found ELSE without a corresponding IF"); } } else { try { OperationCode op = dictionary[s]; switch (op) { case OperationCode.LITERAL: case OperationCode.LITERAL8: case OperationCode.LITERAL_N: case OperationCode.LITERAL8_N: throw new Exception(filename + ":" + (object)lineNumber + ":" + (object)columnNumber + ": Literal commands may not be used directly in a program. Integers should be entered directly."); case OperationCode.JUMP: case OperationCode.JUMP_Z: throw new Exception(filename + ":" + (object)lineNumber + ":" + (object)columnNumber + ": Jumps may not be used directly in a program."); default: if (!isMiniMaestro && (byte)op >= (byte)50) { throw new Exception(filename + ":" + (object)lineNumber + ":" + (object)columnNumber + ": " + op.ToString() + " is only available on the Mini Maestro 12, 18, and 24."); } bytecodeProgram.AddInstruction(new BytecodeInstruction(op, filename, lineNumber, columnNumber)); break; } } catch (KeyNotFoundException) { bytecodeProgram.AddInstruction(BytecodeInstruction.NewCall(s, filename, lineNumber, columnNumber)); } } } }