aboutsummaryrefslogtreecommitdiffstats
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
parentlib: add time/spring (diff)
downloadalive-fef55b7aa19370390d4a86fe6963f6782a9b9a19.tar.gz
alive-fef55b7aa19370390d4a86fe6963f6782a9b9a19.zip
support fractions as number literals
-rw-r--r--alv/parsing.moon3
-rw-r--r--alv/result/const.moon7
-rw-r--r--spec/internal/parsing_spec.moon19
3 files changed, 27 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.
--
diff --git a/spec/internal/parsing_spec.moon b/spec/internal/parsing_spec.moon
index 0486276..09d5b85 100644
--- a/spec/internal/parsing_spec.moon
+++ b/spec/internal/parsing_spec.moon
@@ -23,6 +23,9 @@ describe 'atom parsing', ->
num = verify_parse_nope atom, '1234 nope'
assert.is.equal (Constant.num 1234), num
+ num = verify_parse_nope atom, '-1234 nope'
+ assert.is.equal (Constant.num -1234), num
+
it 'parses floats', ->
num = verify_parse_nope atom, '0.123 nope'
assert.is.equal (Constant.num 0.123), num
@@ -33,6 +36,22 @@ describe 'atom parsing', ->
num = verify_parse_nope atom, '0. nope'
assert.is.equal (Constant.num 0), num
+ num = verify_parse_nope atom, '-45.123 nope'
+ assert.is.equal (Constant.num -45.123), num
+
+ num = verify_parse_nope atom, '-.123 nope'
+ assert.is.equal (Constant.num -0.123), num
+
+ num = verify_parse_nope atom, '-0. nope'
+ assert.is.equal (Constant.num 0), num
+
+ it 'parses fractions', ->
+ num = verify_parse_nope atom, '1/2 nope'
+ assert.is.equal (Constant.num 0.5), num
+
+ num = verify_parse_nope atom, '-3/4 nope'
+ assert.is.equal (Constant.num -0.75), num
+
describe 'strings', ->
it 'parses double-quote strings', ->
str = verify_parse_nope atom, '"help some stuff!" nope'