1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
use std::collections::HashMap;
use crate::Value as Obj;
use super::parse::Token;
pub fn compile(tokens: Vec<Token>, mut data: HashMap<String, Obj>) -> Result<String, String> {
let mut compiler = TemplateCompiler::new(tokens, data);
compiler.compile()
}
// AccessType enum to distinguish between reading and writing operations
#[derive(Debug, Clone, Copy, PartialEq)]
enum AccessType {
Read, // Just reading a value
Write, // Writing to a variable or property
}
struct TemplateCompiler {
tokens: Vec<Token>,
data: HashMap<String, Obj>,
pos: usize,
blocks: HashMap<String, Vec<Token>>,
output: String,
export_mode: bool,
template_name: Option<String>,
}
impl TemplateCompiler {
fn new(tokens: Vec<Token>, data: HashMap<String, Obj>) -> Self {
TemplateCompiler {
tokens,
data,
pos: 0,
blocks: HashMap::new(),
output: String::new(),
export_mode: false,
template_name: None,
}
}
fn compile(&mut self) -> Result<String, String> {
// First pass: identify blocks and template info
self.collect_blocks_and_metadata()?;
// Reset position for second pass
self.pos = 0;
self.output.clear();
// If this is a template file with export directive, we don't directly output
if self.export_mode {
return Ok(String::new());
}
// Second pass: generate output
self.generate_output()
}
fn collect_blocks_and_metadata(&mut self) -> Result<(), String> {
while self.pos < self.tokens.len() {
match &self.tokens[self.pos] {
Token::TemplateKeyword => {
self.pos += 1;
if let Some(Token::Object(Obj::Str(name))) = self.tokens.get(self.pos) {
self.template_name = Some(name.clone());
self.pos += 1;
} else {
return Err("Expected string after template keyword".to_string());
}
},
Token::BlockKeyword => {
self.pos += 1;
if let Some(Token::Identifier(name)) = self.tokens.get(self.pos).cloned() {
self.pos += 1;
// Skip END_OF_STATEMENT
if matches!(self.tokens.get(self.pos), Some(Token::EndOfStatement)) {
self.pos += 1;
}
let start = self.pos;
let mut depth = 1;
// Find the matching endblock
while self.pos < self.tokens.len() && depth > 0 {
match self.tokens[self.pos] {
Token::BlockKeyword => depth += 1,
Token::EndBlockKeyword => depth -= 1,
_ => {}
}
self.pos += 1;
}
if depth > 0 {
return Err(format!("Unterminated block: {}", name));
}
// Take the block content (excluding endblock)
let end = self.pos - 1;
let block_tokens = self.tokens[start..end].to_vec();
self.blocks.insert(name, block_tokens);
} else {
return Err("Expected identifier after block keyword".to_string());
}
},
Token::ExportKeyword => {
self.export_mode = true;
self.pos += 1;
},
_ => self.pos += 1,
}
// Skip END_OF_STATEMENT tokens
if matches!(self.tokens.get(self.pos), Some(Token::EndOfStatement)) {
self.pos += 1;
}
}
Ok(())
}
fn generate_output(&mut self) -> Result<String, String> {
while self.pos < self.tokens.len() {
match &self.tokens[self.pos] {
Token::HtmlContent(content) => {
self.output.push_str(content);
self.pos += 1;
},
Token::PlaceholderKeyword => {
self.pos += 1;
if let Some(Token::Identifier(name)) = self.tokens.get(self.pos) {
let block_name = name.clone();
if let Some(block_tokens) = self.blocks.get(&block_name) {
// Create a new compiler to process this block
let mut block_compiler = TemplateCompiler::new(
block_tokens.clone(),
self.data.clone()
);
match block_compiler.generate_output() {
Ok(block_output) => {
self.output.push_str(&block_output);
// Update data with any changes from the block
self.data = block_compiler.data;
},
Err(e) => return Err(e),
}
} else {
// Use empty content for missing blocks
self.output.push_str(&format!("<!-- Block '{}' not defined -->", block_name));
}
self.pos += 1;
} else {
return Err("Expected identifier after placeholder keyword".to_string());
}
},
Token::BlockKeyword => {
// Skip over blocks in the second pass, as they're already processed
self.pos += 1; // Skip BlockKeyword
// Skip block name
if matches!(self.tokens.get(self.pos), Some(Token::Identifier(_))) {
self.pos += 1;
}
// Skip END_OF_STATEMENT if present
if matches!(self.tokens.get(self.pos), Some(Token::EndOfStatement)) {
self.pos += 1;
}
// Find the matching endblock
// let mut depth = 1;
// while self.pos < self.tokens.len() && depth > 0 {
// match self.tokens[self.pos] {
// Token::BlockKeyword => depth += 1,
// Token::EndBlockKeyword => depth -= 1,
// _ => {}
// }
// self.pos += 1;
// }
},
Token::EndBlockKeyword => {
// Skip EndBlockKeyword as it's handled when skipping blocks
self.pos += 1;
},
Token::LetKeyword => {
self.handle_assignment()?;
},
Token::IfKeyword => {
self.handle_if_statement()?;
},
Token::ForKeyword => {
self.handle_for_loop()?;
},
Token::WhileKeyword => {
self.handle_while_loop()?;
},
Token::OutputKeyword => {
self.pos += 1;
let value = self.evaluate_expression()?;
self.output.push_str(&value.interal_value_as_string());
},
Token::DelKeyword => {
self.handle_deletion()?;
},
Token::Identifier(name) => {
let var_name = name.clone();
self.pos += 1;
// Check for access operations (index, property, or assignment)
match self.handle_variable_access(&var_name, true)? {
Some(value) => {
self.output.push_str(&value.interal_value_as_string());
},
None => {} // Assignment was handled in handle_variable_access
}
},
Token::EndOfStatement => {
self.pos += 1;
},
_ => {
// Skip other tokens
self.pos += 1;
}
}
}
Ok(self.output.clone())
}
/// Handles variable access, including indexing, property access, and assignments
/// Returns Some(value) for read operations, None for write operations
fn handle_variable_access(&mut self, var_name: &str, output_to_stream: bool) -> Result<Option<Obj>, String> {
// Check if this is a property access or indexing
let mut value = match self.data.get(var_name) {
Some(v) => v.clone(),
None => {
if output_to_stream {
self.output.push_str(
&format!("<!-- There should be a value '{}' but not found -->", var_name)
);
}
return Ok(Some(Obj::None));
}
};
let mut access_chain_pos = self.pos;
// Handle indexing with [] syntax
while matches!(self.tokens.get(access_chain_pos), Some(Token::LeftSquareBracket)) {
access_chain_pos += 1;
self.pos = access_chain_pos;
// Evaluate the index expression
let index = self.evaluate_expression()?;
access_chain_pos = self.pos;
if !matches!(self.tokens.get(access_chain_pos), Some(Token::RightSquareBracket)) {
return Err("Expected closing bracket after index".to_string());
}
access_chain_pos += 1;
// Apply the indexing to get the new value
match &value {
Obj::List(list) => {
match &index {
Obj::Numerical(n) => {
let idx = *n as usize;
if idx < list.len() {
value = list[idx].clone();
} else if output_to_stream {
self.output.push_str(
&format!("<!-- Index {} out of bounds for list -->", idx)
);
return Ok(Some(Obj::None));
} else {
return Err(format!("Index {} out of bounds for list", idx));
}
},
_ => {
if output_to_stream {
self.output.push_str("<!-- List index must be a number -->");
return Ok(Some(Obj::None));
} else {
return Err("List index must be a number".to_string());
}
}
}
},
Obj::Dict(dict) => {
// Allow both string literals and string expressions as keys
let key = index.interal_value_as_string();
if let Some(val) = dict.get(&key) {
value = val.clone();
} else if output_to_stream {
self.output.push_str(
&format!("<!-- Key '{}' not found in dictionary -->", key)
);
return Ok(Some(Obj::None));
} else {
return Err(format!("Key '{}' not found in dictionary", key));
}
},
_ => {
if output_to_stream {
self.output.push_str(
&format!("<!-- Cannot index into a {} value -->", value.type_of())
);
return Ok(Some(Obj::None));
} else {
return Err(format!("Cannot index into a {} value", value.type_of()));
}
}
}
}
// Handle dot notation for property/method access
if matches!(self.tokens.get(access_chain_pos), Some(Token::Dot)) {
access_chain_pos += 1;
if let Some(Token::Identifier(prop_name)) = self.tokens.get(access_chain_pos).cloned() {
access_chain_pos += 1;
// Handle built-in methods and properties
match &value {
Obj::List(list) => {
match prop_name.as_str() {
"len" => value = Obj::Numerical(list.len() as f64),
// Add more list methods here as needed
_ => {
if output_to_stream {
self.output.push_str(
&format!("<!-- No property/method '{}' on list -->", prop_name)
);
return Ok(Some(Obj::None));
} else {
return Err(format!("No property/method '{}' on list", prop_name));
}
}
}
},
Obj::Dict(dict) => {
match prop_name.as_str() {
"len" => value = Obj::Numerical(dict.len() as f64),
// Add more dictionary methods here as needed
_ => {
// For dictionaries, treat dot notation as an alternative to [] indexing
if let Some(val) = dict.get(&prop_name) {
value = val.clone();
} else if output_to_stream {
self.output.push_str(
&format!("<!-- Key '{}' not found in dictionary -->", prop_name)
);
return Ok(Some(Obj::None));
} else {
return Err(format!("Key '{}' not found in dictionary", prop_name));
}
}
}
},
Obj::Str(s) => {
match prop_name.as_str() {
"len" => value = Obj::Numerical(s.len() as f64),
// Add more string methods here as needed
_ => {
if output_to_stream {
self.output.push_str(
&format!("<!-- No property/method '{}' on string -->", prop_name)
);
return Ok(Some(Obj::None));
} else {
return Err(format!("No property/method '{}' on string", prop_name));
}
}
}
},
_ => {
if output_to_stream {
self.output.push_str(
&format!("<!-- Type '{}' does not support properties/methods -->", value.type_of())
);
return Ok(Some(Obj::None));
} else {
return Err(format!("Type '{}' does not support properties/methods", value.type_of()));
}
}
}
}
}
// Update position after all access operations
self.pos = access_chain_pos;
// Check if this is an assignment
if matches!(self.tokens.get(self.pos), Some(Token::Assignment)) {
self.pos += 1;
let new_value = self.evaluate_expression()?;
// Handle the assignment based on the access chain
if self.pos == self.tokens.len() || !matches!(self.tokens.get(access_chain_pos-1), Some(Token::RightSquareBracket) | Some(Token::Dot)) {
// Simple variable assignment
self.data.insert(var_name.to_string(), new_value);
} else {
// Assignment to indexed/property value - this is complex and requires maintaining the path
// For now, we're not implementing this part fully
return Err("Assignment to indexed/property values not fully implemented".to_string());
}
return Ok(None); // No value to output for assignments
}
Ok(Some(value))
}
fn handle_assignment(&mut self) -> Result<(), String> {
self.pos += 1; // Skip let keyword
if let Some(Token::Identifier(name)) = self.tokens.get(self.pos).cloned() {
self.pos += 1;
if matches!(self.tokens.get(self.pos), Some(Token::Assignment)) {
self.pos += 1;
let value = self.evaluate_expression()?;
self.data.insert(name, value);
Ok(())
} else {
Err("Expected assignment operator after variable name".to_string())
}
} else {
Err("Expected identifier after let keyword".to_string())
}
}
fn handle_deletion(&mut self) -> Result<(), String> {
self.pos += 1; // Skip del keyword
if let Some(Token::Identifier(name)) = self.tokens.get(self.pos) {
let var_name = name.clone();
self.pos += 1;
// Handle simple variable deletion
if self.pos >= self.tokens.len() || !matches!(self.tokens.get(self.pos), Some(Token::LeftSquareBracket)) {
self.data.remove(&var_name);
return Ok(());
}
// Handle deletion of dictionary/list element
self.pos += 1; // Skip left bracket
let index = self.evaluate_expression()?;
if !matches!(self.tokens.get(self.pos), Some(Token::RightSquareBracket)) {
return Err("Expected closing bracket after index".to_string());
}
self.pos += 1; // Skip right bracket
// Perform the deletion
if let Some(collection) = self.data.get_mut(&var_name) {
match collection {
Obj::List(list) => {
if let Obj::Numerical(i) = index {
let idx = i as usize;
if idx < list.len() {
list.remove(idx);
} else {
self.output.push_str(
&format!("<!-- Index {} out of bounds for list {} -->", idx, var_name)
);
}
} else {
self.output.push_str("<!-- List index must be a number -->");
}
},
Obj::Dict(dict) => {
let key = index.interal_value_as_string();
dict.remove(&key);
},
_ => {
self.output.push_str(
&format!("<!-- Cannot delete from a {} value -->", collection.type_of())
);
}
}
} else {
self.output.push_str(
&format!("<!-- Variable '{}' not found -->", var_name)
);
}
Ok(())
} else {
Err("Expected identifier after del keyword".to_string())
}
}
fn handle_if_statement(&mut self) -> Result<(), String> {
self.pos += 1; // Skip if keyword
let condition = self.evaluate_condition()?;
if !condition {
// Skip to endif
let mut depth = 1;
while self.pos < self.tokens.len() && depth > 0 {
match self.tokens[self.pos] {
Token::IfKeyword => depth += 1,
Token::EndIfKeyword => depth -= 1,
_ => {}
}
self.pos += 1;
}
}
Ok(())
}
fn handle_for_loop(&mut self) -> Result<(), String> {
self.pos += 1; // Skip for keyword
if let Some(Token::Identifier(loop_var)) = self.tokens.get(self.pos).cloned() {
self.pos += 1;
// Skip "in" keyword
if matches!(self.tokens.get(self.pos), Some(Token::InKeyword)) {
self.pos += 1;
}
// Get the iterable collection
let collection = self.evaluate_expression()?;
// Extract the loop body by finding the matching endfor
let loop_start = self.pos;
let mut depth = 1;
while self.pos < self.tokens.len() && depth > 0 {
match self.tokens[self.pos] {
Token::ForKeyword => depth += 1,
Token::EndForKeyword => depth -= 1,
_ => {}
}
self.pos += 1;
}
let loop_end = self.pos - 1; // Exclude the endfor
let loop_body = self.tokens[loop_start..loop_end].to_vec();
// Now iterate over the collection and execute the loop body for each item
match collection {
Obj::List(items) => {
// Clone the items to avoid borrowing conflicts
let items_vec: Vec<_> = items.into_iter().collect();
for item in items_vec {
// Set the loop variable to the current item
self.data.insert(loop_var.clone(), item.clone());
// Execute the loop body
let mut body_compiler = TemplateCompiler::new(
loop_body.clone(),
self.data.clone()
);
match body_compiler.generate_output() {
Ok(body_output) => {
self.output.push_str(&body_output);
self.data = body_compiler.data;
},
Err(e) => return Err(e),
}
}
},
Obj::Dict(map) => {
// Convert dictionary entries to a Vec to avoid borrowing conflicts
let entries: Vec<_> = map.into_iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
for (k, v) in entries {
// In this implementation, we'll make the loop variable an object with 'key' and 'value' properties
let mut entry = HashMap::new();
entry.insert("key".to_string(), Obj::Str(k));
entry.insert("value".to_string(), v);
self.data.insert(loop_var.clone(), Obj::Dict(entry));
// Execute the loop body
let mut body_compiler = TemplateCompiler::new(
loop_body.clone(),
self.data.clone()
);
match body_compiler.generate_output() {
Ok(body_output) => {
self.output.push_str(&body_output);
self.data = body_compiler.data;
},
Err(e) => return Err(e),
}
}
},
Obj::Numerical(n) => {
// No borrowing conflicts here, but keeping the same pattern
let iterations = n as i64;
for i in 0..iterations {
self.data.insert(loop_var.clone(), Obj::Numerical(i as f64));
// Execute the loop body
let mut body_compiler = TemplateCompiler::new(
loop_body.clone(),
self.data.clone()
);
match body_compiler.generate_output() {
Ok(body_output) => {
self.output.push_str(&body_output);
self.data = body_compiler.data;
},
Err(e) => return Err(e),
}
}
},
_ => {
self.output.push_str(
&format!("<!-- For loop requires a list, dictionary, or number, got {} -->", collection.type_of())
);
}
}
Ok(())
} else {
Err("Expected identifier after for keyword".to_string())
}
}
fn handle_while_loop(&mut self) -> Result<(), String> {
self.pos += 1; // Skip while keyword
// Save the position of the condition
let condition_pos = self.pos;
// Find the end of the while loop
let mut depth = 1;
let mut end_pos = self.pos;
while end_pos < self.tokens.len() && depth > 0 {
match self.tokens[end_pos] {
Token::WhileKeyword => depth += 1,
Token::EndWhileKeyword => depth -= 1,
_ => {}
}
end_pos += 1;
}
let loop_body = self.tokens[self.pos + 1..end_pos - 1].to_vec();
// Execute the while loop
let mut iteration = 0;
const MAX_ITERATIONS: usize = 10000; // Safety limit
while iteration < MAX_ITERATIONS {
// Reset position to evaluate condition
self.pos = condition_pos;
let condition = self.evaluate_condition()?;
if !condition {
break;
}
// Execute the loop body
let mut body_compiler = TemplateCompiler::new(
loop_body.clone(),
self.data.clone()
);
match body_compiler.generate_output() {
Ok(body_output) => {
self.output.push_str(&body_output);
self.data = body_compiler.data;
},
Err(e) => {
self.output.push_str(&format!("<!-- Error in while loop: {} -->", e));
break; // Don't halt rendering on error
}
}
iteration += 1;
if iteration == MAX_ITERATIONS {
self.output.push_str("<!-- While loop exceeded maximum iterations - possible infinite loop -->");
break;
}
}
// Skip to after endwhile
self.pos = end_pos;
Ok(())
}
fn process_token(&mut self) -> Result<(), String> {
match &self.tokens[self.pos] {
Token::HtmlContent(content) => {
self.output.push_str(content);
self.pos += 1;
},
Token::OutputKeyword => {
self.pos += 1;
match self.evaluate_expression() {
Ok(value) => self.output.push_str(&value.interal_value_as_string()),
Err(e) => self.output.push_str(&format!("<!-- Error evaluating expression: {} -->", e))
}
},
Token::LetKeyword => {
if let Err(e) = self.handle_assignment() {
self.output.push_str(&format!("<!-- Error in assignment: {} -->", e));
}
},
Token::IfKeyword => {
if let Err(e) = self.handle_if_statement() {
self.output.push_str(&format!("<!-- Error in if statement: {} -->", e));
}
},
Token::ForKeyword => {
if let Err(e) = self.handle_for_loop() {
self.output.push_str(&format!("<!-- Error in for loop: {} -->", e));
}
},
Token::WhileKeyword => {
if let Err(e) = self.handle_while_loop() {
self.output.push_str(&format!("<!-- Error in while loop: {} -->", e));
}
},
Token::Identifier(name) => {
let var_name = name.clone();
self.pos += 1;
if let Err(e) = self.handle_variable_access(&var_name, true) {
self.output.push_str(&format!("<!-- Error accessing variable {}: {} -->", var_name, e));
}
},
Token::EndOfStatement => {
self.pos += 1;
},
_ => {
// Skip other tokens
self.pos += 1;
}
}
Ok(())
}
fn evaluate_condition(&mut self) -> Result<bool, String> {
match self.evaluate_expression() {
Ok(expr_value) => {
match expr_value {
Obj::Boolean(b) => Ok(b),
Obj::Numerical(n) => Ok(n != 0.0),
Obj::Str(s) => Ok(!s.is_empty()),
Obj::List(l) => Ok(!l.is_empty()),
Obj::Dict(d) => Ok(!d.is_empty()),
Obj::None => Ok(false),
}
},
Err(e) => {
// Instead of failing, return false for invalid conditions
self.output.push_str(&format!("<!-- Error in condition: {} -->", e));
Ok(false)
}
}
}
fn evaluate_expression(&mut self) -> Result<Obj, String> {
self.parse_expression(0)
}
// A recursive descent parser for expressions
fn parse_expression(&mut self, precedence: u8) -> Result<Obj, String> {
let mut left = self.parse_primary()?;
while self.pos < self.tokens.len() {
let current_precedence = self.get_operator_precedence();
if current_precedence <= precedence {
break;
}
left = self.parse_binary_op(left, current_precedence)?;
}
if matches!(self.tokens.get(self.pos), Some(Token::EndOfStatement)) {
self.pos += 1;
}
Ok(left)
}
fn parse_primary(&mut self) -> Result<Obj, String> {
if self.pos >= self.tokens.len() {
return Err("Unexpected end of input while parsing expression".to_string());
}
match &self.tokens[self.pos] {
Token::Object(obj) => {
let value = obj.clone();
self.pos += 1;
Ok(value)
},
Token::Identifier(name) => {
let var_name = name.clone();
self.pos += 1;
// Use the centralized variable access method
match self.handle_variable_access(&var_name, false)? {
Some(value) => Ok(value),
None => Ok(Obj::None) // Should never happen in expression context
}
},
Token::LeftParen => {
self.pos += 1;
let expr = self.evaluate_expression()?;
if matches!(self.tokens.get(self.pos), Some(Token::RightParen)) {
self.pos += 1;
// After a parenthesized expression, check if there's property access or indexing
if matches!(self.tokens.get(self.pos), Some(Token::Dot)) {
self.handle_property_access(expr)
} else if matches!(self.tokens.get(self.pos), Some(Token::LeftSquareBracket)) {
self.handle_indexing(expr)
} else {
Ok(expr)
}
} else {
Err("Expected closing parenthesis".to_string())
}
},
Token::Minus => {
self.pos += 1;
let value = self.parse_expression(100)?; // High precedence for unary operators
match value {
Obj::Numerical(n) => Ok(Obj::Numerical(-n)),
_ => Err("Unary minus can only be applied to numbers".to_string()),
}
},
Token::LogicalNot => {
self.pos += 1;
let value = self.parse_expression(100)?; // High precedence for unary operators
match value {
Obj::Boolean(b) => Ok(Obj::Boolean(!b)),
_ => Ok(Obj::Boolean(false)), // Any non-boolean value is treated as falsy
}
},
_ => Err(format!("Unexpected token in expression: {:?}", self.tokens[self.pos])),
}
}
// Handle property access with dot notation
fn handle_property_access(&mut self, obj: Obj) -> Result<Obj, String> {
self.pos += 1; // Skip the dot
if let Some(Token::Identifier(prop_name)) = self.tokens.get(self.pos).cloned() {
self.pos += 1;
match &obj {
Obj::List(list) => {
match prop_name.as_str() {
"len" => Ok(Obj::Numerical(list.len() as f64)),
// Add other list methods as needed
_ => Err(format!("No property/method '{}' on list", prop_name)),
}
},
Obj::Dict(dict) => {
match prop_name.as_str() {
"len" => Ok(Obj::Numerical(dict.len() as f64)),
// Try to get the property by name
_ => {
if let Some(val) = dict.get(&prop_name) {
Ok(val.clone())
} else {
Err(format!("Key '{}' not found in dictionary", prop_name))
}
}
}
},
Obj::Str(s) => {
match prop_name.as_str() {
"len" => Ok(Obj::Numerical(s.len() as f64)),
// Add other string methods as needed
_ => Err(format!("No property/method '{}' on string", prop_name)),
}
},
_ => Err(format!("Type '{}' does not support properties/methods", obj.type_of()))
}
} else {
Err("Expected identifier after dot".to_string())
}
}
// Handle indexing with [] syntax
fn handle_indexing(&mut self, obj: Obj) -> Result<Obj, String> {
self.pos += 1; // Skip the left bracket
let index = self.evaluate_expression()?;
if !matches!(self.tokens.get(self.pos), Some(Token::RightSquareBracket)) {
return Err("Expected closing bracket after index".to_string());
}
self.pos += 1; // Skip the right bracket
match &obj {
Obj::List(list) => {
match &index {
Obj::Numerical(n) => {
let idx = *n as usize;
if idx < list.len() {
Ok(list[idx].clone())
} else {
Err(format!("Index {} out of bounds for list", idx))
}
},
_ => Err("List index must be a number".to_string())
}
},
Obj::Dict(dict) => {
// Allow both string literals and string expressions as keys
let key = index.interal_value_as_string();
if let Some(val) = dict.get(&key) {
Ok(val.clone())
} else {
Err(format!("Key '{}' not found in dictionary", key))
}
},
_ => Err(format!("Cannot index into a {} value", obj.type_of()))
}
}
fn parse_binary_op(&mut self, left: Obj, precedence: u8) -> Result<Obj, String> {
match &self.tokens[self.pos] {
Token::Plus => {
self.pos += 1;
let right = self.parse_expression(precedence)?;
self.apply_binary_op(left, right, |a, b| a + b)
},
Token::Minus => {
self.pos += 1;
let right = self.parse_expression(precedence)?;
self.apply_binary_op(left, right, |a, b| a - b)
},
Token::Multiply => {
self.pos += 1;
let right = self.parse_expression(precedence)?;
self.apply_binary_op(left, right, |a, b| a * b)
},
Token::Divide => {
self.pos += 1;
let right = self.parse_expression(precedence)?;
match right {
Obj::Numerical(r) if r == 0.0 => Err("Division by zero".to_string()),
_ => self.apply_binary_op(left, right, |a, b| a / b),
}
},
Token::Modulus => {
self.pos += 1;
let right = self.parse_expression(precedence)?;
match right {
Obj::Numerical(r) if r == 0.0 => Err("Modulo by zero".to_string()),
_ => self.apply_binary_op(left, right, |a, b| a % b),
}
},
Token::Exponent => {
self.pos += 1;
let right = self.parse_expression(precedence)?;
self.apply_binary_op(left, right, |a, b| a.powf(b))
},
Token::EqualsEquals => {
self.pos += 1;
let right = self.parse_expression(precedence)?;
Ok(Obj::Boolean(left == right))
},
Token::NotEquals => {
self.pos += 1;
let right = self.parse_expression(precedence)?;
Ok(Obj::Boolean(left != right))
},
Token::LessThan => {
self.pos += 1;
let right = self.parse_expression(precedence)?;
self.apply_comparison_op(left, right, |a, b| a < b)
},
Token::LessThanEquals => {
self.pos += 1;
let right = self.parse_expression(precedence)?;
self.apply_comparison_op(left, right, |a, b| a <= b)
},
Token::GreaterThan => {
self.pos += 1;
let right = self.parse_expression(precedence)?;
self.apply_comparison_op(left, right, |a, b| a > b)
},
Token::GreaterThanEquals => {
self.pos += 1;
let right = self.parse_expression(precedence)?;
self.apply_comparison_op(left, right, |a, b| a >= b)
},
Token::LogicalAnd => {
self.pos += 1;
// Short-circuit evaluation
if !self.is_truthy(&left) {
return Ok(Obj::Boolean(false));
}
let right = self.parse_expression(precedence)?;
Ok(Obj::Boolean(self.is_truthy(&left) && self.is_truthy(&right)))
},
Token::LogicalOr => {
self.pos += 1;
// Short-circuit evaluation
if self.is_truthy(&left) {
return Ok(Obj::Boolean(true));
}
let right = self.parse_expression(precedence)?;
Ok(Obj::Boolean(self.is_truthy(&left) || self.is_truthy(&right)))
},
_ => Err(format!("Unknown operator: {:?}", self.tokens[self.pos])),
}
}
fn apply_binary_op<F>(&self, left: Obj, right: Obj, op: F) -> Result<Obj, String>
where
F: Fn(f64, f64) -> f64,
{
match (left, right) {
(Obj::Numerical(l), Obj::Numerical(r)) => Ok(Obj::Numerical(op(l, r))),
(Obj::Str(l), Obj::Str(r)) => {
if op(1.0, 1.0) == 2.0 {
// Addition for strings is concatenation
Ok(Obj::Str(l + &r))
} else {
Err("Only addition is supported for strings".to_string())
}
},
(Obj::Str(s), Obj::Numerical(n)) => {
if op(1.0, 1.0) == 2.0 {
// Addition: String + Number = String + String(Number)
Ok(Obj::Str(s + &n.to_string()))
} else {
Err("Only addition is supported for strings".to_string())
}
},
(Obj::Numerical(n), Obj::Str(s)) => {
if op(1.0, 1.0) == 2.0 {
// Addition: Number + String = String(Number) + String
Ok(Obj::Str(n.to_string() + &s))
} else {
Err("Only addition is supported for strings".to_string())
}
},
(Obj::List(mut l), Obj::List(r)) => {
if op(1.0, 1.0) == 2.0 {
// Concatenate lists
let mut result = l;
result.extend(r);
Ok(Obj::List(result))
} else {
Err("Only addition is supported for lists".to_string())
}
},
_ => Err("Type mismatch for binary operation".to_string()),
}
}
fn apply_comparison_op<F>(&self, left: Obj, right: Obj, op: F) -> Result<Obj, String>
where
F: Fn(f64, f64) -> bool,
{
match (left, right) {
(Obj::Numerical(l), Obj::Numerical(r)) => Ok(Obj::Boolean(op(l, r))),
(Obj::Str(l), Obj::Str(r)) => {
// Compare strings lexicographically
if op(1.0, 2.0) {
// If op is < or <=
Ok(Obj::Boolean(l < r))
} else {
// If op is > or >=
Ok(Obj::Boolean(l > r))
}
},
_ => Err("Cannot compare different types".to_string()),
}
}
fn get_operator_precedence(&self) -> u8 {
if self.pos >= self.tokens.len() {
return 0;
}
match self.tokens[self.pos] {
Token::LogicalOr => 10,
Token::LogicalAnd => 20,
Token::EqualsEquals | Token::NotEquals => 30,
Token::LessThan | Token::LessThanEquals | Token::GreaterThan | Token::GreaterThanEquals => 40,
Token::Plus | Token::Minus => 50,
Token::Multiply | Token::Divide | Token::Modulus => 60,
Token::Exponent => 70,
Token::Dot => 90, // Highest precedence for property access
Token::LeftSquareBracket => 90, // Highest precedence for array indexing
_ => 0,
}
}
fn is_truthy(&self, value: &Obj) -> bool {
match value {
Obj::Boolean(b) => *b,
Obj::Numerical(n) => *n != 0.0,
Obj::Str(s) => !s.is_empty(),
Obj::List(l) => !l.is_empty(),
Obj::Dict(d) => !d.is_empty(),
Obj::None => false,
}
}
}