Exemplo n.º 1
0
 public override ASTNode VisitProgram([NotNull] CoolParser.ProgramContext context)
 {
     return(new ProgramNode(context)
     {
         Classes = context.classDefine().Select(x => Visit(x) as ClassNode).ToList()
     });
 }
Exemplo n.º 2
0
        public override Node VisitProgram([NotNull] CoolParser.ProgramContext context)
        {
            var cl = context.classdef();
            //bool[] b = new bool[s.Length];
            int c = 0;

            //TODO improve this:

            /*
             * in here I do an n^2 iteration through the classdef [] s
             * n order to add the classes in order of dependencies
             * meaning this to never add a class before it's parent
             * I mean I colud sure add the class without parent and then
             * when it comes up update it but this way seems to be better
             * since the classes have now a partial order :S ?
             */
            for (int j = 0; j < cl.Length; j++)
            {
                for (int i = 0; i < cl.Length; i++)
                {
                    /* In antlr when there are two IToken of the same type in a single rule production
                     * they provide a method theat returns an array of that kind of tokens
                     *
                     * this is an example: TYPE()=> ITerminalNode[], TYPE(int i) => ith ITerminalNode
                     */
                    var pname = "Object";
                    if (cl[i].TYPE(1) != null)           // if there is a type from wich this class inherits
                    {
                        pname = cl[i].TYPE(1).GetText(); // take it
                    }
                    //var f= s[i].TYPE();
                    var name = cl[i].TYPE(0).GetText();                                                     // also take your name
                    if (SymbolTable.Classes.ContainsKey(pname) && !(SymbolTable.Classes.ContainsKey(name))) // If you know my father, and not myself
                    {
                        SymbolTable.Classes.Add(name, new ClassNode(name, SymbolTable.Classes[pname]));     //let me introduce myself
                        //b[i] = true;
                        c++;
                        //if (c == cl.Length)
                        //    return VisitChildren(context);
                    }
                }
            }
            var s = VisitChildren(context);

            this.Root = new ProgramNode(context, s);
            return(this.Root);
        }
Exemplo n.º 3
0
        public void Parser_Class_Attr()
        {
            string text   = @"
                            class A 
                            {
                                x : Int;
                                y : Bool;
                                z : Int <- 1 << 20;                               
                            };
                            ";
            var    parser = CalcParser(text);

            CoolParser.ProgramContext tree = parser.program();
            var v = tree.children[0];

            Assert.IsNotNull(tree);
        }
        public override object VisitProgram([NotNull] CoolParser.ProgramContext context)
        {
            p = new ProgramSemantic();
            foreach (var child in context.children)
            {
                if (child is TerminalNodeImpl)
                {
                    continue;
                }
                string name = ((CoolParser.ClassdefContext)child).TYPE()[0].GetText();
                // There is an atempt of type redefinition
                if (p.Types.ContainsKey(name) && !isDummy(p.Types[name]))
                {
                    p.Errors.Add(new Error("Types can't be redefined", null));
                    return(p);
                }

                Type type = (Type)Visit(child);
                p.Types[name] = type;
            }

            // This is to remove all the types that were used in the tree but not actually defined in the cool file, they are not cool.

            foreach (var item in p.Types.TakeWhile(e => isDummy(e.Value)))
            {
                p.Types.Remove(item.Key);
            }

            foreach (var t in p.Types)
            {
                // All types not explicitly inheriting, actually are, from Object
                if (t.Value.Parent == null && t.Key != "Object")
                {
                    t.Value.Parent = p.Types["Object"];
                    p.Types["Object"].Children.Add(t.Value);
                }
            }

            //Run the AST in Program p to update the types of all the nodes
            //Another check
            Type_check typer = new Type_check(p);

            typer.VisitProgram();

            return(p);
        }
        public override Node VisitProgram([NotNull] CoolParser.ProgramContext context)
        {
            var cl = context.classdef();
            //bool[] b = new bool[s.Length];
            int c = 0;

            for (int j = 0; j < cl.Length; j++)
            {
                for (int i = 0; i < cl.Length; i++)
                {
                    /* In antlr when there are two IToken of the same type in a single rule production
                     * they provide a method theat returns an array of that kind of tokens
                     *
                     * this is an example: TYPE()=> ITerminalNode[], TYPE(int i) => ith ITerminalNode
                     */
                    var pname = "Object";
                    if (cl[i].TYPE(1) != null)           // if there is a type from wich this class inherits
                    {
                        pname = cl[i].TYPE(1).GetText(); // take it
                    }
                    //var f= s[i].TYPE();
                    var name = cl[i].TYPE(0).GetText();                                                     // also take your name
                    if (SymbolTable.Classes.ContainsKey(pname) && !(SymbolTable.Classes.ContainsKey(name))) // If you know my father, and not myself
                    {
                        SymbolTable.Classes.Add(name, new ClassNode(name, SymbolTable.Classes[pname]));     //let me introduce myself
                        //b[i] = true;
                        c++;
                        //if (c == cl.Length)
                        //    return VisitChildren(context);
                    }
                }
            }
            var s = VisitChildren(context);

            this.Root = new ProgramNode(context, s);
            return(this.Root);
        }
Exemplo n.º 6
0
        public void Parser_Calc()
        {
            string text   = @"
                            4+5*2;
                            (6*3)/7;
                            8+9;
                            9/3;
                            2*2*2*2 + 5 + 2;
                            true + false;
                            true < 2;
                            2*2*2*2 = (2+2)*(2+2);
                            true <= false;
                            not (not true + false*true + 0*(1+5+8)/9 + 1/9);
                            (0-1);
                            (0-1)*(0-1);
                            ~(0-1) ;          
                            ";
            var    parser = CalcParser(text);

            CoolParser.ProgramContext tree = parser.program();
            CoolVisitor visitor            = new CoolVisitor();

            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(0)), 14);
            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(1)), 2);
            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(2)), 17);
            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(3)), 3);
            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(4)), 2 * 2 * 2 * 2 + 5 + 2);
            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(5)), 1);
            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(6)), 1);
            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(7)), 1);
            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(8)), 0);
            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(9)), 1);
            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(10)), -1);
            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(11)), 1);
            Assert.AreEqual(visitor.Visit((tree.children[0]).GetChild(12)), 0);
        }
Exemplo n.º 7
0
 public override object VisitProgram([NotNull] CoolParser.ProgramContext context)
 {
     return(VisitChildren(context));
 }
 /// <summary>
 /// Visit a parse tree produced by <see cref="CoolParser.program"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitProgram([NotNull] CoolParser.ProgramContext context)
 {
     return(VisitChildren(context));
 }
Exemplo n.º 9
0
 /// <summary>
 /// Exit a parse tree produced by <see cref="CoolParser.program"/>.
 /// <para>The default implementation does nothing.</para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 public virtual void ExitProgram([NotNull] CoolParser.ProgramContext context)
 {
 }
Exemplo n.º 10
0
        //private Node Childs;

        public ProgramNode(CoolParser.ProgramContext context, Node s) : base(s.Childs)
        {
            this.context = context;
            //this.Childs = s;
        }