summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/lib.rs60
1 files changed, 29 insertions, 31 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 71f7b7d..bb8e638 100755
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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> {