| @@ -39,6 +39,7 @@ pub enum Val { |
| 39 | 39 | Int(i64), |
| 40 | 40 | Float(f64), |
| 41 | 41 | Str(Rc<str>), |
| 42 | + Bytes(Rc<[u8]>), |
| 42 | 43 | Data { |
| 43 | 44 | tag: u32, |
| 44 | 45 | name: Rc<str>, |
| @@ -75,6 +76,7 @@ impl fmt::Debug for Val { |
| 75 | 76 | Val::Int(i) => write!(f, "{i}"), |
| 76 | 77 | Val::Float(x) => write!(f, "{x:?}"), |
| 77 | 78 | Val::Str(s) => write!(f, "{s:?}"), |
| 79 | + Val::Bytes(b) => write!(f, "b\"{}\"", b.escape_ascii()), |
| 78 | 80 | Val::Data { name, fields, .. } if fields.is_empty() => f.write_str(name), |
| 79 | 81 | Val::Data { name, fields, .. } => write!(f, "{name}{fields:?}"), |
| 80 | 82 | Val::Tuple(t) => write!(f, "{t:?}"), |
| @@ -587,18 +589,6 @@ impl Interp { |
| 587 | 589 | Some(v) => Ok(Val::Int(v)), |
| 588 | 590 | None => panic("integer overflow"), |
| 589 | 591 | }; |
| 590 | | − let order = |o: std::cmp::Ordering| { |
| 591 | | − let (tag, name) = match o { |
| 592 | | − std::cmp::Ordering::Less => (0, "Lt"), |
| 593 | | − std::cmp::Ordering::Equal => (1, "Eq"), |
| 594 | | − std::cmp::Ordering::Greater => (2, "Gt"), |
| 595 | | − }; |
| 596 | | − Val::Data { |
| 597 | | − tag, |
| 598 | | − name: name.into(), |
| 599 | | − fields: Rc::new([]), |
| 600 | | − } |
| 601 | | − }; |
| 602 | 592 | let a = args.first(); |
| 603 | 593 | let b = args.get(1); |
| 604 | 594 | let arg = |v: Option<&Val>| v.cloned().unwrap_or(Val::Unit); |
| @@ -649,18 +639,17 @@ impl Interp { |
| 649 | 639 | } else { |
| 650 | 640 | s.split(&*sep).collect() |
| 651 | 641 | }; |
| 652 | | − parts.iter().rev().fold( |
| 653 | | − Val::Data { |
| 654 | | − tag: 0, |
| 655 | | − name: "Nil".into(), |
| 656 | | − fields: Rc::new([]), |
| 657 | | − }, |
| 658 | | − |tail, p| Val::Data { |
| 659 | | − tag: 1, |
| 660 | | − name: "Cons".into(), |
| 661 | | − fields: Rc::new([Val::Str((*p).into()), tail]), |
| 662 | | − }, |
| 663 | | − ) |
| 642 | + list_val(parts.iter().map(|p| Val::Str((*p).into()))) |
| 643 | + } |
| 644 | + "tl_string_join" => { |
| 645 | + let parts = list_items(&arg(a))? |
| 646 | + .iter() |
| 647 | + .map(string) |
| 648 | + .collect::<R<Vec<_>>>()?; |
| 649 | + Val::Str(parts.join(&*string(&arg(b))?).into()) |
| 650 | + } |
| 651 | + s if s.starts_with("tl_bytes_") || s == "tl_hash_bytes" => { |
| 652 | + return bytes_extern(s, &args); |
| 664 | 653 | } |
| 665 | 654 | "tl_string_parse_int" => { |
| 666 | 655 | let parsed = string(&arg(a))? |
| @@ -688,15 +677,7 @@ impl Interp { |
| 688 | 677 | "tl_string_debug" => Val::Str(format!("{:?}", &*string(&arg(a))?).into()), |
| 689 | 678 | "tl_hash_int" => Val::Int(mix(int(&arg(a))? as u64)), |
| 690 | 679 | "tl_hash_float" => Val::Int(mix(float(&arg(a))?.to_bits())), |
| 691 | | − "tl_hash_string" => { |
| 692 | | − // FNV-1a, then mixed. |
| 693 | | − let h = string(&arg(a))? |
| 694 | | − .bytes() |
| 695 | | − .fold(0xcbf2_9ce4_8422_2325u64, |h, b| { |
| 696 | | − (h ^ u64::from(b)).wrapping_mul(0x100_0000_01b3) |
| 697 | | − }); |
| 698 | | − Val::Int(mix(h)) |
| 699 | | − } |
| 680 | + "tl_hash_string" => Val::Int(hash_bytes(string(&arg(a))?.as_bytes())), |
| 700 | 681 | "tl_hash_combine" => { |
| 701 | 682 | let (x, y) = (int(&arg(a))? as u64, int(&arg(b))? as u64); |
| 702 | 683 | Val::Int(mix(x.wrapping_mul(31).wrapping_add(y) ^ (x >> 7))) |
| @@ -760,6 +741,135 @@ impl Interp { |
| 760 | 741 | } |
| 761 | 742 | } |
| 762 | 743 | |
| 744 | +/// `std/bytes` (and the prelude's `Bytes` impls). Same as the runtime's |
| 745 | +/// (`tilrt::bytes`). |
| 746 | +fn bytes_extern(sym: &str, args: &[Val]) -> R<Val> { |
| 747 | + let bytes = |i: usize| match args.get(i) { |
| 748 | + Some(Val::Bytes(b)) => Ok(b.clone()), |
| 749 | + other => panic(format!("{sym}: expected Bytes, got {other:?}")), |
| 750 | + }; |
| 751 | + let int = |i: usize| match args.get(i) { |
| 752 | + Some(Val::Int(n)) => Ok(*n), |
| 753 | + other => panic(format!("{sym}: expected an Int, got {other:?}")), |
| 754 | + }; |
| 755 | + let new = |b: &[u8]| Val::Bytes(b.into()); |
| 756 | + Ok(match sym { |
| 757 | + "tl_bytes_length" => Val::Int(bytes(0)?.len() as i64), |
| 758 | + "tl_bytes_at" => { |
| 759 | + let b = bytes(0)?; |
| 760 | + let byte = usize::try_from(int(1)?).ok().and_then(|i| b.get(i)); |
| 761 | + Val::Int(byte.map_or(-1, |&b| i64::from(b))) |
| 762 | + } |
| 763 | + "tl_bytes_slice" => { |
| 764 | + let b = bytes(0)?; |
| 765 | + let clamp = |v: i64| v.clamp(0, b.len() as i64) as usize; |
| 766 | + let (start, end) = (clamp(int(1)?), clamp(int(2)?)); |
| 767 | + new(&b[start..end.max(start)]) |
| 768 | + } |
| 769 | + "tl_bytes_concat" => new(&[bytes(0)?, bytes(1)?].concat()), |
| 770 | + "tl_bytes_concat_all" | "tl_bytes_build" => { |
| 771 | + let mut parts = Vec::new(); |
| 772 | + for p in list_items(args.first().unwrap_or(&Val::Unit))? { |
| 773 | + match p { |
| 774 | + Val::Bytes(b) => parts.push(b), |
| 775 | + other => return panic(format!("{sym}: expected Bytes, got {other:?}")), |
| 776 | + } |
| 777 | + } |
| 778 | + // A builder keeps its parts most recent first. |
| 779 | + if sym == "tl_bytes_build" { |
| 780 | + parts.reverse(); |
| 781 | + } |
| 782 | + new(&parts.concat()) |
| 783 | + } |
| 784 | + "tl_bytes_eq" => bool_val(bytes(0)? == bytes(1)?), |
| 785 | + "tl_bytes_compare" => order(bytes(0)?.cmp(&bytes(1)?)), |
| 786 | + "tl_hash_bytes" => Val::Int(hash_bytes(&bytes(0)?)), |
| 787 | + "tl_bytes_debug" => Val::Str(format!("b\"{}\"", bytes(0)?.escape_ascii()).into()), |
| 788 | + "tl_bytes_from_string" => match args.first() { |
| 789 | + Some(Val::Str(s)) => new(s.as_bytes()), |
| 790 | + other => return panic(format!("{sym}: expected a String, got {other:?}")), |
| 791 | + }, |
| 792 | + "tl_bytes_to_string" => match std::str::from_utf8(&bytes(0)?) { |
| 793 | + Ok(text) => data(0, "Ok", vec![Val::Str(text.into())]), |
| 794 | + Err(e) => data( |
| 795 | + 1, |
| 796 | + "Error", |
| 797 | + vec![Val::Str( |
| 798 | + format!("invalid UTF-8 at byte {}", e.valid_up_to()).into(), |
| 799 | + )], |
| 800 | + ), |
| 801 | + }, |
| 802 | + "tl_bytes_to_string_lossy" => Val::Str(String::from_utf8_lossy(&bytes(0)?).into()), |
| 803 | + "tl_bytes_from_list" => { |
| 804 | + let mut out = Vec::new(); |
| 805 | + for v in list_items(args.first().unwrap_or(&Val::Unit))? { |
| 806 | + match v { |
| 807 | + Val::Int(n) => match u8::try_from(n) { |
| 808 | + Ok(b) => out.push(b), |
| 809 | + Err(_) => return panic(format!("bytes.from_list: {n} is not a byte")), |
| 810 | + }, |
| 811 | + other => return panic(format!("{sym}: expected an Int, got {other:?}")), |
| 812 | + } |
| 813 | + } |
| 814 | + new(&out) |
| 815 | + } |
| 816 | + "tl_bytes_to_list" => list_val(bytes(0)?.iter().map(|&b| Val::Int(i64::from(b)))), |
| 817 | + other => { |
| 818 | + return panic(format!( |
| 819 | + "unknown extern `{other}` (not available in the interpreter)" |
| 820 | + )); |
| 821 | + } |
| 822 | + }) |
| 823 | +} |
| 824 | + |
| 825 | +fn data(tag: u32, name: &str, fields: Vec<Val>) -> Val { |
| 826 | + Val::Data { |
| 827 | + tag, |
| 828 | + name: name.into(), |
| 829 | + fields: fields.into(), |
| 830 | + } |
| 831 | +} |
| 832 | + |
| 833 | +/// `Lt`, `Eq` or `Gt`. |
| 834 | +fn order(o: std::cmp::Ordering) -> Val { |
| 835 | + match o { |
| 836 | + std::cmp::Ordering::Less => data(0, "Lt", Vec::new()), |
| 837 | + std::cmp::Ordering::Equal => data(1, "Eq", Vec::new()), |
| 838 | + std::cmp::Ordering::Greater => data(2, "Gt", Vec::new()), |
| 839 | + } |
| 840 | +} |
| 841 | + |
| 842 | +/// A `List` of `items`. |
| 843 | +fn list_val(items: impl DoubleEndedIterator<Item = Val>) -> Val { |
| 844 | + items.rev().fold(data(0, "Nil", Vec::new()), |tail, head| { |
| 845 | + data(1, "Cons", vec![head, tail]) |
| 846 | + }) |
| 847 | +} |
| 848 | + |
| 849 | +/// The elements of a `List`. |
| 850 | +fn list_items(list: &Val) -> R<Vec<Val>> { |
| 851 | + let mut items = Vec::new(); |
| 852 | + let mut cur = list; |
| 853 | + loop { |
| 854 | + match cur { |
| 855 | + Val::Data { tag: 0, .. } => return Ok(items), |
| 856 | + Val::Data { tag: 1, fields, .. } if fields.len() == 2 => { |
| 857 | + items.push(fields[0].clone()); |
| 858 | + cur = &fields[1]; |
| 859 | + } |
| 860 | + other => return panic(format!("expected a list, got {other:?}")), |
| 861 | + } |
| 862 | + } |
| 863 | +} |
| 864 | + |
| 865 | +/// FNV-1a, then mixed (strings and `Bytes`). |
| 866 | +fn hash_bytes(bytes: &[u8]) -> i64 { |
| 867 | + let h = bytes.iter().fold(0xcbf2_9ce4_8422_2325u64, |h, b| { |
| 868 | + (h ^ u64::from(*b)).wrapping_mul(0x100_0000_01b3) |
| 869 | + }); |
| 870 | + mix(h) |
| 871 | +} |
| 872 | + |
| 763 | 873 | /// Decode `%XX` and `+` (URL form encoding); invalid escapes stay. Same |
| 764 | 874 | /// as the runtime's (`tilrt::prims::url_decode`). |
| 765 | 875 | fn url_decode(s: &str) -> String { |