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

View file

@ -4,8 +4,9 @@ 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, ExprKind, File, FnDecl, IfStmt, Item,
NameTyPair, Stmt, StructDecl, Ty, TyKind, UnaryOp, UnaryOpKind, VarDecl, WhileStmt, Literal, NameTyPair, Stmt, StructDecl, Ty, TyKind, UnaryOp, UnaryOpKind, VarDecl,
WhileStmt,
}, },
lexer::Token, 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 { fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> + Clone {
recursive(|expr| { recursive(|expr| {
let literal = filter_map(|span, token| match token { let literal = filter_map(|span: Span, token| match token {
Token::String(str) => Ok(Expr::Literal(Literal::String( Token::String(str) => Ok(Expr {
kind: ExprKind::Literal(Literal::String(
str[1..str.len() - 2].to_owned(), str[1..str.len() - 2].to_owned(),
span.clone(),
)),
span, span,
))), }),
// todo lol unwrap // 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))), _ => Err(Simple::expected_input_found(span, Vec::new(), Some(token))),
}) })
.labelled("literal"); .labelled("literal");
@ -74,15 +81,20 @@ fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> +
let array = expr_list let array = expr_list
.clone() .clone()
.delimited_by(just(Token::BracketO), just(Token::BracketC)) .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 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(array)
.or(expr .or(expr
.clone() .clone()
.delimited_by(just(Token::ParenO), just(Token::ParenC))) .delimited_by(just(Token::ParenO), just(Token::ParenC)))
.debug("atom")
.boxed(); .boxed();
let call = atom let call = atom
@ -93,10 +105,15 @@ fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> +
.repeated(), .repeated(),
) )
.foldl(|callee: Expr, args: Vec<Expr>| { .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), callee: Box::new(callee),
args, args,
}) }),
span,
}
}) })
.labelled("call") .labelled("call")
.boxed(); .boxed();
@ -110,14 +127,17 @@ fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> +
.repeated() .repeated()
.then(call) .then(call)
.foldr(|kind, rhs| { .foldr(|kind, rhs| {
Expr::UnaryOp(UnaryOp { let span = rhs.span.clone();
Expr {
kind: ExprKind::UnaryOp(UnaryOp {
expr: Box::new(rhs), expr: Box::new(rhs),
kind, kind,
span: 0..0, // lol todo span: span.clone(),
}) }),
span,
}
}) })
.labelled("unary") .labelled("unary")
.debug("unary")
.boxed(); .boxed();
let op = just(Token::Asterisk) let op = just(Token::Asterisk)
@ -128,12 +148,16 @@ fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> +
.clone() .clone()
.then(op.then(unary_op).repeated()) .then(op.then(unary_op).repeated())
.foldl(|a, (kind, b)| { .foldl(|a, (kind, b)| {
Expr::BinOp(BinOp { let span = a.span.start..b.span.end;
Expr {
kind: ExprKind::BinOp(BinOp {
kind, kind,
lhs: Box::new(a), lhs: Box::new(a),
rhs: Box::new(b), rhs: Box::new(b),
span: 0..0, // lol todo span: span.clone(),
}) }),
span,
}
}); });
// Sum ops (add and subtract) have equal precedence // 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() .clone()
.then(op.then(product).repeated()) .then(op.then(product).repeated())
.foldl(|a, (kind, b)| { .foldl(|a, (kind, b)| {
Expr::BinOp(BinOp { let span = a.span.start..b.span.end;
Expr {
kind: ExprKind::BinOp(BinOp {
kind, kind,
lhs: Box::new(a), lhs: Box::new(a),
rhs: Box::new(b), rhs: Box::new(b),
span: 0..0, // lol todo span: span.clone(),
}) }),
span,
}
}) })
.labelled("product") .labelled("product")
.debug("product")
.boxed(); .boxed();
// Comparison ops (equal, not-equal) have equal precedence // 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() .clone()
.then(op.then(sum).repeated()) .then(op.then(sum).repeated())
.foldl(|a, (kind, b)| { .foldl(|a, (kind, b)| {
Expr::BinOp(BinOp { let span = a.span.start..b.span.end;
Expr {
kind: ExprKind::BinOp(BinOp {
kind, kind,
lhs: Box::new(a), lhs: Box::new(a),
rhs: Box::new(b), rhs: Box::new(b),
span: 0..0, // lol todo span: span.clone(),
}) }),
span,
}
}); });
compare.labelled("comparison").boxed() compare.labelled("comparison").boxed()
}) })

View file

@ -1,6 +1,5 @@
--- ---
source: parser/src/parser.rs source: parser/src/parser.rs
assertion_line: 332
expression: r expression: r
--- ---
( (
@ -16,24 +15,33 @@ expression: r
span: 0..20, span: 0..20,
body: [ body: [
Expr( Expr(
BinOp( Expr {
kind: BinOp(
BinOp { BinOp {
kind: Add, kind: Add,
lhs: Literal( lhs: Expr {
kind: Literal(
Integer( Integer(
1, 1,
12..13, 12..13,
), ),
), ),
rhs: Literal( span: 12..13,
},
rhs: Expr {
kind: Literal(
Integer( Integer(
4, 4,
16..17, 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 source: parser/src/parser.rs
assertion_line: 338
expression: r expression: r
--- ---
( (
@ -16,38 +15,56 @@ expression: r
span: 0..32, span: 0..32,
body: [ body: [
Expr( Expr(
BinOp( Expr {
kind: BinOp(
BinOp { BinOp {
kind: Add, kind: Add,
lhs: BinOp( lhs: Expr {
kind: BinOp(
BinOp { BinOp {
kind: Div, kind: Div,
lhs: Literal( lhs: Expr {
kind: Literal(
Integer( Integer(
4, 4,
13..14, 13..14,
), ),
), ),
rhs: Call( span: 13..14,
},
rhs: Expr {
kind: Call(
Call { Call {
callee: Name( callee: Expr {
kind: Name(
"hallo", "hallo",
), ),
span: 17..22,
},
args: [], args: [],
}, },
), ),
span: 0..0, span: 17..22,
},
span: 13..22,
}, },
), ),
rhs: Literal( span: 13..22,
},
rhs: Expr {
kind: Literal(
Integer( Integer(
5, 5,
28..29, 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 source: parser/src/parser.rs
assertion_line: 344
expression: r expression: r
--- ---
( (
@ -21,24 +20,33 @@ expression: r
span: 0..26, span: 0..26,
body: [ body: [
Expr( Expr(
BinOp( Expr {
kind: BinOp(
BinOp { BinOp {
kind: Add, kind: Add,
lhs: Literal( lhs: Expr {
kind: Literal(
Integer( Integer(
1, 1,
18..19, 18..19,
), ),
), ),
rhs: Literal( span: 18..19,
},
rhs: Expr {
kind: Literal(
Integer( Integer(
5, 5,
22..23, 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 source: parser/src/parser.rs
assertion_line: 356
expression: r expression: r
--- ---
( (
@ -22,9 +21,12 @@ expression: r
body: [ body: [
IfStmt( IfStmt(
IfStmt { IfStmt {
cond: Name( cond: Expr {
kind: Name(
"false", "false",
), ),
span: 21..26,
},
body: [], body: [],
else_part: Some( else_part: Some(
Else( Else(

View file

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

View file

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

View file

@ -15,96 +15,138 @@ expression: r
span: 0..63, span: 0..63,
body: [ body: [
Expr( Expr(
UnaryOp( Expr {
kind: UnaryOp(
UnaryOp { UnaryOp {
expr: UnaryOp( expr: Expr {
kind: UnaryOp(
UnaryOp { UnaryOp {
expr: Literal( expr: Expr {
kind: Literal(
Integer( Integer(
5, 5,
19..20, 19..20,
), ),
), ),
span: 19..20,
},
kind: Deref, kind: Deref,
span: 0..0, span: 19..20,
}, },
), ),
span: 19..20,
},
kind: Neg, kind: Neg,
span: 0..0, span: 19..20,
}, },
), ),
span: 19..20,
},
), ),
Expr( Expr(
UnaryOp( Expr {
kind: UnaryOp(
UnaryOp { UnaryOp {
expr: Literal( expr: Expr {
kind: Literal(
Integer( Integer(
5, 5,
28..29, 28..29,
), ),
), ),
span: 28..29,
},
kind: AddrOf, kind: AddrOf,
span: 0..0, span: 28..29,
}, },
), ),
span: 28..29,
},
), ),
Expr( Expr(
BinOp( Expr {
kind: BinOp(
BinOp { BinOp {
kind: Add, kind: Add,
lhs: Literal( lhs: Expr {
kind: Literal(
Integer( Integer(
2, 2,
35..36, 35..36,
), ),
), ),
rhs: UnaryOp( span: 35..36,
},
rhs: Expr {
kind: UnaryOp(
UnaryOp { UnaryOp {
expr: Literal( expr: Expr {
kind: Literal(
Integer( Integer(
8, 8,
40..41, 40..41,
), ),
), ),
span: 40..41,
},
kind: AddrOf, kind: AddrOf,
span: 0..0, span: 40..41,
}, },
), ),
span: 0..0, span: 40..41,
},
span: 35..41,
}, },
), ),
span: 35..41,
},
), ),
Expr( Expr(
BinOp( Expr {
kind: BinOp(
BinOp { BinOp {
kind: Mul, kind: Mul,
lhs: UnaryOp( lhs: Expr {
kind: UnaryOp(
UnaryOp { UnaryOp {
expr: Literal( expr: Expr {
kind: Literal(
Integer( Integer(
6, 6,
48..49, 48..49,
), ),
), ),
span: 48..49,
},
kind: Deref, kind: Deref,
span: 0..0, span: 48..49,
}, },
), ),
rhs: UnaryOp( span: 48..49,
},
rhs: Expr {
kind: UnaryOp(
UnaryOp { UnaryOp {
expr: Literal( expr: Expr {
kind: Literal(
Integer( Integer(
8, 8,
53..54, 53..54,
), ),
), ),
span: 53..54,
},
kind: Deref, 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 source: parser/src/parser.rs
assertion_line: 368
expression: r expression: r
--- ---
( (
@ -28,12 +27,15 @@ expression: r
kind: U64, kind: U64,
}, },
rhs: Some( rhs: Some(
Literal( Expr {
kind: Literal(
Integer( Integer(
5, 5,
30..31, 30..31,
), ),
), ),
span: 30..31,
},
), ),
span: 0..0, span: 0..0,
}, },

View file

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