public IntermediateCode(IntermediateCode icode = null) { if (icode == null) { m_formatter = new BinaryFormatter(); } else { m_formatter = new BinaryFormatter(icode.m_formatter.Stream); } }
public void PopFrame(SymtabNode pRoutineId, ref IntermediateCode pIcode) { TFrameHeader pHeader = (TFrameHeader)stack[pFrameBase]; //--Don't do anything if it's the bottommost stack frame. if (pFrameBase != 0) { //--Return to the caller's intermediate code. pIcode = pHeader.returnAddress.icode; pIcode.GoTo((int)pHeader.returnAddress.location); //--Cut the stack back. Leave a function value on top. tos = pFrameBase; //if (pRoutineId.defn.how != TDefnCode.dcFunction) --tos; // pFrameBase = (TStackItem)pHeader.dynamicLink.Value; } }
static void Main(string[] args) { // string source = @" //PROGRAM fibonacci (output); // //VAR // number : integer; // //FUNCTION fib (n : integer) : integer; // // BEGIN // IF n <= 1 THEN fib := n // ELSE fib := fib(n - 2) + fib(n - 1) // END; // //BEGIN // FOR number:= 0 TO 16 DO BEGIN // writeln('number = ', number:2, // ' fibonacci = ', fib(number):4); // END; //END."; string simpleSource = @"identifier := 123; somestring := 'abc'; updatevalue := identifier + 5; output := updatevalue ."; string testNewtons = @"{Square roots by Newton's algorithm.} number := input; root := number; root := (number/root + root)/2; output := root; root := (number/root + root)/2; output := root; root := (number/root + root)/2; output := root; root := (number/root + root)/2; output := root; root := (number/root + root)/2; output := root; root := (number/root + root)/2; output := root; root := (number/root + root)/2; output := root; root := (number/root + root)/2; output := root; root := (number/root + root)/2; output := root; ."; string source; using (StreamReader reader = new StreamReader(File.OpenRead("source.pas"))) { source = reader.ReadToEnd(); } SharedProperties.globalSymtab = Symtab.CreateTable(); TextBuffer buffer = new TextBuffer(File.OpenRead("source.pas")); IntermediateCode icode = new IntermediateCode(); SymtabStack symbolTableStack = new SymtabStack(SharedProperties.globalSymtab); StagingParser parser = new StagingParser(buffer, icode, symbolTableStack); parser.Error += new EventHandler<ErrorEventArgs>(parser_Error); parser.Parse(); Console.WriteLine(); // -- print the parsers summary string message = string.Format("{0} source lines", buffer.LineNumber - 1); int spaceCount = (Console.WindowWidth - message.Length) / 2; Console.WriteLine(string.Format("{0," + spaceCount + "}", message)); message = string.Format("{0} syntax errors.", buffer.ErrorCount); spaceCount = (Console.WindowWidth - message.Length) / 2; Console.WriteLine(string.Format("{0," + spaceCount + "}", message)); Console.WriteLine(); TExecutor executer = new TExecutor(icode); executer.Error += new EventHandler<RuntimeErrorArgs>(executer_Error); executer.Go(); if (executer.ErrorCount > 0) { Console.WriteLine("Successful completion. {0} statements executed.", executer.StatementCount); } else { Console.WriteLine("Completed with {0} errors.", executer.ErrorCount); } Console.ReadKey(); }
protected TTokenCode token; // code of current token #endregion Fields #region Constructors public TBackend(IntermediateCode icode) { this.icode = icode; }
public TExecutor(IntermediateCode icode) : base(icode) { pInputNode = SharedProperties.globalSymtab.Search("input"); pOutputNode = SharedProperties.globalSymtab.Search("output"); }
private TTokenCode token; // code of current token #endregion Fields #region Constructors public StagingParser(TextBuffer buffer, IntermediateCode icode, SymtabStack symbolStack) { this.buffer = buffer; this.icode = icode; this.symtabStack = symbolStack; pScanner = new TextScanner(this.buffer); // -- Enter the special "input" and "output" variable identifiers // -- into the symbol table. EnterLocal("input"); EnterLocal("output"); }
public object PushFrameHeader(int oldLevel, int newLevel, IntermediateCode pIcode) { TFrameHeader pHeader = (TFrameHeader)stack[pFrameBase]; object pNewFrameBase = stack[tos + 1]; // point to item just above // current TOS item Push(0); // function return value (placeholder) //--Compute the static link. if (newLevel == oldLevel + 1) { //--Callee nested within caller: //--Push address of caller's stack frame. Push(pHeader); } else if (newLevel == oldLevel) { //--Callee at same level as caller: //--Push address of common parent's stack frame. Push(pHeader.staticLink); } else /* newLevel < oldLevel */ { //--Callee nested less deeply than caller: //--Push address of nearest commmon ancestor's stack frame. int delta = oldLevel - newLevel; while (delta-- >= 0) { pHeader = (TFrameHeader)pHeader.staticLink; } Push(pHeader); } Push(pFrameBase); // dynamic link Push(pIcode); // return address icode pointer Push(0); // return address icode location (placeholder) return pNewFrameBase; }