Fixed unsafe

This commit is contained in:
2026-06-10 04:43:36 +02:00
parent afd3d05e2f
commit e3ef968033
5 changed files with 16 additions and 13 deletions
+5 -4
View File
@@ -6,10 +6,6 @@ use crate::{GenRust, GetRust, RustCodegen};
impl GenRust for Block { impl GenRust for Block {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
if self.is_unsafe {
cg.add("unsafe ");
}
cg.addln("{"); cg.addln("{");
cg.indent += 1; cg.indent += 1;
@@ -33,6 +29,11 @@ impl GenRust for Block {
impl GenRust for Statement { impl GenRust for Statement {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
match self { match self {
Statement::UnsafeBlock(block) => {
cg.add("unsafe ");
block.gen_rust(ctx, cg);
}
Statement::Block(block) => block.gen_rust(ctx, cg), Statement::Block(block) => block.gen_rust(ctx, cg),
Statement::VarDecl(VarDeclStmt { decl, init }) => { Statement::VarDecl(VarDeclStmt { decl, init }) => {
+1 -2
View File
@@ -4,14 +4,13 @@ use super::*;
#[derive(Debug, Clone, Serialize, Default)] #[derive(Debug, Clone, Serialize, Default)]
pub struct Block { pub struct Block {
pub is_unsafe: bool,
pub statements: Vec<Spanned<Expression>>, pub statements: Vec<Spanned<Expression>>,
pub soft_return: Option<Spanned<Expression>>, pub soft_return: Option<Spanned<Expression>>,
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum Statement { pub enum Statement {
UnsafeBlock(Block),
Block(Block), Block(Block),
If { If {
initial: StatementBranch, initial: StatementBranch,
+6 -3
View File
@@ -64,8 +64,6 @@ static_path = {
identifier ~ ("::" ~ identifier)* identifier ~ ("::" ~ identifier)*
} }
unsafe_kw = { "unsafe" }
// ====================================================== // ======================================================
// PROGRAM // PROGRAM
// ====================================================== // ======================================================
@@ -345,7 +343,11 @@ top_level = {
// ====================================================== // ======================================================
block = { block = {
unsafe_kw? ~ "{" ~ statement_list ~ expr? ~ "}" "{" ~ statement_list ~ expr? ~ "}"
}
unsafe_block = {
"unsafe" ~ block
} }
statement_list = { statement_list = {
@@ -396,6 +398,7 @@ control_flow = {
| match_stmt | match_stmt
| loop_stmt | loop_stmt
| block | block
| unsafe_block
} }
statement_branch = { expr ~ block } statement_branch = { expr ~ block }
+1 -1
View File
@@ -100,7 +100,7 @@ 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 | Rule::basic_stmt | Rule::control_flow | Rule::block => ast_expr!( Rule::statement | Rule::basic_stmt | Rule::control_flow | Rule::block | Rule::unsafe_block => ast_expr!(
Expression::Statement(pair.try_into().get_map(Box::new).map(Box::new)) Expression::Statement(pair.try_into().get_map(Box::new).map(Box::new))
), ),
+3 -3
View File
@@ -2,8 +2,7 @@ use crate::{
Rule, Rule,
ast::*, ast::*,
ast_ensure, ast_expr, ast_ensure, ast_expr,
error::{AstError, AstResult, IntoErr, collect_recovered}, error::{AstError, IntoErr, collect_recovered},
parser::listen_rule,
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
@@ -14,7 +13,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
ast_ensure!(pair, Rule::block => { ast_ensure!(pair, Rule::block => {
ast_expr!(Block { ast_expr!(Block {
is_unsafe: Ok(listen_rule(&mut inner, Rule::unsafe_kw)) as AstResult<bool>,
statements: collect_recovered(inner.next().unwrap().into_inner()), statements: collect_recovered(inner.next().unwrap().into_inner()),
soft_return: inner.next().map(Spanned::try_from).transpose(), soft_return: inner.next().map(Spanned::try_from).transpose(),
}) })
@@ -49,6 +47,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
Statement::try_from(inner.next().unwrap()) Statement::try_from(inner.next().unwrap())
} }
Rule::unsafe_block => ast_expr!(Statement::UnsafeBlock(inner.next().unwrap().try_into())),
Rule::block => ast_expr!(Statement::Block(pair.try_into())), Rule::block => ast_expr!(Statement::Block(pair.try_into())),
Rule::var_decl_statement => ast_expr!(Statement::VarDecl(pair.try_into())), Rule::var_decl_statement => ast_expr!(Statement::VarDecl(pair.try_into())),