ombre: Rename 'list' to 'tuple' in language
668bc332f3992df7b20713f59aa5746b5fe15b75
1 parent
f731fcc3
ombre/src/lisp/evaluator.rs
+61 -55
| 204 | 204 | ||
| 205 | 205 | /// Expression in the language. |
|
| 206 | 206 | #[derive(Debug, Clone, PartialEq)] |
|
| 207 | 207 | pub enum Expr { |
|
| 208 | 208 | /// Eg. (1 2 3) |
|
| 209 | - | List(Vec<Expr>), |
|
| 209 | + | Tuple(Vec<Expr>), |
|
| 210 | 210 | /// Eg. :true |
|
| 211 | 211 | Bool(bool), |
|
| 212 | 212 | /// Eg. 'fnord |
|
| 213 | 213 | Quoted(Box<Expr>), |
|
| 214 | 214 | /// Eg. [1 2 3] |
| 244 | 244 | } |
|
| 245 | 245 | ||
| 246 | 246 | impl Expr { |
|
| 247 | 247 | /// The empty list, or "nil". |
|
| 248 | 248 | pub fn nil() -> Self { |
|
| 249 | - | Self::List(vec![]) |
|
| 249 | + | Self::Tuple(vec![]) |
|
| 250 | 250 | } |
|
| 251 | 251 | ||
| 252 | 252 | /// Check if this expression is the empty list. |
|
| 253 | 253 | pub fn is_nil(&self) -> bool { |
|
| 254 | - | matches!(self, Self::List(l) if l.is_empty()) |
|
| 254 | + | matches!(self, Self::Tuple(l) if l.is_empty()) |
|
| 255 | 255 | } |
|
| 256 | 256 | ||
| 257 | - | /// Construct a list. |
|
| 258 | - | pub fn list(items: impl Into<Vec<Expr>>) -> Self { |
|
| 259 | - | Expr::List(items.into()) |
|
| 257 | + | /// Construct a tuple. |
|
| 258 | + | pub fn tuple(items: impl Into<Vec<Expr>>) -> Self { |
|
| 259 | + | Expr::Tuple(items.into()) |
|
| 260 | 260 | } |
|
| 261 | 261 | ||
| 262 | 262 | /// Construct a vector. |
|
| 263 | 263 | pub fn vector(items: impl IntoIterator<Item = Expr>) -> Self { |
|
| 264 | 264 | Expr::Vector(items.into_iter().collect()) |
| 414 | 414 | impl TryFrom<Expr> for Vec<Expr> { |
|
| 415 | 415 | type Error = Expr; |
|
| 416 | 416 | ||
| 417 | 417 | fn try_from(expr: Expr) -> Result<Self, Self::Error> { |
|
| 418 | 418 | match expr { |
|
| 419 | - | Expr::List(v) => Ok(v), |
|
| 419 | + | Expr::Tuple(v) => Ok(v), |
|
| 420 | 420 | Expr::Vector(v) => Ok(v), |
|
| 421 | 421 | _ => Err(expr), |
|
| 422 | 422 | } |
|
| 423 | 423 | } |
|
| 424 | 424 | } |
| 567 | 567 | } |
|
| 568 | 568 | ||
| 569 | 569 | impl fmt::Display for Expr { |
|
| 570 | 570 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
| 571 | 571 | match self { |
|
| 572 | - | Expr::List(list) => { |
|
| 572 | + | Expr::Tuple(t) => { |
|
| 573 | 573 | write!(f, "(")?; |
|
| 574 | - | let mut iter = list.iter(); |
|
| 574 | + | let mut iter = t.iter(); |
|
| 575 | 575 | if let Some(first) = iter.next() { |
|
| 576 | 576 | write!(f, "{first}")?; |
|
| 577 | 577 | for item in iter { |
|
| 578 | 578 | write!(f, " {item}")?; |
|
| 579 | 579 | } |
| 1009 | 1009 | intrinsic::min, |
|
| 1010 | 1010 | ); |
|
| 1011 | 1011 | env.intrinsic("not", "Logical *not* of a boolean value", intrinsic::not); |
|
| 1012 | 1012 | env.intrinsic( |
|
| 1013 | 1013 | "first", |
|
| 1014 | - | "Get the first element of a list or vector", |
|
| 1014 | + | "Get the first element of a tuple or vector", |
|
| 1015 | 1015 | intrinsic::first, |
|
| 1016 | 1016 | ); |
|
| 1017 | 1017 | env.intrinsic( |
|
| 1018 | 1018 | "last", |
|
| 1019 | - | "Get the last element of a list or vector", |
|
| 1019 | + | "Get the last element of a tuple or vector", |
|
| 1020 | 1020 | intrinsic::last, |
|
| 1021 | 1021 | ); |
|
| 1022 | 1022 | env.intrinsic( |
|
| 1023 | 1023 | "nth", |
|
| 1024 | - | "Get the *nth* element of a list or vector", |
|
| 1024 | + | "Get the *nth* element of a tuple or vector", |
|
| 1025 | 1025 | intrinsic::nth, |
|
| 1026 | 1026 | ); |
|
| 1027 | 1027 | env.intrinsic( |
|
| 1028 | 1028 | "dict", |
|
| 1029 | 1029 | "Create a new dictionary from the given list", |
| 1034 | 1034 | "Lookup a value in a dictionary, under the given key", |
|
| 1035 | 1035 | intrinsic::lookup, |
|
| 1036 | 1036 | ); |
|
| 1037 | 1037 | env.intrinsic( |
|
| 1038 | 1038 | "map", |
|
| 1039 | - | "Apply a function to each element of a list or vector", |
|
| 1039 | + | "Apply a function to each element of a tuple or vector", |
|
| 1040 | 1040 | intrinsic::map, |
|
| 1041 | 1041 | ); |
|
| 1042 | 1042 | env.intrinsic("error", "Raise a user-defined error", intrinsic::error); |
|
| 1043 | 1043 | env.intrinsic( |
|
| 1044 | 1044 | "length", |
|
| 1045 | - | "Get the length of a list, vector, dictionary or string", |
|
| 1045 | + | "Get the length of a tuple, vector, dictionary or string", |
|
| 1046 | 1046 | intrinsic::length, |
|
| 1047 | 1047 | ); |
|
| 1048 | 1048 | env.intrinsic( |
|
| 1049 | 1049 | "contains?", |
|
| 1050 | 1050 | "Check if a collection contains the given key", |
|
| 1051 | 1051 | intrinsic::contains, |
|
| 1052 | 1052 | ); |
|
| 1053 | 1053 | env.intrinsic("empty?", "Check if a collection is empty", intrinsic::empty); |
|
| 1054 | 1054 | env.intrinsic("append", "Append elements to a vector", intrinsic::append); |
|
| 1055 | - | env.intrinsic("reverse", "Reverse a list or vector", intrinsic::reverse); |
|
| 1055 | + | env.intrinsic("reverse", "Reverse a tuple or vector", intrinsic::reverse); |
|
| 1056 | 1056 | env.intrinsic( |
|
| 1057 | 1057 | "range", |
|
| 1058 | 1058 | "Create a vector of numbers in a specified range", |
|
| 1059 | 1059 | intrinsic::range, |
|
| 1060 | 1060 | ); |
| 1063 | 1063 | env.intrinsic( |
|
| 1064 | 1064 | "set!", |
|
| 1065 | 1065 | "Set the value of a mutable reference", |
|
| 1066 | 1066 | intrinsic::set_ref, |
|
| 1067 | 1067 | ); |
|
| 1068 | + | env.intrinsic("tuple", "Create a tuple from arguments", intrinsic::tuple); |
|
| 1068 | 1069 | env.intrinsic( |
|
| 1069 | 1070 | "modify!", |
|
| 1070 | 1071 | "Modify a mutable reference in-place using a lambda", |
|
| 1071 | 1072 | intrinsic::modify, |
|
| 1072 | 1073 | ); |
| 1130 | 1131 | } |
|
| 1131 | 1132 | ||
| 1132 | 1133 | /// Evaluate a top-level expression. |
|
| 1133 | 1134 | /// These expressions can only exist at the root, or at the top level of a module. |
|
| 1134 | 1135 | pub fn eval_module(expr: Expr, env: &mut Env<'_>, mods: &mut ModuleRegistry) -> Result<Expr> { |
|
| 1135 | - | let Expr::List(list) = expr else { |
|
| 1136 | + | let Expr::Tuple(tuple) = expr else { |
|
| 1136 | 1137 | // If the expression is not a list, then it's not a special form and can be evaluated |
|
| 1137 | 1138 | // with the regular eval function. |
|
| 1138 | 1139 | return eval(expr, env); |
|
| 1139 | 1140 | }; |
|
| 1140 | - | let Some(Expr::Symbol(head)) = list.first() else { |
|
| 1141 | - | // If the list doesn't start with a symbol, we just evaluate it as a regular list. |
|
| 1142 | - | return eval_list(list, env); |
|
| 1141 | + | let Some(Expr::Symbol(head)) = tuple.first() else { |
|
| 1142 | + | // If the tuple doesn't start with a symbol, we just evaluate it as a regular tuple. |
|
| 1143 | + | return eval_tuple(tuple, env); |
|
| 1143 | 1144 | }; |
|
| 1144 | 1145 | ||
| 1145 | 1146 | // Handle special top-level forms. |
|
| 1146 | 1147 | match head.deref() { |
|
| 1147 | 1148 | "def" => { |
|
| 1148 | - | let mut iter = list.into_iter().skip(1); |
|
| 1149 | + | let mut iter = tuple.into_iter().skip(1); |
|
| 1149 | 1150 | let sym = iter.next().ok_or(Error::ExpectedValue("symbol"))?; |
|
| 1150 | 1151 | let Expr::Symbol(name) = sym else { |
|
| 1151 | 1152 | return Err(Error::UnexpectedValue(sym)); |
|
| 1152 | 1153 | }; |
|
| 1153 | 1154 | let (docstr, value) = match (iter.next(), iter.next()) { |
| 1175 | 1176 | } |
|
| 1176 | 1177 | } |
|
| 1177 | 1178 | Ok(Expr::nil()) |
|
| 1178 | 1179 | } |
|
| 1179 | 1180 | "module" => { |
|
| 1180 | - | let mut items = list.into_iter().skip(1).peekable(); |
|
| 1181 | + | let mut items = tuple.into_iter().skip(1).peekable(); |
|
| 1181 | 1182 | let Some(Expr::Symbol(name)) = items.next() else { |
|
| 1182 | 1183 | return Err(Error::ExpectedValue("symbol")); |
|
| 1183 | 1184 | }; |
|
| 1184 | 1185 | // Get optional doc string. |
|
| 1185 | 1186 | let docstr = if let Some(Expr::Str(ref docstr)) = items.peek().cloned() { |
| 1197 | 1198 | mods.register(name.clone(), Module { name, docstr, env }); |
|
| 1198 | 1199 | ||
| 1199 | 1200 | Ok(Expr::nil()) |
|
| 1200 | 1201 | } |
|
| 1201 | 1202 | "import" => { |
|
| 1202 | - | let mut iter = list.into_iter().skip(1); |
|
| 1203 | + | let mut iter = tuple.into_iter().skip(1); |
|
| 1203 | 1204 | let Some(Expr::Symbol(name)) = iter.next() else { |
|
| 1204 | 1205 | return Err(Error::ExpectedValue("symbol")); |
|
| 1205 | 1206 | }; |
|
| 1206 | 1207 | let module = match mods.lookup(&name) { |
|
| 1207 | 1208 | Some(m) => m, |
| 1210 | 1211 | env.import(module); |
|
| 1211 | 1212 | ||
| 1212 | 1213 | Ok(Expr::nil()) |
|
| 1213 | 1214 | } |
|
| 1214 | 1215 | "export" => { |
|
| 1215 | - | let mut iter = list.into_iter().skip(1).peekable(); |
|
| 1216 | + | let mut iter = tuple.into_iter().skip(1).peekable(); |
|
| 1216 | 1217 | if iter.peek().is_none() { |
|
| 1217 | 1218 | return Err(Error::WrongArgumentCount { |
|
| 1218 | 1219 | expected: 1, |
|
| 1219 | 1220 | actual: 0, |
|
| 1220 | 1221 | }); |
| 1225 | 1226 | }; |
|
| 1226 | 1227 | env.export(s); |
|
| 1227 | 1228 | } |
|
| 1228 | 1229 | Ok(Expr::nil()) |
|
| 1229 | 1230 | } |
|
| 1230 | - | _ => eval_list(list, env), |
|
| 1231 | + | _ => eval_tuple(tuple, env), |
|
| 1231 | 1232 | } |
|
| 1232 | 1233 | } |
|
| 1233 | 1234 | ||
| 1234 | 1235 | /// Evaluate a "let" expression. |
|
| 1235 | 1236 | fn eval_let(assignments: Vec<Expr>, body: Vec<Expr>, env: &mut Env<'_>) -> Result<Expr> { |
| 1288 | 1289 | .pop() |
|
| 1289 | 1290 | .unwrap_or(Expr::nil())) |
|
| 1290 | 1291 | } |
|
| 1291 | 1292 | ||
| 1292 | 1293 | /// Evaluate special forms and function application. |
|
| 1293 | - | fn eval_list(sexpr: Vec<Expr>, env: &mut Env<'_>) -> Result<Expr> { |
|
| 1294 | - | let mut items = sexpr.into_iter(); |
|
| 1294 | + | fn eval_tuple(tuple: Vec<Expr>, env: &mut Env<'_>) -> Result<Expr> { |
|
| 1295 | + | let mut items = tuple.into_iter(); |
|
| 1295 | 1296 | let Some(head) = items.next() else { |
|
| 1296 | 1297 | return Ok(Expr::nil()); |
|
| 1297 | 1298 | }; |
|
| 1298 | 1299 | if let Expr::Symbol(ref name) = head { |
|
| 1299 | 1300 | match name.deref() { |
| 1369 | 1370 | Expr::Integer(n) => Ok(Expr::Integer(n)), |
|
| 1370 | 1371 | Expr::Function(f) => Ok(Expr::Function(f)), |
|
| 1371 | 1372 | Expr::Color(c) => Ok(Expr::Color(c)), |
|
| 1372 | 1373 | Expr::Str(s) => Ok(Expr::Str(s)), |
|
| 1373 | 1374 | Expr::Bool(b) => Ok(Expr::Bool(b)), |
|
| 1374 | - | Expr::List(l) => eval_list(l, env), |
|
| 1375 | + | Expr::Tuple(l) => eval_tuple(l, env), |
|
| 1375 | 1376 | Expr::Quoted(e) => Ok(*e), |
|
| 1376 | 1377 | Expr::Symbol(s) => { |
|
| 1377 | 1378 | if let Some(val) = env.lookup(&s) { |
|
| 1378 | 1379 | Ok(val.clone()) |
|
| 1379 | 1380 | } else { |
| 1387 | 1388 | }), |
|
| 1388 | 1389 | Expr::Vector(v) => v |
|
| 1389 | 1390 | .into_iter() |
|
| 1390 | 1391 | .map(|e| eval(e, env)) |
|
| 1391 | 1392 | .collect::<Result<Vec<_>>>() |
|
| 1392 | - | .map(Expr::List), |
|
| 1393 | + | .map(Expr::Tuple), |
|
| 1393 | 1394 | Expr::Vector2D(v) => Ok(Expr::Vector2D(v)), |
|
| 1394 | 1395 | Expr::Vector3D(v) => Ok(Expr::Vector3D(v)), |
|
| 1395 | 1396 | Expr::Vector4D(v) => Ok(Expr::Vector4D(v)), |
|
| 1396 | 1397 | Expr::Dict(d) => d |
|
| 1397 | 1398 | .into_iter() |
| 1527 | 1528 | /// Return the length of a value. |
|
| 1528 | 1529 | pub fn length(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1529 | 1530 | let (value,) = <(Expr,)>::try_from_args(args)?; |
|
| 1530 | 1531 | let length = match value { |
|
| 1531 | 1532 | Expr::Vector(l) => l.len(), |
|
| 1532 | - | Expr::List(l) => l.len(), |
|
| 1533 | + | Expr::Tuple(l) => l.len(), |
|
| 1533 | 1534 | Expr::Dict(d) => d.len(), |
|
| 1534 | 1535 | Expr::Str(s) => s.chars().count(), |
|
| 1535 | 1536 | _ => return Err(Error::UnexpectedValue(value)), |
|
| 1536 | 1537 | }; |
|
| 1537 | 1538 | Ok(Expr::Integer(length.try_into().expect( |
| 1542 | 1543 | /// Check if the collection is empty. |
|
| 1543 | 1544 | pub fn empty(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1544 | 1545 | let (expr,) = <(Expr,)>::try_from_args(args)?; |
|
| 1545 | 1546 | let is_empty = match expr { |
|
| 1546 | 1547 | Expr::Vector(l) => l.is_empty(), |
|
| 1547 | - | Expr::List(l) => l.is_empty(), |
|
| 1548 | + | Expr::Tuple(l) => l.is_empty(), |
|
| 1548 | 1549 | Expr::Str(s) => s.is_empty(), |
|
| 1549 | 1550 | Expr::Dict(d) => d.is_empty(), |
|
| 1550 | 1551 | _ => return Err(Error::UnexpectedValue(expr)), |
|
| 1551 | 1552 | }; |
|
| 1552 | 1553 | Ok(Expr::Bool(is_empty)) |
| 1612 | 1613 | let Some(key) = args.next() else { |
|
| 1613 | 1614 | return Err(Error::ExpectedValue("key or value")); |
|
| 1614 | 1615 | }; |
|
| 1615 | 1616 | let is_contained = match args.next() { |
|
| 1616 | 1617 | Some(Expr::Vector(l)) => l.contains(&key), |
|
| 1617 | - | Some(Expr::List(l)) => l.contains(&key), |
|
| 1618 | + | Some(Expr::Tuple(l)) => l.contains(&key), |
|
| 1618 | 1619 | Some(Expr::Str(s)) => match key { |
|
| 1619 | 1620 | Expr::Str(k) => s.contains(&k), |
|
| 1620 | 1621 | Expr::Char(c) => s.contains(c), |
|
| 1621 | 1622 | _ => return Err(Error::ExpectedValue("string or character")), |
|
| 1622 | 1623 | }, |
| 1638 | 1639 | return Ok(Expr::nil()); |
|
| 1639 | 1640 | } |
|
| 1640 | 1641 | let mut args = args.into_iter(); |
|
| 1641 | 1642 | ||
| 1642 | 1643 | match args.next() { |
|
| 1643 | - | Some(Expr::List(mut l)) => { |
|
| 1644 | + | Some(Expr::Tuple(mut l)) => { |
|
| 1644 | 1645 | l.extend(args); |
|
| 1645 | 1646 | Ok(Expr::Vector(l)) |
|
| 1646 | 1647 | } |
|
| 1647 | 1648 | Some(Expr::Vector(mut v)) => { |
|
| 1648 | 1649 | v.extend(args); |
| 1919 | 1920 | pub fn rgba(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1920 | 1921 | let (r, g, b, a) = <(f32, f32, f32, f32)>::try_from_args(args)?; |
|
| 1921 | 1922 | Ok(Expr::Color(Rgba::new(r, g, b, a))) |
|
| 1922 | 1923 | } |
|
| 1923 | 1924 | ||
| 1925 | + | /// Create a tuple from arguments. |
|
| 1926 | + | pub fn tuple(args: Vec<Expr>, _env: &Env<'_>) -> Result<Expr> { |
|
| 1927 | + | Ok(Expr::Tuple(args)) |
|
| 1928 | + | } |
|
| 1929 | + | ||
| 1924 | 1930 | fn fold<T: TryFrom<Expr, Error = Expr> + Into<Expr>>( |
|
| 1925 | 1931 | args: impl IntoIterator<Item = Expr>, |
|
| 1926 | 1932 | init: T, |
|
| 1927 | 1933 | op: impl Fn(T, T) -> T, |
|
| 1928 | 1934 | ) -> Result<Expr> { |
| 1986 | 1992 | &["(def fnord 'answer)", "fnord"], |
|
| 1987 | 1993 | Expr::Symbol("answer".into()), |
|
| 1988 | 1994 | ); |
|
| 1989 | 1995 | expect( |
|
| 1990 | 1996 | &["(def fnord '(* 2 21))", "fnord"], |
|
| 1991 | - | Expr::list([Expr::symbol("*"), Expr::Integer(2), Expr::Integer(21)]), |
|
| 1997 | + | Expr::tuple([Expr::symbol("*"), Expr::Integer(2), Expr::Integer(21)]), |
|
| 1992 | 1998 | ); |
|
| 1993 | 1999 | expect(&["(def fnord (* 2 21))", "fnord"], Expr::Integer(42)); |
|
| 1994 | 2000 | expect( |
|
| 1995 | 2001 | &["(def fnord '(* 2 21))", "(eval fnord)"], |
|
| 1996 | 2002 | Expr::Integer(42), |
| 2020 | 2026 | "(def f (fn [] fnord))", |
|
| 2021 | 2027 | "(def fnord 0)", |
|
| 2022 | 2028 | "(def g (fn [] fnord))", |
|
| 2023 | 2029 | "[(f) (g)]", |
|
| 2024 | 2030 | ], |
|
| 2025 | - | Expr::list([Expr::Integer(42), Expr::Integer(0)]), |
|
| 2031 | + | Expr::tuple([Expr::Integer(42), Expr::Integer(0)]), |
|
| 2026 | 2032 | ); |
|
| 2027 | 2033 | } |
|
| 2028 | 2034 | ||
| 2029 | 2035 | #[test] |
|
| 2030 | 2036 | fn test_scoping() { |
| 2142 | 2148 | expect(&["(eval '(+ 3 2))"], Expr::Integer(5)); |
|
| 2143 | 2149 | expect(&["(eval [+ 3 2])"], Expr::Integer(5)); |
|
| 2144 | 2150 | expect(&["(eval ''symbol)"], Expr::Symbol("symbol".into())); |
|
| 2145 | 2151 | expect( |
|
| 2146 | 2152 | &["(eval ''(+ 3 2))"], |
|
| 2147 | - | Expr::list([Expr::Symbol("+".into()), Expr::Integer(3), Expr::Integer(2)]), |
|
| 2153 | + | Expr::tuple([Expr::Symbol("+".into()), Expr::Integer(3), Expr::Integer(2)]), |
|
| 2148 | 2154 | ); |
|
| 2149 | 2155 | expect(&["(eval (eval ''(+ 3 2)))"], Expr::Integer(5)); |
|
| 2150 | 2156 | expect( |
|
| 2151 | 2157 | &["(eq? (eval '(+ 1 2)) (eval ''(+ 1 2)))"], |
|
| 2152 | 2158 | Expr::Bool(false), |
| 2165 | 2171 | &["(rgba 1.0 0.0 0.3 1.0)"], |
|
| 2166 | 2172 | Expr::Color(Rgba::new(1.0, 0.0, 0.3, 1.0)), |
|
| 2167 | 2173 | ); |
|
| 2168 | 2174 | expect( |
|
| 2169 | 2175 | &["'(#111111 #ff00ff)"], |
|
| 2170 | - | Expr::list([ |
|
| 2176 | + | Expr::tuple([ |
|
| 2171 | 2177 | Expr::Color(Rgba8::new(0x11, 0x11, 0x11, 0xff).into()), |
|
| 2172 | 2178 | Expr::Color(Rgba8::new(0xff, 0x00, 0xff, 0xff).into()), |
|
| 2173 | 2179 | ]), |
|
| 2174 | 2180 | ); |
|
| 2175 | 2181 | } |
| 2218 | 2224 | expect( |
|
| 2219 | 2225 | &[ |
|
| 2220 | 2226 | "(def greet (fn [x y :name \"greyface\" :age 103] [x y name age]))", |
|
| 2221 | 2227 | "(greet 1 2)", |
|
| 2222 | 2228 | ], |
|
| 2223 | - | Expr::list([ |
|
| 2229 | + | Expr::tuple([ |
|
| 2224 | 2230 | Expr::Integer(1), |
|
| 2225 | 2231 | Expr::Integer(2), |
|
| 2226 | 2232 | Expr::Str("greyface".to_owned()), |
|
| 2227 | 2233 | Expr::Integer(103), |
|
| 2228 | 2234 | ]), |
| 2230 | 2236 | expect( |
|
| 2231 | 2237 | &[ |
|
| 2232 | 2238 | "(def greet (fn [x y :name \"greyface\" :age 103] [x y name age]))", |
|
| 2233 | 2239 | "(greet 1 2 :name \"alice\" :age 31)", |
|
| 2234 | 2240 | ], |
|
| 2235 | - | Expr::list([ |
|
| 2241 | + | Expr::tuple([ |
|
| 2236 | 2242 | Expr::Integer(1), |
|
| 2237 | 2243 | Expr::Integer(2), |
|
| 2238 | 2244 | Expr::Str("alice".to_owned()), |
|
| 2239 | 2245 | Expr::Integer(31), |
|
| 2240 | 2246 | ]), |
| 2242 | 2248 | expect( |
|
| 2243 | 2249 | &[ |
|
| 2244 | 2250 | "(def greet (fn [x y :name \"greyface\" :age 103] [x y name age]))", |
|
| 2245 | 2251 | "(greet 1 2 :age 33)", |
|
| 2246 | 2252 | ], |
|
| 2247 | - | Expr::list([ |
|
| 2253 | + | Expr::tuple([ |
|
| 2248 | 2254 | Expr::Integer(1), |
|
| 2249 | 2255 | Expr::Integer(2), |
|
| 2250 | 2256 | Expr::Str("greyface".to_owned()), |
|
| 2251 | 2257 | Expr::Integer(33), |
|
| 2252 | 2258 | ]), |
| 2254 | 2260 | expect( |
|
| 2255 | 2261 | &[ |
|
| 2256 | 2262 | "(def greet (fn [:name \"greyface\" :eyes :brown] [name eyes]))", |
|
| 2257 | 2263 | "(greet :eyes :blue)", |
|
| 2258 | 2264 | ], |
|
| 2259 | - | Expr::list([Expr::Str("greyface".to_owned()), Expr::Atom("blue".into())]), |
|
| 2265 | + | Expr::tuple([Expr::Str("greyface".to_owned()), Expr::Atom("blue".into())]), |
|
| 2260 | 2266 | ); |
|
| 2261 | 2267 | expect( |
|
| 2262 | 2268 | &[ |
|
| 2263 | 2269 | "(def greet (fn [:name \"greyface\" :eyes :brown] [name eyes]))", |
|
| 2264 | 2270 | "(greet)", |
|
| 2265 | 2271 | ], |
|
| 2266 | - | Expr::list([Expr::Str("greyface".to_owned()), Expr::Atom("brown".into())]), |
|
| 2272 | + | Expr::tuple([Expr::Str("greyface".to_owned()), Expr::Atom("brown".into())]), |
|
| 2267 | 2273 | ); |
|
| 2268 | 2274 | } |
|
| 2269 | 2275 | ||
| 2270 | 2276 | #[test] |
|
| 2271 | 2277 | fn test_first() { |
|
| 2272 | 2278 | expect(&["(first '(4 3 1))"], Expr::Integer(4)); |
|
| 2273 | 2279 | expect(&["(first [4 3 1])"], Expr::Integer(4)); |
|
| 2274 | 2280 | expect( |
|
| 2275 | 2281 | &["(first [[4 2] 3 1])"], |
|
| 2276 | - | Expr::list([Expr::Integer(4), Expr::Integer(2)]), |
|
| 2282 | + | Expr::tuple([Expr::Integer(4), Expr::Integer(2)]), |
|
| 2277 | 2283 | ); |
|
| 2278 | 2284 | expect(&["(first ())"], Expr::nil()); |
|
| 2279 | 2285 | expect(&["(first [])"], Expr::nil()); |
|
| 2280 | 2286 | } |
|
| 2281 | 2287 |
| 2375 | 2381 | #[test] |
|
| 2376 | 2382 | fn test_vector() { |
|
| 2377 | 2383 | expect(&["[]"], Expr::nil()); |
|
| 2378 | 2384 | expect( |
|
| 2379 | 2385 | &["[1 2 3]"], |
|
| 2380 | - | Expr::list([Expr::Integer(1), Expr::Integer(2), Expr::Integer(3)]), |
|
| 2386 | + | Expr::tuple([Expr::Integer(1), Expr::Integer(2), Expr::Integer(3)]), |
|
| 2381 | 2387 | ); |
|
| 2382 | - | expect(&["[(+ 1 2)]"], Expr::list([Expr::Integer(3)])); |
|
| 2388 | + | expect(&["[(+ 1 2)]"], Expr::tuple([Expr::Integer(3)])); |
|
| 2383 | 2389 | expect_err(&["[(4 2)]"], Error::UnexpectedValue(Expr::Integer(4))); |
|
| 2384 | 2390 | expect(&["(eq? () [])"], Expr::Bool(true)); |
|
| 2385 | 2391 | } |
|
| 2386 | 2392 | ||
| 2387 | 2393 | #[test] |
|
| 2388 | - | fn test_list() { |
|
| 2394 | + | fn test_tuple() { |
|
| 2389 | 2395 | expect( |
|
| 2390 | 2396 | &["'(+ 1)"], |
|
| 2391 | - | Expr::list([Expr::Symbol("+".into()), Expr::Integer(1)]), |
|
| 2397 | + | Expr::tuple([Expr::Symbol("+".into()), Expr::Integer(1)]), |
|
| 2392 | 2398 | ); |
|
| 2393 | - | expect(&["'(symbol)"], Expr::list([Expr::Symbol("symbol".into())])); |
|
| 2399 | + | expect(&["'(symbol)"], Expr::tuple([Expr::Symbol("symbol".into())])); |
|
| 2394 | 2400 | expect( |
|
| 2395 | 2401 | &["'((symbol))"], |
|
| 2396 | - | Expr::list([Expr::list([Expr::Symbol("symbol".into())])]), |
|
| 2402 | + | Expr::tuple([Expr::tuple([Expr::Symbol("symbol".into())])]), |
|
| 2397 | 2403 | ); |
|
| 2398 | 2404 | expect( |
|
| 2399 | 2405 | &["'('(symbol))"], |
|
| 2400 | - | Expr::list([Expr::Quoted(Box::new(Expr::list([Expr::Symbol( |
|
| 2406 | + | Expr::tuple([Expr::Quoted(Box::new(Expr::tuple([Expr::Symbol( |
|
| 2401 | 2407 | "symbol".into(), |
|
| 2402 | 2408 | )])))]), |
|
| 2403 | 2409 | ); |
|
| 2404 | 2410 | expect( |
|
| 2405 | 2411 | &["'(+ 1 2)"], |
|
| 2406 | - | Expr::list([Expr::Symbol("+".into()), Expr::Integer(1), Expr::Integer(2)]), |
|
| 2412 | + | Expr::tuple([Expr::Symbol("+".into()), Expr::Integer(1), Expr::Integer(2)]), |
|
| 2407 | 2413 | ); |
|
| 2408 | 2414 | expect( |
|
| 2409 | 2415 | &["'(1 (2 3))"], |
|
| 2410 | - | Expr::list([ |
|
| 2416 | + | Expr::tuple([ |
|
| 2411 | 2417 | Expr::Integer(1), |
|
| 2412 | - | Expr::list([Expr::Integer(2), Expr::Integer(3)]), |
|
| 2418 | + | Expr::tuple([Expr::Integer(2), Expr::Integer(3)]), |
|
| 2413 | 2419 | ]), |
|
| 2414 | 2420 | ); |
|
| 2415 | 2421 | expect(&["(eq? '() ())"], Expr::Bool(true)); |
|
| 2416 | 2422 | } |
|
| 2417 | 2423 |
| 2501 | 2507 | } |
|
| 2502 | 2508 | ||
| 2503 | 2509 | #[test] |
|
| 2504 | 2510 | fn test_quote() { |
|
| 2505 | 2511 | expect(&["'symbol"], Expr::Symbol("symbol".into())); |
|
| 2506 | - | expect(&["'(symbol)"], Expr::list([Expr::Symbol("symbol".into())])); |
|
| 2512 | + | expect(&["'(symbol)"], Expr::tuple([Expr::Symbol("symbol".into())])); |
|
| 2507 | 2513 | expect( |
|
| 2508 | 2514 | &["''(symbol)"], |
|
| 2509 | - | Expr::Quoted(Box::new(Expr::list([Expr::Symbol("symbol".into())]))), |
|
| 2515 | + | Expr::Quoted(Box::new(Expr::tuple([Expr::Symbol("symbol".into())]))), |
|
| 2510 | 2516 | ); |
|
| 2511 | 2517 | } |
|
| 2512 | 2518 | ||
| 2513 | 2519 | #[test] |
|
| 2514 | 2520 | fn test_error() { |
| 2592 | 2598 | "(def c2 (make-counter))", |
|
| 2593 | 2599 | "(c2)", |
|
| 2594 | 2600 | "(c1)", |
|
| 2595 | 2601 | "[(c1) (c2)]", |
|
| 2596 | 2602 | ], |
|
| 2597 | - | Expr::list([Expr::Integer(3), Expr::Integer(1)]), |
|
| 2603 | + | Expr::tuple([Expr::Integer(3), Expr::Integer(1)]), |
|
| 2598 | 2604 | ); |
|
| 2599 | 2605 | // Test modify! function. |
|
| 2600 | 2606 | expect( |
|
| 2601 | 2607 | &[ |
|
| 2602 | 2608 | "(def r (ref 5))", |
ombre/src/lisp/parser.rs
+10 -10
| 119 | 119 | fn parse_sexpr(&mut self) -> Result<Expr> { |
|
| 120 | 120 | self.next_if(|t| t == &Token::LParen)?; |
|
| 121 | 121 | let exprs = self.parse_until(|t| t == &Token::RParen)?; |
|
| 122 | 122 | self.next_if(|t| t == &Token::RParen)?; |
|
| 123 | 123 | ||
| 124 | - | Ok(Expr::list(exprs)) |
|
| 124 | + | Ok(Expr::tuple(exprs)) |
|
| 125 | 125 | } |
|
| 126 | 126 | ||
| 127 | 127 | fn parse_dict(&mut self) -> Result<Expr> { |
|
| 128 | 128 | self.next_if(|t| t == &Token::LBrace)?; |
|
| 129 | 129 | let mut exprs = self.parse_until(|t| t == &Token::RBrace)?.into_iter(); |
| 228 | 228 | } |
|
| 229 | 229 | ||
| 230 | 230 | #[test] |
|
| 231 | 231 | fn test_parse_sexpr() { |
|
| 232 | 232 | ok("()", &[Expr::nil()]); |
|
| 233 | - | ok("(cons)", &[Expr::list([Expr::Symbol("cons".into())])]); |
|
| 233 | + | ok("(cons)", &[Expr::tuple([Expr::Symbol("cons".into())])]); |
|
| 234 | 234 | ok( |
|
| 235 | 235 | "(+ 4 2)", |
|
| 236 | - | &[Expr::list([ |
|
| 236 | + | &[Expr::tuple([ |
|
| 237 | 237 | Expr::Symbol("+".into()), |
|
| 238 | 238 | Expr::Integer(4), |
|
| 239 | 239 | Expr::Integer(2), |
|
| 240 | 240 | ])], |
|
| 241 | 241 | ); |
| 243 | 243 | ||
| 244 | 244 | #[test] |
|
| 245 | 245 | fn test_parse_sexpr_nested() { |
|
| 246 | 246 | ok( |
|
| 247 | 247 | "(cons 1 (cons 2 (cons 3 nil)))", |
|
| 248 | - | &[Expr::list([ |
|
| 248 | + | &[Expr::tuple([ |
|
| 249 | 249 | Expr::Symbol("cons".into()), |
|
| 250 | 250 | Expr::Integer(1), |
|
| 251 | - | Expr::list([ |
|
| 251 | + | Expr::tuple([ |
|
| 252 | 252 | Expr::Symbol("cons".into()), |
|
| 253 | 253 | Expr::Integer(2), |
|
| 254 | - | Expr::list([ |
|
| 254 | + | Expr::tuple([ |
|
| 255 | 255 | Expr::Symbol("cons".into()), |
|
| 256 | 256 | Expr::Integer(3), |
|
| 257 | 257 | Expr::Symbol("nil".into()), |
|
| 258 | 258 | ]), |
|
| 259 | 259 | ]), |
| 299 | 299 | ||
| 300 | 300 | #[test] |
|
| 301 | 301 | fn test_parse_quoted() { |
|
| 302 | 302 | ok( |
|
| 303 | 303 | "'(sym)", |
|
| 304 | - | &[Expr::Quoted(Box::new(Expr::list([Expr::Symbol( |
|
| 304 | + | &[Expr::Quoted(Box::new(Expr::tuple([Expr::Symbol( |
|
| 305 | 305 | "sym".into(), |
|
| 306 | 306 | )])))], |
|
| 307 | 307 | ); |
|
| 308 | 308 | ok( |
|
| 309 | 309 | "''(sym)", |
|
| 310 | - | &[Expr::Quoted(Box::new(Expr::Quoted(Box::new(Expr::list( |
|
| 310 | + | &[Expr::Quoted(Box::new(Expr::Quoted(Box::new(Expr::tuple( |
|
| 311 | 311 | [Expr::Symbol("sym".into())], |
|
| 312 | 312 | )))))], |
|
| 313 | 313 | ); |
|
| 314 | 314 | } |
|
| 315 | 315 | ||
| 316 | 316 | #[test] |
|
| 317 | 317 | fn test_parse_if() { |
|
| 318 | 318 | ok( |
|
| 319 | 319 | "(if (eq? a b) 0 1)", |
|
| 320 | - | &[Expr::list([ |
|
| 320 | + | &[Expr::tuple([ |
|
| 321 | 321 | Expr::Symbol("if".into()), |
|
| 322 | - | Expr::list([ |
|
| 322 | + | Expr::tuple([ |
|
| 323 | 323 | Expr::Symbol("eq?".into()), |
|
| 324 | 324 | Expr::Symbol("a".into()), |
|
| 325 | 325 | Expr::Symbol("b".into()), |
|
| 326 | 326 | ]), |
|
| 327 | 327 | Expr::Integer(0), |