From 6ccc6aaadee873fb995f6e7ba9f98d1e1f3578b9 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Thu, 21 May 2020 20:59:39 -0400 Subject: [PATCH] DEV: Reorganize some conversion code. --- src/build/opcode/convert.rs | 148 +++++++++++++++++++++++++++++++++++- src/build/opcode/mod.rs | 138 --------------------------------- 2 files changed, 145 insertions(+), 141 deletions(-) diff --git a/src/build/opcode/convert.rs b/src/build/opcode/convert.rs index 39e10a6..b248b48 100644 --- a/src/build/opcode/convert.rs +++ b/src/build/opcode/convert.rs @@ -1,7 +1,12 @@ -use std::convert::TryFrom; +use std::convert::{From, TryFrom}; +use std::rc::Rc; -use super::Primitive; -use crate::ast::CastType; +use super::{Composite, Primitive, Val, Value}; +use crate::ast::{CastType, Position}; + +use Composite::{List, Tuple}; +use Primitive::{Bool, Empty, Float, Int, Str}; +use Value::{C, F, M, P, S, T}; pub struct Error { val: Primitive, @@ -91,3 +96,140 @@ impl TryFrom<&Primitive> for bool { } } } + +impl From> for Val { + fn from(val: Rc) -> Val { + val.as_ref().into() + } +} + +impl From for Val { + fn from(val: Value) -> Val { + (&val).into() + } +} + +impl From<&Value> for Val { + fn from(val: &Value) -> Val { + match val { + P(Int(i)) => Val::Int(*i), + P(Float(f)) => Val::Float(*f), + P(Str(s)) => Val::Str(s.clone()), + P(Bool(b)) => Val::Boolean(*b), + C(Tuple(fs, _)) => { + let mut flds = Vec::new(); + for &(ref k, ref v) in fs.iter() { + let v = v.clone(); + flds.push((k.clone(), Rc::new(v.into()))); + } + Val::Tuple(flds) + } + C(List(elems, _)) => { + let mut els = Vec::new(); + for e in elems.iter() { + let e = e.clone(); + els.push(Rc::new(e.into())); + } + Val::List(els) + } + S(_) | F(_) | M(_) | T(_) | P(Empty) => Val::Empty, + } + } +} + +impl From> for Value { + fn from(val: Rc) -> Self { + val.as_ref().into() + } +} + +impl From for Value { + fn from(val: Val) -> Self { + (&val).into() + } +} + +impl From<&Val> for Value { + fn from(val: &Val) -> Self { + match val { + Val::Int(i) => P(Int(*i)), + Val::Float(f) => P(Float(*f)), + Val::Boolean(b) => P(Bool(*b)), + Val::Str(s) => P(Str(s.clone())), + Val::Empty => P(Empty), + Val::List(els) => { + let mut lst = Vec::new(); + let mut positions = Vec::new(); + for e in els.iter() { + let e = e.clone(); + lst.push(Rc::new(e.into())); + positions.push(Position::new(0, 0, 0)); + } + // TODO(jwall): This should have a set of + // Positions of the same length. + C(List(lst, positions)) + } + Val::Tuple(flds) => { + let mut field_list = Vec::new(); + let mut positions = Vec::new(); + for &(ref key, ref val) in flds.iter() { + let val = val.clone(); + field_list.push((key.clone(), Rc::new(val.into()))); + positions.push((Position::new(0, 0, 0), Position::new(0, 0, 0))); + } + C(Tuple(field_list, positions)) + } + Val::Env(flds) => { + let mut field_list = Vec::new(); + let mut positions = Vec::new(); + for &(ref key, ref val) in flds.iter() { + field_list.push((key.clone(), Rc::new(P(Str(val.clone()))))); + positions.push((Position::new(0, 0, 0), Position::new(0, 0, 0))); + } + C(Tuple(field_list, positions)) + } + } + } +} + +impl From<&Composite> for String { + fn from(c: &Composite) -> Self { + let mut buf = String::new(); + match c { + &List(ref elems, _) => { + buf.push_str("["); + for e in elems.iter() { + let val: String = e.as_ref().into(); + buf.push_str(&val); + buf.push_str(","); + } + buf.push_str("]"); + } + &Tuple(ref flds, _) => { + buf.push_str("{"); + for &(ref k, ref v) in flds.iter() { + buf.push_str(&k); + buf.push_str(" = "); + let val: String = v.as_ref().into(); + buf.push_str(&val); + buf.push_str(","); + } + buf.push_str("}"); + } + } + buf + } +} + +impl From<&Value> for String { + fn from(v: &Value) -> Self { + match v { + &S(ref s) => s.clone(), + &P(ref p) => p.into(), + &C(ref c) => c.into(), + &T(_) => "".to_owned(), + &F(_) => "".to_owned(), + &M(_) => "".to_owned(), + } + } +} diff --git a/src/build/opcode/mod.rs b/src/build/opcode/mod.rs index 4e25627..18349d0 100644 --- a/src/build/opcode/mod.rs +++ b/src/build/opcode/mod.rs @@ -72,35 +72,6 @@ pub enum Composite { use Composite::{List, Tuple}; -impl From<&Composite> for String { - fn from(c: &Composite) -> Self { - let mut buf = String::new(); - match c { - &List(ref elems, _) => { - buf.push_str("["); - for e in elems.iter() { - let val: String = e.as_ref().into(); - buf.push_str(&val); - buf.push_str(","); - } - buf.push_str("]"); - } - &Tuple(ref flds, _) => { - buf.push_str("{"); - for &(ref k, ref v) in flds.iter() { - buf.push_str(&k); - buf.push_str(" = "); - let val: String = v.as_ref().into(); - buf.push_str(&val); - buf.push_str(","); - } - buf.push_str("}"); - } - } - buf - } -} - #[derive(Debug, PartialEq, Clone)] pub struct Func { ptr: OpPointer, @@ -133,19 +104,6 @@ pub enum Value { M(Module), } -impl From<&Value> for String { - fn from(v: &Value) -> Self { - match v { - &S(ref s) => s.clone(), - &P(ref p) => p.into(), - &C(ref c) => c.into(), - &T(_) => "".to_owned(), - &F(_) => "".to_owned(), - &M(_) => "".to_owned(), - } - } -} - use Value::{C, F, M, P, S, T}; #[derive(Debug, PartialEq, Clone)] @@ -263,99 +221,3 @@ impl PartialEq for Value { } } } - -// TODO(jwall): Move all of this into the convert module. -impl From> for Val { - fn from(val: Rc) -> Val { - val.as_ref().into() - } -} - -impl From for Val { - fn from(val: Value) -> Val { - (&val).into() - } -} - -impl From<&Value> for Val { - fn from(val: &Value) -> Val { - match val { - P(Int(i)) => Val::Int(*i), - P(Float(f)) => Val::Float(*f), - P(Str(s)) => Val::Str(s.clone()), - P(Bool(b)) => Val::Boolean(*b), - C(Tuple(fs, _)) => { - let mut flds = Vec::new(); - for &(ref k, ref v) in fs.iter() { - let v = v.clone(); - flds.push((k.clone(), Rc::new(v.into()))); - } - Val::Tuple(flds) - } - C(List(elems, _)) => { - let mut els = Vec::new(); - for e in elems.iter() { - let e = e.clone(); - els.push(Rc::new(e.into())); - } - Val::List(els) - } - S(_) | F(_) | M(_) | T(_) | P(Empty) => Val::Empty, - } - } -} - -impl From> for Value { - fn from(val: Rc) -> Self { - val.as_ref().into() - } -} - -impl From for Value { - fn from(val: Val) -> Self { - (&val).into() - } -} - -impl From<&Val> for Value { - fn from(val: &Val) -> Self { - match val { - Val::Int(i) => P(Int(*i)), - Val::Float(f) => P(Float(*f)), - Val::Boolean(b) => P(Bool(*b)), - Val::Str(s) => P(Str(s.clone())), - Val::Empty => P(Empty), - Val::List(els) => { - let mut lst = Vec::new(); - let mut positions = Vec::new(); - for e in els.iter() { - let e = e.clone(); - lst.push(Rc::new(e.into())); - positions.push(Position::new(0, 0, 0)); - } - // TODO(jwall): This should have a set of - // Positions of the same length. - C(List(lst, positions)) - } - Val::Tuple(flds) => { - let mut field_list = Vec::new(); - let mut positions = Vec::new(); - for &(ref key, ref val) in flds.iter() { - let val = val.clone(); - field_list.push((key.clone(), Rc::new(val.into()))); - positions.push((Position::new(0, 0, 0), Position::new(0, 0, 0))); - } - C(Tuple(field_list, positions)) - } - Val::Env(flds) => { - let mut field_list = Vec::new(); - let mut positions = Vec::new(); - for &(ref key, ref val) in flds.iter() { - field_list.push((key.clone(), Rc::new(P(Str(val.clone()))))); - positions.push((Position::new(0, 0, 0), Position::new(0, 0, 0))); - } - C(Tuple(field_list, positions)) - } - } - } -}