void CheckLeaveSamples() { var samples = Directory.GetFiles(SampleDirectory); Assert.NotEmpty(samples); foreach (var sampleFile in samples) { var sourceCode = File.ReadAllText(sampleFile); var isBadSample = sourceCode.Contains("<bad>"); var ast = GetAst(sourceCode); var rootScope = new SymbolScope(); var generator = new IntermediateCodeGenerator(rootScope); generator.ReturnCheckEnabled = true;// enable leave check if (isBadSample) { Assert.Throws <SemanticException>(() => generator.ProcessProgram(ast)); } else { generator.ProcessProgram(ast); } } TestOutput.WriteLine($"{samples.Length} samples tested"); }
private static List <IntermediateCode> GenerateIntermediateCode(ICodeUnit ast) { var generator = new IntermediateCodeGenerator(); var codes = generator.Generate(ast).ToList(); return(codes); }
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()); }
void JustNoException() { var samples = Directory.GetFiles(SampleDirectory, "*.c0"); Assert.NotEmpty(samples); foreach (var sampleFile in samples) { var sourceCode = File.ReadAllText(sampleFile); var ast = GetAst(sourceCode); var rootScope = new SymbolScope(); var generator = new IntermediateCodeGenerator(rootScope); generator.CodeGenerationEnabled = true; generator.ReturnCheckEnabled = true; generator.ProcessProgram(ast); // Scope are pushed and poped corrctly Assert.Equal(rootScope, generator.SymbolScope); // Generate assembly code var asm = new AssemblyCodeGenerator(generator.GlobalBuilder); var codeLines = asm.Generate(); var outputName = sampleFile + ".s"; File.WriteAllLines(outputName, codeLines); // Write machine code var exeData = asm.Executable.GetData(); var exeName = sampleFile.Replace(".c0", ".o0"); File.WriteAllBytes(exeName, exeData); } TestOutput.WriteLine($"{samples.Length} samples tested"); }
static List <Instruction> Generate(string sourceCode) { var lexer = new Lexer(sourceCode); var tokens = lexer.Parse().ToList(); var tokenReader = new TokenReader(tokens); var syntacticAnalyzer = new SyntacticAnalyzer(tokenReader); var success = syntacticAnalyzer.TryOperatorExpression(out OperatorExpressionAst ast); Assert.True(success); Assert.True(tokenReader.ReachedEnd); var rootScope = new SymbolScope(); var generator = new IntermediateCodeGenerator(rootScope); generator.CodeGenerationEnabled = true; generator.ReturnCheckEnabled = true; generator.ProcessOperatorExpression(ast); return(generator.ExpressionBucket.Pop()); }
void CheckAllSamples() { var samples = Directory.GetFiles(SampleDirectory); Assert.NotEmpty(samples); foreach (var sampleFile in samples) { var sourceCode = File.ReadAllText(sampleFile); var sourceDeclStat = GetDeclarationStatisticsFromSource(sourceCode); var isBadSample = sourceCode.Contains("<bad>"); var ast = GetAst(sourceCode); var rootScope = new HookableScope(); var actualStat = new List <(string, string)>(); rootScope.HookFunction = symbol => { if (symbol is FunctionSymbol function) { actualStat.Add((function.Name, "fn")); } else if (symbol is VariableSymbol variable) { var typeString = variable.Type switch { DataType.Long => "int", DataType.Double => "double", _ => throw new Exception() }; if (!variable.IsConstant) { actualStat.Add((variable.Name, typeString)); } else { actualStat.Add((variable.Name, "const " + typeString)); } } }; var generator = new IntermediateCodeGenerator(rootScope); if (isBadSample) { Assert.Throws <SemanticException>(() => generator.ProcessProgram(ast)); } else { generator.ProcessProgram(ast); // Scope are pushed and poped corrctly Assert.Equal(rootScope, generator.SymbolScope); // Check the declaration stat Assert.Equal(sourceDeclStat.Count, actualStat.Count); for (int i = 0; i < actualStat.Count; i++) { Assert.Equal(sourceDeclStat[i], actualStat[i]); } } } TestOutput.WriteLine($"{samples.Length} samples tested"); } }