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