示例#1
0
        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");
        }
    }
示例#2
0
 public HookableScope(HookableScope parent) : base(parent)
 {
 }