public override int VisitExpTypeConversion(YarnSpinnerParser.ExpTypeConversionContext context)
        {
            // Evaluate the single expression, and then call the type-conversion function
            Visit(context.expression());

            // Push the number of parameters onto the stack
            compiler.Emit(OpCode.PushFloat, new Operand(1));

            string functionName;

            switch (context.type().typename.Type)
            {
            case YarnSpinnerLexer.TYPE_STRING:
                functionName = "string";
                break;

            case YarnSpinnerLexer.TYPE_BOOL:
                functionName = "bool";
                break;

            case YarnSpinnerLexer.TYPE_NUMBER:
                functionName = "number";
                break;

            default:
                throw new ParseException($"Cannot convert {context.expression().GetText()} to {context.type().GetText()}: unknown type");
            }

            // Call the function
            compiler.Emit(OpCode.CallFunc, new Operand(functionName));

            return(0);
        }
Beispiel #2
0
        public override Yarn.Type VisitExpTypeConversion(YarnSpinnerParser.ExpTypeConversionContext context)
        {
            // Validate the type of the expression; the actual conversion
            // will be done at runtime, and may fail depending on the
            // actual value
            Visit(context.expression());

            // Return a value whose type depends on which type we're using
            switch (context.type().typename.Type)
            {
            case YarnSpinnerLexer.TYPE_NUMBER:
                return(Yarn.Type.Number);

            case YarnSpinnerLexer.TYPE_STRING:
                return(Yarn.Type.String);

            case YarnSpinnerLexer.TYPE_BOOL:
                return(Yarn.Type.Bool);

            default:
                throw new ArgumentOutOfRangeException($"Unsupported type {context.type().GetText()}");
            }
        }