Simplify transformation from AST to MIR

This commit is contained in:
Sofia 2025-07-06 23:01:28 +03:00
parent 48ae533f33
commit 0932af2e3b
4 changed files with 139 additions and 298 deletions

View File

@ -3,93 +3,50 @@ use std::collections::HashMap;
use crate::{ use crate::{
ast, ast,
mir::{self, StmtKind, VariableReference}, mir::{self, StmtKind, VariableReference},
token_stream::TokenRange,
}; };
#[derive(Clone)] #[derive(Clone)]
pub enum InferredType { pub enum InferredType {
FromVariable(String, TokenRange), FromVariable(String),
FunctionReturn(String, TokenRange), FunctionReturn(String),
Static(mir::TypeKind, TokenRange), Static(mir::TypeKind),
OneOf(Vec<InferredType>), OneOf(Vec<InferredType>),
Void(TokenRange), Void,
DownstreamError(IntoMIRError, TokenRange),
}
fn all_ok<T, E>(result: Vec<Result<T, E>>) -> Option<Vec<T>> {
let mut res = Vec::with_capacity(result.len());
for item in result {
if let Ok(item) = item {
res.push(item);
} else {
return None;
}
}
Some(res)
} }
impl InferredType { impl InferredType {
fn collapse( fn collapse(&self, scope: &VirtualScope) -> mir::TypeKind {
&self,
state: &mut State,
scope: &VirtualScope,
) -> Result<mir::TypeKind, IntoMIRError> {
match self { match self {
InferredType::FromVariable(name, token_range) => { InferredType::FromVariable(name) => {
if let Some(inferred) = scope.get_var(name) { if let Some(inferred) = scope.get_var(name) {
let temp = inferred.collapse(state, scope); inferred.collapse(scope)
state.note(temp)
} else { } else {
state.err(IntoMIRError::VariableNotDefined(name.clone(), *token_range)) mir::TypeKind::Vague(mir::VagueType::Unknown)
} }
} }
InferredType::FunctionReturn(name, token_range) => { InferredType::FunctionReturn(name) => {
if let Some(type_kind) = scope.get_return_type(name) { if let Some(type_kind) = scope.get_return_type(name) {
Ok(*type_kind) type_kind.clone()
} else { } else {
state.err(IntoMIRError::VariableNotDefined(name.clone(), *token_range)) mir::TypeKind::Vague(mir::VagueType::Unknown)
} }
} }
InferredType::Static(type_kind, _) => Ok(*type_kind), InferredType::Static(type_kind) => type_kind.clone(),
InferredType::OneOf(inferred_types) => { InferredType::OneOf(inferred_types) => {
let collapsed = all_ok( let list: Vec<mir::TypeKind> =
inferred_types inferred_types.iter().map(|t| t.collapse(scope)).collect();
.iter()
.map(|t| {
let temp = t.collapse(state, scope);
state.note(temp)
})
.collect(),
);
if let Some(list) = collapsed {
if let Some(first) = list.first() { if let Some(first) = list.first() {
if list.iter().all(|i| i == first) { if list.iter().all(|i| i == first) {
Ok((*first).into()) first.clone().into()
} else { } else {
state.err(IntoMIRError::ConflictingType(self.get_range())) // IntoMIRError::ConflictingType(self.get_range())
mir::TypeKind::Void
} }
} else { } else {
state.err(IntoMIRError::VoidType(self.get_range())) mir::TypeKind::Void
}
} else {
state.err(IntoMIRError::DownstreamError(self.get_range()))
} }
} }
InferredType::Void(token_range) => state.err(IntoMIRError::VoidType(*token_range)), InferredType::Void => mir::TypeKind::Void,
InferredType::DownstreamError(e, _) => state.err(e.clone()),
}
}
fn get_range(&self) -> TokenRange {
match &self {
InferredType::FromVariable(_, token_range) => *token_range,
InferredType::FunctionReturn(_, token_range) => *token_range,
InferredType::Static(_, token_range) => *token_range,
InferredType::OneOf(inferred_types) => {
inferred_types.iter().map(|i| i.get_range()).sum()
}
InferredType::Void(token_range) => *token_range,
InferredType::DownstreamError(_, range) => *range,
} }
} }
} }
@ -97,18 +54,12 @@ impl InferredType {
pub struct VirtualVariable { pub struct VirtualVariable {
name: String, name: String,
inferred: InferredType, inferred: InferredType,
meta: mir::Metadata,
} }
pub struct VirtualFunctionSignature { pub struct VirtualFunctionSignature {
name: String, name: String,
return_type: mir::TypeKind, return_type: mir::TypeKind,
parameter_types: Vec<mir::TypeKind>, parameter_types: Vec<mir::TypeKind>,
metadata: mir::Metadata,
}
pub enum VirtualStorageError {
KeyAlreadyExists(String),
} }
pub struct VirtualStorage<T> { pub struct VirtualStorage<T> {
@ -116,24 +67,16 @@ pub struct VirtualStorage<T> {
} }
impl<T> VirtualStorage<T> { impl<T> VirtualStorage<T> {
fn set(&mut self, name: String, value: T) -> Result<(), VirtualStorageError> { fn set(&mut self, name: String, value: T) {
let result = if let Some(list) = self.storage.get_mut(&name) { if let Some(list) = self.storage.get_mut(&name) {
list.push(value); list.push(value);
Err(VirtualStorageError::KeyAlreadyExists(name.clone()))
} else { } else {
self.storage.insert(name, vec![value]); self.storage.insert(name, vec![value]);
Ok(())
}; };
result
} }
fn get(&self, name: &String) -> Option<&T> { fn get(&self, name: &String) -> Option<&Vec<T>> {
if let Some(list) = self.storage.get(name) { self.storage.get(name)
list.first()
} else {
None
}
} }
} }
@ -145,49 +88,44 @@ impl<T> Default for VirtualStorage<T> {
} }
} }
#[derive(Clone, Debug)]
pub enum IntoMIRError {
DuplicateVariable(String, TokenRange),
DuplicateFunction(String, TokenRange),
VariableNotDefined(String, TokenRange),
FunctionNotDefined(String, TokenRange),
DownstreamError(TokenRange),
ConflictingType(TokenRange),
VoidType(TokenRange),
}
pub struct VirtualScope { pub struct VirtualScope {
variables: VirtualStorage<VirtualVariable>, variables: VirtualStorage<VirtualVariable>,
functions: VirtualStorage<VirtualFunctionSignature>, functions: VirtualStorage<VirtualFunctionSignature>,
} }
impl VirtualScope { impl VirtualScope {
pub fn set_var(&mut self, variable: VirtualVariable) -> Result<(), IntoMIRError> { pub fn set_var(&mut self, variable: VirtualVariable) {
let range = variable.meta.range; self.variables.set(variable.name.clone(), variable);
match self.variables.set(variable.name.clone(), variable) {
Ok(_) => Ok(()),
Err(VirtualStorageError::KeyAlreadyExists(n)) => {
Err(IntoMIRError::DuplicateVariable(n, range))
}
}
} }
pub fn set_fun(&mut self, function: VirtualFunctionSignature) -> Result<(), IntoMIRError> { pub fn set_fun(&mut self, function: VirtualFunctionSignature) {
let range = function.metadata.range; self.functions.set(function.name.clone(), function)
match self.functions.set(function.name.clone(), function) {
Ok(_) => Ok(()),
Err(VirtualStorageError::KeyAlreadyExists(n)) => {
Err(IntoMIRError::DuplicateVariable(n, range))
}
}
} }
pub fn get_var(&self, name: &String) -> Option<&InferredType> { pub fn get_var(&self, name: &String) -> Option<InferredType> {
self.variables.get(name).map(|v| &v.inferred) self.variables.get(name).and_then(|v| {
if v.len() > 1 {
Some(InferredType::OneOf(
v.iter().map(|v| v.inferred.clone()).collect(),
))
} else if let Some(v) = v.first() {
Some(v.inferred.clone())
} else {
None
}
})
} }
pub fn get_return_type(&self, name: &String) -> Option<&mir::TypeKind> { pub fn get_return_type(&self, name: &String) -> Option<mir::TypeKind> {
self.functions.get(name).map(|v| &v.return_type) self.functions.get(name).and_then(|v| {
if v.len() > 1 {
Some(mir::TypeKind::Vague(mir::VagueType::Unknown))
} else if let Some(v) = v.first() {
Some(v.return_type.clone())
} else {
None
}
})
} }
} }
@ -200,53 +138,18 @@ impl Default for VirtualScope {
} }
} }
#[derive(Debug)]
pub struct State {
errors: Vec<IntoMIRError>,
fatal: bool,
}
impl State {
fn note<T: std::fmt::Debug>(
&mut self,
value: Result<T, IntoMIRError>,
) -> Result<T, IntoMIRError> {
dbg!(&value);
if let Err(e) = &value {
self.errors.push(e.clone());
}
value
}
fn err<T>(&mut self, error: IntoMIRError) -> Result<T, IntoMIRError> {
self.errors.push(error.clone());
Err(error)
}
}
impl Default for State {
fn default() -> Self {
Self {
errors: Default::default(),
fatal: false,
}
}
}
impl ast::Module { impl ast::Module {
pub fn process(&self) -> mir::Module { pub fn process(&self) -> mir::Module {
let mut state = State::default();
let mut scope = VirtualScope::default(); let mut scope = VirtualScope::default();
for stmt in &self.top_level_statements { for stmt in &self.top_level_statements {
match stmt { match stmt {
FunctionDefinition(ast::FunctionDefinition(signature, _, range)) => { FunctionDefinition(ast::FunctionDefinition(signature, _, _)) => {
state.note(scope.set_fun(VirtualFunctionSignature { scope.set_fun(VirtualFunctionSignature {
name: signature.name.clone(), name: signature.name.clone(),
return_type: signature.return_type.into(), return_type: signature.return_type.into(),
parameter_types: signature.args.iter().map(|p| p.1.into()).collect(), parameter_types: signature.args.iter().map(|p| p.1.into()).collect(),
metadata: (*range).into(), });
}));
} }
_ => {} _ => {}
} }
@ -265,16 +168,12 @@ impl ast::Module {
} }
FunctionDefinition(ast::FunctionDefinition(signature, block, range)) => { FunctionDefinition(ast::FunctionDefinition(signature, block, range)) => {
for (name, ptype) in &signature.args { for (name, ptype) in &signature.args {
state.note(scope.set_var(VirtualVariable { scope.set_var(VirtualVariable {
name: name.clone(), name: name.clone(),
inferred: InferredType::Static((*ptype).into(), *range), inferred: InferredType::Static((*ptype).into()),
meta: ptype.1.into(), });
}));
} }
dbg!(&signature);
if let Some(mir_block) = block.process(&mut state, &mut scope) {
let def = mir::FunctionDefinition { let def = mir::FunctionDefinition {
name: signature.name.clone(), name: signature.name.clone(),
parameters: signature parameters: signature
@ -283,15 +182,15 @@ impl ast::Module {
.cloned() .cloned()
.map(|p| (p.0, p.1.into())) .map(|p| (p.0, p.1.into()))
.collect(), .collect(),
kind: mir::FunctionDefinitionKind::Local(mir_block, (*range).into()), kind: mir::FunctionDefinitionKind::Local(
block.into_mir(&mut scope),
(*range).into(),
),
}; };
functions.push(def); functions.push(def);
} }
} }
} }
}
dbg!(&state);
// TODO do something with state here // TODO do something with state here
@ -304,79 +203,57 @@ impl ast::Module {
} }
impl ast::Block { impl ast::Block {
pub fn process(&self, state: &mut State, scope: &mut VirtualScope) -> Option<mir::Block> { pub fn into_mir(&self, scope: &mut VirtualScope) -> mir::Block {
let mut mir_statements = Vec::new(); let mut mir_statements = Vec::new();
for statement in &self.0 { for statement in &self.0 {
let (kind, range): (Option<mir::StmtKind>, TokenRange) = match statement { let (kind, range) = match statement {
ast::BlockLevelStatement::Let(s_let) => { ast::BlockLevelStatement::Let(s_let) => {
let res = s_let.1.infer_return_type().collapse(state, scope); let t = s_let.1.infer_return_type().collapse(scope);
let collapsed = state.note(res); let inferred = InferredType::Static(t.clone());
let inferred = match &collapsed { scope.set_var(VirtualVariable {
Ok(t) => InferredType::Static(*t, s_let.2),
Err(e) => InferredType::DownstreamError(e.clone(), s_let.2),
};
state
.note(scope.set_var(VirtualVariable {
name: s_let.0.clone(), name: s_let.0.clone(),
inferred, inferred,
meta: s_let.2.into(), });
}))
.ok();
( (
collapsed.ok().and_then(|t| {
s_let.1.process(state, scope).map(|e| {
mir::StmtKind::Let( mir::StmtKind::Let(
mir::VariableReference(t, s_let.0.clone(), s_let.2.into()), mir::VariableReference(t, s_let.0.clone(), s_let.2.into()),
e, s_let.1.process(scope),
) ),
})
}),
s_let.2, s_let.2,
) )
} }
ast::BlockLevelStatement::Import(_) => todo!(), ast::BlockLevelStatement::Import(_) => todo!(),
ast::BlockLevelStatement::Expression(e) => ( ast::BlockLevelStatement::Expression(e) => {
e.process(state, scope).map(|e| StmtKind::Expression(e)), (StmtKind::Expression(e.process(scope)), e.1)
e.1, }
), ast::BlockLevelStatement::Return(_, e) => {
ast::BlockLevelStatement::Return(_, e) => ( (StmtKind::Expression(e.process(scope)), e.1)
e.process(state, scope).map(|e| StmtKind::Expression(e)), }
e.1,
),
}; };
if let Some(kind) = kind {
mir_statements.push(mir::Statement(kind, range.into())); mir_statements.push(mir::Statement(kind, range.into()));
} else {
state.fatal = true;
}
} }
let return_expression = if let Some(r) = &self.1 { let return_expression = if let Some(r) = &self.1 {
if let Some(expr) = r.1.process(state, scope) { Some((r.0.into(), Box::new(r.1.process(scope))))
Some((r.0.into(), Box::new(expr)))
} else {
state.fatal = true;
None?
}
} else { } else {
None None
}; };
Some(mir::Block { mir::Block {
statements: mir_statements, statements: mir_statements,
return_expression, return_expression,
meta: self.2.into(), meta: self.2.into(),
}) }
} }
fn infer_return_type(&self) -> InferredType { fn infer_return_type(&self) -> InferredType {
self.1 self.1
.as_ref() .as_ref()
.map(|(_, expr)| expr.infer_return_type()) .map(|(_, expr)| expr.infer_return_type())
.unwrap_or(InferredType::Void(self.2)) .unwrap_or(InferredType::Void)
} }
} }
@ -390,102 +267,59 @@ impl From<ast::ReturnType> for mir::ReturnKind {
} }
impl ast::Expression { impl ast::Expression {
fn process(&self, state: &mut State, scope: &mut VirtualScope) -> Option<mir::Expression> { fn process(&self, scope: &mut VirtualScope) -> mir::Expression {
let kind = match &self.0 { let kind = match &self.0 {
ast::ExpressionKind::VariableName(name) => { ast::ExpressionKind::VariableName(name) => mir::ExprKind::Variable(VariableReference(
let ty = scope.get_var(name); if let Some(ty) = scope.get_var(name) {
if let Some(ty) = ty { ty.collapse(scope)
let res = ty.collapse(state, scope);
state
.note(res)
.map(|result| {
mir::ExprKind::Variable(VariableReference(
result,
name.clone(),
self.1.into(),
))
})
.ok()
} else { } else {
state mir::TypeKind::Vague(mir::VagueType::Unknown)
.err(IntoMIRError::VariableNotDefined( },
name.clone(), name.clone(),
self.1.into(), self.1.into(),
)) )),
.ok() ast::ExpressionKind::Literal(literal) => mir::ExprKind::Literal(literal.mir()),
} ast::ExpressionKind::Binop(binary_operator, lhs, rhs) => mir::ExprKind::BinOp(
}
ast::ExpressionKind::Literal(literal) => Some(mir::ExprKind::Literal(literal.mir())),
ast::ExpressionKind::Binop(binary_operator, lhs, rhs) => {
let mir_lhs = lhs.process(state, scope);
let mir_rhs = rhs.process(state, scope);
Some(mir::ExprKind::BinOp(
binary_operator.mir(), binary_operator.mir(),
Box::new(mir_lhs?), Box::new(lhs.process(scope)),
Box::new(mir_rhs?), Box::new(rhs.process(scope)),
)) ),
}
ast::ExpressionKind::FunctionCall(fn_call_expr) => { ast::ExpressionKind::FunctionCall(fn_call_expr) => {
if let Some(fn_type) = scope.get_return_type(&fn_call_expr.0).cloned() { mir::ExprKind::FunctionCall(mir::FunctionCall {
let parameters = all_ok(
fn_call_expr
.1
.iter()
.map(|e| {
e.process(state, scope)
.ok_or(IntoMIRError::DownstreamError(self.1.into()))
})
.collect(),
);
if let Some(parameters) = parameters {
Some(mir::ExprKind::FunctionCall(mir::FunctionCall {
name: fn_call_expr.0.clone(), name: fn_call_expr.0.clone(),
return_type: fn_type, return_type: if let Some(r_type) = scope.get_return_type(&fn_call_expr.0) {
parameters, r_type
}))
} else { } else {
None mir::TypeKind::Vague(mir::VagueType::Unknown)
} },
} else { parameters: fn_call_expr.1.iter().map(|e| e.process(scope)).collect(),
state })
.err(IntoMIRError::FunctionNotDefined(
fn_call_expr.0.clone(),
self.1,
))
.ok()
}
}
ast::ExpressionKind::BlockExpr(block) => {
block.process(state, scope).map(|b| mir::ExprKind::Block(b))
} }
ast::ExpressionKind::BlockExpr(block) => mir::ExprKind::Block(block.into_mir(scope)),
ast::ExpressionKind::IfExpr(if_expression) => { ast::ExpressionKind::IfExpr(if_expression) => {
let cond = if_expression.0.process(state, scope); let cond = if_expression.0.process(scope);
let then_block = if_expression.1.process(state, scope); let then_block = if_expression.1.into_mir(scope);
let else_block = if let Some(el) = &if_expression.2 { let else_block = if let Some(el) = &if_expression.2 {
Some(el.process(state, scope)?) Some(el.into_mir(scope))
} else { } else {
None None
}; };
Some(mir::ExprKind::If(mir::IfExpression( mir::ExprKind::If(mir::IfExpression(Box::new(cond), then_block, else_block))
Box::new(cond?),
then_block?,
else_block,
)))
} }
}; };
kind.map(|k| mir::Expression(k, self.1.into())) mir::Expression(kind, self.1.into())
} }
fn infer_return_type(&self) -> InferredType { fn infer_return_type(&self) -> InferredType {
use ast::ExpressionKind::*; use ast::ExpressionKind::*;
match &self.0 { match &self.0 {
VariableName(name) => InferredType::FromVariable(name.clone(), self.1), VariableName(name) => InferredType::FromVariable(name.clone()),
Literal(lit) => InferredType::Static(lit.mir().as_type(), self.1), Literal(lit) => InferredType::Static(lit.mir().as_type()),
Binop(_, lhs, rhs) => { Binop(_, lhs, rhs) => {
InferredType::OneOf(vec![lhs.infer_return_type(), rhs.infer_return_type()]) InferredType::OneOf(vec![lhs.infer_return_type(), rhs.infer_return_type()])
} }
FunctionCall(fncall) => InferredType::FunctionReturn(fncall.0.clone(), self.1), FunctionCall(fncall) => InferredType::FunctionReturn(fncall.0.clone()),
BlockExpr(block) => block.infer_return_type(), BlockExpr(block) => block.infer_return_type(),
IfExpr(exp) => { IfExpr(exp) => {
let mut types = vec![exp.1.infer_return_type()]; let mut types = vec![exp.1.infer_return_type()];

View File

@ -86,7 +86,7 @@ impl<'ctx, 'a> Scope<'ctx, 'a> {
function: self.function, function: self.function,
context: self.context, context: self.context,
module: self.module, module: self.module,
functions: self.functions.clone(), functions: self.functions,
stack_values: self.stack_values.clone(), stack_values: self.stack_values.clone(),
} }
} }
@ -295,6 +295,7 @@ impl TypeKind {
TypeKind::I32 => Type::I32, TypeKind::I32 => Type::I32,
TypeKind::I16 => Type::I16, TypeKind::I16 => Type::I16,
TypeKind::Void => panic!("Void not a supported type"), TypeKind::Void => panic!("Void not a supported type"),
TypeKind::Vague(_) => panic!("Tried to compile a vague type!"),
} }
} }
} }

View File

@ -29,6 +29,12 @@ pub enum TypeKind {
I32, I32,
I16, I16,
Void, Void,
Vague(VagueType),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VagueType {
Unknown,
} }
impl TypeKind { impl TypeKind {

View File

@ -54,13 +54,13 @@ impl ReturnType for IfExpression {
impl ReturnType for VariableReference { impl ReturnType for VariableReference {
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> { fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
Ok(self.0) Ok(self.0.clone())
} }
} }
impl ReturnType for FunctionCall { impl ReturnType for FunctionCall {
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> { fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
Ok(self.return_type) Ok(self.return_type.clone())
} }
} }
@ -68,7 +68,7 @@ impl ReturnType for FunctionDefinition {
fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> { fn return_type(&self) -> Result<TypeKind, ReturnTypeOther> {
match &self.kind { match &self.kind {
FunctionDefinitionKind::Local(block, _) => block.return_type(), FunctionDefinitionKind::Local(block, _) => block.return_type(),
FunctionDefinitionKind::Extern(type_kind) => Ok(*type_kind), FunctionDefinitionKind::Extern(type_kind) => Ok(type_kind.clone()),
} }
} }
} }