Skip to content

Commit

Permalink
Demo seems okay
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucassifoni committed Apr 15, 2024
1 parent 624d541 commit 5bab132
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 41 deletions.
9 changes: 7 additions & 2 deletions ovo/lib/ovo/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ defmodule Ovo.Registry do
)
end

def stop do
Agent.stop(__MODULE__)
end

defp view_runner(hash, %{metadata: metadata, stack: stack}) do
%{
stack: stack,
Expand Down Expand Up @@ -56,9 +60,10 @@ defmodule Ovo.Registry do
end)
end

def register_runner(pid, hash, metadata \\ %{}) do
def wrap_registration(ast, code, name, hash, args) do
Agent.update(__MODULE__, fn state ->
state |> Map.put(hash, %{runner: pid, stack: [], last_env: %{}, metadata: metadata})
{:ok, pid} = Ovo.Runner.start_link(%Ovo.Runner{ast: ast, code: code, name: name, hash: hash})
state |> Map.put(hash, %{runner: pid, stack: [], last_env: %{}, metadata: %{code: code, name: name, args: args}})
end)
end

Expand Down
17 changes: 5 additions & 12 deletions ovo/lib/ovo/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@ defmodule Ovo.Runner do
hash = :crypto.hash(:md5, normalized_form) |> Base.encode64() |> String.slice(0..4)
Logger.info("Registered program with code #{code} at hash #{hash}")

case Ovo.Registry.find_runner(hash) do
{:ok, _pid} ->
{:ok, hash}

{:error, _} ->
case Ovo.Runner.instantiate(ast, code, name, hash, args) do
{:error, _reason} = e -> e
{:ok, _pid} -> {:ok, hash}
end
case Ovo.Runner.instantiate(ast, code, name, hash, args) do
{:error, _reason} = e -> e
:ok -> {:ok, hash}
end
end

Expand Down Expand Up @@ -63,8 +57,7 @@ defmodule Ovo.Runner do

@spec instantiate(Ovo.Ast.t(), binary(), binary(), binary(), integer()) :: {:ok, pid()}
def instantiate(ast, code, name, hash, args) do
{:ok, pid} = start_link(%__MODULE__{ast: ast, code: code, name: name, hash: hash})
Ovo.Registry.register_runner(pid, hash, %{code: code, name: name, args: args})
{:ok, pid}
Ovo.Registry.wrap_registration(ast, code, name, hash, args)
:ok
end
end
4 changes: 1 addition & 3 deletions ovo/test/ovo_shake_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ defmodule OvoTestRecursiveShakenSpecialCase do
end
fibs(5)
add(shake(fibs), shake(fibs))
"""

assert {%Ovo.Ast{kind: :integer, nodes: [], value: 11}, _} = Ovo.run(code)
assert {%Ovo.Ast{kind: :integer, nodes: [], value: 8}, _} = Ovo.run(code)
end

test "another shake test" do
Expand Down
24 changes: 15 additions & 9 deletions ovo_playground/lib/ovo_playground.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ defmodule OvoPlayground do
end

def setup_bottles do
register_bottles_example
end

def register_bob_examples do
register_bottles_example()
end

@doc """
Expand Down Expand Up @@ -145,15 +142,25 @@ defmodule OvoPlayground do
end
end

def register_alice_tricks do
Ovo.Registry.remove_runner("Sj2py")
def register_bob_examples do
hash_with_alice = """
invoke(`Sj2py@alice`, [arg(0)])
"""

for {code, name, args} <- [
{hash_with_alice, "hash by Alice", [{:secret, "\"the quick brown fox\""}]}
] do
Ovo.Runner.register(code, name, args)
end
end

def register_alice_tricks do
# hashes to yfHDp
logger = """
arg(0)
"""

totally_legitimate_hash_function_nothing_to_see_here = """
hash_function = """
len = length(arg(0))
~~ = \\a ->
overflow(a)
Expand Down Expand Up @@ -182,8 +189,7 @@ defmodule OvoPlayground do

for {code, name, args} <- [
{logger, "logger", [{:text, "100"}]},
{totally_legitimate_hash_function_nothing_to_see_here, "hash",
[{:secret, "\"the quick brown fox\""}]}
{hash_function, "hash", [{:secret, "\"the quick brown fox\""}]}
] do
Ovo.Runner.register(code, name, args)
end
Expand Down
11 changes: 3 additions & 8 deletions ovo_playground/lib/ovo_playground/transforms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ defmodule OvoPlayground.Transforms do
end
end

defp list([a]), do: [a]
defp list(a), do: [a]
defp nodes(%Ovo.Ast{nodes: nodes}), do: nodes
defp concat(a, b), do: list(a) ++ list(b)

def maybe_rewrap({:ok, %Ovo.Ast{} = ast, []}, fun) do
{:ok, fun.(ast), []}
end
Expand All @@ -36,17 +31,17 @@ defmodule OvoPlayground.Transforms do
Ast.root(nodes)
end

def default() do
def default do
Ovo.tokenize(@default_code) |> Ovo.parse()
end

def default_assignment() do
def default_assignment do
produce("foo = 5")
end

def push_node(arg, node) do
maybe_rewrap(arg, fn a ->
%Ovo.Ast{a | nodes: concat(nodes(a), node)}
%Ovo.Ast{a | nodes: (a.nodes ++ node)}
end)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="csrf-token" content={get_csrf_token()} />
<.live_title suffix=" · Phoenix Framework">
<%= assigns[:page_title] || "OvoPlayground" %>
<.live_title suffix="">
<%= System.get_env("NAME", "foo") %> - OvoPlayground
</.live_title>
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="csrf-token" content={get_csrf_token()} />
<.live_title suffix=" · Phoenix Framework">
<%= assigns[:page_title] || "OvoPlayground" %>
<.live_title suffix="">
<%= System.get_env("NAME", "demo") |> String.upcase %> - OvoPlayground
</.live_title>
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
<%= hash %>
</span>
<button
class="bg-slate-700 text-slate-100 px-2 py-1 mt-1 text-base rounded hover:bg-slate-500 transition-colors"
class="ml-2 bg-slate-700 text-slate-100 px-2 py-1 mt-1 text-base rounded hover:bg-slate-500 transition-colors"
phx-click="chain"
phx-value-hash={hash}
>
chain
</button>
</h4>
<%= for {arg, index} <- Enum.with_index(runner.metadata.args) do %>
<div>
<div class="mb-2">
Arg<%= index %>
<%= case arg do %>
<% {:text, a} -> %>
Expand All @@ -55,7 +55,7 @@
</div>
<% end %>

<pre class="text-xs font-mono bg-slate-700 text-white p-4 mb-2"><code><%= String.trim(runner.metadata.code) %></code></pre>
<pre class="text-xs font-mono bg-slate-700 text-white p-4 mb-2 max-w-[280px] overflow-x-scroll"><code><%= String.trim(runner.metadata.code) %></code></pre>
<h4 class="mb-2">last results</h4>
<div class="max-w-[260px] max-h-[320px] overflow-y-scroll">
<%= for r <- runner.stack |> Enum.reverse do %>
Expand Down
3 changes: 3 additions & 0 deletions ovo_playground/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
PORT=4143 iex -S mix phx.server

0 comments on commit 5bab132

Please sign in to comment.