Skip to content

Commit

Permalink
Parser with first tests; ICommand as abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Dec 16, 2011
1 parent 1cb31a4 commit 2ca779c
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 52 deletions.
62 changes: 11 additions & 51 deletions Src/AjLang.Tests/Compiler/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,28 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AjLang.Compiler;
using AjLang.Expressions;

namespace AjLang.Tests.Compiler
{
/// <summary>
/// Summary description for ParserTests
/// </summary>
[TestClass]
public class ParserTests
{
public ParserTests()
[TestMethod]
public void ParseInteger()
{
//
// TODO: Add constructor logic here
//
}
Parser parser = new Parser("123");
IExpression expr = parser.ParseExpression();

private TestContext testContextInstance;
Assert.IsNotNull(expr);
Assert.IsInstanceOfType(expr, typeof(ConstantExpression));

/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
ConstantExpression cexpr = (ConstantExpression)expr;

#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
Assert.AreEqual(123, cexpr.Evaluate(null));

[TestMethod]
public void TestMethod1()
{
//
// TODO: Add test logic here
//
Assert.IsNull(parser.ParseExpression());
}
}
}
3 changes: 3 additions & 0 deletions Src/AjLang/AjLang.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\ICommand.cs" />
<Compile Include="Commands\SetVariableCommand.cs" />
<Compile Include="Compiler\ParserException.cs" />
<Compile Include="Compiler\Lexer.cs" />
<Compile Include="Compiler\LexerException.cs" />
<Compile Include="Compiler\Parser.cs" />
<Compile Include="Compiler\Token.cs" />
<Compile Include="Compiler\TokenType.cs" />
<Compile Include="Context.cs" />
Expand Down
8 changes: 8 additions & 0 deletions Src/AjLang/Commands/ICommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace AjLang.Commands
{
public interface ICommand
{
void Execute(Context context);
}
}
2 changes: 1 addition & 1 deletion Src/AjLang/Commands/SetVariableCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

using AjLang.Expressions;

public class SetVariableCommand
public class SetVariableCommand : ICommand
{
private string name;
private IExpression expression;
Expand Down
45 changes: 45 additions & 0 deletions Src/AjLang/Compiler/Parser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace AjLang.Compiler
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AjLang.Expressions;
using System.Globalization;

public class Parser
{
private Lexer lexer;

public Parser(string text)
: this(new Lexer(text))
{
}

public Parser(Lexer lexer)
{
this.lexer = lexer;
}

public IExpression ParseExpression()
{
Token token = this.NextToken();

if (token == null)
return null;

switch (token.Type)
{
case TokenType.Integer:
return new ConstantExpression(int.Parse(token.Value, CultureInfo.InvariantCulture));
}

throw new ParserException(string.Format("Unexpected '{0}'", token.Value));
}

private Token NextToken()
{
return this.lexer.NextToken();
}
}
}
15 changes: 15 additions & 0 deletions Src/AjLang/Compiler/ParserException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace AjLang.Compiler
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class ParserException : Exception
{
public ParserException(string msg)
: base(msg)
{
}
}
}

0 comments on commit 2ca779c

Please sign in to comment.