Example #1
0
        private static List <IntermediateCode> GenerateIntermediateCode(ICodeUnit ast)
        {
            var generator = new IntermediateCodeGenerator();
            var codes     = generator.Generate(ast).ToList();

            return(codes);
        }
Example #2
0
        public SymbolTable CreateChildScope(ICodeUnit scopeOwner)
        {
            var scope = new SymbolTable(scopeOwner, this);

            children.Add(scope);
            return(scope);
        }
Example #3
0
        private SymbolTable GetScope(ICodeUnit ast)
        {
            var scanner = new ScopeScanner();

            ast.Accept(scanner);
            return(scanner.SymbolTable);
        }
        /// <summary>
        /// Determines whether the given word is the name of a local variable.
        /// </summary>
        /// <param name="word">
        /// The name to check.
        /// </param>
        /// <param name="item">
        /// The token containing the word.
        /// </param>
        /// <param name="parent">
        /// The code unit that the word appears in.
        /// </param>
        /// <returns>
        /// True if the word is the name of a local variable, false if not.
        /// </returns>
        private static bool IsLocalMember(string word, CsToken item, ICodeUnit parent)
        {
            Param.AssertValidString(word, "word");
            Param.AssertNotNull(item, "item");
            Param.AssertNotNull(parent, "parent");

            while (parent != null)
            {
                // Check to see if the name matches a local variable.
                if (ContainsVariable(parent.Variables, word, item))
                {
                    return(true);
                }

                // If the parent is an element, do not look any higher up the stack than this.
                if (parent.CodePartType == CodePartType.Element)
                {
                    break;
                }

                // Check to see whether the variable is defined within the parent.
                parent = parent.Parent as ICodeUnit;
            }

            return(false);
        }
        private static void Assert(ICodeUnit ast, int expectedRefCount)
        {
            var sut = new ReferenceScanner();

            ast.Accept(sut);

            sut.References.Count.Should().Be(expectedRefCount);
        }
 /// <summary>
 /// Gets example summary text for constructor.
 /// </summary>
 public static string GetExampleSummaryTextForConstructor(SourceAnalyzer customDocumentationAnalyzer, ICodeUnit constructor)
 {
     string type = (constructor.Parent is Struct) ? "struct" : "class";
     return (string)typeof(DocumentationRules).InvokeMember(
         "GetExampleSummaryTextForConstructorType",
         BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
         null,
         customDocumentationAnalyzer,
         new object[] { constructor, type });
 }
Example #7
0
        public void AssignNames(ICodeUnit unit)
        {
            var scanner = new ReferenceScanner();

            unit.Accept(scanner);

            var references = scanner.References.Distinct();

            AssignIdentifiersToImplicityReferences(references);
        }
Example #8
0
        private static IReadOnlyCollection <IntermediateCode> Generate(ICodeUnit unit)
        {
            var sut    = new IntermediateCodeGenerator();
            var actual = sut.Generate(unit);
            var namer  = new ImplicitReferenceNameAssigner();

            namer.AssignNames(unit);

            return(actual.ToList().AsReadOnly());
        }
        public IEnumerable <IntermediateCode> Generate(ICodeUnit codeUnit)
        {
            implicitReferenceCount = 0;
            labelCount             = 0;

            var codeGeneratingVisitor = new CodeUnitGeneratingVisitor();

            codeUnit.Accept(codeGeneratingVisitor);

            var code = codeGeneratingVisitor.Code;

            AssignNames(code.ToList());

            return(codeGeneratingVisitor.Code);
        }
        private static void AssertScope(ICodeUnit expressionNode, SymbolTable expectedScope)
        {
            var sut = new ScopeScanner();

            var nameAssigner = new ImplicitReferenceNameAssigner();

            nameAssigner.AssignNames(expressionNode);

            expressionNode.Accept(sut);

            var actual = sut.SymbolTable;

            actual.WithDeepEqual(expectedScope)
            .IgnoreProperty(r => r.Name == "Parent")
            .Assert();
        }
        /// <summary>
        /// Determines whether the given word is the name of a local variable.
        /// </summary>
        /// <param name="word">
        /// The name to check. 
        /// </param>
        /// <param name="item">
        /// The token containing the word. 
        /// </param>
        /// <param name="parent">
        /// The code unit that the word appears in. 
        /// </param>
        /// <returns>
        /// True if the word is the name of a local variable, false if not. 
        /// </returns>
        private static bool IsLocalMember(string word, CsToken item, ICodeUnit parent)
        {
            Param.AssertValidString(word, "word");
            Param.AssertNotNull(item, "item");
            Param.AssertNotNull(parent, "parent");

            while (parent != null)
            {
                // Check to see if the name matches a local variable.
                if (ContainsVariable(parent.Variables, word, item))
                {
                    return true;
                }

                // If the parent is an element, do not look any higher up the stack than this.
                if (parent.CodePartType == CodePartType.Element)
                {
                    break;
                }

                // Check to see whether the variable is defined within the parent.
                parent = parent.Parent as ICodeUnit;
            }

            return false;
        }
Example #12
0
 private void PushScope(ICodeUnit scopeOwner)
 {
     CurrentSymbolTable = CurrentSymbolTable.CreateChildScope(scopeOwner);
 }
        private void Save(ICodeUnit codeUnit, SaveICodeUnit saveICodeUnit)
        {
            if (saveICodeUnit.HasFlag(SaveICodeUnit.IfNotEmpty))
            {
                if (!saveICodeUnit.HasFlag(SaveICodeUnit.Expressions) && codeUnit.ChildStatements.Count == 0)
                {
                    return;
                }
                else if (saveICodeUnit.HasFlag(SaveICodeUnit.Expressions) && codeUnit.ChildExpressions.Count == 0)
                {
                    return;
                }
            }

            this.SetMarkBeginOfBlock();

            this.cppWriter.WriteLine();

            if (!saveICodeUnit.HasFlag(SaveICodeUnit.NoBrackets))
            {
                this.cppWriter.WriteLine("{");
            }

            if (!saveICodeUnit.HasFlag(SaveICodeUnit.Expressions))
            {
                this.Save(codeUnit.ChildStatements);
            }
            else
            {
                this.Save(codeUnit.ChildExpressions);
            }

            if (!saveICodeUnit.HasFlag(SaveICodeUnit.NoBrackets))
            {
                if (!saveICodeUnit.HasFlag(SaveICodeUnit.NoNewLine))
                {
                    this.cppWriter.WriteLine("}");
                }
                else
                {
                    this.cppWriter.Write("}");
                }
            }

            this.SetMarkEndOfBlock();
        }
Example #14
0
 void IWriteableCodeUnit.SetParent(ICodeUnit parentCodeUnit)
 {
     this.parent = parentCodeUnit;
 }
Example #15
0
 public SymbolTable(ICodeUnit owner, SymbolTable parent)
 {
     Owner  = owner ?? throw new ArgumentNullException(nameof(owner));
     Parent = parent;
 }
Example #16
0
        private static void GiveNameToImplicitReferences(ICodeUnit ast)
        {
            var nameAssigner = new ImplicitReferenceNameAssigner();

            nameAssigner.AssignNames(ast);
        }
 /// <summary>
 /// The save.
 /// </summary>
 /// <param name="codeUnit">
 /// The code unit.
 /// </param>
 private void Save(ICodeUnit codeUnit)
 {
     this.Save(codeUnit, SaveICodeUnit.Statements);
 }
Example #18
0
        /// <summary>
        /// Gets example summary text for constructor.
        /// </summary>
        public static string GetExampleSummaryTextForConstructor(SourceAnalyzer customDocumentationAnalyzer, ICodeUnit constructor)
        {
            string type = (constructor.Parent is Struct) ? "struct" : "class";

            return((string)typeof(DocumentationRules).InvokeMember(
                       "GetExampleSummaryTextForConstructorType",
                       BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
                       null,
                       customDocumentationAnalyzer,
                       new object[] { constructor, type }));
        }