Beispiel #1
0
        //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"));
        }
Beispiel #2
0
 //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());
     }
 }
Beispiel #3
0
        //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);
        }
Beispiel #4
0
        /// <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();
                    }
                }
            }
        }
Beispiel #5
0
        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();
        }
Beispiel #6
0
 static object Perent_Fu(SymbolTable s)
 {
     return (s.GetValue(new Id("obj")) as ITreeNode).Perent;
 }
Beispiel #7
0
        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
        }
Beispiel #8
0
 static object ActiveScope_Fu(SymbolTable s)
 {
     return raptor.ActiveScope;
 }
Beispiel #9
0
 static object HashCode_Fu(SymbolTable s)
 {
     return s.GetValue(new Id("obj")).GetHashCode();
 }
Beispiel #10
0
 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;
 }
Beispiel #11
0
 static object ReadLine_Fu(SymbolTable s)
 {
     return Console.ReadLine();
 }
Beispiel #12
0
 public SymbolTable(SymbolTable father) {
     this.Perent = father;
     _symbols = new Dictionary<string, object>();
 }
Beispiel #13
0
 public void SetSymbolTable(SymbolTable symbolTable)
 {
     SymbolTable = symbolTable;
 }
Beispiel #14
0
 public Interpreter() {
     pStack = new Stack<object>();
     activeScope = new SymbolTable(null);
 }
Beispiel #15
0
 public object Return(SymbolTable s){
     stopFlag = true;
     return new ReturnObject(s.GetValue(new Id("toReturn")));
 }
Beispiel #16
0
        //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;
        }
Beispiel #17
0
 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);
 }
Beispiel #18
0
        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();
        }
Beispiel #19
0
 //clears the screan
 static object Clear_Fu(SymbolTable s)
 {
     Console.Clear();
     return new Void();
 }
Beispiel #20
0
        public void EnterScope(SymbolTable t) {
#if _DEBUG
            Console.WriteLine("Entering Scope " + t);
#endif
            this.activeScope = t;
        }
Beispiel #21
0
 //prints a variable to the screan
 static object Peek_Fu(SymbolTable s)
 {
     Console.Write(raptor.ProcessStack.Peek());
     return new Void();
 }
Beispiel #22
0
 public Interpreter(Stack<object> proccesStack, SymbolTable activeScope) {
     pStack = proccesStack;
     this.EnterScope(activeScope);
 }
Beispiel #23
0
 static object DoubleCast_Fu(SymbolTable s)
 {
     object toCast = s.GetValue(new Id("toCast"));
     return Convert.ChangeType(toCast, typeof(double));
 }
Beispiel #24
0
 //pops one item from the pStack
 static object Pop_Fu(SymbolTable s)
 {
     raptor.ProcessStack.Pop();
     return new Void();
 }
Beispiel #25
0
        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;
        }
Beispiel #26
0
 //prints a variable to the screan
 static object Print_Fu(SymbolTable s)
 {
     Console.Write(s.GetValue(new Id("~toPrint")));
     return new Void();
 }