Continue progress, make set-statements as todos for later

This commit is contained in:
Sofia 2025-07-13 19:00:46 +03:00
parent 10cf9341c5
commit a18a5b4199
10 changed files with 102 additions and 79 deletions

View File

@ -133,11 +133,11 @@ pub struct Block(
); );
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct VariableReference(VariableReferenceKind, TokenRange); pub struct VariableReference(pub VariableReferenceKind, pub TokenRange);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum VariableReferenceKind { pub enum VariableReferenceKind {
Name(String), Name(String, TokenRange),
Index(Box<VariableReference>, u64), Index(Box<VariableReference>, u64),
} }

View File

@ -384,7 +384,7 @@ impl Parse for VariableReference {
fn parse(mut stream: TokenStream) -> Result<Self, Error> { fn parse(mut stream: TokenStream) -> Result<Self, Error> {
if let Some(Token::Identifier(ident)) = stream.next() { if let Some(Token::Identifier(ident)) = stream.next() {
let mut var_ref = VariableReference( let mut var_ref = VariableReference(
VariableReferenceKind::Name(ident), VariableReferenceKind::Name(ident, stream.get_one_token_range()),
stream.get_range().unwrap(), stream.get_range().unwrap(),
); );

View File

@ -76,14 +76,7 @@ impl ast::Block {
s_let.4, s_let.4,
), ),
ast::BlockLevelStatement::Set(var_ref, expression, range) => ( ast::BlockLevelStatement::Set(var_ref, expression, range) => (
StmtKind::Set( StmtKind::Set(var_ref.process(), expression.process()),
NamedVariableRef(
mir::TypeKind::Vague(mir::VagueType::Unknown),
todo!(), // was name.clone()
(*range).into(),
),
expression.process(),
),
*range, *range,
), ),
ast::BlockLevelStatement::Import { _i } => todo!(), ast::BlockLevelStatement::Import { _i } => todo!(),
@ -119,16 +112,25 @@ impl From<ast::ReturnType> for mir::ReturnKind {
impl ast::VariableReference { impl ast::VariableReference {
fn process(&self) -> mir::IndexedVariableReference { fn process(&self) -> mir::IndexedVariableReference {
match &self.0 { mir::IndexedVariableReference {
ast::VariableReferenceKind::Name(name) => { kind: self.0.process(),
mir::IndexedVariableReference::Named(NamedVariableRef( meta: self.1.into(),
}
}
}
impl ast::VariableReferenceKind {
fn process(&self) -> mir::IndexedVariableReferenceKind {
match &self {
ast::VariableReferenceKind::Name(name, range) => {
mir::IndexedVariableReferenceKind::Named(NamedVariableRef(
mir::TypeKind::Vague(mir::VagueType::Unknown), mir::TypeKind::Vague(mir::VagueType::Unknown),
name.clone(), name.clone(),
self.1.into(), (*range).into(),
)) ))
} }
ast::VariableReferenceKind::Index(var_ref, idx) => { ast::VariableReferenceKind::Index(var_ref, idx) => {
mir::IndexedVariableReference::Index(Box::new(var_ref.process()), *idx) mir::IndexedVariableReferenceKind::Index(Box::new(var_ref.process()), *idx)
} }
} }
} }
@ -166,7 +168,9 @@ impl ast::Expression {
}; };
mir::ExprKind::If(mir::IfExpression(Box::new(cond), then_block, else_block)) mir::ExprKind::If(mir::IfExpression(Box::new(cond), then_block, else_block))
} }
ast::ExpressionKind::Array(expressions) => todo!("process for array expression"), ast::ExpressionKind::Array(expressions) => {
mir::ExprKind::Array(expressions.iter().map(|e| e.process()).collect())
}
ast::ExpressionKind::Index(expression, idx) => { ast::ExpressionKind::Index(expression, idx) => {
mir::ExprKind::Index(Box::new(expression.process()), *idx) mir::ExprKind::Index(Box::new(expression.process()), *idx)
} }

View File

@ -178,19 +178,20 @@ impl mir::Statement {
None None
} }
mir::StmtKind::Set(var, val) => { mir::StmtKind::Set(var, val) => {
if let Some(StackValue(kind, _)) = scope.stack_values.get(&var.1).cloned() { todo!("Re-think how set needs to work with arrays");
match kind { // if let Some(StackValue(kind, _)) = scope.stack_values.get(&var.1).cloned() {
StackValueKind::Immutable(_) => { // match kind {
panic!("Tried to mutate an immutable variable") // StackValueKind::Immutable(_) => {
} // panic!("Tried to mutate an immutable variable")
StackValueKind::Mutable(ptr) => { // }
let expression = val.codegen(scope).unwrap(); // StackValueKind::Mutable(ptr) => {
Some(scope.block.build(Instr::Store(ptr, expression)).unwrap()) // let expression = val.codegen(scope).unwrap();
} // Some(scope.block.build(Instr::Store(ptr, expression)).unwrap())
} // }
} else { // }
panic!("") // } else {
} // panic!("")
// }
} }
// mir::StmtKind::If(if_expression) => if_expression.codegen(scope), // mir::StmtKind::If(if_expression) => if_expression.codegen(scope),
mir::StmtKind::Import(_) => todo!(), mir::StmtKind::Import(_) => todo!(),

View File

@ -180,9 +180,9 @@ impl Display for NamedVariableRef {
impl Display for IndexedVariableReference { impl Display for IndexedVariableReference {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match &self.kind {
IndexedVariableReference::Named(name) => Display::fmt(name, f), IndexedVariableReferenceKind::Named(name) => Display::fmt(name, f),
IndexedVariableReference::Index(variable_reference_kind, idx) => { IndexedVariableReferenceKind::Index(variable_reference_kind, idx) => {
Display::fmt(&variable_reference_kind, f)?; Display::fmt(&variable_reference_kind, f)?;
write_index(f, *idx) write_index(f, *idx)
} }

View File

@ -273,7 +273,14 @@ pub struct Block {
#[derive(Debug)] #[derive(Debug)]
pub struct Statement(pub StmtKind, pub Metadata); pub struct Statement(pub StmtKind, pub Metadata);
pub enum IndexedVariableReference { #[derive(Debug)]
pub struct IndexedVariableReference {
pub kind: IndexedVariableReferenceKind,
pub meta: Metadata,
}
#[derive(Debug)]
pub enum IndexedVariableReferenceKind {
Named(NamedVariableRef), Named(NamedVariableRef),
Index(Box<IndexedVariableReference>, u64), Index(Box<IndexedVariableReference>, u64),
} }
@ -282,7 +289,7 @@ pub enum IndexedVariableReference {
pub enum StmtKind { pub enum StmtKind {
/// Variable name++mutability+type, evaluation /// Variable name++mutability+type, evaluation
Let(NamedVariableRef, bool, Expression), Let(NamedVariableRef, bool, Expression),
Set(NamedVariableRef, Expression), Set(IndexedVariableReference, Expression),
Import(Import), Import(Import),
Expression(Expression), Expression(Expression),
} }

View File

@ -165,40 +165,41 @@ impl Block {
None None
} }
StmtKind::Set(variable_reference, expression) => { StmtKind::Set(variable_reference, expression) => {
if let Some(var) = state.scope.variables.get(&variable_reference.1).cloned() { todo!("Re-think how set needs to work with arrays")
// Typecheck expression and coerce to variable type // if let Some(var) = state.scope.variables.get(&variable_reference.1).cloned() {
let res = expression.typecheck(&mut state, &hints, Some(&var.ty)); // // Typecheck expression and coerce to variable type
// let res = expression.typecheck(&mut state, &hints, Some(&var.ty));
// If expression resolution itself was erronous, resolve as // // If expression resolution itself was erronous, resolve as
// Unknown. // // Unknown.
let expr_ty = state.or_else(res, Vague(Unknown), expression.1); // let expr_ty = state.or_else(res, Vague(Unknown), expression.1);
// Make sure the expression and variable type to really // // Make sure the expression and variable type to really
// be the same // // be the same
let res_t = state.or_else( // let res_t = state.or_else(
expr_ty.collapse_into(&variable_reference.0.resolve_hinted(&hints)), // expr_ty.collapse_into(&variable_reference.0.resolve_hinted(&hints)),
Vague(Unknown), // Vague(Unknown),
variable_reference.2 + expression.1, // variable_reference.2 + expression.1,
); // );
// Update typing to be more accurate // // Update typing to be more accurate
variable_reference.0 = res_t; // variable_reference.0 = res_t;
if !var.mutable { // if !var.mutable {
state.ok::<_, Infallible>( // state.ok::<_, Infallible>(
Err(ErrorKind::VariableNotMutable(variable_reference.1.clone())), // Err(ErrorKind::VariableNotMutable(variable_reference.1.clone())),
variable_reference.2, // variable_reference.2,
); // );
} // }
None // None
} else { // } else {
state.ok::<_, Infallible>( // state.ok::<_, Infallible>(
Err(ErrorKind::VariableNotDefined(variable_reference.1.clone())), // Err(ErrorKind::VariableNotDefined(variable_reference.1.clone())),
variable_reference.2, // variable_reference.2,
); // );
None // None
} // }
} }
StmtKind::Import(_) => todo!(), // TODO StmtKind::Import(_) => todo!(), // TODO
StmtKind::Expression(expression) => { StmtKind::Expression(expression) => {

View File

@ -111,23 +111,24 @@ impl Block {
} }
} }
StmtKind::Set(var, expr) => { StmtKind::Set(var, expr) => {
// Get the TypeRef for this variable declaration todo!("Re-think how set needs to work with arrays")
let var_ref = inner_hints.find_hint(&var.1); // // Get the TypeRef for this variable declaration
// let var_ref = inner_hints.find_hint(&var.1);
// If ok, update the MIR type to this TypeRef // // If ok, update the MIR type to this TypeRef
if let Some((_, var_ref)) = &var_ref { // if let Some((_, var_ref)) = &var_ref {
var.0 = var_ref.as_type() // var.0 = var_ref.as_type()
} // }
// Infer hints for the expression itself // // Infer hints for the expression itself
let inferred = expr.infer_types(&mut state, &inner_hints); // let inferred = expr.infer_types(&mut state, &inner_hints);
let expr_ty_ref = state.ok(inferred, expr.1); // let expr_ty_ref = state.ok(inferred, expr.1);
// Try to narrow the variable type declaration with the // // Try to narrow the variable type declaration with the
// expression // // expression
if let (Some((_, mut var_ref)), Some(expr_ty_ref)) = (var_ref, expr_ty_ref) { // if let (Some((_, mut var_ref)), Some(expr_ty_ref)) = (var_ref, expr_ty_ref) {
var_ref.narrow(&expr_ty_ref); // var_ref.narrow(&expr_ty_ref);
} // }
} }
StmtKind::Import(_) => todo!(), StmtKind::Import(_) => todo!(),
StmtKind::Expression(expr) => { StmtKind::Expression(expr) => {

View File

@ -1,3 +1,5 @@
use crate::ast::VariableReference;
use super::*; use super::*;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -64,7 +66,7 @@ impl ReturnType for Statement {
), ),
Set(var, expr) => if_hard( Set(var, expr) => if_hard(
expr.return_type()?, expr.return_type()?,
Err(ReturnTypeOther::Set(var.2 + expr.1)), Err(ReturnTypeOther::Set(var.meta + expr.1)),
), ),
Import(_) => todo!(), Import(_) => todo!(),
Expression(expression) => expression.return_type(), Expression(expression) => expression.return_type(),

View File

@ -153,6 +153,13 @@ impl<'a, 'b> TokenStream<'a, 'b> {
end: self.position, end: self.position,
}) })
} }
pub fn get_one_token_range(&self) -> TokenRange {
TokenRange {
start: self.position - 1,
end: self.position,
}
}
} }
impl Drop for TokenStream<'_, '_> { impl Drop for TokenStream<'_, '_> {