Skip to content

Commit edd13d3

Browse files
committed
Enable pysort key
1 parent 7eb4a0a commit edd13d3

File tree

6 files changed

+22
-7
lines changed

6 files changed

+22
-7
lines changed

GlowScriptOffline/glowscript_libraries/RScompiler.3.2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GlowScriptOffline/glowscript_libraries/compiler.3.2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GlowScriptOffline3.2.zip

119 Bytes
Binary file not shown.

lib/compiling/GScompiler.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,14 @@ where compile() was called, in untrusted/run.js.
203203
const asyncpatt1 = new RegExp('\\Wfunction\\s*(\\w+)\\s*\\(') // find "function f(...."
204204
const awaitpatt = new RegExp('\\W([ρσ\\w]+)[ ]*\\(') // find a function call
205205
const filterpatt = new RegExp('\\Wfilter\\s*\\(\\s*(\\w+)+\\s*,') // find name of filter function
206+
const pysortkey = new RegExp('\\.pysort\\s*\\(\\s*key\\s*=\\s*(\\w+)\\s*\\)')
206207

207208
// fcts is a list of GlowScript functions that need "await" and of all user functions that are not
208209
// classes, instances of classes, or methods of classes:
209210
var vpfcts = ['pause', 'waitfor', 'capture'] // These are preceded by "scene."
210211
var vpwaits = ['rate', 'sleep', 'get_library', 'read_local_file']
211212
var fcts = []
213+
var nonasyncfcts = [] // a list of functions that should NOT be labeled async
212214

213215
// If ...name(... is encountered inside a string during the first pass, it is changed
214216
// to ...name+string_insert+.... to prevent finding this apparent function or function call,
@@ -629,6 +631,10 @@ where compile() was called, in untrusted/run.js.
629631
}
630632
}
631633

634+
// In the form a.pysort(key=k), k must not be prepended with async, nor the call to k be prepended with await
635+
m = line.match(pysortkey)
636+
if (m !== null) nonasyncfcts.push(m[1])
637+
632638
start = 0
633639
const bindmethodpatt = new RegExp('(\\w+)\\.(\\w+)\\s*')
634640
while (true) { // convert all classinstance.classmethod without parens to classinstance.classmethod.bind(classinstance)
@@ -923,7 +929,8 @@ where compile() was called, in untrusted/run.js.
923929
// console.log('classinstances', classinstances)
924930
// console.log('classmethods', classmethods)
925931

926-
// Prepend 'async ' to all user functions
932+
// Prepend 'async ' to all user functions (but not if in nonasyncfcts)
933+
927934
let filter_functions = []
928935
while (true) { // locate Python filter(name, list)
929936
m = prog.slice(start).match(filterpatt)
@@ -933,6 +940,7 @@ where compile() was called, in untrusted/run.js.
933940
no_await.push(m[1]) // add this user function to the no_await list
934941
start += m.index+m[0].length
935942
}
943+
936944
start = initialstart
937945
while (true) {
938946
// const asyncpatt1 = new RegExp('\\Wfunction\\s*(\\w+)\\s*\\(') // find "function f(...."
@@ -946,12 +954,19 @@ where compile() was called, in untrusted/run.js.
946954
start += m[0].length
947955
continue
948956
}
957+
if (nonasyncfcts.indexOf(name) >= 0) {
958+
no_await.push(name)
959+
start += m[0].length
960+
continue
961+
}
949962
if (filter_functions.indexOf(name) >= 0) start += m[0].length
950963
else {
951964
prog = prog.slice(0,start+m.index+1)+'async function '+name+string_insert+prog.slice(start+m.index+m[0].length)
952965
start += m.index+'async function '.length+m[1].length+string_insert.length+1
953966
}
967+
954968
}
969+
955970
// ===================== End of prepending 'async' to user functions =====================
956971
// Next, prepend "await " to calls to user functions and the GlowScript async functions (all user functions and class methods are marked async).
957972

@@ -1245,8 +1260,8 @@ where compile() was called, in untrusted/run.js.
12451260
// console.log('classmethods', classmethods)
12461261
// console.log('============================================================================')
12471262
// let i = program.search('"2";')
1248-
// let i = program.search('async function __main__')
1249-
// console.log(program.slice(i))
1263+
let i = program.search('async function __main__')
1264+
console.log(program.slice(i))
12501265
// console.log(program)
12511266
return program
12521267
} // end of compile function

package/RScompiler.3.2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/compiler.3.2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)