-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transpile trailing closure syntax and add std functions for if, try a…
…nd for
- Loading branch information
1 parent
97213bf
commit 2b03218
Showing
19 changed files
with
319 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use crate::{ | ||
Block, ConstructorCall, Expression, FunctionCall, Ident, MemberFieldAccess, MemberFunctionCall, | ||
TypeElement, | ||
}; | ||
use from_pest::pest::iterators::Pairs; | ||
use from_pest::ConversionError::NoMatch; | ||
use from_pest::{ConversionError, FromPest, Void}; | ||
use galvan_pest::Rule; | ||
use typeunion::type_union; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct Closure { | ||
pub arguments: Vec<ClosureArgument>, | ||
pub block: Block, | ||
} | ||
|
||
impl FromPest<'_> for Closure { | ||
type Rule = Rule; | ||
type FatalError = Void; | ||
|
||
fn from_pest( | ||
pairs: &mut Pairs<'_, Self::Rule>, | ||
) -> Result<Self, ConversionError<Self::FatalError>> { | ||
let Some(pair) = pairs.next() else { | ||
return Err(NoMatch); | ||
}; | ||
if pair.as_rule() != Rule::closure && pair.as_rule() != Rule::trailing_closure { | ||
return Err(NoMatch); | ||
} | ||
|
||
let mut pairs = pair.into_inner(); | ||
let arguments = Vec::<ClosureArgument>::from_pest(&mut pairs)?; | ||
let block = Block::from_pest(&mut pairs)?; | ||
|
||
Ok(Self { arguments, block }) | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, FromPest)] | ||
#[pest_ast(rule(Rule::closure_argument))] | ||
pub struct ClosureArgument { | ||
pub ident: Ident, | ||
pub ty: Option<TypeElement>, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct ElseExpression { | ||
pub receiver: Box<Expression>, | ||
pub block: Block, | ||
} | ||
|
||
#[type_union(super = Expression)] | ||
#[derive(Debug, PartialEq, Eq, FromPest)] | ||
#[pest_ast(rule(Rule::allowed_before_else_expression))] | ||
type AllowedBeforeElseExpression = | ||
FunctionCall + ConstructorCall + MemberFunctionCall + MemberFieldAccess + Ident; | ||
|
||
impl FromPest<'_> for ElseExpression { | ||
type Rule = Rule; | ||
type FatalError = Void; | ||
|
||
fn from_pest( | ||
pairs: &mut Pairs<'_, Self::Rule>, | ||
) -> Result<Self, ConversionError<Self::FatalError>> { | ||
let Some(pair) = pairs.next() else { | ||
return Err(NoMatch); | ||
}; | ||
if pair.as_rule() != Rule::else_expression { | ||
return Err(NoMatch); | ||
} | ||
|
||
let mut pairs = pair.into_inner(); | ||
let receiver = Box::new(AllowedBeforeElseExpression::from_pest(&mut pairs)?.into()); | ||
let block = Block::from_pest(&mut pairs)?; | ||
|
||
Ok(Self { receiver, block }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
use galvan_pest::Rule; | ||
|
||
use super::{Block, Ident, StringLiteral}; | ||
use super::{Body, Ident, StringLiteral}; | ||
|
||
#[derive(Debug, PartialEq, Eq, FromPest)] | ||
#[pest_ast(rule(Rule::main))] | ||
pub struct MainDecl { | ||
pub body: Block, | ||
pub body: Body, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, FromPest)] | ||
#[pest_ast(rule(Rule::test))] | ||
pub struct TestDecl { | ||
pub name: Option<StringLiteral>, | ||
pub body: Block, | ||
pub body: Body, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, FromPest)] | ||
#[pest_ast(rule(Rule::task))] | ||
pub struct TaskDecl { | ||
pub ident: Ident, | ||
// name: Option<String>, | ||
pub body: Block, | ||
pub body: Body, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.