diff --git a/morphisms/angle.morphism-base b/morphisms/angle.morphism-base index 71aba96..5fa3baf 100644 --- a/morphisms/angle.morphism-base +++ b/morphisms/angle.morphism-base @@ -3,16 +3,16 @@ ``` morph_angle_as_degrees_to_turns_float () - Angle ~ Degrees ~ ℝ ~ native.Float ---> Angle ~ Turns ~ ℝ ~ native.Float + Angle ~ Degrees ~ ℝ ~ native.Float32 +--> Angle ~ Turns ~ ℝ ~ native.Float32 ``` *dst = *src / 360.0; return 0; ``` morph_angle_as_degrees_to_turns_double () - Angle ~ Degrees ~ ℝ ~ native.Double ---> Angle ~ Turns ~ ℝ ~ native.Double + Angle ~ Degrees ~ ℝ ~ native.Float64 +--> Angle ~ Turns ~ ℝ ~ native.Float64 ``` *dst = *src / 360.0; return 0; @@ -20,16 +20,16 @@ morph_angle_as_degrees_to_turns_double () morph_angle_as_turns_to_degrees_float () - Angle ~ Turns ~ ℝ ~ native.Float ---> Angle ~ Degrees ~ ℝ ~ native.Float + Angle ~ Turns ~ ℝ ~ native.Float32 +--> Angle ~ Degrees ~ ℝ ~ native.Float32 ``` *dst = *src * 360.0; return 0; ``` morph_angle_as_turns_to_degrees_double () - Angle ~ Turns ~ ℝ ~ native.Double ---> Angle ~ Degrees ~ ℝ ~ native.Double + Angle ~ Turns ~ ℝ ~ native.Float64 +--> Angle ~ Degrees ~ ℝ ~ native.Float64 ``` *dst = *src * 360.0; return 0; @@ -39,16 +39,16 @@ morph_angle_as_turns_to_degrees_double () morph_angle_as_radians_to_turns_float () - Angle ~ Radians ~ ℝ ~ native.Float ---> Angle ~ Turns ~ ℝ ~ native.Float + Angle ~ Radians ~ ℝ ~ native.Float32 +--> Angle ~ Turns ~ ℝ ~ native.Float32 ``` *dst = *src / PHI; return 0; ``` morph_angle_as_radians_to_turns_double () - Angle ~ Radians ~ ℝ ~ native.Double ---> Angle ~ Turns ~ ℝ ~ native.Double + Angle ~ Radians ~ ℝ ~ native.Float64 +--> Angle ~ Turns ~ ℝ ~ native.Float64 ``` *dst = *src / PHI; return 0; @@ -56,16 +56,16 @@ morph_angle_as_radians_to_turns_double () morph_angle_as_turns_to_radians_float () - Angle ~ Turns ~ ℝ ~ native.Float ---> Angle ~ Radians ~ ℝ ~ native.Float + Angle ~ Turns ~ ℝ ~ native.Float32 +--> Angle ~ Radians ~ ℝ ~ native.Float32 ``` *dst = *src * PHI; return 0; ``` morph_angle_as_degrees_to_radians_double () - Angle ~ Turns ~ ℝ ~ native.Double ---> Angle ~ Radians ~ ℝ ~ native.Double + Angle ~ Turns ~ ℝ ~ native.Float64 +--> Angle ~ Radians ~ ℝ ~ native.Float64 ``` *dst = *src * PHI; return 0; diff --git a/morphisms/real.morphism-base b/morphisms/real.morphism-base index 3302f80..4add278 100644 --- a/morphisms/real.morphism-base +++ b/morphisms/real.morphism-base @@ -4,7 +4,7 @@ morph_real_as_decimalstr_to_float () ℝ ~ <PosInt 10 BigEndian> ~ <Seq~<ValueTerminated 0> <Digit 10>~Char~Ascii~native.UInt8> ---> ℝ ~ native.Float +--> ℝ ~ native.Float32 ``` sscanf(src, "%f", dst); return 0; @@ -12,14 +12,14 @@ morph_real_as_decimalstr_to_float () morph_real_as_decimalstr_to_double () ℝ ~ <PosInt 10 BigEndian> ~ <Seq~<ValueTerminated 0> <Digit 10>~Char~Ascii~native.UInt8> ---> ℝ ~ native.Double +--> ℝ ~ native.Float64 ``` sscanf(src, "%lf", dst); return 0; ``` morph_real_as_float_to_decimalstr () - ℝ ~ native.Float + ℝ ~ native.Float32 --> ℝ ~ <PosInt 10 BigEndian> ~ <Seq~<ValueTerminated 0> <Digit 10>~Char~Ascii~native.UInt8> ``` sprintf(dst, "%f", *src); @@ -27,7 +27,7 @@ morph_real_as_float_to_decimalstr () ``` morph_real_as_double_to_decimalstr () - ℝ ~ native.Double + ℝ ~ native.Float64 --> ℝ ~ <PosInt 10 BigEndian> ~ <Seq~<ValueTerminated 0> <Digit 10>~Char~Ascii~native.UInt8> ``` sprintf(dst, "%f", *src); @@ -36,16 +36,16 @@ morph_real_as_double_to_decimalstr () morph_real_as_float_to_double () - ℝ ~ native.Float ---> ℝ ~ native.Double + ℝ ~ native.Float32 +--> ℝ ~ native.Float64 ``` *dst = *src; return 0; ``` morph_real_as_double_to_float () - ℝ ~ native.Double ---> ℝ ~ native.Float + ℝ ~ native.Float64 +--> ℝ ~ native.Float32 ``` fprintf(stderr, "Warning: morphin Double -> Float. Precision loss!"); *dst = *src; @@ -54,7 +54,7 @@ morph_real_as_double_to_float () morph_real_as_u64_to_float () ℝ ~ ℕ ~ native.UInt64 ---> ℝ ~ native.Float +--> ℝ ~ native.Float32 ``` fprintf(stderr, "Warning: morphin UInt64 -> Float. Precision loss!"); *dst = *src; @@ -63,7 +63,7 @@ morph_real_as_u64_to_float () morph_real_as_u64_to_double () ℝ ~ ℕ ~ native.UInt64 ---> ℝ ~ native.Double +--> ℝ ~ native.Float64 ``` fprintf(stderr, "Warning: morphin UInt64 -> Double. Precision loss!"); *dst = *src; @@ -72,14 +72,14 @@ morph_real_as_u64_to_double () morph_real_as_quantized_linear_to_float (Begin: ℝ, End: ℝ, Steps: ℤ) ℝ ~ <QuantizedLinear Begin End Steps> ~ ℕ ~ native.UInt64 ---> ℝ ~ native.Float +--> ℝ ~ native.Float32 ``` *dst = (float)Begin + ( *src * ((float)End - (float)Begin) ) / (float)Steps; return 0; ``` morph_real_as_float_to_quantized_linear (Begin: ℝ, End: ℝ, Steps: ℤ) - ℝ ~ native.Float + ℝ ~ native.Float32 --> ℝ ~ <QuantizedLinear Begin End Steps> ~ ℕ ~ native.UInt64 ``` *dst = ((*src - (float)Begin) * (float)Steps) / ((float)End - (float)Begin); @@ -90,14 +90,14 @@ morph_real_as_float_to_quantized_linear (Begin: ℝ, End: ℝ, Steps: ℤ) morph_real_as_quantized_linear_to_double (Begin: ℝ, End: ℝ, Steps: ℤ) ℝ ~ <QuantizedLinear Begin End Steps> ~ ℕ ~ native.UInt64 ---> ℝ ~ native.Double +--> ℝ ~ native.Float64 ``` *dst = (double)Begin + ( *src * ((double)End - (double)Begin) ) / (double)Steps; return 0; ``` morph_real_as_double_to_quantized_linear (Begin: ℝ, End: ℝ, Steps: ℤ) - ℝ ~ native.Double + ℝ ~ native.Float64 --> ℝ ~ <QuantizedLinear Begin End Steps> ~ ℕ ~ native.UInt64 ``` *dst = ((*src - (double)Begin) * (double)Steps) / ((double)End - (double)Begin); diff --git a/morphisms/temperature.morphism-base b/morphisms/temperature.morphism-base index 7831292..6c682bc 100644 --- a/morphisms/temperature.morphism-base +++ b/morphisms/temperature.morphism-base @@ -2,32 +2,32 @@ ``` morph_celsius_to_kelvin () - Temperature ~ Celsius ~ ℝ ~ native.Float ---> Temperature ~ Kelvin ~ ℝ ~ native.Float + Temperature ~ Celsius ~ ℝ ~ native.Float32 +--> Temperature ~ Kelvin ~ ℝ ~ native.Float32 ``` *dst = *src + 273.15; return 0; ``` morph_kelvin_to_celsius () - Temperature ~ Kelvin ~ ℝ ~ native.Float ---> Temperature ~ Celsius ~ ℝ ~ native.Float + Temperature ~ Kelvin ~ ℝ ~ native.Float32 +--> Temperature ~ Celsius ~ ℝ ~ native.Float32 ``` *dst = *src - 273.15; return 0; ``` morph_celsius_to_fahrenheit () - Temperature ~ Celsius ~ ℝ ~ native.Float ---> Temperature ~ Fahrenheit ~ ℝ ~ native.Float + Temperature ~ Celsius ~ ℝ ~ native.Float32 +--> Temperature ~ Fahrenheit ~ ℝ ~ native.Float32 ``` *dst = (*src * 9.0 / 5.0) + 32.0; return 0; ``` morph_fahrenheit_to_celsius () - Temperature ~ Fahrenheit ~ ℝ ~ native.Float ---> Temperature ~ Celsius ~ ℝ ~ native.Float + Temperature ~ Fahrenheit ~ ℝ ~ native.Float32 +--> Temperature ~ Celsius ~ ℝ ~ native.Float32 ``` *dst = (*src - 32.0) * 5.0 / 9.0; return 0; diff --git a/platforms/json.lt b/platforms/json.lt new file mode 100644 index 0000000..8e69138 --- /dev/null +++ b/platforms/json.lt @@ -0,0 +1,100 @@ + +type UTF8-String = <Seq Char~Unicode> ~ UTF-8 ~ <Seq~<ValueTerminated 0> native.UInt8> ; + +type SerializedJson.Value = json.Value + ~ <Enum + <Null json.Null ~ SerializedJson.Null > + <String json.String ~ SerializedJson.String > + <Number json.Number ~ SerializedJson.Number > + <Bool json.Bool ~ SerializedJson.Bool > + <Array json.Array ~ SerializedJson.Array > + <Object json.Object ~ SerializedJson.Object >> + ~ <Seq Char> ; + +type SerializedJson.Null = < Struct~<Seq Char> "\"null\"" > ; + +type SerializedJson.String = <Seq Char> + ~ <Struct~<Seq Char> "\"" <Seq Char> "\""> + ~ <Seq Char> ; + +type SerializedJson.Number = ℝ + ~ <PosInt 10 BigEndian> + ~ <Seq <Digit 10> ~ Char> ; + +type SerializedJson.Bool = Bool + ~ <Enum~<Seq Char> + <True~"\"true\"" <>> + <False~"\"false\"" <>>> + ~ <Seq Char> ; + +type SerializedJson.Array = <Struct~<Seq Char> + "[" + <Seq json.Value> + ~ <ValueSep ',' Char> + ~ <Seq Char> + "]" > + ~ <Seq Char> ; + +type SerializedJson.Object = <Struct~<Seq Char> + "{" + <members <Seq + <Struct~<Seq Char> + <key json.String> + ":" + <value json.Value>>>> + "}" + > + ~ <Seq Char>; + +type parsedJson.Value = json.Value ~ <Enum~native.UInt8 + < parsedJson_Null~0 <> > + < parsedJson_String~1 <Ref UTF8-String> ~ native.Address > + < parsedJson_Int~2 ℝ~ℕ~native.UInt64 > + < parsedJson_Float~2 ℝ~native.Float64 > + < parsedJson_Bool~3 Bool ~ native.UInt8 > + + < parsedJson_Array~4 <Ref <Seq ~ <LengthPrefix native.UInt64> + parsedJsonValue + > + > + ~ native.Address > + + < parsedJson_Object~5 <Ref <Seq ~ <LengthPrefix native.UInt64> + <Struct + <key <Ref UTF8-String> ~ native.Address> + <val parsedJsonValue> + > + > + ~ native.Address >> + ~ <Struct + <tag native.UInt8> + native.UInt64 >; + +parse_json_value () + json.Value ~ UTF8-String +--> json.Value ~ parsedJsonValue +``` + + { + dst->tag = PARSED_JSON_NULL; + dst->parsedJson_Null; + } + + { + dst->tag = PARSED_JSON_STRING; + dst->parsedJson_String = malloc(len); + strncpy(dst->parsedJson_String, src, len); + src += len; + } + + { + dst->tag = PARSED_JSON_NUMBER; + + MORPH( + "ℝ ~ ℕ ~ <PosInt 10 BigEndian> ~ <Seq <Digit 10> ~ Char> ~ UTF8-String", + "ℝ ~ ℕ ~ native.UInt64", + src, + &dst->parsedJson_Number + ); + } +``` diff --git a/platforms/json5.lt b/platforms/json5.lt new file mode 100644 index 0000000..e69de29 diff --git a/platforms/protobuf.lt b/platforms/protobuf.lt new file mode 100644 index 0000000..ce273f6 --- /dev/null +++ b/platforms/protobuf.lt @@ -0,0 +1,3 @@ + +type protobuf.Varint = ℕ ~ <PosInt 128 LittleEndian> ~ <Seq~MsbCont <Digit 128> ~ ℤ_128 ~ native.UInt8> ; +type protobuf.String = <Seq Char ~ Unicode> ~ UTF-8 ~ <Seq ~ <LengthPrefix protobuf.Varint> Byte> ; diff --git a/platforms/spapod.lt b/platforms/spapod.lt new file mode 100644 index 0000000..e69de29 diff --git a/platforms/x86.lt b/platforms/x86.lt new file mode 100644 index 0000000..0348efc --- /dev/null +++ b/platforms/x86.lt @@ -0,0 +1,5 @@ + +type x86.UInt64 = ℤ_2/\64 ~ <PosInt 256 LittleEndian> ~ <Seq~<StaticLength 8> <Digit 256> ~ x86.UInt8 >; +type x86.UInt32 = ℤ_2/\32 ~ <PosInt 256 LittleEndian> ~ <Seq~<StaticLength 4> <Digit 256> ~ x86.UInt8 >; +type x86.UInt16 = ℤ_2^16 ~ <PosInt 256 LittleEndian> ~ <Seq~<StaticLength 2> <Digit 256> ~ x86.UInt8 >; +type x86.UInt8 = ℤ_256 ~ <Posint 2 BigEndian> ~ <Seq~<StaticLength 8> <Digit 2> ~ Bit>;