Function definitions expanded

This commit is contained in:
greg 2017-10-08 20:55:05 -07:00
parent df877830d3
commit 6dff8b029e

View File

@ -287,8 +287,9 @@ variant_specifier := IDENTIFIER | IDENTIFIER '{' typed_identifier_list '}' | IDE
typed_identifier_list := typed_identifier* typed_identifier_list := typed_identifier*
typed_identifier := IDENTIFIER type_anno typed_identifier := IDENTIFIER type_anno
func_declaration := 'fn' IDENTIFIER '(' param_list ')' func_declaration := 'fn' IDENTIFIER formal_param_list
param_list := (IDENTIFIER type_anno+ ',')* formal_param_list := '(' (formal_param ',')* ')'
formal_param := IDENTIFIER type_anno+
binding_declaration: 'var' IDENTIFIER '=' expression binding_declaration: 'var' IDENTIFIER '=' expression
| 'const' IDENTIFIER '=' expression | 'const' IDENTIFIER '=' expression
@ -404,13 +405,13 @@ pub enum Statement {
type ParamName = Rc<String>; type ParamName = Rc<String>;
type TraitName = Rc<String>; type TraitName = Rc<String>;
type FormalParamList = Vec<(ParamName, Option<TypeName>)>; type FormalParam = (ParamName, Option<TypeName>);
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum Declaration { pub enum Declaration {
FuncDecl { FuncDecl {
name: Rc<String>, name: Rc<String>,
params: FormalParamList, params: Vec<FormalParam>,
}, },
TypeDecl(Rc<String>, TypeBody), TypeDecl(Rc<String>, TypeBody),
TypeAlias(Rc<String>, Rc<String>), TypeAlias(Rc<String>, Rc<String>),
@ -645,18 +646,17 @@ impl Parser {
parse_method!(func_declaration(&mut self) -> ParseResult<Declaration> { parse_method!(func_declaration(&mut self) -> ParseResult<Declaration> {
expect!(self, Keyword(Func), "'fn'"); expect!(self, Keyword(Func), "'fn'");
let name = self.identifier()?; let name = self.identifier()?;
expect!(self, LParen, "'('"); let params = delimited!(self, LParen, '(', formal_param, Comma, RParen, ')');
let params = self.param_list()?; Ok(Declaration::FuncDecl { name, params })
expect!(self, RParen, "')'");
let decl = Declaration::FuncDecl {
name: name,
params: params
};
Ok(decl)
}); });
parse_method!(param_list(&mut self) -> ParseResult<FormalParamList> { parse_method!(formal_param(&mut self) -> ParseResult<FormalParam> {
Ok(vec!()) let name = self.identifier()?;
let ty = match self.peek() {
Colon => Some(self.type_anno()?),
_ => None
};
Ok((name, ty))
}); });
parse_method!(binding_declaration(&mut self) -> ParseResult<Declaration> { parse_method!(binding_declaration(&mut self) -> ParseResult<Declaration> {
@ -1112,6 +1112,11 @@ mod parse_tests {
params: vec![ex!(var!("a")), ex!(binexp!("+", IntLiteral(2), IntLiteral(2)))] params: vec![ex!(var!("a")), ex!(binexp!("+", IntLiteral(2), IntLiteral(2)))]
})])); })]));
parse_error!("a(b,,c)"); parse_error!("a(b,,c)");
parse_test!("fn a(b, c: Int)", AST(vec![Declaration(
FuncDecl { name: rc!(a), params: vec![
(rc!(b), None), (rc!(c), Some(ty!("Int")))
] })]));
} }
#[test] #[test]