From cdcec45393388c8cd2d5a14cacac23088c3382c1 Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Wed, 20 Dec 2023 16:15:08 +0800 Subject: [PATCH] add privacy to functions --- werbolg-compile/src/compile.rs | 29 +++++++++++++++++------------ werbolg-compile/src/lib.rs | 2 +- werbolg-core/src/ir.rs | 9 +++++++++ werbolg-lang-lispy/src/lib.rs | 2 ++ werbolg-lang-rusty/src/lib.rs | 1 + 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/werbolg-compile/src/compile.rs b/werbolg-compile/src/compile.rs index 9f922c1..94c39a8 100644 --- a/werbolg-compile/src/compile.rs +++ b/werbolg-compile/src/compile.rs @@ -133,11 +133,16 @@ impl<'a, L: Clone + Eq + core::hash::Hash> RewriteState<'a, L> { } } -pub(crate) fn rewrite_fun<'a, L: Clone + Eq + core::hash::Hash>( +pub(crate) fn generate_func_code<'a, L: Clone + Eq + core::hash::Hash>( state: &mut RewriteState<'a, L>, fundef: ir::FunDef, ) -> Result { - let ir::FunDef { name, vars, body } = fundef; + let ir::FunDef { + privacy: _, + name, + vars, + body, + } = fundef; let mut local = LocalBindings::new(); @@ -155,7 +160,7 @@ pub(crate) fn rewrite_fun<'a, L: Clone + Eq + core::hash::Hash>( } let code_pos = state.get_instruction_address(); - rewrite_expr2(state, &mut local, body.clone())?; + generate_expression_code(state, &mut local, body.clone())?; local.scope_leave(); @@ -168,7 +173,7 @@ pub(crate) fn rewrite_fun<'a, L: Clone + Eq + core::hash::Hash>( }) } -fn rewrite_expr2<'a, L: Clone + Eq + core::hash::Hash>( +fn generate_expression_code<'a, L: Clone + Eq + core::hash::Hash>( state: &mut RewriteState<'a, L>, local: &mut LocalBindings, expr: ir::Expr, @@ -204,7 +209,7 @@ fn rewrite_expr2<'a, L: Clone + Eq + core::hash::Hash>( todo!() } ir::Expr::Let(binder, body, in_expr) => { - rewrite_expr2(state, local, *body)?; + generate_expression_code(state, local, *body)?; match binder { ir::Binder::Ident(ident) => { let bind = append_ident(local, &ident); @@ -218,7 +223,7 @@ fn rewrite_expr2<'a, L: Clone + Eq + core::hash::Hash>( state.write_code().push(Instruction::IgnoreOne); } } - rewrite_expr2(state, local, *in_expr)?; + generate_expression_code(state, local, *in_expr)?; Ok(()) } ir::Expr::Field(expr, struct_ident, field_ident) => { @@ -244,7 +249,7 @@ fn rewrite_expr2<'a, L: Clone + Eq + core::hash::Hash>( )); }; - rewrite_expr2(state, local, *expr)?; + generate_expression_code(state, local, *expr)?; state .write_code() .push(Instruction::AccessField(constr_id, index)); @@ -252,7 +257,7 @@ fn rewrite_expr2<'a, L: Clone + Eq + core::hash::Hash>( } ir::Expr::Lambda(_span, fundef) => { let prev = state.set_in_lambda(); - rewrite_fun(state, *fundef)?; + generate_func_code(state, *fundef)?; state.restore_codestate(prev); todo!() @@ -261,7 +266,7 @@ fn rewrite_expr2<'a, L: Clone + Eq + core::hash::Hash>( assert!(args.len() > 0); let len = args.len() - 1; for arg in args { - rewrite_expr2(state, local, arg)?; + generate_expression_code(state, local, arg)?; } state .write_code() @@ -274,20 +279,20 @@ fn rewrite_expr2<'a, L: Clone + Eq + core::hash::Hash>( then_expr, else_expr, } => { - rewrite_expr2(state, local, (*cond).unspan())?; + generate_expression_code(state, local, (*cond).unspan())?; let cond_jump_ref = state.write_code().push_temp(); let cond_pos = state.get_instruction_address(); local.scope_enter(); - rewrite_expr2(state, local, (*then_expr).unspan())?; + generate_expression_code(state, local, (*then_expr).unspan())?; local.scope_leave(); let jump_else_ref = state.write_code().push_temp(); let else_pos = state.get_instruction_address(); local.scope_enter(); - rewrite_expr2(state, local, (*else_expr).unspan())?; + generate_expression_code(state, local, (*else_expr).unspan())?; local.scope_leave(); let end_pos = state.get_instruction_address(); diff --git a/werbolg-compile/src/lib.rs b/werbolg-compile/src/lib.rs index fe7d3ec..be0091e 100644 --- a/werbolg-compile/src/lib.rs +++ b/werbolg-compile/src/lib.rs @@ -141,7 +141,7 @@ impl CompilationState { ); for (funid, fundef) in vecdata.into_iter() { - let lirdef = compile::rewrite_fun(&mut state, fundef)?; + let lirdef = compile::generate_func_code(&mut state, fundef)?; let lirid = state.funs_vec.push(lirdef); assert_eq!(funid, lirid) } diff --git a/werbolg-core/src/ir.rs b/werbolg-core/src/ir.rs index 6a65757..cc05146 100644 --- a/werbolg-core/src/ir.rs +++ b/werbolg-core/src/ir.rs @@ -43,6 +43,13 @@ pub struct Use { pub renames: Vec<(Ident, Ident)>, } +/// AST for symbol privacy (public / private) +#[derive(Clone, Copy, Debug)] +pub enum Privacy { + Public, + Private, +} + /// AST for function definition /// /// Function definitions are something like: @@ -53,6 +60,7 @@ pub struct Use { /// #[derive(Clone, Debug)] pub struct FunDef { + pub privacy: Privacy, pub name: Option, pub vars: Vec, pub body: Expr, @@ -89,6 +97,7 @@ pub struct EnumDef { #[derive(Clone, Debug)] pub struct Variant(StructDef); +/// A pattern "matching" #[derive(Clone, Debug)] pub enum Binder { Unit, diff --git a/werbolg-lang-lispy/src/lib.rs b/werbolg-lang-lispy/src/lib.rs index e01deae..2649c13 100644 --- a/werbolg-lang-lispy/src/lib.rs +++ b/werbolg-lang-lispy/src/lib.rs @@ -108,6 +108,7 @@ fn exprs_into_let(exprs: Vec>) -> Result { Box::new(ir::Expr::Lambda( span_args, Box::new(ir::FunDef { + privacy: ir::Privacy::Public, name: None, vars: args, body: body, @@ -147,6 +148,7 @@ fn statement(ast: Spanned) -> Result { Ok(ir::Statement::Function( ast.span, ir::FunDef { + privacy: ir::Privacy::Public, name: Some(name.unspan()), vars: args, body: body, diff --git a/werbolg-lang-rusty/src/lib.rs b/werbolg-lang-rusty/src/lib.rs index 34f51f0..7df6a47 100644 --- a/werbolg-lang-rusty/src/lib.rs +++ b/werbolg-lang-rusty/src/lib.rs @@ -23,6 +23,7 @@ pub fn module(fileunit: &FileUnit) -> Result { Statement::Function( span, ir::FunDef { + privacy: ir::Privacy::Public, name: Some(ir::Ident::from(n)), vars: fun.args, body,