i span my head right round right round

This commit is contained in:
nora 2022-06-12 21:23:09 +02:00
parent 31938274a7
commit c9b85152ed
11 changed files with 342 additions and 216 deletions

View file

@ -104,7 +104,13 @@ pub struct LoopStmt {
}
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
pub struct Expr {
pub kind: ExprKind,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ExprKind {
BinOp(BinOp),
UnaryOp(UnaryOp),
FieldAccess(FieldAccess),
@ -123,7 +129,7 @@ pub struct BinOp {
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BinOpKind {
Eq,
Neq,
@ -152,7 +158,7 @@ pub struct UnaryOp {
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UnaryOpKind {
Not,
Neg,
@ -172,7 +178,7 @@ pub struct Call {
pub args: Vec<Expr>,
}
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Literal {
String(String, Span),
Integer(u64, Span),

View file

@ -4,8 +4,9 @@ use chumsky::{prelude::*, Stream};
use crate::{
ast::{
Assignment, BinOp, BinOpKind, Call, ElsePart, Expr, File, FnDecl, IfStmt, Item, Literal,
NameTyPair, Stmt, StructDecl, Ty, TyKind, UnaryOp, UnaryOpKind, VarDecl, WhileStmt,
Assignment, BinOp, BinOpKind, Call, ElsePart, Expr, ExprKind, File, FnDecl, IfStmt, Item,
Literal, NameTyPair, Stmt, StructDecl, Ty, TyKind, UnaryOp, UnaryOpKind, VarDecl,
WhileStmt,
},
lexer::Token,
};
@ -52,13 +53,19 @@ fn ty_parser<'src>() -> impl Parser<Token<'src>, Ty, Error = Error<'src>> + Clon
fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> + Clone {
recursive(|expr| {
let literal = filter_map(|span, token| match token {
Token::String(str) => Ok(Expr::Literal(Literal::String(
let literal = filter_map(|span: Span, token| match token {
Token::String(str) => Ok(Expr {
kind: ExprKind::Literal(Literal::String(
str[1..str.len() - 2].to_owned(),
span.clone(),
)),
span,
))),
}),
// todo lol unwrap
Token::Integer(int) => Ok(Expr::Literal(Literal::Integer(int.parse().unwrap(), span))),
Token::Integer(int) => Ok(Expr {
kind: ExprKind::Literal(Literal::Integer(int.parse().unwrap(), span.clone())),
span,
}),
_ => Err(Simple::expected_input_found(span, Vec::new(), Some(token))),
})
.labelled("literal");
@ -74,15 +81,20 @@ fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> +
let array = expr_list
.clone()
.delimited_by(just(Token::BracketO), just(Token::BracketC))
.map(Expr::Array);
.map_with_span(|exprs: Vec<Expr>, span| Expr {
kind: ExprKind::Array(exprs),
span,
});
let atom = literal
.or(ident_parser().map(Expr::Name))
.or(ident_parser().map_with_span(|name, span| Expr {
kind: ExprKind::Name(name),
span,
}))
.or(array)
.or(expr
.clone()
.delimited_by(just(Token::ParenO), just(Token::ParenC)))
.debug("atom")
.boxed();
let call = atom
@ -93,10 +105,15 @@ fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> +
.repeated(),
)
.foldl(|callee: Expr, args: Vec<Expr>| {
Expr::Call(Call {
let span =
callee.span.start..args.last().map(|e| e.span.end).unwrap_or(callee.span.end);
Expr {
kind: ExprKind::Call(Call {
callee: Box::new(callee),
args,
})
}),
span,
}
})
.labelled("call")
.boxed();
@ -110,14 +127,17 @@ fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> +
.repeated()
.then(call)
.foldr(|kind, rhs| {
Expr::UnaryOp(UnaryOp {
let span = rhs.span.clone();
Expr {
kind: ExprKind::UnaryOp(UnaryOp {
expr: Box::new(rhs),
kind,
span: 0..0, // lol todo
})
span: span.clone(),
}),
span,
}
})
.labelled("unary")
.debug("unary")
.boxed();
let op = just(Token::Asterisk)
@ -128,12 +148,16 @@ fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> +
.clone()
.then(op.then(unary_op).repeated())
.foldl(|a, (kind, b)| {
Expr::BinOp(BinOp {
let span = a.span.start..b.span.end;
Expr {
kind: ExprKind::BinOp(BinOp {
kind,
lhs: Box::new(a),
rhs: Box::new(b),
span: 0..0, // lol todo
})
span: span.clone(),
}),
span,
}
});
// Sum ops (add and subtract) have equal precedence
@ -144,15 +168,18 @@ fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> +
.clone()
.then(op.then(product).repeated())
.foldl(|a, (kind, b)| {
Expr::BinOp(BinOp {
let span = a.span.start..b.span.end;
Expr {
kind: ExprKind::BinOp(BinOp {
kind,
lhs: Box::new(a),
rhs: Box::new(b),
span: 0..0, // lol todo
})
span: span.clone(),
}),
span,
}
})
.labelled("product")
.debug("product")
.boxed();
// Comparison ops (equal, not-equal) have equal precedence
@ -163,12 +190,16 @@ fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> +
.clone()
.then(op.then(sum).repeated())
.foldl(|a, (kind, b)| {
Expr::BinOp(BinOp {
let span = a.span.start..b.span.end;
Expr {
kind: ExprKind::BinOp(BinOp {
kind,
lhs: Box::new(a),
rhs: Box::new(b),
span: 0..0, // lol todo
})
span: span.clone(),
}),
span,
}
});
compare.labelled("comparison").boxed()
})

View file

@ -1,6 +1,5 @@
---
source: parser/src/parser.rs
assertion_line: 332
expression: r
---
(
@ -16,24 +15,33 @@ expression: r
span: 0..20,
body: [
Expr(
BinOp(
Expr {
kind: BinOp(
BinOp {
kind: Add,
lhs: Literal(
lhs: Expr {
kind: Literal(
Integer(
1,
12..13,
),
),
rhs: Literal(
span: 12..13,
},
rhs: Expr {
kind: Literal(
Integer(
4,
16..17,
),
),
span: 0..0,
span: 16..17,
},
span: 12..17,
},
),
span: 12..17,
},
),
],
},

View file

@ -1,6 +1,5 @@
---
source: parser/src/parser.rs
assertion_line: 338
expression: r
---
(
@ -16,38 +15,56 @@ expression: r
span: 0..32,
body: [
Expr(
BinOp(
Expr {
kind: BinOp(
BinOp {
kind: Add,
lhs: BinOp(
lhs: Expr {
kind: BinOp(
BinOp {
kind: Div,
lhs: Literal(
lhs: Expr {
kind: Literal(
Integer(
4,
13..14,
),
),
rhs: Call(
span: 13..14,
},
rhs: Expr {
kind: Call(
Call {
callee: Name(
callee: Expr {
kind: Name(
"hallo",
),
span: 17..22,
},
args: [],
},
),
span: 0..0,
span: 17..22,
},
span: 13..22,
},
),
rhs: Literal(
span: 13..22,
},
rhs: Expr {
kind: Literal(
Integer(
5,
28..29,
),
),
span: 0..0,
span: 28..29,
},
span: 13..29,
},
),
span: 13..29,
},
),
],
},

View file

@ -1,6 +1,5 @@
---
source: parser/src/parser.rs
assertion_line: 344
expression: r
---
(
@ -21,24 +20,33 @@ expression: r
span: 0..26,
body: [
Expr(
BinOp(
Expr {
kind: BinOp(
BinOp {
kind: Add,
lhs: Literal(
lhs: Expr {
kind: Literal(
Integer(
1,
18..19,
),
),
rhs: Literal(
span: 18..19,
},
rhs: Expr {
kind: Literal(
Integer(
5,
22..23,
),
),
span: 0..0,
span: 22..23,
},
span: 18..23,
},
),
span: 18..23,
},
),
],
},

View file

@ -1,6 +1,5 @@
---
source: parser/src/parser.rs
assertion_line: 356
expression: r
---
(
@ -22,9 +21,12 @@ expression: r
body: [
IfStmt(
IfStmt {
cond: Name(
cond: Expr {
kind: Name(
"false",
),
span: 21..26,
},
body: [],
else_part: Some(
Else(

View file

@ -1,6 +1,5 @@
---
source: parser/src/parser.rs
assertion_line: 350
expression: r
---
(
@ -22,9 +21,12 @@ expression: r
body: [
IfStmt(
IfStmt {
cond: Name(
cond: Expr {
kind: Name(
"false",
),
span: 21..26,
},
body: [],
else_part: None,
span: 18..29,

View file

@ -34,12 +34,15 @@ expression: r
),
},
rhs: Some(
Literal(
Expr {
kind: Literal(
Integer(
2,
36..37,
),
),
span: 36..37,
},
),
span: 0..0,
},
@ -57,12 +60,15 @@ expression: r
),
},
rhs: Some(
Literal(
Expr {
kind: Literal(
Integer(
25,
53..55,
),
),
span: 53..55,
},
),
span: 0..0,
},

View file

@ -15,96 +15,138 @@ expression: r
span: 0..63,
body: [
Expr(
UnaryOp(
Expr {
kind: UnaryOp(
UnaryOp {
expr: UnaryOp(
expr: Expr {
kind: UnaryOp(
UnaryOp {
expr: Literal(
expr: Expr {
kind: Literal(
Integer(
5,
19..20,
),
),
span: 19..20,
},
kind: Deref,
span: 0..0,
span: 19..20,
},
),
span: 19..20,
},
kind: Neg,
span: 0..0,
span: 19..20,
},
),
span: 19..20,
},
),
Expr(
UnaryOp(
Expr {
kind: UnaryOp(
UnaryOp {
expr: Literal(
expr: Expr {
kind: Literal(
Integer(
5,
28..29,
),
),
span: 28..29,
},
kind: AddrOf,
span: 0..0,
span: 28..29,
},
),
span: 28..29,
},
),
Expr(
BinOp(
Expr {
kind: BinOp(
BinOp {
kind: Add,
lhs: Literal(
lhs: Expr {
kind: Literal(
Integer(
2,
35..36,
),
),
rhs: UnaryOp(
span: 35..36,
},
rhs: Expr {
kind: UnaryOp(
UnaryOp {
expr: Literal(
expr: Expr {
kind: Literal(
Integer(
8,
40..41,
),
),
span: 40..41,
},
kind: AddrOf,
span: 0..0,
span: 40..41,
},
),
span: 0..0,
span: 40..41,
},
span: 35..41,
},
),
span: 35..41,
},
),
Expr(
BinOp(
Expr {
kind: BinOp(
BinOp {
kind: Mul,
lhs: UnaryOp(
lhs: Expr {
kind: UnaryOp(
UnaryOp {
expr: Literal(
expr: Expr {
kind: Literal(
Integer(
6,
48..49,
),
),
span: 48..49,
},
kind: Deref,
span: 0..0,
span: 48..49,
},
),
rhs: UnaryOp(
span: 48..49,
},
rhs: Expr {
kind: UnaryOp(
UnaryOp {
expr: Literal(
expr: Expr {
kind: Literal(
Integer(
8,
53..54,
),
),
span: 53..54,
},
kind: Deref,
span: 0..0,
span: 53..54,
},
),
span: 0..0,
span: 53..54,
},
span: 48..54,
},
),
span: 48..54,
},
),
],
},

View file

@ -1,6 +1,5 @@
---
source: parser/src/parser.rs
assertion_line: 368
expression: r
---
(
@ -28,12 +27,15 @@ expression: r
kind: U64,
},
rhs: Some(
Literal(
Expr {
kind: Literal(
Integer(
5,
30..31,
),
),
span: 30..31,
},
),
span: 0..0,
},

View file

@ -1,6 +1,5 @@
---
source: parser/src/parser.rs
assertion_line: 362
expression: r
---
(
@ -22,9 +21,12 @@ expression: r
body: [
WhileStmt(
WhileStmt {
cond: Name(
cond: Expr {
kind: Name(
"false",
),
span: 24..29,
},
body: [],
span: 18..32,
},