Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/ast/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ pub enum Stmt {
cond: Box<Expr>,
body: Vec<Stmt>,
},
For {
init: Box<Stmt>,
cond: Box<Expr>,
inc: Box<Stmt>,
body: Vec<Stmt>,
},
SetVar {
name: Name,
value: Box<Expr>,
Expand Down Expand Up @@ -101,6 +107,7 @@ impl Stmt {
Stmt::Forever { span, .. } => span.clone(),
Stmt::Branch { cond, .. } => cond.span(),
Stmt::Until { cond, .. } => cond.span(),
Stmt::For { init, .. } => init.span(),
Stmt::SetVar { name, .. } => name.span(),
Stmt::ChangeVar { name, .. } => name.span(),
Stmt::Show(name) => name.span(),
Expand Down
13 changes: 13 additions & 0 deletions src/codegen/sb3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ impl Stmt {
}
}
Stmt::Until { .. } => "control_repeat_until",
Stmt::For { .. } => "control_repeat_until",
Stmt::SetVar { .. } => "data_setvariableto",
Stmt::ChangeVar { .. } => "data_changevariableby",
Stmt::Show(name) => {
Expand Down Expand Up @@ -1254,6 +1255,18 @@ where T: Write + Seek
else_body,
} => self.branch(s, d, this_id, cond, if_body, else_body),
Stmt::Until { cond, body } => self.until(s, d, this_id, cond, body),
Stmt::For {
init,
cond,
inc,
body,
} => {
let init_id = self.id.new_id();
self.stmt(s, d, init, init_id, None, Some(this_id))?;
let mut body = body.clone();
body.push(*inc.clone());
self.until(s, d, this_id, cond, &body)
}
Stmt::SetVar {
name,
value,
Expand Down
3 changes: 3 additions & 0 deletions src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ pub enum Token {
Forever,
#[token("repeat")]
Repeat,
#[token("for")]
For,
#[token(",")]
Comma,
#[token("(")]
Expand Down Expand Up @@ -276,6 +278,7 @@ impl Display for Token {
Token::Until => write!(f, "until"),
Token::Forever => write!(f, "forever"),
Token::Repeat => write!(f, "repeat"),
Token::For => write!(f, "for"),
Token::Comma => write!(f, ","),
Token::LParen => write!(f, "("),
Token::RParen => write!(f, ")"),
Expand Down
32 changes: 32 additions & 0 deletions src/parser/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ Sound: () = {

Stmts: Vec<Stmt> = "{" <(<Stmt> ";"*)*> "}";

ForInit: Stmt = {
<type_:Type> <l:@L> <name:NAME> <r:@R> "=" <value:BoxedExpr> => {
Stmt::SetVar {
name: Name::Name { name, span: l..r },
value,
type_,
is_local: false,
is_cloud: false,
}
},
<l:@L> <name:NAME> <r:@R> "++" => Stmt::increment(Name::Name { name, span: l..r }),
<l:@L> <name:NAME> <r:@R> "--" => Stmt::decrement(Name::Name { name, span: l..r }),
<l:@L> <name:NAME> <r:@R> "+=" <value:BoxedExpr> => Stmt::augmented_assign(BinOp::Add, name, l..r, *value),
<l:@L> <name:NAME> <r:@R> "-=" <value:BoxedExpr> => Stmt::augmented_assign(BinOp::Sub, name, l..r, *value),
<l:@L> <name:NAME> <r:@R> "*=" <value:BoxedExpr> => Stmt::augmented_assign(BinOp::Mul, name, l..r, *value),
<l:@L> <name:NAME> <r:@R> "/=" <value:BoxedExpr> => Stmt::augmented_assign(BinOp::Div, name, l..r, *value),
<l:@L> <name:NAME> <r:@R> "//=" <value:BoxedExpr> => Stmt::augmented_assign(BinOp::FloorDiv, name, l..r, *value),
<l:@L> <name:NAME> <r:@R> "%=" <value:BoxedExpr> => Stmt::augmented_assign(BinOp::Mod, name, l..r, *value),
<l:@L> <name:NAME> <r:@R> "&=" <value:BoxedExpr> => Stmt::augmented_assign(BinOp::Join, name, l..r, *value),
};

ForInc: Stmt = ForInit;

Stmt: Stmt = {
RETURN <value:BoxedExpr> ";" => Stmt::Return { value, visited: false },
IF <cond:BoxedIfExpr> <if_body:Stmts> => {
Expand All @@ -127,6 +150,14 @@ Stmt: Stmt = {
REPEAT <times:BoxedIfExpr> <body:Stmts> => Stmt::Repeat { times, body },
<l:@L> FOREVER <r:@R> <body:Stmts> => Stmt::Forever { body, span: l..r },
UNTIL <cond:BoxedIfExpr> <body:Stmts> => Stmt::Until { cond, body },
<l:@L> FOR "(" <init:ForInit> ";" <cond:BoxedIfExpr> ";" <inc:ForInc> ")" <body:Stmts> => {
Stmt::For {
init: Box::new(init),
cond,
inc: Box::new(inc),
body,
}
},
<type_:Type> <l:@L> <name:NAME> <r:@R> "=" <value:BoxedExpr> ";" => {
Stmt::SetVar {
name: Name::Name { name, span: l..r },
Expand Down Expand Up @@ -618,6 +649,7 @@ extern {
UNTIL => Token::Until,
FOREVER => Token::Forever,
REPEAT => Token::Repeat,
FOR => Token::For,
"," => Token::Comma,
"(" => Token::LParen,
")" => Token::RParen,
Expand Down
Loading