Permalink
Browse files

ExpressionSample - add type to expression (for semantic analysis)

  • Loading branch information...
bobbymcr committed Apr 17, 2018
1 parent 7c5a6ca commit e5df3f286c8392c97ac0532fb5def95713f70bd6
@@ -10,12 +10,11 @@ namespace GWParse.Expressions
internal sealed class BasicArray : BasicExpression
{
private readonly string name;
private readonly BasicType type;
private readonly BasicExpression[] subs;
private BasicArray(BasicType type, string name, IEnumerable<BasicExpression> subs)
: base(type)
{
this.type = type;
this.name = name;
this.subs = subs.ToArray();
}
@@ -32,7 +31,7 @@ public static BasicArray Str(string name, IEnumerable<BasicExpression> subs)
public override void Accept(IExpressionVisitor visit)
{
visit.Array(this.type, this.name, this.subs);
visit.Array(this.Type, this.name, this.subs);
}
}
}
@@ -8,10 +8,13 @@ namespace GWParse.Expressions
public abstract class BasicExpression
{
protected BasicExpression()
protected BasicExpression(BasicType type)
{
this.Type = type;
}
public BasicType Type { get; private set; }
public static BasicExpression FromString(string input) => Expr.FromString(input);
public abstract void Accept(IExpressionVisitor visit);
@@ -6,12 +6,11 @@ namespace GWParse.Expressions
{
internal sealed class BasicLiteral : BasicExpression
{
private readonly BasicType type;
private readonly object o;
private BasicLiteral(BasicType type, object o)
: base(type)
{
this.type = type;
this.o = o;
}
@@ -21,7 +20,7 @@ private BasicLiteral(BasicType type, object o)
public override void Accept(IExpressionVisitor visit)
{
visit.Literal(this.type, this.o);
visit.Literal(this.Type, this.o);
}
}
}
@@ -9,25 +9,26 @@ internal sealed class BasicOperator : BasicExpression
private readonly string name;
private readonly BasicExpression[] operands;
private BasicOperator(string name, BasicExpression[] operands)
private BasicOperator(string name, BasicType type, BasicExpression[] operands)
: base(type)
{
this.name = name;
this.operands = operands;
}
public static BasicOperator Unary(string name, BasicExpression x)
public static BasicOperator Unary(string name, BasicType type, BasicExpression x)
{
return new BasicOperator(name, new BasicExpression[] { x });
return new BasicOperator(name, type, new BasicExpression[] { x });
}
public static BasicOperator Binary(string name, BasicExpression x, BasicExpression y)
public static BasicOperator Binary(string name, BasicType type, BasicExpression x, BasicExpression y)
{
return new BasicOperator(name, new BasicExpression[] { x, y });
return new BasicOperator(name, type, new BasicExpression[] { x, y });
}
public static BasicOperator Ternary(string name, BasicExpression x, BasicExpression y, BasicExpression z)
public static BasicOperator Ternary(string name, BasicType type, BasicExpression x, BasicExpression y, BasicExpression z)
{
return new BasicOperator(name, new BasicExpression[] { x, y, z });
return new BasicOperator(name, type, new BasicExpression[] { x, y, z });
}
public override void Accept(IExpressionVisitor visit)
@@ -6,12 +6,11 @@ namespace GWParse.Expressions
{
internal sealed class BasicVariable : BasicExpression
{
private readonly BasicType type;
private readonly string name;
private BasicVariable(BasicType type, string name)
: base(type)
{
this.type = type;
this.name = name;
}
@@ -21,7 +20,7 @@ private BasicVariable(BasicType type, string name)
public override void Accept(IExpressionVisitor visit)
{
visit.Variable(this.type, this.name);
visit.Variable(this.Type, this.name);
}
}
}
@@ -174,7 +174,7 @@ private static class Str
from c in Ch.Comma.Token()
from n in Num.Any
from rp in Ch.RightParen.Token()
select BasicOperator.Binary("Left", x, n);
select BasicOperator.Binary("Left", BasicType.Str, x, n);
private static readonly Parser<Tuple<BasicExpression, BasicExpression>> MidPrefix =
from f in Kw.Mid
@@ -190,12 +190,12 @@ private static class Str
from c in Ch.Comma.Token()
from m in Num.Any
from rp in Ch.RightParen.Token()
select BasicOperator.Ternary("Mid", t.Item1, t.Item2, m);
select BasicOperator.Ternary("Mid", BasicType.Str, t.Item1, t.Item2, m);
private static readonly Parser<BasicExpression> Mid2 =
from t in MidPrefix
from rp in Ch.RightParen.Token()
select BasicOperator.Binary("Mid", t.Item1, t.Item2);
select BasicOperator.Binary("Mid", BasicType.Str, t.Item1, t.Item2);
private static readonly Parser<BasicExpression> Mid = Mid3.Or(Mid2);
@@ -207,7 +207,7 @@ private static class Str
from c in Ch.Comma.Token()
from n in Num.Any
from rp in Ch.RightParen.Token()
select BasicOperator.Binary("Right", x, n);
select BasicOperator.Binary("Right", BasicType.Str, x, n);
private static readonly Parser<BasicExpression> Fun = Left.Or(Mid).Or(Right);
@@ -244,17 +244,17 @@ private static class Num
private static readonly Parser<BasicExpression> Exp =
from f in Kw.Exp
from x in Paren
select BasicOperator.Unary("Exp", x);
select BasicOperator.Unary("Exp", BasicType.Num, x);
private static readonly Parser<BasicExpression> Len =
from f in Kw.Len.Token()
from x in Str.Paren
select BasicOperator.Unary("Len", x);
select BasicOperator.Unary("Len", BasicType.Num, x);
private static readonly Parser<BasicExpression> Sqr =
from f in Kw.Sqr
from x in Paren
select BasicOperator.Unary("Sqrt", x);
select BasicOperator.Unary("Sqrt", BasicType.Num, x);
private static readonly Parser<BasicExpression> Fun = Exp.Or(Len).Or(Sqr);
@@ -270,7 +270,7 @@ private static class Num
private static readonly Parser<BasicExpression> Neg =
from m in Ch.Minus.Token()
from x in Pow
select BasicOperator.Unary("Neg", x);
select BasicOperator.Unary("Neg", BasicType.Num, x);
private static readonly Parser<BasicExpression> Mult =
Parse.ChainOperator(Op.Multiplicative, Neg.Or(Pow), Op.Apply);
@@ -284,7 +284,7 @@ private static class Num
private static readonly Parser<BasicExpression> Not =
from k in Kw.Not
from x in Add.Token()
select BasicOperator.Unary("Not", x);
select BasicOperator.Unary("Not", BasicType.Num, x);
private static readonly Parser<BasicExpression> And =
Parse.ChainOperator(Op.And, Not.Or(Relational), Op.Apply);
@@ -373,22 +373,24 @@ private sealed class Binary : IOperator
public static readonly IOperator Lt = new Binary("Lt");
public static readonly IOperator Ge = new Binary("Ge");
public static readonly IOperator Gt = new Binary("Gt");
public static readonly IOperator Add = new Binary("Add");
public static readonly IOperator Add = new Binary("Add", BasicType.None);
public static readonly IOperator Sub = new Binary("Sub");
public static readonly IOperator Mult = new Binary("Mult");
public static readonly IOperator Div = new Binary("Div");
public static readonly IOperator Pow = new Binary("Pow");
private readonly string name;
private readonly BasicType type;
private Binary(string name)
private Binary(string name, BasicType type = BasicType.Num)
{
this.name = name;
this.type = type;
}
public BasicExpression Apply(BasicExpression x, BasicExpression y)
{
return BasicOperator.Binary(this.name, x, y);
return BasicOperator.Binary(this.name, this.type, x, y);
}
}
}

0 comments on commit e5df3f2

Please sign in to comment.