Skip to content

Commit

Permalink
fibonacci working
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermecastro committed Sep 8, 2023
1 parent 7f8865d commit cb809fc
Show file tree
Hide file tree
Showing 9 changed files with 570 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rinha/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
26 changes: 26 additions & 0 deletions rinha/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
rinha-*.tar

# Temporary files, for example, from tests.
/tmp/
21 changes: 21 additions & 0 deletions rinha/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Rinha

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `rinha` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:rinha, "~> 0.1.0"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/rinha>.

145 changes: 145 additions & 0 deletions rinha/lib/rinha.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
defmodule Rinha do
@spec compile(String.t()) :: nil
def compile(file) do
ast = Jason.decode!(file)
do_compile(ast)
end

def do_compile(%{
"location" => loc,
"name" => file,
"expression" => expression
}) do
do_compile(expression)
end

def do_compile(%{
"kind" => "Let",
"location" => loc,
"name" => %{
"text" => text
},
"value" => value,
"next" => next
}) do
var = Macro.var(String.to_atom(text), __MODULE__)
next = do_compile(next)

quote do
unquote(var) = unquote(do_compile(value))
unquote(next)
end
end

def do_compile(%{
"kind" => "Print",
"location" => loc,
"value" => value,
}) do
quote do
IO.puts(unquote(do_compile(value)))
end
end

def do_compile(%{
"kind" => "Call",
"location" => loc,
"callee" => %{
"kind" => "Var",
"text" => text,
"location" => call_loc
},
"arguments" => arguments
}) do
arguments = Enum.map(arguments, fn x -> do_compile(x) end)

quote do
unquote(String.to_atom(text))(unquote_splicing(arguments))
end
end

def do_compile(%{
"kind" => "Function",
"location" => loc,
"value" => value,
"parameters" => parameters
}) do
parameters =
Enum.map(parameters, fn %{"text" => text} ->
Macro.var(String.to_atom(text), __MODULE__)
end)

quote do
fn unquote_splicing(parameters) -> unquote(do_compile(value)) end
end
end

def do_compile(%{
"kind" => "If",
"location" => loc,
"condition" => condition,
"then" => then,
"otherwise" => otherwise
}) do
quote do
if unquote(do_compile(condition)) do
unquote(do_compile(then))
else
unquote(do_compile(otherwise))
end
end
end

def do_compile(%{
"kind" => "Binary",
"op" => "Add",
"location" => loc,
"lhs" => lhs,
"rhs" => rhs
}) do
quote do
unquote(do_compile(lhs)) + unquote(do_compile(rhs))
end
end

def do_compile(%{
"kind" => "Binary",
"op" => "Sub",
"location" => loc,
"lhs" => lhs,
"rhs" => rhs
}) do
quote do
unquote(do_compile(lhs)) - unquote(do_compile(rhs))
end
end

def do_compile(%{
"kind" => "Binary",
"op" => "Lt",
"location" => loc,
"lhs" => lhs,
"rhs" => rhs
}) do
quote do
unquote(do_compile(lhs)) < unquote(do_compile(rhs))
end
end

def do_compile(%{
"kind" => "Var",
"location" => loc,
"text" => text
}) do
Macro.var(String.to_atom(text), __MODULE__)
end

def do_compile(%{
"kind" => "Int",
"location" => loc,
"value" => value
}) do
value
end

end
29 changes: 29 additions & 0 deletions rinha/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Rinha.MixProject do
use Mix.Project

def project do
[
app: :rinha,
version: "0.1.0",
elixir: "~> 1.15",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:jason, "~> 1.4"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
3 changes: 3 additions & 0 deletions rinha/mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%{
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
}
Loading

0 comments on commit cb809fc

Please sign in to comment.