コード例 #1
0
        private static void LogOneProgramPartToConsole([NotNull] ProgramPart part, int level)
        {
            var padding = "";

            for (var i = 0; i < level; i++)
            {
                padding += "  ";
            }

            Logger.Info(padding + part.Key + "\t" + part.Duration.TotalSeconds);
            foreach (var child in part.Children)
            {
                LogOneProgramPartToConsole(child, level + 1);
            }
        }
コード例 #2
0
ファイル: PsiNodeParser.cs プロジェクト: hunpody/psimulex
        public void Parse(string source, string sourceFileName, CompilerDTO dto, ProgramPart part)
        {
            // ANTLR Compile
            var result = ANTLRCompiler.Compile(source, sourceFileName, part);

            // Simple program can not be imported
            if (dto.CompilationUnitList.Count != 0 &&
                result.PsiNode != null &&
                result.PsiNode.Left != null &&
                result.PsiNode.Left.GetType() == typeof(SimpleProgramNode))
            {
                dto.CompilerMessages.Warnings.Add(new Warning
                    {
                        Interval = new Interval() { FileName = sourceFileName },
                        MessageText = string.Format(
                        "Simple programs can not be imported! Compilation unit will be skipped!")
                    });
                return;
            }

            // Create CompilationUnit
            dto.CompilationUnitList.Add(
                new CompilationUnit
                {
                    Source = source,
                    CleanedSourceText = source,
                    FileName = sourceFileName,
                    PsiNodeSyntaxTree = result.PsiNode
                });

            // Generate messages
            if (result.ANTLRExceptionText != "" || result.ANTLRErrorMessages.Count != 0)
            {
                result.ANTLRErrorMessages.ForEach(x =>
                    dto.CompilerMessages.AntlrErrors.Add(new AntlrError
                    {
                        Interval = new Interval() { FileName = sourceFileName },
                        MessageText = x
                    }));

                if (result.ANTLRExceptionText != "")
                    dto.CompilerMessages.AntlrErrors.Add(new AntlrError
                    {
                        Interval = new Interval(),
                        MessageText = result.ANTLRExceptionText
                    });
            }
        }
コード例 #3
0
        public void StartPart([NotNull] string key)
        {
            if (Current == null)
            {
                throw new FlaException("Current was null");
            }

            if (Current.Key == key)
            {
                throw new FlaException("The current key is already " + key + ". Copy&Paste error?");
            }

            var newCurrent = new ProgramPart(Current, key);

            Current.Children.Add(newCurrent);
            Current = newCurrent;
        }
コード例 #4
0
ファイル: ANTLRCompiler.cs プロジェクト: hunpody/psimulex
        /// <summary>
        /// Compile a program part
        /// </summary>
        /// <param name="source">The source code</param>
        /// <param name="fileName">The filename of the source file</param>        
        /// <param name="part">Part of a program</param>
        /// <returns>The result</returns>
        public static ANTLRCompileResult Compile(string source, string fileName, ProgramPart part)
        {
            string antlrExceptionText = "";
            IPsiNode psiNode = null;
            List<string> antlrErrorMessages = new List<string>();

            try
            {
                // Create parser
                var parser = CreateParser(source);

                ParserRuleReturnScope tree = null;

                // Parse a program part
                switch (part)
                {
                    case ProgramPart.CompilationUnit:
                        tree = parser.compilationUnit();
                        break;
                    case ProgramPart.Statement:
                        tree = parser.statement();
                        break;
                    default:
                        tree = parser.compilationUnit();
                        break;
                }

                // Save Antlr Messages
                antlrErrorMessages = parser.ErrorMessages;

                // Convert
                psiNode = ConvertToPsiNode((CommonTree)tree.Tree);
            }
            catch (Exception ex)
            {
                antlrExceptionText = ex.ToString();
            }

            return new ANTLRCompileResult
            {
                ANTLRErrorMessages = antlrErrorMessages,
                PsiNode = psiNode,
                ANTLRExceptionText = antlrExceptionText
            };
        }
コード例 #5
0
        public void StartPart(string key)
        {
            //Logger.Info("Starting "+ key);
            if (Current == null)
            {
                throw new LPGException("Current was null");
            }

            if (Current.Key == key)
            {
                throw new LPGException("The current key is already " + key + ". Copy&Paste error?");
            }

            var newCurrent = new ProgramPart(Current, key);

            Current.Children.Add(newCurrent);
            Current = newCurrent;
        }
コード例 #6
0
ファイル: Compiler.cs プロジェクト: hunpody/psimulex
        public CompileResult Compile(string source, string sourceFileName, bool compileFuncVarTree, ProgramPart part)
        {
            CompileResult = new CompileResult();

            string fullPath = Path.GetFullPath(sourceFileName);
            sourceFileName = Path.GetFileName(fullPath);

            // Data Transfer Object
            var dto = new CompilerDTO { ProgramPath = Path.GetDirectoryName(fullPath) };

            // Resolve Imports
            ResolveImports(source, sourceFileName, dto, part);

            // Finalize TypeIdentifiers
            FinalizeTypeIdentifiers(dto);

            // Analise and Compile all source file.
            foreach (var cu in dto.CompilationUnitList)
            {
                if (dto.CompilerMessages.AntlrErrors.Find(x => x.Interval.FileName == cu.FileName) == null)
                {
                    // Semantic analisis
                    Analise(cu, dto, part);

                    // Code generation
                    GenerateMicrolexCode(cu, dto, part);
                }
            }

            // Function Variable tree builder.
            if (compileFuncVarTree && part == ProgramPart.CompilationUnit)
                CompileResult.PsiFunctionsVariablesNodeList = GenerateFuncVarTree(dto.CompilationUnitList);

            // Finalise the overall result (CompilerDTO -> CompilerResult)
            FinaliseTheResult(dto);

            return CompileResult;
        }
コード例 #7
0
        public void StopPart(string key)
        {
            if (Current == null)
            {
                throw new LPGException("Current was null");
            }

            if (Current.Key != key)
            {
                throw new LPGException("Mismatched key: Current: " + Current.Key + " trying to stop: " + key);
            }

            if (Current == MainPart)
            {
                throw new LPGException("Trying to stop the main");
            }

            Current.Stop = DateTime.Now;

            Logger.Info("Finished " + key + " after " + Current.Duration.ToString());

            Current = Current.Parent;
        }
コード例 #8
0
        public void StopPart([NotNull] string key)
        {
            if (Current == null)
            {
                throw new FlaException("Current was null");
            }

            if (Current.Key != key)
            {
                throw new FlaException("Mismatched key: Current: " + Current.Key + " trying to stop: " + key);
            }

            if (Current == MainPart)
            {
                throw new FlaException("Trying to stop the main");
            }

            Current.Stop = DateTime.Now;

            Console.WriteLine("Finished " + key + " after " + Current.Duration.ToString());

            Current = Current.Parent;
        }
コード例 #9
0
ファイル: Compiler.cs プロジェクト: hunpody/psimulex
        public void ResolveImports(string source, string sourceFileName, CompilerDTO dto, ProgramPart part)
        {
            // Parse
            try
            {
                Parser.Parse(source, sourceFileName, dto, part);
            }
            catch (Exception ex)
            {
                dto.CompilerMessages.AntlrErrors.Add(
                    new AntlrError
                    {
                        Interval = new Interval()
                        { FileName = sourceFileName },
                        MessageText = "Parser Error!"
                    });
            }

            if (part == ProgramPart.CompilationUnit)
            {
                // Resolve Imports
                var resolver = new PsiImportResolverVisitor(dto.CompilationUnitList.Last<CompilationUnit>(), dto, this);
                var ast = dto.CompilationUnitList.Last<CompilationUnit>().PsiNodeSyntaxTree as CompilationUnitNode;
                if (ast != null)
                    resolver.Visit(ast);
            }
        }
コード例 #10
0
ファイル: Compiler.cs プロジェクト: hunpody/psimulex
        private void GenerateMicrolexCode(CompilationUnit cu, CompilerDTO dto, ProgramPart part)
        {
            var visitor = new PsiCodeGeneratorVisitor(cu, dto);

            try
            {
                switch (part)
                {
                    case ProgramPart.CompilationUnit:
                        visitor.Visit(cu.PsiNodeSyntaxTree as CompilationUnitNode);
                        break;
                    case ProgramPart.Statement:
                        var node = new StatementNode();
                        node.Init();
                        node.Type = NodeType.Statement;

                        node.Add(cu.PsiNodeSyntaxTree);
                        visitor.Visit(node);
                        break;
                    default:
                        visitor.Visit(cu.PsiNodeSyntaxTree as CompilationUnitNode);
                        break;
                }

                //visitor.CurrentCompilationUnit.PsiNodeSyntaxTree = result.PsiNode as CompilationUnitNode;

            }
            catch (Exception e)
            {
                dto.CompilerMessages.Errors.Add(new Error
                {
                    MessageText = e.ToString(),
                    Interval = new Interval { FileName = cu.FileName }
                });
            }
        }
コード例 #11
0
ファイル: Compiler.cs プロジェクト: hunpody/psimulex
 private void Analise(CompilationUnit cu, CompilerDTO dto, ProgramPart part)
 {
     if (part == ProgramPart.CompilationUnit)
         new PsiSemanticAnaliserVisitor(cu, dto).Visit(cu.PsiNodeSyntaxTree as CompilationUnitNode);
     //else What To Do ?
 }
コード例 #12
0
 public ProgramPart([CanBeNull] ProgramPart parent, [NotNull] string key)
 {
     Parent = parent;
     Key    = key;
     Start  = DateTime.Now;
 }
コード例 #13
0
 public CalculationProfiler()
 {
     MainPart = new ProgramPart(null, "Main");
     Current  = MainPart;
 }