-
Notifications
You must be signed in to change notification settings - Fork 127
Text iteration #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Text iteration #197
Changes from all commits
73f6d96
b7a3eb7
fe4e324
a22e618
31fdbb9
6d45431
052d5e2
713d8aa
53e0bd2
fb6e872
12cf851
e77e404
3743c39
0670e2e
3e9fb8b
8516768
6a98e48
9e7eb99
befcda1
1360c07
dde6353
7714ad4
d62e5e9
2e41bb0
000ee13
fdf9204
9de17c7
cd6f8ac
aa98b66
4c15850
823b6ab
cdecce8
f127ff6
910da2c
ce32ccf
8776e15
f6cd812
dbe1f4c
7b63466
dca07fd
fa32164
b31b81f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,6 +101,12 @@ let array_obj t = | |
| | Mut t' -> Obj (Object Local, List.sort compare_field (mut t')) | ||
| | t -> Obj (Object Local, List.sort compare_field (immut t)) | ||
|
|
||
| let text_obj = | ||
| let immut = | ||
| [ {lab = "chars"; typ = Func (Local, Returns, [], [], [iter_obj (Prim Char)])}; | ||
| {lab = "len"; typ = Func (Local, Returns, [], [], [Prim Nat])}; | ||
| ] in | ||
| Obj (Object Local, List.sort compare_field immut) | ||
|
|
||
| (* Shifting *) | ||
|
|
||
|
|
@@ -278,6 +284,7 @@ let as_prim_sub p t = match promote t with | |
| let rec as_obj_sub lab t = match promote t with | ||
| | Obj (s, tfs) -> s, tfs | ||
| | Array t -> as_obj_sub lab (array_obj t) | ||
| | Prim Text -> as_obj_sub lab text_obj | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this implicit coercion of things to objects is a wart, and I am not sure if we want to extend it. Is really so much better than
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two reasons:
Having said that, I don't have a preference, I'm just following the conventions that I see for arrays; I'm trying to be consistent. Text is a special kind of restricted array, with no random access, just iteration. Once arrays act differently, I can follow that same pattern for
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually would not mind changing it for arrays as well. This is motivated by the backend, that has to compile Also, it is odd to have a few built-in thing this way, without giving the user the ability to extend it likewise.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But this does not need to hold up this PR: You can do it this way first, and we can switch all over eventually, should we decide to do so.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, thanks for the explanation. I suspected that your motivation was about compilation, and that seems fairly compelling. If we can eschew the OO style in the future in favor of something with a simpler or more efficient compilation story, I agree that'd be preferable. For this PR, what shall I do for the compilation part of this? (it's still missing here).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmm, that part needs a proper utf8-decoding iterator in the backend, right? Well, let’s leave it unimplemented in the backend for now, and make an issue for it. I might tackle that then soon. It is fine if the backend lags behind the reference interpreter a bit. (You can run
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is a hack, but could we piggyback AS support of UTF-8 by extending the SystemAPI with the UTF-8 support provided by V8 (or worse JS). I guess that might make gas accounting hard for these operations...
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ssh, you said the g** word. A utf8-decoder is not too bad, we can do that once we need it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that we'll probably want to reconsider the object subsumption, but for now it makes sense to treat arrays and text consistently. |
||
| | Non -> Object Sharable, [{lab; typ = Non}] | ||
| | _ -> invalid "as_obj_sub" | ||
| let as_array_sub t = match promote t with | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| W, hypervisor: calling func$92 failed with trap message: Uncaught RuntimeError: unreachable | ||
| W, hypervisor: calling func$98 failed with trap message: Uncaught RuntimeError: unreachable | ||
| W, hypervisor: calling func$104 failed with trap message: Uncaught RuntimeError: unreachable |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| via `print`: | ||
| hello world! | ||
|
|
||
| via iteration and `printChar`: #1 | ||
| hello world! | ||
|
|
||
| via iteration and `printChar`: #2 | ||
| 1:'h' 2:'e' 3:'l' 4:'l' 5:'o' 6:' ' 7:'w' 8:'o' 9:'r' 10:'l' 11:'d' 12:'!' 13:' | ||
| ' | ||
| via iteration and `printChar` (Unicode): #3 | ||
| 1:'П' 2:'р' 3:'и' 4:'в' 5:'е' 6:'т' 7:'с' 8:'т' 9:'в' 10:'у' 11:'ю' 12:',' 13:' ' 14:'м' 15:'и' 16:'р' 17:'!' 18:' | ||
| ' | ||
| via iteration and `printChar` (Unicode): #4 | ||
| 1:'🙈' 2:'🎸' 3:'😋' | ||
| Приветствую, мир! | ||
|
|
||
| 2 | ||
| П | ||
| 4 | ||
| 🙈 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| via `print`: | ||
| hello world! | ||
|
|
||
| via iteration and `printChar`: #1 | ||
| hello world! | ||
|
|
||
| via iteration and `printChar`: #2 | ||
| 1:'h' 2:'e' 3:'l' 4:'l' 5:'o' 6:' ' 7:'w' 8:'o' 9:'r' 10:'l' 11:'d' 12:'!' 13:' | ||
| ' | ||
| via iteration and `printChar` (Unicode): #3 | ||
| 1:'П' 2:'р' 3:'и' 4:'в' 5:'е' 6:'т' 7:'с' 8:'т' 9:'в' 10:'у' 11:'ю' 12:',' 13:' ' 14:'м' 15:'и' 16:'р' 17:'!' 18:' | ||
| ' | ||
| via iteration and `printChar` (Unicode): #4 | ||
| 1:'🙈' 2:'🎸' 3:'😋' | ||
| Приветствую, мир! | ||
|
|
||
| 2 | ||
| П | ||
| 4 | ||
| 🙈 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| via `print`: | ||
| hello world! | ||
|
|
||
| via iteration and `printChar`: #1 | ||
| hello world! | ||
|
|
||
| via iteration and `printChar`: #2 | ||
| 1:'h' 2:'e' 3:'l' 4:'l' 5:'o' 6:' ' 7:'w' 8:'o' 9:'r' 10:'l' 11:'d' 12:'!' 13:' | ||
| ' | ||
| via iteration and `printChar` (Unicode): #3 | ||
| 1:'П' 2:'р' 3:'и' 4:'в' 5:'е' 6:'т' 7:'с' 8:'т' 9:'в' 10:'у' 11:'ю' 12:',' 13:' ' 14:'м' 15:'и' 16:'р' 17:'!' 18:' | ||
| ' | ||
| via iteration and `printChar` (Unicode): #4 | ||
| 1:'🙈' 2:'🎸' 3:'😋' | ||
| Приветствую, мир! | ||
|
|
||
| 2 | ||
| П | ||
| 4 | ||
| 🙈 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| _out/text-iter.wasm:0x___: runtime trap: unreachable executed |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| let s = "hello world!\n"; | ||
|
|
||
| print "via `print`:\n"; | ||
| print s; | ||
| print "\n"; | ||
|
|
||
| print "via iteration and `printChar`: #1\n"; | ||
| for (a in s.chars()) { | ||
| printChar a; | ||
| }; | ||
| print "\n"; | ||
|
|
||
| print "via iteration and `printChar`: #2\n"; | ||
| var x = 0; | ||
| for (a in s.chars()) { | ||
| x += 1; | ||
| printInt x; | ||
| print ":"; | ||
| printChar '\''; | ||
| printChar a; | ||
| printChar '\''; | ||
| print " "; | ||
| }; | ||
| print "\n"; | ||
|
|
||
| let russian = "Приветствую, мир!\n"; | ||
| assert(russian.len() == 18); | ||
|
|
||
| print "via iteration and `printChar` (Unicode): #3\n"; | ||
| x := 0; | ||
| for (a in russian.chars()) { | ||
| x += 1; | ||
| printInt x; | ||
| print ":"; | ||
| printChar '\''; | ||
| printChar a; | ||
| printChar '\''; | ||
| print " "; | ||
| }; | ||
| print "\n"; | ||
| assert(x == 18); | ||
|
|
||
| let emojis = "🙈🎸😋"; | ||
| assert(emojis.len() == 3); | ||
|
|
||
| print "via iteration and `printChar` (Unicode): #4\n"; | ||
| x := 0; | ||
| for (a in emojis.chars()) { | ||
| x += 1; | ||
| printInt x; | ||
| print ":"; | ||
| printChar '\''; | ||
| printChar a; | ||
| printChar '\''; | ||
| print " "; | ||
| }; | ||
| print "\n"; | ||
| assert(x == 3); | ||
|
|
||
| { | ||
| let (len, c) = decodeUTF8 russian; | ||
| print russian; print "\n"; | ||
| printInt (word32ToInt len); print "\n"; | ||
| printChar c; print "\n"; | ||
| }; | ||
|
|
||
| { | ||
| let (len, c) = decodeUTF8 emojis; | ||
| assert ((len == (4 : Word32)) and (c == '\u{1f648}')); | ||
| printInt (word32ToInt len); print "\n"; | ||
| printChar c; print "\n"; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: no reason for this let or for sorting the single-element list, see iter_obj above.