6
6
//! https://github.com/micropython/micropython/blob/master/py/compile.c
7
7
8
8
use crate :: bytecode:: { self , CallType , CodeObject , Instruction , Varargs } ;
9
- use crate :: error:: CompileError ;
9
+ use crate :: error:: { CompileError , CompileErrorType } ;
10
10
use crate :: obj:: objcode;
11
11
use crate :: obj:: objcode:: PyCodeRef ;
12
12
use crate :: pyobject:: PyValue ;
@@ -40,17 +40,17 @@ pub fn compile(
40
40
41
41
match mode {
42
42
Mode :: Exec => {
43
- let ast = parser:: parse_program ( source) . map_err ( CompileError :: Parse ) ?;
43
+ let ast = parser:: parse_program ( source) ?;
44
44
let symbol_table = make_symbol_table ( & ast) ;
45
45
compiler. compile_program ( & ast, symbol_table)
46
46
}
47
47
Mode :: Eval => {
48
- let statement = parser:: parse_statement ( source) . map_err ( CompileError :: Parse ) ?;
48
+ let statement = parser:: parse_statement ( source) ?;
49
49
let symbol_table = statements_to_symbol_table ( & statement) ;
50
50
compiler. compile_statement_eval ( & statement, symbol_table)
51
51
}
52
52
Mode :: Single => {
53
- let ast = parser:: parse_program ( source) . map_err ( CompileError :: Parse ) ?;
53
+ let ast = parser:: parse_program ( source) ?;
54
54
let symbol_table = make_symbol_table ( & ast) ;
55
55
compiler. compile_program_single ( & ast, symbol_table)
56
56
}
@@ -158,7 +158,10 @@ impl Compiler {
158
158
if let ast:: Statement :: Expression { ref expression } = statement. node {
159
159
self . compile_expression ( expression) ?;
160
160
} else {
161
- return Err ( CompileError :: ExpectExpr ) ;
161
+ return Err ( CompileError {
162
+ error : CompileErrorType :: ExpectExpr ,
163
+ location : statement. location . clone ( ) ,
164
+ } ) ;
162
165
}
163
166
}
164
167
self . emit ( Instruction :: ReturnValue ) ;
@@ -397,19 +400,28 @@ impl Compiler {
397
400
}
398
401
ast:: Statement :: Break => {
399
402
if !self . in_loop {
400
- return Err ( CompileError :: InvalidBreak ) ;
403
+ return Err ( CompileError {
404
+ error : CompileErrorType :: InvalidBreak ,
405
+ location : statement. location . clone ( ) ,
406
+ } ) ;
401
407
}
402
408
self . emit ( Instruction :: Break ) ;
403
409
}
404
410
ast:: Statement :: Continue => {
405
411
if !self . in_loop {
406
- return Err ( CompileError :: InvalidContinue ) ;
412
+ return Err ( CompileError {
413
+ error : CompileErrorType :: InvalidContinue ,
414
+ location : statement. location . clone ( ) ,
415
+ } ) ;
407
416
}
408
417
self . emit ( Instruction :: Continue ) ;
409
418
}
410
419
ast:: Statement :: Return { value } => {
411
420
if !self . in_function_def {
412
- return Err ( CompileError :: InvalidReturn ) ;
421
+ return Err ( CompileError {
422
+ error : CompileErrorType :: InvalidReturn ,
423
+ location : statement. location . clone ( ) ,
424
+ } ) ;
413
425
}
414
426
match value {
415
427
Some ( v) => {
@@ -462,7 +474,10 @@ impl Compiler {
462
474
self . emit ( Instruction :: DeleteSubscript ) ;
463
475
}
464
476
_ => {
465
- return Err ( CompileError :: Delete ( target. name ( ) ) ) ;
477
+ return Err ( CompileError {
478
+ error : CompileErrorType :: Delete ( target. name ( ) ) ,
479
+ location : self . current_source_location . clone ( ) ,
480
+ } ) ;
466
481
}
467
482
}
468
483
}
@@ -1021,7 +1036,10 @@ impl Compiler {
1021
1036
for ( i, element) in elements. iter ( ) . enumerate ( ) {
1022
1037
if let ast:: Expression :: Starred { .. } = element {
1023
1038
if seen_star {
1024
- return Err ( CompileError :: StarArgs ) ;
1039
+ return Err ( CompileError {
1040
+ error : CompileErrorType :: StarArgs ,
1041
+ location : self . current_source_location . clone ( ) ,
1042
+ } ) ;
1025
1043
} else {
1026
1044
seen_star = true ;
1027
1045
self . emit ( Instruction :: UnpackEx {
@@ -1047,7 +1065,10 @@ impl Compiler {
1047
1065
}
1048
1066
}
1049
1067
_ => {
1050
- return Err ( CompileError :: Assign ( target. name ( ) ) ) ;
1068
+ return Err ( CompileError {
1069
+ error : CompileErrorType :: Assign ( target. name ( ) ) ,
1070
+ location : self . current_source_location . clone ( ) ,
1071
+ } ) ;
1051
1072
}
1052
1073
}
1053
1074
@@ -1237,7 +1258,10 @@ impl Compiler {
1237
1258
}
1238
1259
ast:: Expression :: Yield { value } => {
1239
1260
if !self . in_function_def {
1240
- return Err ( CompileError :: InvalidYield ) ;
1261
+ return Err ( CompileError {
1262
+ error : CompileErrorType :: InvalidYield ,
1263
+ location : self . current_source_location . clone ( ) ,
1264
+ } ) ;
1241
1265
}
1242
1266
self . mark_generator ( ) ;
1243
1267
match value {
0 commit comments