static void Main(string[] args) { // Abstract Syntax Tree (AST) for 5*10 Exp e = new BinaryExp(new NumericConstant(5), new NumericConstant(10), OPERATOR.MUL); // // Evaluate the Expression // // Console.WriteLine(e.Evaluate(null)); // AST for -(10 + (30 + 50 ) ) e = new UnaryExp( new BinaryExp(new NumericConstant(10), new BinaryExp(new NumericConstant(30), new NumericConstant(50), OPERATOR.PLUS), OPERATOR.PLUS), OPERATOR.MINUS); // // Evaluate the Expression // Console.WriteLine(e.Evaluate(null)); // // Pause for a key stroke // Console.Read(); }
public CodeExpression VisitUnary(UnaryExp u) { if (u.op == Op.Sub && u.e is RealLiteral real) { return(new RealLiteral("-" + real.Value, -real.NumericValue, real.Filename, real.Start, real.End).Accept(this)); } var e = u.e.Accept(this); return(new CodeUnaryOperatorExpression(mppyoptocsop[u.op], e)); }
public Constant VisitUnaryExp(UnaryExp unaryExp) { var c = unaryExp.exp.Accept(this); if (!c.IsValid) { return(c); } if (unaryExp.op == TokenType.Minus) { return(Operator.Neg.ApplyConstant(c)); } throw new NotImplementedException(); }
private Exp ParseFactor() { switch (t.Kind) { case Kind.Plus: case Kind.Minus: { var op = t; Next(); var ret = new UnaryExp(op, ParseFactor()); // ParseExp? return(ret); } case Kind.Number: { var ret = new NumberExp(t); Next(); return(ret); } case Kind.Open: { var open = t; Next(); var exp = ParseExpr(); if (t.Kind != Kind.Close) { throw new Exception("')' expected"); } var close = t; Next(); return(new ParenthesesExp(open, exp, close)); } case Kind.Eof: throw new Exception("number expected"); default: throw new Exception($"number expected, found '{t.Raw}'"); } }
public SerializedType VisitUnaryExp(UnaryExp unaryExp) { throw new NotImplementedException(); }
public void VisitUnary(UnaryExp u) { throw new NotImplementedException(); }
public DataType VisitUnary(UnaryExp u) { return(u.e.Accept(this)); }
public CodeExpression VisitUnary(UnaryExp u) { var e = u.e.Accept(this); return(new CodeUnaryOperatorExpression(mppyoptocsop[u.op], e)); }