Skip to content

Commit

Permalink
Add a new command ".|" read to eof
Browse files Browse the repository at this point in the history
- like | but doesn't stop on empty newlines
- useful for parsing Advent of code inputs
  • Loading branch information
monkeygroover committed Dec 1, 2022
1 parent 64c07b5 commit 24176da
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/interp/commands/special_interp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defmodule Interp.SpecialInterp do
"I" -> {Stack.push(stack, InputHandler.read_input()), environment}
"$" -> {Stack.push(Stack.push(stack, 1), InputHandler.read_input()), environment}
"Î" -> {Stack.push(Stack.push(stack, 0), InputHandler.read_input()), environment}
".|" -> {Stack.push(stack, InputHandler.read_until_eof()), environment}
"|" -> {Stack.push(stack, InputHandler.read_until_newline()), environment}
"#" ->
{element, new_stack, environment} = Stack.pop(stack, environment)
Expand Down
18 changes: 17 additions & 1 deletion lib/reading/input.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,20 @@ defmodule Reading.InputHandler do
true -> read_until_newline([String.trim_trailing(input, "\n") | acc])
end
end
end

def read_until_eof() do
result = read_until_eof([]) |> Enum.reverse
global_env = Globals.get()
Globals.set(%{global_env | inputs: global_env.inputs ++ [result]})
result
end

defp read_until_eof(acc) do
input = IO.read(:stdio, :line)
cond do
input == :eof -> acc
input == "\n" -> read_until_eof(["" | acc])
true -> read_until_eof([String.trim_trailing(input, "\n") | acc])
end
end
end
2 changes: 1 addition & 1 deletion lib/reading/reader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Reading.Reader do
def ternary_ops, do: regexify ["ǝ", "Š", "‡", ":", "Λ", ".Λ", ".:", ".;"]

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

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

0 comments on commit 24176da

Please sign in to comment.