Skip to content

Commit d6c75f6

Browse files
committed
More fixes to compilation
1 parent 99eaaf7 commit d6c75f6

File tree

10 files changed

+55
-58
lines changed

10 files changed

+55
-58
lines changed

GlowScriptOffline/glowscript_libraries/RScompiler.2.9.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/RSrun.2.9.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/glow.2.9.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.

GlowScriptOffline2.9.zip

6.25 MB
Binary file not shown.

lib/compiling/GScompiler.js

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,6 @@ where compile() was called, in untrusted/run.js.
692692

693693
if (!foundfct) {
694694
// 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
696695
while (true) {
697696
var m = line.slice(start).match(awaitpatt)
698697
if (m !== null) {
@@ -877,8 +876,7 @@ where compile() was called, in untrusted/run.js.
877876
arr += " Array.prototype['*']=function(r) {return __array_times_number(this, r)}\n" // multiplying Python list times number
878877
if (VPython_names.length <= 0) arr += vars
879878
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
882880

883881
} else { // JavaScript
884882
var prog = 'function __main__() {\n"use strict";\n'
@@ -924,7 +922,6 @@ where compile() was called, in untrusted/run.js.
924922

925923
// Prepend 'async ' to all user functions
926924
while (true) {
927-
// const asyncpatt1 = new RegExp('\\Wfunction\\s*(\\w+)\\s*\\(') // find "function f(...."
928925
if (options.lang != 'javascript') m = prog.slice(start).match(asyncpatt1)
929926
else m = prog.slice(start).match(jsasyncpatt1)
930927
if (m === null) break
@@ -942,55 +939,24 @@ where compile() was called, in untrusted/run.js.
942939

943940
// Prepend "await " to calls to user functions and the GlowScript async functions (all user functions are marked async).
944941
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
961943
while (true) {
962-
m = prog.slice(start).match(awaitpatt)
963-
var period = false
944+
m = prog.slice(start).match(awaitpatt2)
964945
if (m === null) break
965-
period = (prog[start+m.index] == '.')
966946
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)
981949
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+
}
982955
continue
983956
}
984957

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-
992958
// find the beginning of ....f()
993-
var ptr = start+m.index+1
959+
ptr = start+m.index //+1
994960
var brackets = 0
995961
var parens = 0
996962
while (true) {
@@ -1006,7 +972,7 @@ where compile() was called, in untrusted/run.js.
1006972
if (parens === 0) break
1007973
parens++
1008974
}
1009-
if (prog[ptr] == ' ') {
975+
if (prog[ptr] == ' ' || prog[ptr] == '=' || prog[ptr] == '\n') {
1010976
if (brackets === 0 && parens === 0) break
1011977
}
1012978
}
@@ -1027,7 +993,7 @@ where compile() was called, in untrusted/run.js.
1027993

1028994
// find the end of f(.....)
1029995
var parens = 0
1030-
var ptr = start+m.index+name.length+1
996+
var ptr = start+m.index+name.length
1031997
while (true) {
1032998
if (prog[ptr] == '(') parens++
1033999
else if (prog[ptr] == ')') {
@@ -1039,8 +1005,16 @@ where compile() was called, in untrusted/run.js.
10391005
ptr++
10401006
}
10411007
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+
}
10421016

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)
10441018
start += m.index+name.length+prepend.length+6
10451019
} // end of adding "await " to function calls
10461020

@@ -1053,7 +1027,9 @@ where compile() was called, in untrusted/run.js.
10531027
while (true) {
10541028
var m = program.slice(start).match(kw)
10551029
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) {
10571033
program = program.slice(0,start+m.index)+'await '+program.slice(start+m.index)
10581034
}
10591035
start += m.index+kw.length
@@ -1079,8 +1055,8 @@ where compile() was called, in untrusted/run.js.
10791055
// var p = program.split('\n')
10801056
// for (var i=0; i<p.length; i++) console.log(i, p[i])
10811057
// 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))
10841060
//console.log(program)
10851061
return program
10861062
} // end of compile function

lib/glow/canvas.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@
561561

562562
__change: function() { // Called when attributeVectors are changed, axis/center/up
563563
if (this.__lastevent !== null && this.hasmouse) this.mouse.__update(this.__lastevent)
564-
this.__overlay_objects.__changed = true
564+
if (this.__overlay_objects) this.__overlay_objects.__changed = true
565565
},
566566

567567
// Changes to center/axis/up call this.__change():

lib/glow/vectors.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,10 +713,31 @@
713713
if (axis === undefined) axis = new vec(0,0,1)
714714
return v.rotate({angle:angle, axis:axis})
715715
}
716+
716717
function GS_power(x,n) { // Preprocessing converts Math.pow => GS_power
717718
if (x instanceof vec) throw new Error("Cannot raise a vector to a power.")
718719
return Math.pow(x,n)
719720
}
721+
722+
// Because RapydScript-NG compiles to the JavaScript string.replace function, which
723+
// replaces just one instance rather than multiple instances as in standard Python,
724+
// here we implement string.GSrep that acts like standard Python and we
725+
// replace all instances of ".replace" (in user code) with ".GSrep".
726+
String.prototype.GSrep = function(t,r, n) {
727+
if (n === undefined) n = 1000000
728+
var s = this
729+
if (t === r) return s
730+
var tlong = t.length
731+
var i = 0
732+
while (true) {
733+
if (i >= n) return s
734+
i++
735+
var loc = s.search(t)
736+
if (loc < 0) return s
737+
s = s.slice(0,loc) + r + s.slice(loc+tlong)
738+
}
739+
}
740+
720741
var exports = {
721742
mag: mag,
722743
mag2: mag2,

package/RScompiler.2.9.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.2.9.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/glow.2.9.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)