diff options
| author | Brian Cully <bjc@spork.org> | 2025-08-24 11:42:48 -0400 |
|---|---|---|
| committer | Brian Cully <bjc@spork.org> | 2025-08-24 11:42:48 -0400 |
| commit | ddf944d808c71946a67d21d9fd1268ca05b787d1 (patch) | |
| tree | 977abc3b4e73dcf770bdcd5774d204b6160cbd56 /src/lib.rs | |
| parent | 60e9b52f7c692c95f6d6066c02342428885f8995 (diff) | |
| download | automathon-ddf944d808c71946a67d21d9fd1268ca05b787d1.tar.gz automathon-ddf944d808c71946a67d21d9fd1268ca05b787d1.zip | |
try some rusty stuff with std traits
Diffstat (limited to 'src/lib.rs')
| -rwxr-xr-x | src/lib.rs | 60 |
1 files changed, 29 insertions, 31 deletions
@@ -22,20 +22,22 @@ impl From<&forth::interp::InstructionPointer> for ExportedInstructionPointer { pub struct ExportedByteCode(Vec<String>); impl ExportedByteCode { - pub fn from_bc(bc: &forth::interp::ByteCode) -> Self { - fn tr(op: &forth::interp::OpCode) -> String { - use forth::interp::OpCode::*; - let s = match op { - If(t, None) => format!("If({}, none)", t), - If(t, Some(f)) => format!("If({}, {})", t, f), - TIf(t, None) => format!("TIf({}, none)", t), - TIf(t, Some(f)) => format!("TIf({}, {})", t, f), - other => format!("{:?}", other), - }; - s.to_string() - } + fn tr_op(op: &forth::interp::OpCode) -> String { + use forth::interp::OpCode::*; + let s = match op { + If(t, None) => format!("If({}, none)", t), + If(t, Some(f)) => format!("If({}, {})", t, f), + TIf(t, None) => format!("TIf({}, none)", t), + TIf(t, Some(f)) => format!("TIf({}, {})", t, f), + other => format!("{:?}", other), + }; + s.to_string() + } +} - ExportedByteCode(bc.iter().map(tr).collect()) +impl From<&forth::interp::ByteCode> for ExportedByteCode { + fn from(v: &forth::interp::ByteCode) -> Self { + Self(v.iter().map(Self::tr_op).collect()) } } @@ -58,11 +60,11 @@ pub struct ExportedAnnotation { pub end: usize, } -impl From<(usize, usize)> for ExportedAnnotation { - fn from(v: (usize, usize)) -> Self { +impl From<&forth::parser::Annotation> for ExportedAnnotation { + fn from(v: &forth::parser::Annotation) -> Self { Self { - start: v.0, - end: v.1, + start: v.loc.0, + end: v.loc.1, } } } @@ -72,9 +74,9 @@ impl From<(usize, usize)> for ExportedAnnotation { #[derive(Clone)] pub struct ExportedWordAnnotations(Vec<ExportedAnnotation>); -impl ExportedWordAnnotations { - pub fn from_word_annotations(annos: &Vec<forth::parser::Annotation>) -> Self { - ExportedWordAnnotations(annos.iter().map(|anno| anno.loc.into()).collect()) +impl FromIterator<ExportedAnnotation> for ExportedWordAnnotations { + fn from_iter<T: IntoIterator<Item = ExportedAnnotation>>(iter: T) -> Self { + ExportedWordAnnotations(iter.into_iter().collect()) } } @@ -107,16 +109,12 @@ impl ExportedInterp { error!("couldn't parse program text: {:?}", e); return false } - self.annos = p.annotations.iter() - .map(|word_anno| -> ExportedWordAnnotations { - let v = word_anno.into_iter() - .map(|anno| -> ExportedAnnotation { - anno.loc.into() - }) - .collect(); - ExportedWordAnnotations(v) - }) - .collect(); + self.annos = + p.annotations.iter() + .map(|word_anno| -> ExportedWordAnnotations { + word_anno.into_iter().map(|a| a.into()).collect() + }) + .collect(); let interp = forth::interp::Interp::new(p.wordlist); let _ = self.i.insert(interp); true @@ -152,7 +150,7 @@ impl ExportedInterp { return vec![] }; - interp.wordlist.iter().map(|bc| ExportedByteCode::from_bc(bc)).collect() + interp.wordlist.iter().map(|bc| bc.into()).collect() } pub fn annotations(&self) -> Vec<ExportedWordAnnotations> { |
