Skip to content

Commit

Permalink
impl Display for ast::TypeSource
Browse files Browse the repository at this point in the history
  • Loading branch information
poppingmoon committed Jul 10, 2024
1 parent 22451a6 commit 099adf0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub enum AiScriptSyntaxError {
Attribute,
#[error(r#"Reserved word "{0}" cannot be used as variable name."#)]
ReservedWord(String),
#[error("Unknown type: '{0}'")]
UnknownType(String),
}

#[derive(Error, Debug, PartialEq, Clone)]
Expand Down
54 changes: 38 additions & 16 deletions src/type.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
use crate::{error::AiScriptError, node as ast};
use std::fmt::Display;

pub struct TSimple {
pub name: String,
use crate::{error::AiScriptSyntaxError, node as ast};

pub enum Type {
Simple(TSimple),
Generic(TGeneric),
Fn(TFn),
}

pub struct TGeneric {
pub name: String,
pub inners: Vec<Type>,
impl Display for ast::NamedTypeSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let ast::NamedTypeSource { name, inner, .. } = self;
write!(f, "{name}")?;
if let Some(inner) = inner {
write!(f, "<{inner}>")?;
}
Ok(())
}
}

pub struct TFn {
pub args: Vec<Type>,
pub result: Box<Type>,
impl Display for ast::FnTypeSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let ast::FnTypeSource { args, result, .. } = self;
write!(
f,
"@({}) {{ {result} }}",
args.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join(", ")
)
}
}

pub enum Type {
Simple(TSimple),
Generic(TGeneric),
Fn(TFn),
impl Display for ast::TypeSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ast::TypeSource::NamedTypeSource(type_source) => type_source.fmt(f),
ast::TypeSource::FnTypeSource(type_source) => type_source.fmt(f),
}
}
}

pub fn get_type_by_source(type_source: ast::TypeSource) -> Result<Type, AiScriptError> {
pub fn get_type_by_source(type_source: ast::TypeSource) -> Result<Type, AiScriptSyntaxError> {
match type_source {
ast::TypeSource::NamedTypeSource(type_source) => match type_source.name.as_str() {
"null" | "bool" | "num" | "str" | "any" | "void" => Ok(Type::Simple(TSimple {
Expand All @@ -37,14 +59,14 @@ pub fn get_type_by_source(type_source: ast::TypeSource) -> Result<Type, AiScript
|inner| get_type_by_source(*inner),
)?],
})),
_ => todo!(),
_ => Err(AiScriptSyntaxError::UnknownType(type_source.to_string())),
},
ast::TypeSource::FnTypeSource(type_source) => Ok(Type::Fn(TFn {
args: type_source
.args
.into_iter()
.map(get_type_by_source)
.collect::<Result<Vec<Type>, AiScriptError>>()?,
.collect::<Result<Vec<Type>, AiScriptSyntaxError>>()?,
result: get_type_by_source(*type_source.result)?.into(),
})),
}
Expand Down

0 comments on commit 099adf0

Please sign in to comment.