Skip to content

Commit

Permalink
feat: support recusive type/function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shylock-Hg committed Jan 26, 2025
1 parent 5d2f934 commit 5673514
Show file tree
Hide file tree
Showing 8 changed files with 573 additions and 137 deletions.
207 changes: 207 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions tigerc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ edition = "2021"
unicode-xid = "0.2.4"
indexmap = "2.2.6"
tigerc-macros = { path = "../tigerc-macros" }
clap = { version = "4.5.27", features = ["derive"] }
log = "0.4.25"
8 changes: 4 additions & 4 deletions tigerc/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::vec;
use crate::ast;
use crate::ident_pool::Symbol;

struct Escape {
pub struct Escape {
e: Option<Rc<RefCell<bool>>>,
}

Expand Down Expand Up @@ -152,7 +152,7 @@ impl Escape {
ast::LeftValue::Variable(s) => {
// escape!(depth, var_depth, s, var, self.e);
if let Some(v) = var {
if depth > var_depth && dbg!(v) == s {
if depth > var_depth && v == s {
if let Some(e) = &self.e {
e.replace(true);
}
Expand Down Expand Up @@ -238,7 +238,7 @@ mod tests {
let mut parser = Parser::new(Box::new(it));
let mut ast = parser.parse();
Escape::new().escape(&mut ast);
let escape = match dbg!(ast) {
let escape = match ast {
ast::Ast::Expr(ast::Expr::Let(ast::Let { decls, .. })) => {
let decl = decls.get(1).unwrap();
match decl {
Expand Down Expand Up @@ -267,7 +267,7 @@ mod tests {
let mut parser = Parser::new(Box::new(it));
let mut ast = parser.parse();
Escape::new().escape(&mut ast);
let escape = match dbg!(ast) {
let escape = match ast {
ast::Ast::Expr(ast::Expr::Let(ast::Let { decls, .. })) => {
let decl = decls.get(2).unwrap();
match decl {
Expand Down
10 changes: 10 additions & 0 deletions tigerc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ pub mod symbol_table;
pub mod tokenizer;
pub mod type_ast;
pub mod type_inference;

pub fn compile_file(f: &str) {
let content = std::fs::read_to_string(f).unwrap();
let it = tokenizer::tokenize(&content);
let mut parser = parser::Parser::new(Box::new(it));
let mut e = parser.parse();
escape::Escape::new().escape(&mut e);
let mut ti = type_inference::TypeInference::new();
let _te = ti.infer(&e).unwrap();
}
12 changes: 11 additions & 1 deletion tigerc/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
use clap::Parser as cParser;

use tiger_c;

#[derive(cParser, Debug)]
struct Args {
input: String,
}

fn main() {
println!("Hello, world!");
let args = Args::parse();
tiger_c::compile_file(&args.input);
}
19 changes: 18 additions & 1 deletion tigerc/src/type_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,26 @@ pub enum Type {
Record(Record),
Array(Box<Type>),
Function(Function),
// just place holder for recursive type/function
Name(Symbol),
}

impl Type {
// Can assign to other?
pub fn can_assign(&self, other: &Type) -> bool {
match self {
Type::Nil => {
if let Type::Record(_) = other {
true
} else {
false
}
}
_ => self == other,
}
}
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Record {
pub fields: IndexMap<Symbol, Type>,
Expand Down Expand Up @@ -200,7 +217,7 @@ pub struct FuncDecl {
#[derive(Debug, PartialEq, Eq)]
pub struct TyDecl {
pub type_name: Symbol,
pub ty: Ty,
pub ty: Type,
}

#[derive(Debug, PartialEq, Eq)]
Expand Down
Loading

0 comments on commit 5673514

Please sign in to comment.