Working expr statements
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
void main() {
|
void main() {
|
||||||
|
i32 x;
|
||||||
|
|
||||||
println!("hello, world");
|
println!("hello, world");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ pub enum BinaryOp {
|
|||||||
pub enum Expression {
|
pub enum Expression {
|
||||||
Literal(Literal),
|
Literal(Literal),
|
||||||
Path(ExprPath),
|
Path(ExprPath),
|
||||||
|
Statement(Box<Statement>),
|
||||||
Fix {
|
Fix {
|
||||||
initial: Box<Expression>,
|
initial: Box<Expression>,
|
||||||
prefixes: Vec<Prefix>,
|
prefixes: Vec<Prefix>,
|
||||||
|
|||||||
@@ -3,13 +3,11 @@ use serde::Serialize;
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Default)]
|
#[derive(Debug, Clone, Serialize, Default)]
|
||||||
pub struct Block(pub Vec<Spanned<Statement>>);
|
pub struct Block(pub Vec<Spanned<Expression>>);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub enum Statement {
|
pub enum Statement {
|
||||||
Expression(Expression),
|
|
||||||
Block(Block),
|
Block(Block),
|
||||||
|
|
||||||
VarDecl(VarDeclStmt),
|
VarDecl(VarDeclStmt),
|
||||||
Assign {
|
Assign {
|
||||||
target: Expression,
|
target: Expression,
|
||||||
|
|||||||
@@ -342,8 +342,8 @@ block = {
|
|||||||
"{" ~ (statement | unexpected_statement)* ~ "}"
|
"{" ~ (statement | unexpected_statement)* ~ "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
statement = {
|
statement = _{
|
||||||
(expr | basic_stmt_left_recurse) ~ semicolon
|
((expr | basic_stmt_left_recurse) ~ semicolon)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------
|
||||||
|
|||||||
@@ -117,6 +117,12 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
|
|||||||
Rule::static_path => ast_expr!(Expression::Path(pair.try_into())),
|
Rule::static_path => ast_expr!(Expression::Path(pair.try_into())),
|
||||||
Rule::literal => ast_expr!(Expression::Literal(pair.try_into())),
|
Rule::literal => ast_expr!(Expression::Literal(pair.try_into())),
|
||||||
Rule::expr_path => ast_expr!(Expression::Path(pair.try_into())),
|
Rule::expr_path => ast_expr!(Expression::Path(pair.try_into())),
|
||||||
|
Rule::statement => ast_expr!(Expression::Statement(
|
||||||
|
pair.try_into().get_map(Box::new).map(Box::new)
|
||||||
|
)),
|
||||||
|
Rule::basic_stmt => ast_expr!(Expression::Statement(
|
||||||
|
pair.try_into().get_map(Box::new).map(Box::new)
|
||||||
|
)),
|
||||||
|
|
||||||
_ => AstError::bug_unimplemented(pair),
|
_ => AstError::bug_unimplemented(pair),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
|
|||||||
let mut inner = pair.clone().into_inner();
|
let mut inner = pair.clone().into_inner();
|
||||||
|
|
||||||
match rule {
|
match rule {
|
||||||
Rule::statement => Statement::try_from(inner.next().unwrap()),
|
Rule::statement | Rule::basic_stmt => Statement::try_from(inner.next().unwrap()),
|
||||||
|
|
||||||
Rule::block => ast_expr!(Statement::Block(pair.try_into())),
|
Rule::block => ast_expr!(Statement::Block(pair.try_into())),
|
||||||
|
|
||||||
|
|||||||
+7
-5
@@ -169,6 +169,12 @@ impl GetRust for Expression {
|
|||||||
match self {
|
match self {
|
||||||
Expression::Path(path) => path.get_rust(),
|
Expression::Path(path) => path.get_rust(),
|
||||||
Expression::Literal(literal) => literal.get_rust(),
|
Expression::Literal(literal) => literal.get_rust(),
|
||||||
|
Expression::Statement(stmt) => {
|
||||||
|
let mut cg = RustCodegen::new();
|
||||||
|
cg.indent += 1;
|
||||||
|
stmt.to_rust(&mut cg);
|
||||||
|
cg.output
|
||||||
|
}
|
||||||
Expression::Fix {
|
Expression::Fix {
|
||||||
initial,
|
initial,
|
||||||
prefixes,
|
prefixes,
|
||||||
@@ -336,7 +342,7 @@ impl<T: ToRust> ToRust for Spanned<T> {
|
|||||||
impl ToRust for Block {
|
impl ToRust for Block {
|
||||||
fn to_rust(self, cg: &mut RustCodegen) {
|
fn to_rust(self, cg: &mut RustCodegen) {
|
||||||
for stmt in self.0 {
|
for stmt in self.0 {
|
||||||
stmt.to_rust(cg);
|
cg.add_indentedln(&(stmt.get_rust() + ";"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -608,10 +614,6 @@ impl ToRust for TopLevelKind {
|
|||||||
impl ToRust for Statement {
|
impl ToRust for Statement {
|
||||||
fn to_rust(self, cg: &mut RustCodegen) {
|
fn to_rust(self, cg: &mut RustCodegen) {
|
||||||
match self {
|
match self {
|
||||||
Statement::Expression(expr) => {
|
|
||||||
cg.add_indentedln(&format!("{};", expr.get_rust()));
|
|
||||||
}
|
|
||||||
|
|
||||||
Statement::Block(block) => {
|
Statement::Block(block) => {
|
||||||
cg.add_indentedln("{");
|
cg.add_indentedln("{");
|
||||||
cg.indent += 1;
|
cg.indent += 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user