Skip to content

Commit fb47ebf

Browse files
committed
add command clear and cls
1 parent b85d0ed commit fb47ebf

File tree

1 file changed

+20
-164
lines changed

1 file changed

+20
-164
lines changed

src/main.rs

Lines changed: 20 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -244,70 +244,55 @@ impl Executor {
244244

245245
let mut syntax = Vec::new(); // Token string
246246
let mut buffer = String::new(); // Temporary storage
247-
let mut in_brackets = 0; // String's nest structure
248-
let mut in_parentheses = 0; // List's nest structure
249-
let mut in_hash = false; // Is it Comment
247+
let mut brackets = 0; // String's nest structure
248+
let mut parentheses = 0; // List's nest structure
249+
let mut hash = false; // Is it Comment
250+
let mut escape = false; // Flag to indicate next character is escaped
250251

251252
for c in code.chars() {
252253
match c {
253-
'(' => {
254-
in_brackets += 1;
254+
'\\' if !escape => {
255+
escape = true;
256+
}
257+
'(' if !hash && !escape => {
258+
brackets += 1;
255259
buffer.push('(');
256260
}
257-
')' => {
258-
in_brackets -= 1;
261+
')' if !hash && !escape => {
262+
brackets -= 1;
259263
buffer.push(')');
260264
}
261-
'#' if !in_hash => {
262-
in_hash = true;
265+
'#' if !hash && !escape => {
266+
hash = true;
263267
buffer.push('#');
264268
}
265-
'#' if in_hash => {
266-
in_hash = false;
269+
'#' if hash && !escape => {
270+
hash = false;
267271
buffer.push('#');
268272
}
269-
'[' if in_brackets == 0 => {
270-
in_parentheses += 1;
273+
'[' if !hash && brackets == 0 && !escape => {
274+
parentheses += 1;
271275
buffer.push('[');
272276
}
273-
']' if in_brackets == 0 => {
274-
in_parentheses -= 1;
277+
']' if !hash && brackets == 0 && !escape => {
278+
parentheses -= 1;
275279
buffer.push(']');
276280
}
277-
' ' if !in_hash && in_parentheses == 0 && in_brackets == 0 => {
281+
' ' if !hash && parentheses == 0 && brackets == 0 && !escape => {
278282
if !buffer.is_empty() {
279283
syntax.push(buffer.clone());
280284
buffer.clear();
281285
}
282286
}
283287
_ => {
284-
<<<<<<< HEAD
285-
buffer.push(c);
286-
=======
287-
<<<<<<< HEAD
288288
if parentheses == 0 && brackets == 0 && !hash {
289289
if escape {
290-
<<<<<<< HEAD
291-
<<<<<<< HEAD
292-
=======
293-
>>>>>>> 416f9ec (Update main.rs)
294290
match c {
295291
'n' => buffer.push_str("\\n"),
296292
't' => buffer.push_str("\\t"),
297293
'r' => buffer.push_str("\\r"),
298294
_ => buffer.push(c),
299295
}
300-
<<<<<<< HEAD
301-
=======
302-
buffer.push(match c {
303-
'n' => '\n',
304-
't' => '\t',
305-
'r' => '\r',
306-
_ => c,
307-
})
308-
>>>>>>> e0ee7bf (Add string escape)
309-
=======
310-
>>>>>>> 416f9ec (Update main.rs)
311296
} else {
312297
buffer.push(c);
313298
}
@@ -318,10 +303,6 @@ impl Executor {
318303
buffer.push(c);
319304
}
320305
escape = false; // Reset escape flag for non-escape characters
321-
=======
322-
buffer.push(c);
323-
>>>>>>> 76e82be (git)
324-
>>>>>>> upstream-main
325306
}
326307
}
327308
}
@@ -354,11 +335,8 @@ impl Executor {
354335
self.stack.push(Type::Bool(token.parse().unwrap_or(true)));
355336
} else if chars[0] == '(' && chars[chars.len() - 1] == ')' {
356337
// Push string value on the stack
357-
<<<<<<< HEAD
358338
self.stack
359339
.push(Type::String(token[1..token.len() - 1].to_string()));
360-
=======
361-
<<<<<<< HEAD
362340
let string = {
363341
let mut buffer = String::new(); // Temporary storage
364342
let mut brackets = 0; // String's nest structure
@@ -398,27 +376,18 @@ impl Executor {
398376
_ => {
399377
if parentheses == 0 && brackets == 0 && !hash {
400378
if escape {
401-
<<<<<<< HEAD
402-
<<<<<<< HEAD
403-
=======
404-
>>>>>>> 416f9ec (Update main.rs)
405379
match c {
406380
'n' => buffer.push_str("\\n"),
407381
't' => buffer.push_str("\\t"),
408382
'r' => buffer.push_str("\\r"),
409383
_ => buffer.push(c),
410384
}
411-
<<<<<<< HEAD
412-
=======
413385
buffer.push(match c {
414386
'n' => '\n',
415387
't' => '\t',
416388
'r' => '\r',
417389
_ => c,
418390
})
419-
>>>>>>> e0ee7bf (Add string escape)
420-
=======
421-
>>>>>>> 416f9ec (Update main.rs)
422391
} else {
423392
buffer.push(c);
424393
}
@@ -435,11 +404,8 @@ impl Executor {
435404
buffer
436405
};
437406
self.stack.push(Type::String(string));
438-
=======
439407
self.stack
440408
.push(Type::String(token[1..token.len() - 1].to_string()));
441-
>>>>>>> 76e82be (git)
442-
>>>>>>> upstream-main
443409
} else if chars[0] == '[' && chars[chars.len() - 1] == ']' {
444410
// Push list value on the stack
445411
let old_len = self.stack.len(); // length of old stack
@@ -907,32 +873,7 @@ impl Executor {
907873
return;
908874
}
909875
}
910-
<<<<<<< HEAD
911-
<<<<<<< HEAD
912-
<<<<<<< HEAD
913-
<<<<<<< HEAD
914-
<<<<<<< HEAD
915-
=======
916-
>>>>>>> cdb5350 (git)
917-
918-
self.log_print(String::from("Error! item not found in the list\n"));
919-
=======
920-
self.log_print(String::from("Error! item not found in the list").as_str().to_owned() + "\n");
921-
>>>>>>> ce3cc7e (git)
922-
<<<<<<< HEAD
923-
=======
924-
925-
=======
926-
927-
>>>>>>> 61ad1c1 (Format)
928-
self.log_print(String::from("Error! item not found in the list\n"));
929-
>>>>>>> 0c174e6 (Update main.rs)
930-
=======
931-
>>>>>>> cdb5350 (git)
932-
=======
933-
934876
self.log_print(String::from("Error! item not found in the list\n"));
935-
>>>>>>> 76e82be (git)
936877
self.stack.push(Type::Error(String::from("item-not-found")));
937878
}
938879

@@ -1441,52 +1382,6 @@ impl Executor {
14411382
})
14421383
}
14431384

1444-
<<<<<<< HEAD
1445-
=======
1446-
<<<<<<< HEAD
1447-
<<<<<<< HEAD
1448-
<<<<<<< HEAD
1449-
<<<<<<< HEAD
1450-
<<<<<<< HEAD
1451-
=======
1452-
=======
1453-
<<<<<<< HEAD
1454-
>>>>>>> ce3cc7e (git)
1455-
=======
1456-
>>>>>>> ce80cb2 (git)
1457-
>>>>>>> 74bbfe3 (git)
1458-
=======
1459-
=======
1460-
>>>>>>> 1843023 (git)
1461-
=======
1462-
=======
1463-
=======
1464-
<<<<<<< HEAD
1465-
>>>>>>> ce3cc7e (git)
1466-
<<<<<<< HEAD
1467-
>>>>>>> cdb5350 (git)
1468-
=======
1469-
=======
1470-
>>>>>>> ce80cb2 (git)
1471-
>>>>>>> 74bbfe3 (git)
1472-
>>>>>>> e1b0d54 (git)
1473-
=======
1474-
>>>>>>> 76e82be (git)
1475-
>>>>>>> upstream-main
1476-
"index" => {
1477-
let findhint = self.pop_stack().get_string();
1478-
let findtarget = self.pop_stack().get_list();
1479-
1480-
for index in 0..(findtarget.len()) {
1481-
if findhint == findtarget[index].clone().get_string() {
1482-
self.stack.push(Type::Number(index as f64));
1483-
return;
1484-
}
1485-
}
1486-
self.log_print("Error! item not found in this list".to_owned() + "\n");
1487-
self.stack.push(Type::Error(String::from("item-not-found")));
1488-
}
1489-
14901385
"cls" => {
14911386
self.clearscreen();
14921387
}
@@ -1495,45 +1390,6 @@ impl Executor {
14951390
self.clearscreen();
14961391
}
14971392

1498-
<<<<<<< HEAD
1499-
=======
1500-
<<<<<<< HEAD
1501-
<<<<<<< HEAD
1502-
<<<<<<< HEAD
1503-
<<<<<<< HEAD
1504-
<<<<<<< HEAD
1505-
<<<<<<< HEAD
1506-
=======
1507-
>>>>>>> cdb5350 (git)
1508-
=======
1509-
>>>>>>> e1b0d54 (git)
1510-
>>>>>>> 887510b (git)
1511-
=======
1512-
=======
1513-
>>>>>>> 1d34bfe (Refactoring)
1514-
>>>>>>> ce3cc7e (git)
1515-
<<<<<<< HEAD
1516-
<<<<<<< HEAD
1517-
=======
1518-
>>>>>>> e1b0d54 (git)
1519-
=======
1520-
=======
1521-
>>>>>>> 1d34bfe (Refactoring)
1522-
=======
1523-
>>>>>>> 887510b (git)
1524-
>>>>>>> ce80cb2 (git)
1525-
>>>>>>> 74bbfe3 (git)
1526-
<<<<<<< HEAD
1527-
=======
1528-
>>>>>>> 887510b (git)
1529-
>>>>>>> 1843023 (git)
1530-
=======
1531-
>>>>>>> cdb5350 (git)
1532-
=======
1533-
>>>>>>> e1b0d54 (git)
1534-
=======
1535-
>>>>>>> 76e82be (git)
1536-
>>>>>>> upstream-main
15371393
// If it is not recognized as a command, use it as a string.
15381394
_ => self.stack.push(Type::String(command)),
15391395
}

0 commit comments

Comments
 (0)