Beispiel #1
0
        public void Eval(LaxExpression s)
        {
            switch (s.Type)
            {
                case ExprType.Assignment:
                    EvalAssignmentOperator((Assignment)s);
                    return;

                case ExprType.BinaryOp:
                    EvalBinaryOperator((BinaryOp)s);
                    return;

                case ExprType.Identifier:
                    //Get type from block
                    var def = block.Variables[(Identifier)s];
                    s.ValueType = def.ValueType;
                    return;

                case ExprType.Literal:
                    return;

                case ExprType.FunctionCall:
            #warning TODO custom implementation per function.
                    var f = (FunctionCall)s;
                    foreach (var a in f.Arguments)
                        Eval(a);
                    s.ValueType = f.Arguments[0].ValueType;
                    Debug.Assert(s.ValueType != null);
                    return;

                default:
                    throw new NotImplementedException();
            }
        }
Beispiel #2
0
        public ConstDefinition(CodeRange start, Identifier id, LaxExpression value) : base(start, id)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            this.Value = value;
        }
Beispiel #3
0
 /// <summary>
 /// return a new <seealso cref="Assignment"/> with Left and Right assigned.
 /// </summary>
 /// <param name="left"></param>
 /// <param name="right"></param>
 /// <returns></returns>
 public Assignment WithOperands(LaxExpression left, LaxExpression right)
 {
     var op = new Assignment(this);
     op.Left = left;
     op.Right = right;
     op.CodeRange = CodeRange.Expand2(left).Expand2(right);
     return op;
 }
Beispiel #4
0
 /// <summary>
 /// return a new <seealso cref="Assignment"/> with Left and Right assigned.
 /// </summary>
 /// <param name="left"></param>
 /// <param name="right"></param>
 /// <returns></returns>
 public BinaryOp WithOperands(LaxExpression left, LaxExpression right)
 {
     var op = new BinaryOp(CodeRange, Operator);
     op.Left = left;
     op.Right = right;
     op.CodeRange = CodeRange.Expand2(left).Expand2(right);
     return op;
 }
Beispiel #5
0
        public Literal Evaluate(LaxExpression expr)
        {
            if (expr == null)
                return null;

            switch (expr.Type)
            {
                case ExprType.Assignment:
                    return EvaluateAssignment((Assignment)expr);
                case ExprType.Literal:
                    return (Literal)expr;
                case ExprType.Identifier:
                    return EvaluateVariable((Identifier)expr);
                case ExprType.BinaryOp:
                    return EvaluateBinaryOp((BinaryOp)expr);
                case ExprType.FunctionCall:
                    return EvaluateFunctionCall((FunctionCall)expr);

                default:
                    throw new NotImplementedException();
            }
        }
Beispiel #6
0
        static LaxExpression Evaluate(LaxExpression s)
        {
            switch (s.Type)
            {
                case ExprType.Identifier:
                case ExprType.Literal:
                    return s; //Nothing to evaluate further

                case ExprType.Assignment:
                    return EvaluateAssignment((Assignment)s);

                case ExprType.BinaryOp:
                    return EvaluateOp((BinaryOp)s);

                case ExprType.FunctionCall:
                    return EvaluateFunctionCall((FunctionCall)s);

                default:
                    throw new NotImplementedException();

            }
        }
Beispiel #7
0
 public static string Format(LaxExpression expr)
 {
     var format = new CodeFormat(false);
     return format.FormatExpression(expr);
 }
Beispiel #8
0
        /// <summary>
        /// Return source code representation of an expression.
        /// </summary>
        public string FormatExpression(LaxExpression expr)
        {
            if (expr == null)
                throw new ArgumentNullException("expr");
            switch (expr.Type)
            {
                case ExprType.Assignment:
                    return FormatAssignment((Assignment)expr);
                case ExprType.BinaryOp:
                    return FormatBinaryOp((BinaryOp)expr);
                case ExprType.Identifier:
                    return FormatIdentifier((Identifier)expr);
                case ExprType.Literal:
                    return FormatValue((Literal)expr);
                case ExprType.FunctionCall:
                    return FormatFunctionCall((FunctionCall)expr);
            }

            throw new NotImplementedException(expr.GetType().Name);
        }
Beispiel #9
0
 public SemanticError(LaxExpression expr, string message) : base(expr.CodeRange, message)
 {
 }
Beispiel #10
0
        /// <summary>
        /// Allow implicit multuplication
        /// </summary>
        /// <param name="exp"></param>
        void PushImplicitMultiplication(LaxExpression exp)
        {
            if (opStack.Count == operandStack.Count)
            {
                //Operand
                operandStack.Push(exp);
            }
            else
            {
                //Allow expression without "*" such as: 45 apples
                PushOperator(BinaryOp.UnitOp(exp.CodeRange));

                //Add type to literal
                var l = (Literal)exp;
                PushLiteral(l);
            }
        }