Skip to content

Commit

Permalink
feat(ast): add String and return values
Browse files Browse the repository at this point in the history
Co-authored-by: Samyak S Sarnayak <[email protected]>
  • Loading branch information
ishwar00 and Samyak2 committed Nov 11, 2023
1 parent b4a46b4 commit a5953e6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
32 changes: 30 additions & 2 deletions compiler/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ pub enum Expr<'i> {
value: f64, // TODO: other float types?
span: Span<'i>,
},
String {
value: String,
span: Span<'i>,
},
FunctionCall {
function: Box<Expr<'i>>,
args: Args<'i>,
Expand Down Expand Up @@ -399,6 +403,15 @@ impl<'i> Node<'i> for Expr<'i> {
value: pair.as_str().parse().unwrap(),
span: pair.as_span(),
}),
Rule::RawStringLit => Ok(Expr::String {
value: pair.as_str().trim_matches('`').to_owned(),
span: pair.as_span(),
}),
Rule::InterpretedStringLit => Ok(Expr::String {
// TODO: parse escape characters
value: pair.as_str().trim_matches('"').to_owned(),
span: pair.as_span(),
}),
Rule::TupleExpr => {
let expr_list = pair
.into_inner()
Expand Down Expand Up @@ -457,6 +470,8 @@ impl<'i> Expr<'i> {
Rule::FloatLit
| Rule::IntLit
| Rule::Ident
| Rule::InterpretedStringLit
| Rule::RawStringLit
| Rule::FunctionCall
| Rule::TupleExpr => Expr::parse(primary),
invalid_rule => Err(BakugoParsingError::new(
Expand Down Expand Up @@ -567,7 +582,7 @@ impl<'i> Expr<'i> {

#[derive(Debug)]
pub enum Statement<'i> {
Return, // TODO: add expression list
Return(Option<Vec<Expr<'i>>>),
Declaration(Decl<'i>),
Expression(Expr<'i>),
}
Expand Down Expand Up @@ -712,7 +727,20 @@ impl<'i> Node<'i> for StatementList<'i> {
parsed_stmts.push(Statement::Expression(expr))
}

Rule::ReturnStmt => parsed_stmts.push(Statement::Return),
Rule::ReturnStmt => {
let expr_list = pair
.into_inner()
.next()
.unwrap()
.into_inner()
.map(Expr::parse)
.collect::<Result<Vec<_>, _>>()?;
parsed_stmts.push(Statement::Return(if expr_list.is_empty() {
None
} else {
Some(expr_list)
}))
}

Rule::Semicolon => {}

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/bakugo.pest
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ BigUValue = @{ "\\" ~ "U" ~ HexDigit{8} }
EscapedChar = @{ "\\" ~ ("a" | "b" | "f" | "n" | "r" | "t" | "v" | "\\" | "'" | "\"") }

/// string literals
StringLit = { RawStringLit | InterpretedStringLit }
StringLit = _{ RawStringLit | InterpretedStringLit }
// `UnicodeChar | NewLine` is equivalent to `ANY``
RawStringLit = @{ "`" ~ (!"`" ~ ANY)* ~ "`" }
InterpretedStringLit = @{ "\"" ~ (!"\"" ~ (ByteValue | UnicodeValue))* ~ "\"" }
Expand Down
15 changes: 15 additions & 0 deletions compiler/tests/examples/11_return_value.bakugo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func main() {
return 34;
return (34, "hey", 4.5,);
};


func primary() {
return func_call(
`hi
there `,
45,
(4, 2),
"hey"
);
};

0 comments on commit a5953e6

Please sign in to comment.