private void HandleInclude(ref int i) { StringBuilder sb = new StringBuilder(); string includeFile; if (fileContents[++i].Text == "<") { //#include <> i++; //skip first < while (fileContents[i].Text != ">") { sb.Append(fileContents[i++].Text); } includeFile = ResolveInclude(sb.ToString(), IncludeDirs.SystemIncludes); } else { //#include "" sb.Append(fileContents[i].Text.Replace("\"", String.Empty)); includeFile = ResolveInclude(sb.ToString(), IncludeDirs.LocalIncludes); } if (!string.IsNullOrEmpty(includeFile)) { string includeFileContents = Compiler.TryOpenFile(includeFile); if (!string.IsNullOrEmpty(includeFileContents)) { PreprocessorParser newParser = new PreprocessorParser(Tokenizer.Tokenize(includeFileContents)); List <Token> newPreprocess = newParser.Parse(); parsedContent.AddRange(newPreprocess); } } }
static Module DoCompile(string fileContents, OptimizeLevel opLevel = OptimizeLevel.OptimizeNone, PassCount passCount = PassCount.PassAlls) { var tokenizerTokens = Tokenizer.Tokenize(fileContents); var preprocessorParser = new PreprocessorParser(tokenizerTokens); var preProcessorTokens = preprocessorParser.Parse(); var tokenPassTokens = RunTokenPasses(preProcessorTokens); var tokens = tokenPassTokens.GetEnumerator(); tokens.MoveNext(); var currentModule = Module.ParseModule(ref tokens); if (optimizeLevel != OptimizeLevel.OptimizeNone) { Optimizer.Loop.Optimizer.RunOptimizer(ref currentModule, optimizeLevel); } WriteModule(currentModule, 1); // Statement passes if (passCount >= PassCount.Pass2) { StatementPasses.BlockCollapse.Run(currentModule); StatementPasses.IfGotoConversion.Run(currentModule); StatementPasses.LoopGotoConversion.Run(currentModule); StatementPasses.ReorderDeclarations.Run(currentModule); StatementPasses.MarkRecursiveFunctions.Run(currentModule); } WriteModule(currentModule, 2); //if (optimizeLevel != OptimizeLevel.OptimizeNone) // Optimizer.Optimizer.RunOptimizer(ref currentModule, optimizeLevel); WriteModule(currentModule, 3); if (passCount >= PassCount.Pass3) { //StatementPasses.ReplaceLocalsWithGlobals.Run(currentModule); StatementPasses.ApplyCallingConvention.Run(currentModule); StatementPasses.LabelMerger.Run(currentModule); StatementPasses.RemovePointlessGotos.Run(currentModule); // Adjust the arithmetic StatementPasses.ConvertAddSubToIncDec.Run(currentModule); StatementPasses.RemoveMathImmediates.Run(currentModule); StatementPasses.RegisterAllocator.DumbRegisterAllocator.Run(currentModule); } currentModule.IntermediateStrings.Add("#include <string.h>"); string code = currentModule.ToString(); var asmCode = AssemblyGenerator.GenerateCode(ref currentModule); return(currentModule); }
private void CheckTokenReplace(ref WabbitC.Token token) { if (token.Type == TokenType.StringType) { PreprocessorDefine define = PreprocessorParser.DefineValue(token); if (define != null && define is ReplacementDefine) { ReplacementDefine replaceDefine = (ReplacementDefine)define; token = replaceDefine.Value; CheckTokenReplace(ref token); } } }
private void HandleIfDef(ref int i, bool IsPositive) { List <Token> line = GetPreprocessorLine(ref i); Expression exp = new Expression(line); var value = new Token();// exp.Eval()[0]; int nesting = 0; List <Token> conditionalTokens = new List <Token>(); i++; if ((value.Type != TokenType.UndefinedType && IsPositive) || (value.Type != TokenType.UndefinedType && !IsPositive)) { for (; i < fileContents.Count; i++) { if (fileContents[i].Text == "#if" || fileContents[i].Text == "#ifdef") { nesting++; } if (fileContents[i].Text == "#elif" || fileContents[i].Text == "#endif" || fileContents[i].Text == "#else") { if (nesting > 0) { if (fileContents[i].Text == "#endif") { nesting--; } } else { break; } } conditionalTokens.Add(fileContents[i]); } i++; PreprocessorParser parser = new PreprocessorParser(conditionalTokens); List <Token> temp = parser.Parse(); parsedContent.AddRange(temp); } else { bool inCorrectRange = false; for (; i < fileContents.Count; i++) { Token token = fileContents[i]; if (token.Text == "#if" || token.Text == "#ifdef") { nesting++; } if (token.Text == "#endif") { if (nesting > 0) { nesting--; } else { break; } } if (token.Text == "#elif" || token.Text == "#else") { if (nesting == 0) { if (token.Text == "#else") { inCorrectRange = true; } else { } } } if (inCorrectRange) { conditionalTokens.Add(fileContents[i]); } } i++; PreprocessorParser parser = new PreprocessorParser(conditionalTokens); List <Token> temp = parser.Parse(); parsedContent.AddRange(temp); } }