Skip to content

Commit 5ecd788

Browse files
committed
update
1 parent a9739a3 commit 5ecd788

File tree

5 files changed

+83
-12
lines changed

5 files changed

+83
-12
lines changed

src/fs/rom.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,43 @@ use zenlang::tokenizer;
77

88
static SHELL_ZENLANG_CODE: &'static str = r#"
99
mod stdlib;
10+
mod zenos;
11+
12+
fn execute line {
13+
if line == "" {
14+
return null;
15+
}
16+
let splitted = str_split(line, " ");
17+
18+
let command = splitted[0];
19+
if command == "hello" {
20+
println("hi there!");
21+
} elif command == "ver" {
22+
ver();
23+
} else {
24+
println("unknown command");
25+
}
26+
return null;
27+
}
1028
1129
fn main {
1230
while true {
1331
print("> ");
1432
let s = get_string();
33+
execute(s);
1534
}
35+
return null;
36+
}
37+
"#;
38+
static ZENOS_ZENLANG_CODE: &'static str = r#"
39+
mod stdlib;
40+
fn ver {
41+
println("ZenOS nightly");
42+
println("ZenLang nightly");
43+
println("ZenLang constants:");
44+
print("MAX_STACK_SIZE = ");
45+
println(_vmcall_ret_unsafe_1(50));
46+
return null;
1647
}
1748
"#;
1849

@@ -42,6 +73,10 @@ fn compile_code_into(code: String, module_name: String, out: String) {
4273
Ok(bytes) => {
4374
//
4475
if let Some(fs) = get_fs() {
76+
if let Err(e) = fs.create_file(out.clone()) {
77+
println!("[rom] /bin/shell.zenc: {}", e);
78+
return;
79+
}
4580
if let Err(e) = fs.write_file(out, bytes) {
4681
println!("[rom] write error: {}", e);
4782
return;
@@ -62,14 +97,15 @@ pub fn set_rom() {
6297
println!("[rom] /bin: {}", e);
6398
}
6499

65-
if let Err(e) = fs.create_file("/bin/shell.zenc".into()) {
66-
println!("[rom] /bin/shell.zenc: {}", e);
67-
}
68-
println!("{:?}", fs);
69100
compile_code_into(
70101
SHELL_ZENLANG_CODE.into(),
71102
"zenshell".into(),
72103
"/bin/shell.zenc".into(),
73104
);
105+
compile_code_into(
106+
ZENOS_ZENLANG_CODE.into(),
107+
"zenos".into(),
108+
"/lib/zenos.zenc".into(),
109+
);
74110
}
75111
}

src/init/main.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use alloc::string::*;
44
use uefi::println;
55
use zenlang::module::Module;
66

7-
pub fn main() -> Result<(), String> {
8-
let mut process_manager = ProcessManager::new();
9-
10-
// load shell
7+
pub fn run_zenc_process_manager(
8+
process_manager: &mut ProcessManager,
9+
path: &str,
10+
) -> Result<(), String> {
1111
if let Some(fs) = get_fs() {
12-
match fs.read_file("/bin/shell.zenc".into()) {
12+
match fs.read_file(path.into()) {
1313
Ok(bytes) => {
1414
let mut module = Module::new();
1515
if let Err(e) = module.load(bytes) {
@@ -26,6 +26,7 @@ pub fn main() -> Result<(), String> {
2626
}
2727

2828
process_manager.append_process(process);
29+
return Ok(());
2930
}
3031
Err(e) => {
3132
return Err(e.into());
@@ -34,7 +35,14 @@ pub fn main() -> Result<(), String> {
3435
} else {
3536
return Err("get_fs failed".into());
3637
}
38+
}
3739

40+
pub fn main() -> Result<(), String> {
41+
let mut process_manager = ProcessManager::new();
42+
43+
if let Err(e) = run_zenc_process_manager(&mut process_manager, "/bin/shell.zenc") {
44+
return Err(e);
45+
}
3846
println!("[main] shell loading successful");
3947

4048
// step all processes

src/lang/platform.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
use crate::fs::global::get_fs;
12
use crate::io::get_string;
3+
use alloc::format;
24
use alloc::string::*;
35
use alloc::vec::*;
46
use uefi::print;
57
use zenlang::module::Module;
68
use zenlang::platform;
79
use zenlang::stdlib::*;
10+
use zenlang::value::*;
11+
use zenlang::vm::*;
812

913
pub struct Platform {}
1014

@@ -27,10 +31,31 @@ impl platform::Platform for Platform {
2731
let module = compile_stdlib_module();
2832
return Some(module);
2933
}
34+
if let Some(fs) = get_fs() {
35+
if let Ok(bytes) = fs.read_file(format!("/lib/{}.zenc", name)) {
36+
let mut module = Module::new();
37+
if let Err(_) = module.load(bytes) {
38+
return None;
39+
}
40+
return Some(module);
41+
}
42+
}
3043
return None;
3144
}
3245
fn read_file_bytes(&self, _name: String) -> Option<Vec<u8>> {
3346
return None;
3447
}
3548
fn write_file_bytes(&self, _name: String, _bytes: Vec<u8>) {}
49+
fn vmcall(&self, vm: &mut VM, index: u8) -> bool {
50+
match index {
51+
50 => {
52+
// get max stack size
53+
vm.stack.push(Value::Number(MAX_STACK_SIZE as f64));
54+
return true;
55+
}
56+
_ => {
57+
return false;
58+
}
59+
}
60+
}
3661
}

src/process/process_manager.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ impl ProcessManager {
4444
println!("runtime error at pc = {}:{}", pc.get_low(), pc.get_high(),);
4545
println!("-- end runtime error --");
4646
} else {
47-
println!("{} returned {}", process.pid, process.vm.ret);
47+
println!("pid {} returned {}", process.pid, process.vm.ret);
4848
}
4949
}
5050
}
5151
}
5252

5353
pub fn remove_stalling_processes(&mut self) {
54-
for i in 0..self.processes.len() {
54+
let mut i: usize = self.processes.len();
55+
while i > 0 {
56+
i -= 1;
5557
if self.processes[i].stalling {
5658
self.processes.remove(i);
5759
}

0 commit comments

Comments
 (0)