Add more types

This commit is contained in:
Joscha 2021-09-27 22:06:35 +02:00
parent ce525ccce1
commit f42aec4132
5 changed files with 44 additions and 49 deletions

4
src/builtin.rs Normal file
View file

@ -0,0 +1,4 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Builtin {
Print,
}

View file

@ -1,5 +1,7 @@
use crate::table::{Table, TableOwner};
mod builtin;
mod path;
mod table;
mod values;

4
src/path.rs Normal file
View file

@ -0,0 +1,4 @@
use crate::values::Key;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Path(Vec<Key>);

View file

@ -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<RefCell<HashMap<Key, Value>>>);
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, "<broken table>"),
};
let hash_map = match rc.try_borrow_mut() {
Ok(hash_map) => hash_map,
Err(_) => return write!(f, "<cyclic ref>"),
};
hash_map.fmt(f)
}
}
impl PartialEq for Table {
fn eq(&self, other: &Self) -> bool {
self.0.ptr_eq(&other.0)

View file

@ -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<String>),
Bool(bool),
Builtin(Builtin),
Int(i64),
Nil,
Path(Path),
String(Box<String>),
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<Box<String>> for Key {
fn from(s: Box<String>) -> Self {
Self::String(s)
@ -57,25 +51,18 @@ impl From<Table> 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<String>),
Bool(bool),
Builtin(Builtin),
Int(i64),
Float(f64),
Nil,
Path(Path),
String(Box<String>),
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<Box<String>> for Value {
@ -108,14 +95,28 @@ impl From<i64> for Value {
}
}
impl From<Table> for Value {
fn from(t: Table) -> Self {
Self::Table(t)
}
}
impl From<f64> for Value {
fn from(f: f64) -> Self {
Self::Float(f)
}
}
impl From<Table> for Value {
fn from(t: Table) -> Self {
Self::Table(t)
impl From<Key> 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),
}
}
}