mirror of
https://github.com/Noratrieb/ub.git
synced 2026-01-14 16:45:05 +01:00
i span my head right round right round
This commit is contained in:
parent
31938274a7
commit
c9b85152ed
11 changed files with 342 additions and 216 deletions
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
str[1..str.len() - 2].to_owned(),
|
||||
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 {
|
||||
callee: Box::new(callee),
|
||||
args,
|
||||
})
|
||||
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 {
|
||||
expr: Box::new(rhs),
|
||||
kind,
|
||||
span: 0..0, // lol todo
|
||||
})
|
||||
let span = rhs.span.clone();
|
||||
Expr {
|
||||
kind: ExprKind::UnaryOp(UnaryOp {
|
||||
expr: Box::new(rhs),
|
||||
kind,
|
||||
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 {
|
||||
kind,
|
||||
lhs: Box::new(a),
|
||||
rhs: Box::new(b),
|
||||
span: 0..0, // lol todo
|
||||
})
|
||||
let span = a.span.start..b.span.end;
|
||||
Expr {
|
||||
kind: ExprKind::BinOp(BinOp {
|
||||
kind,
|
||||
lhs: Box::new(a),
|
||||
rhs: Box::new(b),
|
||||
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 {
|
||||
kind,
|
||||
lhs: Box::new(a),
|
||||
rhs: Box::new(b),
|
||||
span: 0..0, // lol todo
|
||||
})
|
||||
let span = a.span.start..b.span.end;
|
||||
Expr {
|
||||
kind: ExprKind::BinOp(BinOp {
|
||||
kind,
|
||||
lhs: Box::new(a),
|
||||
rhs: Box::new(b),
|
||||
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 {
|
||||
kind,
|
||||
lhs: Box::new(a),
|
||||
rhs: Box::new(b),
|
||||
span: 0..0, // lol todo
|
||||
})
|
||||
let span = a.span.start..b.span.end;
|
||||
Expr {
|
||||
kind: ExprKind::BinOp(BinOp {
|
||||
kind,
|
||||
lhs: Box::new(a),
|
||||
rhs: Box::new(b),
|
||||
span: span.clone(),
|
||||
}),
|
||||
span,
|
||||
}
|
||||
});
|
||||
compare.labelled("comparison").boxed()
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
BinOp {
|
||||
kind: Add,
|
||||
lhs: Literal(
|
||||
Integer(
|
||||
1,
|
||||
12..13,
|
||||
),
|
||||
),
|
||||
rhs: Literal(
|
||||
Integer(
|
||||
4,
|
||||
16..17,
|
||||
),
|
||||
),
|
||||
span: 0..0,
|
||||
},
|
||||
),
|
||||
Expr {
|
||||
kind: BinOp(
|
||||
BinOp {
|
||||
kind: Add,
|
||||
lhs: Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
1,
|
||||
12..13,
|
||||
),
|
||||
),
|
||||
span: 12..13,
|
||||
},
|
||||
rhs: Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
4,
|
||||
16..17,
|
||||
),
|
||||
),
|
||||
span: 16..17,
|
||||
},
|
||||
span: 12..17,
|
||||
},
|
||||
),
|
||||
span: 12..17,
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
BinOp {
|
||||
kind: Add,
|
||||
lhs: BinOp(
|
||||
BinOp {
|
||||
kind: Div,
|
||||
lhs: Literal(
|
||||
Integer(
|
||||
4,
|
||||
13..14,
|
||||
),
|
||||
),
|
||||
rhs: Call(
|
||||
Call {
|
||||
callee: Name(
|
||||
"hallo",
|
||||
),
|
||||
args: [],
|
||||
Expr {
|
||||
kind: BinOp(
|
||||
BinOp {
|
||||
kind: Add,
|
||||
lhs: Expr {
|
||||
kind: BinOp(
|
||||
BinOp {
|
||||
kind: Div,
|
||||
lhs: Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
4,
|
||||
13..14,
|
||||
),
|
||||
),
|
||||
span: 13..14,
|
||||
},
|
||||
rhs: Expr {
|
||||
kind: Call(
|
||||
Call {
|
||||
callee: Expr {
|
||||
kind: Name(
|
||||
"hallo",
|
||||
),
|
||||
span: 17..22,
|
||||
},
|
||||
args: [],
|
||||
},
|
||||
),
|
||||
span: 17..22,
|
||||
},
|
||||
span: 13..22,
|
||||
},
|
||||
),
|
||||
span: 0..0,
|
||||
span: 13..22,
|
||||
},
|
||||
),
|
||||
rhs: Literal(
|
||||
Integer(
|
||||
5,
|
||||
28..29,
|
||||
),
|
||||
),
|
||||
span: 0..0,
|
||||
},
|
||||
),
|
||||
rhs: Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
5,
|
||||
28..29,
|
||||
),
|
||||
),
|
||||
span: 28..29,
|
||||
},
|
||||
span: 13..29,
|
||||
},
|
||||
),
|
||||
span: 13..29,
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
BinOp {
|
||||
kind: Add,
|
||||
lhs: Literal(
|
||||
Integer(
|
||||
1,
|
||||
18..19,
|
||||
),
|
||||
),
|
||||
rhs: Literal(
|
||||
Integer(
|
||||
5,
|
||||
22..23,
|
||||
),
|
||||
),
|
||||
span: 0..0,
|
||||
},
|
||||
),
|
||||
Expr {
|
||||
kind: BinOp(
|
||||
BinOp {
|
||||
kind: Add,
|
||||
lhs: Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
1,
|
||||
18..19,
|
||||
),
|
||||
),
|
||||
span: 18..19,
|
||||
},
|
||||
rhs: Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
5,
|
||||
22..23,
|
||||
),
|
||||
),
|
||||
span: 22..23,
|
||||
},
|
||||
span: 18..23,
|
||||
},
|
||||
),
|
||||
span: 18..23,
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
"false",
|
||||
),
|
||||
cond: Expr {
|
||||
kind: Name(
|
||||
"false",
|
||||
),
|
||||
span: 21..26,
|
||||
},
|
||||
body: [],
|
||||
else_part: Some(
|
||||
Else(
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
"false",
|
||||
),
|
||||
cond: Expr {
|
||||
kind: Name(
|
||||
"false",
|
||||
),
|
||||
span: 21..26,
|
||||
},
|
||||
body: [],
|
||||
else_part: None,
|
||||
span: 18..29,
|
||||
|
|
|
|||
|
|
@ -34,12 +34,15 @@ expression: r
|
|||
),
|
||||
},
|
||||
rhs: Some(
|
||||
Literal(
|
||||
Integer(
|
||||
2,
|
||||
36..37,
|
||||
Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
2,
|
||||
36..37,
|
||||
),
|
||||
),
|
||||
),
|
||||
span: 36..37,
|
||||
},
|
||||
),
|
||||
span: 0..0,
|
||||
},
|
||||
|
|
@ -57,12 +60,15 @@ expression: r
|
|||
),
|
||||
},
|
||||
rhs: Some(
|
||||
Literal(
|
||||
Integer(
|
||||
25,
|
||||
53..55,
|
||||
Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
25,
|
||||
53..55,
|
||||
),
|
||||
),
|
||||
),
|
||||
span: 53..55,
|
||||
},
|
||||
),
|
||||
span: 0..0,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -15,96 +15,138 @@ expression: r
|
|||
span: 0..63,
|
||||
body: [
|
||||
Expr(
|
||||
UnaryOp(
|
||||
UnaryOp {
|
||||
expr: UnaryOp(
|
||||
UnaryOp {
|
||||
expr: Literal(
|
||||
Expr {
|
||||
kind: UnaryOp(
|
||||
UnaryOp {
|
||||
expr: Expr {
|
||||
kind: UnaryOp(
|
||||
UnaryOp {
|
||||
expr: Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
5,
|
||||
19..20,
|
||||
),
|
||||
),
|
||||
span: 19..20,
|
||||
},
|
||||
kind: Deref,
|
||||
span: 19..20,
|
||||
},
|
||||
),
|
||||
span: 19..20,
|
||||
},
|
||||
kind: Neg,
|
||||
span: 19..20,
|
||||
},
|
||||
),
|
||||
span: 19..20,
|
||||
},
|
||||
),
|
||||
Expr(
|
||||
Expr {
|
||||
kind: UnaryOp(
|
||||
UnaryOp {
|
||||
expr: Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
5,
|
||||
19..20,
|
||||
28..29,
|
||||
),
|
||||
),
|
||||
kind: Deref,
|
||||
span: 0..0,
|
||||
span: 28..29,
|
||||
},
|
||||
),
|
||||
kind: Neg,
|
||||
span: 0..0,
|
||||
},
|
||||
),
|
||||
kind: AddrOf,
|
||||
span: 28..29,
|
||||
},
|
||||
),
|
||||
span: 28..29,
|
||||
},
|
||||
),
|
||||
Expr(
|
||||
UnaryOp(
|
||||
UnaryOp {
|
||||
expr: Literal(
|
||||
Integer(
|
||||
5,
|
||||
28..29,
|
||||
),
|
||||
),
|
||||
kind: AddrOf,
|
||||
span: 0..0,
|
||||
},
|
||||
),
|
||||
Expr {
|
||||
kind: BinOp(
|
||||
BinOp {
|
||||
kind: Add,
|
||||
lhs: Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
2,
|
||||
35..36,
|
||||
),
|
||||
),
|
||||
span: 35..36,
|
||||
},
|
||||
rhs: Expr {
|
||||
kind: UnaryOp(
|
||||
UnaryOp {
|
||||
expr: Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
8,
|
||||
40..41,
|
||||
),
|
||||
),
|
||||
span: 40..41,
|
||||
},
|
||||
kind: AddrOf,
|
||||
span: 40..41,
|
||||
},
|
||||
),
|
||||
span: 40..41,
|
||||
},
|
||||
span: 35..41,
|
||||
},
|
||||
),
|
||||
span: 35..41,
|
||||
},
|
||||
),
|
||||
Expr(
|
||||
BinOp(
|
||||
BinOp {
|
||||
kind: Add,
|
||||
lhs: Literal(
|
||||
Integer(
|
||||
2,
|
||||
35..36,
|
||||
),
|
||||
),
|
||||
rhs: UnaryOp(
|
||||
UnaryOp {
|
||||
expr: Literal(
|
||||
Integer(
|
||||
8,
|
||||
40..41,
|
||||
),
|
||||
Expr {
|
||||
kind: BinOp(
|
||||
BinOp {
|
||||
kind: Mul,
|
||||
lhs: Expr {
|
||||
kind: UnaryOp(
|
||||
UnaryOp {
|
||||
expr: Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
6,
|
||||
48..49,
|
||||
),
|
||||
),
|
||||
span: 48..49,
|
||||
},
|
||||
kind: Deref,
|
||||
span: 48..49,
|
||||
},
|
||||
),
|
||||
kind: AddrOf,
|
||||
span: 0..0,
|
||||
span: 48..49,
|
||||
},
|
||||
),
|
||||
span: 0..0,
|
||||
},
|
||||
),
|
||||
),
|
||||
Expr(
|
||||
BinOp(
|
||||
BinOp {
|
||||
kind: Mul,
|
||||
lhs: UnaryOp(
|
||||
UnaryOp {
|
||||
expr: Literal(
|
||||
Integer(
|
||||
6,
|
||||
48..49,
|
||||
),
|
||||
rhs: Expr {
|
||||
kind: UnaryOp(
|
||||
UnaryOp {
|
||||
expr: Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
8,
|
||||
53..54,
|
||||
),
|
||||
),
|
||||
span: 53..54,
|
||||
},
|
||||
kind: Deref,
|
||||
span: 53..54,
|
||||
},
|
||||
),
|
||||
kind: Deref,
|
||||
span: 0..0,
|
||||
span: 53..54,
|
||||
},
|
||||
),
|
||||
rhs: UnaryOp(
|
||||
UnaryOp {
|
||||
expr: Literal(
|
||||
Integer(
|
||||
8,
|
||||
53..54,
|
||||
),
|
||||
),
|
||||
kind: Deref,
|
||||
span: 0..0,
|
||||
},
|
||||
),
|
||||
span: 0..0,
|
||||
},
|
||||
),
|
||||
span: 48..54,
|
||||
},
|
||||
),
|
||||
span: 48..54,
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
Integer(
|
||||
5,
|
||||
30..31,
|
||||
Expr {
|
||||
kind: Literal(
|
||||
Integer(
|
||||
5,
|
||||
30..31,
|
||||
),
|
||||
),
|
||||
),
|
||||
span: 30..31,
|
||||
},
|
||||
),
|
||||
span: 0..0,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
"false",
|
||||
),
|
||||
cond: Expr {
|
||||
kind: Name(
|
||||
"false",
|
||||
),
|
||||
span: 24..29,
|
||||
},
|
||||
body: [],
|
||||
span: 18..32,
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue