code builder initial

This commit is contained in:
NikVolf
2017-04-07 15:45:52 +03:00
parent 28aa6e9682
commit c0f0f2b571
3 changed files with 44 additions and 0 deletions

View File

@ -177,6 +177,40 @@ impl<F> SignaturesBuilder<F> where F: Invoke<SignatureBindings> {
} }
} }
pub struct FunctionDefinition {
pub signature: Signature,
pub code: elements::FuncBody,
}
impl Default for FunctionDefinition {
fn default() -> Self {
FunctionDefinition {
signature: Signature::TypeReference(0),
code: elements::FuncBody::empty(),
}
}
}
pub struct FunctionBuilder<F=Identity> {
callback: F,
code: FunctionDefinition,
}
impl FunctionBuilder {
pub fn new() -> Self {
FunctionBuilder::with_callback(Identity)
}
}
impl<F> FunctionBuilder<F> where F: Invoke<FunctionDefinition> {
pub fn with_callback(callback: F) -> Self {
FunctionBuilder {
callback: callback,
code: Default::default(),
}
}
}
/// New function builder. /// New function builder.
pub fn signatures() -> SignaturesBuilder { pub fn signatures() -> SignaturesBuilder {
SignaturesBuilder::new() SignaturesBuilder::new()

View File

@ -68,6 +68,11 @@ impl FuncBody {
FuncBody { locals: locals, opcodes: opcodes } FuncBody { locals: locals, opcodes: opcodes }
} }
/// List of individual opcodes
pub fn empty() -> Self {
FuncBody { locals: Vec::new(), opcodes: Opcodes::empty() }
}
/// Locals declared in function body. /// Locals declared in function body.
pub fn locals(&self) -> &[Local] { &self.locals } pub fn locals(&self) -> &[Local] { &self.locals }

View File

@ -15,6 +15,11 @@ impl Opcodes {
Opcodes(elements) Opcodes(elements)
} }
/// Empty expression with only `Opcode::End` opcode
pub fn empty() -> Self {
Opcodes(vec![Opcode::End])
}
/// List of individual opcodes /// List of individual opcodes
pub fn elements(&self) -> &[Opcode] { &self.0 } pub fn elements(&self) -> &[Opcode] { &self.0 }
} }