Add output function
This commit is contained in:
parent
5f958c72b8
commit
b9190514c4
@ -6,6 +6,7 @@ use std::{
|
|||||||
use crate::{
|
use crate::{
|
||||||
abstract_tree::{Identifier, Type},
|
abstract_tree::{Identifier, Type},
|
||||||
error::RwLockPoisonError,
|
error::RwLockPoisonError,
|
||||||
|
value::BuiltInFunction,
|
||||||
Value,
|
Value,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -36,23 +37,42 @@ impl Context {
|
|||||||
&self,
|
&self,
|
||||||
identifier: &Identifier,
|
identifier: &Identifier,
|
||||||
) -> Result<Option<ValueData>, RwLockPoisonError> {
|
) -> Result<Option<ValueData>, RwLockPoisonError> {
|
||||||
Ok(self.inner.read()?.get(identifier).cloned())
|
if let Some(value_data) = self.inner.read()?.get(identifier) {
|
||||||
|
return Ok(Some(value_data.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let value_data = match identifier.as_str() {
|
||||||
|
"output" => ValueData::Value(BuiltInFunction::output()),
|
||||||
|
_ => return Ok(None),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Some(value_data))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_type(&self, identifier: &Identifier) -> Result<Option<Type>, RwLockPoisonError> {
|
pub fn get_type(&self, identifier: &Identifier) -> Result<Option<Type>, RwLockPoisonError> {
|
||||||
if let Some(ValueData::Type(r#type)) = self.inner.read()?.get(identifier) {
|
if let Some(ValueData::Type(r#type)) = self.inner.read()?.get(identifier) {
|
||||||
Ok(Some(r#type.clone()))
|
return Ok(Some(r#type.clone()));
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let r#type = match identifier.as_str() {
|
||||||
|
"output" => BuiltInFunction::Output.r#type(),
|
||||||
|
_ => return Ok(None),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Some(r#type))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_value(&self, identifier: &Identifier) -> Result<Option<Value>, RwLockPoisonError> {
|
pub fn get_value(&self, identifier: &Identifier) -> Result<Option<Value>, RwLockPoisonError> {
|
||||||
if let Some(ValueData::Value(value)) = self.inner.read()?.get(identifier) {
|
if let Some(ValueData::Value(value)) = self.inner.read()?.get(identifier) {
|
||||||
Ok(Some(value.clone()))
|
return Ok(Some(value.clone()));
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let value = match identifier.as_str() {
|
||||||
|
"output" => BuiltInFunction::output(),
|
||||||
|
_ => return Ok(None),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Some(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_type(&self, identifier: Identifier, r#type: Type) -> Result<(), RwLockPoisonError> {
|
pub fn set_type(&self, identifier: Identifier, r#type: Type) -> Result<(), RwLockPoisonError> {
|
||||||
|
@ -56,7 +56,7 @@ impl Error {
|
|||||||
Error::Runtime(_) => todo!(),
|
Error::Runtime(_) => todo!(),
|
||||||
Error::Validation { error, span } => {
|
Error::Validation { error, span } => {
|
||||||
let mut report = Report::build(
|
let mut report = Report::build(
|
||||||
ReportKind::Custom("Lexing Error", Color::White),
|
ReportKind::Custom("Validation Error", Color::White),
|
||||||
(),
|
(),
|
||||||
span.start,
|
span.start,
|
||||||
);
|
);
|
||||||
|
24
src/value.rs
24
src/value.rs
@ -87,6 +87,10 @@ impl Value {
|
|||||||
))))
|
))))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn built_in_function(function: BuiltInFunction) -> Self {
|
||||||
|
Value(Arc::new(ValueInner::Function(Function::BuiltIn(function))))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn r#type(&self) -> Type {
|
pub fn r#type(&self) -> Type {
|
||||||
match self.0.as_ref() {
|
match self.0.as_ref() {
|
||||||
ValueInner::Boolean(_) => Type::Boolean,
|
ValueInner::Boolean(_) => Type::Boolean,
|
||||||
@ -343,22 +347,30 @@ pub struct ParsedFunction {
|
|||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||||
pub enum BuiltInFunction {
|
pub enum BuiltInFunction {
|
||||||
Output(Value),
|
Output,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BuiltInFunction {
|
impl BuiltInFunction {
|
||||||
fn r#type(&self, context: &Context) -> Type {
|
pub fn output() -> Value {
|
||||||
|
static OUTPUT: OnceLock<Value> = OnceLock::new();
|
||||||
|
|
||||||
|
OUTPUT
|
||||||
|
.get_or_init(|| Value::built_in_function(BuiltInFunction::Output))
|
||||||
|
.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn r#type(&self) -> Type {
|
||||||
match self {
|
match self {
|
||||||
BuiltInFunction::Output(_) => Type::Function {
|
BuiltInFunction::Output => Type::Function {
|
||||||
parameter_types: vec![Type::Any],
|
parameter_types: vec![Type::Any],
|
||||||
return_type: Box::new(Type::None),
|
return_type: Box::new(Type::None),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(self, context: &Context) -> Result<Action, RuntimeError> {
|
pub fn call(self, value: Value, _context: &Context) -> Result<Action, RuntimeError> {
|
||||||
match self {
|
match self {
|
||||||
BuiltInFunction::Output(value) => {
|
BuiltInFunction::Output => {
|
||||||
println!("{value}");
|
println!("{value}");
|
||||||
|
|
||||||
Ok(Action::None)
|
Ok(Action::None)
|
||||||
@ -370,7 +382,7 @@ impl BuiltInFunction {
|
|||||||
impl Display for BuiltInFunction {
|
impl Display for BuiltInFunction {
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
BuiltInFunction::Output(_) => write!(f, "(to_output : any) : none rust_magic();"),
|
BuiltInFunction::Output => write!(f, "(to_output : any) : none rust_magic();"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user