Skip to content

Commit f861fba

Browse files
committed
WIP: optimize variables
1 parent bbe1bc7 commit f861fba

File tree

4 files changed

+84
-70
lines changed

4 files changed

+84
-70
lines changed

src/compilable.rs

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extern crate ego_tree;
22

33
use std::any::Any;
44
use std::rc::Rc;
5+
use std::collections::{BTreeMap, HashMap};
56

67
use ego_tree::{Tree, NodeRef, NodeMut};
78
use ordered_float::OrderedFloat;
@@ -21,7 +22,7 @@ impl Compiler {
2122
}
2223

2324
pub fn compile(&mut self, value: Value) {
24-
let mut tree = Tree::new(Compilable::Block);
25+
let mut tree = Tree::new(Compilable::new(CompilableData::Block));
2526

2627
self.compile_(&mut tree.root_mut(), &value);
2728

@@ -31,38 +32,45 @@ impl Compiler {
3132
fn compile_(&mut self, parent: &mut NodeMut<Compilable>, value: &Value) {
3233
match value {
3334
Value::Null => {
34-
parent.append(Compilable::Null);
35+
parent.append(Compilable::new(CompilableData::Null));
3536
}
3637
Value::Boolean(v) => {
37-
parent.append(Compilable::Bool(*v));
38+
parent.append(Compilable::new(CompilableData::Bool(*v)));
3839
}
3940
Value::Integer(v) => {
40-
parent.append(Compilable::Int(*v));
41+
parent.append(Compilable::new(CompilableData::Int(*v)));
4142
}
4243
Value::String(v) => {
43-
parent.append(Compilable::String(v.to_string()));
44+
parent.append(Compilable::new(CompilableData::String(v.to_string())));
4445
}
4546
Value::Symbol(v) => {
46-
parent.append(Compilable::Symbol(v.to_string()));
47+
parent.append(Compilable::new(CompilableData::Symbol(v.to_string())));
4748
}
4849
Value::Array(v) => {
49-
let mut node = parent.append(Compilable::Array);
50-
for item in v.iter() {
51-
self.compile_(&mut node, item);
52-
}
50+
let mut new_arr = Vec::new();
51+
// TODO: add literal values to new_arr
52+
let mut node = parent.append(Compilable::new(CompilableData::Array(new_arr)));
53+
// TODO: compile non-literal items
54+
// for item in v.iter() {
55+
// let mut node2 = parent.append(Compilable::new(CompilableData::ArrayChild(index)));
56+
// self.compile_(&mut node2, item);
57+
// }
5358
}
5459
Value::Map(v) => {
55-
let mut node = parent.append(Compilable::Map);
56-
for (key, value) in v.iter() {
57-
let mut node2 = node.append(Compilable::KeyValue(key.to_string()));
58-
self.compile_(&mut node2, value);
59-
}
60+
// TODO: create map with literals then compile non-literal values and add to map
61+
// let mut node = parent.append(Compilable::new(CompilableData::Map));
62+
// for (key, value) in v.iter() {
63+
// let mut node2 = node.append(Compilable::new(CompilableData::MapChild(key.to_string())));
64+
// self.compile_(&mut node2, value);
65+
// }
6066
}
6167
Value::Gene(v) => {
62-
// let mut node = parent.append(Compilable::Gene(GeneType::Other));
63-
// self.compile_(&mut node, v.kind);
68+
// TODO: create Gene with literals then compile non-literal kind/prop/data
69+
// let mut node = parent.append(Compilable::new(CompilableData::Gene));
70+
// let mut kind_node = node.append(Compilable::new(CompilableData::GeneKind(GeneKind::Other)));
71+
// self.compile_(&mut kind_node, v.kind);
6472
// for (key, value) in v.props.iter() {
65-
// let mut node2 = node.append(Compilable::KeyValue(key.to_string()));
73+
// let mut node2 = node.append(Compilable::new(CompilableData::MapChild(key.to_string())));
6674
// self.compile_(&mut node2, value);
6775
// }
6876
// for item in v.data.iter() {
@@ -80,7 +88,21 @@ impl Compiler {
8088
}
8189
}
8290

83-
pub enum Compilable {
91+
pub struct Compilable {
92+
data: CompilableData,
93+
options: HashMap<String, Box<dyn Any>>,
94+
}
95+
96+
impl Compilable {
97+
pub fn new(data: CompilableData) -> Self {
98+
Compilable {
99+
data,
100+
options: HashMap:: new(),
101+
}
102+
}
103+
}
104+
105+
pub enum CompilableData {
84106
/// literal
85107
Void,
86108
/// literal
@@ -94,22 +116,18 @@ pub enum Compilable {
94116
/// literal
95117
String(String),
96118
Symbol(String),
97-
Array,
98-
Map,
99-
KeyValue(String),
100-
Gene(GeneType),
119+
Array(Vec<Value>),
120+
ArrayChild(u32),
121+
Map(HashMap<String, Value>),
122+
MapChild(String),
123+
Gene(Value, HashMap<String, Value>, Vec<Value>),
124+
GeneKind(GeneKind),
125+
GeneProp(String),
126+
GeneDataChild(u32),
101127
Block,
102128
}
103129

104-
impl Compilable {
105-
pub fn set(&mut self, name: String, value: Box<dyn Any>) {
106-
match self {
107-
_ => unimplemented!()
108-
}
109-
}
110-
}
111-
112-
pub enum GeneType {
130+
pub enum GeneKind {
113131
Function,
114132
If,
115133
Other,

src/compiler.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,17 @@ impl Compiler {
164164
kind, data, ..
165165
} = gene;
166166

167-
match *kind.borrow() {
167+
match *kind {
168168
Value::Symbol(ref s) if s == "var" => {
169169
let first;
170170
{
171171
first = data[0].clone();
172172
}
173173
let second;
174174
{
175-
second = data[1].borrow().clone();
175+
second = data[1].clone();
176176
}
177-
match *first.borrow_mut() {
177+
match first {
178178
Value::Symbol(ref name) => {
179179
self.compile_(block, second.clone());
180180
(*block).add_instr(Instruction::DefMember(name.clone()));
@@ -183,15 +183,15 @@ impl Compiler {
183183
};
184184
}
185185
Value::Symbol(ref s) if s == "fn" => {
186-
let name = data[0].borrow().to_string();
186+
let name = data[0].to_string();
187187

188188
let mut body = Block::new(name.clone());
189189
let body_id = body.id.clone();
190190

191191
self.reg_trackers.insert(body_id.clone(), Vec::new());
192192

193-
let borrowed = data[1].borrow();
194-
let matcher = Matcher::from(&*borrowed);
193+
let borrowed = data[1];
194+
let matcher = Matcher::from(&borrowed);
195195

196196
self.compile_statements(&mut body, &data[2..]);
197197
body.add_instr(Instruction::CallEnd);
@@ -206,19 +206,19 @@ impl Compiler {
206206
self.compile_if(block, data);
207207
}
208208
Value::Symbol(ref s) if s == "=" => {
209-
let name = data[0].borrow().to_string();
210-
let value = data[1].borrow().clone();
209+
let name = data[0].to_string();
210+
let value = data[1].clone();
211211
self.compile_(block, value);
212212
(*block).add_instr(Instruction::SetMember(name));
213213
}
214214
Value::Symbol(ref s) if is_binary_op(s) => {
215-
let first = data[0].borrow().clone();
215+
let first = data[0].clone();
216216
self.compile_(block, first);
217217

218218
let first_reg = self.get_reg(block);
219219
(*block).add_instr(Instruction::CopyFromDefault(first_reg));
220220

221-
let second = data[1].borrow().clone();
221+
let second = data[1].clone();
222222
self.compile_(block, second);
223223

224224
(*block).add_instr(Instruction::BinaryOp(s.to_string(), first_reg));
@@ -232,8 +232,7 @@ impl Compiler {
232232
}
233233
_ => {
234234
// Invocation
235-
let borrowed_kind = kind.borrow().clone();
236-
self.compile_(block, borrowed_kind);
235+
self.compile_(block, *kind);
237236
let target_reg = self.get_reg(block);
238237
(*block).add_instr(Instruction::CopyFromDefault(target_reg));
239238

@@ -242,8 +241,7 @@ impl Compiler {
242241
let args_reg = self.get_reg(block);
243242
(*block).add_instr(Instruction::CreateArguments(args_reg));
244243
for (i, item) in data.iter().enumerate() {
245-
let borrowed = item.borrow();
246-
self.compile_(block, (*borrowed).clone());
244+
self.compile_(block, item.clone());
247245
(*block).add_instr(Instruction::SetItem(args_reg, i));
248246
}
249247

@@ -254,24 +252,23 @@ impl Compiler {
254252
};
255253
}
256254

257-
fn compile_statements(&mut self, block: &mut Block, stmts: &[Rc<RefCell<Value>>]) {
255+
fn compile_statements(&mut self, block: &mut Block, stmts: &[Value]) {
258256
for item in stmts.iter().cloned() {
259-
let borrowed = item.borrow().clone();
257+
let borrowed = item.clone();
260258
self.compile_(block, borrowed);
261259
}
262260
}
263261

264-
fn compile_if(&mut self, block: &mut Block, mut data: Vec<Rc<RefCell<Value>>>) {
262+
fn compile_if(&mut self, block: &mut Block, mut data: Vec<Value>) {
265263
let cond = data.remove(0);
266-
let mut then_stmts = Vec::<Rc<RefCell<Value>>>::new();
267-
let mut else_stmts = Vec::<Rc<RefCell<Value>>>::new();
264+
let mut then_stmts = Vec::new();
265+
let mut else_stmts = Vec::new();
268266
let mut is_else = false;
269267
for item in data.iter() {
270268
if is_else {
271269
else_stmts.push(item.clone());
272270
} else {
273-
let borrowed_item = item.borrow();
274-
match *borrowed_item {
271+
match *item {
275272
Value::Symbol(ref s) if s == "then" => (),
276273
Value::Symbol(ref s) if s == "else" => {
277274
is_else = true;
@@ -283,7 +280,7 @@ impl Compiler {
283280
}
284281
}
285282
}
286-
self.compile_(block, cond.borrow().clone());
283+
self.compile_(block, cond.clone());
287284
let cond_jump_index = block.instructions.len();
288285
(*block).add_instr(Instruction::Dummy);
289286

@@ -300,18 +297,18 @@ impl Compiler {
300297
mem::replace(&mut (*block).instructions[then_jump_index], Instruction::Jump(end_index as i16));
301298
}
302299

303-
fn compile_while(&mut self, block: &mut Block, mut data: Vec<Rc<RefCell<Value>>>) {
300+
fn compile_while(&mut self, block: &mut Block, mut data: Vec<Value>) {
304301
let start_index = block.instructions.len();
305302

306303
(*block).add_instr(Instruction::LoopStart);
307304

308305
let cond = data.remove(0);
309-
self.compile_(block, cond.borrow().clone());
306+
self.compile_(block, cond.clone());
310307
let jump_index = block.instructions.len();
311308
(*block).add_instr(Instruction::Dummy);
312309

313310
for item in data.iter() {
314-
self.compile_(block, item.borrow().clone());
311+
self.compile_(block, item.clone());
315312
}
316313
(*block).add_instr(Instruction::Jump(start_index as i16));
317314
(*block).add_instr(Instruction::LoopEnd);
@@ -593,19 +590,18 @@ fn normalize(gene: Gene) -> Gene {
593590
if gene.data.is_empty() {
594591
gene
595592
} else {
596-
let borrowed = gene.data[0].clone();
597-
let first = borrowed.borrow_mut();
598-
match *first {
593+
let first = gene.data[0].clone();
594+
match first {
599595
Value::Symbol(ref s) if is_binary_op(s) || s == "=" => {
600596
let Gene {
601597
kind,
602598
mut data,
603599
props,
604600
} = gene;
605601
let new_kind = data.remove(0);
606-
data.insert(0, kind);
602+
data.insert(0, *kind);
607603
Gene {
608-
kind: new_kind,
604+
kind: Box::new(new_kind.clone()),
609605
props,
610606
data,
611607
}

src/parser.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<'a> Parser<'a> {
7070
let mut kind_is_set = false;
7171
let mut kind = Value::Null;
7272
let mut props = BTreeMap::<String, Rc<RefCell<Value>>>::new();
73-
let mut data = Vec::<Rc<RefCell<Value>>>::new();
73+
let mut data = Vec::new();
7474
loop {
7575
self.skip_whitespaces();
7676

@@ -88,7 +88,7 @@ impl<'a> Parser<'a> {
8888
if result.is_some() {
8989
let val = result.unwrap().unwrap();
9090
if kind_is_set {
91-
data.push(Rc::new(RefCell::new(val)));
91+
data.push(val);
9292
} else {
9393
kind_is_set = true;
9494
kind = val;
@@ -97,7 +97,7 @@ impl<'a> Parser<'a> {
9797
}
9898
}
9999
return Some(Ok(Value::Gene(Gene {
100-
kind: Rc::new(RefCell::new(kind)),
100+
kind: Box::new(kind),
101101
props,
102102
data,
103103
})));
@@ -161,7 +161,7 @@ impl<'a> Parser<'a> {
161161
self.next();
162162
let mut gene = Gene::new(Value::Symbol("#QUOTE".to_string()));
163163
gene.data
164-
.push(Rc::new(RefCell::new(self.read().unwrap().unwrap())));
164+
.push(self.read().unwrap().unwrap());
165165
return Some(Ok(Value::Gene(gene)));
166166
} else if is_symbol_head(ch) {
167167
return self.read_keyword_or_symbol();

src/types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ impl fmt::Display for Value {
8484

8585
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
8686
pub struct Gene {
87-
pub kind: Rc<RefCell<Value>>,
87+
pub kind: Box<Value>,
8888
pub props: BTreeMap<String, Rc<RefCell<Value>>>,
89-
pub data: Vec<Rc<RefCell<Value>>>,
89+
pub data: Vec<Value>,
9090
}
9191

9292
impl Gene {
9393
pub fn new(kind: Value) -> Self {
9494
Gene {
95-
kind: Rc::new(RefCell::new(kind)),
95+
kind: Box::new(kind),
9696
props: BTreeMap::new(),
9797
data: vec![],
9898
}
@@ -102,7 +102,7 @@ impl Gene {
102102
impl fmt::Display for Gene {
103103
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
104104
fmt.write_str("(")?;
105-
fmt.write_str(&self.kind.borrow().to_string())?;
105+
fmt.write_str(&self.kind.to_string())?;
106106
fmt.write_str(" ...)")?;
107107
Ok(())
108108
}

0 commit comments

Comments
 (0)