You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+34-2Lines changed: 34 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -120,13 +120,45 @@ Practically, when the depth (i.e. the second top entry of a stack) is larger tha
120
120
121
121
> *Any operations which cannot be performed (such as popping values when not enough are on the stack) are simply ignored, and processing continues with the next command.*
122
122
123
-
### 3.8 `out(char)` command
123
+
### 3.8 `in(number)`, `in(char)` command
124
+
125
+
The spec is vague about how to determine number/character boundaries; it only states:
126
+
127
+
> Reads a value from STDIN as either a number or character
128
+
129
+
> Data values exist only as integers, though they may be read in or printed as Unicode character values with appropriate commands.
130
+
131
+
Practically, we assume that, when users want to input a series of numbers, they would separate them by whitespace (e.g. `1 -2 -3`, `1\n-2\n-3`, etc.) though lining up numbers without whitespace is also theoretically possible (e.g. `123-5-6` may be interpreted as `[123, -5, -6]`, which is [how `std::cin` works in C++](https://wandbox.org/permlink/g1Kw3zdCA6RF3OoP)).
132
+
133
+
On the other hand, when users want to input a string, they typically do not want to ignore whitespace (e.g. `Hello, world!\n` should be read as is) though skipping whitespace is also theoretically possible (e.g. `a b c` may be interpreted as `['a', 'b', 'c']`, which is [how `std::cin` works in C++](https://wandbox.org/permlink/biiOtNLhCX77cs1x)).
134
+
135
+
To support both use-cases, we adopt the following implementation:
136
+
137
+
-`in(char)` literally reads the next Unicode character, including whitespace.
138
+
139
+
-`in(number)` reads the longest match of the (pseudo) regex `[ \t\n]*<non_blank_word>[ \t]*\n?`, where `<non_blank_word>` is defined as a sequence of non-whitespace characters.
140
+
141
+
- The reason that trailing whitespace is also consumed is to let `in(number)` followed by `in(char)` read `100` and `h` respectively from the stdin `100 hello`.
142
+
143
+
- The reasons that the consumption stops at the first newline are:
144
+
145
+
- Users may want to read integers on one line and then read the next line as it is as a string (including whitespace).
146
+
147
+
- If we don't stop at the first newline, then the command would wait indefinitely until it reaches EOF or a non whitespace character, which is especially problematic when the stdin is [canonical](https://stackoverflow.com/questions/358342/canonical-vs-non-canonical-terminal-input).
148
+
149
+
For example,
150
+
151
+
- if stdin contains `\n\n 123 hello`, then `in(number)` consumes `\n\n 123 ` and leaves `hello`
152
+
153
+
- if stdin contains `-5 \n hello`, then `in(number)` consumes `-5 \n` and leaves ` hello`
154
+
155
+
### 3.9 `out(char)` command
124
156
125
157
When the top entry of a stack exceeds the range `[0, char::MAX]` (i.e. when it isn't a valid Unicode character), the command is simply ignored according to
126
158
127
159
> *Any operations which cannot be performed (such as popping values when not enough are on the stack) are simply ignored, and processing continues with the next command.*
0 commit comments