1
1
Fork 0
mirror of https://github.com/boxgaming/qbjs.git synced 2024-09-19 20:14:58 +00:00
This commit is contained in:
boxgaming 2022-06-17 16:08:53 -05:00
commit 22a21b8b3d
4 changed files with 153 additions and 37 deletions

View file

@ -35,12 +35,12 @@ CodeMirror.defineMode("qbjs", function(conf, parserConf) {
// TODO: adjust for QB
var atomWords = ['true', 'false', 'nothing', 'empty', 'null'];
var builtinFuncsWords = ['_acos', '_acosh', '_alpha', '_alpha32', '_asin', '_asinh', '_atan2', '_atanh', '_autodisplay', '_blue', '_blue32', '_continue', '_copyimage', '_cosh', '_coth', '_csch', '_d2r', '_delay', '_dest',
'_display', '_fontwidth', '_freeimage', '_fullscreen', '_green', '_green32', '_height', '_instrrev', '_limit', '_keyclear', '_keydown',
var builtinFuncsWords = ['_acos', '_acosh', '_alpha', '_alpha32', '_asin', '_asinh', '_atan2', '_atanh', '_autodisplay', '_blue', '_blue32', '_ceil', '_continue', '_copyimage', '_cosh', '_coth', '_csch', '_d2g', '_d2r', '_delay', '_dest',
'_display', '_fontwidth', '_freeimage', '_fullscreen', '_g2d', '_g2r', '_green', '_green32', '_height', '_hypot', '_instrrev', '_limit', '_keyclear', '_keydown',
'_keyhit', '_loadimage', '_mousebutton', '_mouseinput', '_mousex', '_mousey', '_newimage', '_pi', '_printstring', '_printwidth',
'_putimage', '_r2d', '_red', '_red32', '_resize', '_resizewidth', '_resizeheight', '_rgb', '_rgba', '_rgb32', '_rgba32', '_round',
'_screenexists', '_sech', '_sinh', '_source', '_sndclose', '_sndopen', '_sndplay', '_sndloop', '_sndpause', '_sndstop', '_sndvol', '_tanh',
'_title', '_trim', '_width', 'abs', 'asc', 'atn', 'beep', 'chr', 'circle', 'cls', 'color', 'command', 'cos', 'cvi', 'cvl', 'data', 'draw', 'exp',
'_putimage', '_r2d', '_r2g', '_readbit', '_red', '_red32', '_resetbit', '_resize', '_resizewidth', '_resizeheight', '_rgb', '_rgba', '_rgb32', '_rgba32', '_round',
'_screenexists', '_sech', '_setbit', '_shl', '_shr', '_sinh', '_source', '_sndclose', '_sndopen', '_sndplay', '_sndloop', '_sndpause', '_sndstop', '_sndvol', '_strcmp', '_stricmp', '_tanh',
'_title', '_trim', '_togglebit', '_width', 'abs', 'asc', 'atn', 'beep', 'chr', 'cdbl', 'cint', 'clng', 'csng', 'circle', 'cls', 'color', 'command', 'cos', 'cvi', 'cvl', 'data', 'date', 'draw', 'exp',
'fix', 'hex', 'input', 'inkey', 'instr', 'int', 'lbound', 'left', 'lcase', 'len', 'line', 'locate', 'log', 'ltrim', 'mid', 'mki', 'mkl',
'oct', 'paint', 'point', 'preset', 'print', 'pset', 'right', 'rtrim', 'randomize', 'read', 'restore', 'rnd', 'screen', 'shared', 'sgn', 'sin', 'sleep', 'space', 'sqr',
'str', 'swap', 'tan', 'time', 'timer', 'ubound', 'ucase', 'val', 'varptr', 'window',

110
qb.js
View file

@ -202,6 +202,10 @@ var QB = new function() {
return _color(rgb).b;
};
this.func__Ceil = function(x) {
return Math.ceil(x);
};
this.func__CopyImage = function(srcImageId) {
var srcCanvas = _images[srcImageId].canvas;
var destImageId = QB.func__NewImage(srcCanvas.width, srcCanvas.height);
@ -227,6 +231,10 @@ var QB = new function() {
return x*Math.PI/180;
};
this.func__D2G = function(x) {
return (x * 10/9);
};
this.sub__Delay = async function(seconds) {
await GX.sleep(seconds*1000);
};
@ -274,6 +282,14 @@ var QB = new function() {
// TODO: implement smooth option (maybe) - the canvas does smooth scaling by default
}
this.func__G2D = function(x) {
return (x * 9/10);
};
this.func__G2R = function(x) {
return (x * 9/10) * Math.PI/180;
};
this.func__Green = function(rgb, imageHandle) {
// TODO: implement corresponding logic when an image handle is supplied (maybe)
return _color(rgb).g;
@ -297,6 +313,10 @@ var QB = new function() {
return _images[imageId].canvas.height;
}
this.func__Hypot = function(x, y) {
return Math.hypot(x, y);
};
this.func__InStrRev = function(arg1, arg2, arg3) {
var startIndex = +Infinity;
var strSource = "";
@ -523,6 +543,19 @@ var QB = new function() {
return x*180/Math.PI;
};
this.func__R2G = function(x) {
return (x*(9/10))*180/Math.PI;
};
this.func__Readbit= function(x, y) {
var mask = 1 << y;
if ((x & mask) != 0) {
return -1;
} else {
return 0;
}
};
this.func__Red = function(rgb, imageHandle) {
// TODO: implement corresponding logic when an image handle is supplied (maybe)
return _color(rgb).r;
@ -533,6 +566,11 @@ var QB = new function() {
return _color(rgb).r;
};
this.func__Resetbit = function(x, y) {
var mask = 1 << y;
return x & ~mask;
};
this.func__Resize = function() {
var tmp = _resize;
_resize = false;
@ -578,7 +616,11 @@ var QB = new function() {
}
this.func__Round = function(value) {
return Math.round(value);
if (value < 0) {
return -Math.round(-value);
} else {
return Math.round(value);
}
};
this.func__ScreenExists = function() {
@ -589,6 +631,19 @@ var QB = new function() {
return 1/Math.cosh(x);
};
this.func__Setbit = function(x, y) {
var mask = 1 << y;
return x | mask;
};
this.func__Shl = function(x, y) {
return x << y;
};
this.func__Shr = function(x, y) {
return x >>> y;
};
this.func__Sinh = function(x) {
return Math.sinh(x);
};
@ -628,6 +683,16 @@ var QB = new function() {
GX.soundVolumne(sid, v);
};
this.func__Strcmp = function(x, y) {
return (( x == y ) ? 0 : (( x > y ) ? 1 : -1 ));
};
this.func__Stricmp = function(x, y) {
var a = x.toLowerCase();
var b = y.toLowerCase();
return (( a == b ) ? 0 : (( a > b ) ? 1 : -1 ));
};
this.func__Tanh = function(x) {
return Math.tanh(x);
};
@ -640,6 +705,11 @@ var QB = new function() {
return value.trim();
};
this.func__Togglebit = function(x, y) {
var mask = 1 << y;
return x ^ mask;
};
this.func__Width = function(imageId) {
if (imageId == undefined) { imageId = _activeImage; }
if (_images[imageId].charSizeMode) {
@ -766,6 +836,14 @@ var QB = new function() {
return result;
}
this.func_Date = function() {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
return mm + '-' + dd + '-' + yyyy;
}
this.sub_Draw = function(t) {
// Turn input string into array of characters.
@ -1195,6 +1273,33 @@ var QB = new function() {
return Math.log(value);
};
this.func_Cdbl = function(value) {
const buffer = new ArrayBuffer(16);
const view = new DataView(buffer);
view.setFloat32(1, value);
return view.getFloat32(1);
};
this.func_Cint = function(value) {
if (value > 0) {
return Math.round(value);
} else {
return -Math.round(-value);
}
};
this.func_Clng = function(value) {
if (value > 0) {
return Math.round(value);
} else {
return -Math.round(-value);
}
};
this.func_Csng = function(value) {
return value; // TODO: Implement this.
};
this.sub_Circle = function(step, x, y, radius, color, startAngle, endAngle, aspect) {
var screen = _images[_activeImage];
@ -1717,7 +1822,7 @@ var QB = new function() {
}
this.sub_Restore = function(t) {
if (t == undefined || t.trim() == "") {
if ((t == undefined) || (t.trim() == "")) {
_readCursorPosition = 0;
} else {
_readCursorPosition = _dataLabelMap[t];
@ -1922,6 +2027,7 @@ var QB = new function() {
this.func_Val = function(value) {
var ret;
value = value.toString();
if (value.substring(0, 2) == "&H") {
ret = parseInt(value.slice(2), 16);
} else if (value.substring(0, 2) == "&O") {

View file

@ -31,8 +31,6 @@ async function _QBCompiler() {
var currentMethod = ''; // STRING
var currentModule = ''; // STRING
var programMethods = 0; // INTEGER
var dataTicker = 0; // INTEGER
dataTicker = 1;
if (QB.func_Command() != "" ) {
await sub_QBToJS( QB.func_Command(), FILE, "");
await sub_PrintJS();
@ -2801,10 +2799,12 @@ if (QB.halted()) { return; }
await sub_AddQBMethod( "SUB", "_AutoDisplay", False);
await sub_AddQBMethod( "FUNCTION", "_Blue", False);
await sub_AddQBMethod( "FUNCTION", "_Blue32", False);
await sub_AddQBMethod( "FUNCTION", "_Ceil", False);
await sub_AddQBMethod( "FUNCTION", "_CopyImage", False);
await sub_AddQBMethod( "FUNCTION", "_Cosh", False);
await sub_AddQBMethod( "FUNCTION", "_Coth", False);
await sub_AddQBMethod( "FUNCTION", "_Csch", False);
await sub_AddQBMethod( "FUNCTION", "_D2G", False);
await sub_AddQBMethod( "FUNCTION", "_D2R", False);
await sub_AddQBMethod( "SUB", "_Delay", True);
await sub_AddQBMethod( "FUNCTION", "_Dest", False);
@ -2815,9 +2815,12 @@ if (QB.halted()) { return; }
await sub_AddQBMethod( "SUB", "_FreeImage", False);
await sub_AddQBMethod( "SUB", "_FullScreen", False);
await sub_AddQBMethod( "FUNCTION", "_FullScreen", False);
await sub_AddQBMethod( "FUNCTION", "_G2D", False);
await sub_AddQBMethod( "FUNCTION", "_G2R", False);
await sub_AddQBMethod( "FUNCTION", "_Green", False);
await sub_AddQBMethod( "FUNCTION", "_Green32", False);
await sub_AddQBMethod( "FUNCTION", "_Height", False);
await sub_AddQBMethod( "FUNCTION", "_Hypot", False);
await sub_AddQBMethod( "FUNCTION", "_InStrRev", False);
await sub_AddQBMethod( "SUB", "_Limit", True);
await sub_AddQBMethod( "SUB", "_KeyClear", False);
@ -2834,8 +2837,11 @@ if (QB.halted()) { return; }
await sub_AddQBMethod( "FUNCTION", "_PrintWidth", False);
await sub_AddQBMethod( "SUB", "_PutImage", False);
await sub_AddQBMethod( "FUNCTION", "_R2D", False);
await sub_AddQBMethod( "FUNCTION", "_R2G", False);
await sub_AddQBMethod( "FUNCTION", "_Readbit", False);
await sub_AddQBMethod( "FUNCTION", "_Red", False);
await sub_AddQBMethod( "FUNCTION", "_Red32", False);
await sub_AddQBMethod( "FUNCTION", "_Resetbit", False);
await sub_AddQBMethod( "FUNCTION", "_Resize", False);
await sub_AddQBMethod( "FUNCTION", "_ResizeHeight", False);
await sub_AddQBMethod( "FUNCTION", "_ResizeWidth", False);
@ -2846,6 +2852,9 @@ if (QB.halted()) { return; }
await sub_AddQBMethod( "FUNCTION", "_Round", False);
await sub_AddQBMethod( "FUNCTION", "_ScreenExists", False);
await sub_AddQBMethod( "FUNCTION", "_Sech", False);
await sub_AddQBMethod( "FUNCTION", "_Setbit", False);
await sub_AddQBMethod( "FUNCTION", "_Shl", False);
await sub_AddQBMethod( "FUNCTION", "_Shr", False);
await sub_AddQBMethod( "FUNCTION", "_Sinh", False);
await sub_AddQBMethod( "FUNCTION", "_Source", False);
await sub_AddQBMethod( "SUB", "_Source", False);
@ -2856,8 +2865,11 @@ if (QB.halted()) { return; }
await sub_AddQBMethod( "SUB", "_SndPause", False);
await sub_AddQBMethod( "SUB", "_SndStop", False);
await sub_AddQBMethod( "SUB", "_SndVol", False);
await sub_AddQBMethod( "FUNCTION", "_Strcmp", False);
await sub_AddQBMethod( "FUNCTION", "_Stricmp", False);
await sub_AddQBMethod( "FUNCTION", "_Tanh", False);
await sub_AddQBMethod( "SUB", "_Title", False);
await sub_AddQBMethod( "FUNCTION", "_Togglebit", False);
await sub_AddQBMethod( "FUNCTION", "_Trim", False);
await sub_AddQBMethod( "FUNCTION", "_Width", False);
await sub_AddQBMethod( "FUNCTION", "Abs", False);
@ -2865,6 +2877,10 @@ if (QB.halted()) { return; }
await sub_AddQBMethod( "FUNCTION", "Atn", False);
await sub_AddQBMethod( "SUB", "Beep", False);
await sub_AddQBMethod( "FUNCTION", "Chr$", False);
await sub_AddQBMethod( "FUNCTION", "Cdbl", False);
await sub_AddQBMethod( "FUNCTION", "Cint", False);
await sub_AddQBMethod( "FUNCTION", "Clng", False);
await sub_AddQBMethod( "FUNCTION", "Csng", False);
await sub_AddQBMethod( "SUB", "Circle", False);
await sub_AddQBMethod( "SUB", "Cls", False);
await sub_AddQBMethod( "SUB", "Color", False);
@ -2873,6 +2889,7 @@ if (QB.halted()) { return; }
await sub_AddQBMethod( "FUNCTION", "Csrlin", False);
await sub_AddQBMethod( "FUNCTION", "Cvi", False);
await sub_AddQBMethod( "FUNCTION", "Cvl", False);
await sub_AddQBMethod( "FUNCTION", "Date$", False);
await sub_AddQBMethod( "SUB", "Draw", False);
await sub_AddQBMethod( "FUNCTION", "Exp", False);
await sub_AddQBMethod( "FUNCTION", "Fix", False);

View file

@ -2,6 +2,8 @@ Option _Explicit
$Console:Only
'$ExeIcon:'./../gx/resource/gx.ico'
' In console, after creating qb2js.exe, run: qb2js qb2js.bas > ../qb2js.js
Const FILE = 1
Const TEXT = 2
Const False = 0
@ -69,8 +71,6 @@ Dim Shared modLevel As Integer
Dim Shared As String currentMethod
Dim Shared As String currentModule
Dim Shared As Integer programMethods
Dim Shared As Integer dataTicker
dataTicker = 1
' Only execute the conversion from the native version if we have been passed the
' source file to convert on the command line
@ -1744,19 +1744,6 @@ Function ReadLine (lineIndex As Integer, fline As String, rawJS As Integer)
ReadLine = rawJS
'If (_Trim$(LCase$(Left$(fline, 4))) = "data") Then
' 'AddLineTop dataTicker, fline
' 'AddSubLinesTop dataTicker, fline
' AddLine dataTicker, fline
' AddSubLines dataTicker, fline
' Exit Function
'End If
'If (_Trim$(LCase$(Left$(fline, 6))) = "_label") Then
' 'AddLineTop dataTicker, fline
' AddLine dataTicker, fline
' Exit Function
'End If
If _Trim$(fline) = "" Then Exit Function
Dim word As String
@ -2356,19 +2343,6 @@ Sub AddQBMethod (mtype As String, mname As String, sync As Integer)
AddMethod m, "QB.", sync
End Sub
'Sub AddLineTop (lineIndex As Integer, fline As String)
' Dim lcount As Integer: lcount = UBound(lines) + 1
' ReDim _Preserve As CodeLine lines(lcount)
' Dim j As Integer
' For j = UBound(lines) To dataTicker Step -1
' lines(j).line = lines(j - 1).line
' lines(j).text = lines(j - 1).text
' Next
' lines(dataTicker).line = dataTicker
' lines(dataTicker).text = fline
' dataTicker = dataTicker + 1
'End Sub
Sub AddLine (lineIndex As Integer, fline As String)
__AddLine lineIndex, fline
End Sub
@ -3007,10 +2981,12 @@ Sub InitQBMethods
AddQBMethod "SUB", "_AutoDisplay", False
AddQBMethod "FUNCTION", "_Blue", False
AddQBMethod "FUNCTION", "_Blue32", False
AddQBMethod "FUNCTION", "_Ceil", False
AddQBMethod "FUNCTION", "_CopyImage", False
AddQBMethod "FUNCTION", "_Cosh", False
AddQBMethod "FUNCTION", "_Coth", False
AddQBMethod "FUNCTION", "_Csch", False
AddQBMethod "FUNCTION", "_D2G", False
AddQBMethod "FUNCTION", "_D2R", False
AddQBMethod "SUB", "_Delay", True
AddQBMethod "FUNCTION", "_Dest", False
@ -3021,9 +2997,12 @@ Sub InitQBMethods
AddQBMethod "SUB", "_FreeImage", False
AddQBMethod "SUB", "_FullScreen", False
AddQBMethod "FUNCTION", "_FullScreen", False
AddQBMethod "FUNCTION", "_G2D", False
AddQBMethod "FUNCTION", "_G2R", False
AddQBMethod "FUNCTION", "_Green", False
AddQBMethod "FUNCTION", "_Green32", False
AddQBMethod "FUNCTION", "_Height", False
AddQBMethod "FUNCTION", "_Hypot", False
AddQBMethod "FUNCTION", "_InStrRev", False
AddQBMethod "SUB", "_Limit", True
AddQBMethod "SUB", "_KeyClear", False
@ -3040,8 +3019,11 @@ Sub InitQBMethods
AddQBMethod "FUNCTION", "_PrintWidth", False
AddQBMethod "SUB", "_PutImage", False
AddQBMethod "FUNCTION", "_R2D", False
AddQBMethod "FUNCTION", "_R2G", False
AddQBMethod "FUNCTION", "_Readbit", False
AddQBMethod "FUNCTION", "_Red", False
AddQBMethod "FUNCTION", "_Red32", False
AddQBMethod "FUNCTION", "_Resetbit", False
AddQBMethod "FUNCTION", "_Resize", False
AddQBMethod "FUNCTION", "_ResizeHeight", False
AddQBMethod "FUNCTION", "_ResizeWidth", False
@ -3052,6 +3034,9 @@ Sub InitQBMethods
AddQBMethod "FUNCTION", "_Round", False
AddQBMethod "FUNCTION", "_ScreenExists", False
AddQBMethod "FUNCTION", "_Sech", False
AddQBMethod "FUNCTION", "_Setbit", False
AddQBMethod "FUNCTION", "_Shl", False
AddQBMethod "FUNCTION", "_Shr", False
AddQBMethod "FUNCTION", "_Sinh", False
AddQBMethod "FUNCTION", "_Source", False
AddQBMethod "SUB", "_Source", False
@ -3062,8 +3047,11 @@ Sub InitQBMethods
AddQBMethod "SUB", "_SndPause", False
AddQBMethod "SUB", "_SndStop", False
AddQBMethod "SUB", "_SndVol", False
AddQBMethod "FUNCTION", "_Strcmp", False
AddQBMethod "FUNCTION", "_Stricmp", False
AddQBMethod "FUNCTION", "_Tanh", False
AddQBMethod "SUB", "_Title", False
AddQBMethod "FUNCTION", "_Togglebit", False
AddQBMethod "FUNCTION", "_Trim", False
AddQBMethod "FUNCTION", "_Width", False
@ -3074,6 +3062,10 @@ Sub InitQBMethods
AddQBMethod "FUNCTION", "Atn", False
AddQBMethod "SUB", "Beep", False
AddQBMethod "FUNCTION", "Chr$", False
AddQBMethod "FUNCTION", "Cdbl", False
AddQBMethod "FUNCTION", "Cint", False
AddQBMethod "FUNCTION", "Clng", False
AddQBMethod "FUNCTION", "Csng", False
AddQBMethod "SUB", "Circle", False
AddQBMethod "SUB", "Cls", False
AddQBMethod "SUB", "Color", False
@ -3082,6 +3074,7 @@ Sub InitQBMethods
AddQBMethod "FUNCTION", "Csrlin", False
AddQBMethod "FUNCTION", "Cvi", False
AddQBMethod "FUNCTION", "Cvl", False
AddQBMethod "FUNCTION", "Date$", False
AddQBMethod "SUB", "Draw", False
AddQBMethod "FUNCTION", "Exp", False
AddQBMethod "FUNCTION", "Fix", False