public Boolean RunTest() { try { ANTLRStringStream sStream = new ANTLRStringStream(input); SGLLexer lexer = new SGLLexer(sStream); CommonTokenStream tStream = new CommonTokenStream(lexer); // Parsing SGLParser parser = new SGLParser(tStream); CommonTree t = (CommonTree)parser.compilationUnit().Tree; // Printing tree Console.WriteLine("; " + t.ToStringTree()); // TreeWalking CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t); SGLTreeWalker tw = new SGLTreeWalker(treeStream); SGLNode returned = tw.compilationUnit(); returned.Evaluate(); realOutput = tw.GetStoryboardCode().ToString(); // comparison realOutput = realOutput.Trim(); output.Trim(); if (output.Equals(realOutput)) { return true; } else { return false; } } catch (SGLCompilerException ce) { if (ce.ErrorType.Equals("Antlr.Parser")) { Console.WriteLine("Error (wrong syntax) on " + ce.Message); } else { Console.WriteLine("Error (" + ce.ErrorType + ") on line " + ce.Line + ": " + ce.Message); } return false; } catch (Exception ex) { Console.WriteLine("Es ist ein Fehler aufgetreten."); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); return false; } }
public SGLValue Invoke(List<SGLNode> parameters, Dictionary<String, Method> functions, StringBuilder sb, Scope globalScope) { // Test if the amount of parameters are the same as the identifiers of this method if (parameters.Count != identifiers.Count) { throw new Exception("illegal function call: " + identifiers.Count + " parameters expected for function `" + id + "`"); } // Convert the list of Nodes into Values List<SGLValue> paramValues = new List<SGLValue>(); foreach(SGLNode currentNode in parameters) { paramValues.Add(currentNode.Evaluate()); } // Test if the type of parameters are the same as the identifiers for (int i = 0; i < paramValues.Count; i++) { if (!paramValues[i].GetVarType().Equals(identTypes[i])) { throw new Exception("illegal function call: " + paramValues.ToString() + " does not match " + identTypes.ToString()); } } // Add the methods scope as a child to the global scope scope = new Scope(globalScope); // Assign all expression parameters to this method's identifiers for (int i = 0; i < identifiers.Count; i++) { scope.Assign(identifiers[i], paramValues[i], true, identTypes[i]); } try { // Create a tree walker to evaluate this method's code block CommonTreeNodeStream nodes = new CommonTreeNodeStream(code); SGLTreeWalker walker = new SGLTreeWalker(nodes, scope, functions); // Ok executing the function then SGLValue returnValue = walker.compilationUnit().Evaluate(); // Add Function's SB to the main SB sb.Append(walker.GetStoryboardCode().ToString()); if (!returnValue.GetVarType().Equals(this.returnType)) { throw new Exception("The method doesn't return the expected return type (" + returnValue.ToString() + " is not from type " + this.returnType + ")"); } return returnValue; } catch (RecognitionException e) { // do not recover from this throw new Exception("something went wrong, terminating", e); } }
private String GenerateStoryboardCode(CommonTreeNodeStream input) { Stopwatch timeNeeded = timeRecording ? new Stopwatch() : null; if (timeRecording) timeNeeded.Start(); // Parser: Converting the tokens into an abstract syntax tree SGLTreeWalker treewalker = new SGLTreeWalker(input, this.methods); treewalker.compilationUnit().Evaluate(); String output = treewalker.GetStoryboardCode().ToString(); if (timeRecording) { timeNeeded.Stop(); Console.WriteLine("Time needed for converting the tree into storyboard code: " + timeNeeded.ElapsedMilliseconds + " ms (" + timeNeeded.Elapsed + ")"); } return output; }