ombre: Use `true` and `false` instead of atoms
608ba45f7e426efa2eb908fbf0fdb6e804222216
1 parent
b4cdfded
ombre/src/lisp/evaluator.rs
+25 -29
| 123 | 123 | } |
|
| 124 | 124 | ||
| 125 | 125 | /// Values that can be used as dictionary keys. |
|
| 126 | 126 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
|
| 127 | 127 | pub enum Key { |
|
| 128 | - | /// Eg. :true |
|
| 128 | + | /// Eg. true |
|
| 129 | 129 | Bool(bool), |
|
| 130 | 130 | /// Eg. :yikes |
|
| 131 | 131 | Atom(Rc<str>), |
|
| 132 | 132 | /// Eg. "fnord" |
|
| 133 | 133 | Str(String), |
| 247 | 247 | /// Expression in the language. |
|
| 248 | 248 | #[derive(Debug, Clone, PartialEq)] |
|
| 249 | 249 | pub enum Expr { |
|
| 250 | 250 | /// Eg. (1 2 3) or [1 2 3] |
|
| 251 | 251 | Tuple(Tuple), |
|
| 252 | - | /// Eg. :true |
|
| 252 | + | /// Eg. true |
|
| 253 | 253 | Bool(bool), |
|
| 254 | 254 | /// Eg. 'fnord |
|
| 255 | 255 | Quoted(Box<Expr>), |
|
| 256 | 256 | /// Eg. @[1 2 3] |
|
| 257 | 257 | Vector(Mut<Vec<Expr>>), |
| 1475 | 1475 | Ok(val.clone()) |
|
| 1476 | 1476 | } else { |
|
| 1477 | 1477 | Err(Error::UndefinedSymbol(s)) |
|
| 1478 | 1478 | } |
|
| 1479 | 1479 | } |
|
| 1480 | - | Expr::Atom(s) => Ok(match s.deref() { |
|
| 1481 | - | "true" => Expr::Bool(true), |
|
| 1482 | - | "false" => Expr::Bool(false), |
|
| 1483 | - | _ => Expr::Atom(s), |
|
| 1484 | - | }), |
|
| 1480 | + | Expr::Atom(s) => Ok(Expr::Atom(s)), |
|
| 1485 | 1481 | Expr::Vector(v) => v |
|
| 1486 | 1482 | .borrow() |
|
| 1487 | 1483 | .clone() |
|
| 1488 | 1484 | .into_iter() |
|
| 1489 | 1485 | .map(|e| eval(e, env)) |
| 2298 | 2294 | expect(&["(pow 4 0)"], Expr::Integer(1)); |
|
| 2299 | 2295 | } |
|
| 2300 | 2296 | ||
| 2301 | 2297 | #[test] |
|
| 2302 | 2298 | fn test_bool() { |
|
| 2303 | - | expect(&[":true"], Expr::Bool(true)); |
|
| 2304 | - | expect(&[":false"], Expr::Bool(false)); |
|
| 2299 | + | expect(&["true"], Expr::Bool(true)); |
|
| 2300 | + | expect(&["false"], Expr::Bool(false)); |
|
| 2305 | 2301 | } |
|
| 2306 | 2302 | ||
| 2307 | 2303 | #[test] |
|
| 2308 | 2304 | fn test_not() { |
|
| 2309 | - | expect(&["(not :true)"], Expr::Bool(false)); |
|
| 2310 | - | expect(&["(not (not :false))"], Expr::Bool(false)); |
|
| 2305 | + | expect(&["(not true)"], Expr::Bool(false)); |
|
| 2306 | + | expect(&["(not (not false))"], Expr::Bool(false)); |
|
| 2311 | 2307 | } |
|
| 2312 | 2308 | ||
| 2313 | 2309 | #[test] |
|
| 2314 | 2310 | fn test_and() { |
|
| 2315 | - | expect(&["(and :true :true :true)"], Expr::Bool(true)); |
|
| 2316 | - | expect(&["(and :true :false :true)"], Expr::Bool(false)); |
|
| 2311 | + | expect(&["(and true true true)"], Expr::Bool(true)); |
|
| 2312 | + | expect(&["(and true false true)"], Expr::Bool(false)); |
|
| 2317 | 2313 | } |
|
| 2318 | 2314 | ||
| 2319 | 2315 | #[test] |
|
| 2320 | 2316 | fn test_or() { |
|
| 2321 | - | expect(&["(or :false :true :false)"], Expr::Bool(true)); |
|
| 2322 | - | expect(&["(or :false :false :false)"], Expr::Bool(false)); |
|
| 2317 | + | expect(&["(or false true false)"], Expr::Bool(true)); |
|
| 2318 | + | expect(&["(or false false false)"], Expr::Bool(false)); |
|
| 2323 | 2319 | } |
|
| 2324 | 2320 | ||
| 2325 | 2321 | #[test] |
|
| 2326 | 2322 | fn test_eval() { |
|
| 2327 | 2323 | expect(&["(eval 5)"], Expr::Integer(5)); |
| 2336 | 2332 | expect(&["(eval (eval ''(+ 3 2)))"], Expr::Integer(5)); |
|
| 2337 | 2333 | expect( |
|
| 2338 | 2334 | &["(eq? (eval '(+ 1 2)) (eval ''(+ 1 2)))"], |
|
| 2339 | 2335 | Expr::Bool(false), |
|
| 2340 | 2336 | ); |
|
| 2341 | - | expect(&["(eval '(eq? :true :false))"], Expr::Bool(false)); |
|
| 2342 | - | expect(&["(eval ':true)"], Expr::Bool(true)); |
|
| 2337 | + | expect(&["(eval '(eq? true false))"], Expr::Bool(false)); |
|
| 2338 | + | expect(&["(eval 'true)"], Expr::Bool(true)); |
|
| 2343 | 2339 | } |
|
| 2344 | 2340 | ||
| 2345 | 2341 | #[test] |
|
| 2346 | 2342 | fn test_color() { |
|
| 2347 | 2343 | expect( |
| 2365 | 2361 | fn test_eq() { |
|
| 2366 | 2362 | expect(&["(eq? 1 2)"], Expr::Bool(false)); |
|
| 2367 | 2363 | expect(&["(eq? 1 1)"], Expr::Bool(true)); |
|
| 2368 | 2364 | expect(&["(eq? 1 1 1)"], Expr::Bool(true)); |
|
| 2369 | 2365 | expect(&["(eq? 1 1 2)"], Expr::Bool(false)); |
|
| 2370 | - | expect(&["(eq? :true :true)"], Expr::Bool(true)); |
|
| 2371 | - | expect(&["(eq? :true :false)"], Expr::Bool(false)); |
|
| 2366 | + | expect(&["(eq? true true)"], Expr::Bool(true)); |
|
| 2367 | + | expect(&["(eq? true false)"], Expr::Bool(false)); |
|
| 2372 | 2368 | expect(&["(eq? {4 2} {4 2})"], Expr::Bool(true)); |
|
| 2373 | 2369 | expect(&["(eq? {4 2} {4 3})"], Expr::Bool(false)); |
|
| 2374 | 2370 | } |
|
| 2375 | 2371 | ||
| 2376 | 2372 | #[test] |
|
| 2377 | 2373 | fn test_if() { |
|
| 2378 | 2374 | expect(&["(if (> 4 3) `l` `r`)"], Expr::Char('l')); |
|
| 2379 | 2375 | expect(&["(if (< 4 3) `l` `r`)"], Expr::Char('r')); |
|
| 2380 | - | expect(&["(if :true (+ 2 3) 0)"], Expr::Integer(5)); |
|
| 2381 | - | expect(&["(if :false (+ 2 3) 0)"], Expr::Integer(0)); |
|
| 2382 | - | expect(&["(if :false 0 (if :true 1 2))"], Expr::Integer(1)); |
|
| 2383 | - | expect(&["(if :true 'symbol {})"], Expr::Symbol("symbol".into())); |
|
| 2376 | + | expect(&["(if true (+ 2 3) 0)"], Expr::Integer(5)); |
|
| 2377 | + | expect(&["(if false (+ 2 3) 0)"], Expr::Integer(0)); |
|
| 2378 | + | expect(&["(if false 0 (if true 1 2))"], Expr::Integer(1)); |
|
| 2379 | + | expect(&["(if true 'symbol {})"], Expr::Symbol("symbol".into())); |
|
| 2384 | 2380 | expect( |
|
| 2385 | - | &["(def symbol 42)", "(if :true symbol {})"], |
|
| 2381 | + | &["(def symbol 42)", "(if true symbol {})"], |
|
| 2386 | 2382 | Expr::Integer(42), |
|
| 2387 | 2383 | ); |
|
| 2388 | 2384 | } |
|
| 2389 | 2385 | ||
| 2390 | 2386 | #[test] |
| 2907 | 2903 | expect(&["(doc 'undefined)"], Expr::nil()); |
|
| 2908 | 2904 | } |
|
| 2909 | 2905 | ||
| 2910 | 2906 | #[test] |
|
| 2911 | 2907 | fn test_assert() { |
|
| 2912 | - | expect(&["(assert :true)"], Expr::nil()); |
|
| 2908 | + | expect(&["(assert true)"], Expr::nil()); |
|
| 2913 | 2909 | expect_err( |
|
| 2914 | - | &["(assert :false)"], |
|
| 2910 | + | &["(assert false)"], |
|
| 2915 | 2911 | Error::User(Expr::Str("assertion failed".to_owned())), |
|
| 2916 | 2912 | ); |
|
| 2917 | 2913 | expect_err( |
|
| 2918 | - | &["(assert :false \"custom message\")"], |
|
| 2914 | + | &["(assert false \"custom message\")"], |
|
| 2919 | 2915 | Error::User(Expr::Str("custom message".to_owned())), |
|
| 2920 | 2916 | ); |
|
| 2921 | - | expect_err(&["(assert :false 42)"], Error::User(Expr::Integer(42))); |
|
| 2917 | + | expect_err(&["(assert false 42)"], Error::User(Expr::Integer(42))); |
|
| 2922 | 2918 | expect_err( |
|
| 2923 | - | &["(assert :false '(error details))"], |
|
| 2919 | + | &["(assert false '(error details))"], |
|
| 2924 | 2920 | Error::User(Expr::tuple([ |
|
| 2925 | 2921 | Expr::Symbol("error".into()), |
|
| 2926 | 2922 | Expr::Symbol("details".into()), |
|
| 2927 | 2923 | ])), |
|
| 2928 | 2924 | ); |
ombre/src/lisp/parser.rs
+6 -1
| 1 | 1 | //! Parser. |
|
| 2 | 2 | use std::cell::RefCell; |
|
| 3 | 3 | use std::collections::HashMap; |
|
| 4 | 4 | use std::fmt; |
|
| 5 | 5 | use std::iter::Peekable; |
|
| 6 | + | use std::ops::Deref; |
|
| 6 | 7 | use std::rc::Rc; |
|
| 7 | 8 | use std::result; |
|
| 8 | 9 | ||
| 9 | 10 | use crate::lisp::evaluator::{Expr, Key, Symbol, Tuple}; |
|
| 10 | 11 | use crate::lisp::scanner; |
| 92 | 93 | Token::RBracket => return Err(Error::UnexpectedToken(token.owned())), |
|
| 93 | 94 | Token::Quote => self.parse_quoted()?, |
|
| 94 | 95 | Token::Ref => self.parse_ref()?, |
|
| 95 | 96 | Token::Symbol(s) => { |
|
| 96 | 97 | self.tokens.next(); |
|
| 97 | - | Expr::Symbol(Symbol::from(Rc::from(s))) |
|
| 98 | + | match s.deref() { |
|
| 99 | + | "true" => Expr::Bool(true), |
|
| 100 | + | "false" => Expr::Bool(false), |
|
| 101 | + | _ => Expr::Symbol(Symbol::from(Rc::from(s))), |
|
| 102 | + | } |
|
| 98 | 103 | } |
|
| 99 | 104 | Token::Atom(s) => { |
|
| 100 | 105 | self.tokens.next(); |
|
| 101 | 106 | Expr::Atom(s.into()) |
|
| 102 | 107 | } |