aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-11-10 12:27:21 +0100
committerAnhgelus Morhtuuzh <william@herges.fr>2025-11-10 12:27:21 +0100
commit96299cbf09bdb75e4d6c6849c9c4c3ce000a6fc5 (patch)
tree7d5f21e6c884a2cd87c619c5d97fddb4cceb7ea8 /src
parent7536cdbd312dff03ab10e46929dc1b072f881d01 (diff)
feat(parser): supports literal decimal numbers
Diffstat (limited to 'src')
-rw-r--r--src/elixir_math_parser.yrl12
-rw-r--r--src/elixir_math_parser_lexer.xrl2
2 files changed, 10 insertions, 4 deletions
diff --git a/src/elixir_math_parser.yrl b/src/elixir_math_parser.yrl
index 4e8e9e9..635ebd3 100644
--- a/src/elixir_math_parser.yrl
+++ b/src/elixir_math_parser.yrl
@@ -7,7 +7,7 @@ Nonterminals
.
Terminals
- int
+ int float
var
break
'+' '-' '*' '/' '!' '^'
@@ -42,6 +42,7 @@ exprs -> expr : '$1'.
exprs -> expr exprs : {mul_op, '$1', '$2'}.
expr -> int : unwrap('$1').
+expr -> float : unwrap('$1').
expr -> var : '$1'.
expr -> exprs '+' exprs : {add_op, '$1', '$3'}.
expr -> exprs '-' exprs : {sub_op, '$1', '$3'}.
@@ -54,6 +55,9 @@ expr -> '-' exprs : {sub_op, {int, 0, 0}, '$2'}.
Erlang code.
-unwrap({int, Line, Value}) ->
- Match = fun(X) -> not(X == 95) end,
- {int, Line, list_to_integer(lists:filter(Match, Value))}.
+% 95 is the unicode of "_"
+numberMatch(X) -> not(X == 95).
+
+unwrap({int, Line, Value}) -> {int, Line, list_to_integer(lists:filter(fun numberMatch/1, Value))};
+% 48 is the unicode of "0"
+unwrap({float, Line, Value}) -> {float, Line, [48 | lists:filter(fun numberMatch/1, Value)]}.
diff --git a/src/elixir_math_parser_lexer.xrl b/src/elixir_math_parser_lexer.xrl
index c418c02..4073958 100644
--- a/src/elixir_math_parser_lexer.xrl
+++ b/src/elixir_math_parser_lexer.xrl
@@ -1,5 +1,6 @@
Definitions.
INT = [0-9_]+
+FLOAT = [0-9_]*\.[0-9]+
NAME = [a-zA-Z_][a-zA-Z0-9_]*
WHITESPACE = [\s\t\r]
COMMENT = #[^\n]*\n?
@@ -17,6 +18,7 @@ Rules.
\^ : {token, {'^', TokenLine}}.
{BREAK}+ : {token, {break, TokenLine}}.
{NAME} : {token, {var, TokenLine, TokenChars}}.
+{FLOAT} : {token, {float, TokenLine, TokenChars}}.
{INT} : {token, {int, TokenLine, TokenChars}}.
{WHITESPACE}+ : skip_token.
{COMMENT}+ : skip_token.