Skip to content

Commit

Permalink
start of for
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-gaffney committed Jun 28, 2023
1 parent 3336473 commit 42b5093
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
7 changes: 7 additions & 0 deletions sloth/src/analysis/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ pub(super) fn propagate_types_stmt(node: &mut Stmt) -> Result<(), AnalysisError>
propagate_types(condition)?;
propagate_types_stmt(body)?;
}
StmtKind::ForStmt { iterator, identifier, body } => {
propagate_types(iterator)?;
propagate_types_stmt(body)?;
}
StmtKind::DefineVariable { value, .. } => {
propagate_types(value)?;
}
Expand Down Expand Up @@ -189,6 +193,9 @@ pub(super) fn propagate_types(node: &mut Expr) -> Result<(), AnalysisError> {
table.get_value(identifier).map(|it| it.typ).ok_or(
AnalysisError::UnknownIdentifier(node.line, identifier.to_owned()),
)?
}
ExprKind::Iterator() => {

}
ExprKind::BinaryOp { lhs, rhs, op } => {
// Propagating the types to the children
Expand Down
24 changes: 24 additions & 0 deletions sloth/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ impl<'ctx> Codegen<'ctx> {
// Position the builder at the end of the loop
self.builder.position_at_end(after_bb);
}
StmtKind::ForStmt { iterator, identifier, body } => {
// Get the current function
let func = self.current_func.unwrap();

let loop_bb = self.context.append_basic_block(func, "loop");
let body_bb = self.context.append_basic_block(func, "loop body");
let after_bb = self.context.append_basic_block(func, "after loop");

self.builder.build_unconditional_branch(loop_bb);

// Building the blocks for the head of the loop
self.builder.position_at_end(loop_bb);
let iterator = self.codegen_expr(iterator).unwrap().into_int_value();
self.builder
.build_conditional_branch(iterator, body_bb, after_bb);

// Building the blocks for the body of the loop
self.builder.position_at_end(body_bb);
self.codegen_stmt(body);
self.builder.build_unconditional_branch(loop_bb);

// Position the builder at the end of the loop
self.builder.position_at_end(after_bb);
}
StmtKind::DefineVariable {
identifier, value, ..
} => {
Expand Down
8 changes: 6 additions & 2 deletions sloth/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ impl Stmt {
children.push(condition.as_node());
children.push(body.as_node());
}
StmtKind::ForStmt { iterator, identifier, body} => {
children.push(iterator.as_node());
children.push(body.as_node());
}
StmtKind::DefineVariable { value, .. } => children.push(value.as_node()),
StmtKind::AssignVariable { value, .. } => children.push(value.as_node()),
StmtKind::DefineFunction(Function { kind, .. }) => {
Expand Down Expand Up @@ -216,10 +220,10 @@ pub enum StmtKind {
body: Box<Stmt>,
},
ForStmt {
iter: Expr,
iterator: Expr,
identifier: String,
body: Box<Stmt>,
}
},
DefineVariable {
identifier: String,
value: Expr,
Expand Down
23 changes: 23 additions & 0 deletions sloth/src/parser/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ impl GraphBuilder {
self.traverse_expr0(condition)?;
self.traverse_stmt0(body)?;
}
StmtKind::ForStmt { iterator, identifier, body } => {
writeln!(
&mut self.graph,
"N{} [shape=box label=\"ForStmt\"];",
stmt.id
)?;
self.traverse_expr0(iterator)?;
self.traverse_stmt0(body)?;
}
StmtKind::DefineVariable {
identifier,
value,
Expand Down Expand Up @@ -240,6 +249,20 @@ impl GraphBuilder {
self.traverse_expr(condition)?;
self.traverse_stmt(body)?;
}
StmtKind::ForStmt { iterator, identifier, body } => {
writeln!(
&mut self.graph,
"N{} -> N{} [label = \"Iterator\"];",
stmt.id, iterator.id
)?;
writeln!(
&mut self.graph,
"N{} -> N{} [label = \"Body\"];",
stmt.id, body.id
)?;
self.traverse_expr(iterator)?;
self.traverse_stmt(body)?;
}
StmtKind::DefineVariable { value, .. } => {
writeln!(&mut self.graph, "N{} -> N{};", stmt.id, value.id)?;
self.traverse_expr(value)?;
Expand Down
3 changes: 3 additions & 0 deletions sloth/src/symtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ pub enum Type {
Float,
Boolean,
String,
Iterator {
typ: Box<Type>,
},
Function {
inputs: Vec<Type>,
output: Box<Type>,
Expand Down

0 comments on commit 42b5093

Please sign in to comment.