예제 #1
0
        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();
        }
예제 #2
0
        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));
        }
예제 #3
0
        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();
        }
예제 #4
0
        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}'");
            }
        }
예제 #5
0
 public SerializedType VisitUnaryExp(UnaryExp unaryExp)
 {
     throw new NotImplementedException();
 }
예제 #6
0
 public void VisitUnary(UnaryExp u)
 {
     throw new NotImplementedException();
 }
예제 #7
0
 public DataType VisitUnary(UnaryExp u)
 {
     return(u.e.Accept(this));
 }
예제 #8
0
        public CodeExpression VisitUnary(UnaryExp u)
        {
            var e = u.e.Accept(this);

            return(new CodeUnaryOperatorExpression(mppyoptocsop[u.op], e));
        }
예제 #9
0
 public void VisitUnary(UnaryExp u)
 {
     throw new NotImplementedException();
 }