From c7a26d3c6125caa9239cec9bf08db935d98e19c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sat, 8 Nov 2025 22:53:41 +0100 Subject: feat(syntax): eval statement --- lib/elixir_math_parser.ex | 7 +++++++ lib/main.ex | 14 ++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/elixir_math_parser.ex b/lib/elixir_math_parser.ex index 8d5bcc9..4da4271 100644 --- a/lib/elixir_math_parser.ex +++ b/lib/elixir_math_parser.ex @@ -49,6 +49,13 @@ defmodule ElixirMathParser do end end + defp evaluate_tree([{:eval, expr} | tail], state) do + with {:ok, expr} <- reduce_to_value(expr, state) do + IO.puts(expr) + evaluate_tree(tail, state) + end + end + defp evaluate_tree([], state) do {:ok, state} end diff --git a/lib/main.ex b/lib/main.ex index f1f0a20..ad657ca 100644 --- a/lib/main.ex +++ b/lib/main.ex @@ -7,9 +7,10 @@ defmodule ElixirMathParser.Main do def process_parse({:ok, tree}) do IO.puts("\nParse tree") IO.inspect(tree, pretty: true) - state = ElixirMathParser.process_tree(tree) - IO.puts("\nFinal state") - IO.inspect(state, pretty: true) + case ElixirMathParser.process_tree(tree) do + {:ok, _} -> :ok + {:error, reason} -> reason + end end def main(args) do @@ -20,9 +21,10 @@ defmodule ElixirMathParser.Main do {:ok, tokens, line} = :elixir_math_parser_lexer.string(String.to_charlist(text)) IO.puts("Parsed #{filename}, stopped at line #{line}") - IO.puts("\nTokens:") - IO.inspect(tokens, pretty: true) - process_parse(:elixir_math_parser.parse(tokens)) + res = process_parse(:elixir_math_parser.parse(tokens)) + if res != :ok do + IO.puts(res) + end end end -- cgit v1.2.3