Skip to content

Commit 1c6edf7

Browse files
committed
fix comments (?)
1 parent 57f2955 commit 1c6edf7

File tree

10 files changed

+95
-19
lines changed

10 files changed

+95
-19
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,22 @@ Native data types do have native methods, for example int conversion:
129129
neko s = 1;
130130
log(s->to_string()) @ You won't notice any difference, but if you use typeof you'll see magic.
131131
```
132-
Note that not all data types have methods yet, this can be found [here](https://github.com/Rubenennj/nekoscript/tree/dev/src/native/prototypes)
132+
Note that not all data types have methods yet, this can be found [here](https://github.com/Rubenennj/nekoscript/tree/dev/src/native/prototypes)
133+
134+
# Loading other files
135+
A new method was added in 1.1.0, named `load_file`, this method loads code specified at path.
136+
Give next structure:
137+
index.neko
138+
```
139+
neko util = load_file("./util.neko");
140+
141+
log(util->join("me", "this")); @ Outputs "me this"
142+
```
143+
144+
util.neko
145+
```
146+
public->join = dyn (x, y) {
147+
@ this will join x with y.
148+
return x + " " + y
149+
};
150+
```

src/core/interpreter.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ pub struct Interpreter {
2626
pub type IReturn = Result<Rc<RefCell<DataTypes>>, ReturnTypes>;
2727

2828
impl Interpreter {
29-
pub fn run(&self) {
30-
let scope = Scope::new();
29+
pub fn run(&self, scope: Option<&Scope>) {
30+
if !scope.is_some() {
31+
return self.run(Some(&Scope::new()))
32+
}
33+
34+
let scope = scope.unwrap();
3135

3236
for node in self.nodes.iter() {
33-
match self.execute(&scope, node) {
37+
match self.execute(scope, node) {
3438
Err(err) => match err {
3539
ReturnTypes::RuntimeError(str) => {
3640
println!("RuntimeError: {}\nFile: {}", str, self.path);

src/core/token_stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl<'a> TokenStream<'a> {
166166

167167
if is_comment(char) {
168168
self.skip_comment();
169-
return self.read_next()
169+
return parse_expression(self)
170170
} else if is_op(char) {
171171
return parse_op(self);
172172
} else if char.eq(&b'[') {

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ fn main() {
4646

4747
let itr = Interpreter::new(stream.nodes, path.to_str().unwrap().to_string());
4848

49-
itr.run();
49+
itr.run(None);
5050
}

src/native/load_file.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
use std::cell::RefCell;
3+
use std::collections::HashMap;
4+
use std::fs::{canonicalize, File};
5+
use std::io::Read;
6+
use std::path::Path;
7+
use std::rc::Rc;
8+
use std::sync::RwLock;
9+
use crate::core::native_function::NativeFunction;
10+
use crate::core::return_types::ReturnTypes::RuntimeError;
11+
use crate::{Interpreter, TokenStream};
12+
use crate::core::data_types::DataTypes;
13+
use crate::core::scope::Scope;
14+
15+
pub fn load_file() -> NativeFunction {
16+
NativeFunction {
17+
name: String::from("load_file"),
18+
body: Box::new(| scope, args | {
19+
let path: &Rc<RefCell<DataTypes>> = args.get(0).unwrap();
20+
let path = path.borrow();
21+
let path = path.to_string();
22+
23+
match canonicalize(&path) {
24+
Ok(got) => {
25+
let path = got.to_str().unwrap();
26+
27+
let mut s = String::new();
28+
29+
File::open(&path).unwrap().read_to_string(&mut s);
30+
31+
let mut stream = TokenStream::new(&s, path.to_owned());
32+
33+
stream.start();
34+
35+
let itr = Interpreter::new(stream.nodes, path.to_owned());
36+
37+
let scope = Scope::new();
38+
39+
{
40+
let mut writer = scope.variables.write().unwrap();
41+
writer.insert("public".to_owned(), Rc::new(RefCell::new(DataTypes::Object(Rc::new(RwLock::new(HashMap::new()))))));
42+
}
43+
44+
{
45+
itr.run(Some(&scope));
46+
}
47+
48+
let reader = scope.variables.read().unwrap();
49+
let reader = reader.get("public").unwrap();
50+
51+
Ok(reader.clone())
52+
},
53+
Err(e) => Err(RuntimeError(e.to_string()))
54+
}
55+
})
56+
}
57+
}

src/native/loader.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::core::native_function::NativeFunction;
22
use crate::native::_typeof::_typeof;
3+
use crate::native::load_file::load_file;
34
use crate::native::log::log;
45
use crate::native::system_time::system_time;
56

67
pub fn load_native_functions() -> Vec<NativeFunction> {
7-
vec![log(), _typeof(), system_time()]
8+
vec![log(), _typeof(), system_time(), load_file()]
89
}

src/native/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ mod _typeof;
44
pub mod loader;
55
pub mod prototypes;
66
mod system_time;
7+
mod load_file;

tests/example.neko

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
neko util = load_file("./tests/util.neko");
2+
3+
log(util->join("me", "this")); @ Outputs "me this"

tests/example.nksc

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/util.neko

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public->join = dyn (x, y) {
2+
@ tmr
3+
return x + " " + y;
4+
};

0 commit comments

Comments
 (0)