Skip to content

Commit

Permalink
move alpha converter to AST. fix #22
Browse files Browse the repository at this point in the history
  • Loading branch information
KeenS committed Nov 15, 2019
1 parent 40c4ed6 commit ac66418
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 40 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
pub mod case_check;
mod pp;
pub mod rename;
pub mod typing;
mod util;

use nom;

pub use self::case_check::CaseCheck;
pub use self::rename::Rename;
pub use self::typing::TyEnv as Typing;

use nom;
use std::cell::{Ref, RefCell, RefMut};
use std::error::Error;
use std::fmt;
Expand Down
77 changes: 44 additions & 33 deletions src/hir/rename.rs → src/ast/rename.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ast::util::Traverse;
use crate::ast::*;
use crate::config::Config;
use crate::hir::util::Traverse;
use crate::hir::*;
use crate::id::Id;
use crate::pass::Pass;
use crate::prim::*;
Expand Down Expand Up @@ -58,6 +58,19 @@ impl<'a> Scope<'a> {
symbol.1 = new_id;
}

fn new_symbol_pattern(&mut self, pat: &mut Pattern) {
use Pattern::*;
match pat {
Wildcard { .. } | Lit { .. } => (),
Var { name, .. } => self.new_symbol(name),
Tuple { tuple } => {
for (_, sym) in tuple {
self.new_symbol(sym)
}
}
}
}

fn rename(&mut self, symbol: &mut Symbol) {
let pos = self.pos;
for table in self.tables[0..pos].iter_mut().rev() {
Expand All @@ -73,15 +86,15 @@ impl<'a> Scope<'a> {
}

impl<'a> util::Traverse for Scope<'a> {
fn traverse_hir<'b, 'c>(&'b mut self, hir: &'c mut HIR) {
fn traverse_ast<'b, 'c>(&'b mut self, hir: &'c mut AST) {
let scope = self;
for val in hir.0.iter_mut() {
if val.rec {
scope.new_symbol(&mut val.name);
scope.new_symbol_pattern(&mut val.pattern);
scope.traverse_val(val);
} else {
scope.traverse_val(val);
scope.new_symbol(&mut val.name);
scope.new_symbol_pattern(&mut val.pattern);
}
}
}
Expand All @@ -90,56 +103,54 @@ impl<'a> util::Traverse for Scope<'a> {
self.traverse_expr(&mut val.expr);
}

fn traverse_binds(&mut self, _ty: &mut HTy, binds: &mut Vec<Val>, ret: &mut Box<Expr>) {
fn traverse_binds(&mut self, _ty: &mut TyDefer, binds: &mut Vec<Val>, ret: &mut Box<Expr>) {
let mut scope = self.new_scope();
for bind in binds.iter_mut() {
scope.traverse_val(bind);
scope.new_symbol(&mut bind.name);
scope.new_symbol_pattern(&mut bind.pattern);
}
scope.traverse_expr(ret);
}

fn traverse_binop(
&mut self,
op: &mut Symbol,
_ty: &mut TyDefer,
l: &mut Box<Expr>,
r: &mut Box<Expr>,
) {
self.rename(op);
self.traverse_expr(l);
self.traverse_expr(r);
}

fn traverse_fun(
&mut self,
param: &mut (HTy, Symbol),
_body_ty: &mut HTy,
_param_ty: &mut TyDefer,
param: &mut Symbol,
_body_ty: &mut TyDefer,
body: &mut Box<Expr>,
_captures: &mut Vec<(HTy, Symbol)>,
) {
let mut scope = self.new_scope();
scope.new_symbol(&mut param.1);
scope.new_symbol(param);
scope.traverse_expr(body);
}

fn traverse_case(
&mut self,
_ty: &mut HTy,
_ty: &mut TyDefer,
expr: &mut Box<Expr>,
arms: &mut Vec<(Pattern, Expr)>,
) {
self.traverse_expr(expr);
for &mut (ref mut pat, ref mut arm) in arms.iter_mut() {
let mut scope = self.new_scope();
for sym in pat.symbols_mut() {
scope.new_symbol(sym)
}
scope.new_symbol_pattern(pat);
scope.traverse_expr(arm);
}
}

fn traverse_closure(
&mut self,
envs: &mut Vec<(HTy, Symbol)>,
_param_ty: &mut HTy,
_body_ty: &mut HTy,
_fname: &mut Symbol,
) {
for &mut (_, ref mut var) in envs.iter_mut() {
self.rename(var);
}
}

fn traverse_sym(&mut self, _ty: &mut HTy, name: &mut Symbol) {
fn traverse_sym(&mut self, _ty: &mut TyDefer, name: &mut Symbol) {
self.rename(name);
}
}
Expand All @@ -164,11 +175,11 @@ impl Rename {
}
}

impl<E> Pass<HIR, E> for Rename {
type Target = HIR;
impl<E> Pass<AST, E> for Rename {
type Target = AST;

fn trans(&mut self, mut hir: HIR, _: &Config) -> ::std::result::Result<Self::Target, E> {
self.scope().traverse_hir(&mut hir);
Ok(hir)
fn trans(&mut self, mut ast: AST, _: &Config) -> ::std::result::Result<Self::Target, E> {
self.scope().traverse_ast(&mut ast);
Ok(ast)
}
}
2 changes: 0 additions & 2 deletions src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub mod flat_expr;
pub mod flat_let;
pub mod force_closure;
pub mod pp;
pub mod rename;
pub mod unnest_func;
pub mod util;

Expand All @@ -13,7 +12,6 @@ pub use self::find_builtin::FindBuiltin;
pub use self::flat_expr::FlatExpr;
pub use self::flat_let::FlatLet;
pub use self::force_closure::ForceClosure;
pub use self::rename::Rename;
pub use self::unnest_func::UnnestFunc;

use crate::prim::*;
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub use crate::pass::{Chain, Pass};

static BUILTIN_FUNCTIONS: &[&str] = &[
"print", // "+", "-", "*", "div", "/", "mod",
// "=", "<>", ">", ">=", "<", "<="
// "=", "<>", ">", ">=", "<", "<="
];

pub fn compile_str<'a>(input: &'a str, config: &Config) -> Result<Vec<u8>, TypeError<'a>> {
Expand All @@ -35,10 +35,10 @@ pub fn compile_str<'a>(input: &'a str, config: &Config) -> Result<Vec<u8>, TypeE

let mut passes = compile_pass![
parse: ConvError::new(parse),
rename: ast::Rename::new(id.clone()),
typing: ast::Typing::new(),
case_check: ast::CaseCheck::new(),
ast_to_hir: hir::AST2HIR::new(id.clone()),
rename: hir::Rename::new(id.clone()),
find_buildin: hir::FindBuiltin::new(),
flattening_expresion: hir::FlatExpr::new(id.clone()),
flattening_let: hir::FlatLet::new(),
Expand Down

0 comments on commit ac66418

Please sign in to comment.