Skip to content

Commit 2ea519e

Browse files
committed
test -> self & update examples
1 parent c77c912 commit 2ea519e

File tree

6 files changed

+43
-30
lines changed

6 files changed

+43
-30
lines changed

examples/class.zen

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,36 @@ mod stdlib;
33
// ZenLang is not object oriented - therefore it doesn't have classes
44
// But you can make something similar
55
fn class_new {
6-
let inst = {"a" = 0, "b" = 0};
6+
let inst = {
7+
"add_a" = class_add_a,
8+
"add_b" = class_add_b,
9+
"print" = class_print,
10+
"a" = 0,
11+
"b" = 0
12+
};
713
return inst;
814
}
915

10-
fn class_add_a self n {
16+
fn class_add_a n {
1117
let self.a = self.a + n;
1218
return null;
1319
}
1420

15-
fn class_add_b self n {
21+
fn class_add_b n {
1622
let self.b = self.b + n;
1723
return null;
1824
}
1925

20-
fn class_print self {
26+
fn class_print {
2127
let res = self.a + self.b;
2228
println(res);
2329
return null;
2430
}
2531

2632
fn main {
2733
let class = class_new();
28-
class_add_a(class, 42);
29-
class_add_b(class, 69);
30-
class_print(class);
34+
class.add_a(42);
35+
class.add_b(69);
36+
class.print();
3137
return null;
3238
}

examples/stack.zen

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
11
mod stdlib;
22

33
fn stack_new {
4-
return {"data" = []};
4+
return {
5+
"top" = stack_top,
6+
"push" = stack_push,
7+
"pop" = stack_pop,
8+
"size" = stack_size,
9+
"is_empty" = stack_is_empty,
10+
"data" = []
11+
};
512
}
613

7-
fn stack_top self {
14+
fn stack_top {
815
return array_last(self.data);
916
}
1017

11-
fn stack_push self element {
18+
fn stack_push element {
1219
let self.data = array_push(self.data, element);
1320
return null;
1421
}
1522

16-
fn stack_pop self {
23+
fn stack_pop {
1724
let self.data = array_pop(self.data);
1825
return null;
1926
}
2027

21-
fn stack_size self {
28+
fn stack_size {
2229
return array_size(self.data);
2330
}
2431

25-
fn stack_is_empty self {
26-
return stack_size(self) == 0;
32+
fn stack_is_empty {
33+
return self.size() == 0;
2734
}
2835

2936
fn main {
3037
let stack = stack_new();
31-
stack_push(stack, 69);
32-
println(stack_top(stack));
33-
return stack_is_empty(stack);
38+
stack.push(69);
39+
println(stack.top());
40+
return stack.is_empty();
3441
}

zenlang/src/vm/opcodes/vm_call.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ impl VM {
1414
self.pc.sub_low(1);
1515
self.add_scope();
1616

17-
let this_name = &String::from("this");
17+
let this_name = &String::from("self");
1818
let scope = self.scopes.last_mut().unwrap();
1919
scope.create_if_doesnt_exist(this_name);
20-
*scope.get_mut(this_name).unwrap() = core::mem::take(&mut self.this);
20+
*scope.get_mut(this_name).unwrap() = core::mem::take(&mut self.self_var);
2121

2222
let start = self.bfas_stack_start.pop().unwrap();
2323
let end = self.bfas_stack_end.pop().unwrap();

zenlang/src/vm/opcodes/vm_iafs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl VM {
1919
self.error = format!("iafs failed: no more values on stack for array");
2020
return;
2121
}
22-
self.this = array.clone();
22+
self.self_var = array.clone();
2323

2424
match array {
2525
Value::Object(obj) => match &*obj.borrow() {

zenlang/src/vm/vm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct VM {
2121
pub platform: Option<Box<dyn Platform>>,
2222
pub global_scope: Scope,
2323
pub halted: bool,
24-
pub this: Value,
24+
pub self_var: Value,
2525
pub(crate) bfas_stack_start: Vec<i64>,
2626
pub(crate) bfas_stack_end: Vec<i64>,
2727
}
@@ -39,7 +39,7 @@ impl VM {
3939
platform: None,
4040
global_scope: Scope::new(),
4141
halted: false,
42-
this: Value::Null(),
42+
self_var: Value::Null(),
4343
bfas_stack_start: Vec::new(),
4444
bfas_stack_end: Vec::new(),
4545
};

zenlang/tests/vm_this.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ fn expect_to_return_obj(code: String, object: Object) {
6262
}
6363

6464
#[test]
65-
fn vm_test_this_1() {
65+
fn vm_test_self_1() {
6666
expect_to_return(
6767
r#"
6868
fn f2 {
69-
let this.hi = this.hi + 3;
69+
let self.hi = self.hi + 3;
7070
}
7171
7272
fn f {
73-
let this.hi = 1;
74-
this.test2();
73+
let self.hi = 1;
74+
self.test2();
7575
}
7676
7777
fn main {
@@ -88,20 +88,20 @@ fn main {
8888
}
8989

9090
#[test]
91-
fn vm_test_this_2() {
91+
fn vm_test_self_2() {
9292
expect_to_return(
9393
r#"
9494
fn f2 {
95-
let this.hi = 3;
95+
let self.hi = 3;
9696
}
9797
9898
fn f {
99-
let this.hi = 1;
99+
let self.hi = 1;
100100
let obj = {
101101
"test" = f2
102102
};
103103
obj.test();
104-
let this.hi = obj.hi - 1;
104+
let self.hi = obj.hi - 1;
105105
}
106106
107107
fn main {

0 commit comments

Comments
 (0)