//Decloration static object Def_Fu(SymbolTable s) { if (!(s.GetVariable(new Id("~id")) is Id)) throw new Exception("invalid ID"); string name = (s.GetVariable(new Id("~id")) as Id).ToString(); object value = s.GetValue(new Id("~value")); s.Perent.AddVariable(name, value); //Console.WriteLine("variable \"" + name + "\" has been assigend with the value \"" +value.ToString() +"\""); return s.GetVariable(new Id("~id")); }
//you can add 2 variables static object Add_Fu(SymbolTable s) { object a = s.GetValue(new Id("~a")); object b = s.GetValue(new Id("~b")); try { return (double)a + (double)b; } catch { throw new Exception("you cannot add " + a.GetType() + " and " + b.GetType()); } }
//assigens value to variable static object Be_Fu(SymbolTable s) { if (!(s.GetVariable(new Id("~id")) is Id)) throw new Exception("invalid ID"); Id name = (s.GetVariable(new Id("id")) as Id); object value = s.GetVariable(new Id("~value")); while (value is Id) value = s.GetVariable(value as Id); //Console.WriteLine("variable \"" + name + "\" has been assigend with the value \"" + value.ToString() + "\""); return s.SetVariable(name, value); }
/// <summary> /// Interprets root and generates XHTML files /// </summary> /// <param name="tree">Tree to interpret</param> public void InterpretAST(SyntaxTree tree) { Module root = tree.GetRoot(); //Get dependency's and add functions to SymbolTable List<Module> dependencyList = ModuleCache.Instance.RequestDependencies(root); SymbolTable = new SymbolTable(); foreach (Module module in dependencyList) { foreach (FunctionDefinition function in module.GetFunctionDefinitions()) { SymbolTable.AddFunctionDefinition(function); } } //Interpret the main function if (SymbolTable.ContainsFunction("main")) { XHTMLVisitor xhtmlVisitor = new XHTMLVisitor(SymbolTable); SymbolTable.GetFunctionDefinition("main").AcceptVisitor(xhtmlVisitor); //Write xhtml output XHTMLStreamWriter writer = new XHTMLStreamWriter(Console.Out, XHTMLStreamWriter.DocType.TRANSITIONAL, xhtmlVisitor.GetTree()); writer.WriteStream(); } //Interpret all sites an write to files foreach (Module module in dependencyList) { foreach (Site site in module.GetSites()) { foreach (Mapping mapping in site.GetMappings()) { //Get function which should be called Markup markup = mapping.GetMarkup(); //Determine site path and open writer and lets interpret it Writer = CreateOutputStream(mapping.GetPath().ToString()); XHTMLVisitor xhtmlVisitor = new XHTMLVisitor(SymbolTable); markup.AcceptVisitor(xhtmlVisitor); //Write xhtml output XHTMLStreamWriter writer = new XHTMLStreamWriter(Writer, XHTMLStreamWriter.DocType.TRANSITIONAL, xhtmlVisitor.GetTree()); writer.WriteStream(); } } } }
static object DefMethod_Fu(SymbolTable s) { //body -opCodes //paramList -list //id - id if (!(s.GetVariable(new Id("~id")) is Id)) throw new Exception("invalid ID"); string name = (s.GetVariable(new Id("~id")) as Id).ToString(); //db Opcodes body = s.GetVariable(new Id("~body")) as Opcodes; List<object> raw = s.GetValue(new Id("~paramList")) as List<object>; List<string> param = raw.Select(k => (string)k).ToList(); #if _DEBUG string toSay = "Defining new Function\n name: " + name+"\n params: "; foreach (var item in param) toSay += item; Console.WriteLine(toSay); #endif s.Perent.AddFunction(name, body, param); return new Void(); }
static object Perent_Fu(SymbolTable s) { return (s.GetValue(new Id("obj")) as ITreeNode).Perent; }
public void ScopeOut() { #if _DEBUG Console.Write("Leaving Scope " + this.ActiveScope); #endif this.activeScope = this.activeScope.Perent; if (activeScope == null) throw new Exception(" you are trying to scope out of the last scoup"); #if _DEBUG Console.WriteLine(" to scope " + this.ActiveScope); #endif }
static object ActiveScope_Fu(SymbolTable s) { return raptor.ActiveScope; }
static object HashCode_Fu(SymbolTable s) { return s.GetValue(new Id("obj")).GetHashCode(); }
static object ReadFile_Fu(SymbolTable s) { System.IO.StreamReader myFile = new System.IO.StreamReader(s.GetValue(new Id("~path"))as string); string myString = myFile.ReadToEnd(); myFile.Close(); return myString; }
static object ReadLine_Fu(SymbolTable s) { return Console.ReadLine(); }
public SymbolTable(SymbolTable father) { this.Perent = father; _symbols = new Dictionary<string, object>(); }
public void SetSymbolTable(SymbolTable symbolTable) { SymbolTable = symbolTable; }
public Interpreter() { pStack = new Stack<object>(); activeScope = new SymbolTable(null); }
public object Return(SymbolTable s){ stopFlag = true; return new ReturnObject(s.GetValue(new Id("toReturn"))); }
//casting static object ListCast_Fu(SymbolTable s) { Opcodes p = s.GetValue(new Id("items")) as Opcodes; List<object> toReturn = new List<object>(); foreach (object a in p.ops) { if (a is Token) { switch ((a as Token).type) { case Token.Type.String: toReturn.Add((a as Token).lexema); break; case Token.Type.Double: toReturn.Add(Double.Parse((a as Token).lexema)); break; case Token.Type.Integer: toReturn.Add(Double.Parse((a as Token).lexema)); break; } } } return toReturn; }
static object Indexer_Fu(SymbolTable s) { int p = (int)s.GetValue(new Id("~index")); IEnumerable<object> ls =( IEnumerable<object>) s.GetValue(new Id("~enums")); return ls.ElementAt<object>(p); }
static object Eval_Fu(SymbolTable s) { raptor.EnterScope(new SymbolTable(raptor.ActiveScope)); string row = s.GetValue(new Id("~string")) as string; LinkedList<object> tokens = null; try { try { nizer = new Tokenizer(row); tokens = nizer.Tokenize(); } catch (Exception e) { throw new Exception("Syntax Error: " + e.Message); }/* foreach (Token item in tokens) { Console.WriteLine("[" + item.type + "] " + item.lexema); }*/ try { raptor.Process(tokens); } catch (Exception e) { throw new Exception("Interpritation Error: " + e.Message); } } catch (Exception e) { Console.WriteLine(" "+e.Message); raptor.Reset(); } raptor.ScopeOut(); return new Void(); }
//clears the screan static object Clear_Fu(SymbolTable s) { Console.Clear(); return new Void(); }
public void EnterScope(SymbolTable t) { #if _DEBUG Console.WriteLine("Entering Scope " + t); #endif this.activeScope = t; }
//prints a variable to the screan static object Peek_Fu(SymbolTable s) { Console.Write(raptor.ProcessStack.Peek()); return new Void(); }
public Interpreter(Stack<object> proccesStack, SymbolTable activeScope) { pStack = proccesStack; this.EnterScope(activeScope); }
static object DoubleCast_Fu(SymbolTable s) { object toCast = s.GetValue(new Id("toCast")); return Convert.ChangeType(toCast, typeof(double)); }
//pops one item from the pStack static object Pop_Fu(SymbolTable s) { raptor.ProcessStack.Pop(); return new Void(); }
public SymbolTable GetCallSymbolTable(SymbolTable s, List<object> variables) { SymbolTable fuTable = new SymbolTable(s); #if _DEBUG Console.WriteLine("Building call table "+fuTable+"..."); #endif if (variables.Count != this.parameters.Count) { throw new Exception("Interpretation error: parameters length dont match"); } for (int i = 0; i < variables.Count; i++) { fuTable.AddVariable(this.parameters[i], variables[i]); } #if _DEBUG Console.WriteLine("Done building " + fuTable); #endif return fuTable; }
//prints a variable to the screan static object Print_Fu(SymbolTable s) { Console.Write(s.GetValue(new Id("~toPrint"))); return new Void(); }