コード例 #1
0
        public void WholeProgramWithoutBindingTest()
        {
            const string Code =
                @"
            Output(""Hello world""); ... 0
            Var name = Input();      ... 1
            Output(""Hey"" + name);  ... 2
            While True               ... 3
            {
                Output(""Hey"");
            }
            ";

            Lexer            lexer   = new(Code);
            LexemeCollection lexemes = lexer.LexAll();

            ProgramParser parser = new(lexemes, Code);
            Compilation   tree   = new(parser.ParseWholeProgram() !, Code);

            Assert.NotNull(tree);
            Assert.IsInstanceOf <ProgramNode>(tree.Program);

            ProgramNode programNode = tree.Program;

            Assert.AreEqual(4, programNode.TopLevelStatementNodes.Count);

            Assert.IsInstanceOf <FunctionCallStatementNode>(programNode.TopLevelStatementNodes[0]);
            Assert.IsInstanceOf <VariableDeclarationStatementNode>(programNode.TopLevelStatementNodes[1]);
            Assert.IsInstanceOf <FunctionCallStatementNode>(programNode.TopLevelStatementNodes[2]);
            Assert.IsInstanceOf <WhileStatementNode>(programNode.TopLevelStatementNodes[3]);

            WhileStatementNode @while = (WhileStatementNode)programNode.TopLevelStatementNodes[3];

            Assert.AreEqual(1, @while.StatementNodes.Count);
        }
コード例 #2
0
        public void TestInvalidIntegers()
        {
            var source = "0x1GG";
            LexemeCollection <LoreToken> actual = null;

            Assert.DoesNotThrow(() => actual = Support.GrabLexer(source).Tokenize());
        }
コード例 #3
0
ファイル: ProgramParser.cs プロジェクト: Phantonia/Krypton
        public ProgramParser(LexemeCollection lexemes, string code)
        {
            Lexemes = lexemes;

            this.code        = code;
            expressionParser = new ExpressionParser(lexemes, code);
            typeParser       = new TypeParser(lexemes, code);
            statementParser  = new StatementParser(lexemes, expressionParser, typeParser, code);
        }
コード例 #4
0
ファイル: StatementParser.cs プロジェクト: Phantonia/Krypton
 public StatementParser(LexemeCollection lexemes,
                        ExpressionParser expressionParser,
                        TypeParser typeParser,
                        string code)
 {
     this.lexemes          = lexemes;
     this.expressionParser = expressionParser;
     this.typeParser       = typeParser;
     this.code             = code;
 }
コード例 #5
0
        public void TestIntegers()
        {
            const string source = "1337 0x1337 25";
            LexemeCollection <LoreToken> actual = null;

            Assert.DoesNotThrow(() => actual = Support.GrabLexer(source).Tokenize());
            Assert.That(actual?.All(tk => tk.Token == LoreToken.IntLiteral) ?? false);
            Assert.That(actual?.Any(tk => tk.Value == "1337") ?? false);
            Assert.That(actual?.Any(tk => tk.Value == "4919") ?? false);
            Assert.That(actual?.Any(tk => tk.Value == "25") ?? false);
        }
コード例 #6
0
        public void TestIdentifiers()
        {
            const string source = "these are four identifiers";
            LexemeCollection <LoreToken> actual = null;

            Assert.DoesNotThrow(() => actual = Support.GrabLexer(source).Tokenize());
            Assert.That(actual?.All(tk => tk.Token == LoreToken.Identifier) ?? false);
            Assert.That(actual?.Any(tk => tk.Value == "these") ?? false);
            Assert.That(actual?.Any(tk => tk.Value == "are") ?? false);
            Assert.That(actual?.Any(tk => tk.Value == "four") ?? false);
            Assert.That(actual?.Any(tk => tk.Value == "identifiers") ?? false);
        }
コード例 #7
0
        public void TestFloats()
        {
            var source = ".10 13.37 73.31 1.";
            LexemeCollection <LoreToken> actual = null;

            Assert.DoesNotThrow(() => actual = Support.GrabLexer(source).Tokenize());
            Assert.That(actual?.All(tk => tk.Token == LoreToken.FloatLiteral) ?? false);
            Assert.That(actual?.Any(tk => tk.Value == "13.37") ?? false);
            Assert.That(actual?.Any(tk => tk.Value == "73.31") ?? false);
            Assert.That(actual?.Any(tk => tk.Value == "0.10") ?? false);
            Assert.That(actual?.Any(tk => tk.Value == "1.0") ?? false);
            source = "0. 1..something";
            Assert.DoesNotThrow(() => actual = Support.GrabLexer(source).Tokenize());
        }
コード例 #8
0
        public void TestOperators()
        {
            var operators = new [] { ".", "=", "==", "!=", "<=", ">=", "^=" };
            LexemeCollection <LoreToken> actual = null;

            foreach (var op in operators)
            {
                var source = $"a {op} b";
                try {
                    actual = Support.GrabLexer(source).Tokenize();
                } catch (LexerException e) {
                    Assert.Fail(e.Message);
                }
                Assert.That(actual?.ElementAt(0).Token == LoreToken.Identifier);
                Assert.That(actual?.ElementAt(1).Token == LoreToken.Operator);
                Assert.That(actual?.ElementAt(1).Value == op);
                Assert.That(actual?.ElementAt(2).Token == LoreToken.Identifier);
            }
        }
コード例 #9
0
        public void TestRealProgram()
        {
            const string source = @"
            // Lore master race
            fn print (msg) {
                stdout.writeln (msg);
            }
            fn main {
                a = 2;
                let _print ==> [a] print (a)
                _print ()
            }";
            LexemeCollection <LoreToken> actual = null;

            try {
                actual = Support.GrabLexer(source).Tokenize();
            } catch (LexerException e) {
                Assert.Fail(e.Message);
            }
            Assert.DoesNotThrow(() => actual = Support.GrabLexer(source).Tokenize());
        }
コード例 #10
0
ファイル: LexerTests.cs プロジェクト: Phantonia/Krypton
        public void SimpleStatementTest()
        {
            LexemeCollection lexemes = LexCode("Var number As Int = 4 + Sin(PI)");

            Assert.NotNull(lexemes);
            Assert.IsTrue(lexemes ![0] is KeywordLexeme {
コード例 #11
0
 public static ParsingUnit Create(LexemeCollection <LoreToken> lexemes) => new ParsingUnit(lexemes);
コード例 #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ParsingUnit"/> class.
 /// </summary>
 /// <param name="lexemes">Lexemes.</param>
 ParsingUnit(LexemeCollection <LoreToken> lexemes)
 {
     this.tokens = lexemes.ToList();
     cursor      = new SourceCursor(this.tokens.Count);
 }
コード例 #13
0
 public TypeParser(LexemeCollection lexemes, string code)
 {
     Lexemes   = lexemes;
     this.code = code;
 }
コード例 #14
0
 public ExpressionParser(LexemeCollection lexemes, string code)
 {
     Lexemes   = lexemes;
     this.code = code;
 }