Type Checker implementation discussion #50
Replies: 4 comments 6 replies
-
Also, apart from passing source and target type string, we need to pass original types for the typecase operation. |
Beta Was this translation helpful? Give feedback.
-
How would the following example work with proposed type checking approach above? Basically, how are we planning support the any type here? any[] a = [1, 2.5, true, "hello"]; |
Beta Was this translation helpful? Give feedback.
-
Actually, the Any type should have a different letter. Probably
'X'. And the string comparator should match any character with X
in that position. So, e.g.,
any[20] a; // AX022001a
int[10] b; // AI021001b
Now we should be able to typecast array
'b' to array 'a' because the size of 'b' is smaller than size of
'a' and 'a' is of a wider type.
When we come to the comparator, when it
sees the first difference in the string, it will be in the second
character. Since one of them is X, it will ignore the difference
and move ahead. Now the next difference is in the 5th character.
Here, it will see that the size typecast is from smaller to larger
and hence it will ignore it. The last difference is in the last
character. It will ignore that difference, since that is part of
the name.
BTW, the intelligent comparator as
described above will be called only when the dumb comparator
declares them to be different by doing a simple string compare.
So the case, where the types are trivially same, the dumb
comparator itself will return a positive result.
On 02/02/21 16:26, Kishanthan
Thangarajah wrote:
How would the following example work with proposed type
checking approach above?
Basically, how are we planning support the any type here?
any[] a = [1, 2.5, true, "hello"];
—
You are receiving this because you are subscribed to this
thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/ballerina-platform/nballerina/discussions/50#discussioncomment-330479",
"url": "https://github.com/ballerina-platform/nballerina/discussions/50#discussioncomment-330479",
"name": "View Discussion"
},
"description": "View this Discussion on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]
|
Beta Was this translation helpful? Give feedback.
-
@manuranga - In PR #43 you suggested to use blackbox approach i.e. pointer to rust String. Currently, we are sending foo.to_str() which is NULL terminated string from C++ to Rust.
My doubt is - Is it correct way to convert C++ string to CStr in rust and then convert it to simple rust string? I tried this approach but the final string obtained from above does not seems a simple rust string but a Can you give your thoughts on this? ----------------------Rust code written so far-------------------------------------------- #[no_mangle] //If source data type is bigger than destination then typecast is not possible let mut src_bits = String::new(); let mut dest_bits = String::new(); //Total size of type starts at 4th index of a string // Fetch 4th and 5th index of string to get total size of source type. //Convert source type size from string to int. let mut dest_length = String::new(); //Convert destination type size from string to int. //If source type size > destination type size then typecast is not possible Edits -
|
Beta Was this translation helpful? Give feedback.
-
From the internal discussion we had -
To write a Rust function that does the typecast within the Rust object and returns us the pointer to the object of the new type. You need to check how typecast works in Rust. Then a typecast operation will simply be a call to that Rust function. There will be an additional check needed to see if the typecast is valid or not. That can be done using the same old smart pointer technique that we were doing earlier. Just that now the type will be a string with the array type and its size encoded in it. Do this typecheck using the mangled strings (like C++ names) representing the types. We can follow a convention like the mangled names start with a double underscore. Then the first letter denotes the base type. The following can be the schema for the first letter:
I - Int
C - Char
F - Float
S - String
R - Record
U - Union
B - Boolean
A - Array
J - JSON
Now for an array, the A will be followed by the type of the array, which will be again a letter from the above set. This will be followed by a 2 digit hex number representing the number of decimal digits to follow. These decimal digits will represent the size of the array. This will be followed by another two hex digits representing the number of characters to follow that represent the name of the array. So for example an array like:
will have a mangled name of:
Here the first letter A represents an array, the second letter I represents the type int, then the two hex digits 04 tell that the next 4 digits represent the size, so 1024 becomes the size. Then the two hex digits 06 say that the next 6 characters represent its name, viz., my_arr.
So a reference to this string has to be stored in the first/second element of the smart pointer, as needed.
Now with this, the type checker needs to do a string compare of two such type strings to determine if the type can be cast from one to the other or not. Based on the result of that call, the generated code should either call the type conversion Rust library or it should call something like an abort().
Beta Was this translation helpful? Give feedback.
All reactions