Move reduced ir types to separate file

This commit is contained in:
Greg Shuflin 2021-10-24 15:59:40 -07:00
parent f2c9cf20cb
commit 16164c2235
2 changed files with 101 additions and 94 deletions

View File

@ -4,10 +4,12 @@ use crate::builtin::Builtin;
use std::str::FromStr;
use std::collections::HashMap;
use std::rc::Rc;
mod types;
mod test;
pub use types::*;
pub fn reduce(ast: &ast::AST, symbol_table: &SymbolTable) -> ReducedIR {
let reducer = Reducer::new(symbol_table);
reducer.reduce(ast)
@ -209,96 +211,3 @@ impl<'a> Reducer<'a> {
}
}
}
/// The reduced intermediate representation consists of a list of function definitions, and a block
/// of entrypoint statements. In a repl or script context this can be an arbitrary list of
/// statements, in an executable context will likely just be a pointer to the main() function.
#[derive(Debug)]
pub struct ReducedIR {
pub functions: HashMap<DefId, FunctionDefinition>,
pub entrypoint: Vec<Statement>,
}
impl ReducedIR {
fn debug(&self, symbol_table: &SymbolTable) {
println!("Reduced IR:");
println!("Functions:");
println!("-----------");
for (id, callable) in self.functions.iter() {
let name = &symbol_table.lookup_symbol_by_def(id).unwrap().local_name;
println!("{}({}) -> {:?}", id, name, callable);
}
println!("");
println!("Entrypoint:");
println!("-----------");
for stmt in self.entrypoint.iter() {
println!("{:?}", stmt);
}
println!("-----------");
}
}
#[derive(Debug)]
pub enum Statement {
Expression(Expression),
Binding {
id: DefId,
constant: bool,
expr: Expression
},
}
#[derive(Debug)]
pub enum Expression {
Literal(Literal),
Tuple(Vec<Expression>),
Lookup {
id: DefId, //TODO eventually not everything that can be looked up will have a DefId
kind: Lookup,
},
Assign {
lval: DefId,
rval: Box<Expression>,
},
Callable(Function),
Call {
f: Box<Expression>,
args: Vec<Expression>
},
Unimplemented,
ReductionError(String),
}
impl Expression {
pub fn unit() -> Self {
Expression::Tuple(vec![])
}
}
#[derive(Debug)]
pub struct FunctionDefinition {
pub body: Vec<Statement>
}
#[derive(Debug, Clone)]
pub enum Function {
Builtin(Builtin),
UserDefined(DefId)
}
#[derive(Debug)]
pub enum Lookup {
LocalVar,
GlobalVar,
Function,
Param,
}
#[derive(Debug, Clone)]
pub enum Literal {
Nat(u64),
Int(i64),
Float(f64),
Bool(bool),
StringLit(Rc<String>),
}

View File

@ -0,0 +1,98 @@
use std::collections::HashMap;
use std::rc::Rc;
use crate::builtin::Builtin;
use crate::symbol_table::{DefId, Symbol, SymbolSpec, SymbolTable};
/// The reduced intermediate representation consists of a list of function definitions, and a block
/// of entrypoint statements. In a repl or script context this can be an arbitrary list of
/// statements, in an executable context will likely just be a pointer to the main() function.
#[derive(Debug)]
pub struct ReducedIR {
pub functions: HashMap<DefId, FunctionDefinition>,
pub entrypoint: Vec<Statement>,
}
impl ReducedIR {
fn debug(&self, symbol_table: &SymbolTable) {
println!("Reduced IR:");
println!("Functions:");
println!("-----------");
for (id, callable) in self.functions.iter() {
let name = &symbol_table.lookup_symbol_by_def(id).unwrap().local_name;
println!("{}({}) -> {:?}", id, name, callable);
}
println!("");
println!("Entrypoint:");
println!("-----------");
for stmt in self.entrypoint.iter() {
println!("{:?}", stmt);
}
println!("-----------");
}
}
#[derive(Debug)]
pub enum Statement {
Expression(Expression),
Binding {
id: DefId,
constant: bool,
expr: Expression
},
}
#[derive(Debug)]
pub enum Expression {
Literal(Literal),
Tuple(Vec<Expression>),
Lookup {
id: DefId, //TODO eventually not everything that can be looked up will have a DefId
kind: Lookup,
},
Assign {
lval: DefId,
rval: Box<Expression>,
},
Callable(Function),
Call {
f: Box<Expression>,
args: Vec<Expression>
},
Unimplemented,
ReductionError(String),
}
impl Expression {
pub fn unit() -> Self {
Expression::Tuple(vec![])
}
}
#[derive(Debug)]
pub struct FunctionDefinition {
pub body: Vec<Statement>
}
#[derive(Debug, Clone)]
pub enum Function {
Builtin(Builtin),
UserDefined(DefId)
}
#[derive(Debug)]
pub enum Lookup {
LocalVar,
GlobalVar,
Function,
Param,
}
#[derive(Debug, Clone)]
pub enum Literal {
Nat(u64),
Int(i64),
Float(f64),
Bool(bool),
StringLit(Rc<String>),
}