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 {
str[1..str.len() - 2].to_owned(), kind: ExprKind::Literal(Literal::String(
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: Box::new(callee), callee.span.start..args.last().map(|e| e.span.end).unwrap_or(callee.span.end);
args, Expr {
}) kind: ExprKind::Call(Call {
callee: Box::new(callee),
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: Box::new(rhs), Expr {
kind, kind: ExprKind::UnaryOp(UnaryOp {
span: 0..0, // lol todo expr: Box::new(rhs),
}) kind,
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;
kind, Expr {
lhs: Box::new(a), kind: ExprKind::BinOp(BinOp {
rhs: Box::new(b), kind,
span: 0..0, // lol todo lhs: Box::new(a),
}) rhs: Box::new(b),
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;
kind, Expr {
lhs: Box::new(a), kind: ExprKind::BinOp(BinOp {
rhs: Box::new(b), kind,
span: 0..0, // lol todo lhs: Box::new(a),
}) rhs: Box::new(b),
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;
kind, Expr {
lhs: Box::new(a), kind: ExprKind::BinOp(BinOp {
rhs: Box::new(b), kind,
span: 0..0, // lol todo lhs: Box::new(a),
}) rhs: Box::new(b),
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 {
BinOp { kind: BinOp(
kind: Add, BinOp {
lhs: Literal( kind: Add,
Integer( lhs: Expr {
1, kind: Literal(
12..13, Integer(
), 1,
), 12..13,
rhs: Literal( ),
Integer( ),
4, span: 12..13,
16..17, },
), rhs: Expr {
), kind: Literal(
span: 0..0, Integer(
}, 4,
), 16..17,
),
),
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 {
BinOp { kind: BinOp(
kind: Add, BinOp {
lhs: BinOp( kind: Add,
BinOp { lhs: Expr {
kind: Div, kind: BinOp(
lhs: Literal( BinOp {
Integer( kind: Div,
4, lhs: Expr {
13..14, kind: Literal(
), Integer(
), 4,
rhs: Call( 13..14,
Call { ),
callee: Name( ),
"hallo", span: 13..14,
), },
args: [], 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: Expr {
rhs: Literal( 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 {
BinOp { kind: BinOp(
kind: Add, BinOp {
lhs: Literal( kind: Add,
Integer( lhs: Expr {
1, kind: Literal(
18..19, Integer(
), 1,
), 18..19,
rhs: Literal( ),
Integer( ),
5, span: 18..19,
22..23, },
), rhs: Expr {
), kind: Literal(
span: 0..0, Integer(
}, 5,
), 22..23,
),
),
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 {
"false", kind: Name(
), "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 {
"false", kind: Name(
), "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 {
Integer( kind: Literal(
2, Integer(
36..37, 2,
36..37,
),
), ),
), span: 36..37,
},
), ),
span: 0..0, span: 0..0,
}, },
@ -57,12 +60,15 @@ expression: r
), ),
}, },
rhs: Some( rhs: Some(
Literal( Expr {
Integer( kind: Literal(
25, Integer(
53..55, 25,
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 {
UnaryOp { kind: UnaryOp(
expr: UnaryOp( UnaryOp {
UnaryOp { expr: Expr {
expr: Literal( 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( Integer(
5, 5,
19..20, 28..29,
), ),
), ),
kind: Deref, span: 28..29,
span: 0..0,
}, },
), kind: AddrOf,
kind: Neg, span: 28..29,
span: 0..0, },
}, ),
), span: 28..29,
},
), ),
Expr( Expr(
UnaryOp( Expr {
UnaryOp { kind: BinOp(
expr: Literal( BinOp {
Integer( kind: Add,
5, lhs: Expr {
28..29, kind: Literal(
), Integer(
), 2,
kind: AddrOf, 35..36,
span: 0..0, ),
}, ),
), 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( Expr(
BinOp( Expr {
BinOp { kind: BinOp(
kind: Add, BinOp {
lhs: Literal( kind: Mul,
Integer( lhs: Expr {
2, kind: UnaryOp(
35..36, UnaryOp {
), expr: Expr {
), kind: Literal(
rhs: UnaryOp( Integer(
UnaryOp { 6,
expr: Literal( 48..49,
Integer( ),
8, ),
40..41, span: 48..49,
), },
kind: Deref,
span: 48..49,
},
), ),
kind: AddrOf, span: 48..49,
span: 0..0,
}, },
), rhs: Expr {
span: 0..0, kind: UnaryOp(
}, UnaryOp {
), expr: Expr {
), kind: Literal(
Expr( Integer(
BinOp( 8,
BinOp { 53..54,
kind: Mul, ),
lhs: UnaryOp( ),
UnaryOp { span: 53..54,
expr: Literal( },
Integer( kind: Deref,
6, span: 53..54,
48..49, },
),
), ),
kind: Deref, span: 53..54,
span: 0..0,
}, },
), span: 48..54,
rhs: UnaryOp( },
UnaryOp { ),
expr: Literal( span: 48..54,
Integer( },
8,
53..54,
),
),
kind: Deref,
span: 0..0,
},
),
span: 0..0,
},
),
), ),
], ],
}, },

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 {
Integer( kind: Literal(
5, Integer(
30..31, 5,
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 {
"false", kind: Name(
), "false",
),
span: 24..29,
},
body: [], body: [],
span: 18..32, span: 18..32,
}, },