Skip to content

Commit

Permalink
Shorthand function syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jac3km4 committed Feb 22, 2021
1 parent b614e31 commit a6e5def
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
17 changes: 17 additions & 0 deletions compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,4 +581,21 @@ mod tests {
compiler.compile(sources)?;
Ok(())
}

#[test]
fn compile_class_with_shorthand_funcs() -> Result<(), Error> {
let sources = parser::parse(
"
public class ShorthandTest {
public func InstanceVal() -> String = ShorthandTest.StaticVal()
public static func StaticVal() -> String = \"static\"
}",
)
.unwrap();

let mut scripts = ScriptBundle::load(&mut Cursor::new(PREDEF))?;
let mut compiler = Compiler::new(&mut scripts.pool)?;
compiler.compile(sources)?;
Ok(())
}
}
4 changes: 3 additions & 1 deletion compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ peg::parser! {
pub rule function() -> FunctionSource
= declaration:decl(<keyword("func")>) _ "(" _ parameters:commasep(<param()>) _ ")" _ type_:func_type()? _ body:function_body()?
{ FunctionSource { declaration, type_, parameters, body } }
rule function_body() -> Seq = "{" _ body:seq() _ "}" { body }
rule function_body() -> Seq
= "{" _ body:seq() _ "}" { body }
/ pos:position!() "=" _ expr:expr() { Seq::new(vec![Expr::Return(Some(Box::new(expr)), Pos::new(pos))]) }

rule param() -> ParameterSource
= qualifiers:qualifiers() _ name:ident() _ type_:let_type()
Expand Down

0 comments on commit a6e5def

Please sign in to comment.