/// <summary>
        /// Constructs a Code Node that has generated asm commands that deallocates registers (if possible) after given statement has been already constructed.
        /// </summary>
        /// <param name="aastNode">Already constructed statement.</param>
        /// <param name="parent">Parent Code Node</param>
        /// <param name="dependOnStatementNum">Whether we deallocate only those variables that are never appear after given statement
        /// or just to deallocate everything what has been allocated (in current context branch).</param>
        /// <returns>Constructed Code Node with asm commands representing register deallocation.</returns>
        protected CodeNode GetRegisterDeallocationNode(AASTNode aastNode, CodeNode?parent, bool dependOnStatementNum = true)
        {
            Generator g   = Program.currentCompiler.generator;
            Context?  ctx = SemanticAnalyzer.FindParentContext(aastNode);

            if (ctx == null)
            {
                throw new CompilationErrorException("TODO: ");
            }
            CodeNode regDeallocNode = new CodeNode("Register deallocation", parent);

            foreach (string var in g.regAllocVTR.Keys)
            {
                if ((ctx.IsVarDeclared(var) && !dependOnStatementNum || ctx.IsVarDeclaredInThisContext(var) && dependOnStatementNum) &&
                    !ctx.IsVarRoutine(var) && !ctx.IsVarConstant(var) && !ctx.IsVarLabel(var))
                {
                    int liEnd = ctx.GetLIEnd(var);

                    if (liEnd <= aastNode.BlockPosition || !dependOnStatementNum)
                    {
                        byte reg = g.regAllocVTR[var];
                        regDeallocNode.Children.AddLast(GetStoreVariableNode(regDeallocNode, var, reg, ctx));
                        g.regAllocVTR.Remove(var);
                        g.regAllocRTV.Remove(reg);
                        g.FreeReg(reg);
                    }
                }
            }
            return(regDeallocNode);
        }
Example #2
0
        private static TResult AnalyzeSemanticsCommon <TResult>(
            System.Data.Entity.Core.Common.EntitySql.AST.Node astExpr,
            Perspective perspective,
            ParserOptions parserOptions,
            IEnumerable <DbParameterReferenceExpression> parameters,
            IEnumerable <DbVariableReferenceExpression> variables,
            Func <SemanticAnalyzer, System.Data.Entity.Core.Common.EntitySql.AST.Node, TResult> analysisFunction)
            where TResult : class
        {
            TResult result = default(TResult);

            try
            {
                SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(SemanticResolver.Create(perspective, parserOptions, parameters, variables));
                return(analysisFunction(semanticAnalyzer, astExpr));
            }
            catch (MetadataException ex)
            {
                throw new EntitySqlException(Strings.GeneralExceptionAsQueryInnerException((object)"Metadata"), (Exception)ex);
            }
            catch (MappingException ex)
            {
                throw new EntitySqlException(Strings.GeneralExceptionAsQueryInnerException((object)"Mapping"), (Exception)ex);
            }
        }
        public override AASTNode Annotate(ASTNode astNode, AASTNode?parent)
        {
            // TODO: name of the block body (if we really need it)
            string   bbName = "name";
            AASTNode bb     = new AASTNode(astNode, parent, SemanticAnalyzer.no_type);

            bb.Context = new Context("BlockBody_" + bbName, SemanticAnalyzer.FindParentContext(parent), bb);
            bool   setLIEnd = false;
            string varName  = "";

            if (Program.currentCompiler.semantics.varToAddToCtx != null)
            {
                bb.Context.AddVar(Program.currentCompiler.semantics.varToAddToCtx, Program.currentCompiler.semantics.varToAddToCtx.Token.Value);
                varName  = Program.currentCompiler.semantics.varToAddToCtx.Token.Value;
                setLIEnd = true;
                Program.currentCompiler.semantics.varToAddToCtx = null;
            }
            foreach (ASTNode child in astNode.Children)
            {
                bb.Children.Add(base.Annotate(child, bb));
            }
            if (setLIEnd)
            {
                bb.Context.SetLIEnd(varName, SemanticAnalyzer.GetMaxDepth(bb));
            }
            return(bb);
        }
Example #4
0
        public string compile(string filepath)
        {
            var tokenizer   = new Tokenizer.Tokenizer(filepath);
            var tokenReader = new TokenReader(tokenizer);

            Parser.Parser._debug = false;
            var tree = Parser.Parser.ParseUnit(tokenReader);

            printDebug("Parse tree:\n" + tree + "\n");

            var semantic = new SemanticAnalyzer(tree);

            semantic.analyze();

            var aTree = semantic.annotatedTree;

            printDebug("Semantic tree:\n" + aTree + "\n");

            //var semantic = new SemanticAnalyzer(tree);
            //semantic.generateTables();
            //semantic.analyze();
            //printDebug("Semantic tree:\n" + tree + "\n");

            var codeGen = new CodeGenerator(aTree, semantic.moduleTable, semantic.dataTable);

            codeGen.generate();

            var asmCode = codeGen.assembly.ToString();

            printDebug("Generated assembly:\n" + asmCode);

            return(asmCode);
        }
		public async Task CanFindUnusedParameters()
		{
			const string Code = @"namespace abc
{
	using System;

	public class MyClass
	{
		public int Foo(int x, string y)
		{
			return x;
		}
	}
}";
			var solution = CreateSolution(Code);
			var doc = solution.Projects.First().Documents.First();

			var model = await doc.GetSemanticModelAsync();
			var root = await doc.GetSyntaxRootAsync();
			var method = root.DescendantNodes().OfType<MethodDeclarationSyntax>().First();
			var analyzer = new SemanticAnalyzer(model);
			var unused = analyzer.GetUnusedParameters(method);

			Assert.True(unused.Any(x => x.Identifier.ValueText == "y"));
		}
        public async void CanFindUnusedParameters()
        {
            const string Code     = @"namespace abc
{
	using System;

	public class MyClass
	{
		public int Foo(int x, string y)
		{
			return x;
		}
	}
}";
            var          solution = CreateSolution(Code);
            var          doc      = solution.Projects.First().Documents.First();

            var model = await doc.GetSemanticModelAsync();

            var root = await doc.GetSyntaxRootAsync();

            var method   = root.DescendantNodes().OfType <MethodDeclarationSyntax>().First();
            var analyzer = new SemanticAnalyzer(model);
            var unused   = analyzer.GetUnusedParameters(method);

            Assert.True(unused.Any(x => x.Identifier.ValueText == "y"));
        }
Example #7
0
 /// <summary>
 /// Constructor of class
 /// </summary>
 /// <param name="fileWriter"></param>
 public CodeGenerator(SemanticAnalyzer semanticAnalyzer, FileWriter fileWriter)
 {
     _fileWriter = fileWriter;
     countLoop   = 0;
     countLabel  = 0;
     _fileWriter.WriteCommand("resn " + semanticAnalyzer.VariablesCount);
 }
Example #8
0
        public static SyntaxNode ParseExpression(string expression, Environment environment)
        {
            SyntaxTreeBuilder builder = new SyntaxTreeBuilder();
            SyntaxNode        root    = builder.BuildTree(Lexer.Lex(new RawExpression(expression)));

            return(SemanticAnalyzer.Analyze(root, environment));
        }
        public PackageIL CompilePackage(
            string name,
            IEnumerable <CodeFile> files,
            FixedDictionary <Name, PackageIL> references)
        {
            var lexer            = new Lexer();
            var parser           = new CompilationUnitParser();
            var compilationUnits = files
                                   .Select(file =>
            {
                var context = new ParseContext(file, new Diagnostics());
                var tokens  = lexer.Lex(context).WhereNotTrivia();
                return(parser.Parse(tokens));
            })
                                   .ToFixedSet();
            var packageSyntax = new PackageSyntax(name, compilationUnits, references);

            var analyzer = new SemanticAnalyzer()
            {
                SaveLivenessAnalysis   = SaveLivenessAnalysis,
                SaveReachabilityGraphs = SaveReachabilityGraphs,
            };

            return(analyzer.Check(packageSyntax));
        }
Example #10
0
        // TryParse parses the source string and returns a list of errors on failure.
        // On success it fills the tree out parameter with the parsed tree.
        public static List <IError> TryParse(string source, out Tree tree)
        {
            // FIXME(an): Add try-catch block & convert to IError,
            // since Try* variants are supposed to be exception safe
            var lexer = new Lexer();

            lexer.Run(source);

            var sa = new SyntacticAnalyzer();

            tree = sa.Run(lexer.GetTokens());
            if (sa.GetErrors().Count > 0)
            {
                return(sa.GetErrors());
            }

            var treeWalker = new Walker();

            var semanticAnalyzer = new SemanticAnalyzer();

            treeWalker.Walk(semanticAnalyzer, tree);
            if (semanticAnalyzer.GetErrors().Count > 0)
            {
                return(semanticAnalyzer.GetErrors());
            }

            return(new List <IError>());
        }
Example #11
0
        public void TestASTGeneration()
        {
            SemanticAnalyzer analyzer = new SemanticAnalyzer();
            ASTNode          start    = analyzer.GenerateAST(_testTree);

            Assert.IsTrue(start.GetType() == typeof(Operator));
        }
Example #12
0
        public override CodeNode Construct(AASTNode aastNode, CodeNode?parent)
        {
            Generator g          = Program.currentCompiler.generator;
            CodeNode  asgmntNode = new CodeNode(aastNode, parent);

            Context?ctx = SemanticAnalyzer.FindParentContext(aastNode)
                          ?? throw new CompilationErrorException("No parent context found!!!\r\n  At line " + aastNode.Token.Position.Line);

            // Expression (right-value)
            CodeNode exprNode = base.Construct((AASTNode)aastNode.Children[1], asgmntNode);

            asgmntNode.Children.AddLast(exprNode);
            byte fr0 = exprNode.ByteToReturn;

            // Left-value to be assigned
            CodeNode receiverNode = base.Construct((AASTNode)aastNode.Children[0], asgmntNode);

            asgmntNode.Children.AddLast(receiverNode);
            byte fr1 = receiverNode.ByteToReturn;

            switch (aastNode.Children[0].ASTType)
            {
            case "Primary":     // Array or variable (TODO: dot-notation for structures and modules)
            {
                AASTNode prim = (AASTNode)aastNode.Children[0];
                if (prim.Children[^ 1].ASTType.Equals("IDENTIFIER"))
Example #13
0
        public void TestASTGeneration()
        {
            SemanticAnalyzer analyzer = new SemanticAnalyzer();
            ASTNode start = analyzer.GenerateAST(_testTree);

            Assert.IsTrue(start.GetType() == typeof (Operator));
        }
Example #14
0
        private static void Main(string[] args)
        {
            try
            {
                string text   = File.ReadAllText(args[0]);
                var    tokens = Tokenizer.GetTokens(text);

                TreeNode <Token> tree;
                using (var parser = new Parser(tokens))
                {
                    tree = parser.Parse();
                }

                var analyzer = new SemanticAnalyzer();
                analyzer.Build(tree);

                var interpreter = new Interpreter();
                interpreter.Interpret(tree);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

#if DEBUG
            Console.Read();
#endif
        }
		public async Task CanFindPossibleStaticMethod()
		{
			const string Code = @"namespace abc
{
	using System;

	public class MyClass
	{
		public int Foo()
		{
			return 5;
		}
	}
}";
			var solution = CreateSolution(Code);
			var doc = solution.Projects.First().Documents.First();

			var model = await doc.GetSemanticModelAsync();
			var root = await doc.GetSyntaxRootAsync();
			var type = root.DescendantNodes().OfType<TypeDeclarationSyntax>().First();
			var analyzer = new SemanticAnalyzer(model);
			var staticMethods = analyzer.GetPossibleStaticMethods(type);

			Assert.NotEmpty(staticMethods);
		}
Example #16
0
 public CodeContext()
 {
     resolver      = new SymbolResolver();
     analyzer      = new SemanticAnalyzer();
     flow_analyzer = new FlowAnalyzer();
     used_attr     = new UsedAttr();
 }
        public async void DoesNotSuggestAlreadyStaticMethods()
        {
            const string Code     = @"namespace abc
{
	using System;

	public class MyClass
	{
		public static int Foo()
		{
			return 5;
		}
	}
}";
            var          solution = CreateSolution(Code);
            var          doc      = solution.Projects.First().Documents.First();

            var model = await doc.GetSemanticModelAsync();

            var root = await doc.GetSyntaxRootAsync();

            var type          = root.DescendantNodes().OfType <TypeDeclarationSyntax>().First();
            var analyzer      = new SemanticAnalyzer(model);
            var staticMethods = analyzer.GetPossibleStaticMethods(type);

            CollectionAssert.IsEmpty(staticMethods);
        }
Example #18
0
        public virtual DataType get_actual_type(DataType derived_instance_type, List <DataType> method_type_arguments, CodeNodes.CodeNode node_reference)
        {
            DataType result = this.copy();

            if (derived_instance_type == null && method_type_arguments == null)
            {
                return(result);
            }

            if (result is GenericType)
            {
                result = SemanticAnalyzer.get_actual_type(derived_instance_type, method_type_arguments, (GenericType)result, node_reference);
                // don't try to resolve type arguments of returned actual type
                // they can never be resolved and are not related to the instance type
            }
            else if (result.type_argument_list != null)
            {
                // recursely get actual types for type arguments
                for (int i = 0; i < result.type_argument_list.Count; i++)
                {
                    result.type_argument_list[i] = result.type_argument_list[i].get_actual_type(derived_instance_type, method_type_arguments, node_reference);
                }
            }

            return(result);
        }
Example #19
0
        public override void visit_method_call(MethodCall expr)
        {
            if (expr.call is MemberAccess)
            {
                push_line(expr.source_reference);

                var ma = expr.call as MemberAccess;
                if (ma.inner != null && ma.inner.symbol_reference == gobject_type &&
                    (ma.member_name == "new" || ma.member_name == "newv"))
                {
                    // Object.new (...) creation
                    // runtime check to ref_sink the instance if it's a floating type
                    base.visit_method_call(expr);

                    var initiallyunowned_ccall = new CCodeFunctionCall(new CCodeIdentifier("G_IS_INITIALLY_UNOWNED"));
                    initiallyunowned_ccall.add_argument(get_cvalue(expr));
                    var sink_ref_ccall = new CCodeFunctionCall(new CCodeIdentifier("g_object_ref_sink"));
                    sink_ref_ccall.add_argument(get_cvalue(expr));
                    var cexpr = new CCodeConditionalExpression(initiallyunowned_ccall, sink_ref_ccall, get_cvalue(expr));

                    expr.target_value = store_temp_value(new GLibValue(expr.value_type, cexpr), expr);
                    return;
                }
                else if (ma.symbol_reference == gobject_type)
                {
                    // Object (...) chain up
                    // check it's only used with valid properties
                    foreach (var arg in expr.get_argument_list())
                    {
                        var named_argument = arg as NamedArgument;
                        if (named_argument == null)
                        {
                            Report.error(arg.source_reference, "Named argument expected");
                            break;
                        }
                        var prop = SemanticAnalyzer.symbol_lookup_inherited(current_class, named_argument.name) as Property;
                        if (prop == null)
                        {
                            Report.error(arg.source_reference, "Property `%s' not found in `%s'".printf(named_argument.name, current_class.get_full_name()));
                            break;
                        }
                        if (!is_gobject_property(prop))
                        {
                            Report.error(arg.source_reference, "Property `%s' not supported in Object (property: value) constructor chain up".printf(named_argument.name));
                            break;
                        }
                        if (!arg.value_type.compatible(prop.property_type))
                        {
                            Report.error(arg.source_reference, "Cannot convert from `%s' to `%s'".printf(arg.value_type.ToString(), prop.property_type.ToString()));
                            break;
                        }
                    }
                }

                pop_line();
            }

            base.visit_method_call(expr);
        }
Example #20
0
        public void ProgramTests(string source)
        {
            Context.Source = Text.Of(source);

            var parser    = new Parser(new Scanner());
            var cfg       = SemanticAnalyzer.Analyze((ProgramNode)parser.BuildTree());
            var generated = new Generator(cfg).Generate();
        }
Example #21
0
        public void TestQueueGeneration()
        {
            SemanticAnalyzer analyzer = new SemanticAnalyzer();
            var elements = analyzer.GetBreadthFirstQueue(_testTree);

            Assert.AreEqual(16, elements.Count);
            Assert.IsTrue(elements[0].GetEnum() == ParseEnum.Start);
        }
        public override CodeNode Construct(AASTNode aastNode, CodeNode?parent)
        {
            Generator g        = Program.currentCompiler.generator;
            CodeNode  primNode = new CodeNode(aastNode, parent);
            Context?  ctx      = SemanticAnalyzer.FindParentContext(aastNode)
                                 ?? throw new CompilationErrorException("No parent context found!!!\r\n  At line " + aastNode.Token.Position.Line);

            if (aastNode.Children[^ 1].ASTType.Equals("IDENTIFIER")) // Regular identifier access. TODO: context descend using dot-notation
Example #23
0
 public virtual Symbol get_member(string member_name)
 {
     if (data_type != null)
     {
         return(SemanticAnalyzer.symbol_lookup_inherited(data_type, member_name));
     }
     return(null);
 }
Example #24
0
        public void TestQueueGeneration()
        {
            SemanticAnalyzer analyzer = new SemanticAnalyzer();
            var elements = analyzer.GetBreadthFirstQueue(_testTree);

            Assert.AreEqual(16, elements.Count);
            Assert.IsTrue(elements[0].GetEnum() == ParseEnum.Start);
        }
        public void Analyze_NumberValue_ReturnsNumberNode()
        {
            SyntaxNode node = new NumberNode(1);

            node = SemanticAnalyzer.Analyze(node, new Environment());

            Assert.AreEqual("1", node.ToString());
        }
        private void AssertNoErrors(ProgramNode program)
        {
            var analyzer = new SemanticAnalyzer(program, errors);

            analyzer.Analyze();
            Assert.That(!errors.HasErrors);
            Assert.That(errors.HasErrors, Is.False);
        }
        public void Analyze_Keyword_ReturnsIdentifierNode()
        {
            SyntaxNode root = new IdentifierNode("e");

            root = SemanticAnalyzer.Analyze(root, new Environment());

            Assert.AreEqual("e", root.ToString());
        }
        public void Analyze_FunctionIllegalCallExpression_ThrowsException()
        {
            FunctionNode node = new FunctionNode(new IdentifierNode("ln"), new List <SyntaxNode>
            {
                new IdentifierNode("x")
            });

            Assert.Throws <UndefinedSymbolException>(() => SemanticAnalyzer.Analyze(node, new Environment()));
        }
        public void Analyze_FunctionUndefinedName_ThrowsException()
        {
            FunctionNode node = new FunctionNode(new IdentifierNode("f"), new List <SyntaxNode>
            {
                new NumberNode(2)
            });

            Assert.Throws <UndefinedSymbolException>(() => SemanticAnalyzer.Analyze(node, new Environment()));
        }
        public void Analyze_OperatorIllegalRHS_ThrowsException()
        {
            OperatorNode node = new OperatorNode(Operator.Addition, new List <SyntaxNode>
            {
                new NumberNode(2),
                new IdentifierNode("x")
            });

            Assert.Throws <UndefinedSymbolException>(() => SemanticAnalyzer.Analyze(node, new Environment()));
        }
        public void Analyze_AmbiguousPredefinedFunction_ReturnsFunctionNode()
        {
            IdentifierNode left  = new IdentifierNode("sin");
            SyntaxNode     right = new NumberNode(1);
            SyntaxNode     root  = new FunctionOrDistributionNode(left, right);

            root = SemanticAnalyzer.Analyze(root, new Environment());

            Assert.AreEqual("sin(1)", root.ToString());
        }
Example #32
0
        public void TestForm(string input, int exprectedErrorCount, string failureMessage)
        {
            var result           = new Result();
            var SemanticAnalyzer = new SemanticAnalyzer(result);
            var node             = ASTFactory.BuildForm(input);

            SemanticAnalyzer.AnalyzeForm(node);

            Assert.AreEqual(exprectedErrorCount, result.Events.Count, failureMessage);
        }
        private void AssertErrorContains(ProgramNode program, string expectedError)
        {
            var analyzer = new SemanticAnalyzer(program, errors);

            analyzer.Analyze();
            Assert.That(errors.HasErrors);
            var error = errors.GetErrors()[0];

            Assert.That(error.ErrorMessage, Does.Contain(expectedError));
        }
        public void Analyze_VariableSymbol_ReturnsIdentifierNode()
        {
            SyntaxNode  node        = new IdentifierNode("x");
            Environment environment = new Environment();

            environment.AddSymbol("x");

            node = SemanticAnalyzer.Analyze(node, environment);

            Assert.AreEqual("x", node.ToString());
        }
Example #35
0
        static void Main(string[] args)
        {
            // TODO(kai): parse out the args and do things

            if (args.Length == 0)
            {
                Console.WriteLine("No file passed to compile.");
                Wait();
                return;
            }

            var filePath = args[0];
            var dir = Path.GetDirectoryName(filePath);
            var fileName = Path.GetFileNameWithoutExtension(filePath);

            var log = new DetailLogger();

            var lexer = new Lexer();
            var tokens = lexer.GetTokens(log, filePath);

            if (log.HasErrors)
            {
                Fail(log);
                return;
            }

            var parser = new Parser();
            var ast = parser.Parse(log, tokens, filePath);

            if (log.HasErrors)
            {
                Fail(log);
                return;
            }

            var writer = new AstWriter(Console.Out);
            writer.Visit(ast);

            var semantics = new SemanticAnalyzer(log);
            var symbols = semantics.Analyze(ast);

            if (log.HasErrors)
            {
                Fail(log);
                return;
            }

            Console.WriteLine(symbols);

            var tyChecker = new TypeChecker(log);
            tyChecker.Check(ast, symbols);

            if (log.HasErrors)
            {
                Fail(log);
                return;
            }

            var compiler = new ScoreCompiler(log);
            var module = compiler.Compile(fileName, ast, symbols);

            if (log.HasErrors)
            {
                Fail(log);
                return;
            }

            DumpModule(module);
            var bcFilePath = Path.Combine(dir, fileName + ".bc");
            WriteBitcodeToFile(module, bcFilePath);

            /*
            // Run the interpreter!
            Console.WriteLine();
            Console.WriteLine("Interpreting:");
            var cmd = @"/c ..\..\..\..\TEST_FILES\lli.exe " + bcFilePath;
            //Console.WriteLine(cmd);
            var processInfo = new ProcessStartInfo("cmd.exe", cmd);
            processInfo.CreateNoWindow = false;
            processInfo.UseShellExecute = false;
            processInfo.RedirectStandardOutput = true;
            processInfo.RedirectStandardError = true;

            var process = Process.Start(processInfo);
            process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
            {
                Console.WriteLine(e.Data);
            };
            process.BeginOutputReadLine();

            process.WaitForExit();
            //*/

            Wait();
        }