pest for loop

This commit is contained in:
collin 2020-04-13 13:08:39 -07:00
parent 3f59714eac
commit 940806ec50
4 changed files with 75 additions and 35 deletions

View File

@ -1,6 +1,4 @@
struct Bar { for field i in 0..4 do
field a a = 1 + 1
} endfor
def foo() -> (field):
return 1
return 1 return 1

View File

@ -320,12 +320,20 @@ impl<'ast> From<ast::ReturnStatement<'ast>> for types::Statement {
} }
} }
impl<'ast> From<ast::IterationStatement<'ast>> for types::Statement {
fn from(statement: ast::IterationStatement<'ast>) -> Self {
println!("{:#?}", statement);
unimplemented!()
}
}
impl<'ast> From<ast::Statement<'ast>> for types::Statement { impl<'ast> From<ast::Statement<'ast>> for types::Statement {
fn from(statement: ast::Statement<'ast>) -> Self { fn from(statement: ast::Statement<'ast>) -> Self {
match statement { match statement {
ast::Statement::Assign(statement) => types::Statement::from(statement), ast::Statement::Assign(statement) => types::Statement::from(statement),
ast::Statement::Definition(statement) => types::Statement::from(statement), ast::Statement::Definition(statement) => types::Statement::from(statement),
ast::Statement::Return(statement) => types::Statement::from(statement), ast::Statement::Return(statement) => types::Statement::from(statement),
ast::Statement::Iteration(statement) => types::Statement::from(statement),
} }
} }
} }

View File

@ -223,6 +223,16 @@ pub enum Type<'ast> {
Struct(StructType<'ast>), Struct(StructType<'ast>),
} }
impl<'ast> fmt::Display for Type<'ast> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match *self {
Type::Basic(ref _ty) => write!(f, "basic"),
Type::Array(ref _ty) => write!(f, "array"),
Type::Struct(ref _ty) => write!(f, "struct"),
}
}
}
// Visibility // Visibility
#[derive(Clone, Debug, FromPest, PartialEq)] #[derive(Clone, Debug, FromPest, PartialEq)]
@ -685,10 +695,26 @@ pub struct AssignStatement<'ast> {
pub span: Span<'ast>, pub span: Span<'ast>,
} }
impl<'ast> fmt::Display for AssignStatement<'ast> { #[derive(Clone, Debug, FromPest, PartialEq)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { #[pest_ast(rule(Rule::statement_definition))]
write!(f, "{}", self.expression) pub struct DefinitionStatement<'ast> {
} pub ty: Type<'ast>,
pub variable: Variable<'ast>,
pub expression: Expression<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::statement_iteration))]
pub struct IterationStatement<'ast> {
pub ty: Type<'ast>,
pub index: Variable<'ast>,
pub from: Expression<'ast>,
pub to: Expression<'ast>,
pub statements: Vec<Statement<'ast>>,
#[pest_ast(outer())]
pub span: Span<'ast>,
} }
#[derive(Debug, FromPest, PartialEq, Clone)] #[derive(Debug, FromPest, PartialEq, Clone)]
@ -699,6 +725,37 @@ pub struct ReturnStatement<'ast> {
pub span: Span<'ast>, pub span: Span<'ast>,
} }
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::statement))]
pub enum Statement<'ast> {
Assign(AssignStatement<'ast>),
Definition(DefinitionStatement<'ast>),
Iteration(IterationStatement<'ast>),
Return(ReturnStatement<'ast>),
}
impl<'ast> fmt::Display for AssignStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} = {}", self.variable, self.expression)
}
}
impl<'ast> fmt::Display for DefinitionStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {} = {}", self.ty, self.variable, self.expression)
}
}
impl<'ast> fmt::Display for IterationStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"for {} {} in {}..{} do {:#?} endfor",
self.ty, self.index, self.from, self.to, self.statements
)
}
}
impl<'ast> fmt::Display for ReturnStatement<'ast> { impl<'ast> fmt::Display for ReturnStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, expression) in self.expressions.iter().enumerate() { for (i, expression) in self.expressions.iter().enumerate() {
@ -711,35 +768,12 @@ impl<'ast> fmt::Display for ReturnStatement<'ast> {
} }
} }
#[derive(Debug, FromPest, PartialEq, Clone)]
#[pest_ast(rule(Rule::statement_definition))]
pub struct DefinitionStatement<'ast> {
pub ty: Type<'ast>,
pub variable: Variable<'ast>,
pub expression: Expression<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::statement))]
pub enum Statement<'ast> {
Assign(AssignStatement<'ast>),
Definition(DefinitionStatement<'ast>),
Return(ReturnStatement<'ast>),
}
impl<'ast> fmt::Display for DefinitionStatement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.expression)
}
}
impl<'ast> fmt::Display for Statement<'ast> { impl<'ast> fmt::Display for Statement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
Statement::Assign(ref statement) => write!(f, "{}", statement), Statement::Assign(ref statement) => write!(f, "{}", statement),
Statement::Definition(ref statement) => write!(f, "{}", statement), Statement::Definition(ref statement) => write!(f, "{}", statement),
Statement::Iteration(ref statement) => write!(f, "{}", statement),
Statement::Return(ref statement) => write!(f, "{}", statement), Statement::Return(ref statement) => write!(f, "{}", statement),
} }
} }

View File

@ -135,7 +135,7 @@ expression_tuple = _{ (expression ~ ("," ~ expression)*)? }
/// Statements /// Statements
statement_expression = { expression } // statement_expression = { expression }
statement_assign = { variable ~ "=" ~ expression } statement_assign = { variable ~ "=" ~ expression }
statement_definition = { ty ~ variable ~ "=" ~ expression } statement_definition = { ty ~ variable ~ "=" ~ expression }
statement_return = { "return" ~ expression_tuple } statement_return = { "return" ~ expression_tuple }
@ -146,7 +146,7 @@ statement = {
| (statement_iteration | (statement_iteration
| statement_assign | statement_assign
| statement_definition | statement_definition
| statement_expression // | statement_expression
) ~ NEWLINE ) ~ NEWLINE
) ~ NEWLINE* ) ~ NEWLINE*
} }