Generating body
This commit is contained in:
@@ -112,7 +112,66 @@ pub fn class_decl(
|
||||
{
|
||||
let body = method.item.body;
|
||||
|
||||
method.item.body = Some(Block(Vec::new(), None));
|
||||
method.item.body = Some(Block {
|
||||
is_unsafe: false,
|
||||
statements: Vec::new(),
|
||||
soft_return: Some(Spanned {
|
||||
line: 1,
|
||||
column: 1,
|
||||
item: Expression::Statement(Box::new(Statement::Block(Block {
|
||||
is_unsafe: true,
|
||||
statements: vec![Spanned {
|
||||
line: 1,
|
||||
column: 1,
|
||||
item: Expression::Statement(Box::new(Statement::VarDecl(
|
||||
VarDeclStmt {
|
||||
decl: VarDecl {
|
||||
name: Pattern::Path(
|
||||
false,
|
||||
Path(vec![Identifier(String::from("func_ptr"))]),
|
||||
),
|
||||
type_: None,
|
||||
},
|
||||
init: Some(Expression::Fix {
|
||||
initial: Box::new(Expression::Path(ExprPath(vec![
|
||||
ExprPathSegment {
|
||||
ident: Identifier(String::from("self")),
|
||||
generics: None,
|
||||
},
|
||||
]))),
|
||||
prefixes: vec![Prefix::Deref],
|
||||
postfixes: vec![
|
||||
Postfix::FieldAccess(
|
||||
Identifier(String::from("_m_oop")),
|
||||
None,
|
||||
),
|
||||
Postfix::TupleFieldAccess(0, None),
|
||||
Postfix::FieldAccess(
|
||||
Identifier(String::from("add")),
|
||||
None,
|
||||
),
|
||||
Postfix::Call(vec![Expression::Path(ExprPath(vec![
|
||||
ExprPathSegment {
|
||||
ident: Identifier(String::from("Self")),
|
||||
generics: None,
|
||||
},
|
||||
ExprPathSegment {
|
||||
ident: Identifier(format!(
|
||||
"__FN_{}",
|
||||
method.item.name.0.to_uppercase()
|
||||
)),
|
||||
generics: None,
|
||||
},
|
||||
]))]),
|
||||
],
|
||||
}),
|
||||
},
|
||||
))),
|
||||
}],
|
||||
soft_return: None,
|
||||
}))),
|
||||
}),
|
||||
});
|
||||
|
||||
method.gen_rust(ctx, cg);
|
||||
method.item.body = body;
|
||||
|
||||
@@ -6,16 +6,20 @@ use crate::{GenRust, GetRust, RustCodegen};
|
||||
|
||||
impl GenRust for Block {
|
||||
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
|
||||
if self.is_unsafe {
|
||||
cg.add("unsafe ");
|
||||
}
|
||||
|
||||
cg.addln("{");
|
||||
cg.indent += 1;
|
||||
|
||||
for stmt in &self.0 {
|
||||
for stmt in &self.statements {
|
||||
ctx.expr_ensure_semicolon = true;
|
||||
stmt.gen_rust(ctx, cg);
|
||||
cg.addln("");
|
||||
}
|
||||
|
||||
if let Some(soft_return) = &self.1 {
|
||||
if let Some(soft_return) = &self.soft_return {
|
||||
ctx.expr_ensure_semicolon = false;
|
||||
soft_return.gen_rust(ctx, cg);
|
||||
cg.addln("");
|
||||
|
||||
@@ -3,10 +3,12 @@ use serde::Serialize;
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Default)]
|
||||
pub struct Block(
|
||||
pub Vec<Spanned<Expression>>,
|
||||
pub Option<Spanned<Expression>>,
|
||||
);
|
||||
pub struct Block {
|
||||
pub is_unsafe: bool,
|
||||
|
||||
pub statements: Vec<Spanned<Expression>>,
|
||||
pub soft_return: Option<Spanned<Expression>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub enum StatementBody {
|
||||
|
||||
@@ -63,6 +63,8 @@ static_path = {
|
||||
identifier ~ ("::" ~ identifier)*
|
||||
}
|
||||
|
||||
unsafe_kw = { "unsafe" }
|
||||
|
||||
// ======================================================
|
||||
// PROGRAM
|
||||
// ======================================================
|
||||
@@ -340,7 +342,7 @@ top_level = {
|
||||
// ======================================================
|
||||
|
||||
block = {
|
||||
"{" ~ statement_list ~ expr? ~ "}"
|
||||
unsafe_kw? ~ "{" ~ statement_list ~ expr? ~ "}"
|
||||
}
|
||||
|
||||
statement_list = {
|
||||
|
||||
@@ -2,7 +2,8 @@ use crate::{
|
||||
Rule,
|
||||
ast::*,
|
||||
ast_ensure, ast_expr,
|
||||
error::{AstError, IntoErr, collect_recovered},
|
||||
error::{AstError, AstResult, IntoErr, collect_recovered},
|
||||
parser::listen_rule,
|
||||
};
|
||||
|
||||
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
|
||||
@@ -12,7 +13,11 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
|
||||
let mut inner = pair.clone().into_inner();
|
||||
|
||||
ast_ensure!(pair, Rule::block => {
|
||||
ast_expr!(Block(collect_recovered(inner.next().unwrap().into_inner()), inner.next().map(Spanned::try_from).transpose()))
|
||||
ast_expr!(Block {
|
||||
is_unsafe: Ok(listen_rule(&mut inner, Rule::unsafe_kw)) as AstResult<bool>,
|
||||
statements: collect_recovered(inner.next().unwrap().into_inner()),
|
||||
soft_return: inner.next().map(Spanned::try_from).transpose(),
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user