diff options
author | Case Duckworth | 2024-03-24 13:26:34 -0500 |
---|---|---|
committer | Case Duckworth | 2024-03-24 13:26:34 -0500 |
commit | 7d61396a5bcbefd488c49a7b1ed883372db8983f (patch) | |
tree | 4545847e00825d6b6f91c3dfee4a111e89ae3e69 | |
parent | Read negative numbers (diff) | |
download | lam-7d61396a5bcbefd488c49a7b1ed883372db8983f.tar.gz lam-7d61396a5bcbefd488c49a7b1ed883372db8983f.zip |
Special-case '.', '...', '+', '-'
-rw-r--r-- | read.lua | 18 |
1 files 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 | |||
95 | 95 | ||
96 | local function consume_number (chars) | 96 | local function consume_number (chars) |
97 | local digits, chars = consume_token(chars) | 97 | local digits, chars = consume_token(chars) |
98 | -- The signs by themselves are symbols, as well as '...' | ||
99 | if digits:match("[-+.]") or digits == "..." then | ||
100 | return digits, chars | ||
101 | end | ||
102 | -- Otherwise try converting the digits to a number | ||
98 | local num = tonumber(digits) | 103 | local num = tonumber(digits) |
99 | if num == nil then error("Bad number: " .. num) end | 104 | if num == nil then error("Bad number: " .. num) end |
100 | return num, chars | 105 | return num, chars |
@@ -121,7 +126,6 @@ read.readtable = { | |||
121 | ["'"] = function(chars) return pop(chars), "quote", chars end, | 126 | ["'"] = function(chars) return pop(chars), "quote", chars end, |
122 | ["`"] = function(chars) return pop(chars), "quote", chars end, | 127 | ["`"] = function(chars) return pop(chars), "quote", chars end, |
123 | [","] = function(chars) return pop(chars), "quote", chars end, | 128 | [","] = function(chars) return pop(chars), "quote", chars end, |
124 | ["."] = function(chars) return pop(chars), "dot", chars end, | ||
125 | ["\""] = consume_string, | 129 | ["\""] = consume_string, |
126 | [";"] = consume_comment, | 130 | [";"] = consume_comment, |
127 | -- ["#"] = ..., | 131 | -- ["#"] = ..., |
@@ -139,9 +143,19 @@ function read.scan (chars) | |||
139 | return token, toktype | 143 | return token, toktype |
140 | elseif chars[1]:match("%s") then | 144 | elseif chars[1]:match("%s") then |
141 | chars = consume_whitespace(chars) | 145 | chars = consume_whitespace(chars) |
142 | elseif chars[1]:match("[%d-]") then | 146 | elseif chars[1]:match("%d") then |
143 | token, chars = consume_number(chars) | 147 | token, chars = consume_number(chars) |
144 | return token, "number" | 148 | return token, "number" |
149 | elseif chars[1]:match("[.+-]") then | ||
150 | -- special casing for ., ..., +, - | ||
151 | token, chars = consume_number(chars) | ||
152 | if token == "." then | ||
153 | return token, "dot" | ||
154 | elseif token == "..." then | ||
155 | return token, "symbol" | ||
156 | else | ||
157 | return token, "number" | ||
158 | end | ||
145 | else | 159 | else |
146 | token, chars = consume_symbol(chars) | 160 | token, chars = consume_symbol(chars) |
147 | return token, "symbol" | 161 | return token, "symbol" |