1
1
try {
2
- var { hanzi2num, hanzi2numstr, num2hanzi, bool2hanzi } = require ( "./hanzi2num" ) ;
2
+ var {
3
+ hanzi2num,
4
+ hanzi2numstr,
5
+ num2hanzi,
6
+ bool2hanzi
7
+ } = require ( "./hanzi2num" ) ;
3
8
var hanzi2pinyin = require ( "./hanzi2pinyin" ) ;
4
9
var STDLIB = require ( "./stdlib" ) ;
5
10
var { NUMBER_KEYWORDS , KEYWORDS } = require ( "./keywords" ) ;
@@ -648,10 +653,21 @@ function defaultReader(x) {
648
653
}
649
654
}
650
655
651
- function compile (
652
- lang ,
653
- txt ,
654
- {
656
+ function compile ( arg1 , arg2 , arg3 ) {
657
+ let options = { } ;
658
+ let txt = "" ;
659
+
660
+ if ( typeof arg2 === "string" ) {
661
+ // backward compatible
662
+ txt = arg2 ;
663
+ options = { ...arg3 , lang : arg1 } ;
664
+ } else {
665
+ txt = arg1 ;
666
+ options = arg2 ;
667
+ }
668
+
669
+ const {
670
+ lang = "js" ,
655
671
romanizeIdentifiers = "none" ,
656
672
resetVarCnt,
657
673
logCallback = x =>
@@ -661,8 +677,8 @@ function compile(
661
677
errorCallback = process . exit ,
662
678
lib = typeof STDLIB == "undefined" ? { } : STDLIB ,
663
679
reader = defaultReader
664
- } = { }
665
- ) {
680
+ } = options ;
681
+
666
682
if ( resetVarCnt ) idenMap = { } ;
667
683
txt = ( txt || "" ) . replace ( / \r \n / g, "\n" ) ;
668
684
@@ -692,7 +708,7 @@ function compile(
692
708
return 0 ;
693
709
}
694
710
695
- var macros = extractMacros ( lang , txt , { lib, reader } ) ;
711
+ var macros = extractMacros ( txt , { lib, reader, lang } ) ;
696
712
txt = expandMacros ( txt , macros ) ;
697
713
698
714
logCallback ( "\n\n=== [PASS 0] EXPAND-MACROS ===" ) ;
@@ -740,7 +756,8 @@ function compile(
740
756
targ =
741
757
mwrapper (
742
758
imports [ i ] ,
743
- compile ( lang , isrc , {
759
+ compile ( isrc , {
760
+ lang,
744
761
romanizeIdentifiers,
745
762
resetVarCnt : false ,
746
763
logCallback,
@@ -753,8 +770,72 @@ function compile(
753
770
return targ ;
754
771
}
755
772
773
+ function isLangSupportedForEval ( lang ) {
774
+ if ( lang !== "js" )
775
+ throw new Error (
776
+ `Executing for target language "${ lang } " is not supported in current environment`
777
+ ) ;
778
+ return true ;
779
+ }
780
+
781
+ function hanzinize ( value ) {
782
+ if ( typeof value == "number" ) {
783
+ return num2hanzi ( value ) ;
784
+ } else if ( typeof value == "boolean" ) {
785
+ return bool2hanzi ( value ) ;
786
+ } else if ( Array . isArray ( value ) ) {
787
+ return value . map ( i => hanzinize ( i ) ) . join ( "。" ) ;
788
+ } else {
789
+ return value ;
790
+ }
791
+ }
792
+
793
+ function outputHanziWrapper ( log , outputHanzi ) {
794
+ return function output ( ...args ) {
795
+ log ( ...args . map ( i => ( outputHanzi ? hanzinize ( i ) : i ) ) ) ;
796
+ } ;
797
+ }
798
+
799
+ function evalCompiled ( compiledCode , options = { } ) {
800
+ const {
801
+ outputHanzi = true ,
802
+ scoped = false ,
803
+ lang = "js" ,
804
+ output = console . log
805
+ } = options ;
806
+
807
+ isLangSupportedForEval ( lang ) ;
808
+
809
+ let code = compiledCode ;
810
+
811
+ ( ( ) => {
812
+ const _console_log = console . log ;
813
+ console . log = outputHanziWrapper ( output , outputHanzi ) ;
814
+ try {
815
+ if ( ! scoped && "window" in this ) {
816
+ window . eval ( code ) ;
817
+ } else {
818
+ eval ( code ) ;
819
+ }
820
+ } catch ( e ) {
821
+ throw e ;
822
+ } finally {
823
+ console . log = _console_log ;
824
+ }
825
+ } ) ( ) ;
826
+ }
827
+
828
+ function execute ( source , options = { } ) {
829
+ const { lang = "js" } = options ;
830
+ isLangSupportedForEval ( lang ) ;
831
+ const compiled = compile ( source , options ) ;
832
+ evalCompiled ( compiled , options ) ;
833
+ }
834
+
756
835
var parser = {
757
836
compile,
837
+ evalCompiled,
838
+ execute,
758
839
version,
759
840
wy2tokens,
760
841
tokens2asc,
0 commit comments