This repository was archived by the owner on Nov 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.rs
More file actions
1105 lines (893 loc) · 37.1 KB
/
mod.rs
File metadata and controls
1105 lines (893 loc) · 37.1 KB
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
use crate::ast::{BinaryOp, Expr, Program, Stmt, UnaryOp, Type, Parameter, MethodDeclaration, UseItems, Visibility};
use crate::lexer::token::{Token, TokenType, Position};
pub struct Parser {
tokens: Vec<Token>,
current: usize,
}
#[derive(Debug)]
pub enum ParseError {
UnexpectedToken {
expected: String,
found: TokenType,
},
UnexpectedEOF,
InvalidExpression,
}
type ParseResult<T> = Result<T, ParseError>;
impl Parser {
pub fn new(tokens: Vec<Token>) -> Self {
Parser { tokens, current: 0 }
}
fn current_token(&self) -> Token {
self.tokens.get(self.current)
.cloned()
.unwrap_or_else(|| {
let pos = Position::new(0, 0, 0);
Token::new(TokenType::EOF, String::new(), pos.clone(), pos)
})
}
fn peek(&self, offset: usize) -> Token {
self.tokens.get(self.current + offset)
.cloned()
.unwrap_or_else(|| {
let pos = Position::new(0, 0, 0);
Token::new(TokenType::EOF, String::new(), pos.clone(), pos)
})
}
fn advance(&mut self) -> &Token {
if self.current < self.tokens.len() {
self.current += 1;
}
self.tokens.get(self.current - 1).unwrap()
}
fn check(&self, token_type: TokenType) -> bool {
self.current_token().token_type == token_type
}
fn match_token(&mut self, types: &[TokenType]) -> bool {
for t in types {
if self.check(t.clone()) {
self.advance();
return true;
}
}
false
}
fn consume(&mut self, token_type: TokenType, message: &str) -> ParseResult<Token> {
if self.check(token_type) {
Ok(self.advance().clone())
} else {
Err(ParseError::UnexpectedToken {
expected: message.to_string(),
found: self.current_token().token_type.clone(),
})
}
}
pub fn parse(&mut self) -> ParseResult<Program> {
let mut program = Program::new();
while !self.check(TokenType::EOF) {
let stmt = self.declaration()?;
program.add_statement(stmt);
}
Ok(program)
}
fn declaration(&mut self) -> ParseResult<Stmt> {
// 检查可见性修饰符
let visibility = if self.match_token(&[TokenType::Pub]) {
Visibility::Public
} else {
Visibility::Private
};
if self.match_token(&[TokenType::Let, TokenType::Var]) {
self.var_declaration()
} else if self.match_token(&[TokenType::Fn]) {
self.fn_declaration(visibility)
} else if self.match_token(&[TokenType::Struct]) {
self.struct_declaration(visibility)
} else if self.match_token(&[TokenType::Type]) {
self.type_alias_declaration(visibility)
} else if self.match_token(&[TokenType::Impl]) {
self.impl_block()
} else if self.match_token(&[TokenType::Mod]) {
self.mod_declaration(visibility)
} else if self.match_token(&[TokenType::Use]) {
self.use_statement()
} else {
// 如果有 pub 但没有后续声明,报错
if visibility == Visibility::Public {
return Err(ParseError::UnexpectedToken {
expected: "fn, struct, type, or mod after 'pub'".to_string(),
found: self.current_token().token_type.clone(),
});
}
self.statement()
}
}
fn var_declaration(&mut self) -> ParseResult<Stmt> {
let is_mutable = self.tokens.get(self.current.saturating_sub(1))
.map(|t| t.token_type == TokenType::Var)
.unwrap_or(false);
let name_token = self.consume(TokenType::Identifier, "Expected variable name")?;
let name = name_token.value.clone();
// 解析可选的类型注解
let type_annotation = if self.match_token(&[TokenType::Colon]) {
Some(self.parse_type()?)
} else {
None
};
let initializer = if self.match_token(&[TokenType::Equal]) {
Some(self.expression()?)
} else {
None
};
self.consume(TokenType::Semicolon, "Expected ';' after variable declaration")?;
Ok(Stmt::VarDeclaration {
name,
mutable: is_mutable,
type_annotation,
initializer,
})
}
fn fn_declaration(&mut self, visibility: Visibility) -> ParseResult<Stmt> {
let name_token = self.consume(TokenType::Identifier, "Expected function name")?;
let name = name_token.value.clone();
self.consume(TokenType::LeftParen, "Expected '(' after function name")?;
let mut parameters = Vec::new();
if !self.check(TokenType::RightParen) {
loop {
let param_name = self.consume(TokenType::Identifier, "Expected parameter name")?;
// 解析可选的类型注解
let type_annotation = if self.match_token(&[TokenType::Colon]) {
Some(self.parse_type()?)
} else {
None
};
parameters.push(Parameter {
name: param_name.value.clone(),
type_annotation,
});
if !self.match_token(&[TokenType::Comma]) {
break;
}
}
}
self.consume(TokenType::RightParen, "Expected ')' after parameters")?;
// 解析可选的返回类型
let return_type = if self.match_token(&[TokenType::Arrow]) {
Some(self.parse_type()?)
} else {
None
};
self.consume(TokenType::LeftBrace, "Expected '{' before function body")?;
let mut body = Vec::new();
while !self.check(TokenType::RightBrace) && !self.check(TokenType::EOF) {
body.push(self.declaration()?);
}
self.consume(TokenType::RightBrace, "Expected '}' after function body")?;
Ok(Stmt::FnDeclaration {
visibility,
name,
parameters,
return_type,
body,
})
}
fn struct_declaration(&mut self, visibility: Visibility) -> ParseResult<Stmt> {
let name_token = self.consume(TokenType::Identifier, "Expected struct name")?;
let name = name_token.value.clone();
self.consume(TokenType::LeftBrace, "Expected '{' after struct name")?;
let mut fields = Vec::new();
while !self.check(TokenType::RightBrace) && !self.check(TokenType::EOF) {
let field_name_token = self.consume(TokenType::Identifier, "Expected field name")?;
let field_name = field_name_token.value.clone();
self.consume(TokenType::Colon, "Expected ':' after field name")?;
let field_type = self.parse_type()?;
fields.push(crate::ast::StructField {
name: field_name,
field_type,
});
// 允许可选的逗号
if self.match_token(&[TokenType::Comma]) {
// 继续
} else {
break;
}
}
self.consume(TokenType::RightBrace, "Expected '}' after struct fields")?;
self.consume(TokenType::Semicolon, "Expected ';' after struct declaration")?;
Ok(Stmt::StructDeclaration { visibility, name, fields })
}
fn type_alias_declaration(&mut self, visibility: Visibility) -> ParseResult<Stmt> {
let name_token = self.consume(TokenType::Identifier, "Expected type alias name")?;
let name = name_token.value.clone();
self.consume(TokenType::Equal, "Expected '=' after type alias name")?;
// 检查是否是匿名结构体
let target_type = if self.match_token(&[TokenType::Struct]) {
self.consume(TokenType::LeftBrace, "Expected '{' after 'struct'")?;
let mut fields = Vec::new();
while !self.check(TokenType::RightBrace) && !self.check(TokenType::EOF) {
let field_name_token = self.consume(TokenType::Identifier, "Expected field name")?;
let field_name = field_name_token.value.clone();
self.consume(TokenType::Colon, "Expected ':' after field name")?;
let field_type = self.parse_type()?;
fields.push(crate::ast::StructField {
name: field_name,
field_type,
});
// 允许可选的逗号
if self.match_token(&[TokenType::Comma]) {
// 继续
} else {
break;
}
}
self.consume(TokenType::RightBrace, "Expected '}' after struct fields")?;
Type::Struct(crate::ast::StructType {
name: format!("anonymous_{}", name),
fields,
})
} else {
// 普通类型别名 - 可以是基本类型或用户定义类型
self.parse_type()?
};
self.consume(TokenType::Semicolon, "Expected ';' after type alias")?;
Ok(Stmt::TypeAlias { visibility, name, target_type })
}
fn impl_block(&mut self) -> ParseResult<Stmt> {
// impl TypeName { ... }
let type_token = self.consume(TokenType::Identifier, "Expected type name after 'impl'")?;
let type_name = type_token.value.clone();
self.consume(TokenType::LeftBrace, "Expected '{' after type name")?;
let mut methods = Vec::new();
while !self.check(TokenType::RightBrace) && !self.check(TokenType::EOF) {
// 解析方法 (跟函数类似,但有隐式的 self 参数)
self.consume(TokenType::Fn, "Expected 'fn' for method declaration")?;
let method_name_token = self.consume(TokenType::Identifier, "Expected method name")?;
let method_name = method_name_token.value.clone();
self.consume(TokenType::LeftParen, "Expected '(' after method name")?;
let mut parameters = Vec::new();
// 第一个参数应该是 self
if !self.check(TokenType::RightParen) {
let first_param = self.consume(TokenType::Identifier, "Expected parameter name")?;
if first_param.value == "self" {
// self 参数不需要类型注解,会自动推断为当前类型
// 继续解析后面的参数
if self.match_token(&[TokenType::Comma]) {
loop {
let param_name = self.consume(TokenType::Identifier, "Expected parameter name")?;
let type_annotation = if self.match_token(&[TokenType::Colon]) {
Some(self.parse_type()?)
} else {
None
};
parameters.push(Parameter {
name: param_name.value.clone(),
type_annotation,
});
if !self.match_token(&[TokenType::Comma]) {
break;
}
}
}
} else {
return Err(ParseError::UnexpectedToken {
expected: "self".to_string(),
found: TokenType::Identifier,
});
}
}
self.consume(TokenType::RightParen, "Expected ')' after parameters")?;
// 解析可选的返回类型
let return_type = if self.match_token(&[TokenType::Arrow]) {
Some(self.parse_type()?)
} else {
None
};
self.consume(TokenType::LeftBrace, "Expected '{' before method body")?;
let mut body = Vec::new();
while !self.check(TokenType::RightBrace) && !self.check(TokenType::EOF) {
body.push(self.declaration()?);
}
self.consume(TokenType::RightBrace, "Expected '}' after method body")?;
methods.push(MethodDeclaration {
name: method_name,
parameters,
return_type,
body,
});
}
self.consume(TokenType::RightBrace, "Expected '}' after impl block")?;
Ok(Stmt::ImplBlock { type_name, methods })
}
// 解析模块声明: mod name { ... }
fn mod_declaration(&mut self, visibility: Visibility) -> ParseResult<Stmt> {
let name_token = self.consume(TokenType::Identifier, "Expected module name after 'mod'")?;
let name = name_token.value.clone();
// 检查是否是模块引用(从文件加载): mod name;
if self.match_token(&[TokenType::Semicolon]) {
return Ok(Stmt::ModuleReference {
name,
is_public: visibility == Visibility::Public,
});
}
// 否则是内联模块声明: mod name { ... }
self.consume(TokenType::LeftBrace, "Expected '{' or ';' after module name")?;
let mut statements = Vec::new();
while !self.check(TokenType::RightBrace) && !self.check(TokenType::EOF) {
statements.push(self.declaration()?);
}
self.consume(TokenType::RightBrace, "Expected '}' after module body")?;
Ok(Stmt::ModuleDeclaration {
name,
statements,
is_public: visibility == Visibility::Public,
})
}
// 解析导入语句: use path::item;
fn use_statement(&mut self) -> ParseResult<Stmt> {
// 解析模块路径
let mut path = vec![
self.consume(TokenType::Identifier, "Expected module name after 'use'")?.value
];
// 解析路径 (module::submodule::item)
while self.match_token(&[TokenType::DoubleColon]) {
// 检查通配符 *
if self.match_token(&[TokenType::Star]) {
self.consume(TokenType::Semicolon, "Expected ';' after use statement")?;
return Ok(Stmt::UseStatement {
path,
items: UseItems::All,
});
}
// 检查多项导入 {item1, item2, ...}
if self.check(TokenType::LeftBrace) {
self.advance();
let mut items = Vec::new();
loop {
let item = self.consume(TokenType::Identifier, "Expected identifier in use list")?.value;
items.push(item);
if !self.match_token(&[TokenType::Comma]) {
break;
}
}
self.consume(TokenType::RightBrace, "Expected '}' after use list")?;
self.consume(TokenType::Semicolon, "Expected ';' after use statement")?;
return Ok(Stmt::UseStatement {
path,
items: UseItems::Multiple(items),
});
}
// 继续解析路径
path.push(self.consume(TokenType::Identifier, "Expected identifier after '::'")?.value);
}
// 检查重命名 (as keyword)
if self.match_token(&[TokenType::As]) {
let alias = self.consume(TokenType::Identifier, "Expected alias after 'as'")?.value;
let item = path.pop().unwrap();
self.consume(TokenType::Semicolon, "Expected ';' after use statement")?;
return Ok(Stmt::UseStatement {
path,
items: UseItems::Renamed(item, alias),
});
}
// 单个项导入
let item = path.pop().unwrap();
self.consume(TokenType::Semicolon, "Expected ';' after use statement")?;
Ok(Stmt::UseStatement {
path,
items: UseItems::Single(item),
})
}
fn parse_type(&mut self) -> ParseResult<Type> {
// 检查数组类型 [element_type]
if self.check(TokenType::LeftBracket) {
self.advance(); // 消费 '['
let element_type = self.parse_type()?;
self.consume(TokenType::RightBracket, "Expected ']' after array element type")?;
return Ok(Type::Array(Box::new(element_type)));
}
// 检查匿名结构体类型
if self.match_token(&[TokenType::Struct]) {
self.consume(TokenType::LeftBrace, "Expected '{' after 'struct'")?;
let mut fields = Vec::new();
while !self.check(TokenType::RightBrace) && !self.check(TokenType::EOF) {
let field_name_token = self.consume(TokenType::Identifier, "Expected field name")?;
let field_name = field_name_token.value.clone();
self.consume(TokenType::Colon, "Expected ':' after field name")?;
let field_type = self.parse_type()?;
fields.push(crate::ast::StructField {
name: field_name,
field_type,
});
if self.match_token(&[TokenType::Comma]) {
// 继续
} else {
break;
}
}
self.consume(TokenType::RightBrace, "Expected '}' after struct fields")?;
return Ok(Type::Struct(crate::ast::StructType {
name: "anonymous".to_string(),
fields,
}));
}
let token = self.current_token();
match token.token_type {
TokenType::Int => {
self.advance();
Ok(Type::Int)
}
TokenType::Float64 => {
self.advance();
Ok(Type::Float)
}
TokenType::String64 => {
self.advance();
Ok(Type::String)
}
TokenType::Bool => {
self.advance();
Ok(Type::Bool)
}
TokenType::Void => {
self.advance();
Ok(Type::Void)
}
TokenType::Null => {
self.advance();
Ok(Type::Null)
}
TokenType::Char => {
self.advance();
Ok(Type::Char)
}
TokenType::Identifier => {
// 用户定义的类型(结构体名或类型别名)
let type_name = token.value.clone();
self.advance();
Ok(Type::Named(type_name))
}
_ => Err(ParseError::UnexpectedToken {
expected: "type name".to_string(),
found: token.token_type.clone(),
}),
}
}
fn statement(&mut self) -> ParseResult<Stmt> {
if self.match_token(&[TokenType::Return]) {
self.return_statement()
} else if self.match_token(&[TokenType::Break]) {
self.break_statement()
} else if self.match_token(&[TokenType::Continue]) {
self.continue_statement()
} else if self.match_token(&[TokenType::If]) {
self.if_statement()
} else if self.match_token(&[TokenType::While]) {
self.while_statement()
} else if self.match_token(&[TokenType::For]) {
self.for_statement()
} else if self.match_token(&[TokenType::Print]) {
self.print_statement()
} else if self.match_token(&[TokenType::LeftBrace]) {
self.block_statement()
} else {
self.expression_statement()
}
}
fn break_statement(&mut self) -> ParseResult<Stmt> {
self.consume(TokenType::Semicolon, "Expected ';' after break")?;
Ok(Stmt::Break)
}
fn continue_statement(&mut self) -> ParseResult<Stmt> {
self.consume(TokenType::Semicolon, "Expected ';' after continue")?;
Ok(Stmt::Continue)
}
fn return_statement(&mut self) -> ParseResult<Stmt> {
let value = if !self.check(TokenType::Semicolon) {
Some(self.expression()?)
} else {
None
};
self.consume(TokenType::Semicolon, "Expected ';' after return value")?;
Ok(Stmt::Return { value })
}
fn if_statement(&mut self) -> ParseResult<Stmt> {
let condition = self.expression()?;
self.consume(TokenType::LeftBrace, "Expected '{' after if condition")?;
let mut then_branch = Vec::new();
while !self.check(TokenType::RightBrace) && !self.check(TokenType::EOF) {
then_branch.push(self.declaration()?);
}
self.consume(TokenType::RightBrace, "Expected '}' after then branch")?;
let else_branch = if self.match_token(&[TokenType::Else]) {
self.consume(TokenType::LeftBrace, "Expected '{' after else")?;
let mut else_stmts = Vec::new();
while !self.check(TokenType::RightBrace) && !self.check(TokenType::EOF) {
else_stmts.push(self.declaration()?);
}
self.consume(TokenType::RightBrace, "Expected '}' after else branch")?;
Some(else_stmts)
} else {
None
};
Ok(Stmt::If {
condition,
then_branch,
else_branch,
})
}
fn while_statement(&mut self) -> ParseResult<Stmt> {
let condition = self.expression()?;
self.consume(TokenType::LeftBrace, "Expected '{' after while condition")?;
let mut body = Vec::new();
while !self.check(TokenType::RightBrace) && !self.check(TokenType::EOF) {
body.push(self.declaration()?);
}
self.consume(TokenType::RightBrace, "Expected '}' after while body")?;
Ok(Stmt::While { condition, body })
}
fn for_statement(&mut self) -> ParseResult<Stmt> {
let var_token = self.consume(TokenType::Identifier, "Expected variable name")?;
let variable = var_token.value.clone();
self.consume(TokenType::In, "Expected 'in' after loop variable")?;
let start = self.expression()?;
self.consume(TokenType::DotDot, "Expected '..' in range")?;
let end = self.expression()?;
self.consume(TokenType::LeftBrace, "Expected '{' after for range")?;
let mut body = Vec::new();
while !self.check(TokenType::RightBrace) && !self.check(TokenType::EOF) {
body.push(self.declaration()?);
}
self.consume(TokenType::RightBrace, "Expected '}' after for body")?;
Ok(Stmt::For {
variable,
start,
end,
body,
})
}
fn print_statement(&mut self) -> ParseResult<Stmt> {
self.consume(TokenType::LeftParen, "Expected '(' after 'print'")?;
let value = self.expression()?;
self.consume(TokenType::RightParen, "Expected ')' after print value")?;
self.consume(TokenType::Semicolon, "Expected ';' after print statement")?;
Ok(Stmt::Print { value })
}
fn block_statement(&mut self) -> ParseResult<Stmt> {
let mut statements = Vec::new();
while !self.check(TokenType::RightBrace) && !self.check(TokenType::EOF) {
statements.push(self.declaration()?);
}
self.consume(TokenType::RightBrace, "Expected '}' after block")?;
Ok(Stmt::Block { statements })
}
fn expression_statement(&mut self) -> ParseResult<Stmt> {
let expr = self.expression()?;
self.consume(TokenType::Semicolon, "Expected ';' after expression")?;
Ok(Stmt::Expression(expr))
}
fn expression(&mut self) -> ParseResult<Expr> {
self.assignment()
}
fn assignment(&mut self) -> ParseResult<Expr> {
let expr = self.or()?;
if self.match_token(&[TokenType::Equal]) {
match expr {
Expr::Identifier(name) => {
let value = self.assignment()?;
return Ok(Expr::assign(name, value));
}
Expr::Index { object, index } => {
let value = self.assignment()?;
return Ok(Expr::index_assign(*object, *index, value));
}
Expr::FieldAccess { object, field } => {
let value = self.assignment()?;
return Ok(Expr::field_assign(*object, field, value));
}
_ => {}
}
} else if self.match_token(&[TokenType::PlusEqual, TokenType::MinusEqual,
TokenType::StarEqual, TokenType::SlashEqual,
TokenType::PercentEqual]) {
// 获取运算符类型
let prev_token = self.tokens[self.current - 1].token_type.clone();
let op = match prev_token {
TokenType::PlusEqual => BinaryOp::Add,
TokenType::MinusEqual => BinaryOp::Subtract,
TokenType::StarEqual => BinaryOp::Multiply,
TokenType::SlashEqual => BinaryOp::Divide,
TokenType::PercentEqual => BinaryOp::Modulo,
_ => unreachable!(),
};
match expr.clone() {
Expr::Identifier(name) => {
let value = self.assignment()?;
// x += y 转换为 x = x + y
let new_value = Expr::binary(expr, op, value);
return Ok(Expr::assign(name, new_value));
}
Expr::Index { object, index } => {
let value = self.assignment()?;
// arr[i] += y 转换为 arr[i] = arr[i] + y
let new_value = Expr::binary(expr, op, value);
return Ok(Expr::index_assign(*object, *index, new_value));
}
Expr::FieldAccess { object, field } => {
let value = self.assignment()?;
// obj.field += y 转换为 obj.field = obj.field + y
let new_value = Expr::binary(expr, op, value);
return Ok(Expr::field_assign(*object, field, new_value));
}
_ => {}
}
}
Ok(expr)
}
fn or(&mut self) -> ParseResult<Expr> {
let mut expr = self.and()?;
while self.match_token(&[TokenType::Or]) {
let right = self.and()?;
expr = Expr::binary(expr, BinaryOp::Or, right);
}
Ok(expr)
}
fn and(&mut self) -> ParseResult<Expr> {
let mut expr = self.equality()?;
while self.match_token(&[TokenType::And]) {
let right = self.equality()?;
expr = Expr::binary(expr, BinaryOp::And, right);
}
Ok(expr)
}
fn equality(&mut self) -> ParseResult<Expr> {
let mut expr = self.comparison()?;
while self.match_token(&[TokenType::EqualEqual, TokenType::BangEqual]) {
let op = match self.tokens.get(self.current.saturating_sub(1))
.map(|t| &t.token_type)
.unwrap() {
TokenType::EqualEqual => BinaryOp::Equal,
TokenType::BangEqual => BinaryOp::NotEqual,
_ => unreachable!(),
};
let right = self.comparison()?;
expr = Expr::binary(expr, op, right);
}
Ok(expr)
}
fn comparison(&mut self) -> ParseResult<Expr> {
let mut expr = self.term()?;
while self.match_token(&[
TokenType::Greater,
TokenType::GreaterEqual,
TokenType::Less,
TokenType::LessEqual,
]) {
let op = match self.tokens.get(self.current.saturating_sub(1))
.map(|t| &t.token_type)
.unwrap() {
TokenType::Greater => BinaryOp::Greater,
TokenType::GreaterEqual => BinaryOp::GreaterEqual,
TokenType::Less => BinaryOp::Less,
TokenType::LessEqual => BinaryOp::LessEqual,
_ => unreachable!(),
};
let right = self.term()?;
expr = Expr::binary(expr, op, right);
}
Ok(expr)
}
fn term(&mut self) -> ParseResult<Expr> {
let mut expr = self.factor()?;
while self.match_token(&[TokenType::Plus, TokenType::Minus]) {
let op = match self.tokens.get(self.current.saturating_sub(1))
.map(|t| &t.token_type)
.unwrap() {
TokenType::Plus => BinaryOp::Add,
TokenType::Minus => BinaryOp::Subtract,
_ => unreachable!(),
};
let right = self.factor()?;
expr = Expr::binary(expr, op, right);
}
Ok(expr)
}
fn factor(&mut self) -> ParseResult<Expr> {
let mut expr = self.unary()?;
while self.match_token(&[TokenType::Star, TokenType::Slash, TokenType::Percent]) {
let op = match self.tokens.get(self.current.saturating_sub(1))
.map(|t| &t.token_type)
.unwrap() {
TokenType::Star => BinaryOp::Multiply,
TokenType::Slash => BinaryOp::Divide,
TokenType::Percent => BinaryOp::Modulo,
_ => unreachable!(),
};
let right = self.unary()?;
expr = Expr::binary(expr, op, right);
}
Ok(expr)
}
fn unary(&mut self) -> ParseResult<Expr> {
if self.match_token(&[TokenType::Bang, TokenType::Minus]) {
let op = match self.tokens.get(self.current.saturating_sub(1))
.map(|t| &t.token_type)
.unwrap() {
TokenType::Bang => UnaryOp::Not,
TokenType::Minus => UnaryOp::Negate,
_ => unreachable!(),
};
let operand = self.unary()?;
return Ok(Expr::unary(op, operand));
}
self.call()
}
fn call(&mut self) -> ParseResult<Expr> {
let mut expr = self.primary()?;
loop {
if self.match_token(&[TokenType::LeftParen]) {
expr = self.finish_call(expr)?;
} else if self.match_token(&[TokenType::LeftBracket]) {
let index = self.expression()?;
self.consume(TokenType::RightBracket, "Expected ']' after index")?;
expr = Expr::index(expr, index);
} else if self.match_token(&[TokenType::Dot]) {
// 字段访问或方法调用
let field_token = self.consume(TokenType::Identifier, "Expected field name after '.'")?;
let field = field_token.value.clone();
// 检查是否是方法调用 (后面跟着左括号)
if self.check(TokenType::LeftParen) {
self.advance(); // 消费 '('
expr = self.finish_method_call(expr, field)?;
} else {
expr = Expr::field_access(expr, field);
}
} else {
break;
}
}
Ok(expr)
}
fn finish_call(&mut self, callee: Expr) -> ParseResult<Expr> {
let mut arguments = Vec::new();
if !self.check(TokenType::RightParen) {
loop {
arguments.push(self.expression()?);
if !self.match_token(&[TokenType::Comma]) {
break;
}
}
}
self.consume(TokenType::RightParen, "Expected ')' after arguments")?;
Ok(Expr::call(callee, arguments))
}
fn finish_method_call(&mut self, object: Expr, method: String) -> ParseResult<Expr> {
let mut arguments = Vec::new();
if !self.check(TokenType::RightParen) {
loop {
arguments.push(self.expression()?);
if !self.match_token(&[TokenType::Comma]) {
break;
}
}
}
self.consume(TokenType::RightParen, "Expected ')' after arguments")?;
Ok(Expr::method_call(object, method, arguments))
}
fn primary(&mut self) -> ParseResult<Expr> {
if self.match_token(&[TokenType::True]) {
return Ok(Expr::boolean(true));
}
if self.match_token(&[TokenType::False]) {
return Ok(Expr::boolean(false));
}
if self.match_token(&[TokenType::Integer]) {
let value = self.tokens.get(self.current.saturating_sub(1))
.unwrap().value.parse::<i64>().unwrap();
return Ok(Expr::integer(value));
}
if self.match_token(&[TokenType::Float]) {
let value = self.tokens.get(self.current.saturating_sub(1))
.unwrap().value.parse::<f64>().unwrap();
return Ok(Expr::float(value));
}
if self.match_token(&[TokenType::String]) {
let value = self.tokens.get(self.current.saturating_sub(1))
.unwrap().value.clone();
return Ok(Expr::string(value));
}
if self.match_token(&[TokenType::Char]) {
let value = self.tokens.get(self.current.saturating_sub(1))
.unwrap().value.clone();
// 解析字符字面量 (移除单引号)
let char_value = if value.len() >= 2 {
value.chars().nth(0).unwrap_or('\0')
} else {
'\0'
};
return Ok(Expr::Char(char_value));
}
if self.match_token(&[TokenType::Identifier]) {
let name = self.tokens.get(self.current.saturating_sub(1))
.unwrap().value.clone();
// 检查是否是路径表达式 module::item
if self.check(TokenType::DoubleColon) {