Compare commits

...

4 commits

5 changed files with 237 additions and 14 deletions

View file

@ -0,0 +1,72 @@
```
#define PHI 6.28318530718
```
morph_angle_as_degrees_to_turns_float ()
Angle ~ Degrees ~ ~ native.Float
--> Angle ~ Turns ~ ~ native.Float
```
*dst = *src / 360.0;
return 0;
```
morph_angle_as_degrees_to_turns_double ()
Angle ~ Degrees ~ ~ native.Double
--> Angle ~ Turns ~ ~ native.Double
```
*dst = *src / 360.0;
return 0;
```
morph_angle_as_turns_to_degrees_float ()
Angle ~ Turns ~ ~ native.Float
--> Angle ~ Degrees ~ ~ native.Float
```
*dst = *src * 360.0;
return 0;
```
morph_angle_as_turns_to_degrees_double ()
Angle ~ Turns ~ ~ native.Double
--> Angle ~ Degrees ~ ~ native.Double
```
*dst = *src * 360.0;
return 0;
```
morph_angle_as_radians_to_turns_float ()
Angle ~ Radians ~ ~ native.Float
--> Angle ~ Turns ~ ~ native.Float
```
*dst = *src / PHI;
return 0;
```
morph_angle_as_radians_to_turns_double ()
Angle ~ Radians ~ ~ native.Double
--> Angle ~ Turns ~ ~ native.Double
```
*dst = *src / PHI;
return 0;
```
morph_angle_as_turns_to_radians_float ()
Angle ~ Turns ~ ~ native.Float
--> Angle ~ Radians ~ ~ native.Float
```
*dst = *src * PHI;
return 0;
```
morph_angle_as_degrees_to_radians_double ()
Angle ~ Turns ~ ~ native.Double
--> Angle ~ Radians ~ ~ native.Double
```
*dst = *src * PHI;
return 0;
```

View file

@ -14,9 +14,9 @@ morph_nat_as_u64_to_pos ()
return 0;
```
morph_nat_as_u64_to_pos ()
morph_nat_as_pos_to_u64 (Endianness:Type)
~ <PosInt 0 LittleEndian>
~ <PosInt 0 Endianness>
~ <Seq~<LengthPrefix x86.UInt64> <Digit 0>~x86.UInt64>
-->
~ x86.UInt64
@ -42,14 +42,16 @@ morph_posint_radix_le (SrcRadix:, DstRadix:)
length_prefix_uint64_array_clear( dst );
if( DstRadix == 0 ) {
#if DstRadix==0
length_prefix_uint64_array_push( dst, value );
} else if( DstRadix > 0 ) {
while( value > 0 ) {
#else
if( value == 0 ) {
length_prefix_uint64_array_push( dst, 0 );
} else while( value > 0 ) {
length_prefix_uint64_array_push( dst, value % DstRadix );
value /= DstRadix;
}
}
#endif
return 0;
```
@ -69,13 +71,16 @@ morph_posint_radix_be (SrcRadix:, DstRadix:)
value += src->items[i];
}
if( DstRadix == 0 ) {
#if DstRadix==0
dst->len = 1;
dst->items[0] = value;
} else {
#else
uint64_t v = value;
dst->len = 0;
while( v ) {
if( v == 0 ) {
dst->len = 1;
} else while( v ) {
dst->len++;
v /= DstRadix;
}
@ -85,7 +90,7 @@ morph_posint_radix_be (SrcRadix:, DstRadix:)
dst->items[--i] = value % DstRadix;
value /= DstRadix;
}
}
#endif
return 0;
```

View file

@ -0,0 +1,105 @@
```
#include <stdio.h>
```
morph_real_as_decimalstr_to_float ()
~ <PosInt 10 BigEndian> ~ <Seq~<ValueTerminated 0> <Digit 10>~Char~Ascii~x86.UInt8>
--> ~ native.Float
```
sscanf(src, "%f", dst);
return 0;
```
morph_real_as_decimalstr_to_double ()
~ <PosInt 10 BigEndian> ~ <Seq~<ValueTerminated 0> <Digit 10>~Char~Ascii~x86.UInt8>
--> ~ native.Double
```
sscanf(src, "%lf", dst);
return 0;
```
morph_real_as_float_to_decimalstr ()
~ native.Float
--> ~ <PosInt 10 BigEndian> ~ <Seq~<ValueTerminated 0> <Digit 10>~Char~Ascii~x86.UInt8>
```
sprintf(dst, "%f", *src);
return 0;
```
morph_real_as_double_to_decimalstr ()
~ native.Double
--> ~ <PosInt 10 BigEndian> ~ <Seq~<ValueTerminated 0> <Digit 10>~Char~Ascii~x86.UInt8>
```
sprintf(dst, "%f", *src);
return 0;
```
morph_real_as_float_to_double ()
~ native.Float
--> ~ native.Double
```
*dst = *src;
return 0;
```
morph_real_as_double_to_float ()
~ native.Double
--> ~ native.Float
```
fprintf(stderr, "Warning: morphin Double -> Float. Precision loss!");
*dst = *src;
return 0;
```
morph_real_as_u64_to_float ()
~ ~ x86.UInt64
--> ~ native.Float
```
fprintf(stderr, "Warning: morphin UInt64 -> Float. Precision loss!");
*dst = *src;
return 0;
```
morph_real_as_u64_to_double ()
~ ~ x86.UInt64
--> ~ native.Double
```
fprintf(stderr, "Warning: morphin UInt64 -> Double. Precision loss!");
*dst = *src;
return 0;
```
morph_real_as_quantized_linear_to_float (Begin: , End: , Steps: )
~ <QuantizedLinear Begin End Steps> ~ ~ x86.UInt64
--> ~ native.Float
```
*dst = (float)Begin + ( *src * ((float)End - (float)Begin) ) / (float)Steps;
return 0;
```
morph_real_as_float_to_quantized_linear (Begin: , End: , Steps: )
~ native.Float
--> ~ <QuantizedLinear Begin End Steps> ~ ~ x86.UInt64
```
*dst = ((*src - (float)Begin) * (float)Steps) / ((float)End - (float)Begin);
return 0;
```
morph_real_as_quantized_linear_to_double (Begin: , End: , Steps: )
~ <QuantizedLinear Begin End Steps> ~ ~ x86.UInt64
--> ~ native.Double
```
*dst = (double)Begin + ( *src * ((double)End - (double)Begin) ) / (double)Steps;
return 0;
```
morph_real_as_double_to_quantized_linear (Begin: , End: , Steps: )
~ native.Double
--> ~ <QuantizedLinear Begin End Steps> ~ ~ x86.UInt64
```
*dst = ((*src - (double)Begin) * (double)Steps) / ((double)End - (double)Begin);
return 0;
```

View file

@ -0,0 +1,34 @@
```
```
morph_celsius_to_kelvin ()
Temperature ~ Celsius ~ ~ native.Float
--> Temperature ~ Kelvin ~ ~ native.Float
```
*dst = *src + 273.15;
return 0;
```
morph_kelvin_to_celsius ()
Temperature ~ Kelvin ~ ~ native.Float
--> Temperature ~ Celsius ~ ~ native.Float
```
*dst = *src - 273.15;
return 0;
```
morph_celsius_to_fahrenheit ()
Temperature ~ Celsius ~ ~ native.Float
--> Temperature ~ Fahrenheit ~ ~ native.Float
```
*dst = (*src * 9.0 / 5.0) + 32.0;
return 0;
```
morph_fahrenheit_to_celsius ()
Temperature ~ Fahrenheit ~ ~ native.Float
--> Temperature ~ Celsius ~ ~ native.Float
```
*dst = (*src - 32.0) * 5.0 / 9.0;
return 0;
```

View file

@ -30,6 +30,10 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_
Some("uint32_t".into())
} else if t == &dict.parse("x86.UInt64").expect("parse") {
Some("uint64_t".into())
} else if t == &dict.parse("native.Float").expect("parse") {
Some("float".into())
} else if t == &dict.parse("native.Double").expect("parse") {
Some("double".into())
} else {
match t {
laddertypes::TypeTerm::App(args) => {
@ -158,15 +162,18 @@ pub fn generate_main(type_dict: &mut impl TypeDict, path: Vec<MorphismInstance<L
println!(r#"
int main() {{
uint8_t bufA[1024];
uint8_t bufB[1024];
uint8_t bufA[128];
uint8_t bufB[128];
memset(bufA, 0, sizeof(bufA));
memset(bufB, 0, sizeof(bufB));
char in_str[] = "read :: {} \n";
char out_str[]= "write:: {} \n";
write(2, in_str, strlen(in_str));
write(2, out_str, strlen(out_str));
int l = read(0, bufA, 1024);
int l = read(0, bufA, sizeof(bufA));
fprintf(stderr, "read %d bytes\n", l);
"#,
@ -217,7 +224,7 @@ printf("%s\n", {});"#, out_buf);
println!(r#"
write(1, {}, {});"#,
out_buf,
1024
"sizeof(bufA)"
);
}