feat: 将 kernel 接入 CLI 流程
This commit is contained in:
Generated
+1
@@ -762,6 +762,7 @@ dependencies = [
|
|||||||
"clap",
|
"clap",
|
||||||
"oxide_api",
|
"oxide_api",
|
||||||
"oxide_compiler",
|
"oxide_compiler",
|
||||||
|
"oxide_kernel",
|
||||||
"oxide_parser",
|
"oxide_parser",
|
||||||
"oxide_vm",
|
"oxide_vm",
|
||||||
"rustyline",
|
"rustyline",
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ path = "src/main.rs"
|
|||||||
oxide_parser = { path = "../oxide_parser" }
|
oxide_parser = { path = "../oxide_parser" }
|
||||||
oxide_compiler = { path = "../oxide_compiler" }
|
oxide_compiler = { path = "../oxide_compiler" }
|
||||||
oxide_vm = { path = "../oxide_vm" }
|
oxide_vm = { path = "../oxide_vm" }
|
||||||
|
oxide_kernel = { path = "../oxide_kernel" }
|
||||||
oxide_api = { path = "../oxide_api" }
|
oxide_api = { path = "../oxide_api" }
|
||||||
clap.workspace = true
|
clap.workspace = true
|
||||||
rustyline.workspace = true
|
rustyline.workspace = true
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
|
#![allow(clippy::arc_with_non_send_sync)]
|
||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::process::ExitCode;
|
use std::process::ExitCode;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use ansi_term::Colour::Red;
|
use ansi_term::Colour::Red;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use oxide_compiler::compiler::Compiler;
|
use oxide_compiler::compiler::Compiler;
|
||||||
|
use oxide_kernel::kernel::{KernelConfig, OxideKernel};
|
||||||
|
use oxide_kernel::string_forge::StringForge;
|
||||||
use oxide_parser::Allocator;
|
use oxide_parser::Allocator;
|
||||||
use oxide_vm::vm::Vm;
|
use oxide_vm::vm_pool::VmPool;
|
||||||
use oxide_vm::JsValue;
|
use oxide_vm::JsValue;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
@@ -55,7 +60,18 @@ fn main() -> ExitCode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn make_kernel() -> Arc<OxideKernel> {
|
||||||
|
Arc::new(OxideKernel::new(KernelConfig::standard()))
|
||||||
|
}
|
||||||
|
|
||||||
fn eval(code: &str) -> ExitCode {
|
fn eval(code: &str) -> ExitCode {
|
||||||
|
let kernel = make_kernel();
|
||||||
|
let pool = VmPool::new(
|
||||||
|
Arc::clone(&kernel),
|
||||||
|
kernel.config().min_pool_size,
|
||||||
|
kernel.config().max_pool_size,
|
||||||
|
);
|
||||||
|
|
||||||
let allocator = Allocator::default();
|
let allocator = Allocator::default();
|
||||||
let program = match oxide_parser::parse(&allocator, code) {
|
let program = match oxide_parser::parse(&allocator, code) {
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
@@ -68,7 +84,7 @@ fn eval(code: &str) -> ExitCode {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let compiler = Compiler::new();
|
let compiler = Compiler::new();
|
||||||
let module = match compiler.compile(&program) {
|
let module = match kernel.code_forge().get_or_compile(&program, &compiler) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("{}", Red.paint(err));
|
eprintln!("{}", Red.paint(err));
|
||||||
@@ -76,10 +92,10 @@ fn eval(code: &str) -> ExitCode {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut vm = Vm::new();
|
let mut guard = pool.spawn();
|
||||||
match vm.run(&module) {
|
match guard.vm_mut().run(&module) {
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
format_result(&vm, result);
|
format_result(kernel.string_forge().as_ref(), result);
|
||||||
ExitCode::SUCCESS
|
ExitCode::SUCCESS
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -89,9 +105,9 @@ fn eval(code: &str) -> ExitCode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_result(vm: &Vm, val: JsValue) {
|
fn format_result(string_forge: &StringForge, val: JsValue) {
|
||||||
if val.is_string() {
|
if val.is_string() {
|
||||||
if let Some(s) = vm.lookup_str(val) {
|
if let Some(s) = string_forge.lookup(val.as_string_index()) {
|
||||||
println!("\"{s}\"");
|
println!("\"{s}\"");
|
||||||
} else {
|
} else {
|
||||||
println!("{val}");
|
println!("{val}");
|
||||||
@@ -111,7 +127,7 @@ fn format_result(vm: &Vm, val: JsValue) {
|
|||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
print!("{:?}: ", i);
|
print!("{:?}: ", i);
|
||||||
print_value(vm, prop_val);
|
print_value(string_forge, prop_val);
|
||||||
}
|
}
|
||||||
println!("}}");
|
println!("}}");
|
||||||
} else if val.is_undefined() {
|
} else if val.is_undefined() {
|
||||||
@@ -121,9 +137,9 @@ fn format_result(vm: &Vm, val: JsValue) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_value(vm: &Vm, val: JsValue) {
|
fn print_value(string_forge: &StringForge, val: JsValue) {
|
||||||
if val.is_string() {
|
if val.is_string() {
|
||||||
if let Some(s) = vm.lookup_str(val) {
|
if let Some(s) = string_forge.lookup(val.as_string_index()) {
|
||||||
print!("\"{s}\"");
|
print!("\"{s}\"");
|
||||||
} else {
|
} else {
|
||||||
print!("{val}");
|
print!("{val}");
|
||||||
|
|||||||
Reference in New Issue
Block a user