Skip to content

Commit

Permalink
add privacy to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenthz committed Dec 20, 2023
1 parent f3b1b81 commit cdcec45
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
29 changes: 17 additions & 12 deletions werbolg-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunDef, CompilationError> {
let ir::FunDef { name, vars, body } = fundef;
let ir::FunDef {
privacy: _,
name,
vars,
body,
} = fundef;

let mut local = LocalBindings::new();

Expand All @@ -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();

Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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) => {
Expand All @@ -244,15 +249,15 @@ 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));
Ok(())
}
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!()
Expand All @@ -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()
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion werbolg-compile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl<L: Clone + Eq + core::hash::Hash> CompilationState<L> {
);

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)
}
Expand Down
9 changes: 9 additions & 0 deletions werbolg-core/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -53,6 +60,7 @@ pub struct Use {
///
#[derive(Clone, Debug)]
pub struct FunDef {
pub privacy: Privacy,
pub name: Option<Ident>,
pub vars: Vec<Variable>,
pub body: Expr,
Expand Down Expand Up @@ -89,6 +97,7 @@ pub struct EnumDef {
#[derive(Clone, Debug)]
pub struct Variant(StructDef);

/// A pattern "matching"
#[derive(Clone, Debug)]
pub enum Binder {
Unit,
Expand Down
2 changes: 2 additions & 0 deletions werbolg-lang-lispy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ fn exprs_into_let(exprs: Vec<Spanned<Ast>>) -> Result<ir::Expr, ParseError> {
Box::new(ir::Expr::Lambda(
span_args,
Box::new(ir::FunDef {
privacy: ir::Privacy::Public,
name: None,
vars: args,
body: body,
Expand Down Expand Up @@ -147,6 +148,7 @@ fn statement(ast: Spanned<Ast>) -> Result<ir::Statement, ParseError> {
Ok(ir::Statement::Function(
ast.span,
ir::FunDef {
privacy: ir::Privacy::Public,
name: Some(name.unspan()),
vars: args,
body: body,
Expand Down
1 change: 1 addition & 0 deletions werbolg-lang-rusty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn module(fileunit: &FileUnit) -> Result<ir::Module, ParseError> {
Statement::Function(
span,
ir::FunDef {
privacy: ir::Privacy::Public,
name: Some(ir::Ident::from(n)),
vars: fun.args,
body,
Expand Down

0 comments on commit cdcec45

Please sign in to comment.