Permalink
Browse files

Lots of progress on the expression parser.

  • Loading branch information...
1 parent b4d2c32 commit f0cde9d2f82bd47beeaeb8c51fdaad6d703178e0 @JamesDunne committed May 24, 2012
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+
+namespace WellDunne.Expressions
+{
+ public sealed class AndExpression : BinaryExpression
+ {
+ private readonly Token _token;
+
+ public AndExpression(Token tok, Expression l, Expression r)
+ : base(l, r)
+ {
+ _token = tok;
+ }
+
+ protected override void WriteInner(TextWriter tw)
+ {
+ tw.Write(" and ");
+ }
+ }
+}
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+
+namespace WellDunne.Expressions
+{
+ public abstract class BinaryExpression : Expression
+ {
+ protected readonly Expression _l;
+ protected readonly Expression _r;
+
+ public Expression Left { get { return _l; } }
+ public Expression Right { get { return _r; } }
+
+ protected BinaryExpression(Expression l, Expression r)
+ {
+ _l = l;
+ _r = r;
+ }
+
+ protected abstract void WriteInner(TextWriter tw);
+
+ public override void WriteTo(TextWriter tw)
+ {
+ tw.Write("(");
+ Left.WriteTo(tw);
+ WriteInner(tw);
+ Right.WriteTo(tw);
+ tw.Write(")");
+ }
+ }
+}
@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+
+namespace WellDunne.Expressions
+{
+ public sealed class BooleanExpression : Expression
+ {
+ private readonly Token _token;
+ private readonly bool _value;
+
+ public BooleanExpression(Token tok, bool value)
+ {
+ this._token = tok;
+ this._value = value;
+ }
+
+ public override void WriteTo(TextWriter tw)
+ {
+ tw.Write(_value ? "true" : "false");
+ }
+ }
+}
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+
+namespace WellDunne.Expressions
+{
+ public sealed class CompareExpression : BinaryExpression
+ {
+ private readonly Token _token;
+
+ public CompareExpression(Token tok, Expression l, Expression r)
+ : base(l, r)
+ {
+ _token = tok;
+ }
+
+ protected override void WriteInner(TextWriter tw)
+ {
+ tw.Write(" " + _token.Value + " ");
+ }
+ }
+}
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+
+namespace WellDunne.Expressions
+{
+ public sealed class DecimalExpression : Expression
+ {
+ private readonly Token _token;
+
+ public DecimalExpression(Token tok)
+ {
+ this._token = tok;
+ }
+
+ public override void WriteTo(TextWriter tw)
+ {
+ tw.Write(_token.Value);
+ }
+ }
+}
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+
+namespace WellDunne.Expressions
+{
+ public sealed class EqualExpression : BinaryExpression
+ {
+ private readonly Token _token;
+
+ public EqualExpression(Token tok, Expression l, Expression r)
+ : base(l, r)
+ {
+ _token = tok;
+ }
+
+ protected override void WriteInner(TextWriter tw)
+ {
+ tw.Write(" " + _token.Value + " ");
+ }
+ }
+}
@@ -2,10 +2,24 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
+using System.IO;
namespace WellDunne.Expressions
{
public abstract class Expression
{
+ public virtual void WriteTo(TextWriter tw)
+ {
+ tw.Write("<expr>");
+ }
+
+ public override string ToString()
+ {
+ using (var tw = new StringWriter())
+ {
+ WriteTo(tw);
+ return tw.ToString();
+ }
+ }
}
}
@@ -2,15 +2,22 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
+using System.IO;
namespace WellDunne.Expressions
{
public class IdentifierExpression : Expression
{
private readonly Token _token;
+
public IdentifierExpression(Token token)
{
_token = token;
}
+
+ public override void WriteTo(TextWriter tw)
+ {
+ tw.Write(_token.Value);
+ }
}
}
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+
+namespace WellDunne.Expressions
+{
+ public sealed class IntegerExpression : Expression
+ {
+ private readonly Token _token;
+
+ public IntegerExpression(Token tok)
+ {
+ this._token = tok;
+ }
+
+ public override void WriteTo(TextWriter tw)
+ {
+ tw.Write(_token.Value);
+ }
+ }
+}
@@ -23,20 +23,6 @@ public Lexer(TextReader sr, long charPosition)
_charPosition = charPosition;
}
- private char Read()
- {
- ++_charPosition;
- return (char)_reader.Read();
- }
-
- private char Peek() { return (char)_reader.Peek(); }
- private long Position() { return _charPosition; }
- private bool EndOfStream() { return _reader.Peek() == -1; }
-
- private static readonly HashSet<string> operatorNames = new HashSet<string>(new string[] {
- "eq", "ne", "lt", "gt", "le", "ge", "like", "in", "not", "and", "or"
- });
-
public IEnumerable<Token> Lex()
{
while (!EndOfStream())
@@ -150,16 +136,42 @@ public IEnumerable<Token> Lex()
yield return new Token(TokenKind.StringLiteral, position, sb.ToString());
}
else if (c == ',')
+ {
+ Read();
yield return new Token(TokenKind.Comma, position);
+ }
else if (c == '(')
+ {
+ Read();
yield return new Token(TokenKind.ParenOpen, position);
+ }
else if (c == ')')
+ {
+ Read();
yield return new Token(TokenKind.ParenClose, position);
+ }
else
+ {
+ Read();
yield return new Token(TokenKind.Invalid, position, String.Format("Unrecognized character '{0}' at position {1}", c, position + 1));
+ }
}
yield break;
}
+
+ private char Read()
+ {
+ ++_charPosition;
+ return (char)_reader.Read();
+ }
+
+ private char Peek() { return (char)_reader.Peek(); }
+ private long Position() { return _charPosition; }
+ private bool EndOfStream() { return _reader.Peek() == -1; }
+
+ private static readonly HashSet<string> operatorNames = new HashSet<string>(new string[] {
+ "eq", "ne", "lt", "gt", "le", "ge", "like", "in", "not", "and", "or"
+ });
}
}
@@ -1,16 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace WellDunne.Expressions
-{
- public class LiteralExpression : Expression
- {
- private readonly Token _token;
- public LiteralExpression(Token token)
- {
- _token = token;
- }
- }
-}
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+
+namespace WellDunne.Expressions
+{
+ public sealed class NullExpression : Expression
+ {
+ private readonly Token _token;
+
+ public NullExpression(Token tok)
+ {
+ _token = tok;
+ }
+
+ public override void WriteTo(TextWriter tw)
+ {
+ tw.Write("null");
+ }
+ }
+}
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+
+namespace WellDunne.Expressions
+{
+ public class OrExpression : BinaryExpression
+ {
+ private readonly Token _token;
+
+ public OrExpression(Token tok, Expression l, Expression r)
+ : base(l, r)
+ {
+ _token = tok;
+ }
+
+ protected override void WriteInner(TextWriter tw)
+ {
+ tw.Write(" or ");
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit f0cde9d

Please sign in to comment.