aboutsummaryrefslogtreecommitdiff
path: root/src/interpret
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2021-11-21 14:52:50 +0100
committerMel <einebeere@gmail.com>2021-11-21 14:52:50 +0100
commitabaa72af8a1be00f3f84d7771a7994bfbbccf719 (patch)
treeb47edcef38cf7bdebae99428510b9fc8857f41bd /src/interpret
parent395d086f0dce355ccdcf3da149c309826c539b48 (diff)
downloadrabbithole-abaa72af8a1be00f3f84d7771a7994bfbbccf719.tar.zst
rabbithole-abaa72af8a1be00f3f84d7771a7994bfbbccf719.zip
Rename all Variants to Kinds
Diffstat (limited to 'src/interpret')
-rw-r--r--src/interpret/operator.rs148
-rw-r--r--src/interpret/scope.rs18
-rw-r--r--src/interpret/value.rs46
-rw-r--r--src/interpret/walker.rs26
4 files changed, 118 insertions, 120 deletions
diff --git a/src/interpret/operator.rs b/src/interpret/operator.rs
index 78ebe75..ce062c7 100644
--- a/src/interpret/operator.rs
+++ b/src/interpret/operator.rs
@@ -3,7 +3,7 @@ use thiserror::Error;
use crate::{parse::ast::expression::Expression, types::bag::TypeBag};
use super::{
- value::{Value, ValueVariant},
+ value::{Value, ValueKind},
walker::{Walker, WalkerError},
};
@@ -17,23 +17,23 @@ impl<'t> ValueOperator<'t> {
}
pub fn add(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Str(ref l) => match rhs.variant {
- ValueVariant::Str(r) => Ok(Value::str(format!("{}{}", l, r), self.types)),
- ValueVariant::Float(r) => Ok(Value::str(format!("{}{}", l, r), self.types)),
- ValueVariant::Int(r) => Ok(Value::str(format!("{}{}", l, r), self.types)),
+ match lhs.kind {
+ ValueKind::Str(ref l) => match rhs.kind {
+ ValueKind::Str(r) => Ok(Value::str(format!("{}{}", l, r), self.types)),
+ ValueKind::Float(r) => Ok(Value::str(format!("{}{}", l, r), self.types)),
+ ValueKind::Int(r) => Ok(Value::str(format!("{}{}", l, r), self.types)),
_ => Err(OperationError::AddTypes(lhs, rhs)),
},
- ValueVariant::Float(l) => match rhs.variant {
- ValueVariant::Str(r) => Ok(Value::str(l.to_string() + &r, self.types)),
- ValueVariant::Float(r) => Ok(Value::float(l + r, self.types)),
- ValueVariant::Int(r) => Ok(Value::float(l + r as f64, self.types)),
+ ValueKind::Float(l) => match rhs.kind {
+ ValueKind::Str(r) => Ok(Value::str(l.to_string() + &r, self.types)),
+ ValueKind::Float(r) => Ok(Value::float(l + r, self.types)),
+ ValueKind::Int(r) => Ok(Value::float(l + r as f64, self.types)),
_ => Err(OperationError::AddTypes(lhs, rhs)),
},
- ValueVariant::Int(l) => match rhs.variant {
- ValueVariant::Str(r) => Ok(Value::str(l.to_string() + &r, self.types)),
- ValueVariant::Float(r) => Ok(Value::float(l as f64 + r, self.types)),
- ValueVariant::Int(r) => Ok(Value::int(l + r, self.types)),
+ ValueKind::Int(l) => match rhs.kind {
+ ValueKind::Str(r) => Ok(Value::str(l.to_string() + &r, self.types)),
+ ValueKind::Float(r) => Ok(Value::float(l as f64 + r, self.types)),
+ ValueKind::Int(r) => Ok(Value::int(l + r, self.types)),
_ => Err(OperationError::AddTypes(lhs, rhs)),
},
_ => Err(OperationError::AddTypes(lhs, rhs)),
@@ -41,15 +41,15 @@ impl<'t> ValueOperator<'t> {
}
pub fn sub(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Float(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::float(l - r, self.types)),
- ValueVariant::Int(r) => Ok(Value::float(l - r as f64, self.types)),
+ match lhs.kind {
+ ValueKind::Float(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::float(l - r, self.types)),
+ ValueKind::Int(r) => Ok(Value::float(l - r as f64, self.types)),
_ => Err(OperationError::SubTypes(lhs, rhs)),
},
- ValueVariant::Int(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::float(l as f64 - r, self.types)),
- ValueVariant::Int(r) => Ok(Value::int(l - r, self.types)),
+ ValueKind::Int(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::float(l as f64 - r, self.types)),
+ ValueKind::Int(r) => Ok(Value::int(l - r, self.types)),
_ => Err(OperationError::SubTypes(lhs, rhs)),
},
_ => Err(OperationError::SubTypes(lhs, rhs)),
@@ -57,15 +57,15 @@ impl<'t> ValueOperator<'t> {
}
pub fn mul(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Float(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::float(l * r, self.types)),
- ValueVariant::Int(r) => Ok(Value::float(l * r as f64, self.types)),
+ match lhs.kind {
+ ValueKind::Float(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::float(l * r, self.types)),
+ ValueKind::Int(r) => Ok(Value::float(l * r as f64, self.types)),
_ => Err(OperationError::MulTypes(lhs, rhs)),
},
- ValueVariant::Int(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::float(l as f64 * r, self.types)),
- ValueVariant::Int(r) => Ok(Value::int(l * r, self.types)),
+ ValueKind::Int(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::float(l as f64 * r, self.types)),
+ ValueKind::Int(r) => Ok(Value::int(l * r, self.types)),
_ => Err(OperationError::MulTypes(lhs, rhs)),
},
_ => Err(OperationError::MulTypes(lhs, rhs)),
@@ -73,15 +73,15 @@ impl<'t> ValueOperator<'t> {
}
pub fn div(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Float(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::float(l / r, self.types)),
- ValueVariant::Int(r) => Ok(Value::float(l / r as f64, self.types)),
+ match lhs.kind {
+ ValueKind::Float(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::float(l / r, self.types)),
+ ValueKind::Int(r) => Ok(Value::float(l / r as f64, self.types)),
_ => Err(OperationError::DivTypes(lhs, rhs)),
},
- ValueVariant::Int(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::float(l as f64 / r, self.types)),
- ValueVariant::Int(r) => Ok(Value::float(l as f64 / r as f64, self.types)),
+ ValueKind::Int(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::float(l as f64 / r, self.types)),
+ ValueKind::Int(r) => Ok(Value::float(l as f64 / r as f64, self.types)),
_ => Err(OperationError::DivTypes(lhs, rhs)),
},
_ => Err(OperationError::DivTypes(lhs, rhs)),
@@ -89,21 +89,21 @@ impl<'t> ValueOperator<'t> {
}
pub fn eq(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Str(l) => match rhs.variant {
- ValueVariant::Str(r) => Ok(Value::bool(l == r, self.types)),
+ match lhs.kind {
+ ValueKind::Str(l) => match rhs.kind {
+ ValueKind::Str(r) => Ok(Value::bool(l == r, self.types)),
_ => Ok(Value::bool(false, self.types)),
},
- ValueVariant::Float(l) => match rhs.variant {
- ValueVariant::Float(r) => Ok(Value::bool(l == r, self.types)),
+ ValueKind::Float(l) => match rhs.kind {
+ ValueKind::Float(r) => Ok(Value::bool(l == r, self.types)),
_ => Ok(Value::bool(false, self.types)),
},
- ValueVariant::Int(l) => match rhs.variant {
- ValueVariant::Int(r) => Ok(Value::bool(l == r, self.types)),
+ ValueKind::Int(l) => match rhs.kind {
+ ValueKind::Int(r) => Ok(Value::bool(l == r, self.types)),
_ => Ok(Value::bool(false, self.types)),
},
- ValueVariant::Bool(l) => match rhs.variant {
- ValueVariant::Bool(r) => Ok(Value::bool(l == r, self.types)),
+ ValueKind::Bool(l) => match rhs.kind {
+ ValueKind::Bool(r) => Ok(Value::bool(l == r, self.types)),
_ => Ok(Value::bool(false, self.types)),
},
_ => Ok(Value::bool(false, self.types)),
@@ -112,7 +112,7 @@ impl<'t> ValueOperator<'t> {
pub fn neq(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
if let Ok(Value {
- variant: ValueVariant::Bool(value),
+ kind: ValueKind::Bool(value),
..
}) = self.eq(lhs, rhs)
{
@@ -123,15 +123,15 @@ impl<'t> ValueOperator<'t> {
}
pub fn gt(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Float(r) => match rhs.variant {
- ValueVariant::Float(l) => Ok(Value::bool(r > l, self.types)),
- ValueVariant::Int(l) => Ok(Value::bool(r > l as f64, self.types)),
+ match lhs.kind {
+ ValueKind::Float(r) => match rhs.kind {
+ ValueKind::Float(l) => Ok(Value::bool(r > l, self.types)),
+ ValueKind::Int(l) => Ok(Value::bool(r > l as f64, self.types)),
_ => Err(OperationError::CompareTypes(lhs, rhs)),
},
- ValueVariant::Int(r) => match rhs.variant {
- ValueVariant::Float(l) => Ok(Value::bool(r as f64 > l, self.types)),
- ValueVariant::Int(l) => Ok(Value::bool(r > l, self.types)),
+ ValueKind::Int(r) => match rhs.kind {
+ ValueKind::Float(l) => Ok(Value::bool(r as f64 > l, self.types)),
+ ValueKind::Int(l) => Ok(Value::bool(r > l, self.types)),
_ => Err(OperationError::CompareTypes(lhs, rhs)),
},
_ => Err(OperationError::CompareTypes(lhs, rhs)),
@@ -139,15 +139,15 @@ impl<'t> ValueOperator<'t> {
}
pub fn gte(&self, lhs: Value, rhs: Value) -> Result<Value, OperationError> {
- match lhs.variant {
- ValueVariant::Float(r) => match rhs.variant {
- ValueVariant::Float(l) => Ok(Value::bool(r >= l, self.types)),
- ValueVariant::Int(l) => Ok(Value::bool(r >= l as f64, self.types)),
+ match lhs.kind {
+ ValueKind::Float(r) => match rhs.kind {
+ ValueKind::Float(l) => Ok(Value::bool(r >= l, self.types)),
+ ValueKind::Int(l) => Ok(Value::bool(r >= l as f64, self.types)),
_ => Err(OperationError::CompareTypes(lhs, rhs)),
},
- ValueVariant::Int(r) => match rhs.variant {
- ValueVariant::Float(l) => Ok(Value::bool(r as f64 >= l, self.types)),
- ValueVariant::Int(l) => Ok(Value::bool(r >= l, self.types)),
+ ValueKind::Int(r) => match rhs.kind {
+ ValueKind::Float(l) => Ok(Value::bool(r as f64 >= l, self.types)),
+ ValueKind::Int(l) => Ok(Value::bool(r >= l, self.types)),
_ => Err(OperationError::CompareTypes(lhs, rhs)),
},
_ => Err(OperationError::CompareTypes(lhs, rhs)),
@@ -155,28 +155,28 @@ impl<'t> ValueOperator<'t> {
}
pub fn neg(&self, val: Value) -> Result<Value, OperationError> {
- match val.variant {
- ValueVariant::Float(float) => Ok(Value::float(-float, self.types)),
- ValueVariant::Int(int) => Ok(Value::int(-int, self.types)),
+ match val.kind {
+ ValueKind::Float(float) => Ok(Value::float(-float, self.types)),
+ ValueKind::Int(int) => Ok(Value::int(-int, self.types)),
_ => Err(OperationError::NegType(val)),
}
}
pub fn not(&self, val: Value) -> Result<Value, OperationError> {
- match val.variant {
- ValueVariant::Bool(bool) => Ok(Value::bool(bool, self.types)),
+ match val.kind {
+ ValueKind::Bool(bool) => Ok(Value::bool(bool, self.types)),
_ => Err(OperationError::NotType(val)),
}
}
pub fn subscript(&self, val: Value, index: Value) -> Result<Value, OperationError> {
- let index = match index.variant {
- ValueVariant::Int(i) => i,
+ let index = match index.kind {
+ ValueKind::Int(i) => i,
_ => return Err(OperationError::ArrayIndexType(index)),
};
- match val.variant {
- ValueVariant::Array(a) => {
+ match val.kind {
+ ValueKind::Array(a) => {
let array = a.borrow();
if index < 0 || index as usize >= array.len() {
Err(OperationError::ArrayIndexOutOfRange {
@@ -198,13 +198,13 @@ impl<'t> ValueOperator<'t> {
index: Value,
value: Value,
) -> Result<Value, OperationError> {
- let index = match index.variant {
- ValueVariant::Int(i) => i,
+ let index = match index.kind {
+ ValueKind::Int(i) => i,
_ => return Err(OperationError::ArrayIndexType(index)),
};
- match &val.variant {
- ValueVariant::Array(a) => {
+ match &val.kind {
+ ValueKind::Array(a) => {
let mut array = a.borrow_mut();
if index < 0 || index as usize >= array.len() {
Err(OperationError::ArrayIndexOutOfRange {
@@ -221,8 +221,8 @@ impl<'t> ValueOperator<'t> {
}
pub fn call(&self, val: &Value, arguments: Vec<Value>) -> Result<Value, WalkerError> {
- let called = match &val.variant {
- ValueVariant::Fn(i) => i,
+ let called = match &val.kind {
+ ValueKind::Fn(i) => i,
_ => {
return Err(WalkerError::OperationError(OperationError::CallableType(
val.clone(),
diff --git a/src/interpret/scope.rs b/src/interpret/scope.rs
index 3813cab..8a5db83 100644
--- a/src/interpret/scope.rs
+++ b/src/interpret/scope.rs
@@ -1,7 +1,7 @@
-use super::value::{Value, ValueVariant};
+use super::value::{Value, ValueKind};
use crate::{
parse::ast::nodes::Identifier,
- types::{bag::TypeBag, TypeVariant},
+ types::{bag::TypeBag, TypeKind},
};
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use thiserror::Error;
@@ -117,23 +117,23 @@ impl AssignedValue {
fn get_value(&self, types: &TypeBag) -> Value {
match self {
Self::Mutable(value) => value.clone(),
- Self::Constant(value) => match &value.variant {
- ValueVariant::Array(reference) => {
+ Self::Constant(value) => match &value.kind {
+ ValueKind::Array(reference) => {
let underlying_value = reference.borrow().clone();
Value {
- variant: ValueVariant::Array(Rc::new(RefCell::new(underlying_value))),
+ kind: ValueKind::Array(Rc::new(RefCell::new(underlying_value))),
// FIXME: Give arrays actual type instead of void.
- typ: types.create_type(TypeVariant::Array(types.void())),
+ typ: types.create_type(TypeKind::Array(types.void())),
}
}
- ValueVariant::Fn(reference) => {
+ ValueKind::Fn(reference) => {
let underlying_value = reference.borrow().clone();
Value {
- variant: ValueVariant::Fn(Rc::new(RefCell::new(underlying_value))),
+ kind: ValueKind::Fn(Rc::new(RefCell::new(underlying_value))),
// FIXME: Give functions actual types.
- typ: types.create_type(TypeVariant::Fn {
+ typ: types.create_type(TypeKind::Fn {
parameters: HashMap::new(),
returns: types.void(),
}),
diff --git a/src/interpret/value.rs b/src/interpret/value.rs
index 45c177d..6d46a0d 100644
--- a/src/interpret/value.rs
+++ b/src/interpret/value.rs
@@ -10,12 +10,12 @@ type ReferenceOnCopy<T> = Rc<RefCell<T>>;
#[derive(Clone, Debug)]
pub struct Value {
- pub variant: ValueVariant,
+ pub kind: ValueKind,
pub typ: Type,
}
#[derive(Clone, Debug)]
-pub enum ValueVariant {
+pub enum ValueKind {
Str(String),
Float(f64),
Int(i64),
@@ -29,49 +29,49 @@ pub enum ValueVariant {
impl Value {
pub fn str(val: String, types: &TypeBag) -> Self {
Self {
- variant: ValueVariant::Str(val),
+ kind: ValueKind::Str(val),
typ: types.str(),
}
}
pub fn int(val: i64, types: &TypeBag) -> Self {
Self {
- variant: ValueVariant::Int(val),
+ kind: ValueKind::Int(val),
typ: types.int(),
}
}
pub fn float(val: f64, types: &TypeBag) -> Self {
Self {
- variant: ValueVariant::Float(val),
+ kind: ValueKind::Float(val),
typ: types.float(),
}
}
pub fn bool(val: bool, types: &TypeBag) -> Self {
Self {
- variant: ValueVariant::Bool(val),
+ kind: ValueKind::Bool(val),
typ: types.bool(),
}
}
pub fn void(types: &TypeBag) -> Self {
Self {
- variant: ValueVariant::Void,
+ kind: ValueKind::Void,
typ: types.void(),
}
}
pub fn type_name(&self) -> &'static str {
// TODO: Base this off of the type.
- match self.variant {
- ValueVariant::Str(_) => "Str",
- ValueVariant::Float(_) => "Float",
- ValueVariant::Int(_) => "Int",
- ValueVariant::Bool(_) => "Bool",
- ValueVariant::Array(_) => "Array",
- ValueVariant::Fn(_) => "Fn",
- ValueVariant::Void => "Void",
+ match self.kind {
+ ValueKind::Str(_) => "Str",
+ ValueKind::Float(_) => "Float",
+ ValueKind::Int(_) => "Int",
+ ValueKind::Bool(_) => "Bool",
+ ValueKind::Array(_) => "Array",
+ ValueKind::Fn(_) => "Fn",
+ ValueKind::Void => "Void",
_ => todo!(),
}
}
@@ -79,12 +79,12 @@ impl Value {
impl Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match &self.variant {
- ValueVariant::Str(v) => write!(f, "{}", v),
- ValueVariant::Float(v) => write!(f, "{}", v),
- ValueVariant::Int(v) => write!(f, "{}", v),
- ValueVariant::Bool(v) => write!(f, "{}", v),
- ValueVariant::Array(a) => {
+ match &self.kind {
+ ValueKind::Str(v) => write!(f, "{}", v),
+ ValueKind::Float(v) => write!(f, "{}", v),
+ ValueKind::Int(v) => write!(f, "{}", v),
+ ValueKind::Bool(v) => write!(f, "{}", v),
+ ValueKind::Array(a) => {
write!(
f,
"[{}]",
@@ -95,8 +95,8 @@ impl Display for Value {
.join(", ")
)
}
- ValueVariant::Fn(v) => write!(f, "<fn {:?}>", v.as_ptr()),
- ValueVariant::Void => write!(f, "<void>"),
+ ValueKind::Fn(v) => write!(f, "<fn {:?}>", v.as_ptr()),
+ ValueKind::Void => write!(f, "<void>"),
_ => todo!(),
}
}
diff --git a/src/interpret/walker.rs b/src/interpret/walker.rs
index 171f4d1..01bde11 100644
--- a/src/interpret/walker.rs
+++ b/src/interpret/walker.rs
@@ -3,7 +3,7 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc};
use crate::{
interpret::{
operator::ValueOperator,
- value::{FnValue, ValueVariant},
+ value::{FnValue, ValueKind},
},
parse::ast::{
expression::Expression,
@@ -13,7 +13,7 @@ use crate::{
statement::Statement,
Program,
},
- types::{bag::TypeBag, TypeVariant},
+ types::{bag::TypeBag, TypeKind},
};
use thiserror::Error;
@@ -93,8 +93,8 @@ impl Walker {
// Short-circuting operators
if let BinOp::And | BinOp::Or = op {
let left_value = self.walk_expression(left)?;
- let is_left_true = match left_value.variant {
- ValueVariant::Bool(bool) => bool,
+ let is_left_true = match left_value.kind {
+ ValueKind::Bool(bool) => bool,
_ => return Err(WalkerError::WrongAndOrType),
};
@@ -170,11 +170,9 @@ impl Walker {
elements.push(self.walk_expression(expression)?);
}
Ok(Value {
- variant: ValueVariant::Array(Rc::new(RefCell::new(elements))),
+ kind: ValueKind::Array(Rc::new(RefCell::new(elements))),
// FIXME: Use actual type.
- typ: self
- .types
- .create_type(TypeVariant::Array(self.types.void())),
+ typ: self.types.create_type(TypeKind::Array(self.types.void())),
})
}
Expression::SimpleLiteral(token) => {
@@ -202,20 +200,20 @@ impl Walker {
Ok(Value::str(buffer, &self.types))
}
Expression::FnLiteral(node) => Ok(Value {
- variant: ValueVariant::Fn(Rc::new(RefCell::new(FnValue {
+ kind: ValueKind::Fn(Rc::new(RefCell::new(FnValue {
node: node.as_ref().clone(),
scope: self.scope.clone(),
}))),
// FIXME: Use actual type here.
- typ: self.types.create_type(TypeVariant::Fn {
+ typ: self.types.create_type(TypeKind::Fn {
parameters: HashMap::new(),
returns: self.types.void(),
}),
}),
Expression::If(if_node) => {
for conditional in &if_node.conditionals {
- if let ValueVariant::Bool(bool) =
- self.walk_expression(&conditional.condition)?.variant
+ if let ValueKind::Bool(bool) =
+ self.walk_expression(&conditional.condition)?.kind
{
if bool {
return self.walk_block(&conditional.block);
@@ -234,8 +232,8 @@ impl Walker {
Expression::Loop(loop_node) => {
if let Some(condition) = &loop_node.condition {
loop {
- if let ValueVariant::Bool(should_repeat) =
- self.walk_expression(condition)?.variant
+ if let ValueKind::Bool(should_repeat) =
+ self.walk_expression(condition)?.kind
{
if should_repeat {
match self.walk_block(&loop_node.body) {