Skip to content

Commit 57f2955

Browse files
committed
Arrays and objects are not printable
1 parent db57b03 commit 57f2955

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ neko arr = [
4343
];
4444
4545
log(arr->at(0)); @ Will print uwu
46+
log(arr); @ Gibberish
4647
```
47-
I forgot, do not try to log objects or arrays, these cannot be deserialized to strings *yet*.
48-
4948
There is only one kind of loop implemented, that is `while` loop:
5049
```
5150
neko x = 0;

src/core/data_types.rs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::constants::data_types::{ARRAY_TYPE, BOOL_TYPE, FN_TYPE, INT_TYPE, NULL_TYPE, OBJECT_TYPE, RAW_ARRAY_TYPE, RAW_OBJECT_TYPE, TEXT_TYPE};
22
use crate::core::nodes::{Node, Nodes};
3-
use std::borrow::Borrow;
3+
44
use std::cell::{RefCell, RefMut};
55
use std::collections::HashMap;
66
use std::rc::Rc;
@@ -183,6 +183,70 @@ impl DataTypes {
183183
Self::Int(s) => s.to_string(),
184184
Self::Bool(s) => s.to_string(),
185185
Self::Null => NULL_TYPE.to_string(),
186+
Self::Array(arr) => {
187+
let arr = arr.read().unwrap();
188+
189+
let mut s = String::from('[');
190+
191+
let mut y = 0;
192+
let last = arr.len();
193+
194+
for i in arr.iter() {
195+
let val = i.borrow();
196+
s.push_str(&format!("{}{}", {
197+
if val.is_text() {
198+
format!("\"{}\"", val.to_string())
199+
} else {
200+
val.to_string()
201+
}
202+
}, {
203+
if y + 1 == last {
204+
String::new()
205+
} else {
206+
String::from(",")
207+
}
208+
}));
209+
210+
y += 1;
211+
}
212+
213+
s.push(']');
214+
215+
s
216+
}
217+
Self::Object(obj) => {
218+
let reader = obj.read().unwrap();
219+
let mut s = String::from('{');
220+
221+
let last = reader.len();
222+
223+
let mut y = 0;
224+
225+
for (key, value) in reader.iter() {
226+
let value = value.borrow();
227+
let str = value.to_string();
228+
229+
s.push_str(&format!("\"{}\":{}{}", key, {
230+
if value.is_text() {
231+
format!("\"{}\"", str)
232+
} else {
233+
str
234+
}
235+
}, {
236+
if y + 1 == last {
237+
String::new()
238+
} else {
239+
String::from(",")
240+
}
241+
}));
242+
243+
y += 1;
244+
}
245+
246+
s.push('}');
247+
248+
s
249+
}
186250
_ => panic!("Cannot deserialize {} to string.", self.kind()),
187251
}
188252
}

tests/example.nksc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
log(2 * 4 / 3);
1+
neko ok = [
2+
"depends",
3+
object!{
4+
yuh: 1,
5+
k: null
6+
},
7+
1,
8+
true,
9+
null
10+
];
11+
12+
log(ok);

0 commit comments

Comments
 (0)