Skip to content

Commit f857833

Browse files
committed
Brokten Code Rewrite
1 parent dde0900 commit f857833

File tree

11 files changed

+655
-213
lines changed

11 files changed

+655
-213
lines changed

interpreter/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ crate-type = ["rlib"]
1616
lealang_chalk_rs = "1"
1717
paste = "1"
1818
phf = { version = "0.11.3", features = ["macros"], optional = true }
19+
tokio = { version = "1.44.2", features = ["rt", "sync", "time"] }
1920

2021
[features]
2122
default = ["phf"]
2223
phf = ["dep:phf"]
23-
parallel = []

interpreter/src/lib.rs

Lines changed: 31 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
#![feature(concat_idents)]
44
#![feature(macro_metavar_expr)]
55
#![feature(get_mut_unchecked)]
6+
#![feature(impl_trait_in_bindings)]
67

7-
use std::{
8-
collections::HashMap,
9-
process,
10-
time::{Duration, Instant},
11-
};
8+
use std::{collections::HashMap, process, sync::Arc, time::Instant};
129

1310
pub use paste::paste;
1411

@@ -21,14 +18,10 @@ pub use runtime::RuntimeValue;
2118
#[cfg(feature = "phf")]
2219
pub use phf;
2320

24-
mod ipreter;
25-
26-
#[cfg(feature = "parallel")]
2721
mod parallel_ipreter;
2822

29-
#[cfg(feature = "parallel")]
3023
mod scheduler;
31-
#[cfg(feature = "parallel")]
24+
3225
pub use scheduler::Scheduler;
3326

3427
#[macro_use]
@@ -37,8 +30,8 @@ pub mod types;
3730
pub mod val;
3831

3932
pub(crate) use package::*;
40-
pub use types::{Extends, Heap, LanguagePackages, MethodRes, PrototypeDocs};
4133
use types::ExtendsInternal;
34+
pub use types::{Extends, Heap, LanguagePackages, MethodRes, PrototypeDocs};
4235
pub use val::*;
4336

4437
pub use lealang_chalk_rs::Chalk;
@@ -71,59 +64,53 @@ pub struct RespPackage {
7164
}
7265

7366
pub struct Application<'a> {
74-
code: HashMap<String, String>,
75-
#[cfg(feature = "parallel")]
76-
scheduler: scheduler::Scheduler,
67+
code: Arc<Structure>,
7768
pub(crate) pkg: LanguagePackages<'a>,
78-
entry: &'a str,
79-
heap: Option<Heap>,
80-
81-
// Resolve files
82-
module_resolver: Box<dyn FnMut(&str) -> Vec<u8>>,
8369
// Resolve path from mod name
8470
pkg_resolver: Box<dyn FnMut(&str, bool) -> Vec<RespPackage>>,
8571
// Log in case of full access request
8672
log_info: Box<dyn FnMut(&str) -> ()>,
87-
inst: Instant,
8873
}
8974

9075
unsafe impl Send for Application<'_> {}
9176
unsafe impl Sync for Application<'_> {}
9277

78+
pub type Args = Vec<&'static str>;
79+
80+
pub enum LeadCode {
81+
// Lead Modules will be lazily used
82+
LeadModule(&'static str),
83+
// Lead Code should be instantly made ready
84+
Code(Vec<Args>),
85+
}
86+
87+
pub type Structure = HashMap<
88+
// File
89+
&'static str,
90+
// Code
91+
LeadCode,
92+
>;
93+
9394
impl<'a> Application<'a> {
9495
pub fn new<
95-
T: FnMut(&str) -> Vec<u8> + 'static,
96+
T: FnOnce() -> Structure,
9697
F: FnMut(&str, bool) -> Vec<RespPackage> + 'static,
9798
R: FnMut(&str) -> () + 'static,
9899
>(
99-
file: &'a str,
100-
mut fs_resolver: T,
101100
dll_resolver: F,
102101
requested_perm: R,
102+
structure: T,
103103
) -> Self {
104-
let main = String::from_utf8(fs_resolver(file)).expect("Invalid utf8");
104+
let code = Arc::new(structure());
105105

106-
let mut code = HashMap::new();
107-
code.insert(":entry".to_string(), main);
108106
Self {
109107
code,
110-
#[cfg(feature = "parallel")]
111-
scheduler: Scheduler::new(),
112108
pkg: LanguagePackages::new(),
113-
heap: None,
114-
entry: &file,
115-
module_resolver: Box::new(fs_resolver),
116109
pkg_resolver: Box::new(dll_resolver),
117110
log_info: Box::new(requested_perm),
118-
inst: Instant::now(),
119111
}
120112
}
121113

122-
pub fn add_file(&mut self, name: String, file: String) -> &mut Self {
123-
self.code.insert(name, file);
124-
self
125-
}
126-
127114
pub fn add_pkg<T: Package + 'static>(&mut self, package: T) -> &mut Self {
128115
self.pkg.import(package);
129116
self
@@ -159,35 +146,17 @@ impl<'a> Application<'a> {
159146
}
160147

161148
/// ⚠️ This function still is panicking
162-
pub fn run_non(mut self) -> Duration {
163-
ipreter::interpret(":entry", &mut self);
164-
165-
self.inst.elapsed()
166-
}
167-
168-
#[cfg(feature = "parallel")]
169-
/// ⚠️ This function still is panicking
170-
pub fn run_non_parallel(mut self) -> Duration {
171-
parallel_ipreter::schedule(&mut self);
172-
173-
self.inst.elapsed()
149+
pub fn run_non(mut self) {
150+
parallel_ipreter::schedule(&mut self)
174151
}
175152

176-
pub fn run(mut self, time: bool) -> ! {
177-
self.heap = Some(Heap::new(self.pkg.extends.clone()));
178-
let dur = self.run_non();
153+
pub fn run(self, time: bool) -> ! {
154+
// Start the Timer NOW!!!
155+
let inst = Instant::now();
179156

180-
if time {
181-
println!("\nTime Elasped: {:?}", dur);
182-
}
183-
184-
process::exit(0)
185-
}
157+
self.run_non();
186158

187-
#[cfg(feature = "parallel")]
188-
pub fn run_parallel(mut self, time: bool) -> ! {
189-
self.heap = Some(Heap::new(self.pkg.extends.clone()));
190-
let dur = self.run_non_parallel();
159+
let dur = inst.elapsed();
191160

192161
if time {
193162
println!("\nTime Elasped: {:?}", dur);

0 commit comments

Comments
 (0)