aboutsummaryrefslogtreecommitdiff
path: root/sloth/src/parser
diff options
context:
space:
mode:
Diffstat (limited to 'sloth/src/parser')
-rw-r--r--sloth/src/parser/ast.rs9
-rw-r--r--sloth/src/parser/graph.rs23
-rw-r--r--sloth/src/parser/mod.rs2
-rw-r--r--sloth/src/parser/stmt.rs25
4 files changed, 58 insertions, 1 deletions
diff --git a/sloth/src/parser/ast.rs b/sloth/src/parser/ast.rs
index 00719e7..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, .. }) => {
@@ -215,6 +219,11 @@ pub enum StmtKind {
condition: Expr,
body: Box<Stmt>,
},
+ ForStmt {
+ 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/parser/mod.rs b/sloth/src/parser/mod.rs
index 09a26fd..9bf4dd6 100644
--- a/sloth/src/parser/mod.rs
+++ b/sloth/src/parser/mod.rs
@@ -40,7 +40,7 @@ impl<'a> AstParser<'a> {
let mut statements = Vec::new();
while !parser.eof() {
statements.push(parser.statement()?);
- }
+ }
let root = Stmt::new(
parser.reserve_id(),
diff --git a/sloth/src/parser/stmt.rs b/sloth/src/parser/stmt.rs
index 6af4634..4771abc 100644
--- a/sloth/src/parser/stmt.rs
+++ b/sloth/src/parser/stmt.rs
@@ -83,6 +83,31 @@ impl<'a> AstParser<'a> {
))
}
+ fn for_stmt(&mut self) -> Result<Stmt, ParsingError> {
+ // Consume the for token
+ self.consume(TokenType::For, "Expected for")?;
+
+
+ let identifier = self.consume_identifier()?;
+ self.consume(TokenType::In, "Expected in")?;
+ let iterator = self.expression()?;
+
+ let body = self.block()?;
+
+ let kind = StmtKind::ForStmt {
+ iterator,
+ identifier,
+ body: body.into(),
+ };
+
+ Ok(Stmt::new(
+ self.reserve_id(),
+ self.line,
+ kind,
+ self.top.clone(),
+ ))
+ }
+
// TODO: Make variable types optional
fn define_variable(&mut self) -> Result<Stmt, ParsingError> {
// Consume the var token