public void MethodDefWithArguments()
        {
            String contents =
                "class MyClass \r\n" +
                "" +
                " def self.method1(x, y, z: int) : int" +
                " end " +
                "" +
                "end \r\n";

            CompilationUnitNode unit = RookParser.ParseContents(contents);

            Assert.IsNotNull(unit);
            Assert.AreEqual(0, unit.Namespaces.Count);
            Assert.AreEqual(1, unit.ClassesTypes.Count);

            ClassDefinitionStatement ClassDefinitionStatement = unit.ClassesTypes[0] as ClassDefinitionStatement;

            Assert.IsNotNull(ClassDefinitionStatement);
            Assert.AreEqual("MyClass", ClassDefinitionStatement.Name);
            Assert.AreEqual(0, ClassDefinitionStatement.BaseTypes.Count);
            Assert.AreEqual(1, ClassDefinitionStatement.Statements.Count);

            MethodDefinitionStatement stmt = ClassDefinitionStatement.Statements[0] as MethodDefinitionStatement;

            Assert.IsNotNull(stmt);
        }
        public void NestedDefinitionOfInstanceVars()
        {
            String contents =
                "                 \r\n" +
                "def some()       \r\n" +
                "  @x:int = 1     \r\n" +
                "end              \r\n" +
                "                 \r\n" +
                "def some2()      \r\n" +
                "  @y += 2        \r\n" +
                "end              \r\n" +
                "                 \r\n" +
                "@y:int = 1       \r\n" +
                "";

            SourceUnit unit = container.ParserService.Parse(contents);

            AssertNoErrorOrWarnings();

            Assert.IsNotNull(unit);
            Assert.AreEqual(3, unit.Statements.Count);

            DeclarationBinding sb = container[typeof(DeclarationBinding)] as DeclarationBinding;

            sb.ExecutePass(unit.CompilationUnit);

            Assert.IsNotNull(unit);
            Assert.AreEqual(4, unit.Statements.Count);

            AssertNoErrorOrWarnings();

            MethodDefinitionStatement          m1Stmt   = unit.Statements[0] as MethodDefinitionStatement;
            MethodDefinitionStatement          m2Stmt   = unit.Statements[1] as MethodDefinitionStatement;
            SingleVariableDeclarationStatement varDecl1 = unit.Statements[2] as SingleVariableDeclarationStatement;
            SingleVariableDeclarationStatement varDecl2 = unit.Statements[3] as SingleVariableDeclarationStatement;

            Assert.IsNotNull(m1Stmt);
            Assert.IsNotNull(m2Stmt);
            Assert.IsNotNull(varDecl1);
            Assert.IsNotNull(varDecl2);

            Assert.AreEqual("some", m1Stmt.FullName);
            ExpressionStatement  expStmt   = m1Stmt.Statements[0] as ExpressionStatement;
            AssignmentExpression assignExp = expStmt.Expression as AssignmentExpression;

            Assert.AreEqual("@x", (assignExp.Target as VariableReferenceExpression).Identifier.Name);
            Assert.AreEqual("1", (assignExp.Value as LiteralReferenceExpression).Content);

            Assert.AreEqual("some2", m2Stmt.FullName);
            expStmt = m2Stmt.Statements[0] as ExpressionStatement;
            AugAssignmentExpression augExp = expStmt.Expression as AugAssignmentExpression;

            Assert.AreEqual("@y", (augExp.Target as VariableReferenceExpression).Identifier.Name);
            Assert.AreEqual("2", (augExp.Value as LiteralReferenceExpression).Content);
        }
        private void DeclareAndPopulateGlobalSourceUnit(CompilationUnit unit)
        {
            SourceUnit globalUnit = new SourceUnit(unit, "<global>");

            TypeDefinitionStatement globalType = new TypeDefinitionStatement(AccessLevel.Public, "RookGlobal");

            globalUnit.Statements.Add(globalType);

            MethodDefinitionStatement entryPoint = new MethodDefinitionStatement(AccessLevel.Public);

            entryPoint.Name = "self.main";

            foreach (SourceUnit sunit in unit.SourceUnits)
            {
                foreach (IStatement stmt in sunit.Statements)
                {
                    if (stmt.StatementType == StatementType.TypeDefinition)
                    {
                        continue;
                    }

                    stmt.Parent.RemoveChild(stmt);

                    if (stmt.StatementType == StatementType.MethodDef)
                    {
                        (stmt as MethodDefinitionStatement).IsStatic = true;
                    }
                    if (stmt.StatementType == StatementType.MultipleVarDeclaration)
                    {
                        // TODO: Ensure no instance vars defined
                    }

                    if (stmt.StatementType == StatementType.ExpressionStmt ||
                        stmt.StatementType == StatementType.MultipleVarDeclaration)
                    {
                        entryPoint.Statements.Add(stmt);
                    }
                    else
                    {
                        globalType.Statements.Add(stmt);
                    }
                }
            }

            if (entryPoint.Statements.Count != 0)
            {
                globalType.Statements.Add(entryPoint);

                // Might be necessary
                // unit.EntryPointMethod = entryPoint;
            }

            unit.SourceUnits.Add(globalUnit);
        }
Ejemplo n.º 4
0
        public virtual bool VisitMethodDefinitionStatement(MethodDefinitionStatement methodDef)
        {
            if (VisitEnter(methodDef))
            {
                VisitNodes(methodDef.Arguments);
                VisitNodes(methodDef.Statements);

                return(VisitLeave(methodDef));
            }

            return(false);
        }
Ejemplo n.º 5
0
        public override bool VisitMethodDefinitionStatement(MethodDefinitionStatement methodDef)
        {
            nodeStack.Push(CurrentNode.Nodes.Add("Method " + methodDef.Name + "[ret " + methodDef.ReturnType + "]"));
            CurrentNode.Tag = methodDef;

            CurrentNode.EnsureVisible();

            base.VisitMethodDefinitionStatement(methodDef);

            nodeStack.Pop();

            return(true);
        }
 public void AddMethod(MethodDefinitionStatement methodDef)
 {
     if (methodDef.IsConstructor)
     {
         AddConstructor(methodDef);
     }
     else if (methodDef.IsStatic)
     {
         AddStaticMethod(methodDef);
     }
     else
     {
         AddInstanceMethod(methodDef);
     }
 }
        public void AssignConvertedToDeclaration2()
        {
            String contents =
                "                 \r\n" +
                "def some()       \r\n" +
                "  @x = 1         \r\n" +
                "end              \r\n" +
                "                 \r\n" +
                "";

            SourceUnit unit = container.ParserService.Parse(contents);

            AssertNoErrorOrWarnings();

            Assert.IsNotNull(unit);
            Assert.AreEqual(1, unit.Statements.Count);

            DeclarationBinding sb = container[typeof(DeclarationBinding)] as DeclarationBinding;

            sb.ExecutePass(unit.CompilationUnit);

            AssertNoErrorOrWarnings();

            Assert.AreEqual(2, unit.Statements.Count);

            MethodDefinitionStatement          m1Stmt   = unit.Statements[0] as MethodDefinitionStatement;
            SingleVariableDeclarationStatement varDecl1 = unit.Statements[1] as SingleVariableDeclarationStatement;
            ExpressionStatement assignStmt = m1Stmt.Statements[0] as ExpressionStatement;

            Assert.IsNotNull(m1Stmt);
            Assert.IsNotNull(varDecl1);
            Assert.IsNotNull(assignStmt);

            AssignmentExpression assignExp = assignStmt.Expression as AssignmentExpression;

            Assert.AreEqual("@x", (assignExp.Target as VariableReferenceExpression).Identifier.Name);
            Assert.AreEqual("1", (assignExp.Value as LiteralReferenceExpression).Content);

            Assert.AreEqual("@x", varDecl1.Identifier.Name);
        }
        public void AssignmentsAndMethods()
        {
            String contents =
                "class MyClass \r\n" +
                "private \r\n" +
                "  @@myfield = 1 \r\n" +
                "   \r\n" +
                "  def method1() \r\n" +
                "  end \r\n" +
                "   \r\n" +
                "  def self.method2() \r\n" +
                "  end \r\n" +
                "end \r\n";

            CompilationUnitNode unit = RookParser.ParseContents(contents);

            Assert.IsNotNull(unit);
            Assert.AreEqual(0, unit.Namespaces.Count);
            Assert.AreEqual(1, unit.ClassesTypes.Count);

            ClassDefinitionStatement ClassDefinitionStatement = unit.ClassesTypes[0] as ClassDefinitionStatement;

            Assert.IsNotNull(ClassDefinitionStatement);
            Assert.AreEqual("MyClass", ClassDefinitionStatement.Name);
            Assert.AreEqual(0, ClassDefinitionStatement.BaseTypes.Count);
            Assert.AreEqual(3, ClassDefinitionStatement.Statements.Count);

            FieldDeclarationStatement assignStmt  = ClassDefinitionStatement.Statements[0] as FieldDeclarationStatement;
            MethodDefinitionStatement method1Stmt = ClassDefinitionStatement.Statements[1] as MethodDefinitionStatement;
            MethodDefinitionStatement method2Stmt = ClassDefinitionStatement.Statements[2] as MethodDefinitionStatement;

            Assert.IsNotNull(assignStmt);
            Assert.IsNotNull(method1Stmt);
            Assert.IsNotNull(method2Stmt);

            Assert.AreEqual("method1", method1Stmt.Name);
            Assert.AreEqual("method2", method2Stmt.Name);
            Assert.AreEqual("self", method2Stmt.BoundTo);
        }
Ejemplo n.º 9
0
 public void DefineMethod(MethodDefinitionStatement methodDefinition)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 10
0
 public virtual bool VisitLeave(MethodDefinitionStatement methodDef)
 {
     return(true);
 }
Ejemplo n.º 11
0
 public override bool VisitEnter(MethodDefinitionStatement methodDef)
 {
     return(false);
 }
 private void AddInstanceMethod(MethodDefinitionStatement def)
 {
     methods.Add(def.Name, def);
 }
Ejemplo n.º 13
0
        public override bool VisitMethodDefinitionStatement(MethodDefinitionStatement methodDef)
        {
            methodDef.DefiningSymbolTable.Parent.AddMethod(methodDef);

            return(base.VisitMethodDefinitionStatement(methodDef));
        }
 private void AddConstructor(MethodDefinitionStatement def)
 {
     constructors.Add(def);
 }
 private void AddStaticMethod(MethodDefinitionStatement def)
 {
     smethods.Add(def.Name, def);
 }
        public void AssignConvertedToDeclaration4()
        {
            String contents =
                "namespace Family::Guy					\r\n"+
                "  										\r\n"+
                "  class Boat							\r\n"+
                "										\r\n"+
                "    def initialize						\r\n"+
                "      @x = 1							\r\n"+
                "    end    							\r\n"+
                "										\r\n"+
                "  end									\r\n"+
                "										\r\n"+
                "  class Song							\r\n"+
                "										\r\n"+
                "    def save							\r\n"+
                "      @name = 1						\r\n"+
                "    end								\r\n"+
                " 										\r\n"+
                "  end 									\r\n"+
                "   									\r\n"+
                "end									\r\n"+
                "";

            SourceUnit unit = container.ParserService.Parse(contents);

            AssertNoErrorOrWarnings();

            Assert.IsNotNull(unit);

            DeclarationBinding sb = container[typeof(DeclarationBinding)] as DeclarationBinding;

            sb.ExecutePass(unit.CompilationUnit);

            AssertNoErrorOrWarnings();

            Assert.AreEqual(1, unit.Namespaces.Count);

            NamespaceDeclaration nsDecl = unit.Namespaces[0] as NamespaceDeclaration;

            Assert.AreEqual(2, nsDecl.TypeDeclarations.Count);

            TypeDefinitionStatement boatType = nsDecl.TypeDeclarations[0] as TypeDefinitionStatement;
            TypeDefinitionStatement songType = nsDecl.TypeDeclarations[1] as TypeDefinitionStatement;

            Assert.AreEqual(2, boatType.Statements.Count);
            Assert.AreEqual(2, songType.Statements.Count);

            MethodDefinitionStatement          initMethod  = boatType.Statements[0] as MethodDefinitionStatement;
            SingleVariableDeclarationStatement boatVarDecl = boatType.Statements[1] as SingleVariableDeclarationStatement;

            MethodDefinitionStatement          saveMethod  = songType.Statements[0] as MethodDefinitionStatement;
            SingleVariableDeclarationStatement songVarDecl = songType.Statements[1] as SingleVariableDeclarationStatement;

            Assert.AreEqual(1, initMethod.Statements.Count);
            Assert.AreEqual(1, saveMethod.Statements.Count);

            ExpressionStatement assignStmt1 = initMethod.Statements[0] as ExpressionStatement;
            ExpressionStatement assignStmt2 = saveMethod.Statements[0] as ExpressionStatement;

            Assert.IsNotNull(assignStmt1);
            Assert.IsNotNull(assignStmt2);

            AssignmentExpression assignExp1 = assignStmt1.Expression as AssignmentExpression;
            AssignmentExpression assignExp2 = assignStmt2.Expression as AssignmentExpression;

            Assert.IsNotNull(assignExp1);
            Assert.IsNotNull(assignExp2);

            Assert.AreEqual("@x", (assignExp1.Target as VariableReferenceExpression).Identifier.Name);
            Assert.AreEqual("1", (assignExp1.Value as LiteralReferenceExpression).Content);

            Assert.AreEqual("@name", (assignExp2.Target as VariableReferenceExpression).Identifier.Name);
            Assert.AreEqual("1", (assignExp2.Value as LiteralReferenceExpression).Content);
        }
        public override bool VisitMethodDefinitionStatement(MethodDefinitionStatement methodDef)
        {
            visitedNodes.Add(methodDef);

            return(base.VisitMethodDefinitionStatement(methodDef));
        }
Ejemplo n.º 18
0
 public override bool VisitEnter(MethodDefinitionStatement methodDef)
 {
     nowQueue = true;
     return(base.VisitEnter(methodDef));
 }
Ejemplo n.º 19
0
 public override bool VisitLeave(MethodDefinitionStatement methodDef)
 {
     nowQueue = false;
     return(base.VisitLeave(methodDef));
 }