public RecordStructType(string modulePrefix, string localName, string description, Type runtimeType, IEnumerable <RecordStructField> fields, OperatorCollection allowedPrefixOperators, OperatorCollection allowedSuffixOperators, Dictionary <string, IMethodInvokeFactory> allowedMethods, Dictionary <string, IFieldAccessFactory> allowedFields, ConstructorInfo constructor = null) : base(allowedSuffixOperators) { this.modulePrefix = modulePrefix; this.localName = localName; Description = description; this.runtimeType = runtimeType; this.allowedPrefixOperators = allowedPrefixOperators; DeclaredMethods = allowedMethods; this.allowedFields = allowedFields; this.constructor = constructor; itemTypes = new SortedDictionary <string, TO2Type>(); this.fields = new SortedDictionary <string, FieldInfo>(); foreach (var f in fields) { itemTypes.Add(f.name, f.type); this.fields.Add(f.name, f.field); this.allowedFields.Add(f.name, new BoundFieldAccessFactory(f.description, () => f.type, runtimeType, f.field)); } }
public ResultType(TO2Type successType, TO2Type errorType) { this.successType = successType; this.errorType = errorType; allowedSuffixOperators = new OperatorCollection { { Operator.Unwrap, new ResultUnwrapOperator(this) } }; DeclaredFields = new Dictionary <string, IFieldAccessFactory> { { "success", new ResultFieldAccess(this, ResultField.Success) }, { "value", new ResultFieldAccess(this, ResultField.Value) }, { "error", new ResultFieldAccess(this, ResultField.Error) } }; }
public OptionType(TO2Type elementType) { this.elementType = elementType; allowedSuffixOperators = new OperatorCollection { { Operator.BitOr, new OptionBitOrOperator(this) }, { Operator.Unwrap, new OptionUnwrapOperator(this) } }; DeclaredMethods = new Dictionary <string, IMethodInvokeFactory> { { "map", new OptionMapFactory(this) }, { "then", new OptionThenFactory(this) }, { "ok_or", new OptionOkOrFactory(this) } }; DeclaredFields = new Dictionary <string, IFieldAccessFactory> { { "defined", new OptionFieldAccess(this, OptionField.Defined) }, { "value", new OptionFieldAccess(this, OptionField.Value) } }; }
internal TO2Bool() { allowedPrefixOperators = new OperatorCollection { { Operator.Not, new DirectOperatorEmitter(() => Unit, () => Bool, OpCodes.Ldc_I4_0, OpCodes.Ceq) } }; allowedSuffixOperators = new OperatorCollection { { Operator.Eq, new DirectOperatorEmitter(() => Bool, () => Bool, OpCodes.Ceq) }, { Operator.NotEq, new DirectOperatorEmitter(() => Bool, () => Bool, OpCodes.Ldc_I4_0, OpCodes.Ceq) }, { Operator.BoolAnd, new DirectOperatorEmitter(() => Bool, () => Bool, OpCodes.And) }, { Operator.BoolOr, new DirectOperatorEmitter(() => Bool, () => Bool, OpCodes.Or) } }; DeclaredMethods = new Dictionary <string, IMethodInvokeFactory> { { "to_string", new BoundMethodInvokeFactory("Convert boolean to string", true, () => String, () => new List <RealizedParameter>(), false, typeof(FormatUtils), typeof(FormatUtils).GetMethod("BoolToString")) } }; DeclaredFields = new Dictionary <string, IFieldAccessFactory> { { "to_int", new InlineFieldAccessFactory("Value converted to integer (false -> 0, true -> 1)", () => Int, OpCodes.Conv_I8) }, { "to_float", new InlineFieldAccessFactory("Value converted to float (false -> 0.0, true -> 1.0)", () => Float, OpCodes.Conv_R8) }, }; }
public BoundType(string modulePrefix, string localName, string description, Type runtimeType, OperatorCollection allowedPrefixOperators, OperatorCollection allowedSuffixOperators, IEnumerable <(string name, IMethodInvokeFactory invoker)> allowedMethods,
internal TO2Int() { allowedPrefixOperators = new OperatorCollection { { Operator.Neg, new DirectOperatorEmitter(() => Unit, () => Int, OpCodes.Neg) }, }; allowedSuffixOperators = new OperatorCollection { { Operator.Add, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Add) }, { Operator.AddAssign, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Add) }, { Operator.Sub, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Sub) }, { Operator.SubAssign, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Sub) }, { Operator.Mul, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Mul) }, { Operator.MulAssign, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Mul) }, { Operator.Div, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Div) }, { Operator.DivAssign, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Div) }, { Operator.Mod, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Rem) }, { Operator.ModAssign, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Rem) }, { Operator.BitOr, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Or) }, { Operator.BitOrAssign, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Or) }, { Operator.BitAnd, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.And) }, { Operator.BitAndAssign, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.And) }, { Operator.BitXor, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Xor) }, { Operator.BitXorAssign, new DirectOperatorEmitter(() => Int, () => Int, OpCodes.Xor) }, { Operator.Eq, new DirectOperatorEmitter(() => Int, () => Bool, OpCodes.Ceq) }, { Operator.NotEq, new DirectOperatorEmitter(() => Int, () => Bool, OpCodes.Ceq, OpCodes.Ldc_I4_0, OpCodes.Ceq) }, { Operator.Gt, new DirectOperatorEmitter(() => Int, () => Bool, OpCodes.Cgt) }, { Operator.Lt, new DirectOperatorEmitter(() => Int, () => Bool, OpCodes.Clt) }, { Operator.Ge, new DirectOperatorEmitter(() => Int, () => Bool, OpCodes.Clt, OpCodes.Ldc_I4_0, OpCodes.Ceq) }, { Operator.Le, new DirectOperatorEmitter(() => Int, () => Bool, OpCodes.Cgt, OpCodes.Ldc_I4_0, OpCodes.Ceq) }, }; DeclaredMethods = new Dictionary <string, IMethodInvokeFactory> { { "to_string", new BoundMethodInvokeFactory("Convert integer to string", true, () => String, () => new List <RealizedParameter>(), false, typeof(FormatUtils), typeof(FormatUtils).GetMethod("IntToString")) } }; DeclaredFields = new Dictionary <string, IFieldAccessFactory> { { "to_bool", new InlineFieldAccessFactory("Value converted to bool (0 -> false, != 0 -> true)", () => Bool, OpCodes.Conv_I4) }, { "to_float", new InlineFieldAccessFactory("Value converted to float", () => Float, OpCodes.Conv_R8) }, { "abs", new BoundPropertyLikeFieldAccessFactory("Absolute value", () => Int, typeof(Math), typeof(Math).GetMethod("Abs", new[] { typeof(long) }), null) }, { "sign", new BoundPropertyLikeFieldAccessFactory("Sign of the value (< 0 -> -1, 0 -> 0, > 0 -> 1)", () => Int, typeof(Math), typeof(Math).GetMethod("Sign", new[] { typeof(long) }), null, OpCodes.Conv_I8) }, }; }
internal TO2Float() { allowedPrefixOperators = new OperatorCollection { { Operator.Neg, new DirectOperatorEmitter(() => Unit, () => Float, OpCodes.Neg) }, { Operator.Add, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Add) }, { Operator.Sub, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Sub) }, { Operator.Mul, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Mul) }, { Operator.Div, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Div) }, }; allowedSuffixOperators = new OperatorCollection { { Operator.Add, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Add) }, { Operator.AddAssign, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Add) }, { Operator.Sub, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Sub) }, { Operator.SubAssign, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Sub) }, { Operator.Mul, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Mul) }, { Operator.MulAssign, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Mul) }, { Operator.Div, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Div) }, { Operator.DivAssign, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Div) }, { Operator.Mod, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Rem) }, { Operator.ModAssign, new DirectOperatorEmitter(() => Float, () => Float, OpCodes.Rem) }, { Operator.Eq, new DirectOperatorEmitter(() => Float, () => Bool, OpCodes.Ceq) }, { Operator.NotEq, new DirectOperatorEmitter(() => Float, () => Bool, OpCodes.Ceq, OpCodes.Ldc_I4_0, OpCodes.Ceq) }, { Operator.Gt, new DirectOperatorEmitter(() => Float, () => BuiltinType.Bool, OpCodes.Cgt) }, { Operator.Lt, new DirectOperatorEmitter(() => Float, () => BuiltinType.Bool, OpCodes.Clt) }, { Operator.Ge, new DirectOperatorEmitter(() => Float, () => BuiltinType.Bool, OpCodes.Clt, OpCodes.Ldc_I4_0, OpCodes.Ceq) }, { Operator.Le, new DirectOperatorEmitter(() => Float, () => BuiltinType.Bool, OpCodes.Cgt, OpCodes.Ldc_I4_0, OpCodes.Ceq) }, { Operator.BitXor, new StaticMethodOperatorEmitter(() => Float, () => Float, typeof(Math).GetMethod("Pow")) }, }; DeclaredMethods = new Dictionary <string, IMethodInvokeFactory> { { "to_string", new BoundMethodInvokeFactory("Convert the float to string.", true, () => String, () => new List <RealizedParameter>(), false, typeof(FormatUtils), typeof(FormatUtils).GetMethod("FloatToString")) }, { "to_fixed", new BoundMethodInvokeFactory("Convert the float to string with fixed number of `decimals`.", true, () => String, () => new List <RealizedParameter>() { new RealizedParameter("decimals", BuiltinType.Int) }, false, typeof(FormatUtils), typeof(FormatUtils).GetMethod("FloatToFixed")) } }; DeclaredFields = new Dictionary <string, IFieldAccessFactory> { { "to_int", new InlineFieldAccessFactory("Value converted to int (will be truncated as necessary)", () => Int, OpCodes.Conv_I8) }, { "abs", new BoundPropertyLikeFieldAccessFactory("Absolute value", () => Float, typeof(Math), typeof(Math).GetMethod("Abs", new[] { typeof(double) }), null) }, { "sign", new BoundPropertyLikeFieldAccessFactory("Sign of the value (< 0 -> -1, 0 -> 0, > 0 -> 1)", () => Int, typeof(Math), typeof(Math).GetMethod("Sign", new[] { typeof(double) }), null, OpCodes.Conv_I8) }, { "is_nan", new BoundPropertyLikeFieldAccessFactory("Check if float is not a number", () => Bool, typeof(Double), typeof(Double).GetMethod("IsNaN", new[] { typeof(double) }), null) }, { "is_infinity", new BoundPropertyLikeFieldAccessFactory("Check if float is infinity", () => Bool, typeof(Double), typeof(Double).GetMethod("IsInfinity", new[] { typeof(double) }), null) }, { "is_finite", new BoundPropertyLikeFieldAccessFactory("Check if float is finite", () => Bool, typeof(Double), typeof(Double).GetMethod("IsFinite", new[] { typeof(double) }), null) } }; intToFloatAssign = new IntToFloatAssign(); }
internal TO2SString() { allowedOperators = new OperatorCollection { { Operator.Add, new StaticMethodOperatorEmitter(() => String, () => String, typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) })) }, { Operator.AddAssign, new StaticMethodOperatorEmitter(() => String, () => String, typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) })) }, { Operator.Eq, new StaticMethodOperatorEmitter(() => String, () => Bool, typeof(string).GetMethod("Equals", new[] { typeof(string), typeof(string) })) }, { Operator.NotEq, new StaticMethodOperatorEmitter(() => String, () => Bool, typeof(string).GetMethod("Equals", new[] { typeof(string), typeof(string) }), OpCodes.Ldc_I4_0, OpCodes.Ceq) }, { Operator.Gt, new StaticMethodOperatorEmitter(() => String, () => Bool, typeof(string).GetMethod("Compare", new[] { typeof(string), typeof(string) }), OpCodes.Ldc_I4_0, OpCodes.Cgt) }, { Operator.Ge, new StaticMethodOperatorEmitter(() => String, () => Bool, typeof(string).GetMethod("Compare", new[] { typeof(string), typeof(string) }), OpCodes.Ldc_I4_M1, OpCodes.Cgt) }, { Operator.Lt, new StaticMethodOperatorEmitter(() => String, () => Bool, typeof(string).GetMethod("Compare", new[] { typeof(string), typeof(string) }), OpCodes.Ldc_I4_0, OpCodes.Clt) }, { Operator.Le, new StaticMethodOperatorEmitter(() => String, () => Bool, typeof(string).GetMethod("Compare", new[] { typeof(string), typeof(string) }), OpCodes.Ldc_I4_1, OpCodes.Clt) }, }; DeclaredMethods = new Dictionary <string, IMethodInvokeFactory> { { "repeat", new BoundMethodInvokeFactory("Repeat the string `count` number of time", true, () => String, () => new List <RealizedParameter> { new RealizedParameter("count", BuiltinType.Int) }, false, typeof(FormatUtils), typeof(FormatUtils).GetMethod("StringRepeat")) }, { "pad_left", new BoundMethodInvokeFactory("Pad the string to `length` by filling spaces from the left side", true, () => String, () => new List <RealizedParameter> { new RealizedParameter("length", BuiltinType.Int) }, false, typeof(FormatUtils), typeof(FormatUtils).GetMethod("StringPadLeft")) }, { "pad_right", new BoundMethodInvokeFactory("Pad the string to `length` by filling spaces from the right side", true, () => String, () => new List <RealizedParameter> { new RealizedParameter("length", BuiltinType.Int) }, false, typeof(FormatUtils), typeof(FormatUtils).GetMethod("StringPadRight")) }, { "contains", new BoundMethodInvokeFactory("Check if the string contains a sub string `other`", true, () => Bool, () => new List <RealizedParameter> { new RealizedParameter("other", BuiltinType.String) }, false, typeof(string), typeof(string).GetMethod("Contains", new[] { typeof(string) })) }, { "starts_with", new BoundMethodInvokeFactory("Check if the string starts with `other`", true, () => Bool, () => new List <RealizedParameter> { new RealizedParameter("other", BuiltinType.String) }, false, typeof(string), typeof(string).GetMethod("StartsWith", new[] { typeof(string) })) }, { "ends_with", new BoundMethodInvokeFactory("Check if the string ends with `other`", true, () => Bool, () => new List <RealizedParameter> { new RealizedParameter("other", BuiltinType.String) }, false, typeof(string), typeof(string).GetMethod("EndsWith", new[] { typeof(string) })) }, { "to_lower", new BoundMethodInvokeFactory("Convert string to lower case", true, () => String, () => new List <RealizedParameter>(), false, typeof(string), typeof(string).GetMethod("ToLowerInvariant")) }, { "to_upper", new BoundMethodInvokeFactory("Convert string to upper case", true, () => String, () => new List <RealizedParameter>(), false, typeof(string), typeof(string).GetMethod("ToUpperInvariant")) }, }; DeclaredFields = new Dictionary <string, IFieldAccessFactory> { { "length", new BoundPropertyLikeFieldAccessFactory( "Length of the string, i.e. number of characters in the string", () => Int, typeof(String), typeof(String).GetProperty("Length"), OpCodes.Conv_I8) }, }; }