Skip to content

Commit

Permalink
Parse variable, with test
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Dec 16, 2011
1 parent 2ca779c commit 7c010ec
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Src/AjLang.Tests/Compiler/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,21 @@ public void ParseInteger()

Assert.IsNull(parser.ParseExpression());
}

[TestMethod]
public void ParseVariable()
{
Parser parser = new Parser("foo");
IExpression expr = parser.ParseExpression();

Assert.IsNotNull(expr);
Assert.IsInstanceOfType(expr, typeof(VariableExpression));

VariableExpression vexpr = (VariableExpression)expr;

Assert.AreEqual("foo", vexpr.Name);

Assert.IsNull(parser.ParseExpression());
}
}
}
2 changes: 2 additions & 0 deletions Src/AjLang/Compiler/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public IExpression ParseExpression()
{
case TokenType.Integer:
return new ConstantExpression(int.Parse(token.Value, CultureInfo.InvariantCulture));
case TokenType.Name:
return new VariableExpression(token.Value);
}

throw new ParserException(string.Format("Unexpected '{0}'", token.Value));
Expand Down
4 changes: 3 additions & 1 deletion Src/AjLang/Expressions/VariableExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class VariableExpression : IExpression
public VariableExpression(string name)
{
this.name = name;
}
}

public string Name { get { return this.name; } }

public object Evaluate(Context context)
{
Expand Down

0 comments on commit 7c010ec

Please sign in to comment.