-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use hashbrown::HashMap; | ||
use werbolg_core::{ConstrId, ValueFun}; | ||
use werbolg_exec::{ExecutionError, Valuable, ValueKind}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum Value { | ||
Unit, | ||
// Simple values | ||
Bool(bool), | ||
Integral(u64), | ||
Binary(Box<[u8]>), | ||
Check warning on line 11 in src/value.rs
|
||
HashMap(HashMap<u32, u64>), | ||
// Composite | ||
List(Box<[Value]>), | ||
Struct(ConstrId, Box<[Value]>), | ||
Enum(u32, Box<[Value]>), | ||
// Functions | ||
Fun(ValueFun), | ||
} | ||
|
||
impl Value { | ||
fn desc(&self) -> ValueKind { | ||
match self { | ||
Value::Unit => b" unit", | ||
Value::Bool(_) => b" bool", | ||
Value::HashMap(_) => b" hashmap", | ||
Value::Integral(_) => b" int", | ||
Value::Binary(_) => b" binary", | ||
Value::List(_) => b" list", | ||
Value::Struct(_, _) => b" struct", | ||
Value::Enum(_, _) => b" enum", | ||
Value::Fun(_) => b" fun", | ||
} | ||
} | ||
} | ||
|
||
impl Valuable for Value { | ||
fn descriptor(&self) -> werbolg_exec::ValueKind { | ||
self.desc() | ||
} | ||
|
||
fn conditional(&self) -> Option<bool> { | ||
match self { | ||
Value::Bool(b) => Some(*b), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn fun(&self) -> Option<ValueFun> { | ||
match self { | ||
Self::Fun(valuefun) => Some(*valuefun), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn structure(&self) -> Option<(ConstrId, &[Self])> { | ||
todo!() | ||
} | ||
|
||
fn index(&self, index: usize) -> Option<&Self> { | ||
Check warning on line 60 in src/value.rs
|
||
todo!() | ||
} | ||
|
||
fn make_fun(fun: ValueFun) -> Self { | ||
Value::Fun(fun) | ||
} | ||
|
||
fn make_dummy() -> Self { | ||
Value::Unit | ||
} | ||
} | ||
|
||
impl Value { | ||
pub fn int(&self) -> Result<u64, ExecutionError> { | ||
match self { | ||
Value::Integral(o) => Ok(*o), | ||
_ => Err(ExecutionError::ValueKindUnexpected { | ||
value_expected: Value::Integral(0).descriptor(), | ||
value_got: self.descriptor(), | ||
}), | ||
} | ||
} | ||
} |