Make for-loops syntax sugar instead
This commit is contained in:
parent
1a5e3ef1d9
commit
75a7a435d1
@ -3,8 +3,8 @@ use std::path::PathBuf;
|
||||
use crate::{
|
||||
ast::{self},
|
||||
mir::{
|
||||
self, CustomTypeKey, ForStatement, ModuleMap, NamedVariableRef, SourceModuleId, StmtKind,
|
||||
StructField, StructType, WhileStatement,
|
||||
self, CustomTypeKey, ModuleMap, NamedVariableRef, SourceModuleId, StmtKind, StructField,
|
||||
StructType, WhileStatement,
|
||||
},
|
||||
};
|
||||
|
||||
@ -148,20 +148,64 @@ impl ast::Block {
|
||||
ast::BlockLevelStatement::Return(_, e) => {
|
||||
(StmtKind::Expression(e.process(module_id)), e.1)
|
||||
}
|
||||
ast::BlockLevelStatement::ForLoop(counter, counter_range, start, end, block) => (
|
||||
StmtKind::For(ForStatement {
|
||||
counter: NamedVariableRef(
|
||||
mir::TypeKind::Vague(mir::VagueType::Unknown),
|
||||
counter.clone(),
|
||||
counter_range.as_meta(module_id),
|
||||
ast::BlockLevelStatement::ForLoop(counter, counter_range, start, end, block) => {
|
||||
let counter_var = NamedVariableRef(
|
||||
mir::TypeKind::Vague(mir::VagueType::Unknown),
|
||||
counter.clone(),
|
||||
counter_range.as_meta(module_id),
|
||||
);
|
||||
let let_statement = mir::Statement(
|
||||
StmtKind::Let(counter_var.clone(), true, start.process(module_id)),
|
||||
counter_range.as_meta(module_id),
|
||||
);
|
||||
mir_statements.push(let_statement);
|
||||
|
||||
let set_new = mir::Statement(
|
||||
StmtKind::Set(
|
||||
mir::Expression(
|
||||
mir::ExprKind::Variable(counter_var.clone()),
|
||||
counter_range.as_meta(module_id),
|
||||
),
|
||||
mir::Expression(
|
||||
mir::ExprKind::BinOp(
|
||||
mir::BinaryOperator::Add,
|
||||
Box::new(mir::Expression(
|
||||
mir::ExprKind::Variable(counter_var.clone()),
|
||||
counter_range.as_meta(module_id),
|
||||
)),
|
||||
Box::new(mir::Expression(
|
||||
mir::ExprKind::Literal(mir::Literal::Vague(
|
||||
mir::VagueLiteral::Number(1),
|
||||
)),
|
||||
counter_range.as_meta(module_id),
|
||||
)),
|
||||
),
|
||||
counter_range.as_meta(module_id),
|
||||
),
|
||||
),
|
||||
start: start.process(module_id),
|
||||
end: end.process(module_id),
|
||||
block: block.into_mir(module_id),
|
||||
meta: self.2.as_meta(module_id),
|
||||
}),
|
||||
self.2,
|
||||
),
|
||||
counter_range.as_meta(module_id),
|
||||
);
|
||||
let mut block = block.into_mir(module_id);
|
||||
block.statements.insert(0, set_new);
|
||||
(
|
||||
StmtKind::While(WhileStatement {
|
||||
condition: mir::Expression(
|
||||
mir::ExprKind::BinOp(
|
||||
mir::BinaryOperator::Cmp(mir::CmpOperator::LT),
|
||||
Box::new(mir::Expression(
|
||||
mir::ExprKind::Variable(counter_var),
|
||||
counter_range.as_meta(module_id),
|
||||
)),
|
||||
Box::new(end.process(module_id)),
|
||||
),
|
||||
counter_range.as_meta(module_id),
|
||||
),
|
||||
block,
|
||||
meta: self.2.as_meta(module_id),
|
||||
}),
|
||||
self.2,
|
||||
)
|
||||
}
|
||||
ast::BlockLevelStatement::WhileLoop(expression, block) => (
|
||||
StmtKind::While(WhileStatement {
|
||||
condition: expression.process(module_id),
|
||||
|
@ -598,8 +598,7 @@ impl mir::Statement {
|
||||
}
|
||||
mir::StmtKind::Import(_) => todo!(),
|
||||
mir::StmtKind::Expression(expression) => expression.codegen(scope, state),
|
||||
mir::StmtKind::For(for_statement) => todo!(),
|
||||
mir::StmtKind::While(while_statement) => todo!(),
|
||||
mir::StmtKind::While(_) => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -159,16 +159,7 @@ impl Display for StmtKind {
|
||||
StmtKind::Set(var, expr) => write!(f, "{} = {}", var, expr),
|
||||
StmtKind::Import(n) => write!(f, "import {}", n),
|
||||
StmtKind::Expression(exp) => Display::fmt(exp, f),
|
||||
StmtKind::For(for_statement) => {
|
||||
write!(
|
||||
f,
|
||||
"for {} in {} to {} {}",
|
||||
for_statement.counter,
|
||||
for_statement.start,
|
||||
for_statement.end,
|
||||
for_statement.block
|
||||
)
|
||||
}
|
||||
|
||||
StmtKind::While(while_statement) => {
|
||||
write!(
|
||||
f,
|
||||
|
@ -375,7 +375,6 @@ impl Statement {
|
||||
),
|
||||
Import(_) => todo!(),
|
||||
Expression(expression) => expression.return_type(refs, mod_id),
|
||||
For(_) => Err(ReturnTypeOther::Loop),
|
||||
While(_) => Err(ReturnTypeOther::Loop),
|
||||
}
|
||||
}
|
||||
@ -386,7 +385,6 @@ impl Statement {
|
||||
StmtKind::Set(_, _) => None,
|
||||
StmtKind::Import(_) => None,
|
||||
StmtKind::Expression(expr) => expr.backing_var(),
|
||||
StmtKind::For(_) => None,
|
||||
StmtKind::While(_) => None,
|
||||
}
|
||||
}
|
||||
|
@ -241,7 +241,7 @@ pub enum ReturnKind {
|
||||
Soft,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct NamedVariableRef(pub TypeKind, pub String, pub Metadata);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
@ -338,19 +338,9 @@ pub enum StmtKind {
|
||||
Set(Expression, Expression),
|
||||
Import(Import),
|
||||
Expression(Expression),
|
||||
For(ForStatement),
|
||||
While(WhileStatement),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ForStatement {
|
||||
pub counter: NamedVariableRef,
|
||||
pub start: Expression,
|
||||
pub end: Expression,
|
||||
pub block: Block,
|
||||
pub meta: Metadata,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WhileStatement {
|
||||
pub condition: Expression,
|
||||
|
@ -394,11 +394,6 @@ impl Statement {
|
||||
StmtKind::Expression(expression) => {
|
||||
expression.pass(pass, state, scope, mod_id)?;
|
||||
}
|
||||
StmtKind::For(for_statement) => {
|
||||
for_statement.start.pass(pass, state, scope, mod_id);
|
||||
for_statement.end.pass(pass, state, scope, mod_id);
|
||||
for_statement.block.pass(pass, state, scope, mod_id);
|
||||
}
|
||||
StmtKind::While(while_statement) => {
|
||||
while_statement.condition.pass(pass, state, scope, mod_id);
|
||||
while_statement.block.pass(pass, state, scope, mod_id);
|
||||
@ -423,7 +418,6 @@ impl Statement {
|
||||
StmtKind::Set(_, _) => {}
|
||||
StmtKind::Import(_) => {}
|
||||
StmtKind::Expression(_) => {}
|
||||
StmtKind::For(_) => {}
|
||||
StmtKind::While(_) => {}
|
||||
};
|
||||
Ok(())
|
||||
|
@ -335,7 +335,6 @@ impl Block {
|
||||
None
|
||||
}
|
||||
}
|
||||
StmtKind::For(for_statement) => todo!(),
|
||||
StmtKind::While(while_statement) => todo!(),
|
||||
};
|
||||
|
||||
|
@ -126,7 +126,6 @@ impl Block {
|
||||
let expr_res = expr.infer_types(&mut state, &inner_refs);
|
||||
state.ok(expr_res, expr.1);
|
||||
}
|
||||
StmtKind::For(for_statement) => todo!(),
|
||||
StmtKind::While(while_statement) => todo!(),
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user