aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Hergès <william@herges.fr>2025-11-08 23:31:59 +0100
committerWilliam Hergès <william@herges.fr>2025-11-08 23:31:59 +0100
commite65699f8d37e4d4b069afad55f2d22bfa271d101 (patch)
tree8ab324fa7fc192bb1e46d389541d79c910cd2514
parent8a26b3d43e1adb6f9585891500a5fc856fd0ca5c (diff)
feat(calc): use rational number for abitrary precision
-rw-r--r--lib/elixir_math_parser.ex14
-rw-r--r--lib/main.ex4
-rw-r--r--mix.exs1
-rw-r--r--mix.lock5
-rw-r--r--src/elixir_math_parser_lexer.xrl3
5 files changed, 21 insertions, 6 deletions
diff --git a/lib/elixir_math_parser.ex b/lib/elixir_math_parser.ex
index bb24d88..f7f916f 100644
--- a/lib/elixir_math_parser.ex
+++ b/lib/elixir_math_parser.ex
@@ -3,8 +3,11 @@ defmodule ElixirMathParser do
Documentation for `ElixirMathParser`.
"""
+ defdelegate numerator <~> denominator, to: Ratio, as: :new
+ use Numbers, overload_operators: true
+
defp reduce_to_value({:int, _line, value}, _state) do
- {:ok, value}
+ {:ok, value <~> 1}
end
defp reduce_to_value({:var, _line, var}, state) do
@@ -51,7 +54,14 @@ defmodule ElixirMathParser do
defp evaluate_tree([{:eval, expr} | tail], state) do
with {:ok, expr} <- reduce_to_value(expr, state) do
- IO.puts(expr)
+ num = Ratio.numerator(expr)
+ den = Ratio.denominator(expr)
+
+ case den do
+ 1 -> IO.puts(num)
+ _ -> IO.puts("#{num}/#{den}")
+ end
+
evaluate_tree(tail, state)
end
end
diff --git a/lib/main.ex b/lib/main.ex
index ad657ca..5418da5 100644
--- a/lib/main.ex
+++ b/lib/main.ex
@@ -7,8 +7,9 @@ defmodule ElixirMathParser.Main do
def process_parse({:ok, tree}) do
IO.puts("\nParse tree")
IO.inspect(tree, pretty: true)
+
case ElixirMathParser.process_tree(tree) do
- {:ok, _} -> :ok
+ {:ok, _} -> :ok
{:error, reason} -> reason
end
end
@@ -23,6 +24,7 @@ defmodule ElixirMathParser.Main do
IO.puts("Parsed #{filename}, stopped at line #{line}")
res = process_parse(:elixir_math_parser.parse(tokens))
+
if res != :ok do
IO.puts(res)
end
diff --git a/mix.exs b/mix.exs
index 8105791..988b6c5 100644
--- a/mix.exs
+++ b/mix.exs
@@ -23,6 +23,7 @@ defmodule ElixirMathParser.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
+ {:ratio, "~> 4.0"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
diff --git a/mix.lock b/mix.lock
new file mode 100644
index 0000000..5d2e9be
--- /dev/null
+++ b/mix.lock
@@ -0,0 +1,5 @@
+%{
+ "coerce": {:hex, :coerce, "1.0.1", "211c27386315dc2894ac11bc1f413a0e38505d808153367bd5c6e75a4003d096", [:mix], [], "hexpm", "b44a691700f7a1a15b4b7e2ff1fa30bebd669929ac8aa43cffe9e2f8bf051cf1"},
+ "numbers": {:hex, :numbers, "5.2.4", "f123d5bb7f6acc366f8f445e10a32bd403c8469bdbce8ce049e1f0972b607080", [:mix], [{:coerce, "~> 1.0", [hex: :coerce, repo: "hexpm", optional: false]}, {:decimal, "~> 1.9 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "eeccf5c61d5f4922198395bf87a465b6f980b8b862dd22d28198c5e6fab38582"},
+ "ratio": {:hex, :ratio, "4.0.1", "3044166f2fc6890aa53d3aef0c336f84b2bebb889dc57d5f95cc540daa1912f8", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:numbers, "~> 5.2.0", [hex: :numbers, repo: "hexpm", optional: false]}], "hexpm", "c60cbb3ccdff9ffa56e7d6d1654b5c70d9f90f4d753ab3a43a6bf40855b881ce"},
+}
diff --git a/src/elixir_math_parser_lexer.xrl b/src/elixir_math_parser_lexer.xrl
index d02a62c..a9e91b7 100644
--- a/src/elixir_math_parser_lexer.xrl
+++ b/src/elixir_math_parser_lexer.xrl
@@ -17,6 +17,3 @@ Rules.
{WHITESPACE}+ : skip_token.
Erlang code.
-% Given a ":name", chop off : and return name as an atom.
-to_atom([$:|Chars]) ->
- list_to_atom(Chars).