From 7d61396a5bcbefd488c49a7b1ed883372db8983f Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sun, 24 Mar 2024 13:26:34 -0500 Subject: Special-case '.', '...', '+', '-' --- read.lua | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/read.lua b/read.lua index 9561d9f..f23c5cc 100644 --- a/read.lua +++ b/read.lua @@ -95,6 +95,11 @@ local consume_symbol = consume_token local function consume_number (chars) local digits, chars = consume_token(chars) + -- The signs by themselves are symbols, as well as '...' + if digits:match("[-+.]") or digits == "..." then + return digits, chars + end + -- Otherwise try converting the digits to a number local num = tonumber(digits) if num == nil then error("Bad number: " .. num) end return num, chars @@ -121,7 +126,6 @@ read.readtable = { ["'"] = function(chars) return pop(chars), "quote", chars end, ["`"] = function(chars) return pop(chars), "quote", chars end, [","] = function(chars) return pop(chars), "quote", chars end, - ["."] = function(chars) return pop(chars), "dot", chars end, ["\""] = consume_string, [";"] = consume_comment, -- ["#"] = ..., @@ -139,9 +143,19 @@ function read.scan (chars) return token, toktype elseif chars[1]:match("%s") then chars = consume_whitespace(chars) - elseif chars[1]:match("[%d-]") then + elseif chars[1]:match("%d") then token, chars = consume_number(chars) return token, "number" + elseif chars[1]:match("[.+-]") then + -- special casing for ., ..., +, - + token, chars = consume_number(chars) + if token == "." then + return token, "dot" + elseif token == "..." then + return token, "symbol" + else + return token, "number" + end else token, chars = consume_symbol(chars) return token, "symbol" -- cgit 1.4.1-21-gabe81