Change Name into Arc<str>

This commit is contained in:
Nicolas Abril 2024-02-13 14:48:49 +01:00
parent 9fe6a6bbe8
commit b2e6d0410e
7 changed files with 23 additions and 25 deletions

View File

@ -194,17 +194,7 @@ fn execute_cli_mode(cli: Cli, verbose: &dyn Fn(&hvml::term::Book)) -> Result<(),
desugar_book(&mut book, opts, None)?;
println!("{book}");
}
Mode::Run {
path,
mem,
debug,
mut single_core,
linear,
arg_stats,
comp_opts,
warn_opts,
lazy_mode,
} => {
Mode::Run { path, mem, debug, mut single_core, linear, arg_stats, comp_opts, warn_opts, lazy_mode } => {
if debug && lazy_mode {
return Err("Unsupported configuration, can not use debug mode `-d` with lazy mode `-L`".to_string());
}

View File

@ -1,7 +1,7 @@
use super::{
net_to_term::ReadbackError, Book, Definition, MatchNum, Name, Op, Pattern, Rule, Tag, Term, Type,
};
use std::fmt;
use std::{fmt, ops::Deref};
/* Some aux structures for things that are not so simple to display */
@ -265,5 +265,5 @@ impl Tag {
}
fn var_as_str(nam: &Option<Name>) -> &str {
nam.as_ref().map_or("*", |x| x.as_str())
nam.as_ref().map_or("*", Name::deref)
}

View File

@ -190,8 +190,8 @@ pub enum AdtEncoding {
TaggedScott,
}
#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Default)]
pub struct Name(pub Arc<String>);
#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord)]
pub struct Name(pub Arc<str>);
pub fn num_to_name(mut num: Val) -> String {
let mut name = String::new();
@ -758,7 +758,7 @@ impl Name {
impl From<String> for Name {
fn from(value: String) -> Self {
Name(Arc::new(value))
Name(Arc::from(value))
}
}
@ -769,13 +769,19 @@ impl From<Val> for Name {
}
impl Deref for Name {
type Target = String;
type Target = str;
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
impl AsRef<str> for Name {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl Book {
pub fn hvmc_entrypoint(&self) -> String {
if let Some(nam) = &self.entrypoint { nam.to_string() } else { ENTRY_POINT.to_string() }

View File

@ -47,8 +47,10 @@ impl Term {
let ctr_args = ctrs.get(nam).unwrap();
if pat_args.is_empty() && !ctr_args.is_empty() {
// Implicit ctr args
*pat_args =
ctr_args.iter().map(|field| Pattern::Var(Some(format!("{matched}.{field}").into()))).collect();
*pat_args = ctr_args
.iter()
.map(|field| Pattern::Var(Some(format!("{matched}.{field}").into())))
.collect();
}
}
Pattern::Num(MatchNum::Zero) => (),

View File

@ -69,7 +69,7 @@ impl Term {
// If variable not defined, we check if it's a ref and swap if it is.
Term::Var { nam } => {
if is_var_in_scope(nam, scope) {
if matches!(nam.as_str(), ENTRY_POINT | HVM1_ENTRY_POINT) {
if matches!(nam.as_ref(), ENTRY_POINT | HVM1_ENTRY_POINT) {
return Err("Main definition can't be referenced inside the program".to_string());
}

View File

@ -20,7 +20,7 @@ impl Term {
let head = std::mem::take(head);
let mut tail = std::mem::take(tail);
if ctr.as_str() == SCONS
if ctr.as_ref() == SCONS
&& let Term::Num { val } = head
&& let Term::Str { val: tail } = tail
{
@ -39,7 +39,7 @@ impl Term {
}
}
// (String.nil)
Term::Ref { nam: def_name } if def_name.as_str() == SNIL => *self = Term::Str { val: String::new() },
Term::Ref { nam: def_name } if def_name.as_ref() == SNIL => *self = Term::Str { val: String::new() },
Term::Mat { matched, arms } => {
matched.resugar_strings();
@ -88,7 +88,7 @@ impl Term {
let head = std::mem::take(head);
let tail = std::mem::take(tail);
if ctr.as_str() == LCONS
if ctr.as_ref() == LCONS
&& let Term::Lst { els: tail } = tail
{
// If well formed list, cons the next element to the list being formed
@ -100,7 +100,7 @@ impl Term {
}
}
// (List.nil)
Term::Ref { nam: def_name } if def_name.as_str() == LNIL => *self = Term::Lst { els: vec![] },
Term::Ref { nam: def_name } if def_name.as_ref() == LNIL => *self = Term::Lst { els: vec![] },
Term::Mat { matched, arms } => {
matched.resugar_lists();

View File

@ -186,7 +186,7 @@ fn encode_pattern_match() {
book.encode_builtins();
encode_pattern_matching(&mut book, &mut Vec::new(), adt_encoding)?;
book.prune(main.as_ref(), false, adt_encoding, &mut Vec::new());
book.merge_definitions(&main.clone().unwrap_or_default());
book.merge_definitions(&main.clone().unwrap_or(Name::new("")));
writeln!(result, "{adt_encoding:?}:").unwrap();
writeln!(result, "{book}\n").unwrap();