예제 #1
0
    public void DetectTypeCode(VeinTypeCode code, object value)
    {
        var str    = value.ToString();
        var result =
            FieldDeclaratorSyntax.RedefineIntegerExpression(new UndefinedIntegerNumericLiteral(str)
                                                            .SetPos(new Position(0, 0, 0), 0), false);

        Assert.AreEqual(code, result.GetTypeCode());
    }
예제 #2
0
        public unsafe void DIV_Test(OpCodeValue op, VeinTypeCode code)
        {
            using var ctx = CreateContext();
            var result = ctx.Execute((gen, _) =>
            {
                gen.Emit(OpCodes.all[op]);
                gen.Emit(OpCodes.all[op]);
                gen.Emit(OpCodes.DIV);
                gen.Emit(OpCodes.RET);
            });

            Validate(result);
            Assert.AreEqual(code, (*result.returnValue).type);
            Assert.AreEqual(5 / 5, (*result.returnValue).data.l);
        }
예제 #3
0
        public static Func <string, object> GetConverter(this VeinTypeCode code)
        {
            Func <string, object> result = (code) switch
            {
                (TYPE_BOOLEAN) => (x) => bool.Parse(x),
                (TYPE_CHAR) => (x) => char.Parse(x),
                (TYPE_I1) => (x) => sbyte.Parse(x),
                (TYPE_I2) => (x) => short.Parse(x),
                (TYPE_I4) => (x) => int.Parse(x),
                (TYPE_I8) => (x) => long.Parse(x),
                (TYPE_U1) => (x) => byte.Parse(x),
                (TYPE_U2) => (x) => ushort.Parse(x),
                (TYPE_U4) => (x) => uint.Parse(x),
                (TYPE_U8) => (x) => ulong.Parse(x),
                (TYPE_R2) => (x) => Half.Parse(x),
                (TYPE_R4) => (x) => float.Parse(x),
                (TYPE_R8) => (x) => double.Parse(x),
                (TYPE_R16) => (x) => decimal.Parse(x),
                (TYPE_STRING) => (x) => x,
                _ => throw new NotSupportedException($"Cannot fetch converter for {code}.")
            };

            return(WrapConverter(result, code));
        }
예제 #4
0
 public void validate(CallFrame frame, VeinTypeCode typeCode) =>
 VM.Assert(type == VeinTypeCode.TYPE_ARRAY, WNE.TYPE_MISMATCH,
           $"stack type mismatch, current: '{type}', expected: '{typeCode}'. opcode: '{frame.last_ip}'", frame);
예제 #5
0
 public void TypeCodeValidTest(Type t, VeinTypeCode code)
 => Assert.AreEqual(code, CreateExpressionByType(t).GetTypeCode());
예제 #6
0
 public void CompatibleTrue(VeinTypeCode variable, VeinTypeCode value)
 => Assert.True(variable.IsCompatibleNumber(value));
 public ConvertNotSupportedException(VeinTypeCode typeCode)
     : base($"Cannot get converted, '{typeCode}' is not supported.")
 {
 }
예제 #8
0
 public static RuntimeIshtarClass AsRuntimeClass(this VeinTypeCode code)
 => (RuntimeIshtarClass)code.AsClass();
예제 #9
0
        public static ExpressionSyntax Const <T>(VeinTypeCode code, T value)
        {
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            var str = value.ToString() ?? throw new ArgumentNullException(nameof(value));

            switch (code)
            {
            case VeinTypeCode.TYPE_NONE:
            case VeinTypeCode.TYPE_VOID:
            case VeinTypeCode.TYPE_OBJECT:
            case VeinTypeCode.TYPE_CHAR:
            case VeinTypeCode.TYPE_CLASS:
            case VeinTypeCode.TYPE_ARRAY:
                throw new NotImplementedException();

            case VeinTypeCode.TYPE_BOOLEAN:
                return(new BoolLiteralExpressionSyntax(str.ToLowerInvariant()));

            case VeinTypeCode.TYPE_I1:
                return(new SByteLiteralExpressionSyntax(sbyte.Parse(str)));

            case VeinTypeCode.TYPE_U1:
                return(new ByteLiteralExpressionSyntax(byte.Parse(str)));

            case VeinTypeCode.TYPE_I2:
                return(new Int16LiteralExpressionSyntax(short.Parse(str)));

            case VeinTypeCode.TYPE_U2:
                return(new UInt16LiteralExpressionSyntax(ushort.Parse(str)));

            case VeinTypeCode.TYPE_I4:
                return(new Int32LiteralExpressionSyntax(int.Parse(str)));

            case VeinTypeCode.TYPE_U4:
                return(new UInt32LiteralExpressionSyntax(uint.Parse(str)));

            case VeinTypeCode.TYPE_I8:
                return(new Int64LiteralExpressionSyntax(long.Parse(str)));

            case VeinTypeCode.TYPE_U8:
                return(new UInt64LiteralExpressionSyntax(ulong.Parse(str)));

            case VeinTypeCode.TYPE_R2:
                return(new HalfLiteralExpressionSyntax(float.Parse(str)));

            case VeinTypeCode.TYPE_R4:
                return(new SingleLiteralExpressionSyntax(float.Parse(str)));

            case VeinTypeCode.TYPE_R8:
                return(new DoubleLiteralExpressionSyntax(double.Parse(str)));

            case VeinTypeCode.TYPE_R16:
                return(new DecimalLiteralExpressionSyntax(decimal.Parse(str)));

            case VeinTypeCode.TYPE_STRING:
                return(new StringLiteralExpressionSyntax(str));

            default:
                throw new ArgumentOutOfRangeException(nameof(code), code, null);
            }
        }
예제 #10
0
 private static Func <string, object> WrapConverter(Func <string, object> actor, VeinTypeCode typeCode) => x =>
 {
     try
     {
         return(actor(x));
     }
     catch (OverflowException e)
     {
         throw new ValueWasIncorrectException(x, typeCode, e);
     }
 };
예제 #11
0
 public ValueWasIncorrectException(string value, VeinTypeCode typeCode, Exception inner)
     : base($"Value: '{value}', Type: '{typeCode}', {inner.Message}", inner)
 {
 }