aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornic-gaffney <gaffney_nic@protonmail.com>2023-06-28 14:23:09 -0500
committernic-gaffney <gaffney_nic@protonmail.com>2023-06-28 14:23:09 -0500
commit42b509365ce43e2f83475cbbc0f01bcd7b34fcd3 (patch)
tree937e69cfd0a777c88e32c049174ec6117c45b487
parent33364734df31543a3d6b7f7dbc3852edb402d9ca (diff)
downloadsloth-42b509365ce43e2f83475cbbc0f01bcd7b34fcd3.tar.gz
start of for
-rw-r--r--sloth/src/analysis/setup.rs7
-rw-r--r--sloth/src/codegen/mod.rs24
-rw-r--r--sloth/src/parser/ast.rs8
-rw-r--r--sloth/src/parser/graph.rs23
-rw-r--r--sloth/src/symtable.rs3
5 files changed, 63 insertions, 2 deletions
diff --git a/sloth/src/analysis/setup.rs b/sloth/src/analysis/setup.rs
index 16ac100..1da3df7 100644
--- a/sloth/src/analysis/setup.rs
+++ b/sloth/src/analysis/setup.rs
@@ -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)?;
}
@@ -190,6 +194,9 @@ pub(super) fn propagate_types(node: &mut Expr) -> Result<(), AnalysisError> {
AnalysisError::UnknownIdentifier(node.line, identifier.to_owned()),
)?
}
+ ExprKind::Iterator() => {
+
+ }
ExprKind::BinaryOp { lhs, rhs, op } => {
// Propagating the types to the children
propagate_types(lhs)?;
diff --git a/sloth/src/codegen/mod.rs b/sloth/src/codegen/mod.rs
index debc151..86e1ff0 100644
--- a/sloth/src/codegen/mod.rs
+++ b/sloth/src/codegen/mod.rs
@@ -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, ..
} => {
diff --git a/sloth/src/parser/ast.rs b/sloth/src/parser/ast.rs
index bae3340..a676a9b 100644
--- a/sloth/src/parser/ast.rs
+++ b/sloth/src/parser/ast.rs
@@ -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, .. }) => {
@@ -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,
diff --git a/sloth/src/parser/graph.rs b/sloth/src/parser/graph.rs
index 23f46fe..34c759f 100644
--- a/sloth/src/parser/graph.rs
+++ b/sloth/src/parser/graph.rs
@@ -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,
@@ -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)?;
diff --git a/sloth/src/symtable.rs b/sloth/src/symtable.rs
index eb3509a..549bb59 100644
--- a/sloth/src/symtable.rs
+++ b/sloth/src/symtable.rs
@@ -152,6 +152,9 @@ pub enum Type {
Float,
Boolean,
String,
+ Iterator {
+ typ: Box<Type>,
+ },
Function {
inputs: Vec<Type>,
output: Box<Type>,