Functio & cart comparisons and hashing

This commit is contained in:
Alex Bethel 2021-08-07 16:33:28 -06:00
parent 3686b3b608
commit fd9054d3d3
2 changed files with 53 additions and 13 deletions

View file

@ -8,11 +8,13 @@
//! just plain subroutines and they do not return any value, //! just plain subroutines and they do not return any value,
//! so their calls are statements. //! so their calls are statements.
use std::hash::Hash;
use crate::variables::Value; use crate::variables::Value;
type Span = std::ops::Range<usize>; type Span = std::ops::Range<usize>;
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, Clone)]
pub struct Iden { pub struct Iden {
pub iden: String, pub iden: String,
pub span: Span, pub span: Span,
@ -24,19 +26,43 @@ impl Iden {
} }
} }
#[derive(Debug, PartialEq, Clone)] impl PartialEq for Iden {
fn eq(&self, other: &Self) -> bool {
self.iden == other.iden
}
}
impl Hash for Iden {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.iden.hash(state)
}
}
#[derive(Debug, PartialEq, Clone, Hash)]
pub struct Block { pub struct Block {
pub block: Vec<Stmt>, pub block: Vec<Stmt>,
} }
/// A syntactic unit expressing an effect. /// A syntactic unit expressing an effect.
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, Clone)]
pub struct Stmt { pub struct Stmt {
pub kind: StmtKind, pub kind: StmtKind,
pub span: Span, pub span: Span,
} }
#[derive(Debug, PartialEq, Clone)] impl PartialEq for Stmt {
fn eq(&self, other: &Self) -> bool {
self.kind == other.kind
}
}
impl Hash for Stmt {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.kind.hash(state)
}
}
#[derive(Debug, PartialEq, Clone, Hash)]
pub enum StmtKind { pub enum StmtKind {
// Control flow // Control flow
If { If {
@ -87,13 +113,25 @@ impl Stmt {
/// Expression is parse unit which do not cause any effect, /// Expression is parse unit which do not cause any effect,
/// like math and logical operations or values. /// like math and logical operations or values.
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, Clone)]
pub struct Expr { pub struct Expr {
pub kind: ExprKind, pub kind: ExprKind,
pub span: Span, pub span: Span,
} }
#[derive(Debug, PartialEq, Clone)] impl PartialEq for Expr {
fn eq(&self, other: &Self) -> bool {
self.kind == other.kind
}
}
impl Hash for Expr {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.kind.hash(state)
}
}
#[derive(Debug, PartialEq, Clone, Hash)]
pub enum ExprKind { pub enum ExprKind {
BinOp { BinOp {
lhs: Box<Expr>, lhs: Box<Expr>,
@ -116,7 +154,7 @@ impl Expr {
} }
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone, Hash)]
pub enum BinOpKind { pub enum BinOpKind {
Add, Add,
Subtract, Subtract,

View file

@ -1,4 +1,7 @@
use std::{cell::RefCell, collections::HashMap, fmt::Display, hash::Hash, io::Write, rc::Rc}; use std::{
cell::RefCell, collections::HashMap, fmt::Display, hash::Hash, io::Write, mem::discriminant,
rc::Rc,
};
use rand::Rng; use rand::Rng;
@ -31,7 +34,7 @@ impl From<Abool> for bool {
} }
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone, Hash)]
pub enum Functio { pub enum Functio {
BfFunctio { BfFunctio {
instructions: Vec<u8>, instructions: Vec<u8>,
@ -56,13 +59,14 @@ pub enum Value {
impl Hash for Value { impl Hash for Value {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
discriminant(self).hash(state);
match self { match self {
Value::Nul => (), Value::Nul => (),
Value::Str(v) => v.hash(state), Value::Str(v) => v.hash(state),
Value::Int(v) => v.hash(state), Value::Int(v) => v.hash(state),
Value::Bool(v) => v.hash(state), Value::Bool(v) => v.hash(state),
Value::Abool(v) => v.to_string().hash(state), Value::Abool(v) => v.to_string().hash(state),
Value::Functio(_) => todo!(), Value::Functio(statements) => statements.hash(state),
Value::Cart(_) => self.to_string().hash(state), Value::Cart(_) => self.to_string().hash(state),
} }
} }
@ -77,9 +81,7 @@ impl PartialEq for Value {
(Value::Bool(left), Value::Bool(right)) => left == right, (Value::Bool(left), Value::Bool(right)) => left == right,
(Value::Abool(left), Value::Abool(right)) => left == right, (Value::Abool(left), Value::Abool(right)) => left == right,
(Value::Functio(left), Value::Functio(right)) => left == right, (Value::Functio(left), Value::Functio(right)) => left == right,
(Value::Cart(_left), Value::Cart(_right)) => { (Value::Cart(_), Value::Cart(_)) => self.to_string() == other.to_string(),
todo!()
}
(_, _) => false, (_, _) => false,
// TODO: do more coercions! // TODO: do more coercions!
} }