Esempio n. 1
0
        /// <summary>
        /// Compiles the specified code
        /// </summary>
        /// <param name="code">MuteScript code to compile</param>
        /// <returns>Returns a <see cref="CompilationResult" /></returns>
        public CompilationResult Compile(string code)
        {
            var lexer  = new MuteGrammarLexer(new AntlrInputStream(code));
            var parser = new MuteGrammarParser(new BufferedTokenStream(lexer));

            var errorListener = new TokenErrorListener {
                Messages = parser.Messages
            };

            parser.RemoveErrorListeners();
            parser.AddErrorListener(errorListener);
            parser.NamespaceLookup = this.NamespaceLookup;
            parser.UsingTypes      = this.ImplicitImports;

            var ctx = parser.compileUnit();

            if (parser.Messages.Count(x => x.Severity == MessageSeverity.Error) > 0)
            {
                return(new CompilationResult(null, parser.Messages));
            }

            var cmp    = new ExpressionCompiler();
            var result = cmp.Compile(ctx.module);

            return(new CompilationResult(result, parser.Messages));
        }
        public void Foo()
        {
            var parser = new MuteScriptParser();
            var lexer = new MuteGrammarLexer(new Antlr4.Runtime.AntlrInputStream("{ 123 . 456 }"));
            var muteParser = new MuteGrammarParser(new Antlr4.Runtime.UnbufferedTokenStream(lexer));

            var result = parser.VisitStatementBlock(muteParser.statementBlock());
        }
        public void ConstInteger_ReturnsConstIntegerExpression()
        {
            var parser = new MuteScriptParser();
            var lexer = new MuteGrammarLexer(new Antlr4.Runtime.AntlrInputStream("123"));
            var muteParser = new MuteGrammarParser(new Antlr4.Runtime.UnbufferedTokenStream(lexer));

            var result = (ConstIntegerExpression)parser.VisitConstExpression(muteParser.constExpression());
            Assert.That(result?.ToString(), Is.EqualTo("123"));
        }
        public void ConstExpression_ReturnsTerminal(string input, Type expectedType)
        {
            var parser = new MuteScriptParser();
            var lexer = new MuteGrammarLexer(new Antlr4.Runtime.AntlrInputStream(input));
            var muteParser = new MuteGrammarParser(new Antlr4.Runtime.UnbufferedTokenStream(lexer));

            var result = parser.VisitConstExpression(muteParser.constExpression());

            Assert.That(result, Is.TypeOf(expectedType));
            Assert.That(result?.ToString(), Is.EqualTo(input));
        }
        private static Method ParseMethod(string code)
        {
            var parser = new MuteScriptParser();
            var lexer = new MuteGrammarLexer(new Antlr4.Runtime.AntlrInputStream(code));
            var muteParser = new MuteGrammarParser(new Antlr4.Runtime.UnbufferedTokenStream(lexer));

            return (Method)parser.VisitClassMember(muteParser.classMember());
        }
 private static Expression ParseExpression(string code)
 {
     var parser = new MuteScriptParser();
     var lexer = new MuteGrammarLexer(new Antlr4.Runtime.AntlrInputStream(code));
     var muteParser = new MuteGrammarParser(new Antlr4.Runtime.UnbufferedTokenStream(lexer));
     var exp = muteParser.expression();
     return (Expression)parser.VisitExpression(exp);
 }
        private static Class ParseClass(string code)
        {
            var parser = new MuteScriptParser();
            var lexer = new MuteGrammarLexer(new Antlr4.Runtime.AntlrInputStream(code));
            var muteParser = new MuteGrammarParser(new Antlr4.Runtime.UnbufferedTokenStream(lexer));

            return (Class)parser.VisitTypeDefinitionStatement(muteParser.typeDefinitionStatement());
        }
        public void TestFoo()
        {
            const string input = @"
            module foo

            public class bar
            {
            public meth(const arg : int) : bool => abc <- 100
            }
            ";

            var parser = new MuteScriptParser();
            var lexer = new MuteGrammarLexer(new Antlr4.Runtime.AntlrInputStream(input));
            var muteParser = new MuteGrammarParser(new Antlr4.Runtime.UnbufferedTokenStream(lexer));

            var cmp = parser.VisitCompileUnit(muteParser.compileUnit());
            Console.WriteLine(cmp.ToString());
        }