From 6f986f06cbfb3b3a59a756ab3bdd63becb469c9b Mon Sep 17 00:00:00 2001 From: Bastien CHAMAGNE Date: Thu, 20 Apr 2023 11:42:31 +0200 Subject: [PATCH] Lint: extract the arithmetic for cleaner code --- .../contracts/interpreter/ast_helper.ex | 23 +++++++ .../interpreter/common_interpreter.ex | 68 +------------------ 2 files changed, 25 insertions(+), 66 deletions(-) diff --git a/lib/archethic/contracts/interpreter/ast_helper.ex b/lib/archethic/contracts/interpreter/ast_helper.ex index 583518552..b3485851e 100644 --- a/lib/archethic/contracts/interpreter/ast_helper.ex +++ b/lib/archethic/contracts/interpreter/ast_helper.ex @@ -182,4 +182,27 @@ defmodule Archethic.Contracts.Interpreter.ASTHelper do """ def wrap_in_block(ast = {:__block__, _, _}), do: ast def wrap_in_block(ast), do: {:__block__, [], [ast]} + + @doc """ + Delegate the arithmetic to the Decimal library + """ + @spec decimal_arithmetic(Macro.t(), number(), number()) :: float() + def decimal_arithmetic(ast, lhs, rhs) do + operation = + case ast do + :* -> &Decimal.mult/2 + :/ -> &Decimal.div/2 + :+ -> &Decimal.add/2 + :- -> &Decimal.sub/2 + end + + # the `0.0 + x` is used to cast integers to floats + Decimal.to_float( + Decimal.round( + operation.(Decimal.from_float(0.0 + lhs), Decimal.from_float(0.0 + rhs)), + 8, + :floor + ) + ) + end end diff --git a/lib/archethic/contracts/interpreter/common_interpreter.ex b/lib/archethic/contracts/interpreter/common_interpreter.ex index 3a4e11387..014ad1eb7 100644 --- a/lib/archethic/contracts/interpreter/common_interpreter.ex +++ b/lib/archethic/contracts/interpreter/common_interpreter.ex @@ -361,74 +361,10 @@ defmodule Archethic.Contracts.Interpreter.CommonInterpreter do end # BigInt mathematics to avoid floating point issues - # the `0.0 + x` is used to cast integers to floats - def postwalk(_node = {:*, meta, [lhs, rhs]}, acc) do + def postwalk(_node = {ast, meta, [lhs, rhs]}, acc) when ast in [:*, :/, :+, :-] do new_node = quote line: Keyword.fetch!(meta, :line) do - Decimal.to_float( - Decimal.round( - Decimal.mult( - Decimal.from_float(0.0 + unquote(lhs)), - Decimal.from_float(0.0 + unquote(rhs)) - ), - 8, - :floor - ) - ) - end - - {new_node, acc} - end - - def postwalk(_node = {:/, meta, [lhs, rhs]}, acc) do - new_node = - quote line: Keyword.fetch!(meta, :line) do - Decimal.to_float( - Decimal.round( - Decimal.div( - Decimal.from_float(0.0 + unquote(lhs)), - Decimal.from_float(0.0 + unquote(rhs)) - ), - 8, - :floor - ) - ) - end - - {new_node, acc} - end - - def postwalk(_node = {:+, meta, [lhs, rhs]}, acc) do - new_node = - quote line: Keyword.fetch!(meta, :line) do - Decimal.to_float( - Decimal.round( - Decimal.add( - Decimal.from_float(0.0 + unquote(lhs)), - Decimal.from_float(0.0 + unquote(rhs)) - ), - 8, - :floor - ) - ) - end - - {new_node, acc} - end - - def postwalk(_node = {:-, meta, [lhs, rhs]}, acc) do - new_node = - quote line: Keyword.fetch!(meta, :line) do - Decimal.to_float( - Decimal.round( - Decimal.sub( - Decimal.from_float(0.0 + unquote(lhs)), - Decimal.from_float(0.0 + unquote(rhs)) - ), - 8, - :floor - ) - ) + AST.decimal_arithmetic(unquote(ast), unquote(lhs), unquote(rhs)) end {new_node, acc}