Example #1
0
        private TinyValue Add(TinyUnityParser.AddExpressionContext context)
        {
            TinyValue lhs = this.Visit(context.expression(0));
            TinyValue rhs = this.Visit(context.expression(1));

            if (lhs == null || rhs == null)
            {
                throw new EvalException("Null operators: " + context.op.Type, context);
            }

            if (lhs.value is float && rhs.value is float)
            {
                return(new TinyValue((float)lhs.value + (float)rhs.value));
            }

            if (lhs.isList())
            {
                List <TinyValue> nlst = lhs.asList();
                nlst.Add(rhs);
                return(new TinyValue(nlst));
            }

            if (lhs.isString())
            {
                return(new TinyValue(lhs.asString() + rhs.value.ToString()));
            }

            if (rhs.isString())
            {
                return(new TinyValue(lhs.value.ToString() + rhs.asString()));
            }

            return(new TinyValue(lhs.value.ToString() + rhs.value.ToString()));
        }
Example #2
0
        public override TinyValue VisitAddExpression([NotNull] TinyUnityParser.AddExpressionContext context)
        {
            switch (context.op.Type)
            {
            case TinyUnityLexer.Add:
                return(Add(context));

            case TinyUnityLexer.Subtract:
                return(Subtract(context));

            default:
                throw new EvalException("Bad operators: " + context.op.Type, context);
            }
        }
Example #3
0
        private TinyValue Subtract(TinyUnityParser.AddExpressionContext context)
        {
            TinyValue lhs = this.Visit(context.expression(0));
            TinyValue rhs = this.Visit(context.expression(1));

            if (lhs == null || rhs == null)
            {
                throw new EvalException("Null operators: " + context.op.Type, context);
            }

            if (lhs.value is float && rhs.value is float)
            {
                return(new TinyValue((float)lhs.value - (float)rhs.value));
            }

            if (lhs.isList())
            {
                List <TinyValue> nlst = lhs.asList();
                nlst.Remove(rhs);
                return(new TinyValue(nlst));
            }

            throw new EvalException("Bad operators: " + context.op.Type, context);
        }