Impl blocks
This commit is contained in:
parent
d15fd0c748
commit
4cd65504f2
79
src/parse.rs
79
src/parse.rs
|
@ -33,8 +33,9 @@ impl<'a> ParseTree<'a> {
|
|||
Token::Struct => declarations.push(Definition::build_structure(lexer)),
|
||||
Token::Function => declarations.push(Definition::build_function(None, lexer)),
|
||||
Token::Interface => declarations.push(Definition::build_interface(lexer)),
|
||||
Token::Impl => declarations.push(Definition::build_impl(lexer)),
|
||||
Token::Identifier => associated_struct = Some(lexer.slice()),
|
||||
_ => lexer.panic_message("Expected function, struct, or interface"),
|
||||
_ => lexer.panic_message("Expected function, struct, impl block, or interface"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -51,14 +52,17 @@ pub enum Definition<'a> {
|
|||
},
|
||||
Function {
|
||||
associated_struct: Option<(&'a str, bool)>,
|
||||
name: &'a str,
|
||||
signature: FnSig<'a>,
|
||||
body: BranchBody<'a>,
|
||||
implementation: FnImpl<'a>,
|
||||
},
|
||||
Interface {
|
||||
name: &'a str,
|
||||
functions: Vec<(&'a str, FnSig<'a>)>,
|
||||
},
|
||||
Impl {
|
||||
structure: &'a str,
|
||||
interface: Option<&'a str>,
|
||||
functions: Vec<FnImpl<'a>>,
|
||||
},
|
||||
}
|
||||
|
||||
impl<'a> Definition<'a> {
|
||||
|
@ -90,20 +94,11 @@ impl<'a> Definition<'a> {
|
|||
associated_struct: Option<(&'a str, bool)>,
|
||||
lexer: &mut Lexer<'a>,
|
||||
) -> Self {
|
||||
let name = lexer.eat_expect_id();
|
||||
let (signature, tok) = FnSig::build(lexer);
|
||||
|
||||
if tok != Token::BraceOpen {
|
||||
lexer.panic_message("Expected open brace");
|
||||
}
|
||||
|
||||
let body = BranchBody::build(lexer);
|
||||
let implementation = FnImpl::build(lexer);
|
||||
|
||||
Self::Function {
|
||||
name,
|
||||
associated_struct,
|
||||
signature,
|
||||
body,
|
||||
implementation,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,6 +125,60 @@ impl<'a> Definition<'a> {
|
|||
|
||||
Self::Interface { name, functions }
|
||||
}
|
||||
|
||||
pub fn build_impl(lexer: &mut Lexer<'a>) -> Self {
|
||||
let structure = lexer.eat_expect_id();
|
||||
let interface = match lexer.next().unwrap() {
|
||||
Token::Identifier => {
|
||||
let interface = lexer.slice();
|
||||
lexer.eat_expect(Token::BraceOpen);
|
||||
Some(interface)
|
||||
}
|
||||
Token::BraceOpen => None,
|
||||
_ => lexer.panic_message("Expected interface name or opening brace"),
|
||||
};
|
||||
|
||||
let mut functions = Vec::new();
|
||||
loop {
|
||||
match lexer.next().unwrap() {
|
||||
Token::BraceClose => break,
|
||||
Token::Function => functions.push(FnImpl::build(lexer)),
|
||||
_ => lexer.panic_message("Expected function implementation or closing brace"),
|
||||
}
|
||||
}
|
||||
|
||||
Self::Impl {
|
||||
structure,
|
||||
interface,
|
||||
functions,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FnImpl<'a> {
|
||||
pub name: &'a str,
|
||||
pub signature: FnSig<'a>,
|
||||
pub body: BranchBody<'a>,
|
||||
}
|
||||
|
||||
impl<'a> FnImpl<'a> {
|
||||
pub fn build(lexer: &mut Lexer<'a>) -> Self {
|
||||
let name = lexer.eat_expect_id();
|
||||
let (signature, tok) = FnSig::build(lexer);
|
||||
|
||||
if tok != Token::BraceOpen {
|
||||
lexer.panic_message("Expected open brace");
|
||||
}
|
||||
|
||||
let body = BranchBody::build(lexer);
|
||||
|
||||
Self {
|
||||
name,
|
||||
signature,
|
||||
body,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
|
@ -8,10 +8,10 @@ struct ExampleStruct {
|
|||
i32 member,
|
||||
}
|
||||
|
||||
impl ExampleStruct ExampleInterfact {
|
||||
impl ExampleStruct ExampleInterface {
|
||||
fn method1() {}
|
||||
|
||||
fn method2(i32 arg1, i32 arg2) {}
|
||||
|
||||
fn method3(i32 arg1, i32 arg2) { arg1 + arg2 }
|
||||
fn method3(i32 arg1, i32 arg2) i32 { arg1 + arg2 }
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue