2024-02-25 18:49:26 +00:00
|
|
|
use std::{
|
2024-02-25 19:26:22 +00:00
|
|
|
cmp::Ordering,
|
2024-02-28 23:36:47 +00:00
|
|
|
collections::BTreeMap,
|
2024-03-06 20:36:58 +00:00
|
|
|
fmt::{self, Display, Formatter},
|
2024-03-17 21:39:39 +00:00
|
|
|
io::stdin,
|
2024-02-25 18:49:26 +00:00
|
|
|
ops::Range,
|
|
|
|
sync::{Arc, OnceLock},
|
|
|
|
};
|
|
|
|
|
2024-03-08 03:20:59 +00:00
|
|
|
use stanza::{
|
|
|
|
renderer::{console::Console, Renderer},
|
|
|
|
style::{HAlign, MinWidth, Styles},
|
|
|
|
table::Table,
|
|
|
|
};
|
|
|
|
|
2024-03-06 22:32:31 +00:00
|
|
|
use crate::{
|
2024-03-17 11:31:45 +00:00
|
|
|
abstract_tree::{AbstractTree, Action, Block, Identifier, Type, WithPosition},
|
2024-03-09 03:34:17 +00:00
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
2024-03-06 22:32:31 +00:00
|
|
|
};
|
2024-02-25 18:49:26 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub struct Value(Arc<ValueInner>);
|
|
|
|
|
|
|
|
impl Value {
|
|
|
|
pub fn inner(&self) -> &Arc<ValueInner> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn boolean(boolean: bool) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Boolean(boolean)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn float(float: f64) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Float(float)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn integer(integer: i64) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Integer(integer)))
|
|
|
|
}
|
|
|
|
|
2024-02-26 21:27:01 +00:00
|
|
|
pub fn list(list: Vec<Value>) -> Self {
|
2024-02-25 18:49:26 +00:00
|
|
|
Value(Arc::new(ValueInner::List(list)))
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:36:47 +00:00
|
|
|
pub fn map(map: BTreeMap<Identifier, Value>) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Map(map)))
|
|
|
|
}
|
2024-02-25 18:49:26 +00:00
|
|
|
|
|
|
|
pub fn range(range: Range<i64>) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Range(range)))
|
|
|
|
}
|
|
|
|
|
2024-03-08 21:22:24 +00:00
|
|
|
pub fn string(string: String) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::String(string)))
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-17 05:26:05 +00:00
|
|
|
pub fn function(
|
2024-03-17 11:31:45 +00:00
|
|
|
parameters: Vec<(Identifier, WithPosition<Type>)>,
|
|
|
|
return_type: WithPosition<Type>,
|
|
|
|
body: WithPosition<Block>,
|
2024-03-17 05:26:05 +00:00
|
|
|
) -> Self {
|
2024-03-09 03:34:17 +00:00
|
|
|
Value(Arc::new(ValueInner::Function(Function::Parsed(
|
|
|
|
ParsedFunction {
|
|
|
|
parameters,
|
|
|
|
return_type,
|
|
|
|
body,
|
|
|
|
},
|
|
|
|
))))
|
2024-03-08 21:14:47 +00:00
|
|
|
}
|
|
|
|
|
2024-03-09 11:55:19 +00:00
|
|
|
pub fn built_in_function(function: BuiltInFunction) -> Self {
|
|
|
|
Value(Arc::new(ValueInner::Function(Function::BuiltIn(function))))
|
|
|
|
}
|
|
|
|
|
2024-03-06 22:32:31 +00:00
|
|
|
pub fn r#type(&self) -> Type {
|
|
|
|
match self.0.as_ref() {
|
|
|
|
ValueInner::Boolean(_) => Type::Boolean,
|
|
|
|
ValueInner::Float(_) => Type::Float,
|
|
|
|
ValueInner::Integer(_) => Type::Integer,
|
|
|
|
ValueInner::List(values) => {
|
|
|
|
let mut types = Vec::with_capacity(values.len());
|
|
|
|
|
|
|
|
for value in values {
|
|
|
|
types.push(value.r#type());
|
|
|
|
}
|
|
|
|
|
|
|
|
Type::ListExact(types)
|
|
|
|
}
|
|
|
|
ValueInner::Map(_) => Type::Map,
|
|
|
|
ValueInner::Range(_) => Type::Range,
|
|
|
|
ValueInner::String(_) => Type::String,
|
2024-03-08 21:14:47 +00:00
|
|
|
ValueInner::Function(_) => todo!(),
|
2024-03-06 22:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 20:59:52 +00:00
|
|
|
pub fn as_boolean(&self) -> Option<bool> {
|
2024-02-25 19:26:22 +00:00
|
|
|
if let ValueInner::Boolean(boolean) = self.0.as_ref() {
|
2024-03-17 20:59:52 +00:00
|
|
|
Some(*boolean)
|
|
|
|
} else {
|
|
|
|
None
|
2024-03-09 13:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-07 17:29:07 +00:00
|
|
|
pub fn as_list(&self) -> Option<&Vec<Value>> {
|
|
|
|
if let ValueInner::List(list) = self.inner().as_ref() {
|
|
|
|
Some(list)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_integer(&self) -> Option<i64> {
|
|
|
|
if let ValueInner::Integer(integer) = self.inner().as_ref() {
|
|
|
|
Some(*integer)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 18:26:55 +00:00
|
|
|
pub fn add(&self, other: &Self) -> Result<Value, ValidationError> {
|
2024-03-09 00:05:17 +00:00
|
|
|
match (self.inner().as_ref(), other.inner().as_ref()) {
|
|
|
|
(ValueInner::Integer(left), ValueInner::Integer(right)) => {
|
|
|
|
let sum = left.saturating_add(*right);
|
|
|
|
|
|
|
|
Ok(Value::integer(sum))
|
|
|
|
}
|
|
|
|
(ValueInner::Float(left), ValueInner::Float(right)) => {
|
|
|
|
let sum = left + right;
|
|
|
|
|
|
|
|
Ok(Value::float(sum))
|
|
|
|
}
|
|
|
|
(ValueInner::Float(left), ValueInner::Integer(right)) => {
|
|
|
|
let sum = left + *right as f64;
|
|
|
|
|
|
|
|
Ok(Value::float(sum))
|
|
|
|
}
|
|
|
|
(ValueInner::Integer(left), ValueInner::Float(right)) => {
|
|
|
|
let sum = *left as f64 + right;
|
|
|
|
|
|
|
|
Ok(Value::float(sum))
|
|
|
|
}
|
|
|
|
_ => Err(ValidationError::ExpectedIntegerOrFloat),
|
2024-03-08 18:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn subtract(&self, other: &Self) -> Result<Value, ValidationError> {
|
2024-03-09 00:05:17 +00:00
|
|
|
match (self.inner().as_ref(), other.inner().as_ref()) {
|
|
|
|
(ValueInner::Integer(left), ValueInner::Integer(right)) => {
|
|
|
|
let sum = left.saturating_sub(*right);
|
|
|
|
|
|
|
|
Ok(Value::integer(sum))
|
|
|
|
}
|
|
|
|
(ValueInner::Float(left), ValueInner::Float(right)) => {
|
|
|
|
let sum = left - right;
|
|
|
|
|
|
|
|
Ok(Value::float(sum))
|
|
|
|
}
|
|
|
|
(ValueInner::Float(left), ValueInner::Integer(right)) => {
|
|
|
|
let sum = left - *right as f64;
|
|
|
|
|
|
|
|
Ok(Value::float(sum))
|
|
|
|
}
|
|
|
|
(ValueInner::Integer(left), ValueInner::Float(right)) => {
|
|
|
|
let sum = *left as f64 - right;
|
|
|
|
|
|
|
|
Ok(Value::float(sum))
|
|
|
|
}
|
|
|
|
_ => Err(ValidationError::ExpectedIntegerOrFloat),
|
2024-03-08 18:26:55 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-06 20:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Value {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-03-08 03:20:59 +00:00
|
|
|
fn create_table() -> Table {
|
|
|
|
Table::with_styles(Styles::default().with(HAlign::Centred).with(MinWidth(3)))
|
|
|
|
}
|
|
|
|
|
2024-03-06 20:36:58 +00:00
|
|
|
match self.inner().as_ref() {
|
2024-03-08 21:14:47 +00:00
|
|
|
ValueInner::Boolean(boolean) => write!(f, "{boolean}"),
|
|
|
|
ValueInner::Float(float) => write!(f, "{float}"),
|
|
|
|
ValueInner::Integer(integer) => write!(f, "{integer}"),
|
|
|
|
ValueInner::List(list) => {
|
2024-03-08 03:20:59 +00:00
|
|
|
let mut table = create_table();
|
|
|
|
|
|
|
|
for value in list {
|
|
|
|
table = table.with_row([value.to_string()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{}", Console::default().render(&table))
|
|
|
|
}
|
2024-03-08 21:14:47 +00:00
|
|
|
ValueInner::Map(map) => {
|
2024-03-08 03:20:59 +00:00
|
|
|
let mut table = create_table();
|
2024-03-07 21:19:24 +00:00
|
|
|
|
|
|
|
for (identifier, value) in map {
|
2024-03-08 03:20:59 +00:00
|
|
|
table = table.with_row([identifier.as_str(), &value.to_string()]);
|
2024-03-07 21:19:24 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 03:20:59 +00:00
|
|
|
write!(f, "{}", Console::default().render(&table))
|
2024-03-07 21:19:24 +00:00
|
|
|
}
|
2024-03-08 21:14:47 +00:00
|
|
|
ValueInner::Range(_) => todo!(),
|
|
|
|
ValueInner::String(string) => write!(f, "{string}"),
|
2024-03-09 03:34:17 +00:00
|
|
|
ValueInner::Function(Function::Parsed(ParsedFunction {
|
2024-03-08 21:14:47 +00:00
|
|
|
parameters,
|
|
|
|
return_type,
|
|
|
|
body,
|
2024-03-09 03:34:17 +00:00
|
|
|
})) => {
|
2024-03-08 21:14:47 +00:00
|
|
|
write!(f, "(")?;
|
|
|
|
|
|
|
|
for (identifier, r#type) in parameters {
|
2024-03-17 10:26:12 +00:00
|
|
|
write!(f, "{identifier}: {}", r#type.node)?;
|
2024-03-08 21:14:47 +00:00
|
|
|
}
|
2024-03-08 03:20:59 +00:00
|
|
|
|
2024-03-17 05:26:05 +00:00
|
|
|
write!(f, "): {} {:?}", return_type.node, body.node)
|
2024-03-08 21:14:47 +00:00
|
|
|
}
|
2024-03-09 03:34:17 +00:00
|
|
|
ValueInner::Function(Function::BuiltIn(built_in_function)) => {
|
|
|
|
write!(f, "{built_in_function}")
|
|
|
|
}
|
2024-03-06 20:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-25 19:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Value {}
|
|
|
|
|
|
|
|
impl PartialOrd for Value {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for Value {
|
|
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
|
|
|
self.0.as_ref().cmp(other.0.as_ref())
|
|
|
|
}
|
2024-02-25 18:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum ValueInner {
|
|
|
|
Boolean(bool),
|
|
|
|
Float(f64),
|
2024-03-08 21:14:47 +00:00
|
|
|
Function(Function),
|
2024-02-25 18:49:26 +00:00
|
|
|
Integer(i64),
|
2024-02-26 21:27:01 +00:00
|
|
|
List(Vec<Value>),
|
2024-02-28 23:36:47 +00:00
|
|
|
Map(BTreeMap<Identifier, Value>),
|
2024-02-25 18:49:26 +00:00
|
|
|
Range(Range<i64>),
|
|
|
|
String(String),
|
|
|
|
}
|
|
|
|
|
2024-02-25 19:26:22 +00:00
|
|
|
impl Eq for ValueInner {}
|
|
|
|
|
|
|
|
impl PartialOrd for ValueInner {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for ValueInner {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
use ValueInner::*;
|
|
|
|
|
|
|
|
match (self, other) {
|
|
|
|
(Boolean(left), Boolean(right)) => left.cmp(right),
|
|
|
|
(Boolean(_), _) => Ordering::Greater,
|
|
|
|
(Float(left), Float(right)) => left.total_cmp(right),
|
|
|
|
(Float(_), _) => Ordering::Greater,
|
|
|
|
(Integer(left), Integer(right)) => left.cmp(right),
|
|
|
|
(Integer(_), _) => Ordering::Greater,
|
|
|
|
(List(left), List(right)) => left.cmp(right),
|
|
|
|
(List(_), _) => Ordering::Greater,
|
2024-02-28 23:36:47 +00:00
|
|
|
(Map(left), Map(right)) => left.cmp(right),
|
|
|
|
(Map(_), _) => Ordering::Greater,
|
2024-02-25 19:26:22 +00:00
|
|
|
(Range(left), Range(right)) => {
|
|
|
|
let start_cmp = left.start.cmp(&right.start);
|
|
|
|
|
|
|
|
if start_cmp.is_eq() {
|
|
|
|
left.end.cmp(&right.end)
|
|
|
|
} else {
|
|
|
|
start_cmp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Range(_), _) => Ordering::Greater,
|
|
|
|
(String(left), String(right)) => left.cmp(right),
|
|
|
|
(String(_), _) => Ordering::Greater,
|
2024-03-08 21:14:47 +00:00
|
|
|
(Function(left), Function(right)) => left.cmp(right),
|
|
|
|
(Function(_), _) => Ordering::Greater,
|
2024-02-25 19:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 21:14:47 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
2024-03-09 03:34:17 +00:00
|
|
|
pub enum Function {
|
|
|
|
Parsed(ParsedFunction),
|
|
|
|
BuiltIn(BuiltInFunction),
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:10:54 +00:00
|
|
|
impl Function {
|
2024-03-17 05:26:05 +00:00
|
|
|
pub fn call(self, arguments: Vec<Value>, context: Context) -> Result<Action, RuntimeError> {
|
2024-03-09 13:10:54 +00:00
|
|
|
let action = match self {
|
|
|
|
Function::Parsed(ParsedFunction {
|
|
|
|
parameters, body, ..
|
|
|
|
}) => {
|
|
|
|
for ((identifier, _), value) in parameters.into_iter().zip(arguments.into_iter()) {
|
|
|
|
context.set_value(identifier.clone(), value)?;
|
|
|
|
}
|
|
|
|
|
2024-03-17 05:26:05 +00:00
|
|
|
body.node.run(&context)?
|
2024-03-09 13:10:54 +00:00
|
|
|
}
|
|
|
|
Function::BuiltIn(built_in_function) => built_in_function.call(arguments, &context)?,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 03:34:17 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub struct ParsedFunction {
|
2024-03-17 11:31:45 +00:00
|
|
|
parameters: Vec<(Identifier, WithPosition<Type>)>,
|
|
|
|
return_type: WithPosition<Type>,
|
|
|
|
body: WithPosition<Block>,
|
2024-03-08 21:14:47 +00:00
|
|
|
}
|
2024-03-09 03:34:17 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub enum BuiltInFunction {
|
2024-03-09 11:55:19 +00:00
|
|
|
Output,
|
2024-03-17 21:39:39 +00:00
|
|
|
ReadLine,
|
2024-03-09 03:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BuiltInFunction {
|
2024-03-09 11:55:19 +00:00
|
|
|
pub fn output() -> Value {
|
|
|
|
static OUTPUT: OnceLock<Value> = OnceLock::new();
|
|
|
|
|
|
|
|
OUTPUT
|
|
|
|
.get_or_init(|| Value::built_in_function(BuiltInFunction::Output))
|
|
|
|
.clone()
|
|
|
|
}
|
|
|
|
|
2024-03-17 21:39:39 +00:00
|
|
|
pub fn read_line() -> Value {
|
|
|
|
static READ_LINE: OnceLock<Value> = OnceLock::new();
|
|
|
|
|
|
|
|
READ_LINE
|
|
|
|
.get_or_init(|| Value::built_in_function(BuiltInFunction::ReadLine))
|
|
|
|
.clone()
|
|
|
|
}
|
|
|
|
|
2024-03-09 11:55:19 +00:00
|
|
|
pub fn r#type(&self) -> Type {
|
2024-03-09 03:34:17 +00:00
|
|
|
match self {
|
2024-03-09 11:55:19 +00:00
|
|
|
BuiltInFunction::Output => Type::Function {
|
2024-03-09 03:34:17 +00:00
|
|
|
parameter_types: vec![Type::Any],
|
|
|
|
return_type: Box::new(Type::None),
|
|
|
|
},
|
2024-03-17 21:39:39 +00:00
|
|
|
BuiltInFunction::ReadLine => Type::Function {
|
|
|
|
parameter_types: Vec::with_capacity(0),
|
|
|
|
return_type: Box::new(Type::String),
|
|
|
|
},
|
2024-03-09 03:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 13:10:54 +00:00
|
|
|
pub fn call(&self, arguments: Vec<Value>, _context: &Context) -> Result<Action, RuntimeError> {
|
2024-03-09 03:34:17 +00:00
|
|
|
match self {
|
2024-03-09 11:55:19 +00:00
|
|
|
BuiltInFunction::Output => {
|
2024-03-09 13:10:54 +00:00
|
|
|
println!("{}", arguments[0]);
|
2024-03-09 03:34:17 +00:00
|
|
|
|
|
|
|
Ok(Action::None)
|
|
|
|
}
|
2024-03-17 21:39:39 +00:00
|
|
|
BuiltInFunction::ReadLine => {
|
|
|
|
let mut input = String::new();
|
|
|
|
|
|
|
|
stdin().read_line(&mut input)?;
|
|
|
|
|
|
|
|
Ok(Action::Return(Value::string(input)))
|
|
|
|
}
|
2024-03-09 03:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for BuiltInFunction {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2024-03-09 11:55:19 +00:00
|
|
|
BuiltInFunction::Output => write!(f, "(to_output : any) : none rust_magic();"),
|
2024-03-17 21:39:39 +00:00
|
|
|
BuiltInFunction::ReadLine => todo!(),
|
2024-03-09 03:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-17 21:39:39 +00:00
|
|
|
|
|
|
|
pub enum BuiltInValue {
|
|
|
|
Io,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BuiltInValue {
|
|
|
|
pub fn io() -> Value {
|
|
|
|
static IO: OnceLock<Value> = OnceLock::new();
|
|
|
|
|
|
|
|
let mut properties = BTreeMap::new();
|
|
|
|
|
|
|
|
properties.insert(Identifier::new("read_line"), BuiltInFunction::read_line());
|
|
|
|
|
|
|
|
IO.get_or_init(|| Value::map(properties)).clone()
|
|
|
|
}
|
|
|
|
}
|