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); }
public void TestInvalidIntegers() { var source = "0x1GG"; LexemeCollection <LoreToken> actual = null; Assert.DoesNotThrow(() => actual = Support.GrabLexer(source).Tokenize()); }
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); }
public StatementParser(LexemeCollection lexemes, ExpressionParser expressionParser, TypeParser typeParser, string code) { this.lexemes = lexemes; this.expressionParser = expressionParser; this.typeParser = typeParser; this.code = code; }
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); }
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); }
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()); }
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); } }
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()); }
public void SimpleStatementTest() { LexemeCollection lexemes = LexCode("Var number As Int = 4 + Sin(PI)"); Assert.NotNull(lexemes); Assert.IsTrue(lexemes ![0] is KeywordLexeme {
public static ParsingUnit Create(LexemeCollection <LoreToken> lexemes) => new ParsingUnit(lexemes);
/// <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); }
public TypeParser(LexemeCollection lexemes, string code) { Lexemes = lexemes; this.code = code; }
public ExpressionParser(LexemeCollection lexemes, string code) { Lexemes = lexemes; this.code = code; }