mirror of
https://github.com/Noratrieb/ub.git
synced 2026-01-14 16:45:05 +01:00
fixed it!!!!
This commit is contained in:
parent
b1756c7c21
commit
535e95069d
2 changed files with 44 additions and 33 deletions
|
|
@ -19,8 +19,7 @@ pub fn parse(_str: &str, _file_name: PathBuf) -> Result<ast::File, ()> {
|
||||||
pub fn test() {
|
pub fn test() {
|
||||||
let src = "
|
let src = "
|
||||||
fn main() {
|
fn main() {
|
||||||
// if 1 { 5 + 5; }
|
if false {} else {}
|
||||||
u64 hello = 5;
|
|
||||||
}
|
}
|
||||||
";
|
";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use chumsky::{prelude::*, Stream};
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{
|
ast::{
|
||||||
Assignment, BinOp, BinOpKind, Call, ElsePart, Expr, File, FnDecl, IfStmt, Item, Literal,
|
Assignment, BinOp, BinOpKind, Call, ElsePart, Expr, File, FnDecl, IfStmt, Item, Literal,
|
||||||
NameTyPair, Stmt, StructDecl, Ty, TyKind, VarDecl,
|
NameTyPair, Stmt, StructDecl, Ty, TyKind, VarDecl, WhileStmt,
|
||||||
},
|
},
|
||||||
lexer::Token,
|
lexer::Token,
|
||||||
};
|
};
|
||||||
|
|
@ -148,6 +148,7 @@ fn statement_parser<'src>() -> impl Parser<Token<'src>, Stmt, Error = Error<'src
|
||||||
let assignment = expr_parser()
|
let assignment = expr_parser()
|
||||||
.then_ignore(just(Token::Eq))
|
.then_ignore(just(Token::Eq))
|
||||||
.then(expr_parser())
|
.then(expr_parser())
|
||||||
|
.then_ignore(just(Token::Semi))
|
||||||
.map(|(place, rhs)| {
|
.map(|(place, rhs)| {
|
||||||
Stmt::Assignment(Assignment {
|
Stmt::Assignment(Assignment {
|
||||||
place,
|
place,
|
||||||
|
|
@ -156,39 +157,50 @@ fn statement_parser<'src>() -> impl Parser<Token<'src>, Stmt, Error = Error<'src
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
// let if_stmt = recursive(|if_stmt| {
|
let while_loop = just(Token::While)
|
||||||
// just(Token::If)
|
.ignore_then(expr_parser())
|
||||||
// .ignore_then(expr_parser())
|
.then(
|
||||||
// .then(
|
stmt.clone()
|
||||||
// stmt.clone()
|
.repeated()
|
||||||
// .repeated()
|
.delimited_by(just(Token::BraceO), just(Token::BraceC)),
|
||||||
// .delimited_by(just(Token::BraceO), just(Token::BraceC)),
|
)
|
||||||
// )
|
.map_with_span(|(cond, body), span| Stmt::WhileStmt(WhileStmt { cond, body, span }))
|
||||||
// .then(
|
.labelled("while loop");
|
||||||
// just(Token::Else).ignore_then(
|
|
||||||
// if_stmt
|
let if_stmt = recursive(|if_stmt| {
|
||||||
// .map(|if_stmt| ElsePart::ElseIf(Box::new(if_stmt)))
|
just(Token::If)
|
||||||
// .or(stmt
|
.ignore_then(expr_parser())
|
||||||
// .clone()
|
.then(
|
||||||
// .repeated()
|
stmt.clone()
|
||||||
// .delimited_by(just(Token::BraceO), just(Token::BraceC))
|
.repeated()
|
||||||
// .map_with_span(ElsePart::Else))
|
.delimited_by(just(Token::BraceO), just(Token::BraceC)),
|
||||||
// .or_not(),
|
)
|
||||||
// ),
|
.then(
|
||||||
// )
|
just(Token::Else).ignore_then(
|
||||||
// .map_with_span(|((cond, body), else_part), span| IfStmt {
|
if_stmt
|
||||||
// cond,
|
.map(|if_stmt| ElsePart::ElseIf(Box::new(if_stmt)))
|
||||||
// body,
|
.or(stmt
|
||||||
// else_part,
|
.clone()
|
||||||
// span,
|
.repeated()
|
||||||
// })
|
.delimited_by(just(Token::BraceO), just(Token::BraceC))
|
||||||
// })
|
.map_with_span(ElsePart::Else))
|
||||||
// .map(Stmt::IfStmt);
|
.or_not(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.map_with_span(|((cond, body), else_part), span| IfStmt {
|
||||||
|
cond,
|
||||||
|
body,
|
||||||
|
else_part,
|
||||||
|
span,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.map(Stmt::IfStmt);
|
||||||
|
|
||||||
var_decl
|
var_decl
|
||||||
.or(assignment)
|
.or(assignment)
|
||||||
.or(expr_parser().map(Stmt::Expr))
|
.or(expr_parser().then_ignore(just(Token::Semi)).map(Stmt::Expr))
|
||||||
.then_ignore(just(Token::Semi))
|
.or(if_stmt)
|
||||||
|
.or(while_loop)
|
||||||
})
|
})
|
||||||
.labelled("statement")
|
.labelled("statement")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue