Bunch of AST refactoring to make api easier.

This commit is contained in:
Jesse Brault 2026-03-09 16:35:19 -05:00
parent 9d09f7481b
commit e35bacb583
23 changed files with 561 additions and 245 deletions

View File

@ -47,14 +47,26 @@ fn main() {
let mut symbol_table = SymbolTable::new(); let mut symbol_table = SymbolTable::new();
let gather_names_diagnostics = compilation_unit.gather_declared_names(&mut symbol_table); match compilation_unit.gather_declared_names(&mut symbol_table) {
check_and_report_diagnostics(&files, script_file_id, &gather_names_diagnostics); Ok(_) => {}
Err(diagnostics) => {
report_and_exit(&diagnostics, script_file_id, &files);
}
}
let name_usages_diagnostics = compilation_unit.check_name_usages(&symbol_table); match compilation_unit.check_name_usages(&symbol_table) {
check_and_report_diagnostics(&files, script_file_id, &name_usages_diagnostics); Ok(_) => {}
Err(diagnostics) => {
report_and_exit(&diagnostics, script_file_id, &files);
}
}
let type_check_diagnostics = compilation_unit.type_check(&symbol_table); match compilation_unit.type_check(&symbol_table) {
check_and_report_diagnostics(&files, script_file_id, &type_check_diagnostics); Ok(_) => {}
Err(diagnostics) => {
report_and_exit(&diagnostics, script_file_id, &files);
}
}
let mut ir_functions = compilation_unit.to_ir(&symbol_table); let mut ir_functions = compilation_unit.to_ir(&symbol_table);
@ -118,26 +130,34 @@ fn check_and_report_diagnostics(
diagnostics: &[Diagnostic], diagnostics: &[Diagnostic],
) { ) {
if !diagnostics.is_empty() { if !diagnostics.is_empty() {
let writer = StandardStream::stderr(ColorChoice::Always); report_and_exit(diagnostics, script_file_id, files);
let config = term::Config::default();
for diagnostic in diagnostics {
let csr_diagnostic = codespan_reporting::diagnostic::Diagnostic::error()
.with_message(diagnostic.message())
.with_label(Label::primary(
script_file_id,
diagnostic.start()..diagnostic.end(),
));
term::emit_to_write_style(&mut writer.lock(), &config, files, &csr_diagnostic).unwrap();
}
if diagnostics.len() == 1 {
eprintln!("There was 1 diagnostic. See above for more information.");
} else {
eprintln!(
"There were {} diagnostics. See above for more information.",
diagnostics.len()
);
}
std::process::exit(1);
} }
} }
fn report_and_exit(
diagnostics: &[Diagnostic],
script_file_id: usize,
files: &SimpleFiles<&str, &str>,
) -> ! {
let writer = StandardStream::stderr(ColorChoice::Always);
let config = term::Config::default();
for diagnostic in diagnostics {
let csr_diagnostic = codespan_reporting::diagnostic::Diagnostic::error()
.with_message(diagnostic.message())
.with_label(Label::primary(
script_file_id,
diagnostic.start()..diagnostic.end(),
));
term::emit_to_write_style(&mut writer.lock(), &config, files, &csr_diagnostic).unwrap();
}
if diagnostics.len() == 1 {
eprintln!("There was 1 diagnostic. See above for more information.");
} else {
eprintln!(
"There were {} diagnostics. See above for more information.",
diagnostics.len()
);
}
std::process::exit(1);
}

View File

@ -6,18 +6,20 @@ use crate::source_range::SourceRange;
use crate::symbol_table::SymbolTable; use crate::symbol_table::SymbolTable;
use crate::type_info::TypeInfo; use crate::type_info::TypeInfo;
pub struct AdditiveExpression { pub struct AddExpression {
lhs: Box<Expression>, lhs: Box<Expression>,
rhs: Box<Expression>, rhs: Box<Expression>,
source_range: SourceRange, source_range: SourceRange,
type_info: Option<TypeInfo>,
} }
impl AdditiveExpression { impl AddExpression {
pub fn new(lhs: Expression, rhs: Expression, source_range: SourceRange) -> Self { pub fn new(lhs: Expression, rhs: Expression, source_range: SourceRange) -> Self {
Self { Self {
lhs: lhs.into(), lhs: lhs.into(),
rhs: rhs.into(), rhs: rhs.into(),
source_range, source_range,
type_info: None,
} }
} }
@ -33,9 +35,12 @@ impl AdditiveExpression {
&mut self, &mut self,
symbol_table: &mut SymbolTable, symbol_table: &mut SymbolTable,
) -> Result<(), Vec<Diagnostic>> { ) -> Result<(), Vec<Diagnostic>> {
let mut diagnostics = vec![]; let diagnostics: Vec<Diagnostic> = [self.lhs.as_mut(), self.rhs.as_mut()]
diagnostics.append(&mut self.lhs.gather_declared_names(symbol_table)); .iter_mut()
diagnostics.append(&mut self.rhs.gather_declared_names(symbol_table)); .map(|expression| expression.gather_declared_names(symbol_table))
.filter_map(Result::err)
.flatten()
.collect();
if diagnostics.is_empty() { if diagnostics.is_empty() {
Ok(()) Ok(())
} else { } else {
@ -44,9 +49,12 @@ impl AdditiveExpression {
} }
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> { pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
let mut diagnostics = vec![]; let diagnostics: Vec<Diagnostic> = [self.lhs.as_mut(), self.rhs.as_mut()]
diagnostics.append(&mut self.lhs.check_name_usages(symbol_table)); .iter_mut()
diagnostics.append(&mut self.rhs.check_name_usages(symbol_table)); .map(|expression| expression.check_name_usages(symbol_table))
.filter_map(Result::err)
.flatten()
.collect();
if diagnostics.is_empty() { if diagnostics.is_empty() {
Ok(()) Ok(())
} else { } else {
@ -55,12 +63,13 @@ impl AdditiveExpression {
} }
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> { pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
self.lhs.type_check(symbol_table); self.lhs.type_check(symbol_table)?;
self.rhs.type_check(symbol_table); self.rhs.type_check(symbol_table)?;
let lhs_type_info = self.lhs.type_info(); let lhs_type_info = self.lhs.type_info();
let rhs_type_info = self.rhs.type_info(); let rhs_type_info = self.rhs.type_info();
if lhs_type_info.can_add(&rhs_type_info) { if lhs_type_info.can_add(rhs_type_info) {
self.type_info = Some(lhs_type_info.add_result(rhs_type_info));
Ok(()) Ok(())
} else { } else {
Err(vec![Diagnostic::new( Err(vec![Diagnostic::new(
@ -83,8 +92,8 @@ impl AdditiveExpression {
IrAdd::new(lhs_ir_expression, rhs_ir_expression) IrAdd::new(lhs_ir_expression, rhs_ir_expression)
} }
pub fn type_info(&self) -> TypeInfo { pub fn type_info(&self) -> &TypeInfo {
self.lhs.type_info().additive_result(&self.rhs.type_info()) self.type_info.as_ref().unwrap()
} }
pub fn source_range(&self) -> &SourceRange { pub fn source_range(&self) -> &SourceRange {

View File

@ -14,6 +14,7 @@ pub struct Call {
callee: Box<Expression>, callee: Box<Expression>,
arguments: Vec<Expression>, arguments: Vec<Expression>,
source_range: SourceRange, source_range: SourceRange,
return_type_info: Option<TypeInfo>,
} }
impl Call { impl Call {
@ -22,6 +23,7 @@ impl Call {
callee: callee.into(), callee: callee.into(),
arguments, arguments,
source_range, source_range,
return_type_info: None,
} }
} }
@ -33,44 +35,74 @@ impl Call {
self.arguments.iter().collect() self.arguments.iter().collect()
} }
pub fn gather_declared_names(&mut self, symbol_table: &mut SymbolTable) -> Vec<Diagnostic> { pub fn gather_declared_names(
let mut diagnostics = vec![]; &mut self,
diagnostics.append(&mut self.callee.gather_declared_names(symbol_table)); symbol_table: &mut SymbolTable,
for argument in &mut self.arguments { ) -> Result<(), Vec<Diagnostic>> {
diagnostics.append(&mut argument.gather_declared_names(symbol_table)); let mut to_gather = vec![];
to_gather.push(self.callee.as_mut());
to_gather.extend(&mut self.arguments);
let diagnostics: Vec<Diagnostic> = to_gather
.iter_mut()
.map(|expression| expression.gather_declared_names(symbol_table))
.filter_map(Result::err)
.flatten()
.collect();
if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
} }
diagnostics
} }
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
let mut diagnostics = vec![]; let mut to_check = vec![];
diagnostics.append(&mut self.callee.check_name_usages(symbol_table)); to_check.push(self.callee.as_mut());
for argument in &mut self.arguments { to_check.extend(&mut self.arguments);
diagnostics.append(&mut argument.check_name_usages(symbol_table)); let diagnostics: Vec<Diagnostic> = to_check
.iter_mut()
.map(|expression| expression.check_name_usages(symbol_table))
.filter_map(Result::err)
.flatten()
.collect();
if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
} }
diagnostics
} }
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
let mut diagnostics = vec![]; self.callee.as_mut().type_check(symbol_table)?;
diagnostics.append(&mut self.callee.type_check(symbol_table));
for argument in &mut self.arguments { let mut diagnostics: Vec<Diagnostic> = self
diagnostics.append(&mut argument.type_check(symbol_table)); .arguments
} .iter_mut()
.map(|argument| argument.type_check(symbol_table))
.filter_map(Result::err)
.flatten()
.collect();
// check that callee is callable // check that callee is callable
let function_symbol = match self.callee.type_info() { let function_symbol = match self.callee.type_info() {
TypeInfo::Function(function_symbol) => function_symbol, TypeInfo::Function(function_symbol) => function_symbol,
_ => { _ => {
diagnostics.push(Diagnostic::new( diagnostics.push(Diagnostic::new(
"Receiver is not callable", &format!(
"Receiver of type {} is not callable.",
self.callee.type_info()
),
self.callee.source_range().start(), self.callee.source_range().start(),
self.callee.source_range().end(), self.callee.source_range().end(),
)); ));
return diagnostics; return Err(diagnostics);
} }
}; };
// set return type
self.return_type_info = Some(function_symbol.borrow().return_type_info().clone());
// check arguments length // check arguments length
let function_symbol_ref = function_symbol.borrow(); let function_symbol_ref = function_symbol.borrow();
let parameters = function_symbol_ref.parameters(); let parameters = function_symbol_ref.parameters();
@ -87,23 +119,20 @@ impl Call {
} }
if !diagnostics.is_empty() { if !diagnostics.is_empty() {
return diagnostics; return Err(diagnostics);
} }
// check argument types // check argument types
for i in 0..parameters.len() { for i in 0..parameters.len() {
let parameter = &parameters[i]; let parameter = &parameters[i].borrow();
let argument = &self.arguments[i]; let argument = &self.arguments[i];
if !parameter let parameter_type_info = parameter.type_info();
.borrow() let argument_type_info = argument.type_info();
.type_info() if !parameter_type_info.is_assignable_from(argument_type_info) {
.is_assignable_from(&argument.type_info())
{
diagnostics.push(Diagnostic::new( diagnostics.push(Diagnostic::new(
&format!( &format!(
"Mismatched types; expected {} but found {}", "Mismatched types: expected {} but found {}",
parameter.borrow().type_info(), parameter_type_info, argument_type_info
argument.type_info()
), ),
argument.source_range().start(), argument.source_range().start(),
argument.source_range().end(), argument.source_range().end(),
@ -111,14 +140,15 @@ impl Call {
} }
} }
diagnostics if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
}
} }
pub fn type_info(&self) -> TypeInfo { pub fn return_type_info(&self) -> &TypeInfo {
match self.callee.type_info() { self.return_type_info.as_ref().unwrap()
TypeInfo::Function(function_symbol) => function_symbol.borrow().return_type(),
_ => panic!(),
}
} }
fn get_callee_symbol(&self) -> Rc<RefCell<FunctionSymbol>> { fn get_callee_symbol(&self) -> Rc<RefCell<FunctionSymbol>> {

View File

@ -16,30 +16,57 @@ impl CompilationUnit {
&self.declarations &self.declarations
} }
pub fn gather_declared_names(&mut self, symbol_table: &mut SymbolTable) -> Vec<Diagnostic> { pub fn gather_declared_names(
let mut diagnostics = vec![]; &mut self,
symbol_table: &mut SymbolTable,
) -> Result<(), Vec<Diagnostic>> {
symbol_table.push_scope("compilation_unit_scope"); symbol_table.push_scope("compilation_unit_scope");
for declaration in &mut self.declarations {
diagnostics.append(&mut declaration.gather_declared_names(symbol_table)); let diagnostics: Vec<Diagnostic> = self
} .declarations
.iter_mut()
.map(|declaration| declaration.gather_declared_names(symbol_table))
.filter_map(Result::err)
.flatten()
.collect();
symbol_table.pop_scope(); symbol_table.pop_scope();
diagnostics
if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
}
} }
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
let mut diagnostics = vec![]; let diagnostics: Vec<Diagnostic> = self
for declaration in &mut self.declarations { .declarations
diagnostics.append(&mut declaration.check_name_usages(symbol_table)); .iter_mut()
.map(|declaration| declaration.check_name_usages(symbol_table))
.filter_map(Result::err)
.flatten()
.collect();
if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
} }
diagnostics
} }
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
let mut diagnostics = vec![]; let diagnostics: Vec<Diagnostic> = self
for declaration in &mut self.declarations { .declarations
diagnostics.append(&mut declaration.type_check(symbol_table)); .iter_mut()
.map(|declaration| declaration.type_check(symbol_table))
.filter_map(Result::err)
.flatten()
.collect();
if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
} }
diagnostics
} }
pub fn to_ir(&self, symbol_table: &SymbolTable) -> Vec<IrFunction> { pub fn to_ir(&self, symbol_table: &SymbolTable) -> Vec<IrFunction> {

View File

@ -1,4 +1,4 @@
use crate::ast::additive_expression::AdditiveExpression; use crate::ast::add_expression::AddExpression;
use crate::ast::call::Call; use crate::ast::call::Call;
use crate::ast::identifier::Identifier; use crate::ast::identifier::Identifier;
use crate::ast::integer_literal::IntegerLiteral; use crate::ast::integer_literal::IntegerLiteral;
@ -19,84 +19,88 @@ use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
pub enum Expression { pub enum Expression {
Call(Call), Add(AddExpression),
IntegerLiteral(IntegerLiteral),
String(StringLiteral),
Identifier(Identifier),
Additive(AdditiveExpression),
Subtract(SubtractExpression), Subtract(SubtractExpression),
Negative(NegativeExpression), Negative(NegativeExpression),
Call(Call),
Identifier(Identifier),
Integer(IntegerLiteral),
String(StringLiteral),
} }
impl Expression { impl Expression {
pub fn gather_declared_names(&mut self, symbol_table: &mut SymbolTable) -> Vec<Diagnostic> { pub fn gather_declared_names(
&mut self,
symbol_table: &mut SymbolTable,
) -> Result<(), Vec<Diagnostic>> {
match self { match self {
Expression::Add(add_expression) => add_expression.gather_declared_names(symbol_table),
Expression::Subtract(subtract_expression) => {
subtract_expression.gather_declared_names(symbol_table)
}
Expression::Negative(negative_expression) => {
negative_expression.gather_declared_names(symbol_table)
}
Expression::Call(call) => call.gather_declared_names(symbol_table), Expression::Call(call) => call.gather_declared_names(symbol_table),
Expression::Identifier(identifier) => identifier.gather_declared_names(symbol_table), Expression::Identifier(identifier) => identifier.gather_declared_names(symbol_table),
Expression::Additive(additive_expression) => { Expression::Integer(_) => Ok(()),
match additive_expression.gather_declared_names(symbol_table) { Expression::String(_) => Ok(()),
Ok(_) => {
vec![]
}
Err(diagnostics) => diagnostics,
}
}
_ => vec![],
} }
} }
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
match self { match self {
Expression::Add(add_expression) => add_expression.check_name_usages(symbol_table),
Expression::Subtract(subtract_expression) => {
subtract_expression.check_name_usages(symbol_table)
}
Expression::Negative(negative_expression) => {
negative_expression.check_name_usages(symbol_table)
}
Expression::Call(call) => call.check_name_usages(symbol_table), Expression::Call(call) => call.check_name_usages(symbol_table),
Expression::Identifier(identifier) => identifier.check_name_usages(symbol_table), Expression::Identifier(identifier) => identifier.check_name_usages(symbol_table),
Expression::Additive(additive_expression) => { Expression::Integer(_) => Ok(()),
match additive_expression.check_name_usages(symbol_table) { Expression::String(_) => Ok(()),
Ok(_) => {
vec![]
}
Err(diagnostics) => diagnostics,
}
}
_ => vec![],
} }
} }
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
match self { match self {
Expression::Add(add_expression) => add_expression.type_check(symbol_table),
Expression::Subtract(subtract_expression) => {
subtract_expression.type_check(symbol_table)
}
Expression::Negative(negative_expression) => {
negative_expression.type_check(symbol_table)
}
Expression::Call(call) => call.type_check(symbol_table), Expression::Call(call) => call.type_check(symbol_table),
Expression::Additive(additive_expression) => { Expression::Identifier(identifier) => identifier.type_check(symbol_table),
match additive_expression.type_check(symbol_table) { Expression::Integer(_) => Ok(()),
Ok(_) => { Expression::String(_) => Ok(()),
vec![]
}
Err(diagnostics) => diagnostics,
}
}
_ => vec![],
} }
} }
pub fn type_info(&self) -> TypeInfo { pub fn type_info(&self) -> &TypeInfo {
match self { match self {
Expression::Call(call) => call.type_info(), Expression::Add(add_expression) => add_expression.type_info(),
Expression::IntegerLiteral(_) => TypeInfo::Integer, Expression::Subtract(subtract_expression) => subtract_expression.type_info(),
Expression::String(_) => TypeInfo::String, Expression::Negative(negative_expression) => negative_expression.type_info(),
Expression::Call(call) => call.return_type_info(),
Expression::Identifier(identifier) => identifier.type_info(), Expression::Identifier(identifier) => identifier.type_info(),
Expression::Additive(additive_expression) => additive_expression.type_info(), Expression::Integer(integer_literal) => integer_literal.type_info(),
Expression::Subtract(subtract_expression) => todo!(), Expression::String(string_literal) => string_literal.type_info(),
Expression::Negative(_) => todo!(),
} }
} }
pub fn source_range(&self) -> &SourceRange { pub fn source_range(&self) -> &SourceRange {
match self { match self {
Expression::Call(call) => call.source_range(), Expression::Add(additive_expression) => additive_expression.source_range(),
Expression::IntegerLiteral(integer_literal) => integer_literal.source_range(),
Expression::String(string_literal) => string_literal.source_range(),
Expression::Identifier(identifier) => identifier.source_range(),
Expression::Additive(additive_expression) => additive_expression.source_range(),
Expression::Subtract(subtract_expression) => subtract_expression.source_range(), Expression::Subtract(subtract_expression) => subtract_expression.source_range(),
Expression::Negative(negative_expression) => negative_expression.source_range(), Expression::Negative(negative_expression) => negative_expression.source_range(),
Expression::Call(call) => call.source_range(),
Expression::Identifier(identifier) => identifier.source_range(),
Expression::Integer(integer_literal) => integer_literal.source_range(),
Expression::String(string_literal) => string_literal.source_range(),
} }
} }
@ -108,7 +112,7 @@ impl Expression {
match self { match self {
Expression::Call(call) => { Expression::Call(call) => {
let ir_call = call.to_ir(builder, symbol_table); let ir_call = call.to_ir(builder, symbol_table);
if matches!(call.type_info(), TypeInfo::Void) { if matches!(call.return_type_info(), TypeInfo::Void) {
builder builder
.current_block_mut() .current_block_mut()
.add_statement(IrStatement::Call(ir_call)); .add_statement(IrStatement::Call(ir_call));
@ -117,7 +121,7 @@ impl Expression {
let t_var = IrVariable::new_vr( let t_var = IrVariable::new_vr(
builder.new_t_var().into(), builder.new_t_var().into(),
builder.current_block().id(), builder.current_block().id(),
call.type_info(), call.return_type_info(),
); );
let as_rc = Rc::new(RefCell::new(t_var)); let as_rc = Rc::new(RefCell::new(t_var));
let assign = IrAssign::new(as_rc.clone(), IrOperation::Call(ir_call)); let assign = IrAssign::new(as_rc.clone(), IrOperation::Call(ir_call));
@ -127,7 +131,7 @@ impl Expression {
Some(IrExpression::Variable(as_rc)) Some(IrExpression::Variable(as_rc))
} }
} }
Expression::IntegerLiteral(integer_literal) => { Expression::Integer(integer_literal) => {
Some(IrExpression::Int(integer_literal.value())) Some(IrExpression::Int(integer_literal.value()))
} }
Expression::String(string_literal) => { Expression::String(string_literal) => {
@ -137,7 +141,7 @@ impl Expression {
let expressible_symbol = identifier.expressible_symbol(); let expressible_symbol = identifier.expressible_symbol();
Some(expressible_symbol.ir_expression()) Some(expressible_symbol.ir_expression())
} }
Expression::Additive(additive_expression) => { Expression::Add(additive_expression) => {
let ir_add = additive_expression.to_ir(builder, symbol_table); let ir_add = additive_expression.to_ir(builder, symbol_table);
let t_var = IrVariable::new_vr( let t_var = IrVariable::new_vr(
builder.new_t_var().into(), builder.new_t_var().into(),

View File

@ -20,15 +20,18 @@ impl ExpressionStatement {
&self.expression &self.expression
} }
pub fn gather_declared_names(&mut self, symbol_table: &mut SymbolTable) -> Vec<Diagnostic> { pub fn gather_declared_names(
&mut self,
symbol_table: &mut SymbolTable,
) -> Result<(), Vec<Diagnostic>> {
self.expression.gather_declared_names(symbol_table) self.expression.gather_declared_names(symbol_table)
} }
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
self.expression.check_name_usages(symbol_table) self.expression.check_name_usages(symbol_table)
} }
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
self.expression.type_check(symbol_table) self.expression.type_check(symbol_table)
} }

View File

@ -31,7 +31,10 @@ impl ExternFunction {
&self.declared_name &self.declared_name
} }
pub fn gather_declared_names(&mut self, symbol_table: &mut SymbolTable) -> Vec<Diagnostic> { pub fn gather_declared_names(
&mut self,
symbol_table: &mut SymbolTable,
) -> Result<(), Vec<Diagnostic>> {
let mut diagnostics = vec![]; let mut diagnostics = vec![];
let insert_result = symbol_table.insert_function_symbol(FunctionSymbol::new( let insert_result = symbol_table.insert_function_symbol(FunctionSymbol::new(
@ -53,7 +56,7 @@ impl ExternFunction {
self.declared_name_source_range.start(), self.declared_name_source_range.start(),
self.declared_name_source_range.end(), self.declared_name_source_range.end(),
)); ));
diagnostics Err(diagnostics)
} }
}; };
} }
@ -79,24 +82,40 @@ impl ExternFunction {
symbol_table.pop_scope(); symbol_table.pop_scope();
diagnostics if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
}
} }
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
self.parameters let diagnostics: Vec<Diagnostic> = self
.parameters
.iter_mut() .iter_mut()
.map(|parameter| parameter.check_name_usages(symbol_table)) .map(|parameter| parameter.check_name_usages(symbol_table))
.filter_map(Result::err) .filter_map(Result::err)
.flatten() .flatten()
.collect() .collect();
if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
}
} }
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
self.parameters let diagnostics: Vec<Diagnostic> = self
.parameters
.iter_mut() .iter_mut()
.map(|parameter| parameter.type_check(symbol_table)) .map(|parameter| parameter.type_check(symbol_table))
.filter_map(Result::err) .filter_map(Result::err)
.flatten() .flatten()
.collect() .collect();
if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
}
} }
} }

View File

@ -48,7 +48,10 @@ impl Function {
self.statements.iter().collect() self.statements.iter().collect()
} }
pub fn gather_declared_names(&mut self, symbol_table: &mut SymbolTable) -> Vec<Diagnostic> { pub fn gather_declared_names(
&mut self,
symbol_table: &mut SymbolTable,
) -> Result<(), Vec<Diagnostic>> {
let mut diagnostics = vec![]; let mut diagnostics = vec![];
// insert function symbol // insert function symbol
@ -75,7 +78,7 @@ impl Function {
self.declared_name_source_range.start(), self.declared_name_source_range.start(),
self.declared_name_source_range.end(), self.declared_name_source_range.end(),
)); ));
diagnostics Err(diagnostics)
} }
}; };
} }
@ -103,14 +106,24 @@ impl Function {
symbol_table.push_scope(&format!("body_scope({})", self.declared_name)); symbol_table.push_scope(&format!("body_scope({})", self.declared_name));
for statement in &mut self.statements { for statement in &mut self.statements {
diagnostics.append(&mut statement.gather_declared_names(symbol_table)); match statement.gather_declared_names(symbol_table) {
Ok(_) => {}
Err(mut statement_diagnostics) => {
diagnostics.append(&mut statement_diagnostics);
}
}
} }
symbol_table.pop_scope(); // body symbol_table.pop_scope(); // body
symbol_table.pop_scope(); // params symbol_table.pop_scope(); // params
diagnostics
if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
}
} }
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
let mut diagnostics = vec![]; let mut diagnostics = vec![];
// parameters // parameters
@ -126,12 +139,22 @@ impl Function {
// statements // statements
for statement in &mut self.statements { for statement in &mut self.statements {
diagnostics.append(&mut statement.check_name_usages(symbol_table)); match statement.check_name_usages(symbol_table) {
Ok(_) => {}
Err(mut statement_diagnostics) => {
diagnostics.append(&mut statement_diagnostics);
}
}
}
if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
} }
diagnostics
} }
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
let mut diagnostics = vec![]; let mut diagnostics = vec![];
// parameters // parameters
@ -140,17 +163,27 @@ impl Function {
.parameters .parameters
.iter_mut() .iter_mut()
.map(|parameter| parameter.type_check(symbol_table)) .map(|parameter| parameter.type_check(symbol_table))
.filter_map(|result| result.err()) .filter_map(Result::err)
.flatten() .flatten()
.collect(), .collect(),
); );
// statements // statements
for statement in &mut self.statements { diagnostics.append(
diagnostics.append(&mut statement.type_check(symbol_table)); &mut self
} .statements
.iter_mut()
.map(|statement| statement.type_check(symbol_table))
.filter_map(Result::err)
.flatten()
.collect(),
);
diagnostics if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
}
} }
pub fn to_ir(&self, symbol_table: &SymbolTable) -> IrFunction { pub fn to_ir(&self, symbol_table: &SymbolTable) -> IrFunction {
@ -172,12 +205,8 @@ impl Function {
let entry_block_id = builder.new_block(); let entry_block_id = builder.new_block();
let return_type_info = self let function_symbol = self.function_symbol.as_ref().unwrap().borrow();
.function_symbol let return_type_info = function_symbol.return_type_info();
.as_ref()
.unwrap()
.borrow()
.return_type();
let should_return_value = !matches!(return_type_info, TypeInfo::Void); let should_return_value = !matches!(return_type_info, TypeInfo::Void);

View File

@ -8,6 +8,7 @@ pub struct Identifier {
name: String, name: String,
scope_id: Option<usize>, scope_id: Option<usize>,
expressible_symbol: Option<ExpressibleSymbol>, expressible_symbol: Option<ExpressibleSymbol>,
type_info: Option<TypeInfo>,
source_range: SourceRange, source_range: SourceRange,
} }
@ -17,6 +18,7 @@ impl Identifier {
name: name.into(), name: name.into(),
scope_id: None, scope_id: None,
expressible_symbol: None, expressible_symbol: None,
type_info: None,
source_range, source_range,
} }
} }
@ -25,41 +27,38 @@ impl Identifier {
&self.name &self.name
} }
pub fn gather_declared_names(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn gather_declared_names(
&mut self,
symbol_table: &SymbolTable,
) -> Result<(), Vec<Diagnostic>> {
self.scope_id = Some(symbol_table.current_scope_id()); self.scope_id = Some(symbol_table.current_scope_id());
vec![] Ok(())
} }
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
let maybe_expressible_symbol = let maybe_expressible_symbol =
symbol_table.find_expressible_symbol(self.scope_id.unwrap(), &self.name); symbol_table.find_expressible_symbol(self.scope_id.unwrap(), &self.name);
match maybe_expressible_symbol { match maybe_expressible_symbol {
None => { None => Err(vec![Diagnostic::new(
vec![Diagnostic::new( &format!("Unable to resolve symbol {}", self.name),
&format!("Unable to resolve symbol {}", self.name), self.source_range.start(),
self.source_range.start(), self.source_range.end(),
self.source_range.end(), )]),
)]
}
Some(expressible_symbol) => { Some(expressible_symbol) => {
self.expressible_symbol = Some(expressible_symbol); self.expressible_symbol = Some(expressible_symbol);
vec![] Ok(())
} }
} }
} }
pub fn type_info(&self) -> TypeInfo { pub fn type_check(&mut self, _symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
match self.expressible_symbol.as_ref().unwrap() { let type_info = self.expressible_symbol.as_ref().unwrap().type_info();
ExpressibleSymbol::Function(function_symbol) => { self.type_info = Some(type_info);
TypeInfo::Function(function_symbol.clone()) Ok(())
} }
ExpressibleSymbol::Parameter(parameter_symbol) => {
parameter_symbol.borrow().type_info().clone() pub fn type_info(&self) -> &TypeInfo {
} self.type_info.as_ref().unwrap()
ExpressibleSymbol::Variable(variable_symbol) => {
variable_symbol.borrow().type_info().clone()
}
}
} }
pub fn expressible_symbol(&self) -> &ExpressibleSymbol { pub fn expressible_symbol(&self) -> &ExpressibleSymbol {

View File

@ -1,15 +1,19 @@
use crate::source_range::SourceRange; use crate::source_range::SourceRange;
use crate::type_info::TypeInfo;
pub struct IntegerLiteral { pub struct IntegerLiteral {
value: i32, value: i32,
source_range: SourceRange, source_range: SourceRange,
type_info: &'static TypeInfo,
} }
impl IntegerLiteral { impl IntegerLiteral {
pub fn new(value: i32, source_range: SourceRange) -> Self { pub fn new(value: i32, source_range: SourceRange) -> Self {
const TYPE_INFO: TypeInfo = TypeInfo::Integer;
Self { Self {
value, value,
source_range, source_range,
type_info: &TYPE_INFO,
} }
} }
@ -17,6 +21,10 @@ impl IntegerLiteral {
self.value self.value
} }
pub fn type_info(&self) -> &TypeInfo {
&self.type_info
}
pub fn source_range(&self) -> &SourceRange { pub fn source_range(&self) -> &SourceRange {
&self.source_range &self.source_range
} }

View File

@ -45,9 +45,19 @@ impl LetStatement {
&mut self.initializer &mut self.initializer
} }
pub fn gather_declared_names(&mut self, symbol_table: &mut SymbolTable) -> Vec<Diagnostic> { pub fn gather_declared_names(
&mut self,
symbol_table: &mut SymbolTable,
) -> Result<(), Vec<Diagnostic>> {
let mut diagnostics = vec![]; let mut diagnostics = vec![];
self.initializer_mut().gather_declared_names(symbol_table);
match self.initializer_mut().gather_declared_names(symbol_table) {
Ok(_) => {}
Err(mut initializer_diagnostics) => {
diagnostics.append(&mut initializer_diagnostics);
}
}
let insert_result = let insert_result =
symbol_table.insert_variable_symbol(VariableSymbol::new(self.declared_name())); symbol_table.insert_variable_symbol(VariableSymbol::new(self.declared_name()));
if let Err(symbol_insert_error) = insert_result { if let Err(symbol_insert_error) = insert_result {
@ -64,32 +74,35 @@ impl LetStatement {
} }
} }
} }
self.scope_id = Some(symbol_table.current_scope_id()); self.scope_id = Some(symbol_table.current_scope_id());
diagnostics
if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
}
} }
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
self.initializer.check_name_usages(symbol_table) self.initializer.check_name_usages(symbol_table)
} }
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
let initializer_diagnostics = self.initializer.type_check(symbol_table); self.initializer.type_check(symbol_table)?;
if !initializer_diagnostics.is_empty() {
return initializer_diagnostics;
}
let initializer_type_info = self.initializer.type_info(); let initializer_type_info = self.initializer.type_info();
let variable_symbol = let variable_symbol =
symbol_table.get_variable_symbol(self.scope_id.unwrap(), &self.declared_name); symbol_table.get_variable_symbol(self.scope_id.unwrap(), &self.declared_name);
variable_symbol variable_symbol
.borrow_mut() .borrow_mut()
.set_type_info(initializer_type_info); .set_type_info(initializer_type_info.clone());
vec![] Ok(())
} }
pub fn to_ir(&self, builder: &mut IrBuilder, symbol_table: &SymbolTable) { pub fn to_ir(&self, builder: &mut IrBuilder, symbol_table: &SymbolTable) {
let init_operation = match self.initializer() { let init_operation = match self.initializer() {
Expression::Call(call) => IrOperation::Call(call.to_ir(builder, symbol_table)), Expression::Call(call) => IrOperation::Call(call.to_ir(builder, symbol_table)),
Expression::IntegerLiteral(integer_literal) => { Expression::Integer(integer_literal) => {
IrOperation::Load(IrExpression::Int(integer_literal.value())) IrOperation::Load(IrExpression::Int(integer_literal.value()))
} }
Expression::String(string_literal) => { Expression::String(string_literal) => {
@ -98,7 +111,7 @@ impl LetStatement {
Expression::Identifier(identifier) => { Expression::Identifier(identifier) => {
IrOperation::Load(identifier.expressible_symbol().ir_expression()) IrOperation::Load(identifier.expressible_symbol().ir_expression())
} }
Expression::Additive(additive_expression) => { Expression::Add(additive_expression) => {
IrOperation::Add(additive_expression.to_ir(builder, symbol_table)) IrOperation::Add(additive_expression.to_ir(builder, symbol_table))
} }
Expression::Subtract(subtract_expression) => todo!(), Expression::Subtract(subtract_expression) => todo!(),

View File

@ -1,4 +1,4 @@
pub mod additive_expression; pub mod add_expression;
pub mod call; pub mod call;
pub mod compilation_unit; pub mod compilation_unit;
pub mod expression; pub mod expression;

View File

@ -9,7 +9,10 @@ pub enum ModuleLevelDeclaration {
} }
impl ModuleLevelDeclaration { impl ModuleLevelDeclaration {
pub fn gather_declared_names(&mut self, symbol_table: &mut SymbolTable) -> Vec<Diagnostic> { pub fn gather_declared_names(
&mut self,
symbol_table: &mut SymbolTable,
) -> Result<(), Vec<Diagnostic>> {
match self { match self {
ModuleLevelDeclaration::Function(function) => { ModuleLevelDeclaration::Function(function) => {
function.gather_declared_names(symbol_table) function.gather_declared_names(symbol_table)
@ -20,7 +23,7 @@ impl ModuleLevelDeclaration {
} }
} }
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
match self { match self {
ModuleLevelDeclaration::Function(function) => function.check_name_usages(symbol_table), ModuleLevelDeclaration::Function(function) => function.check_name_usages(symbol_table),
ModuleLevelDeclaration::ExternFunction(extern_function) => { ModuleLevelDeclaration::ExternFunction(extern_function) => {
@ -29,7 +32,7 @@ impl ModuleLevelDeclaration {
} }
} }
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
match self { match self {
ModuleLevelDeclaration::Function(function) => function.type_check(symbol_table), ModuleLevelDeclaration::Function(function) => function.type_check(symbol_table),
ModuleLevelDeclaration::ExternFunction(extern_function) => { ModuleLevelDeclaration::ExternFunction(extern_function) => {

View File

@ -1,9 +1,13 @@
use crate::ast::expression::Expression; use crate::ast::expression::Expression;
use crate::diagnostic::Diagnostic;
use crate::source_range::SourceRange; use crate::source_range::SourceRange;
use crate::symbol_table::SymbolTable;
use crate::type_info::TypeInfo;
pub struct NegativeExpression { pub struct NegativeExpression {
operand: Box<Expression>, operand: Box<Expression>,
source_range: SourceRange, source_range: SourceRange,
type_info: Option<TypeInfo>,
} }
impl NegativeExpression { impl NegativeExpression {
@ -11,6 +15,7 @@ impl NegativeExpression {
Self { Self {
operand: operand.into(), operand: operand.into(),
source_range, source_range,
type_info: None,
} }
} }
@ -25,4 +30,35 @@ impl NegativeExpression {
pub fn operand_mut(&mut self) -> &mut Expression { pub fn operand_mut(&mut self) -> &mut Expression {
&mut self.operand &mut self.operand
} }
pub fn gather_declared_names(
&mut self,
symbol_table: &mut SymbolTable,
) -> Result<(), Vec<Diagnostic>> {
self.operand.gather_declared_names(symbol_table)
}
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
self.operand.check_name_usages(symbol_table)
}
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
self.operand.type_check(symbol_table)?;
let type_info = self.operand.type_info();
if type_info.can_negate() {
self.type_info = Some(type_info.negate_result());
Ok(())
} else {
Err(vec![Diagnostic::new(
&format!("Cannot negate {}", type_info),
self.source_range.start(),
self.source_range.end(),
)])
}
}
pub fn type_info(&self) -> &TypeInfo {
self.type_info.as_ref().unwrap()
}
} }

View File

@ -10,7 +10,10 @@ pub enum Statement {
} }
impl Statement { impl Statement {
pub fn gather_declared_names(&mut self, symbol_table: &mut SymbolTable) -> Vec<Diagnostic> { pub fn gather_declared_names(
&mut self,
symbol_table: &mut SymbolTable,
) -> Result<(), Vec<Diagnostic>> {
match self { match self {
Statement::Let(let_statement) => let_statement.gather_declared_names(symbol_table), Statement::Let(let_statement) => let_statement.gather_declared_names(symbol_table),
Statement::Expression(expression_statement) => { Statement::Expression(expression_statement) => {
@ -19,7 +22,7 @@ impl Statement {
} }
} }
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
match self { match self {
Statement::Let(let_statement) => let_statement.check_name_usages(symbol_table), Statement::Let(let_statement) => let_statement.check_name_usages(symbol_table),
Statement::Expression(expression_statement) => { Statement::Expression(expression_statement) => {
@ -28,7 +31,7 @@ impl Statement {
} }
} }
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Vec<Diagnostic> { pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
match self { match self {
Statement::Let(let_statement) => let_statement.type_check(symbol_table), Statement::Let(let_statement) => let_statement.type_check(symbol_table),
Statement::Expression(expression_statement) => { Statement::Expression(expression_statement) => {

View File

@ -1,15 +1,19 @@
use crate::source_range::SourceRange; use crate::source_range::SourceRange;
use crate::type_info::TypeInfo;
pub struct StringLiteral { pub struct StringLiteral {
content: String, content: String,
source_range: SourceRange, source_range: SourceRange,
type_info: &'static TypeInfo,
} }
impl StringLiteral { impl StringLiteral {
pub fn new(content: &str, source_range: SourceRange) -> Self { pub fn new(content: &str, source_range: SourceRange) -> Self {
const TYPE_INFO: TypeInfo = TypeInfo::Integer;
Self { Self {
content: content.into(), content: content.into(),
source_range, source_range,
type_info: &TYPE_INFO,
} }
} }
@ -17,6 +21,10 @@ impl StringLiteral {
&self.content &self.content
} }
pub fn type_info(&self) -> &TypeInfo {
&self.type_info
}
pub fn source_range(&self) -> &SourceRange { pub fn source_range(&self) -> &SourceRange {
&self.source_range &self.source_range
} }

View File

@ -1,10 +1,14 @@
use crate::ast::expression::Expression; use crate::ast::expression::Expression;
use crate::diagnostic::Diagnostic;
use crate::source_range::SourceRange; use crate::source_range::SourceRange;
use crate::symbol_table::SymbolTable;
use crate::type_info::TypeInfo;
pub struct SubtractExpression { pub struct SubtractExpression {
lhs: Box<Expression>, lhs: Box<Expression>,
rhs: Box<Expression>, rhs: Box<Expression>,
source_range: SourceRange, source_range: SourceRange,
type_info: Option<TypeInfo>,
} }
impl SubtractExpression { impl SubtractExpression {
@ -13,6 +17,7 @@ impl SubtractExpression {
lhs: lhs.into(), lhs: lhs.into(),
rhs: rhs.into(), rhs: rhs.into(),
source_range, source_range,
type_info: None,
} }
} }
@ -24,6 +29,62 @@ impl SubtractExpression {
&self.rhs &self.rhs
} }
pub fn gather_declared_names(
&mut self,
symbol_table: &mut SymbolTable,
) -> Result<(), Vec<Diagnostic>> {
let diagnostics = [&mut self.lhs, &mut self.rhs]
.iter_mut()
.map(|expression| expression.gather_declared_names(symbol_table))
.filter_map(|result| result.err())
.flatten()
.collect::<Vec<Diagnostic>>();
if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
}
}
pub fn check_name_usages(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
let diagnostics: Vec<Diagnostic> = [&mut self.lhs, &mut self.rhs]
.iter_mut()
.map(|expression| expression.check_name_usages(symbol_table))
.filter_map(Result::err)
.flatten()
.collect();
if diagnostics.is_empty() {
Ok(())
} else {
Err(diagnostics)
}
}
pub fn type_check(&mut self, symbol_table: &SymbolTable) -> Result<(), Vec<Diagnostic>> {
self.lhs.type_check(symbol_table)?;
self.rhs.type_check(symbol_table)?;
let lhs_type_info = self.lhs.type_info();
let rhs_type_info = self.rhs.type_info();
if lhs_type_info.can_subtract(rhs_type_info) {
self.type_info = Some(lhs_type_info.add_result(rhs_type_info));
Ok(())
} else {
Err(vec![Diagnostic::new(
&format!(
"Incompatible types: cannot subtract {} from {}",
rhs_type_info, lhs_type_info
), // n.b. order
self.lhs.source_range().start(),
self.lhs.source_range().end(),
)])
}
}
pub fn type_info(&self) -> &TypeInfo {
self.type_info.as_ref().unwrap()
}
pub fn source_range(&self) -> &SourceRange { pub fn source_range(&self) -> &SourceRange {
&self.source_range &self.source_range
} }

View File

@ -23,13 +23,13 @@ impl IrFunction {
pub fn new( pub fn new(
function_symbol: Rc<RefCell<FunctionSymbol>>, function_symbol: Rc<RefCell<FunctionSymbol>>,
parameters: &[Rc<IrParameter>], parameters: &[Rc<IrParameter>],
return_type_info: TypeInfo, return_type_info: &TypeInfo,
entry: Rc<RefCell<IrBlock>>, entry: Rc<RefCell<IrBlock>>,
) -> Self { ) -> Self {
Self { Self {
function_symbol, function_symbol,
parameters: parameters.to_vec(), parameters: parameters.to_vec(),
return_type_info, return_type_info: return_type_info.clone(),
entry, entry,
} }
} }

View File

@ -9,19 +9,19 @@ pub struct IrVariable {
} }
impl IrVariable { impl IrVariable {
pub fn new_vr(name: Rc<str>, block_id: usize, type_info: TypeInfo) -> Self { pub fn new_vr(name: Rc<str>, block_id: usize, type_info: &TypeInfo) -> Self {
Self { Self {
descriptor: IrVariableDescriptor::VirtualRegister(IrVrVariableDescriptor::new( descriptor: IrVariableDescriptor::VirtualRegister(IrVrVariableDescriptor::new(
name, block_id, name, block_id,
)), )),
type_info, type_info: type_info.clone(),
} }
} }
pub fn new_stack(name: Rc<str>, block_id: usize, type_info: TypeInfo) -> Self { pub fn new_stack(name: Rc<str>, block_id: usize, type_info: TypeInfo) -> Self {
Self { Self {
descriptor: IrVariableDescriptor::Stack(IrStackVariableDescriptor::new(name, block_id)), descriptor: IrVariableDescriptor::Stack(IrStackVariableDescriptor::new(name, block_id)),
type_info, type_info: type_info.clone(),
} }
} }

View File

@ -1,4 +1,4 @@
use crate::ast::additive_expression::AdditiveExpression; use crate::ast::add_expression::AddExpression;
use crate::ast::call::Call; use crate::ast::call::Call;
use crate::ast::compilation_unit::CompilationUnit; use crate::ast::compilation_unit::CompilationUnit;
use crate::ast::expression::Expression; use crate::ast::expression::Expression;
@ -408,8 +408,7 @@ impl<'a> Parser<'a> {
let rhs = self.prefix_expression()?; let rhs = self.prefix_expression()?;
let source_range = let source_range =
SourceRange::new(result.source_range().start(), rhs.source_range().end()); SourceRange::new(result.source_range().start(), rhs.source_range().end());
result = result = Expression::Add(AddExpression::new(result, rhs, source_range));
Expression::Additive(AdditiveExpression::new(result, rhs, source_range));
} }
TokenKind::Minus => { TokenKind::Minus => {
self.advance(); // minus self.advance(); // minus
@ -477,7 +476,7 @@ impl<'a> Parser<'a> {
let raw = self.token_text(&current); let raw = self.token_text(&current);
let source_range = SourceRange::new(current.start(), current.end()); let source_range = SourceRange::new(current.start(), current.end());
self.advance(); self.advance();
Ok(Expression::IntegerLiteral(IntegerLiteral::new( Ok(Expression::Integer(IntegerLiteral::new(
i32::from_str(raw).unwrap(), i32::from_str(raw).unwrap(),
source_range, source_range,
))) )))
@ -716,7 +715,7 @@ mod concrete_tests {
let expression = assert_expression("-1"); let expression = assert_expression("-1");
match expression { match expression {
Expression::Negative(negative_expression) => match negative_expression.operand() { Expression::Negative(negative_expression) => match negative_expression.operand() {
Expression::IntegerLiteral(integer_literal) => { Expression::Integer(integer_literal) => {
assert_eq!(integer_literal.value(), 1); assert_eq!(integer_literal.value(), 1);
} }
_ => panic!("Expected integer literal"), _ => panic!("Expected integer literal"),
@ -729,9 +728,9 @@ mod concrete_tests {
fn add_negative() { fn add_negative() {
let expression = assert_expression("1 + -1"); let expression = assert_expression("1 + -1");
match expression { match expression {
Expression::Additive(add_expression) => { Expression::Add(add_expression) => {
match add_expression.lhs() { match add_expression.lhs() {
Expression::IntegerLiteral(integer_literal) => { Expression::Integer(integer_literal) => {
assert_eq!(integer_literal.value(), 1); assert_eq!(integer_literal.value(), 1);
} }
_ => panic!("Expected integer literal"), _ => panic!("Expected integer literal"),
@ -739,7 +738,7 @@ mod concrete_tests {
match add_expression.rhs() { match add_expression.rhs() {
Expression::Negative(negative_expression) => { Expression::Negative(negative_expression) => {
match negative_expression.operand() { match negative_expression.operand() {
Expression::IntegerLiteral(integer_literal) => { Expression::Integer(integer_literal) => {
assert_eq!(integer_literal.value(), 1); assert_eq!(integer_literal.value(), 1);
} }
_ => panic!("Expected integer literal"), _ => panic!("Expected integer literal"),
@ -758,13 +757,13 @@ mod concrete_tests {
match expression { match expression {
Expression::Subtract(subtract_expression) => { Expression::Subtract(subtract_expression) => {
match subtract_expression.lhs() { match subtract_expression.lhs() {
Expression::IntegerLiteral(integer_literal) => { Expression::Integer(integer_literal) => {
assert_eq!(integer_literal.value(), 1); assert_eq!(integer_literal.value(), 1);
} }
_ => panic!("Expected integer literal"), _ => panic!("Expected integer literal"),
} }
match subtract_expression.rhs() { match subtract_expression.rhs() {
Expression::IntegerLiteral(integer_literal) => { Expression::Integer(integer_literal) => {
assert_eq!(integer_literal.value(), 1); assert_eq!(integer_literal.value(), 1);
} }
_ => panic!("Expected integer literal"), _ => panic!("Expected integer literal"),

View File

@ -38,8 +38,8 @@ impl FunctionSymbol {
self.parameters.as_ref().unwrap() self.parameters.as_ref().unwrap()
} }
pub fn return_type(&self) -> TypeInfo { pub fn return_type_info(&self) -> &TypeInfo {
self.return_type.clone() &self.return_type
} }
pub fn is_platform(&self) -> bool { pub fn is_platform(&self) -> bool {
@ -134,6 +134,20 @@ pub enum ExpressibleSymbol {
} }
impl ExpressibleSymbol { impl ExpressibleSymbol {
pub fn type_info(&self) -> TypeInfo {
match self {
ExpressibleSymbol::Function(function_symbol) => {
TypeInfo::Function(function_symbol.clone()) // n.b. not the return type!
}
ExpressibleSymbol::Parameter(parameter_symbol) => {
parameter_symbol.borrow().type_info().clone()
}
ExpressibleSymbol::Variable(variable_symbol) => {
variable_symbol.borrow().type_info().clone()
}
}
}
pub fn ir_expression(&self) -> IrExpression { pub fn ir_expression(&self) -> IrExpression {
match self { match self {
ExpressibleSymbol::Function(_) => { ExpressibleSymbol::Function(_) => {

View File

@ -68,11 +68,36 @@ impl TypeInfo {
} }
} }
pub fn additive_result(&self, _rhs: &Self) -> TypeInfo { pub fn add_result(&self, _rhs: &Self) -> TypeInfo {
match self { match self {
TypeInfo::Integer => TypeInfo::Integer, TypeInfo::Integer => TypeInfo::Integer,
TypeInfo::String => TypeInfo::String, TypeInfo::String => TypeInfo::String,
_ => panic!("Adding things other than integers and strings not yet supported"), _ => panic!("Adding things other than integers and strings not yet supported"),
} }
} }
pub fn can_subtract(&self, rhs: &Self) -> bool {
matches!(self, TypeInfo::Integer) && matches!(rhs, TypeInfo::Integer)
}
pub fn subtract_result(&self, rhs: &Self) -> TypeInfo {
match self {
TypeInfo::Integer => match rhs {
TypeInfo::Integer => TypeInfo::Integer,
_ => panic!(),
},
_ => panic!(),
}
}
pub fn can_negate(&self) -> bool {
matches!(self, TypeInfo::Integer)
}
pub fn negate_result(&self) -> TypeInfo {
match self {
TypeInfo::Integer => TypeInfo::Integer,
_ => panic!(),
}
}
} }

View File

@ -35,19 +35,25 @@ mod e2e_tests {
let mut symbol_table = SymbolTable::new(); let mut symbol_table = SymbolTable::new();
let gather_names_diagnostics = compilation_unit.gather_declared_names(&mut symbol_table); match compilation_unit.gather_declared_names(&mut symbol_table) {
if !gather_names_diagnostics.is_empty() { Ok(_) => {}
report_diagnostics(&gather_names_diagnostics); Err(diagnostics) => {
report_diagnostics(&diagnostics);
}
} }
let name_usages_diagnostics = compilation_unit.check_name_usages(&symbol_table); match compilation_unit.check_name_usages(&symbol_table) {
if !name_usages_diagnostics.is_empty() { Ok(_) => {}
report_diagnostics(&name_usages_diagnostics); Err(diagnostics) => {
report_diagnostics(&diagnostics);
}
} }
let type_check_diagnostics = compilation_unit.type_check(&symbol_table); match compilation_unit.type_check(&symbol_table) {
if !type_check_diagnostics.is_empty() { Ok(_) => {}
report_diagnostics(&type_check_diagnostics); Err(diagnostics) => {
report_diagnostics(&diagnostics);
}
} }
let mut ir_functions = compilation_unit.to_ir(&symbol_table); let mut ir_functions = compilation_unit.to_ir(&symbol_table);