Skip to content

Commit b4629af

Browse files
adição do parser, adição na ast e keywords
1 parent 829325d commit b4629af

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/ir/ast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ pub enum Statement {
119119
ValDeclaration(Name, Box<Expression>),
120120
Assignment(Name, Box<Expression>),
121121
IfThenElse(Box<Expression>, Box<Statement>, Option<Box<Statement>>),
122+
IfChain {
123+
branches: Vec<(Box<Expr>, Box<Block>)>,
124+
else_branch: Option<Box<Block>>,
125+
},
122126
While(Box<Expression>, Box<Statement>),
123127
For(Name, Box<Expression>, Box<Statement>),
124128
Block(Vec<Statement>),

src/parser/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub const KEYWORDS: &[&str] = &[
22
"if",
33
"in",
44
"else",
5+
"elif",
56
"def",
67
"while",
78
"for",

src/parser/parser_stmt.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,44 @@ fn parse_if_else_statement(input: &str) -> IResult<&str, Statement> {
9999
)(input)
100100
}
101101

102+
pub fn parse_if_chain_statement(input: &str) -> IResult<&str, Statement> {
103+
let (input, _) = keyword(IF_KEYWORD)(input)?;
104+
let (input, cond_if) = preceded(multispace1, parse_expression)(input)?;
105+
let (input, block_if) = parse_block(input)?;
106+
107+
let mut branches = vec![(Box::new(cond_if), Box::new(block_if))];
108+
let mut current_input = input;
109+
110+
loop {
111+
let result = tuple((
112+
multispace0,
113+
keyword(ELIF_KEYWORD),
114+
preceded(multispace1, parse_expression),
115+
parse_block,
116+
))(current_input);
117+
118+
match result {
119+
Ok((next_input, (_, _, cond_elif, block_elif))) => {
120+
branches.push((Box::new(cond_elif), Box::new(block_elif)));
121+
current_input = next_input;
122+
}
123+
Err(_) => break,
124+
}
125+
}
126+
let (input, else_branch) = opt(preceded(
127+
tuple((multispace0, keyword(ELSE_KEYWORD))),
128+
parse_block,
129+
))(current_input)?;
130+
131+
Ok((
132+
input,
133+
Statement::IfChain {
134+
branches,
135+
else_branch: else_branch.map(Box::new),
136+
},
137+
))
138+
}
139+
102140
fn parse_while_statement(input: &str) -> IResult<&str, Statement> {
103141
map(
104142
tuple((
@@ -155,7 +193,7 @@ fn parse_function_definition_statement(input: &str) -> IResult<&str, Statement>
155193
keyword(DEF_KEYWORD),
156194
preceded(multispace1, identifier),
157195
delimited(
158-
char::<&str, Error<&str>>(LEFT_PAREN),
196+
char:// Tenta parsear um else opcional:<&str, Error<&str>>(LEFT_PAREN),
159197
separated_list0(
160198
tuple((
161199
multispace0,
@@ -170,7 +208,7 @@ fn parse_function_definition_statement(input: &str) -> IResult<&str, Statement>
170208
preceded(multispace0, parse_type),
171209
parse_block,
172210
)),
173-
|(_, name, args, _, t, block)| {
211+
|(_, name, ar// Tenta parsear um else opcionalgs, _, t, block)| {
174212
Statement::FuncDef(Function {
175213
name: name.to_string(),
176214
kind: t,

0 commit comments

Comments
 (0)