Get initial AST-UIR translation sort of working

This commit is contained in:
Alex Bethel 2022-08-23 22:57:09 -06:00
parent df7e8b9d74
commit 4480a1973a
3 changed files with 39 additions and 9 deletions

View file

@ -148,6 +148,7 @@ enum Location {
/// Converts a program's abstract syntax tree into untyped IR code.
pub fn ast_to_untyped_ir(ast: SyntaxTree) -> Program {
let mut defs = vec![];
let mut counter = 0;
for stmt in ast.0.into_iter() {
match stmt {
syntax::Statement::TypeDefinition {
@ -169,7 +170,14 @@ pub fn ast_to_untyped_ir(ast: SyntaxTree) -> Program {
arguments,
definition,
}) => {
todo!();
defs.push((
name,
convert_fn(
&mut counter,
arguments,
definition.expect("Empty functions unimplemented"),
),
));
}
syntax::Statement::ClassMember(syntax::ClassMember::TypeAlias {
left: _,
@ -203,14 +211,21 @@ fn convert_fn(
let arg_loc = temporary(counter);
vec![
Instruction::DefLambda {
target: lambda_loc,
param: arg_loc,
body: todo!(),
target: lambda_loc.clone(),
param: arg_loc.clone(),
body: bind_pattern(counter, first, &arg_loc)
.into_iter()
.chain(convert_fn(counter, arguments, definition))
.collect(),
},
Instruction::Return { target: lambda_loc },
]
} else {
todo!()
let ret_loc = temporary(counter);
eval_expr(counter, definition, &ret_loc)
.into_iter()
.chain(std::iter::once(Instruction::Return { target: ret_loc }))
.collect()
}
}
@ -287,7 +302,19 @@ fn eval_expr(counter: &mut u64, e: syntax::Expr, l: &Location) -> Vec<Instructio
syntax::Expr::Lambda { arguments, result } => todo!(),
syntax::Expr::DotSubscript { value, subscript } => todo!(),
syntax::Expr::BracketSubscript { value, subscript } => todo!(),
syntax::Expr::Tuple(_) => todo!(),
syntax::Expr::Tuple(elems) => {
let elem_locs: Vec<_> = elems.iter().map(|_| temporary(counter)).collect();
elems
.into_iter()
.zip(elem_locs.iter())
.map(|(elem, elem_loc)| eval_expr(counter, elem, elem_loc))
.flatten()
.chain(std::iter::once(Instruction::Collect {
target: l.to_owned(),
source: elem_locs.clone(),
}))
.collect()
}
syntax::Expr::VariableReference(_) => todo!(),
syntax::Expr::Literal(_) => todo!(),
}

View file

@ -5,7 +5,7 @@ use std::{error::Error, fmt::Display, process::exit, str::FromStr};
use clap::Parser;
use drimc_rs::{
parser::{parser, ParserError, ParserMeta},
typeck::typeck,
typeck::typeck, ir_untyped::ast_to_untyped_ir,
};
/// Optimization levels.
@ -254,9 +254,9 @@ fn main() {
let source = std::fs::read_to_string(&args.source_file)?;
let meta = ParserMeta::default();
let ast = chumsky::Parser::parse(&parser(&meta), source).map_err(ParserError)?;
let ast = typeck(ast)?;
let untyped_ir = ast_to_untyped_ir(ast);
println!("{ast:?}");
println!("{untyped_ir:#?}");
Ok(())
}

3
drimc_rs/uir_test.drim Normal file
View file

@ -0,0 +1,3 @@
// IR Test
def nul (a, b) = ();