1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

hole span

This commit is contained in:
Natapat Samutpong 2022-03-13 12:41:23 +07:00
parent 74a3422271
commit 87d96378f4
3 changed files with 12 additions and 6 deletions

View file

@ -118,7 +118,7 @@ pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
// Remove all `Hole`(s) from the args // Remove all `Hole`(s) from the args
let index = args.0.iter().position(|arg| match arg.0 { let index = args.0.iter().position(|arg| match arg.0 {
Expr::Hole => true, Expr::Hole(..) => true,
_ => false _ => false
}); });
@ -152,7 +152,7 @@ pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
let mut largs = Vec::new(); let mut largs = Vec::new();
for arg in &args.0 { for arg in &args.0 {
match arg.0 { match arg.0 {
Expr::Hole => { Expr::Hole(..) => {
largs.push(lhs_ir.0.clone().unwrap()); largs.push(lhs_ir.0.clone().unwrap());
}, },
_ => { _ => {
@ -270,7 +270,12 @@ pub fn expr_to_ir(expr: &Expr) -> (Option<IRKind>, Option<LoweringError>) {
Expr::Boolean(value) => (Some(IRKind::Value { value: Value::Boolean(*value) }), None), Expr::Boolean(value) => (Some(IRKind::Value { value: Value::Boolean(*value) }), None),
Expr::String(value) => (Some(IRKind::Value { value: Value::String(value.clone()) }), None), Expr::String(value) => (Some(IRKind::Value { value: Value::String(value.clone()) }), None),
Expr::Identifier(value) => (Some(IRKind::Value { value: Value::Ident(value.clone()) }), None), Expr::Identifier(value) => (Some(IRKind::Value { value: Value::Ident(value.clone()) }), None),
Expr::Hole => (None, None), // Probably will never happen because it is catched in parser
Expr::Hole(start, end) => (None, Some(LoweringError {
span: *start..*end,
message: "Hole can only be used in piping, it is not allowed here.".to_string(),
note: None
})),
_ => { dbg!(expr); todo!() } _ => { dbg!(expr); todo!() }
} }
} }

View file

@ -36,7 +36,8 @@ pub enum Expr {
body: Vec<Spanned<Self>> body: Vec<Spanned<Self>>
}, },
Hole, // Hole for positional argument(s) in piping
Hole(usize, usize), // The usize is the span of the hole (prob should be single but whatever)
} }
fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>> + Clone { fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>> + Clone {
@ -45,11 +46,11 @@ fn expr_parser() -> impl Parser<Token, Vec<Spanned<Expr>>, Error = Simple<Token>
_ => Err(Simple::expected_input_found(span, Vec::new(), Some(token))), _ => Err(Simple::expected_input_found(span, Vec::new(), Some(token))),
}).labelled("identifier"); }).labelled("identifier");
let literal = filter_map(|span, token| match token { let literal = filter_map(|span: std::ops::Range<usize>, token| match token {
Token::Int(i) => Ok((Expr::Int(i), span)), Token::Int(i) => Ok((Expr::Int(i), span)),
Token::Boolean(b) => Ok((Expr::Boolean(b), span)), Token::Boolean(b) => Ok((Expr::Boolean(b), span)),
Token::String(s) => Ok((Expr::String(s), span)), Token::String(s) => Ok((Expr::String(s), span)),
Token::Hole => Ok((Expr::Hole, span)), Token::Hole => Ok((Expr::Hole(span.start, span.end), 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");