@@ -692,7 +692,6 @@ where compile() was called, in untrusted/run.js.
692
692
693
693
if ( ! foundfct ) {
694
694
// Check whether this line contains function calls inside strings, which will not need "await"
695
- // const awaitpatt = new RegExp('\\W(\\w+)\\s*\\(') // find a function call
696
695
while ( true ) {
697
696
var m = line . slice ( start ) . match ( awaitpatt )
698
697
if ( m !== null ) {
@@ -877,8 +876,7 @@ where compile() was called, in untrusted/run.js.
877
876
arr += " Array.prototype['*']=function(r) {return __array_times_number(this, r)}\n" // multiplying Python list times number
878
877
if ( VPython_names . length <= 0 ) arr += vars
879
878
program = program . slice ( 0 , start ) + arr + program . slice ( start - 4 )
880
- } // end handle Python imports
881
- //program = program.replace(new RegExp('\\(function\\(\\)', 'g'), '(async function()')
879
+ } // end handle Python imports
882
880
883
881
} else { // JavaScript
884
882
var prog = 'function __main__() {\n"use strict";\n'
@@ -924,7 +922,6 @@ where compile() was called, in untrusted/run.js.
924
922
925
923
// Prepend 'async ' to all user functions
926
924
while ( true ) {
927
- // const asyncpatt1 = new RegExp('\\Wfunction\\s*(\\w+)\\s*\\(') // find "function f(...."
928
925
if ( options . lang != 'javascript' ) m = prog . slice ( start ) . match ( asyncpatt1 )
929
926
else m = prog . slice ( start ) . match ( jsasyncpatt1 )
930
927
if ( m === null ) break
@@ -942,55 +939,24 @@ where compile() was called, in untrusted/run.js.
942
939
943
940
// Prepend "await " to calls to user functions and the GlowScript async functions (all user functions are marked async).
944
941
start = initialstart
945
- const noawait = [ 'if' , 'else' , 'defineProperties' , 'for' , 'while' , 'append' , 'function' , 'return' ,
946
- 'vec' , 'vector' , 'cross' , 'dot' , 'add' , 'divide' , 'multiply' , 'sub' , 'mag' , 'mag2' , 'norm' ,
947
- 'hat' , 'proj' , 'comp' , 'diff_angle' , 'radians' , 'degrees' , 'rotate' , 'pow' , 'radians' , 'degrees' ,
948
- 'GS_power' , 'float' , 'format' , 'int' , 'len' , 'pow' , 'print' , 'range' , 'arange' , 'str' ,
949
- 'abs' , 'sqrt' , 'sin' , 'cos' , 'tan' , 'asin' , 'acos' , 'atan' , 'atan2' , 'exp' , 'log' ,
950
- 'ceil' , 'floor' , 'sign' , 'round' , 'max' , 'min' , 'random' , 'factorial' , 'combin' ,
951
- 'box' , 'cylinder' , 'cone' , 'pyramid' , 'sphere' , 'simple_sphere' , 'arrow' , 'curve' , 'points' ,
952
- 'paths' , 'shapes' , 'helix' , 'ring' , 'compound' , 'vertex' , 'triangle' , 'quad' , 'label' ,
953
- 'distant_light' , 'attach_trail' , 'textures' , 'bumpmaps' , 'text' , 'wtext' , 'winput' ,
954
- 'radio' , 'checkbox' , 'button' , 'slider' , 'menu' , 'input' , 'extrusion' ,
955
- 'graph' , 'gdisplay' , 'series' , 'gcurve' , 'gdots' , 'gvbars' , 'ghbars' , 'ghistogram' ]
956
- const noawait_method = [ 'rotate' , 'clone' , 'rgb_to_hsv' , 'hsv_to_rgb' , 'norm' , 'diff_angle' , 'project' ,
957
- 'bind' , 'unbind' , 'trigger' ]
958
-
959
- // VPython_import is the prefix of VPython objects, with value 'null' if no import statement
960
- // const awaitpatt = new RegExp('\\W(\\w+)\\s*\\(') // find a function call
942
+ const awaitpatt2 = new RegExp ( '(\\w+)\\s*\\(' ) // find a function call
961
943
while ( true ) {
962
- m = prog . slice ( start ) . match ( awaitpatt )
963
- var period = false
944
+ m = prog . slice ( start ) . match ( awaitpatt2 )
964
945
if ( m === null ) break
965
- period = ( prog [ start + m . index ] == '.' )
966
946
name = m [ 1 ]
967
-
968
- // If this is a class, delete the call to __init__ (we will insert this call after creating the class instance)
969
- if ( ! period && classes . indexOf ( name ) >= 0 ) {
970
- start += m . index + name . length + 1
971
- var lbrace = prog . slice ( start ) . search ( '{' )
972
- var rbrace = prog . slice ( start ) . search ( 'arguments' )
973
- prog = prog . slice ( 0 , start + lbrace + 1 ) + prog . slice ( start + 'arguments' . length + rbrace + 1 )
974
- continue
975
- }
976
-
977
- // Might start with 'RS_' or be string.option(...) or be color.gray(.7)
978
- if ( name . slice ( 0 , 3 ) == 'RS_' ||
979
- ( period && ( prog [ start + m . index - 1 ] == '"' ) ) ||
980
- ( name == 'gray' && period ) ) {
947
+ if ( fcts . indexOf ( name ) < 0 ) { // fcts is a list of user functions plus GlowScript async functions such as rate
948
+ // If this is a class, delete the call to __init__ (we will insert this call after creating the class instance)
981
949
start += m . index + name . length + 1
950
+ if ( classes . indexOf ( m [ 1 ] ) >= 0 ) {
951
+ var lbrace = prog . slice ( start ) . search ( '{' )
952
+ var rbrace = prog . slice ( start ) . search ( 'arguments' )
953
+ prog = prog . slice ( 0 , start + lbrace + 1 ) + prog . slice ( start + 'arguments' . length + rbrace + 1 )
954
+ }
982
955
continue
983
956
}
984
957
985
- // If not a user function nor pause/waitfor/rate/sleep/read_local_file/get_library, and it
986
- // is in the list of functions not to need await, don't prepend await to this function call:
987
- if ( fcts . indexOf ( name ) < 0 && noawait . indexOf ( name ) >= 0 ) {
988
- start += m . index + name . length + 1
989
- continue
990
- }
991
-
992
958
// find the beginning of ....f()
993
- var ptr = start + m . index + 1
959
+ ptr = start + m . index // +1
994
960
var brackets = 0
995
961
var parens = 0
996
962
while ( true ) {
@@ -1006,7 +972,7 @@ where compile() was called, in untrusted/run.js.
1006
972
if ( parens === 0 ) break
1007
973
parens ++
1008
974
}
1009
- if ( prog [ ptr ] == ' ' ) {
975
+ if ( prog [ ptr ] == ' ' || prog [ ptr ] == '=' || prog [ ptr ] == '\n' ) {
1010
976
if ( brackets === 0 && parens === 0 ) break
1011
977
}
1012
978
}
@@ -1027,7 +993,7 @@ where compile() was called, in untrusted/run.js.
1027
993
1028
994
// find the end of f(.....)
1029
995
var parens = 0
1030
- var ptr = start + m . index + name . length + 1
996
+ var ptr = start + m . index + name . length
1031
997
while ( true ) {
1032
998
if ( prog [ ptr ] == '(' ) parens ++
1033
999
else if ( prog [ ptr ] == ')' ) {
@@ -1039,8 +1005,16 @@ where compile() was called, in untrusted/run.js.
1039
1005
ptr ++
1040
1006
}
1041
1007
var pend = ptr // end of f(......)
1008
+
1009
+ // To my dismay, I found that "!" requires this special handling:
1010
+ var pst = pstart
1011
+ if ( prog [ pstart ] == '!' ) {
1012
+ if ( prepend == '(' ) prepend = '!('
1013
+ else prepend = ';!('
1014
+ pstart ++
1015
+ }
1042
1016
1043
- prog = prog . slice ( 0 , pstart ) + prepend + 'await ' + prog . slice ( pstart , pend ) + ')' + prog . slice ( pend )
1017
+ prog = prog . slice ( 0 , pst ) + prepend + 'await ' + prog . slice ( pstart , pend ) + ')' + prog . slice ( pend )
1044
1018
start += m . index + name . length + prepend . length + 6
1045
1019
} // end of adding "await " to function calls
1046
1020
@@ -1053,7 +1027,9 @@ where compile() was called, in untrusted/run.js.
1053
1027
while ( true ) {
1054
1028
var m = program . slice ( start ) . match ( kw )
1055
1029
if ( m == null ) break
1056
- if ( m [ 2 ] != 'this' || fcts . indexOf ( m [ 3 ] ) >= 0 ) {
1030
+ if ( m [ 2 ] != 'this' ) {
1031
+ program = program . slice ( 0 , start + m . index ) + 'await ' + program . slice ( start + m . index )
1032
+ } else if ( fcts . indexOf ( m [ 3 ] ) >= 0 ) {
1057
1033
program = program . slice ( 0 , start + m . index ) + 'await ' + program . slice ( start + m . index )
1058
1034
}
1059
1035
start += m . index + kw . length
@@ -1079,8 +1055,8 @@ where compile() was called, in untrusted/run.js.
1079
1055
// var p = program.split('\n')
1080
1056
// for (var i=0; i<p.length; i++) console.log(i, p[i])
1081
1057
// console.log('============================================================================')
1082
- // var i = program.search('scene = ')
1083
- // console.log(program.slice(i))
1058
+ var i = program . search ( 'scene = canvas() ' )
1059
+ console . log ( program . slice ( i ) )
1084
1060
//console.log(program)
1085
1061
return program
1086
1062
} // end of compile function
0 commit comments