Compare commits

...

3 commits

26 changed files with 440 additions and 409 deletions

View file

@ -2,45 +2,45 @@
#define PHI 6.28318530718
```
morph_angle_as_degrees_to_turns_float ()
morph angle_as_degrees_to_turns_float :
Angle ~ Degrees ~ ~ native.Float32
--> Angle ~ Turns ~ ~ native.Float32
```*dst = *src / 360.0;```
= ```*dst = *src / 360.0;```
morph_angle_as_degrees_to_turns_double ()
morph angle_as_degrees_to_turns_double :
Angle ~ Degrees ~ ~ native.Float64
--> Angle ~ Turns ~ ~ native.Float64
```*dst = *src / 360.0;```
= ```*dst = *src / 360.0;```;
morph_angle_as_turns_to_degrees_float ()
morph angle_as_turns_to_degrees_float :
Angle ~ Turns ~ ~ native.Float32
--> Angle ~ Degrees ~ ~ native.Float32
```*dst = *src * 360.0;```
= ```*dst = *src * 360.0;```;
morph_angle_as_turns_to_degrees_double ()
morph angle_as_turns_to_degrees_double :
Angle ~ Turns ~ ~ native.Float64
--> Angle ~ Degrees ~ ~ native.Float64
```*dst = *src * 360.0;```
= ```*dst = *src * 360.0;```;
morph_angle_as_radians_to_turns_float ()
morph angle_as_radians_to_turns_float :
Angle ~ Radians ~ ~ native.Float32
--> Angle ~ Turns ~ ~ native.Float32
```*dst = *src / PHI;```
= ```*dst = *src / PHI;```;
morph_angle_as_radians_to_turns_double ()
morph angle_as_radians_to_turns_double :
Angle ~ Radians ~ ~ native.Float64
--> Angle ~ Turns ~ ~ native.Float64
```*dst = *src / PHI;```
= ```*dst = *src / PHI;```;
morph_angle_as_turns_to_radians_float ()
morph angle_as_turns_to_radians_float :
Angle ~ Turns ~ ~ native.Float32
--> Angle ~ Radians ~ ~ native.Float32
```*dst = *src * PHI;```
= ```*dst = *src * PHI;```;
morph_angle_as_turns_to_radians_double ()
morph angle_as_turns_to_radians_double :
Angle ~ Turns ~ ~ native.Float64
--> Angle ~ Radians ~ ~ native.Float64
```*dst = *src * PHI;```
= ```*dst = *src * PHI;```;

View file

@ -2,7 +2,7 @@
#include <math.h>
```
morph_color_as_rgb_to_hsv ()
morph color_as_rgb_to_hsv :
RgbaColor ~ RGB ~ <Struct
<red ~ native.Float32>
<green ~ native.Float32>
@ -15,7 +15,7 @@ morph_color_as_rgb_to_hsv ()
<value ~ native.Float32>
<alpha ~ native.Float32>
>
```
= ```
float max = fmaxf(fmaxf(src->red, src->green), src->blue);
float min = fminf(fminf(src->red, src->green), src->blue);
float delta = max - min;
@ -35,10 +35,10 @@ morph_color_as_rgb_to_hsv ()
dst->saturation = (max == 0) ? 0 : (delta / max);
dst->value = max;
dst->alpha = src->alpha;
```
```;
morph_color_as_hsv_to_rgb ()
morph color_as_hsv_to_rgb :
RgbaColor ~ HSV ~ <Struct
<hue Angle ~ Turns ~ ~ native.Float32>
<saturation ~ native.Float32>
@ -51,7 +51,7 @@ morph_color_as_hsv_to_rgb ()
<blue ~ native.Float32>
<alpha ~ native.Float32>
>
```
= ```
float C = src->value * src->saturation;
float X = C * (1 - fabsf(fmodf(src->hue * 6.0, 2) - 1));
float m = src->value - C;
@ -76,4 +76,4 @@ morph_color_as_hsv_to_rgb ()
dst->green = g + m;
dst->blue = b + m;
dst->alpha = src->alpha;
```
```;

View file

@ -1,16 +1,32 @@
```
```
morph_complex_as_polar_to_cartesian ()
~ Polar ~ <Struct~Aligned <phi Angle~Radians~~native.Float64> <val ~native.Float64>>
--> ~ Cartesian ~ <Struct~Aligned <x ~native.Float64> <y ~native.Float64>>
```
morph complex_as_polar_to_cartesian :
~ Polar
~ <Struct~Aligned
<phi Angle~Radians~~native.Float64>
<val ~native.Float64>
>
--> ~ Cartesian
~ <Struct~Aligned
<x ~native.Float64>
<y ~native.Float64>
>
= ```
return 0;
```
```;
morph_complex_as_cartesian_to_polar ()
~ Cartesian ~ <Struct~Aligned <x ~native.Float64> <y ~native.Float64>>
--> ~ Polar ~ <Struct~Aligned <phi Angle~Radians~~native.Float64> <val ~native.Float64>>
```
morph complex_as_cartesian_to_polar :
~ Cartesian
~ <Struct~Aligned
<x ~native.Float64>
<y ~native.Float64>
>
--> ~ Polar
~ <Struct~Aligned
<phi Angle~Radians~~native.Float64>
<val ~native.Float64>
>
= ```
return 0;
```
```;

View file

@ -1,21 +1,21 @@
```
```
morph_ref_to_val8 (T: Type) where (T<=native.UInt8)
morph ref_to_val8 : (T: Type) where (T native.UInt8)
< Ref~Ptr T >
~ native.Address
--> T~native.UInt8
```
=```
*dst = **src ;
```
```;
morph_ref_to_val16 (T: Type) where (T<=native.UInt8)
morph ref_to_val16 : ∀(T: Type) where (T<native.UInt8)
< Ref~Ptr T~native.UInt16 >
~ native.Address
--> T~native.UInt16
```
=```
*dst = **src ;
```
```;
morph_ref_to_val32 (T: Type)
< Ref~Ptr T~native.UInt32 >

View file

@ -1,11 +1,10 @@
```
#include <stdio.h>
```
morph_digit_as_char_to_uint8 (Radix:)
morph digit_as_char_to_uint8 : ∀(Radix:).
<Digit Radix> ~ Char ~ Ascii ~ native.UInt8
--> <Digit Radix> ~ ~ native.UInt8
```
--> <Digit Radix> ~ < Radix> ~ < 256> ~ native.UInt8
= ```
if( *src >= '0' && *src <= '9' )
*dst = *src - '0';
else if( *src >= 'a' && *src <= 'f')
@ -23,12 +22,12 @@ morph_digit_as_char_to_uint8 (Radix:)
fprintf(stderr, "digit %u is out of range for radix %u\n", *dst, Radix);
return -1;
}
```
```;
morph_digit_as_uint8_to_char (Radix:_16)
<Digit Radix> ~ ~ native.UInt8
morph digit_as_uint8_to_char : ∀(Radix:< 16>).
<Digit Radix> ~ < Radix> ~ < 256> ~ native.UInt8
--> <Digit Radix> ~ Char ~ Ascii ~ native.UInt8
```
= ```
if ( *src < 10 )
*dst = *src + '0';
else if( *dst < 16 )
@ -37,4 +36,4 @@ morph_digit_as_uint8_to_char (Radix:_16)
fprintf(stderr, "digit %u is out of rage for char\n", *dst);
return -1;
}
```
```;

View file

@ -1,16 +1,18 @@
```
```
morph_energy_as_wh_to_joule ()
type Joule = Energy ~ Ws;
morph energy_as_wh_to_joule :
Energy ~ Wh ~ ~ native.Float64
--> Energy ~ Joule ~ ~ native.Float64
```
--> Energy ~ Ws ~ ~ native.Float64
= ```
*dst = *src * 3600.0;
```
```;
morph_energy_as_joule_to_wh ()
Energy ~ Joule ~ ~ native.Float64
morph energy_as_joule_to_wh :
Energy ~ Ws ~ ~ native.Float64
--> Energy ~ Wh ~ ~ native.Float64
```
= ```
*dst = *src / 3600.0;
```
```;

View file

@ -3,50 +3,52 @@
#include <array/length-prefix.h>
```
morph_array_as_static_to_lenpfx (Len: , T: Type)
morph array_as_static_to_lenpfx : ∀(Len: ). ∀(T: Type).
<Seq~<StaticLength Len> T>
--> <Seq~<LengthPrefix native.UInt64> T>
```
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, clear)( dst );
= ```
//PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, clear)( dst );
length_prefix_nativeUInt64_array_##T##_clear(dst);
for( nativeUInt64 i = 0; i < Len; ++i )
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst, src->items[i] );
```
length_prefix_nativeUInt64_array_##T##_push(dst, src->items[i]);
//PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst, src->items[i] );
```;
morph_array_as_lenpfx_to_static (Len: , T: Type)
morph array_as_lenpfx_to_static : ∀(Len: ). ∀(T: Type).
<Seq~<LengthPrefix native.UInt64> T>
--> <Seq~<StaticLength Len> T>
```
= ```
nativeUInt64 i;
for( i = 0; i < Len && i < src->len; ++i )
dst->items[i] = src->items[i];
if( i < Len )
memset( &dst[i], 0, (Len-i) * sizeof(T) );
```
```;
morph_array_as_valterm_to_lenpfx (T: Type, Terminator:T)
morph array_as_valterm_to_lenpfx : ∀(T: Type). ∀(Terminator:T).
<Seq~<ValueTerminated Terminator> T>
--> <Seq~<LengthPrefix native.UInt64> T>
```
= ```
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst );
while( *src != Terminator )
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst, *src++ );
```
```;
morph_array_as_lenpfx_to_valterm (T: Type, Terminator: T)
morph array_as_lenpfx_to_valterm : ∀(T: Type). ∀(Terminator: T).
<Seq~<LengthPrefix native.UInt64> T>
--> <Seq~<ValueTerminated Terminator> T>
```
=```
for( uint64_t i = 0; i < src->len; ++i )
*dst++ = src->items[i];
*dst = Terminator;
```
```;
morph_array_as_lenpfx_to_continuation_bit (T:Type)
morph array_as_lenpfx_to_continuation_bit : ∀(T:Type).
<Seq~<LengthPrefix native.UInt64> T>
--> <Seq~MsbCont T>
```
= ```
for( uint64_t i = 0; i < src->len; ++i ) {
const size_t n_bits = 8*sizeof(T);
if( src->items[i] & ((uint64_t)1<<(n_bits-1)) ) {
@ -58,4 +60,4 @@ morph_array_as_lenpfx_to_continuation_bit (T:Type)
if( i+1 < src->len )
dst[i] |= ((uint64_t)1<<(n_bits-1));
}
```
```;

View file

@ -2,36 +2,37 @@
#include <array/length-prefix.h>
```
morph_nat_as_u64_to_pos ()
morph nat_as_u64_to_pos :
~ native.UInt64
-->
~ <PosInt 0 LittleEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit 0> ~~ native.UInt64>
```
~ <Seq~<LengthPrefix native.UInt64> <Digit 0> ~~ native.UInt64
> = ```
dst->len = 1;
dst->items[0] = *src;
```
```;
morph_nat_as_pos_to_u64 (Endianness:Type)
morph nat_as_pos_to_u64 : (Endianness:Type)
~ <PosInt 0 Endianness>
~ <Seq~<LengthPrefix native.UInt64> <Digit 0> ~~ native.UInt64>
-->
-->
~ native.UInt64
```
= ```
*dst = src->items[0];
```
```;
morph_posint_radix_le (SrcRadix:, DstRadix:)
morph posint_radix_le : ∀(SrcRadix:). ∀(DstRadix:).
~ <PosInt SrcRadix LittleEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit SrcRadix> ~~ native.UInt64>
-->
~ <PosInt DstRadix LittleEndian>
~ <Seq~<LenghtPrefix native.UInt64> <Digit DstRadix> ~~ native.UInt64>
```
= ```
uint64_t value = 0;
for( uint64_t i = 0; i < src->len; ++i ) {
@ -51,16 +52,16 @@ morph_posint_radix_le (SrcRadix:, DstRadix:)
value /= DstRadix;
}
#endif
```
```;
morph_posint_radix_be (SrcRadix:, DstRadix:)
morph posint_radix_be : ∀(SrcRadix:).∀(DstRadix:).
~ <PosInt SrcRadix BigEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit SrcRadix> ~~ native.UInt64>
-->
~ <PosInt DstRadix BigEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit DstRadix> ~~ native.UInt64>
```
= ```
uint64_t value = 0;
for( uint64_t i = 0; i < src->len; ++i ) {
@ -88,48 +89,48 @@ morph_posint_radix_be (SrcRadix:, DstRadix:)
value /= DstRadix;
}
#endif
```
```;
morph_posint_endianness_le_to_be (Radix:)
morph posint_endianness_le_to_be : ∀(Radix:).
~ <PosInt Radix LittleEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit Radix> ~~ native.UInt64>
-->
~ <PosInt Radix BigEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit Radix> ~~ native.UInt64>
```
= ```
return length_prefix_nativeUInt64_array_nativeUInt64_reverse( (void*)src, (void*)dst );
```
```;
morph_posint_endianness_be_to_le (Radix:)
morph posint_endianness_be_to_le : ∀(Radix:).
~ <PosInt Radix BigEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit Radix> ~~ native.UInt64>
-->
~ <PosInt Radix LittleEndian>
~ <Seq~<LengthPrefix native.UInt64> <Digit Radix> ~~ native.UInt64>
```
= ```
return length_prefix_nativeUInt64_array_nativeUInt64_reverse( (void*)src, (void*)dst );
```
```;
morph_posint_uint32_endianness_be_to_le ()
morph posint_uint32_endianness_be_to_le :
~ <PosInt 256 BigEndian>
~ < Seq~<StaticLength 8> <Digit 256> ~~ native.UInt8 >
~ < Seq~<StaticLength 8> <Digit 256> ~< 256>~ native.UInt8 >
-->
~ <PosInt 256 LittleEndian>
~ <Seq~<StaticLength 8> <Digit 256> ~~ native.UInt8 >
```
~ < Seq~<StaticLength 8> <Digit 256> ~< 256>~ native.UInt8 >
= ```
*dst = __builtin_bswap32(*src);
```
```;
morph_posint_uint32_endianness_le_to_be ()
morph posint_uint32_endianness_le_to_be :
~ <PosInt 256 LittleEndian>
~ < Seq~<StaticLength 8> <Digit 256> ~~ native.UInt8 >
~ < Seq~<StaticLength 8> <Digit 256> ~< 256>~ native.UInt8 >
-->
~ <PosInt 256 BigEndian>
~ <Seq~<StaticLength 8> <Digit 256> ~~ native.UInt8 >
```
~ < Seq~<StaticLength 8> <Digit 256> ~< 256>~ native.UInt8 >
= ```
*dst = __builtin_bswap32(*src);
```
```;

View file

@ -2,89 +2,87 @@
#include <stdio.h>
```
morph_real_as_decimalstr_to_float ()
morph real_as_decimalstr_to_float :
~ <PosInt 10 BigEndian> ~ <Seq~<ValueTerminated 0> <Digit 10>~Char~Ascii~native.UInt8>
--> ~ native.Float32
```sscanf(src, "%f", dst);```
= ```sscanf(src, "%f", dst);```;
morph_real_as_decimalstr_to_double ()
morph real_as_decimalstr_to_double :
~ <PosInt 10 BigEndian> ~ <Seq~<ValueTerminated 0> <Digit 10>~Char~Ascii~native.UInt8>
--> ~ native.Float64
```sscanf(src, "%lf", dst);```
= ```sscanf(src, "%lf", dst);```;
morph_real_as_float_to_decimalstr ()
morph real_as_float_to_decimalstr :
~ native.Float32
--> ~ <PosInt 10 BigEndian> ~ <Seq~<ValueTerminated 0> <Digit 10>~Char~Ascii~native.UInt8>
```sprintf(dst, "%f", *src);```
= ```sprintf(dst, "%f", *src);```;
morph_real_as_double_to_decimalstr ()
morph real_as_double_to_decimalstr :
~ native.Float64
--> ~ <PosInt 10 BigEndian> ~ <Seq~<ValueTerminated 0> <Digit 10>~Char~Ascii~native.UInt8>
```sprintf(dst, "%f", *src);```
= ```sprintf(dst, "%f", *src);```;
morph_real_as_float_to_double ()
morph real_as_float_to_double :
~ native.Float32
--> ~ native.Float64
```*dst = *src;```
= ```*dst = *src;```;
morph_real_as_double_to_float ()
morph real_as_double_to_float :
~ native.Float64
--> ~ native.Float32
```
= ```
fprintf(stderr, "Warning: morphin Double -> Float. Precision loss!");
*dst = *src;
```
```;
morph_real_as_u64_to_float ()
morph real_as_u64_to_float :
~ ~ native.UInt64
--> ~ native.Float32
```
= ```
fprintf(stderr, "Warning: morphin UInt64 -> Float. Precision loss!");
*dst = *src;
```
```;
morph_real_as_u64_to_double ()
morph real_as_u64_to_double :
~ ~ native.UInt64
--> ~ native.Float64
```
= ```
fprintf(stderr, "Warning: morphin UInt64 -> Double. Precision loss!");
*dst = *src;
```
```;
morph_real_as_nat_to_quantized_linear ()
morph real_as_nat_to_quantized_linear :
~ ~ native.UInt64
--> ~ <QuantizedLinear 0 1 1> ~ ~ native.UInt64
```
= ```
*dst = *src;
```
```;
morph_real_as_quantized_linear_to_float (Begin: , End: , Steps: )
morph real_as_quantized_linear_to_float : ∀(Begin: ). ∀(End: ). ∀(Steps: ).
~ <QuantizedLinear Begin End Steps> ~ ~ native.UInt64
--> ~ native.Float32
```
= ```
*dst = (float)Begin + ( *src * ((float)End - (float)Begin) ) / (float)Steps;
```
```;
morph_real_as_float_to_quantized_linear (Begin: , End: , Steps: )
morph real_as_float_to_quantized_linear : ∀(Begin: ). ∀(End: ). ∀(Steps: ).
~ native.Float32
--> ~ <QuantizedLinear Begin End Steps> ~ ~ native.UInt64
```
= ```
*dst = ((*src - (float)Begin) * (float)Steps) / ((float)End - (float)Begin);
```
```;
morph_real_as_quantized_linear_to_double (Begin: , End: , Steps: )
morph real_as_quantized_linear_to_double : ∀(Begin: ). ∀(End: ). ∀(Steps: ).
~ <QuantizedLinear Begin End Steps> ~ ~ native.UInt64
--> ~ native.Float64
```
*dst = (double)Begin + ( *src * ((double)End - (double)Begin) ) / (double)Steps;
```
= ```
*dst = (double)Begin + ( *src * ((double)End - (double)Begin) ) / (double)Steps;
```;
morph_real_as_double_to_quantized_linear (Begin: , End: , Steps: )
morph real_as_double_to_quantized_linear : ∀(Begin: ). ∀(End: ). ∀(Steps: ).
~ native.Float64
--> ~ <QuantizedLinear Begin End Steps> ~ ~ native.UInt64
```
= ```
*dst = ((*src - (double)Begin) * (double)Steps) / ((double)End - (double)Begin);
```
```;

View file

@ -1,30 +1,30 @@
```
```
morph_celsius_to_kelvin ()
morph celsius_to_kelvin :
Temperature ~ Celsius ~ ~ native.Float32
--> Temperature ~ Kelvin ~ ~ native.Float32
```*dst = *src + 273.15;```
= ```*dst = *src + 273.15;```;
morph_kelvin_to_celsius ()
morph kelvin_to_celsius :
Temperature ~ Kelvin ~ ~ native.Float32
--> Temperature ~ Celsius ~ ~ native.Float32
```*dst = *src - 273.15;```
= ```*dst = *src - 273.15;```;
morph_celsius_to_fahrenheit ()
morph celsius_to_fahrenheit :
Temperature ~ Celsius ~ ~ native.Float32
--> Temperature ~ Fahrenheit ~ ~ native.Float32
```*dst = (*src * 9.0 / 5.0) + 32.0;```
= ```*dst = (*src * 9.0 / 5.0) + 32.0;```;
morph_fahrenheit_to_celsius ()
morph fahrenheit_to_celsius :
Temperature ~ Fahrenheit ~ ~ native.Float32
--> Temperature ~ Celsius ~ ~ native.Float32
```*dst = (*src - 32.0) * 5.0 / 9.0;```
=```*dst = (*src - 32.0) * 5.0 / 9.0;```;
morph_pt100_resistance_to_celsius ()
morph pt100_resistance_to_celsius :
Temperature ~ PT100 ~ Resistance ~ Ohms ~ ~ native.Float64
--> Temperature ~ Celsius ~ ~ native.Float64
```
= ```
double resistance = *src;
// Constants for PT100 (ITS-90 standard)
@ -46,4 +46,4 @@ morph_pt100_resistance_to_celsius ()
}
*dst = (-b + sqrt(discriminant)) / (2 * a); // use positive root
```
```;

View file

@ -2,13 +2,13 @@
#include <time.h>
```
morph_unixtime_to_iso ()
morph unixtime_to_iso :
TimePoint ~ <TimeSince UnixEpoch> ~ Duration ~ Seconds ~ ~ <QuantizedLinear 0 1 1> ~ ~ native.UInt64
--> TimePoint ~ ISO8601 ~ <Seq~<ValueTerminated 0> Char~Ascii~native.UInt8>
```
= ```
time_t rawtime = (time_t)(*src);
struct tm *timeinfo = gmtime(&rawtime);
if (!timeinfo) return -1;
strftime((char*)dst, 20, "%Y-%m-%dT%H:%M:%SZ", timeinfo);
```
```;

View file

@ -1,37 +1,36 @@
```
```
morph_nat_as_u8_to_u16 ()
morph nat_as_u8_to_u16 :
~ native.UInt8
--> ~ native.UInt16
```*dst = *src;```
= ```*dst = *src;```;
morph_nat_as_u16_to_u32 ()
morph nat_as_u16_to_u32 :
~ native.UInt16
--> ~ native.UInt32
```*dst = *src;```
= ```*dst = *src;```;
morph_nat_as_u32_to_u64 ()
morph nat_as_u32_to_u64 :
~ native.UInt32
--> ~ native.UInt64
```*dst = *src;```
= ```*dst = *src;```;
morph_nat_as_u64_to_u16 ()
morph nat_as_u64_to_u16 :
~ native.UInt64
--> ~ native.UInt16
```
= ```
if( *src > 65535 ) {
fprintf(stderr, "error: value out of range for native.UInt16");
return -1;
}
*dst = *src;
```
```;
morph_nat_as_uint64_to_uint8
morph nat_as_uint64_to_uint8 :
~ native.UInt64
--> ~ native.UInt8
```
= ```
if ( *src < 256 ) {
*dst = *src;
}
@ -39,4 +38,4 @@ morph_nat_as_uint64_to_uint8
fprintf(stderr, "value %u is out of rage for UInt8\n", *dst);
return -1;
}
```
```;

View file

@ -2,23 +2,23 @@
#include <stdio.h>
```
morph_string_as_ascii_to_utf8 ()
morph string_as_ascii_to_utf8 :
<Seq ~ <ValueTerminated 0> Char~Ascii~native.UInt8>
--> <Seq Char~Unicode>
~ UTF-8
~ <Seq~<ValueTerminated 0> native.UInt8>
```
= ```
while( *src ) { *dst++ = *src++; }
*dst = 0;
return 0;
```
```;
morph_string_as_utf8_to_ascii ()
morph string_as_utf8_to_ascii :
<Seq Char~Unicode>
~ UTF-8
~ <Seq~<ValueTerminated 0> native.UInt8>
--> <Seq ~ <ValueTerminated 0> Char~Ascii~native.UInt8>
```
= ```
while( *src ) {
if( *src < 128 ) {
*dst++ = *src++;
@ -28,29 +28,26 @@ morph_string_as_utf8_to_ascii ()
}
}
*dst = 0;
```
```;
morph_string_as_ascii_to_utf32 ()
morph string_as_ascii_to_utf32 :
<Seq ~ <ValueTerminated 0> Char~Ascii~native.UInt8>
--> <Seq Char~Unicode>
~ UTF-32
~ <Seq~<ValueTerminated 0> native.UInt32>
```
= ```
while( *src ) { *dst++ = *src++; }
*dst = 0;
```
morph_string_as_utf8_to_utf32 ()
```;
morph string_as_utf8_to_utf32 :
<Seq Char~Unicode>
~ UTF-8
~ <Seq~<ValueTerminated 0> native.UInt8>
--> <Seq Char~Unicode>
~ UTF-32
~ <Seq~<ValueTerminated 0> native.UInt32>
```
= ```
bool has_multibyte = false;
uint32_t val = 0;
while( *src ) {
@ -87,4 +84,4 @@ morph_string_as_utf8_to_utf32 ()
*dst++ = val;
*dst++ = 0;
```
```;

View file

@ -3,7 +3,7 @@
#include <stdlib.h>
```
morph_valsep_delim (T: Type, SrcDelim: T, DstDelim: T)
morph valsep_delim : ∀(T: Type). ∀(SrcDelim: T). ∀(DstDelim: T).
< Seq <Seq T> >
~ < ValueSep SrcDelim T >
~ < Seq~<LengthPrefix native.UInt64> T >
@ -11,7 +11,7 @@ morph_valsep_delim (T: Type, SrcDelim: T, DstDelim: T)
--> < Seq <Seq T> >
~ < ValueSep DstDelim T >
~ < Seq~<LengthPrefix native.UInt64> T >
```
=```
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, clear)( dst );
uint8_t * dst_items = dst->items;
@ -30,9 +30,9 @@ morph_valsep_delim (T: Type, SrcDelim: T, DstDelim: T)
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, push)( dst, src->items[i] );
}
}
```
```;
morph_seqseq_as_valsep_to_lenpfx (T: Type, Delim: T, EscKey: T)
morph seqseq_as_valsep_to_lenpfx : ∀(T: Type). ∀(Delim: T). ∀(EscKey: T).
< Seq <Seq T> >
~ < ValueSep T Delim >
~ < Seq~<LengthPrefix native.UInt64> T >
@ -43,7 +43,7 @@ morph_seqseq_as_valsep_to_lenpfx (T: Type, Delim: T, EscKey: T)
~ native.Address
~ native.UInt64
>
```
= ```
length_prefix_nativeUInt64_array_nativeUInt64_clear( dst );
LENGTH_PREFIX_ARRAY_TYPE( nativeUInt64, T ) * cur_item = NULL;
@ -66,9 +66,9 @@ morph_seqseq_as_valsep_to_lenpfx (T: Type, Delim: T, EscKey: T)
cur++;
}
}
```
```;
morph_seqeq_as_lenpfx_to_valsep (T: Type, Delim: T, EscKey: T)
morph seqeq_as_lenpfx_to_valsep : ∀(T: Type). ∀(Delim: T). ∀(EscKey: T).
< Seq~<LengthPrefix native.UInt64>
<Seq~<LengthPrefix native.UInt64> T >
~ <RefMut < Seq~<LengthPrefix native.UInt64> T>>
@ -78,7 +78,7 @@ morph_seqeq_as_lenpfx_to_valsep (T: Type, Delim: T, EscKey: T)
--> < Seq <Seq T> >
~ < ValueSep T Delim >
~ < Seq~<LengthPrefix native.UInt64> T >
```
= ```
PRESCAN_LENGTH_PREFIX_CALL(nativeUInt64, T, clear)( dst );
for( uint64_t i = 0; i < src->len; ++i ) {
@ -92,4 +92,4 @@ morph_seqeq_as_lenpfx_to_valsep (T: Type, Delim: T, EscKey: T)
PRESCAN_LENGTH_PREFIX_CALL( nativeUInt64, T, push )( Delim );
}
}
```
```;

View file

@ -1,15 +1,14 @@
```
```
morph_wheatstone_reading_to_ohms (
vin: Resistance ~ Ohms ~ ,
r1: Resistance ~ Ohms ~ ,
r2: Resistance ~ Ohms ~ ,
r3: Resistance ~ Ohms ~
)
morph wheatstone_reading_to_ohms :
∀(vin: Resistance ~ Ohms ~ ).
∀(r1: Resistance ~ Ohms ~ ).
∀(r2: Resistance ~ Ohms ~ ).
∀(r3: Resistance ~ Ohms ~ ).
Resistance ~ <WheatstoneBridge vin r1 r2 r3> ~ ~ native.Float64
--> Resistance ~ Ohms ~ ~ native.Float64
```
= ```
// Voltage at midpoints of two voltage dividers:
double v1 = vin * (r2 / (r1 + r2));
double v2 = *src; // From the ADC
@ -21,4 +20,4 @@ morph_wheatstone_reading_to_ohms (
}
*dst = r3 * v2 / (vin - v2);
```
```;

View file

@ -1,24 +1,24 @@
```
```
morph_i64_as_twos_complement_to_zigzag ()
morph i64_as_twos_complement_to_zigzag :
~ native.Int64
--> ~ ZigZagInt ~ ~ native.UInt64
```
= ```
if( *src >= 0 ) {
*dst = (2 * (uint64_t)*src)
} else {
*dst = (2 * (uint64_t)(- *src)) - 1;
}
```
```;
morph_i64_as_zigzag_to_twos_complement ()
morph i64_as_zigzag_to_twos_complement :
~ ZigZagInt ~ ~ native.UInt64
--> ~ native.Int64
```
= ```
if( *src % 2 == 0 ) {
*dst = *src / 2;
} else {
*dst = - ((*src+1) / 2);
}
```
```;

View file

@ -1,9 +1,11 @@
use {
super::types::get_c_repr_arg_type, crate::{c_gen::LdmcCTargetMorph, LdmcPrimMorph}, laddertypes::{morphism::MorphismInstance, parser::*, MorphismType, TypeDict}, std::collections::HashMap
super::types::get_c_repr_arg_type, crate::{c_gen::LdmcCTargetMorph, LdmcPrimMorph},
laddertypes::{morphism::MorphismInstance, parser::*, Context, MorphismType, TypeDict},
std::{collections::HashMap, sync::{Arc, RwLock}}
};
pub fn generate_lib(
dict: &mut impl TypeDict,
mut ctx: Arc<RwLock<Context>>,
include_blocks: Vec< String >,
include_dependencies: HashMap< MorphismType, usize >,
morphisms: Vec< (String, MorphismInstance<LdmcPrimMorph>) >
@ -12,7 +14,7 @@ pub fn generate_lib(
let mut wrappers = String::new();
for (name, morph) in morphisms {
match target.add_instantiation(dict, morph) {
match target.add_instantiation(ctx.clone(), morph) {
Ok(inst) => {
if name == "main" {
let mut c_source = String::new();
@ -25,7 +27,7 @@ pub fn generate_lib(
if let Ok(_) = laddertypes::subtype_unify(
&inst.ty.src_type,
&dict.parse("<Seq~<ValueTerminated '\\n'> Char~Ascii~native.UInt8>").expect("")
&ctx.parse("<Seq~<ValueTerminated '\\n'> Char~Ascii~native.UInt8>").expect("")
) {
c_source.push_str("scanf(\"%s\", bufIn);\n");
} else {
@ -35,12 +37,12 @@ pub fn generate_lib(
c_source.push_str(
&format!(r#"FUSE( {}, (void const*)bufIn, (void*)bufOut );
"#,
inst.instantiated_symbol_name(dict, &HashMap::new()))
inst.instantiated_symbol_name(&ctx, &HashMap::new()))
);
if let Ok(ψ) = laddertypes::subtype_unify(
&inst.ty.dst_type,
&dict.parse("<Seq~<ValueTerminated 0> native.UInt8>").expect("")
&ctx.parse("<Seq~<ValueTerminated 0> native.UInt8>").expect("")
) {
c_source.push_str("printf(\"%s\\n\", bufOut);\n");
} else {
@ -51,8 +53,8 @@ pub fn generate_lib(
}");
wrappers.push_str(&c_source);
} else {
target.add_type(dict, inst.ty.src_type.clone());
target.add_type(dict, inst.ty.dst_type.clone());
target.add_type(ctx.clone(), inst.ty.src_type.clone());
target.add_type(ctx.clone(), inst.ty.dst_type.clone());
wrappers.push_str(&format!("
int {} (
@ -62,9 +64,9 @@ pub fn generate_lib(
return {}( (void*)src, (void*)dst );
}}
", name,
get_c_repr_arg_type(dict, &inst.ty.src_type),
get_c_repr_arg_type(dict, &inst.ty.dst_type),
inst.instantiated_symbol_name(dict, &HashMap::new())
get_c_repr_arg_type(ctx.clone(), &inst.ty.src_type),
get_c_repr_arg_type(ctx.clone(), &inst.ty.dst_type),
inst.instantiated_symbol_name(&ctx, &HashMap::new())
));
}
}
@ -75,7 +77,7 @@ pub fn generate_lib(
}
}
let mut c_source = target.into_c_source(dict);
let mut c_source = target.into_c_source(ctx);
c_source.push_str(&wrappers);
Ok(c_source)

View file

@ -9,57 +9,51 @@ use {
},
morphism::LdmcPrimMorph
},
laddertypes::{TypeDict, Substitution},
laddertypes::{Context, HashMapSubst, Substitution, TypeDict},
std::sync::{Arc, RwLock},
};
impl LdmcPrimMorph {
pub fn instantiated_symbol_name(&self, dict: &mut impl TypeDict, σ: &impl Substitution) -> String {
pub fn instantiated_symbol_name(&self, ctx: &Arc<RwLock<Context>>, σ: &impl Substitution) -> String {
let mut s = self.symbol.clone();
for (k_id,v) in self.type_args.iter() {
if let Some(val_trm) = σ.get(k_id) {
if let laddertypes::TypeID::Var(var_id) = k_id {
for (var_id,v) in self.type_args.iter() {
if let Ok(val_trm) = σ.get(*var_id) {
//if self.get_type().strip_halo().src_type.contains_var(*var_id) ||
//self.get_type().strip_halo().dst_type.contains_var(*var_id) {
let name = dict.get_varname(*var_id).unwrap();
let val_str = get_c_repr_type(dict, &val_trm);
let name = ctx.get_varname(*var_id).unwrap();
let val_str = get_c_repr_type(ctx, &val_trm);
s.push_str(&format!("_{}_{}", name, val_str));
//}
}
} else {
if let laddertypes::TypeID::Var(var_id) = k_id {
let k = dict.get_varname(*var_id).unwrap();
s.push_str(&format!("_{:?}_MISSING", k));
eprintln!("INCOMPLETE MORPHISM INSTANTIATION, missing type parameter {} ({})", k, var_id);
}
let k = ctx.get_varname(*var_id).unwrap();
s.push_str(&format!("_{:?}_MISSING", k));
eprintln!("INCOMPLETE MORPHISM INSTANTIATION, missing type parameter {} ({})", k, var_id);
}
}
s
}
pub fn expected_c_type_signature(&self, dict: &mut impl TypeDict,
σ: &std::collections::HashMap<laddertypes::TypeID, laddertypes::TypeTerm>
pub fn expected_c_type_signature(&self, ctx: Arc<RwLock<Context>>,
σ: &HashMapSubst
) -> String {
format!("int {} ({} const * restrict src, {} * restrict dst);",
self.instantiated_symbol_name(dict, σ),
get_c_repr_definition(dict, self.ty.src_type.clone(), true).expect("cant get c-repr type for src type"),
get_c_repr_definition(dict, self.ty.dst_type.clone(), true).expect("cant get c-repr type for dst type"))
self.instantiated_symbol_name(&ctx, σ),
get_c_repr_definition(ctx.clone(), self.ty.src_type.clone(), true).expect("cant get c-repr type for src type"),
get_c_repr_definition(ctx.clone(), self.ty.dst_type.clone(), true).expect("cant get c-repr type for dst type"))
}
pub fn generate_instantiation(&self, dict: &mut impl TypeDict, σ: &impl Substitution) -> Option<String> {
pub fn generate_instantiation(&self, mut ctx: Arc<RwLock<Context>>, σ: &impl Substitution) -> Option<String> {
let mut s = String::new();
let symbol = self.instantiated_symbol_name(dict, σ);
let symbol = self.instantiated_symbol_name(&ctx, σ);
eprintln!("generate instantiation:");
let ty = self.ty.clone();
eprintln!("full type: {} ----> {}", ty.src_type.pretty(dict, 0), ty.dst_type.pretty(dict,0));
let ty = ty.strip_halo().apply_subst(σ);
eprintln!("stripped type: {} ----> {}", ty.src_type.pretty(dict, 0), ty.dst_type.pretty(dict,0));
eprintln!("full type: {} ----> {}", ty.src_type.pretty(&mut ctx, 0), ty.dst_type.pretty(&mut ctx,0));
let ty = ty.strip_common_rungs().apply_subst(σ);
eprintln!("stripped type: {} ----> {}", ty.src_type.pretty(&mut ctx, 0), ty.dst_type.pretty(&mut ctx,0));
let src_c_symbol = get_c_repr_arg_type(dict, &ty.src_type);
let dst_c_symbol = get_c_repr_arg_type(dict, &ty.dst_type);
let src_c_symbol = get_c_repr_arg_type(ctx.clone(), &ty.src_type);
let dst_c_symbol = get_c_repr_arg_type(ctx.clone(), &ty.dst_type);
s.push_str(&format!(r#"
int {} ( {} const * restrict src, {} * restrict dst ) {{

View file

@ -6,9 +6,8 @@ use {
morphism::LdmcPrimMorph
},
laddertypes::{
parser::*, Morphism, MorphismInstance, MorphismType, TypeDict, TypeTerm
},
std::collections::HashMap
parser::*, Context, HashMapSubst, LayeredContext, Morphism, MorphismInstance, MorphismType, Substitution, SubstitutionMut, TypeDict, TypeID, TypeKind, TypeTerm
}, std::{collections::HashMap, ops::Deref, sync::{Arc, RwLock}}
};
pub struct LdmcCTargetMorph {
@ -54,10 +53,10 @@ impl LdmcCTargetMorph {
m
}
pub fn add_type(&mut self, dict: &mut impl TypeDict, ty: TypeTerm) {
pub fn add_type(&mut self, mut ctx: Arc<RwLock<Context>>, ty: TypeTerm) {
let ty = ty.strip();
if ! self.active_types.contains(&ty) {
eprintln!("add type {}", ty.pretty(dict,0));
eprintln!("add type {}", ty.pretty(&mut ctx,0));
let (ht,ft) = ty.get_floor_type();
if ht.is_empty() {
@ -65,22 +64,23 @@ impl LdmcCTargetMorph {
match &ft {
TypeTerm::Seq { seq_repr, items } => {
let item_type = items.first().unwrap().clone();
self.add_type(dict, item_type.clone());
self.add_type(ctx.clone(), item_type.clone());
if let Some(seq_repr) = seq_repr {
dict.add_varname("LengthType".into());
let mut ctx = ctx.scope();
ctx.add_variable("LengthType", TypeKind::Type);
if let Ok(σ) =
laddertypes::constraint_system::unify(
seq_repr.as_ref(),
&dict.parse_desugared("<LengthPrefix LengthType>").expect("").sugar(dict)
&ctx.parse_desugared("<LengthPrefix LengthType>").expect("").sugar(&mut ctx)
)
{
let length_type = σ.get(&dict.get_typeid(&"LengthType".into()).expect("")).expect("cant get Length type");
self.add_type(dict, length_type.clone());
let length_type = σ.get(&ctx.get_varid("LengthType").expect("")).expect("cant get Length type");
self.add_type(ctx.clone(), length_type.clone());
self.macro_calls.push(
format!("DEFINE_LENGTH_PREFIX_ARRAY({}, {})\n",
get_c_repr_type(dict, &length_type),
get_c_repr_type(dict, &item_type)
get_c_repr_type(&ctx, &length_type),
get_c_repr_type(&ctx, &item_type)
)
);
}
@ -90,16 +90,16 @@ impl LdmcCTargetMorph {
}
self.active_types.push(ty.clone());
if let Some(type_def) = get_c_repr_definition(dict, ft, false) {
let type_name = get_c_repr_type(dict, &ty);
if let Some(type_def) = get_c_repr_definition(ctx.clone(), ft, false) {
let type_name = get_c_repr_type(&ctx, &ty);
self.typedefs.push(format!("typedef {} {};\n", type_def, type_name));
} else {
eprintln!("cant get c-repr type for type '{}'", ty.pretty(dict,0));
eprintln!("cant get c-repr type for type '{}'", ty.pretty(&mut ctx,0));
}
} else {
let type_name = get_c_repr_type(dict, &ty);
let type_def = get_c_repr_type(dict, &ft);
self.add_type(dict, ft);
let type_name = get_c_repr_type(&ctx, &ty);
let type_def = get_c_repr_type(&ctx, &ft);
self.add_type(ctx, ft);
self.typedefs.push(format!("typedef {} {};\n", type_def, type_name));
}
}
@ -107,23 +107,25 @@ impl LdmcCTargetMorph {
pub fn add_instantiation(
&mut self,
dict: &mut impl TypeDict,
ctx: Arc<RwLock<Context>>,
morph: MorphismInstance<LdmcPrimMorph>,
) -> Result<LdmcPrimMorph, ()> {
let new_inst = self.bake_morphism(dict, morph)?;
let mut σ = HashMap::new();
let new_inst = self.bake_morphism(ctx.clone(), TypeTerm::unit(), &mut σ, morph)?;
if ! self.active_morphisms.contains(&new_inst) {
self.active_morphisms.push(new_inst.clone());
}
let ty = new_inst.get_type().strip_halo();
self.add_type(dict, ty.src_type);
self.add_type(dict, ty.dst_type);
let ty = new_inst.get_type().strip_common_rungs();
self.add_type(ctx.clone(), ty.src_type);
self.add_type(ctx, ty.dst_type);
Ok(new_inst)
}
pub fn into_c_source(mut self, dict: &mut impl TypeDict) -> String {
pub fn into_c_source(mut self, ctx: Arc<RwLock<Context>>) -> String {
let mut source = String::new();
self.active_headers.sort();
self.active_headers.dedup();
self.typedefs.dedup();
self.macro_calls.dedup();
@ -148,60 +150,65 @@ impl LdmcCTargetMorph {
source.push('\n');
for m in self.active_morphisms {
source.push_str(&m.generate_instantiation(dict, &HashMap::new()).expect("cant create function"));
source.push_str(&m.generate_instantiation(ctx.clone(), &HashMap::new()).expect("cant create function"));
}
source
}
pub fn bake_morphism(
&mut self,
dict: &mut impl laddertypes::TypeDict,
ctx: Arc<RwLock<Context>>,
mut ψ: TypeTerm,
σ: &mut HashMapSubst,
morph_inst: MorphismInstance<LdmcPrimMorph>,
) -> Result<LdmcPrimMorph, ()> {
let ty = morph_inst.get_type();
let symbol = encode_morph_type_to_symbol(dict, &ty);
let symbol = encode_morph_type_to_symbol(&ctx, &ty);
match &morph_inst {
MorphismInstance::Id { ψ } => {
MorphismInstance::Id { τ } => {
self.add_required_header_block("#include <string.h>".into());
Ok(LdmcPrimMorph {
symbol,
type_args: Vec::new(),
ty: MorphismType { src_type: ψ.clone(), dst_type: ψ.clone() },
ty: MorphismType { bounds: Vec::new(), src_type: τ.clone(), dst_type: τ.clone() },
c_source: String::from("memcpy(dst, src, sizeof(*src));")
})
}
MorphismInstance::Primitive { ψ, σ, morph } => {
MorphismInstance::Sub { ψ:ψ1, m } => {
ψ = TypeTerm::Ladder(vec![ ψ1.clone(), ψ.clone() ]).normalize();
self.bake_morphism(ctx, ψ, σ, m.deref().clone())
}
MorphismInstance::Specialize { σ:σ1, m } => {
σ.append(&σ1);
self.bake_morphism(ctx, ψ, σ, m.deref().clone())
}
MorphismInstance::Primitive { m: morph } => {
if let Some(i) = self.header_dependencies.get(&morph.get_type()) {
self.active_headers.push(*i);
}
let mut c_source = String::new();
for (ty_id, kind) in morph.type_args.iter() {
if let laddertypes::TypeID::Var(var_id) = ty_id {
if let Some(val) = σ.get(ty_id) {
for (v, kind) in morph.type_args.iter() {
if let Some(val) = σ.get(v) {
let type_var_value =
if kind == "Type" {
get_c_repr_type(dict, val)
get_c_repr_type(&ctx, val)
} else {
encode_type_to_value(dict, val)
encode_type_to_value(&ctx, val)
};
c_source.push_str(&format!(" #define {} {}\n", dict.get_typename(&ty_id).unwrap(), type_var_value));
c_source.push_str(&format!(" #define {} {}\n", ctx.get_varname(*v).unwrap(), type_var_value));
}
}
}
c_source.push_str(&morph.c_source);
for (ty_id, kind) in morph.type_args.iter() {
if let laddertypes::TypeID::Var(var_id) = ty_id {
c_source.push_str(&format!("\n #undef {}", dict.get_typename(&ty_id).unwrap()));
}
for (v, kind) in morph.type_args.iter() {
c_source.push_str(&format!("\n #undef {}", ctx.get_varname(*v).unwrap()));
}
Ok(LdmcPrimMorph{
symbol: morph.instantiated_symbol_name(dict, σ),
symbol: morph.instantiated_symbol_name(&ctx, σ),
type_args: Vec::new(),
ty: morph_inst.get_type().apply_subst(σ).strip_halo(),
ty: morph_inst.get_type().apply_subst(σ).strip_common_rungs(),
c_source
})
},
@ -216,8 +223,8 @@ impl LdmcCTargetMorph {
}
for (i,morph) in path.iter().enumerate() {
if let Ok(inst) = self.add_instantiation(dict, morph.clone()) {
let morph_symbol = inst.instantiated_symbol_name(dict, &morph.get_subst());
if let Ok(inst) = self.add_instantiation(ctx.clone(), morph.clone()) {
let morph_symbol = inst.instantiated_symbol_name(&ctx, &morph.get_subst());
let src_buf = if i == 0 { "(void*)src" } else if i%2 == 0 { "(void*)bufA" } else { "(void*)bufB" };
let dst_buf = if i+1 == path.len() { "(void*)dst" } else if i%2 == 0 { "(void*)bufB" } else { "(void*)bufA" };
@ -236,24 +243,25 @@ impl LdmcCTargetMorph {
c_source
})
}
MorphismInstance::MapSeq { ψ, seq_repr, item_morph } => {
if let Ok(item_morph_inst) = self.add_instantiation(dict, item_morph.as_ref().clone()) {
MorphismInstance::MapSeq { seq_repr, item_morph } => {
if let Ok(item_morph_inst) = self.add_instantiation(ctx.clone(), item_morph.as_ref().clone()) {
if let Some(seq_repr) = seq_repr {
dict.add_varname("Length".into());
dict.add_varname("LengthType".into());
dict.add_varname("TerminatorValue".into());
let mut ctx = ctx.scope();
ctx.add_variable("Length", TypeKind::ValueUInt);
ctx.add_variable("LengthType", TypeKind::Type);
ctx.add_variable("TerminatorValue", TypeKind::ValueUInt);
if let Ok(γ) = laddertypes::constraint_system::unify(
&dict.parse_desugared("<StaticLength Length>").expect("parse type template").sugar(dict),
&ctx.parse_desugared("<StaticLength Length>").expect("parse type template").sugar(&mut ctx),
seq_repr.as_ref()
) {
let length = γ.get(&dict.get_typeid(&"Length".into()).expect("")).expect("cant get Length");
let length = γ.get(&ctx.get_varid("Length").expect("")).expect("cant get Length");
match length {
TypeTerm::Num(l) => {
let item_morph_symbol = item_morph_inst.instantiated_symbol_name(dict, &HashMap::new());
let item_morph_symbol = item_morph_inst.instantiated_symbol_name(&ctx, &HashMap::new());
self.add_type( dict, morph_inst.get_type().strip_halo().src_type );
self.add_type( dict, morph_inst.get_type().strip_halo().dst_type );
self.add_type( ctx.clone(), morph_inst.get_type().strip_common_rungs().src_type );
self.add_type( ctx.clone(), morph_inst.get_type().strip_common_rungs().dst_type );
let c_source = format!(r#"
for( size_t i = 0; i < {}; ++i ) {{
@ -270,22 +278,22 @@ impl LdmcCTargetMorph {
})
}
_ => {
eprintln!("invalid length '{}'", length.pretty(dict, 0));
eprintln!("invalid length '{}'", length.pretty(&mut ctx, 0));
Err(())
}
}
}
else if let Ok(γ) = laddertypes::constraint_system::unify(
&dict.parse_desugared("<LengthPrefix LengthType>").expect("parse type template").sugar(dict),
&ctx.parse_desugared("<LengthPrefix LengthType>").expect("parse type template").sugar(&mut ctx),
seq_repr.as_ref()
) {
// todo: assert that length type is native.UIntX
//let length_type = γ.get(&dict.get_typeid(&"LengthType".into()).expect("")).expect("cant get LengthType");
let item_morph_symbol = item_morph_inst.instantiated_symbol_name(dict, &HashMap::new());
let item_morph_symbol = item_morph_inst.instantiated_symbol_name(&ctx, &HashMap::new());
self.add_type( dict, morph_inst.get_type().strip_halo().src_type );
self.add_type( dict, morph_inst.get_type().strip_halo().dst_type );
self.add_type( ctx.clone(), morph_inst.get_type().strip_common_rungs().src_type );
self.add_type( ctx.clone(), morph_inst.get_type().strip_common_rungs().dst_type );
let c_source = format!(r#"
for( size_t i = 0; i < src->len; ++i ) {{
@ -303,12 +311,12 @@ impl LdmcCTargetMorph {
}
else if let Ok(γ) = laddertypes::constraint_system::unify(
&dict.parse_desugared("<ValueTerminated TerminatorValue>").expect("parse type template").sugar(dict),
&ctx.parse_desugared("<ValueTerminated TerminatorValue>").expect("parse type template").sugar(&mut ctx),
seq_repr.as_ref()
) {
let terminator_value = γ.get(&dict.get_typeid(&"TerminatorValue".into()).expect("")).expect("cant get TerminatorValue");
let item_morph_symbol = item_morph_inst.instantiated_symbol_name(dict, &HashMap::new());
let terminator = encode_type_to_value(dict, terminator_value);
let terminator_value = γ.get(&ctx.get_varid("TerminatorValue").expect("")).expect("cant get TerminatorValue");
let item_morph_symbol = item_morph_inst.instantiated_symbol_name(&ctx, &HashMap::new());
let terminator = encode_type_to_value(&ctx, terminator_value);
let c_source = format!(r#"
while( *src != {} ) {{
@ -342,16 +350,16 @@ impl LdmcCTargetMorph {
Err(())
}
},
MorphismInstance::MapStruct { ψ, src_struct_repr, dst_struct_repr, member_morph } => {
MorphismInstance::MapStruct { src_struct_repr, dst_struct_repr, member_morph } => {
let mut c_source = String::new();
for (name, morph) in member_morph.iter() {
if let Ok(inst) = self.add_instantiation(dict, morph.clone()) {
if let Ok(inst) = self.add_instantiation(ctx.clone(), morph.clone()) {
let name = name.replace("-", "_").replace(".","_dot_");
c_source.push_str(
&format!("
FUSE( {}, &src->{}, &dst->{} );",
FUSE( {}, (void*)&src->{}, (void*)&dst->{} );",
inst.symbol,
name, name
));
@ -367,7 +375,7 @@ impl LdmcCTargetMorph {
c_source
})
}
MorphismInstance::MapEnum { ψ, enum_repr, variant_morph } => {
MorphismInstance::MapEnum { enum_repr, variant_morph } => {
todo!();
}
}

View file

@ -1,9 +1,11 @@
use {
crate::struct_layout::LayoutType,
chumsky::chain::Chain, laddertypes::{morphism::MorphismType,
parser::*,
substitution::Substitution,
unparser::*, EnumVariant, StructMember, TypeTerm, TypeDict}
laddertypes::{
morphism::MorphismType, parser::*, unparser::*,
Context, EnumVariant, LayeredContext, StructMember,
TypeDict, TypeID, TypeKind, TypeTerm
},
std::sync::{Arc, RwLock}
};
/*
@ -17,22 +19,22 @@ pub fn get_c_repr_kind(kind: String) -> Option<String> {
/* for a given ladder type `t`, get the corresponding C type
*/
pub fn get_c_repr_definition(dict: &mut impl TypeDict, t: TypeTerm, skip_pointer: bool) -> Option<String> {
pub fn get_c_repr_definition(mut ctx: Arc<RwLock<Context>>, t: TypeTerm, skip_pointer: bool) -> Option<String> {
match t {
TypeTerm::TypeID(tyid) => {
if tyid == dict.get_typeid_creat("native.UInt8") {
TypeTerm::Id(tyid) => {
if TypeID::Fun(tyid) == ctx.get_typeid_creat("native.UInt8") {
Some("uint8_t".into())
} else if tyid == dict.get_typeid_creat("native.UInt16") {
} else if TypeID::Fun(tyid) == ctx.get_typeid_creat("native.UInt16") {
Some("uint16_t".into())
} else if tyid == dict.get_typeid_creat("native.UInt32") {
} else if TypeID::Fun(tyid) == ctx.get_typeid_creat("native.UInt32") {
Some("uint32_t".into())
} else if tyid == dict.get_typeid_creat("native.UInt64") {
} else if TypeID::Fun(tyid) == ctx.get_typeid_creat("native.UInt64") {
Some("uint64_t".into())
} else if tyid == dict.get_typeid_creat("native.Address") {
} else if TypeID::Fun(tyid) == ctx.get_typeid_creat("native.Address") {
Some("void*".into())
} else if tyid == dict.get_typeid_creat("native.Float32") {
} else if TypeID::Fun(tyid) == ctx.get_typeid_creat("native.Float32") {
Some("float".into())
} else if tyid == dict.get_typeid_creat("native.Float64") {
} else if TypeID::Fun(tyid) == ctx.get_typeid_creat("native.Float64") {
Some("double".into())
} else {
None
@ -42,19 +44,21 @@ pub fn get_c_repr_definition(dict: &mut impl TypeDict, t: TypeTerm, skip_pointer
TypeTerm::Seq{ seq_repr, items } => {
if let Some(seq_repr) = seq_repr {
let item_type = items.first().expect("no item type specified!").clone();
let item_c_type : String = get_c_repr_type(dict, &item_type);
let item_c_type : String = get_c_repr_type(&mut ctx, &item_type);
dict.add_varname("Length".into()); // Kind: Num
dict.add_varname("LengthType".into()); // Kind: Type
dict.add_varname("TermValue".into()); // Kind: Value of Type ItemType
let ctx = ctx.scope();
ctx.add_variable("Length", TypeKind::ValueUInt);
ctx.add_variable("LengthType", TypeKind::Type);
ctx.add_variable("TermValue", TypeKind::ValueUInt); // Kind: Value of Type ItemType
if let Ok((ψ, σ)) = laddertypes::constraint_system::subtype_unify(
seq_repr.as_ref(),
&dict.parse_desugared("<StaticLength Length>").expect("parse template type").sugar(dict)
&ctx.clone().parse_desugared("<StaticLength Length>").expect("parse template type").sugar(&mut ctx.clone())
) {
let length = match
σ.get(
&dict.get_typeid(&"Length".into()).expect("no Length ID")
&ctx.get_varid("Length").expect("no Length ID")
).expect("no length specified!")
{
TypeTerm::Num(l) => l,
@ -67,19 +71,19 @@ pub fn get_c_repr_definition(dict: &mut impl TypeDict, t: TypeTerm, skip_pointer
}
else if let Ok((ψ, σ)) = laddertypes::constraint_system::subtype_unify(
seq_repr.as_ref(),
&dict.parse_desugared("<LengthPrefix LengthType>").expect("parse template type").sugar(dict)
&ctx.clone().parse_desugared("<LengthPrefix LengthType>").expect("parse template type").sugar(&mut ctx.clone())
) {
let length_type = σ.get(
&dict.get_typeid(&"LengthType".into()).expect("no LengthType ID")
&ctx.get_varid("LengthType").expect("no LengthType ID")
).expect("no length type specified");
let length_c_type = get_c_repr_type(dict, &length_type);
let length_c_type = get_c_repr_type(&ctx, &length_type);
Some(format!("struct {{ {} len; {} items[]; }} ", length_c_type, item_c_type))
}
else if let Ok((ψ, σ)) = laddertypes::constraint_system::subtype_unify(
seq_repr.as_ref(),
&dict.parse_desugared("<ValueTerminated TermValue>").expect("parse template type").sugar(dict)
&ctx.clone().parse_desugared("<ValueTerminated TermValue>").expect("parse template type").sugar(&mut ctx.clone())
) {
if skip_pointer {
Some(item_c_type)
@ -89,7 +93,7 @@ pub fn get_c_repr_definition(dict: &mut impl TypeDict, t: TypeTerm, skip_pointer
}
else if let Ok((ψ, σ)) = laddertypes::constraint_system::subtype_unify(
seq_repr.as_ref(),
&dict.parse_desugared("MsbCont").expect("parse template type").sugar(dict)
&ctx.clone().parse_desugared("MsbCont").expect("parse template type").sugar(&mut ctx.clone())
) {
if skip_pointer {
Some(item_c_type)
@ -98,7 +102,7 @@ pub fn get_c_repr_definition(dict: &mut impl TypeDict, t: TypeTerm, skip_pointer
}
}
else {
eprintln!("can't get C-repr for sequence type `{}`!", seq_repr.pretty(dict, 0));
eprintln!("can't get C-repr for sequence type `{}`!", seq_repr.pretty(&mut ctx.clone(), 0));
None
}
} else {
@ -110,8 +114,8 @@ pub fn get_c_repr_definition(dict: &mut impl TypeDict, t: TypeTerm, skip_pointer
TypeTerm::Struct{ struct_repr, members } => {
let c_struct_attribute =
if let Some(sr) = struct_repr {
let sr = sr.desugar(dict);
match LayoutType::parse(dict, &sr).expect("layout type") {
let sr = sr.desugar(&mut ctx);
match LayoutType::parse(&mut ctx, &sr).expect("layout type") {
LayoutType::Aligned => "",
LayoutType::Packed => "__attribute((packed))__"
}
@ -120,7 +124,7 @@ pub fn get_c_repr_definition(dict: &mut impl TypeDict, t: TypeTerm, skip_pointer
let mut c_type = format!("struct {} {{\n", c_struct_attribute);
for StructMember{ symbol, ty } in members {
let field_c_type = get_c_repr_definition(dict, ty, false).expect("");
let field_c_type = get_c_repr_definition(ctx.clone(), ty, false).expect("");
c_type.push_str(&format!(" {} {};\n",
field_c_type,
symbol.replace("-", "_").replace(".","_dot_")
@ -154,7 +158,7 @@ struct {{
");
for EnumVariant{ symbol, ty } in variants {
let variant_c_type = get_c_repr_definition(dict, ty, false).expect("cant get C-Repr type for variant");
let variant_c_type = get_c_repr_definition(ctx.clone(), ty, false).expect("cant get C-Repr type for variant");
c_type.push_str(&format!(
" {} {};\n", variant_c_type, symbol.replace("-", "_").replace(".","_dot_")
));
@ -169,7 +173,7 @@ struct {{
TypeTerm::Ladder(rungs) => {
if let Some(t) = rungs.last() {
get_c_repr_definition(dict, t.clone(), false)
get_c_repr_definition(ctx, t.clone(), false)
} else {
None
}
@ -179,10 +183,10 @@ struct {{
}
}
pub fn get_c_repr_type(dict: &mut impl TypeDict, t: &laddertypes::TypeTerm)-> String {
pub fn get_c_repr_type(parent: &Arc<RwLock<Context>>, t: &laddertypes::TypeTerm)-> String {
let t = t.clone().strip();
match t.desugar(dict) {
match t.desugar(&mut parent.clone()) {
laddertypes::DesugaredTypeTerm::Char(c) => {
match c {
'.' => format!("_dot_"),
@ -195,7 +199,7 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: &laddertypes::TypeTerm)-> St
format!("{}", n)
}
laddertypes::DesugaredTypeTerm::TypeID(ty_id) => {
if let Some(name) = dict.get_typename(&ty_id) {
if let Some(name) = parent.get_name(ty_id.clone()) {
name
.replace("-", "_")
.replace(".", "")
@ -208,8 +212,8 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: &laddertypes::TypeTerm)-> St
let mut s = String::new();
s.push('_');
for r in rs {
let r = r.sugar(dict);
s.push_str(&get_c_repr_type(dict, &r));
let r = r.sugar(&mut parent.clone());
s.push_str(&get_c_repr_type(parent, &r));
s.push('_');
}
s
@ -217,41 +221,41 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: &laddertypes::TypeTerm)-> St
}
}
pub fn get_c_repr_arg_type(dict: &mut impl TypeDict, t: &laddertypes::TypeTerm)-> String {
pub fn get_c_repr_arg_type(mut ctx: Arc<RwLock<Context>>, t: &laddertypes::TypeTerm)-> String {
let t = t.clone().strip().get_floor_type().1;
dict.add_varname("TerminatorValue".into());
ctx.add_variable("TerminatorValue", TypeKind::ValueUInt);
match &t {
TypeTerm::Seq { seq_repr, items } => {
if let Some(seq_repr) = seq_repr.as_ref() {
if let Ok(ψ) = laddertypes::constraint_system::subtype_unify(
seq_repr,
&dict.parse_desugared("<ValueTerminated TerminatorValue>").expect("").sugar(dict)
&ctx.parse_desugared("<ValueTerminated TerminatorValue>").expect("").sugar(&mut ctx.clone())
) {
return get_c_repr_type(dict, &items[0].clone().strip().get_floor_type().1);
return get_c_repr_type(&ctx, &items[0].clone().strip().get_floor_type().1);
}
else if let Ok(ψ) = laddertypes::constraint_system::subtype_unify(
seq_repr,
&dict.parse_desugared("MsbCont").expect("").sugar(dict)
&ctx.parse_desugared("MsbCont").expect("").sugar(&mut ctx.clone())
) {
return get_c_repr_type(dict, &items[0].clone().strip().get_floor_type().1);
return get_c_repr_type(&ctx, &items[0].clone().strip().get_floor_type().1);
}
}
}
_ => {}
}
get_c_repr_type(dict, &t)
get_c_repr_type(&ctx, &t)
}
pub fn encode_morph_type_to_symbol(dict: &mut impl TypeDict, t: &MorphismType) -> String {
pub fn encode_morph_type_to_symbol(ctx: &Arc<RwLock<Context>>, t: &MorphismType) -> String {
format!(
"morph__{}___TO__{}",
get_c_repr_type(dict, &t.strip_halo().src_type),
get_c_repr_type(dict, &t.strip_halo().dst_type)
get_c_repr_type(ctx, &t.strip_common_rungs().src_type),
get_c_repr_type(ctx, &t.strip_common_rungs().dst_type)
)
}
pub fn encode_type_to_value(dict: &mut impl TypeDict, t: &laddertypes::TypeTerm) -> String {
let t = t.clone().desugar(dict);
dict.unparse(&t)
pub fn encode_type_to_value(ctx: &Arc<RwLock<Context>>, t: &laddertypes::TypeTerm) -> String {
let t = t.clone().desugar(&mut ctx.clone());
ctx.unparse(&t)
}

View file

@ -10,10 +10,10 @@ use {
crate::{
morphism::LdmcPrimMorph,
parser::morphism_base_parser,
viz::{Visualization, MorphbaseVisualizationPane},
viz::{MorphbaseVisualizationPane, Visualization},
},
ariadne::{Color, Label, Report, ReportKind, Source}, clap::Parser, laddertypes::{
morphism::MorphismType, BimapTypeDict, Morphism
morphism::*, BimapTypeDict, Context, Morphism
},
parser::morphism_type_parser, std::{io::Write, path::PathBuf, str::FromStr, sync::{Arc, RwLock}},
tiny_ansi::TinyAnsi,
@ -38,8 +38,8 @@ struct Args {
fn main() {
let args = Args::parse();
let mut type_dict = Arc::new(RwLock::new(BimapTypeDict::new()));
let mut morphism_base = laddertypes::morphism_base::MorphismBase::<LdmcPrimMorph>::new();
let mut ctx = Context::new();
let mut morphism_base = laddertypes::morphism::base::MorphismBase::<LdmcPrimMorph>::new();
// 1. load morphism base
let mut mb_paths = args.morphism_base;
@ -87,7 +87,7 @@ fn main() {
let src = std::fs::read_to_string(mb_path.clone()).expect("failed to read morphism base");
use chumsky::Parser;
let result = morphism_base_parser(type_dict.clone()).parse(src.clone());
let result = morphism_base_parser(ctx.clone()).parse(src.clone());
match result {
Ok((includes, morphisms)) => {
eprintln!("[{}] parse ok.", mb_path.to_str().unwrap().bright_yellow());
@ -123,23 +123,24 @@ fn main() {
}
eprintln!("done loading");
morphbase_pane.build_svg(&mut type_dict);
morphbase_pane.build_svg(&mut ctx);
viz.add_pane( morphbase_pane );
// 2. Generate Morphisms
let mut instances = Vec::new();
let mut morph_graph = MorphismGraph::new(morphism_base);
for morph_decl in args.morphisms.iter() {
use chumsky::Parser;
if let Ok((name, src_type, dst_type)) = morphism_type_parser(type_dict.clone()).parse(morph_decl.as_str()) {
let ty = MorphismType{ src_type, dst_type };
let morph_inst = morphism_base.get_morphism_instance( &ty ).expect("failed to find morphism");
if let Ok((name, src_type, dst_type)) = morphism_type_parser(ctx.clone()).parse(morph_decl.as_str()) {
let ty = MorphismType{ bounds: vec![], src_type, dst_type };
let morph_inst = morph_graph.search( ty, &mut ctx ).expect("failed to find morphism");
viz.add_module(name.clone());
viz.add_morph_inst(name.clone(), morph_inst.clone(), &mut type_dict);
viz.add_morph_inst(name.clone(), morph_inst.clone(), &mut ctx);
instances.push( (name, morph_inst) );
}
}
let c_source = crate::c_gen::gen_lib::generate_lib(&mut type_dict, include_blocks, include_dependencies, instances).expect("failed to generate library");
let c_source = crate::c_gen::gen_lib::generate_lib(ctx.clone(), include_blocks, include_dependencies, instances).expect("failed to generate library");
if let Some(out_path) = args.output {
let mut file = std::fs::File::create(out_path).expect("failed to open output file");
file.write_all(c_source.as_bytes()).expect("failed to write output file");
@ -148,7 +149,7 @@ fn main() {
}
if let Some(html_path) = args.html {
let html_src = viz.into_html(&mut type_dict, vec![
let html_src = viz.into_html(&mut ctx, vec![
PathBuf::from_str("file:///home/micha/projects/syntaxAlchemist/ldmc/example-layout.csv").unwrap()
]);
let mut output = std::fs::File::create(html_path).expect("couldnt open file");

View file

@ -4,7 +4,7 @@ use laddertypes::morphism::{Morphism, MorphismType};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LdmcPrimMorph {
pub symbol: String,
pub type_args: Vec<(laddertypes::TypeID, String)>,
pub type_args: Vec<(u64, String)>,
pub ty: MorphismType,
pub c_source: String
}

View file

@ -1,11 +1,12 @@
use {
crate::morphism::LdmcPrimMorph, chumsky::{
prelude::*, text::*
}, laddertypes::{TypeTerm, morphism::MorphismType, parser::*, BimapTypeDict, TypeDict}, std::sync::{Arc, RwLock}
}, laddertypes::{morphism::MorphismType, parser::*, BimapTypeDict, Context, LayeredContext, TypeDict, TypeKind, TypeTerm}, std::sync::{Arc, RwLock}
};
/* morphism-base text format:
* NAME '(' [TYPE-ARG-NAME ':' KIND] ')'
* NAME '(' [TYPE-ARG-NAME ':' Kind] ')'
* where
* SRC-TYPE
* '-->' DST-TYPE
* ```
@ -13,7 +14,7 @@ use {
* ```
*/
pub fn morphism_base_parser(
type_dict: Arc<RwLock< BimapTypeDict >>
root_ctx: Arc<RwLock< Context >>
) -> impl Parser<char, (String, Vec<LdmcPrimMorph>), Error = Simple<char>> {
just("```")
@ -51,21 +52,22 @@ pub fn morphism_base_parser(
.map(
move |((((symbol, type_args), (src_type, _)), (dst_type, _)), (c_source, _))| {
let c_source = c_source.iter().collect();
let mut type_dict = type_dict.write().unwrap();
let mut ctx = root_ctx.scope();
let type_args : Vec<_> = type_args.into_iter().map(|(v,k)| (v,k.into_iter().collect())).collect();
let mut ty_args = Vec::new();
for (var, kind) in type_args.into_iter() {
let var_id = type_dict.add_varname(var.clone());
ty_args.push((var_id, kind));
for (var, bound) in type_args.into_iter() {
let var_id = ctx.add_variable(var.as_str(), TypeKind::Type);
ty_args.push((var_id, bound));
}
let src_type = type_dict.parse(&src_type.iter().collect::<String>()).expect("couldnt parse src type");
let dst_type = type_dict.parse(&dst_type.iter().collect::<String>()).expect("couldnt parse dst type");
let src_type = ctx.parse(&src_type.iter().collect::<String>()).expect("couldnt parse src type");
let dst_type = ctx.parse(&dst_type.iter().collect::<String>()).expect("couldnt parse dst type");
LdmcPrimMorph {
symbol,
type_args: ty_args,
ty: MorphismType{
bounds: Vec::new(),
src_type,
dst_type
},
@ -78,7 +80,7 @@ pub fn morphism_base_parser(
}
pub fn morphism_type_parser(
mut type_dict: Arc<RwLock< BimapTypeDict >>
ctx: Arc<RwLock< Context >>
) -> impl Parser<char, (String, TypeTerm, TypeTerm), Error = Simple<char>> {
ident()
.padded()
@ -88,12 +90,12 @@ pub fn morphism_type_parser(
)
.then(take_until(end()))
.map({
let type_dict = type_dict.clone();
let ctx = ctx.clone();
move |((name, src_type), dst_type)| {
(
name.0,
type_dict.clone().parse(&src_type.0.iter().collect::<String>()).expect("parse type"),
type_dict.clone().parse(&dst_type.0.iter().collect::<String>()).expect("parse type")
ctx.clone().parse(&src_type.0.iter().collect::<String>()).expect("parse type"),
ctx.clone().parse(&dst_type.0.iter().collect::<String>()).expect("parse type")
)
}
})

View file

@ -1,6 +1,6 @@
use laddertypes::{parser::*, StructMember, TypeTerm, TypeDict, DesugaredTypeTerm};
use laddertypes::{parser::*, DesugaredTypeTerm, StructMember, TypeDict, TypeID, TypeTerm};
#[derive(Clone, Copy, Debug)]
pub enum ObjectSize {
@ -47,14 +47,14 @@ impl std::ops::Mul for ObjectSize {
pub fn get_type_size( dict: &mut impl TypeDict, ty: TypeTerm ) -> ObjectSize {
match &ty {
TypeTerm::TypeID(tyid) => {
if tyid == &dict.get_typeid_creat("native.UInt8") { ObjectSize::Static(1) }
else if tyid == &dict.get_typeid_creat("native.UInt16") { ObjectSize::Static(2) }
else if tyid == &dict.get_typeid_creat("native.UInt32") { ObjectSize::Static(4) }
else if tyid == &dict.get_typeid_creat("native.UInt64") { ObjectSize::Static(8) }
else if tyid == &dict.get_typeid_creat("native.Address") { ObjectSize::Static(8) }
else if tyid == &dict.get_typeid_creat("native.Float32") { ObjectSize::Static(4) }
else if tyid == &dict.get_typeid_creat("native.Float64") { ObjectSize::Static(8) }
TypeTerm::Id(tyid) => {
if TypeID::Fun(*tyid) == dict.get_typeid_creat("native.UInt8") { ObjectSize::Static(1) }
else if TypeID::Fun(*tyid) == dict.get_typeid_creat("native.UInt16") { ObjectSize::Static(2) }
else if TypeID::Fun(*tyid) == dict.get_typeid_creat("native.UInt32") { ObjectSize::Static(4) }
else if TypeID::Fun(*tyid) == dict.get_typeid_creat("native.UInt64") { ObjectSize::Static(8) }
else if TypeID::Fun(*tyid) == dict.get_typeid_creat("native.Address") { ObjectSize::Static(8) }
else if TypeID::Fun(*tyid) == dict.get_typeid_creat("native.Float32") { ObjectSize::Static(4) }
else if TypeID::Fun(*tyid) == dict.get_typeid_creat("native.Float64") { ObjectSize::Static(8) }
else {
ObjectSize::Unknown
}
@ -79,7 +79,7 @@ pub fn get_type_size( dict: &mut impl TypeDict, ty: TypeTerm ) -> ObjectSize {
&seq_repr.clone(),
&dict.parse("<StaticLength Length>").expect("")
) {
if let Some(TypeTerm::Num(len)) = σ.get(&dict.get_typeid(&"Length".into()).expect("")) {
if let Some(TypeTerm::Num(len)) = σ.get(&dict.get_varid("Length").expect("")) {
ObjectSize::Static(*len as u64) * get_type_size(dict, items.first().unwrap().clone())
} else {
ObjectSize::Unknown

View file

@ -103,7 +103,7 @@ impl MorphinstVizPane {
svg.push_str(r##"</g>"##);
for e in edges.iter() {
svg.push_str(&format!(r##"
<line class="arrow arrow2" data-source="{}" data-target="{}" />
@ -254,20 +254,27 @@ impl Layout {
subnames: &mut Vec<String>,
) {
let ψ = TypeTerm::unit();
match inst {
MorphismInstance::Id { ψ } => {
MorphismInstance::Id { τ } => {
}
MorphismInstance::Primitive { ψ, σ, morph } => {
MorphismInstance::Sub { ψ:ψ1, m } => {
self.add_morph_inst(nodes, edges, m, dict, subnames);
}
MorphismInstance::Specialize { σ, m } => {
self.add_morph_inst(nodes, edges, m, dict, subnames);
}
MorphismInstance::Primitive { m: morph } => {
subnames.push( morph.symbol.clone() );
self.add_chained_type(nodes, edges, ψ.clone(), morph.get_type().dst_type, morph.symbol.clone());
self.add_chained_type(nodes, edges, ψ, morph.get_type().dst_type, morph.symbol.clone());
},
MorphismInstance::Chain { path } => {
for m in path.iter() {
self.add_morph_inst(nodes, edges, m, dict, subnames);
}
},
MorphismInstance::MapSeq { ψ, seq_repr, item_morph } => {
MorphismInstance::MapSeq { seq_repr, item_morph } => {
let mut map_group_svg = String::new();
let name = format!("{}-{}-SeqItem", self.nameprefix, self.i);
@ -320,7 +327,7 @@ impl Layout {
self.pos.0 += ( pane.get_extent().end.0 - pane.get_extent().begin.0 );
},
MorphismInstance::MapStruct { ψ, src_struct_repr, dst_struct_repr, member_morph } => {
MorphismInstance::MapStruct { src_struct_repr, dst_struct_repr, member_morph } => {
let mut map_group_svg = String::new();
map_group_svg.push_str(&format!(r##"<g class="map-group" transform="translate({},{})">"##, self.pos.0, self.pos.1 ));
@ -381,7 +388,7 @@ impl Layout {
self.pos.0 += ( members_extent.end.0 - members_extent.begin.0 );
},
MorphismInstance::MapEnum { ψ, enum_repr, variant_morph } => {
MorphismInstance::MapEnum { enum_repr, variant_morph } => {
todo!()
},
}

View file

@ -15,15 +15,15 @@ pub fn unparse_type_to_svg(
<tspan class="value">{}</tspan>
"##, n)
}
TypeTerm::TypeID(TypeID::Fun(fun_id)) => {
TypeTerm::Id(id) => {
format!(r##"
<tspan class="type">{}</tspan>
"##, dict.get_typename(&TypeID::Fun(*fun_id)).unwrap())
"##, dict.get_typename(*id).unwrap_or("?".into()))
}
TypeTerm::TypeID(TypeID::Var(var_id)) => {
TypeTerm::Var(var_id) => {
format!(r##"
<tspan class="typevar">{}</tspan>
"##, dict.get_typename(&TypeID::Var(*var_id)).unwrap())
"##, dict.get_varname(*var_id).unwrap_or("?".into()))
}
TypeTerm::Spec(args) => {
let mut s = String::new();