Skip to content

Commit

Permalink
parsing '&mut' into pointer for, e.g. function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
treemcgee42 committed Jul 14, 2023
1 parent 0fac8c6 commit b10823b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cobalt-ast/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,10 @@ pub fn pre_op<'ctx>(loc: SourceSpan, (mut val, vloc): (Value<'ctx>, SourceSpan),
_ => Err(err)
}
Type::Int(s, u) => match op {
"&mut" => {
val.data_type = Type::Pointer(Box::new(Type::Mut(Box::new(Type::Int(s, u)))));
Ok(val)
}
"+" => {
val.data_type = Type::Int(s, u);
Ok(val)
Expand Down
26 changes: 25 additions & 1 deletion cobalt-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,8 +1454,32 @@ fn expr(mode: u8, src: &str, start: usize) -> ParserReturn<Box<dyn AST>> {
else {break}
process(ignored, &mut src, &mut start, &mut errs);
}

let mut cleaned_ops = Vec::with_capacity(ops.len());
if ops.len() > 0 {
let mut i=0;
let mut just_combined = false;
while i < ops.len()-1 {
if ops[i].1 == "&" && ops[i+1].1 == "mut" {
cleaned_ops.push((ops[i].0, "&mut".to_string()));
i += 2;
just_combined = true;
} else {
cleaned_ops.push(ops[i].clone());
i += 1;
just_combined = false;
}
}

if !just_combined {
cleaned_ops.push(ops[ops.len()-1].clone());
}
}

let ast = process(postfix, &mut src, &mut start, &mut errs)?.0;
let ast = ops.into_iter().rfold(ast, |ast, (loc, op)| Box::new(PrefixAST::new((loc, op.len()).into(), op, ast)) as _);
// Start from the rightmost prefix operator and work left (right associative).
let ast = cleaned_ops.into_iter().rfold(ast, |ast, (loc, op)| Box::new(PrefixAST::new((loc, op.len()).into(), op, ast)) as _);

Some((ast, (begin..start).into(), src, errs))
}
// TODO: Macros would be more DRY, maybe switch to them?
Expand Down

0 comments on commit b10823b

Please sign in to comment.