12
12
var compilers = require ( "./compiler/compilers" ) ;
13
13
var { typecheck, printSignature } = require ( "./typecheck" ) ;
14
14
var { expandMacros, extractMacros } = require ( "./macro.js" ) ;
15
+ var { defaultImportReader } = require ( "./reader" ) ;
15
16
} catch ( e ) { }
16
17
17
18
function wy2tokens (
@@ -631,30 +632,7 @@ function pyWrapModule(name, src) {
631
632
return `#/*___wenyan_module_${ name } _start___*/\n${ src } \n#/*___wenyan_module_${ name } _end___*/\n` ;
632
633
}
633
634
634
- function defaultReader ( x ) {
635
- try {
636
- const fs = eval ( "require" ) ( "fs" ) ;
637
- try {
638
- return fs . readFileSync ( x + ".wy" ) . toString ( ) ;
639
- } catch ( e ) {
640
- var files = fs . readdirSync ( "./" ) ;
641
- for ( var i = 0 ; i < files . length ; i ++ ) {
642
- if ( fs . lstatSync ( files [ i ] ) . isDirectory ( ) ) {
643
- try {
644
- return fs . readFileSync ( files [ i ] + "/" + x + ".wy" ) . toString ( ) ;
645
- } catch ( e ) { }
646
- }
647
- }
648
- }
649
- console . log ( "Cannot import " , x ) ;
650
- } catch ( e ) {
651
- console . error (
652
- `Cannot import ${ x } , please specify the "reader" option in compile.`
653
- ) ;
654
- }
655
- }
656
-
657
- function compile ( arg1 , arg2 , arg3 ) {
635
+ async function compile ( arg1 , arg2 , arg3 ) {
658
636
let options = { } ;
659
637
let txt = "" ;
660
638
@@ -677,7 +655,8 @@ function compile(arg1, arg2, arg3) {
677
655
: console . dir ( x , { depth : null , maxArrayLength : null } ) ,
678
656
errorCallback = process . exit ,
679
657
lib = typeof STDLIB == "undefined" ? { } : STDLIB ,
680
- reader = defaultReader ,
658
+ reader = defaultImportReader ,
659
+ importPaths = [ ] ,
681
660
strict = false
682
661
} = options ;
683
662
@@ -710,7 +689,7 @@ function compile(arg1, arg2, arg3) {
710
689
return 0 ;
711
690
}
712
691
713
- var macros = extractMacros ( txt , { lib, reader, lang } ) ;
692
+ var macros = await extractMacros ( txt , { lib, reader, lang, importPaths } ) ;
714
693
txt = expandMacros ( txt , macros ) ;
715
694
716
695
logCallback ( "\n\n=== [PASS 0] EXPAND-MACROS ===" ) ;
@@ -745,7 +724,7 @@ function compile(arg1, arg2, arg3) {
745
724
}
746
725
var klass = compilers [ lang ] ;
747
726
var compiler = new klass ( asc ) ;
748
- var result = compiler . compile ( { imports } ) ;
727
+ var result = await compiler . compile ( { imports } ) ;
749
728
var { imports, result } = result ;
750
729
var targ = result ;
751
730
logCallback ( targ ) ;
@@ -758,19 +737,21 @@ function compile(arg1, arg2, arg3) {
758
737
} else if ( imports [ i ] in lib ) {
759
738
isrc = lib [ imports [ i ] ] ;
760
739
} else {
761
- isrc = reader ( imports [ i ] ) ;
740
+ isrc = await reader ( imports [ i ] , importPaths ) ;
762
741
}
763
742
targ =
764
743
mwrapper (
765
744
imports [ i ] ,
766
- compile ( isrc , {
745
+ await compile ( isrc , {
767
746
lang,
768
747
romanizeIdentifiers,
769
748
resetVarCnt : false ,
770
749
strict : false ,
771
750
logCallback,
772
751
errorCallback,
773
- lib
752
+ lib,
753
+ reader,
754
+ importPaths
774
755
} )
775
756
) + targ ;
776
757
}
0 commit comments