Exemple #1
0
 public CILCodeGenerator(CPreBuilder preBuilder)
 {
     _fileName        = preBuilder.FileName;
     _programName     = preBuilder.ProgramName;
     _programFileName = preBuilder.ProgramFileName;
     _hasEntryPoint   = preBuilder.HasEntryPoint;
     _compilationUnit = preBuilder.CompilationUnit;
 }
        public CPreBuilder(
            string fileName,
            CParser.CompilationUnitContext compilationUnit)
        {
            FileName    = fileName;
            ProgramName = Path.GetFileNameWithoutExtension(FileName).First().ToString().ToUpper()
                          + Path.GetFileNameWithoutExtension(FileName).Substring(1);
            CompilationUnit = compilationUnit;
            HasEntryPoint   = false;

            PrepareToCompile();
        }
        public string Compile(ICharStream cSource)
        {
            CLexer            lexer  = new CLexer(cSource);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            CParser           parser = new CParser(tokens);

            parser.RemoveErrorListeners();

            parser.AddErrorListener(this);

            try
            {
                CParser.CompilationUnitContext compilationUnit = parser.compilationUnit();
                ParseTreeWalker walker = new ParseTreeWalker();

                walker.Walk(this, compilationUnit);

                string idents = "";

                foreach (CIdentifier ident in CIdentifier.FileScopeIdentifiers)
                {
                    if (ident.Name.Equals("main"))
                    {
                        idents = ident.Emit() + idents;
                    }
                    else
                    {
                        idents += ident.Emit();
                    }
                }

                return(idents);
            }
            catch (SemanticException e)
            {
                m_outStream.WriteLine("Compilation Failed: " + e.Message);
                return(null);
            }
        }
Exemple #4
0
        public IFile ParseFile()
        {
            using (var def = Lifetime.Define())
            {
                var builder = new PsiBuilder(new CILexer(myLexer.Buffer), SpringFileNodeType.Instance, new TokenFactory(), def.Lifetime);

                string  inputText   = myLexer.Buffer.GetText();
                var     tokenStream = new CommonTokenStream(new CLexer(new AntlrInputStream(inputText)));
                CParser cParser     = new CParser(tokenStream);
                cParser.AddErrorListener(new ParserErrorListener(builder));
                var visitor = new BuilderVisitor(builder);

                var fileMark = builder.Mark();

                CParser.CompilationUnitContext fileContext = cParser.compilationUnit();
                visitor.Visit(fileContext);

                builder.Done(fileMark, SpringFileNodeType.Instance, null);
                var file = (IFile)builder.BuildTree();
                return(file);
            }
        }