Skip to content

Commit 27f60c5

Browse files
committed
ctor functions
1 parent c4d815f commit 27f60c5

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

zenlang/src/ast/function.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ impl Compile for AstFunction {
6565
let module = compiler.get_module();
6666
{
6767
let name = self.name.to_string();
68+
let ctor = self.attrs.contains(&FunctionAttribute::Ctor);
6869
module.functions.push(ModuleFunction::new(
6970
name,
7071
module.opcodes.len() as u32,
7172
self.args.len() as u64,
73+
ctor,
7274
));
7375
}
7476

zenlang/src/func_attr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ use alloc::string::*;
33
#[derive(PartialEq)]
44
pub enum FunctionAttribute {
55
Naked,
6+
Ctor,
67
}
78

89
impl FunctionAttribute {
910
pub fn map(name: String) -> Option<FunctionAttribute> {
1011
if name == "naked" {
1112
return Some(FunctionAttribute::Naked);
13+
} else if name == "ctor" {
14+
return Some(FunctionAttribute::Ctor);
1215
}
1316
return None;
1417
}

zenlang/src/module.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ pub struct ModuleFunction {
2121
pub addr: u32,
2222
/// Argument count
2323
pub args_count: u64,
24+
/// Is a constructor function
25+
pub ctor: bool,
2426
}
2527

2628
impl ModuleFunction {
27-
pub fn new(name: String, addr: u32, args_count: u64) -> ModuleFunction {
29+
pub fn new(name: String, addr: u32, args_count: u64, ctor: bool) -> ModuleFunction {
2830
return ModuleFunction {
2931
name: name,
3032
addr: addr,
3133
args_count: args_count,
34+
ctor: ctor,
3235
};
3336
}
3437
}

zenlang/src/stdlib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ fn number str {
113113
"_vmcall_ret_unsafe_1".into(),
114114
module.opcodes.len() as u32,
115115
1,
116+
false,
116117
));
117118
module.opcodes.push(Opcode::Dynvmcall());
118119
module.opcodes.push(Opcode::Ret());
@@ -121,6 +122,7 @@ fn number str {
121122
"_vmcall_ret_unsafe_2".into(),
122123
module.opcodes.len() as u32,
123124
2,
125+
false,
124126
));
125127
module.opcodes.push(Opcode::Dynvmcall());
126128
module.opcodes.push(Opcode::Ret());
@@ -129,6 +131,7 @@ fn number str {
129131
"_vmcall_ret_unsafe_3".into(),
130132
module.opcodes.len() as u32,
131133
3,
134+
false,
132135
));
133136
module.opcodes.push(Opcode::Dynvmcall());
134137
module.opcodes.push(Opcode::Ret());
@@ -137,6 +140,7 @@ fn number str {
137140
"_vmcall_ret_unsafe_4".into(),
138141
module.opcodes.len() as u32,
139142
4,
143+
false,
140144
));
141145
module.opcodes.push(Opcode::Dynvmcall());
142146
module.opcodes.push(Opcode::Ret());

zenlang/src/vm/vm.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,38 @@ impl VM {
4646
}
4747

4848
pub fn load_module(&mut self, module: &Module) -> Result<(), String> {
49+
// check if already loaded
50+
for m in self.modules.iter_mut() {
51+
if m.name == module.name {
52+
return Ok(());
53+
}
54+
}
55+
4956
self.modules.push(module.clone());
5057

58+
for func in module.functions.iter() {
59+
if func.ctor {
60+
self.check_stack_overflow();
61+
self.pc.set_low(func.addr);
62+
self.pc.set_high((self.modules.len() - 1) as u32);
63+
self.add_scope();
64+
65+
loop {
66+
if !self.step() {
67+
break;
68+
}
69+
}
70+
71+
if !self.error.is_empty() {
72+
return Err(format!(
73+
"in constructor of module {}: {}",
74+
module.name, self.error
75+
));
76+
}
77+
self.halted = false;
78+
}
79+
}
80+
5181
for var in module.globals.iter() {
5282
if self.global_scope.get(var).is_some() {
5383
return Err(format!(

0 commit comments

Comments
 (0)