From c3c8c239562cf1926a1f9df9278b3e5ff2860db6 Mon Sep 17 00:00:00 2001 From: Erin Date: Fri, 5 Aug 2022 23:41:09 +0200 Subject: [PATCH] added basic datatypes --- src/interpreter/mod.rs | 4 +-- src/interpreter/value.rs | 0 src/interpreter/value/function.rs | 2 ++ src/interpreter/value/list.rs | 2 ++ src/interpreter/value/mod.rs | 25 ++++++++++++++++ src/interpreter/value/string.rs | 48 +++++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 2 deletions(-) delete mode 100644 src/interpreter/value.rs create mode 100644 src/interpreter/value/function.rs create mode 100644 src/interpreter/value/list.rs create mode 100644 src/interpreter/value/mod.rs create mode 100644 src/interpreter/value/string.rs diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index 51eaba8..b5c3d37 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -1,8 +1,8 @@ //! **bodge** //! (third-person singular simple present bodges, present participle **bodging**, //! simple past and past participle **bodged**) -//! 1. (Britain) To do a clumsy or inelegant job, usually as a temporary repair; mend, patch up, repair. -//! 2. To work green wood using traditional country methods; to perform the craft of a bodger. +//! 1. (Britain) To do a clumsy or inelegant job, usually as a temporary repair; mend, patch up, repair. +//! 2. To work green wood using traditional country methods; to perform the craft of a bodger. //! //! A simple tree-walk interpreter intended for testing purposes. //! To be replaced by VM soon™️ :ferrisClueless: diff --git a/src/interpreter/value.rs b/src/interpreter/value.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/interpreter/value/function.rs b/src/interpreter/value/function.rs new file mode 100644 index 0000000..f3a0e28 --- /dev/null +++ b/src/interpreter/value/function.rs @@ -0,0 +1,2 @@ +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Function; diff --git a/src/interpreter/value/list.rs b/src/interpreter/value/list.rs new file mode 100644 index 0000000..da8868a --- /dev/null +++ b/src/interpreter/value/list.rs @@ -0,0 +1,2 @@ +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct List; diff --git a/src/interpreter/value/mod.rs b/src/interpreter/value/mod.rs new file mode 100644 index 0000000..6505580 --- /dev/null +++ b/src/interpreter/value/mod.rs @@ -0,0 +1,25 @@ +mod function; +mod list; +mod string; + +pub use function::Function; +pub use list::List; +pub use string::Str; + +use std::{collections::BTreeMap, rc::Rc}; + +pub type OrderedF64 = ordered_float::OrderedFloat; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Value<'s> { + List(List), + Vector(Rc>), + Map(Rc>), + Symbol(Str<'s>), + Keyword(Str<'s>), + Number(OrderedF64), + String(Str<'s>), + Function(Function), + Macro(Function), +} + diff --git a/src/interpreter/value/string.rs b/src/interpreter/value/string.rs new file mode 100644 index 0000000..52cc22a --- /dev/null +++ b/src/interpreter/value/string.rs @@ -0,0 +1,48 @@ +use std::{rc::Rc, ops::Deref, fmt::Display}; + +#[derive(Debug, Clone)] +pub enum Str<'a> { + Borrowed(&'a str), + Shared(Rc), +} + +impl<'a> From<&'a str> for Str<'a> { + fn from(x: &'a str) -> Self { + Self::Borrowed(x) + } +} + +impl<'a> From> for Str<'a> { + fn from(x: Rc) -> Self { + Self::Shared(x) + } +} + +impl<'a> Deref for Str<'a> { + type Target = str; + + fn deref(&self) -> &Self::Target { + match self { + Self::Borrowed(b) => b, + Self::Shared(s) => s, + } + } +} + +impl<'a> PartialEq for Str<'a> { + fn eq(&self, other: &Self) -> bool { + **self == **other + } +} + +impl<'a> Eq for Str<'a> {} + +impl<'a> Display for Str<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Str::Borrowed(b) => *b, + Str::Shared(s) => s, + } + .fmt(f) + } +}