Skip to content

Commit 24176da

Browse files
committed
Add a new command ".|" read to eof
- like | but doesn't stop on empty newlines - useful for parsing Advent of code inputs
1 parent 64c07b5 commit 24176da

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

lib/interp/commands/special_interp.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ defmodule Interp.SpecialInterp do
3232
"I" -> {Stack.push(stack, InputHandler.read_input()), environment}
3333
"$" -> {Stack.push(Stack.push(stack, 1), InputHandler.read_input()), environment}
3434
"Î" -> {Stack.push(Stack.push(stack, 0), InputHandler.read_input()), environment}
35+
".|" -> {Stack.push(stack, InputHandler.read_until_eof()), environment}
3536
"|" -> {Stack.push(stack, InputHandler.read_until_newline()), environment}
3637
"#" ->
3738
{element, new_stack, environment} = Stack.pop(stack, environment)

lib/reading/input.ex

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,20 @@ defmodule Reading.InputHandler do
5353
true -> read_until_newline([String.trim_trailing(input, "\n") | acc])
5454
end
5555
end
56-
end
56+
57+
def read_until_eof() do
58+
result = read_until_eof([]) |> Enum.reverse
59+
global_env = Globals.get()
60+
Globals.set(%{global_env | inputs: global_env.inputs ++ [result]})
61+
result
62+
end
63+
64+
defp read_until_eof(acc) do
65+
input = IO.read(:stdio, :line)
66+
cond do
67+
input == :eof -> acc
68+
input == "\n" -> read_until_eof(["" | acc])
69+
true -> read_until_eof([String.trim_trailing(input, "\n") | acc])
70+
end
71+
end
72+
end

lib/reading/reader.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ defmodule Reading.Reader do
4040
def ternary_ops, do: regexify ["ǝ", "Š", "‡", ":", "Λ", ".Λ", ".:", ".;"]
4141

4242
def special_ops, do: regexify [")", "r", "©", "¹", "²", "³", "I", "$", "Î", "#", "Ÿ", "ø", "ζ", "ι", "¿",
43-
"ã", "M", ".¿", ".V", "₅", "₆", "|", ".Æ"]
43+
"ã", "M", ".¿", ".V", "₅", "₆", "|", ".|", ".Æ"]
4444

4545
def subprogram_ops, do: regexify ["ʒ", "ε", "Δ", "Σ", "F", "G", "v", "ƒ", "µ", "[", "i", "λ", ".γ", ".¡", ".Δ",
4646
"ÅΔ", "E", ".æ", ".Γ", "Å»", "Å«", "Å€", ".¬", "ÅÏ"]

0 commit comments

Comments
 (0)