From f42aec41328ab3c0f54c03e2015f2f794420a6f0 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 27 Sep 2021 22:06:35 +0200 Subject: [PATCH] Add more types --- src/builtin.rs | 4 ++++ src/main.rs | 2 ++ src/path.rs | 4 ++++ src/table.rs | 20 ++-------------- src/values.rs | 63 +++++++++++++++++++++++++------------------------- 5 files changed, 44 insertions(+), 49 deletions(-) create mode 100644 src/builtin.rs create mode 100644 src/path.rs diff --git a/src/builtin.rs b/src/builtin.rs new file mode 100644 index 0000000..60fddb9 --- /dev/null +++ b/src/builtin.rs @@ -0,0 +1,4 @@ +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Builtin { + Print, +} diff --git a/src/main.rs b/src/main.rs index ebfde17..3c5a84b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ use crate::table::{Table, TableOwner}; +mod builtin; +mod path; mod table; mod values; diff --git a/src/path.rs b/src/path.rs new file mode 100644 index 0000000..ab2c10d --- /dev/null +++ b/src/path.rs @@ -0,0 +1,4 @@ +use crate::values::Key; + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Path(Vec); diff --git a/src/table.rs b/src/table.rs index 8801550..03e8282 100644 --- a/src/table.rs +++ b/src/table.rs @@ -1,6 +1,6 @@ use std::cell::RefCell; use std::collections::HashMap; -use std::fmt::{self, Debug}; +use std::fmt::Debug; use std::hash::{Hash, Hasher}; use std::rc::{Rc, Weak}; @@ -14,25 +14,9 @@ impl TableOwner { } } -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct Table(Weak>>); -impl Debug for Table { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let rc = match self.0.upgrade() { - Some(rc) => rc, - None => return write!(f, ""), - }; - - let hash_map = match rc.try_borrow_mut() { - Ok(hash_map) => hash_map, - Err(_) => return write!(f, ""), - }; - - hash_map.fmt(f) - } -} - impl PartialEq for Table { fn eq(&self, other: &Self) -> bool { self.0.ptr_eq(&other.0) diff --git a/src/values.rs b/src/values.rs index 7b27c9b..51b215a 100644 --- a/src/values.rs +++ b/src/values.rs @@ -1,26 +1,20 @@ -use std::fmt::{self, Debug}; +use std::fmt::Debug; +use crate::builtin::Builtin; +use crate::path::Path; use crate::table::Table; -#[derive(Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum Key { - String(Box), Bool(bool), + Builtin(Builtin), Int(i64), + Nil, + Path(Path), + String(Box), Table(Table), } -impl Debug for Key { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::String(s) => s.fmt(f), - Self::Bool(b) => b.fmt(f), - Self::Int(i) => i.fmt(f), - Self::Table(t) => t.fmt(f), - } - } -} - impl From> for Key { fn from(s: Box) -> Self { Self::String(s) @@ -57,25 +51,18 @@ impl From for Key { } } -#[derive(Clone, PartialEq)] +// TODO: Unwrap the Key part so this is only 16, not 24 bytes +#[derive(Debug, Clone, PartialEq)] pub enum Value { - String(Box), Bool(bool), + Builtin(Builtin), Int(i64), - Float(f64), + Nil, + Path(Path), + String(Box), Table(Table), -} -impl Debug for Value { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::String(s) => s.fmt(f), - Self::Bool(b) => b.fmt(f), - Self::Int(i) => i.fmt(f), - Self::Table(t) => t.fmt(f), - Self::Float(d) => d.fmt(f), - } - } + Float(f64), } impl From> for Value { @@ -108,14 +95,28 @@ impl From for Value { } } +impl From
for Value { + fn from(t: Table) -> Self { + Self::Table(t) + } +} + impl From for Value { fn from(f: f64) -> Self { Self::Float(f) } } -impl From
for Value { - fn from(t: Table) -> Self { - Self::Table(t) +impl From for Value { + fn from(k: Key) -> Self { + match k { + Key::Bool(b) => Self::Bool(b), + Key::Builtin(b) => Self::Builtin(b), + Key::Int(i) => Self::Int(i), + Key::Nil => Self::Nil, + Key::Path(p) => Self::Path(p), + Key::String(s) => Self::String(s), + Key::Table(t) => Self::Table(t), + } } }