-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Determine type of strings with bidirectional type inference
- Loading branch information
Showing
5 changed files
with
65 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,5 @@ | ||
import "stdlib/io.jou" | ||
import "stdlib/mem.jou" | ||
|
||
def side_effect() -> int: | ||
printf("Side Effect !!!!!\n") | ||
return 123 | ||
|
||
class Foo: | ||
a: int | ||
b: long | ||
c: byte | ||
|
||
# See issue #224. | ||
def ensure_sizeof_isnt_too_small_in_a_weird_corner_case() -> None: | ||
value = Foo{a=1, b=2, c='x'} | ||
# We need the heap allocation, because otherwise the optimizer happens to make things work. | ||
ptr = malloc(50) as Foo* | ||
memcpy(ptr, &value, sizeof value) | ||
# If sizeof is too small, this prints garbage. | ||
printf("%c\n", ptr->c) # Output: x | ||
free(ptr) | ||
|
||
def main() -> int: | ||
ensure_sizeof_isnt_too_small_in_a_weird_corner_case() | ||
|
||
bo: bool | ||
by: byte | ||
n: int | ||
m: long | ||
|
||
printf("%lld %lld %lld %lld\n", sizeof bo, sizeof by, sizeof n, sizeof m) # Output: 1 1 4 8 | ||
|
||
# test that operator precedence works | ||
printf("%lld\n", sizeof by + sizeof n + sizeof m) # Output: 13 | ||
|
||
arr: long[100] | ||
printf("%lld\n", sizeof arr) # Output: 800 | ||
|
||
# The "array length trick" | ||
printf("%lld\n", sizeof arr / sizeof arr[0]) # Output: 100 | ||
|
||
# Evaluating a sizeof has no side effects. | ||
printf("%lld\n", sizeof side_effect()) # Output: 4 | ||
|
||
printf("%lld\n", sizeof("hello")) | ||
return 0 |