1
1
Fork 0
mirror of https://github.com/boxgaming/qbjs.git synced 2024-09-20 04:24:45 +00:00

added support for single-line if/then and if/then/else statements. added support for screen-specific last x and y position for STEP operations

This commit is contained in:
boxgaming 2022-03-14 15:49:12 -05:00
parent 7fcb98c960
commit 2497e09a96
3 changed files with 155 additions and 164 deletions

57
qb.js
View file

@ -6,8 +6,6 @@ var QB = new function() {
var _fgColor = null; var _fgColor = null;
var _bgColor = null; var _bgColor = null;
var _colormap = []; var _colormap = [];
var _lastX = 0;
var _lastY = 0;
var _locX = 0; var _locX = 0;
var _locY = 0; var _locY = 0;
var _lastKey = null; var _lastKey = null;
@ -262,7 +260,7 @@ var QB = new function() {
canvas.height = iheight; canvas.height = iheight;
ctx = canvas.getContext("2d"); ctx = canvas.getContext("2d");
_images[_nextImageId] = { canvas: canvas, ctx: ctx }; _images[_nextImageId] = { canvas: canvas, ctx: ctx, lastX: 0, lastY: 0 };
var tmpId = _nextImageId; var tmpId = _nextImageId;
_nextImageId++; _nextImageId++;
return tmpId; return tmpId;
@ -601,6 +599,8 @@ var QB = new function() {
this.sub_Circle = function(step, x, y, radius, color, startAngle, endAngle, aspect) { this.sub_Circle = function(step, x, y, radius, color, startAngle, endAngle, aspect) {
// TODO: implement aspect parameter // TODO: implement aspect parameter
var screen = _images[_activeImage];
if (color == undefined) { if (color == undefined) {
color = _fgColor; color = _fgColor;
} }
@ -612,13 +612,13 @@ var QB = new function() {
if (endAngle == undefined) { endAngle = 2 * Math.PI; } if (endAngle == undefined) { endAngle = 2 * Math.PI; }
if (step) { if (step) {
x = _lastX + x; x = screen.lastX + x;
y = _lastY + y; y = screen.lastY + y;
} }
_lastX = x; screen.lastX = x;
_lastY = y; screen.lastY = y;
var ctx = _images[_activeImage].ctx; var ctx = screen.ctx;
ctx.strokeStyle = color.rgba(); ctx.strokeStyle = color.rgba();
ctx.beginPath(); ctx.beginPath();
ctx.arc(x, y, radius, startAngle, endAngle); ctx.arc(x, y, radius, startAngle, endAngle);
@ -626,6 +626,8 @@ var QB = new function() {
}; };
this.sub_Line = function(sstep, sx, sy, estep, ex, ey, color, style, pattern) { this.sub_Line = function(sstep, sx, sy, estep, ex, ey, color, style, pattern) {
var screen = _images[_activeImage];
if (color == undefined) { if (color == undefined) {
if (style == "BF") { if (style == "BF") {
color = _bgColor; color = _bgColor;
@ -639,24 +641,24 @@ var QB = new function() {
} }
if (sstep) { if (sstep) {
sx = _lastX + sx; sx = screen.lastX + sx;
sy = _lastY + sy; sy = screen.lastY + sy;
} }
if (sx == undefined) { if (sx == undefined) {
sx = _lastX; sx = screen.lastX;
sy = _lastY; sy = screen.lastY;
} }
_lastX = sx; screen.lastX = sx;
_lastY = sy; screen.lastY = sy;
if (estep) { if (estep) {
ex = _lastX + ex; ex = screen.lastX + ex;
ey = _lastY + ey; ey = screen.lastY + ey;
} }
_lastX = ex; screen.lastX = ex;
_lastY = ey; screen.lastY = ey;
var ctx = _images[_activeImage].ctx; var ctx = screen.ctx;
if (style == "B") { if (style == "B") {
ctx.strokeStyle = color.rgba(); ctx.strokeStyle = color.rgba();
@ -787,6 +789,8 @@ var QB = new function() {
} }
this.sub_PSet = function(sstep, x, y, color) { this.sub_PSet = function(sstep, x, y, color) {
var screen = _images[_activeImage];
if (color == undefined) { if (color == undefined) {
color = _fgColor; color = _fgColor;
} }
@ -794,13 +798,13 @@ var QB = new function() {
color = _color(color); color = _color(color);
} }
if (sstep) { if (sstep) {
x = _lastX + x; x = screen.lastX + x;
y = _lastY + y; y = screen.lastY + y;
} }
_lastX = x; screen.lastX = x;
_lastY = y; screen.lastY = y;
var ctx = _images[_activeImage].ctx; var ctx = screen.ctx;
ctx.fillStyle = color.rgba(); ctx.fillStyle = color.rgba();
ctx.beginPath(); ctx.beginPath();
ctx.fillRect(x, y, 1, 1); ctx.fillRect(x, y, 1, 1);
@ -847,13 +851,11 @@ var QB = new function() {
GX.sceneCreate(img.canvas.width, img.canvas.height); GX.sceneCreate(img.canvas.width, img.canvas.height);
} }
} }
_images[0] = { canvas: GX.canvas(), ctx: GX.ctx() }; _images[0] = { canvas: GX.canvas(), ctx: GX.ctx(), lastX: 0, lastY: 0 };
// initialize the graphics // initialize the graphics
_fgColor = _color(7); _fgColor = _color(7);
_bgColor = _color(0); _bgColor = _color(0);
_lastX = 0;
_lastY = 0;
_locX = 0; _locX = 0;
_locY = 0; _locY = 0;
@ -861,6 +863,7 @@ var QB = new function() {
_inputMode = false; _inputMode = false;
_inkeyBuffer = []; _inkeyBuffer = [];
_keyHitBuffer = []; _keyHitBuffer = [];
_keyDownMap = {};
}; };
this.func_Sgn = function(value) { this.func_Sgn = function(value) {

View file

@ -135,7 +135,7 @@ if (QB.halted()) { return; }
l = (QB.func__Trim(QB.arrayValue(lines, [ i]).value .text)); l = (QB.func__Trim(QB.arrayValue(lines, [ i]).value .text));
var parts = QB.initArray([{l:1,u:0}], ''); // STRING var parts = QB.initArray([{l:1,u:0}], ''); // STRING
var c = 0; // INTEGER var c = 0; // INTEGER
c = (await func_SLSplit( l, parts)); c = (await func_SLSplit( l, parts , True));
var js = ''; // STRING var js = ''; // STRING
js = ""; js = "";
var first = ''; // STRING var first = ''; // STRING
@ -386,7 +386,7 @@ var ConvertSub = null;
if ( m.name == "Line" ) { if ( m.name == "Line" ) {
var parts = QB.initArray([{l:1,u:0}], ''); // STRING var parts = QB.initArray([{l:1,u:0}], ''); // STRING
var plen = 0; // INTEGER var plen = 0; // INTEGER
plen = (await func_SLSplit( args, parts)); plen = (await func_SLSplit( args, parts , False));
if ( plen > 0) { if ( plen > 0) {
if ((QB.func_UCase(QB.arrayValue(parts, [ 1]).value)) == "INPUT" ) { if ((QB.func_UCase(QB.arrayValue(parts, [ 1]).value)) == "INPUT" ) {
m.name = "Line Input"; m.name = "Line Input";
@ -463,7 +463,11 @@ var ConvertLine = null;
endCord = (await func_ConvertExpression( endCord)); endCord = (await func_ConvertExpression( endCord));
theRest = (await func_ConvertExpression( theRest)); theRest = (await func_ConvertExpression( theRest));
theRest = (GXSTR.replace( theRest, " BF" , " " +(QB.func_Chr( 34)) +"BF" +(QB.func_Chr( 34)))); theRest = (GXSTR.replace( theRest, " BF" , " " +(QB.func_Chr( 34)) +"BF" +(QB.func_Chr( 34))));
theRest = (GXSTR.replace( theRest, " bf" , " " +(QB.func_Chr( 34)) +"BF" +(QB.func_Chr( 34))));
theRest = (GXSTR.replace( theRest, " bF" , " " +(QB.func_Chr( 34)) +"BF" +(QB.func_Chr( 34))));
theRest = (GXSTR.replace( theRest, " Bf" , " " +(QB.func_Chr( 34)) +"BF" +(QB.func_Chr( 34))));
theRest = (GXSTR.replace( theRest, " B" , " " +(QB.func_Chr( 34)) +"B" +(QB.func_Chr( 34)))); theRest = (GXSTR.replace( theRest, " B" , " " +(QB.func_Chr( 34)) +"B" +(QB.func_Chr( 34))));
theRest = (GXSTR.replace( theRest, " b" , " " +(QB.func_Chr( 34)) +"B" +(QB.func_Chr( 34))));
ConvertLine = sstep +", " + startCord +", " + estep +", " + endCord +", " + theRest; ConvertLine = sstep +", " + startCord +", " + estep +", " + endCord +", " + theRest;
return ConvertLine; return ConvertLine;
} }
@ -1255,15 +1259,63 @@ if (QB.halted()) { return; }
fline = (QB.func_Left( fline, i - 1)); fline = (QB.func_Left( fline, i - 1));
break; break;
} }
}
if ((QB.func__Trim( fline)) == "" ) {
return;
}
var word = ''; // STRING
var words = QB.initArray([{l:1,u:0}], ''); // STRING
var wcount = 0; // INTEGER
wcount = (await func_SLSplit( fline, words , False));
var ifIdx = 0; // INTEGER
var thenIdx = 0; // INTEGER
var elseIdx = 0; // INTEGER
for ( i= 1; i <= wcount; i= i + 1) { if (QB.halted()) { return; }
word = (QB.func_UCase(QB.arrayValue(words, [ i]).value));
if ( word == "IF" ) {
ifIdx = i;
} else if ( word == "THEN" ) {
thenIdx = i;
} else if ( word == "ELSE" ) {
elseIdx = i;
}
}
if ( thenIdx > 0 && thenIdx < wcount) {
await sub_AddLine( lineIndex, (await func_Join(words , 1, thenIdx, " ")));
if ( elseIdx > 0) {
await sub_AddSubLines( lineIndex, (await func_Join(words , thenIdx + 1, elseIdx - 1, " ")));
await sub_AddLine( lineIndex, "Else");
await sub_AddSubLines( lineIndex, (await func_Join(words , elseIdx + 1, -1, " ")));
} else {
await sub_AddSubLines( lineIndex, (await func_Join(words , thenIdx + 1, -1, " ")));
}
await sub_AddLine( lineIndex, "End If");
} else {
await sub_AddSubLines( lineIndex, fline);
}
}
async function sub_AddSubLines(lineIndex/*INTEGER*/,fline/*STRING*/) {
if (QB.halted()) { return; }
var quoteDepth = 0; // INTEGER
quoteDepth = 0;
var i = 0; // INTEGER
for ( i= 1; i <= (QB.func_Len( fline)); i= i + 1) { if (QB.halted()) { return; }
var c = ''; // STRING
c = (QB.func_Mid( fline, i, 1));
if ( c == (QB.func_Chr( 34)) ) {
if ( quoteDepth == 0) {
quoteDepth = 1;
} else {
quoteDepth = 0;
}
}
if ( quoteDepth == 0 && c == ":" ) { if ( quoteDepth == 0 && c == ":" ) {
await sub_AddLine( lineIndex, (QB.func_Left( fline, i - 1))); await sub_AddLine( lineIndex, (QB.func_Left( fline, i - 1)));
fline = (QB.func_Right( fline, (QB.func_Len( fline)) - i)); fline = (QB.func_Right( fline, (QB.func_Len( fline)) - i));
i = 0; i = 0;
} }
} }
if ((QB.func__Trim( fline)) != "" ) {
await sub_AddLine( lineIndex, fline); await sub_AddLine( lineIndex, fline);
}
} }
async function sub_FindMethods() { async function sub_FindMethods() {
if (QB.halted()) { return; } if (QB.halted()) { return; }
@ -1353,7 +1405,7 @@ var dpos = 0; // LONG
Split = arrpos; Split = arrpos;
return Split; return Split;
} }
async function func_SLSplit(sourceString/*STRING*/,results/*STRING*/) { async function func_SLSplit(sourceString/*STRING*/,results/*STRING*/,escapeStrings/*INTEGER*/) {
if (QB.halted()) { return; } if (QB.halted()) { return; }
var SLSplit = null; var SLSplit = null;
var cstr = ''; // STRING var cstr = ''; // STRING
@ -1374,7 +1426,7 @@ var dpos = 0; // LONG
if ( c == (QB.func_Chr( 34)) ) { if ( c == (QB.func_Chr( 34)) ) {
quoteMode = ! quoteMode; quoteMode = ! quoteMode;
result = result + c; result = result + c;
if (! quoteMode) { if (! quoteMode && escapeStrings) {
result = (GXSTR.replace( result, "\\" , "\\\\")); result = (GXSTR.replace( result, "\\" , "\\\\"));
} }
} else if ( c == " " ) { } else if ( c == " " ) {
@ -1622,29 +1674,7 @@ if (QB.halted()) { return; }
} }
async function sub_AddLine(lineIndex/*INTEGER*/,fline/*STRING*/) { async function sub_AddLine(lineIndex/*INTEGER*/,fline/*STRING*/) {
if (QB.halted()) { return; } if (QB.halted()) { return; }
var parts = QB.initArray([{l:1,u:0}], ''); // STRING
var c = 0; // INTEGER
c = (await func_Split( fline, " " , parts));
if ((QB.func_UCase(QB.arrayValue(parts, [ 1]).value)) == "IF" ) {
var thenIndex = 0; // INTEGER
thenIndex = 0;
var i = 0; // INTEGER
for ( i= 1; i <= c; i= i + 1) { if (QB.halted()) { return; }
if ((QB.func_UCase(QB.arrayValue(parts, [ i]).value)) == "THEN" ) {
thenIndex = i;
break;
}
}
if ( thenIndex != c) {
await sub___AddLine( lineIndex, (await func_Join(parts , 1, thenIndex, " ")));
await sub___AddLine( lineIndex, (await func_Join(parts , thenIndex + 1, c, " ")));
await sub___AddLine( lineIndex, "End If");
} else {
await sub___AddLine( lineIndex, fline); await sub___AddLine( lineIndex, fline);
}
} else {
await sub___AddLine( lineIndex, fline);
}
} }
async function sub___AddLine(lineIndex/*INTEGER*/,fline/*STRING*/) { async function sub___AddLine(lineIndex/*INTEGER*/,fline/*STRING*/) {
if (QB.halted()) { return; } if (QB.halted()) { return; }

View file

@ -58,24 +58,6 @@ Dim Shared As String currentMethod
Dim Shared As Integer programMethods Dim Shared As Integer programMethods
'Print ConvertCoordParam("(10, 20)-(15, 18)", True)
'Print ConvertCoordParam("(10, 20)", True)
'Print ConvertCoordParam("-(15, 18)", True)
'Print ConvertCoordParam("STEP(10, 20)-(15, 18)", True)
'Print ConvertCoordParam("(10, 20)-STEP(15, 18)", True)
'Print ConvertCoordParam("STEP(10, 20)-STEP(15, 18)", True)
'Print ConvertCoordParam("STEP(10, 20)", True)
'Print ConvertCoordParam("STEP(15, 18)", True)
'Print ConvertCoordParam("(10, 20)", False)
'Print ConvertCoordParam("STEP(10, 20)", False)
'Print ConvertPutImage(", myImage")
'Print ConvertPutImage(", myImage, 0")
'Print ConvertPutImage("(100,200), myImage")
'Print ConvertPutImage("(100,200), myImage, , (200, 300)")
'Print ConvertPutImage(", myImage, 0, _SMOOTH")
'End
' Only execute the conversion from the native version if we have been passed the ' Only execute the conversion from the native version if we have been passed the
' source file to convert on the command line ' source file to convert on the command line
If Command$ <> "" Then If Command$ <> "" Then
@ -198,7 +180,7 @@ Sub ConvertLines (firstLine As Integer, lastLine As Integer, functionName As Str
l = _Trim$(lines(i).text) l = _Trim$(lines(i).text)
ReDim As String parts(0) ReDim As String parts(0)
Dim c As Integer Dim c As Integer
c = SLSplit(l, parts()) c = SLSplit(l, parts(), True)
Dim js As String Dim js As String
js = "" js = ""
@ -237,7 +219,7 @@ Sub ConvertLines (firstLine As Integer, lastLine As Integer, functionName As Str
ElseIf first = "SELECT" Then ElseIf first = "SELECT" Then
caseVar = GenJSVar '"___c" + _Trim$(Str$(_Round(Rnd * 10000000))) caseVar = GenJSVar
js = "var " + caseVar + " = " + ConvertExpression(Join(parts(), 3, -1, " ")) + ";" + GX_CRLF js = "var " + caseVar + " = " + ConvertExpression(Join(parts(), 3, -1, " ")) + ";" + GX_CRLF
js = js + "switch (" + caseVar + ") {" js = js + "switch (" + caseVar + ") {"
indent = 1 indent = 1
@ -250,7 +232,6 @@ Sub ConvertLines (firstLine As Integer, lastLine As Integer, functionName As Str
ElseIf UCase$(parts(2)) = "IS" Then ElseIf UCase$(parts(2)) = "IS" Then
js = js + "case " + caseVar + " " + ConvertExpression(Join(parts(), 3, -1, " ")) + ":" js = js + "case " + caseVar + " " + ConvertExpression(Join(parts(), 3, -1, " ")) + ":"
Else Else
'js = js + "case " + ConvertExpression(parts(2)) + ":"
ReDim As String caseParts(0) ReDim As String caseParts(0)
Dim cscount As Integer Dim cscount As Integer
cscount = ListSplit(Join(parts(), 2, -1, " "), caseParts()) cscount = ListSplit(Join(parts(), 2, -1, " "), caseParts())
@ -289,7 +270,6 @@ Sub ConvertLines (firstLine As Integer, lastLine As Integer, functionName As Str
Dim uval As String Dim uval As String
uval = ConvertExpression(Join(parts(), toIdx + 1, stepIdx - 1, " ")) uval = ConvertExpression(Join(parts(), toIdx + 1, stepIdx - 1, " "))
'If Val(fstep) < 0 Then fcond = " >= "
If Left$(_Trim$(fstep), 1) = "-" Then fcond = " >= " If Left$(_Trim$(fstep), 1) = "-" Then fcond = " >= "
js = "for (" + fvar + "=" + sval + "; " + fvar + fcond + uval + "; " + fvar + "=" + fvar + " + " + fstep + ") {" js = "for (" + fvar + "=" + sval + "; " + fvar + fcond + uval + "; " + fvar + "=" + fvar + " + " + fstep + ") {"
@ -323,9 +303,6 @@ Sub ConvertLines (firstLine As Integer, lastLine As Integer, functionName As Str
ElseIf first = "END" Then ElseIf first = "END" Then
If UBound(parts) = 1 Then If UBound(parts) = 1 Then
js = "QB.halt(); return;" js = "QB.halt(); return;"
'js = "// END"
'AddWarning i, "End is not currently supported in this context, ignoring."
Else Else
If UCase$(parts(2)) = "SELECT" Then js = "break;" If UCase$(parts(2)) = "SELECT" Then js = "break;"
js = js + "}" js = js + "}"
@ -425,7 +402,6 @@ Sub ConvertLines (firstLine As Integer, lastLine As Integer, functionName As Str
subargs = Mid$(subline, Len(subname) + 2, Len(subline) - Len(subname) - 2) subargs = Mid$(subline, Len(subname) + 2, Len(subline) - Len(subname) - 2)
js = ConvertSub(m, subargs) js = ConvertSub(m, subargs)
Else Else
'js = "// " + l
AddWarning i, "Missing Sub [" + subname + "], ignoring Call command" AddWarning i, "Missing Sub [" + subname + "], ignoring Call command"
End If End If
@ -464,7 +440,6 @@ Sub ConvertLines (firstLine As Integer, lastLine As Integer, functionName As Str
End If End If
If (indent < 0) Then totalIndent = totalIndent + indent If (indent < 0) Then totalIndent = totalIndent + indent
'*Print GXSTR_LPad("", " ", (totalIndent + tempIndent) * 3) + js
AddJSLine i, GXSTR_LPad("", " ", (totalIndent + tempIndent) * 3) + js AddJSLine i, GXSTR_LPad("", " ", (totalIndent + tempIndent) * 3) + js
If (indent > 0) Then totalIndent = totalIndent + indent If (indent > 0) Then totalIndent = totalIndent + indent
@ -483,7 +458,7 @@ Function ConvertSub$ (m As Method, args As String)
If m.name = "Line" Then If m.name = "Line" Then
Dim parts(0) As String Dim parts(0) As String
Dim plen As Integer Dim plen As Integer
plen = SLSplit(args, parts()) plen = SLSplit(args, parts(), False)
If plen > 0 Then If plen > 0 Then
If UCase$(parts(1)) = "INPUT" Then If UCase$(parts(1)) = "INPUT" Then
m.name = "Line Input" m.name = "Line Input"
@ -573,8 +548,13 @@ Function ConvertLine$ (args As String)
endCord = ConvertExpression(endCord) endCord = ConvertExpression(endCord)
theRest = ConvertExpression(theRest) theRest = ConvertExpression(theRest)
' TODO: fix this nonsense
theRest = GXSTR_Replace(theRest, " BF", " " + Chr$(34) + "BF" + Chr$(34)) theRest = GXSTR_Replace(theRest, " BF", " " + Chr$(34) + "BF" + Chr$(34))
theRest = GXSTR_Replace(theRest, " bf", " " + Chr$(34) + "BF" + Chr$(34))
theRest = GXSTR_Replace(theRest, " bF", " " + Chr$(34) + "BF" + Chr$(34))
theRest = GXSTR_Replace(theRest, " Bf", " " + Chr$(34) + "BF" + Chr$(34))
theRest = GXSTR_Replace(theRest, " B", " " + Chr$(34) + "B" + Chr$(34)) theRest = GXSTR_Replace(theRest, " B", " " + Chr$(34) + "B" + Chr$(34))
theRest = GXSTR_Replace(theRest, " b", " " + Chr$(34) + "B" + Chr$(34))
ConvertLine = sstep + ", " + startCord + ", " + estep + ", " + endCord + ", " + theRest ConvertLine = sstep + ", " + startCord + ", " + estep + ", " + endCord + ", " + theRest
End Function End Function
@ -621,20 +601,14 @@ Function ConvertCoordParam$ (param As String, hasEndCoord As Integer)
sstep = "false" sstep = "false"
estep = "false" estep = "false"
'If hasEndCoord Then
idx = FindParamChar(param, "-") idx = FindParamChar(param, "-")
If idx = -1 Then If idx = -1 Then
'endCoord = param
startCoord = param startCoord = param
endCoord = "" endCoord = ""
Else Else
startCoord = Left$(param, idx - 1) startCoord = Left$(param, idx - 1)
endCoord = Right$(param, Len(param) - idx) endCoord = Right$(param, Len(param) - idx)
End If End If
'Else
' startCoord = param
' endCoord = ""
'End If
If UCase$(_Trim$(Left$(startCoord, 4))) = "STEP" Then If UCase$(_Trim$(Left$(startCoord, 4))) = "STEP" Then
sstep = "true" sstep = "true"
@ -864,6 +838,7 @@ Function DeclareVar$ (parts() As String)
If asIdx = 2 Or _ If asIdx = 2 Or _
(asIdx = 3 And (isGlobal Or preserve = "true")) Or _ (asIdx = 3 And (isGlobal Or preserve = "true")) Or _
(asIdx = 4 And isGlobal And preserve = "true") Then (asIdx = 4 And isGlobal And preserve = "true") Then
' Handle Dim As syntax ' Handle Dim As syntax
bvar.type = UCase$(parts(asIdx + 1)) bvar.type = UCase$(parts(asIdx + 1))
Dim nextIdx As Integer Dim nextIdx As Integer
@ -1150,12 +1125,10 @@ Function ConvertExpression$ (ex As String)
Else Else
' This is the case where a dimension is specified in order to retrieve or set a value in the array ' This is the case where a dimension is specified in order to retrieve or set a value in the array
js = js + fneg + "QB.arrayValue(" + bvar.jsname + ", [" + ConvertExpression(ex2) + "]).value" js = js + fneg + "QB.arrayValue(" + bvar.jsname + ", [" + ConvertExpression(ex2) + "]).value"
'If bvar.typeId < 1 Then js = js + ".value"
End If End If
ElseIf FindMethod(word, m, "FUNCTION") Then ElseIf FindMethod(word, m, "FUNCTION") Then
js = js + fneg + "(" + CallMethod(m) + "(" + ConvertExpression(ex2) + "))" js = js + fneg + "(" + CallMethod(m) + "(" + ConvertExpression(ex2) + "))"
Else Else
'If _Trim$(word) <> "" Then AddJSLine 0, "//// MISSING FUNCTION? [" + word + "]" '*Print "//// MISSING FUNCTION? [" + word + "]"
If _Trim$(word) <> "" Then AddWarning i, "Missing function or array [" + word + "]" If _Trim$(word) <> "" Then AddWarning i, "Missing function or array [" + word + "]"
' nested condition ' nested condition
js = js + fneg + "(" + ConvertExpression(ex2) + ")" js = js + fneg + "(" + ConvertExpression(ex2) + ")"
@ -1186,9 +1159,6 @@ Function FindVariable (varname As String, bvar As Variable, isArray As Integer)
For i = 1 To UBound(localVars) For i = 1 To UBound(localVars)
If localVars(i).isArray = isArray And UCase$(localVars(i).name) = fvarname Then If localVars(i).isArray = isArray And UCase$(localVars(i).name) = fvarname Then
found = True found = True
'bvar = localVars(i)
' TODO: This is a work around for the pass by reference issue.
' Once corrected, the code above can be used instead
bvar.type = localVars(i).type bvar.type = localVars(i).type
bvar.name = localVars(i).name bvar.name = localVars(i).name
bvar.jsname = localVars(i).jsname bvar.jsname = localVars(i).jsname
@ -1203,9 +1173,6 @@ Function FindVariable (varname As String, bvar As Variable, isArray As Integer)
For i = 1 To UBound(globalVars) For i = 1 To UBound(globalVars)
If globalVars(i).isArray = isArray And UCase$(globalVars(i).name) = fvarname Then If globalVars(i).isArray = isArray And UCase$(globalVars(i).name) = fvarname Then
found = True found = True
'bvar = globalVars(i)
' TODO: This is a work around for the pass by reference issue.
' Once corrected, the code above can be used instead
bvar.type = globalVars(i).type bvar.type = globalVars(i).type
bvar.name = globalVars(i).name bvar.name = globalVars(i).name
bvar.jsname = globalVars(i).jsname bvar.jsname = globalVars(i).jsname
@ -1227,9 +1194,6 @@ Function FindMethod (mname As String, m As Method, t As String)
For i = 1 To UBound(methods) For i = 1 To UBound(methods)
If methods(i).uname = _Trim$(UCase$(RemoveSuffix(mname))) And methods(i).type = t Then If methods(i).uname = _Trim$(UCase$(RemoveSuffix(mname))) And methods(i).type = t Then
found = True found = True
'm = methods(i)
' TODO: This is a work around for the pass by reference issue.
' Once corrected, the code above can be used instead
m.line = methods(i).line m.line = methods(i).line
m.type = methods(i).type m.type = methods(i).type
m.returnType = methods(i).returnType m.returnType = methods(i).returnType
@ -1360,6 +1324,7 @@ Sub ReadLinesFromText (sourceText As String)
End Sub End Sub
Sub ReadLine (lineIndex As Integer, fline As String) Sub ReadLine (lineIndex As Integer, fline As String)
' Step 1: Remove any comments from the line
Dim quoteDepth As Integer Dim quoteDepth As Integer
quoteDepth = 0 quoteDepth = 0
Dim i As Integer Dim i As Integer
@ -1377,6 +1342,56 @@ Sub ReadLine (lineIndex As Integer, fline As String)
fline = Left$(fline, i - 1) fline = Left$(fline, i - 1)
Exit For Exit For
End If End If
Next i
If _Trim$(fline) = "" Then Exit Sub
' Step 2: Determine whether this line contains a single line if/then or if/then/else statement
Dim word As String
Dim words(0) As String
Dim wcount As Integer
wcount = SLSplit(fline, words(), False)
Dim As Integer ifIdx, thenIdx, elseIdx
For i = 1 To wcount
word = UCase$(words(i))
If word = "IF" Then
ifIdx = i
ElseIf word = "THEN" Then
thenIdx = i
ElseIf word = "ELSE" Then
elseIdx = i
End If
Next i
If thenIdx > 0 And thenIdx < wcount Then
AddLine lineIndex, Join(words(), 1, thenIdx, " ")
If elseIdx > 0 Then
AddSubLines lineIndex, Join(words(), thenIdx + 1, elseIdx - 1, " ")
AddLine lineIndex, "Else"
AddSubLines lineIndex, Join(words(), elseIdx + 1, -1, " ")
Else
AddSubLines lineIndex, Join(words(), thenIdx + 1, -1, " ")
End If
AddLine lineIndex, "End If"
Else
AddSubLines lineIndex, fline
End If
End Sub
Sub AddSubLines (lineIndex As Integer, fline As String)
Dim quoteDepth As Integer
quoteDepth = 0
Dim i As Integer
For i = 1 To Len(fline)
Dim c As String
c = Mid$(fline, i, 1)
If c = Chr$(34) Then
If quoteDepth = 0 Then
quoteDepth = 1
Else
quoteDepth = 0
End If
End If
If quoteDepth = 0 And c = ":" Then If quoteDepth = 0 And c = ":" Then
AddLine lineIndex, Left$(fline, i - 1) AddLine lineIndex, Left$(fline, i - 1)
fline = Right$(fline, Len(fline) - i) fline = Right$(fline, Len(fline) - i)
@ -1384,12 +1399,10 @@ Sub ReadLine (lineIndex As Integer, fline As String)
End If End If
Next i Next i
' If once we have removed the comments the line is empty do not add it
If _Trim$(fline) <> "" Then
AddLine lineIndex, fline AddLine lineIndex, fline
End If
End Sub End Sub
Sub FindMethods Sub FindMethods
Dim i As Integer Dim i As Integer
Dim pcount As Integer Dim pcount As Integer
@ -1415,32 +1428,24 @@ Sub FindMethods
For a = 3 To UBound(parts) For a = 3 To UBound(parts)
args = args + parts(a) + " " args = args + parts(a) + " "
Next a Next a
'Print "---> " + args
'args = _Trim$(GXSTR_Replace(GXSTR_Replace(args, "(", ""), ")", ""))
args = Mid$(_Trim$(args), 2, Len(_Trim$(args)) - 2) args = Mid$(_Trim$(args), 2, Len(_Trim$(args)) - 2)
'Print "---< " + args
ReDim As String arga(0) ReDim As String arga(0)
'm.argc = GXSTR_Split(args, ",", arga())
m.argc = ListSplit(args, arga()) m.argc = ListSplit(args, arga())
args = "" args = ""
For a = 1 To m.argc For a = 1 To m.argc
'Dim arg As String
ReDim As String aparts(0) ReDim As String aparts(0)
Dim apcount As Integer Dim apcount As Integer
Dim argname As String Dim argname As String
Dim isArray As String: isArray = "false" Dim isArray As String: isArray = "false"
apcount = Split(arga(a), " ", aparts()) apcount = Split(arga(a), " ", aparts())
argname = aparts(1) argname = aparts(1)
'Print "---: " + argname
If EndsWith(argname, "()") Then If EndsWith(argname, "()") Then
isArray = "true" isArray = "true"
argname = Left$(argname, Len(argname) - 2) argname = Left$(argname, Len(argname) - 2)
End If End If
If apcount = 3 Then If apcount = 3 Then
'args = args + aparts(1) + ":" + UCase$(aparts(3))
args = args + argname + ":" + UCase$(aparts(3)) + ":" + isArray args = args + argname + ":" + UCase$(aparts(3)) + ":" + isArray
Else Else
'args = args + aparts(1) + ":" + DataTypeFromName(aparts(1))
args = args + argname + ":" + DataTypeFromName(aparts(1)) + ":" + isArray args = args + argname + ":" + DataTypeFromName(aparts(1)) + ":" + isArray
End If End If
If a <> m.argc Then If a <> m.argc Then
@ -1493,7 +1498,7 @@ End Function
' String literal-aware split ' String literal-aware split
Function SLSplit (sourceString As String, results() As String) Function SLSplit (sourceString As String, results() As String, escapeStrings As Integer)
Dim cstr As String Dim cstr As String
Dim As Long p, curpos, arrpos, dpos Dim As Long p, curpos, arrpos, dpos
@ -1516,7 +1521,7 @@ Function SLSplit (sourceString As String, results() As String)
' This is not the most intuitive place for this... ' This is not the most intuitive place for this...
' If we find a string then escape any backslashes ' If we find a string then escape any backslashes
If Not quoteMode Then If Not quoteMode And escapeStrings Then
result = GXSTR_Replace(result, "\", "\\") result = GXSTR_Replace(result, "\", "\\")
End If End If
@ -1573,11 +1578,6 @@ Function SLSplit2 (sourceString As String, results() As String)
quoteMode = Not quoteMode quoteMode = Not quoteMode
result = result + c result = result + c
' This is not the most intuitive place for this...
' If we find a string then escape any backslashes
'If Not quoteMode Then
' result = GXSTR_Replace(result, "\", "\\")
'End If
ElseIf quoteMode Then ElseIf quoteMode Then
result = result + c result = result + c
@ -1593,9 +1593,6 @@ Function SLSplit2 (sourceString As String, results() As String)
result = result + c result = result + c
ElseIf c = " " Then ElseIf c = " " Then
'If quoteMode Then
' result = result + c
If lastChar = " " Then If lastChar = " " Then
' extra space, move along ' extra space, move along
@ -1816,41 +1813,12 @@ End Sub
Sub AddLine (lineIndex As Integer, fline As String) Sub AddLine (lineIndex As Integer, fline As String)
' check for single line if statements
Dim parts(0) As String
Dim c As Integer
c = Split(fline, " ", parts())
If UCase$(parts(1)) = "IF" Then
Dim thenIndex As Integer
thenIndex = 0
Dim i As Integer
For i = 1 To c
If UCase$(parts(i)) = "THEN" Then
thenIndex = i
Exit For
End If
Next i
If thenIndex <> c Then
__AddLine lineIndex, Join(parts(), 1, thenIndex, " ")
__AddLine lineIndex, Join(parts(), thenIndex + 1, c, " ")
__AddLine lineIndex, "End If"
Else
__AddLine lineIndex, fline __AddLine lineIndex, fline
End If
Else
__AddLine lineIndex, fline
End If
End Sub End Sub
Sub __AddLine (lineIndex As Integer, fline As String) Sub __AddLine (lineIndex As Integer, fline As String)
Dim lcount As Integer: lcount = UBound(lines) + 1 Dim lcount As Integer: lcount = UBound(lines) + 1
ReDim _Preserve As CodeLine lines(lcount) ReDim _Preserve As CodeLine lines(lcount)
'Dim cline As CodeLine
'cline.line = lineIndex
'cline.text = fline
'lines(lcount) = cline
lines(lcount).line = lineIndex lines(lcount).line = lineIndex
lines(lcount).text = fline lines(lcount).text = fline
End Sub End Sub
@ -1858,10 +1826,6 @@ End Sub
Sub AddJSLine (sourceLine As Integer, jsline As String) Sub AddJSLine (sourceLine As Integer, jsline As String)
Dim lcount As Integer: lcount = UBound(jsLines) + 1 Dim lcount As Integer: lcount = UBound(jsLines) + 1
ReDim _Preserve As CodeLine jsLines(lcount) ReDim _Preserve As CodeLine jsLines(lcount)
'Dim cline As CodeLine
'cline.line = sourceLine
'cline.text = jsline
'jsLines(lcount) = cline
jsLines(lcount).line = sourceLine jsLines(lcount).line = sourceLine
jsLines(lcount).text = jsline jsLines(lcount).text = jsline
End Sub End Sub
@ -1948,8 +1912,6 @@ End Sub
Sub AddSystemType (tname As String, args As String) Sub AddSystemType (tname As String, args As String)
Dim t As QBType Dim t As QBType
t.name = tname t.name = tname
't.argc = argc
't.args = args
AddType t AddType t
Dim typeId As Integer Dim typeId As Integer
typeId = UBound(types) typeId = UBound(types)
@ -2091,10 +2053,6 @@ Function MethodJS$ (m As Method, prefix As String)
End If End If
Next i Next i
'If m.name = "_Limit" Or m.name = "_Delay" Or m.name = "Sleep" Or m.name = "Input" Or m.name = "Print" Or m.name = "Fetch" Then
'jsname = "await " + jsname
'End If
MethodJS = jsname MethodJS = jsname
End Function End Function