Beginnings of loops

This commit is contained in:
2024-11-16 13:44:04 -06:00
parent 5fdd28fe0e
commit 7213f4a487
7 changed files with 41 additions and 12 deletions

View File

@@ -107,6 +107,7 @@ pub fn emit(w: &mut dyn Write, stmt: &parser::Stmt, ctx: &mut LexicalContext) ->
writeln!(w, "}}")?;
}
}
parser::Stmt::Loop(_) => todo!()
}
Ok(())
}

3
src/internal.rs Normal file
View File

@@ -0,0 +1,3 @@
//! Internal compiler steps, expansion, cps, optimization.

View File

@@ -1,6 +1,7 @@
mod parser;
mod evaluator;
mod emitter;
mod internal;
use clap::Parser;

View File

@@ -5,6 +5,7 @@ pub enum Stmt {
Assignment(String, Expr),
Funcall(Expr),
Conditional(Vec<GuardedBlock>, Option<Block>),
Loop(Loop),
BareExpr(Expr),
}
@@ -49,6 +50,13 @@ pub struct GuardedBlock {
pub block: Block,
}
#[derive(Debug,PartialEq,Clone)]
pub enum Loop {
Bare(Block),
Until(GuardedBlock),
Over(String, Expr, Block),
}
peg::parser! {
grammar deelang_parser() for str {
pub rule program() -> Vec<Stmt>
@@ -57,6 +65,7 @@ peg::parser! {
= a:assignment() { a } /
f:funcall() stop() { Stmt::Funcall(f) } /
c:conditional() { c } /
l:_loop() { Stmt::Loop(l) } /
e:expr() stop() { Stmt::BareExpr(e) }
rule expr() -> Expr = precedence! {
e1:(@) "=" _ e2:@ { Expr::Equal(Box::new(e1), Box::new(e2)) }
@@ -114,6 +123,17 @@ peg::parser! {
}
rule _else() -> Block
= "else" _ b:block() { b }
rule _loop() -> Loop
= "loop" _ "until" _ e:expr() b:block() __* {
Loop::Until(GuardedBlock{
guard:e,
block: b,
})
} /
"loop" _ "over" _ i:id() "in" _ e:expr() b:block() __* {
Loop::Over(i, e, b)
} /
"loop" _ b:block() __* { Loop::Bare(b) }
rule block() -> Block
= i:indented_block() { i } /
e:expr() { vec![Stmt::BareExpr(e)] }