ombre: Support negative numbers

8bcb7b6edf47cd0a3ca1605e0a73b38c4043188e
Alexis Sellier committed ago 1 parent d8b6f591
ombre/src/lisp/parser.rs +23 -0
244 244
        err("(}");
245 245
        err("(const 1 nil}");
246 246
        err("{const 1 nil)");
247 247
    }
248 248
249 +
    #[test]
250 +
    fn test_parse_integer() {
251 +
        ok("42", &[Expr::Integer(42)]);
252 +
        ok("-42", &[Expr::Integer(-42)]);
253 +
    }
254 +
255 +
    #[test]
256 +
    fn test_parse_minus_expr() {
257 +
        ok("(-)", &[Expr::tuple([Expr::symbol("-")])]);
258 +
        ok(
259 +
            "(- 4)",
260 +
            &[Expr::tuple([Expr::symbol("-"), Expr::Integer(4)])],
261 +
        );
262 +
        ok(
263 +
            "(- 4 3)",
264 +
            &[Expr::tuple([
265 +
                Expr::symbol("-"),
266 +
                Expr::Integer(4),
267 +
                Expr::Integer(3),
268 +
            ])],
269 +
        );
270 +
    }
271 +
249 272
    #[test]
250 273
    fn test_parse_tuple() {
251 274
        ok("()", &[Expr::nil()]);
252 275
        ok("(cons)", &[Expr::tuple([Expr::Symbol("cons".into())])]);
253 276
        ok(
ombre/src/lisp/scanner.rs +50 -6
160 160
        } else {
161 161
            Ok(ident)
162 162
        }
163 163
    }
164 164
165 -
    fn scan_number(&mut self) -> Result<Token<'a>> {
165 +
    fn scan_number(&mut self, negative: bool) -> Result<Token<'a>> {
166 166
        let (start, _) = self.skip_if(|c| c.is_ascii_digit())?;
167 167
        let mut floating = false;
168 168
169 169
        while let Some((_, ch)) = self.peek() {
170 170
            if ch.is_ascii_digit() {
177 177
            }
178 178
        }
179 179
        let string = &self.input[start..self.index()];
180 180
181 181
        if floating {
182 -
            string.parse::<f64>().map(Token::Float).map_err(Error::ParseFloat)
182 +
            string
183 +
                .parse::<f64>()
184 +
                .map(|n| if negative { -n } else { n })
185 +
                .map(Token::Float)
186 +
                .map_err(Error::ParseFloat)
183 187
        } else {
184 -
            string.parse::<i64>().map(Token::Integer).map_err(Error::ParseInteger)
188 +
            string
189 +
                .parse::<i64>()
190 +
                .map(|n| if negative { -n } else { n })
191 +
                .map(Token::Integer)
192 +
                .map_err(Error::ParseInteger)
185 193
        }
186 194
    }
187 195
188 196
    fn scan_char(&mut self) -> Result<Token<'a>> {
189 197
        self.skip_char('`')?;
276 284
277 285
impl<'a> Iterator for Scanner<'a> {
278 286
    type Item = Result<Token<'a>>;
279 287
280 288
    fn next(&mut self) -> Option<Result<Token<'a>>> {
281 -
        while let Some((_, c)) = self.peek() {
289 +
        while let Some((ix, c)) = self.peek() {
282 290
            match c {
283 291
                '(' => {
284 292
                    self.chars.next();
285 293
                    return Some(Ok(Token::LParen));
286 294
                }
326 334
                }
327 335
                '@' => {
328 336
                    self.chars.next();
329 337
                    return Some(Ok(Token::Ref));
330 338
                }
339 +
                '-' => {
340 +
                    self.chars.next();
341 +
                    match self.peek() {
342 +
                        Some((_, c)) if c.is_ascii_digit() => return Some(self.scan_number(true)),
343 +
                        _ => {
344 +
                            let symbol = match self.scan_ident() {
345 +
                                Ok(ident) => &self.input[ix..=ix + ident.len()],
346 +
                                Err(_) => "-",
347 +
                            };
348 +
                            return Some(Ok(Token::Symbol(Cow::Borrowed(symbol))));
349 +
                        }
350 +
                    }
351 +
                }
331 352
                c if c.is_ascii_whitespace() => {
332 353
                    self.chars.next();
333 354
                }
334 355
                c if c.is_ascii_digit() => {
335 -
                    return Some(self.scan_number());
356 +
                    return Some(self.scan_number(false));
336 357
                }
337 358
                _ => {
338 359
                    return Some(self.scan_symbol());
339 360
                }
340 361
            }
378 399
    fn test_symbol() {
379 400
        ok("const", &[Token::Symbol("const".into())]);
380 401
        ok("_c_o_n_s_", &[Token::Symbol("_c_o_n_s_".into())]);
381 402
        ok("_", &[Token::Symbol("_".into())]);
382 403
        ok("one/two/three", &[Token::Symbol("one/two/three".into())]);
404 +
        ok("one-two-three-", &[Token::Symbol("one-two-three-".into())]);
405 +
        ok("-one-two-three", &[Token::Symbol("-one-two-three".into())]);
383 406
        ok("fNord_42!", &[Token::Symbol("fNord_42!".into())]);
384 -
        ok("-42", &[Token::Symbol("-42".into())]);
385 407
        ok("+", &[Token::Symbol("+".into())]);
386 408
        ok("-", &[Token::Symbol("-".into())]);
387 409
        ok("*", &[Token::Symbol("*".into())]);
388 410
    }
389 411
429 451
        ok("1.0", &[Token::Float(1.0)]);
430 452
        ok("0.0", &[Token::Float(0.0)]);
431 453
        ok("0.0001", &[Token::Float(0.0001)]);
432 454
        ok("41.", &[Token::Float(41.0)]);
433 455
        ok("41.0", &[Token::Float(41.0)]);
456 +
        ok("-42", &[Token::Integer(-42)]);
457 +
        ok("-3.12", &[Token::Float(-3.12)]);
458 +
        ok("-0", &[Token::Integer(0)]);
459 +
        ok("-0.0", &[Token::Float(-0.0)]);
460 +
    }
461 +
462 +
    #[test]
463 +
    fn test_minus() {
464 +
        ok(
465 +
            "(-)",
466 +
            &[Token::LParen, Token::Symbol("-".into()), Token::RParen],
467 +
        );
468 +
        ok(
469 +
            "(- 1 2)",
470 +
            &[
471 +
                Token::LParen,
472 +
                Token::Symbol("-".into()),
473 +
                Token::Integer(1),
474 +
                Token::Integer(2),
475 +
                Token::RParen,
476 +
            ],
477 +
        );
434 478
    }
435 479
436 480
    #[test]
437 481
    fn test_comment() {
438 482
        ok("; Some comment", &[]);