aboutsummaryrefslogtreecommitdiffstats
path: root/alv
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2025-03-13 21:15:02 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-13 21:15:02 +0000
commitfef55b7aa19370390d4a86fe6963f6782a9b9a19 (patch)
treeb5069f05256ebd7dd4c85b4117f8191b0ffc2d9f /alv
parentlib: add time/spring (diff)
downloadalive-fef55b7aa19370390d4a86fe6963f6782a9b9a19.tar.gz
alive-fef55b7aa19370390d4a86fe6963f6782a9b9a19.zip
support fractions as number literals
Diffstat (limited to 'alv')
-rw-r--r--alv/parsing.moon3
-rw-r--r--alv/result/const.moon7
2 files changed, 8 insertions, 2 deletions
diff --git a/alv/parsing.moon b/alv/parsing.moon
index 5f4c911..c44a1ab 100644
--- a/alv/parsing.moon
+++ b/alv/parsing.moon
@@ -27,8 +27,9 @@ strq = "'" * (C ((P "\\'") + (P '\\\\') + (1 - P "'"))^0) * "'" / Constant\parse
str = strd + strq
int = digit^1
+fract = digit^1 * '/' * digit^1
float = (digit^1 * '.' * digit^0) + (digit^0 * '.' * digit^1)
-num = ((P '-')^-1 * (float + int)) / Constant\parse 'num'
+num = ((P '-')^-1 * (float + fract + int)) / Constant\parse 'num'
atom = num + sym + str
diff --git a/alv/result/const.moon b/alv/result/const.moon
index 48c79f1..43f97a0 100644
--- a/alv/result/const.moon
+++ b/alv/result/const.moon
@@ -102,9 +102,14 @@ class Constant extends Result
-- @tparam string sep the seperator char (only for `str`)
@parse: (type, sep) =>
switch type
- when 'num' then (match) -> @ T.num, (tonumber match), match
when 'sym' then (match) -> @ T.sym, match, match
when 'str' then (match) -> @ T.str, (unescape match), sep .. match .. sep
+ when 'num' then (match) ->
+ num, den = match\match '^(-?%d+)/(%d+)$'
+ num = tonumber num or match
+ if den
+ num = num / tonumber den
+ @ T.num, num, match
--- wrap a Lua value.
--