Author Topic: QBJS - QBasic for the Web  (Read 40647 times)

0 Members and 1 Guest are viewing this topic.

Offline dbox

  • Newbie
  • Posts: 80
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #15 on: February 18, 2022, 07:27:44 pm »
Hey @bplus nice example!  Yes, Subs and Functions are supported and at present all variables must be declared. 

Goto and gosub are not supported at present. There will be some complexity to do that as there is not a parallel in javascript.

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: QBJS - QBasic for the Web
« Reply #16 on: February 18, 2022, 07:37:38 pm »
If you support SUBS, all a GOSUB is is a SUB with a label. I would think you should be able to process a GOSUB the same way as a SUB.


Hey @bplus nice example!  Yes, Subs and Functions are supported and at present all variables must be declared. 

Goto and gosub are not supported at present. There will be some complexity to do that as there is not a parallel in javascript.
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #17 on: February 18, 2022, 08:19:23 pm »
@dbox
I am trying a game of Hangman and I can't seem to get to first base with this code:

Code: QB64: [Select]
  1. Dim Shared As String w(1 To 7) ' word list
  2. LoadWords
  3. For i = 1 To 7
  4.     Print w(i)
  5.  
  6. Sub LoadWords
  7.     w(1) = "HANGMAN"
  8.     w(2) = "GALLOWS"
  9.     w(3) = "REVEAL"
  10.     w(4) = "KEYPRESS"
  11.     w(5) = "REPORT"
  12.     w(6) = "LETTERS"
  13.     w(7) = "BASIC"
  14.     'w(8) = ""
  15.     'w(9) = ""
  16.     'w(10) = ""
  17.     'w(11) = ""
  18.     'w(12) = ""
  19.     'w(13) = ""
  20.     'w(14) = ""
  21.     'w(15) = ""
  22.     'w(16) = ""
  23.     'w(17) = ""
  24.     'w(18) = ""
  25.     'w(19) = ""
  26.     'w(20) = ""
  27.     'w(21) = ""
  28.     'w(22) = ""
  29.     'w(23) = ""
  30.     'w(24) = ""
  31.     'w(25) = ""
  32.     'w(26) = ""
  33.     'w(27) = ""
  34.     'w(28) = ""
  35.     'w(29) = ""
  36.     'w(30) = ""
  37.     'w(31) = ""
  38.     'w(32) = ""
  39.     'w(33) = ""
  40.     'w(34) = ""
  41.     'w(35) = ""
  42.     'w(36) = ""
  43.     'w(37) = ""
  44.     'w(38) = ""
  45.     'w(39) = ""
  46.     'w(40) = ""
  47.     'w(41) = ""
  48.  
  49.  

Originally I had Data and Read to read into w$() but those aren't supported so am trying a simple array def in a sub.

What am I doing wrong?

Offline dbox

  • Newbie
  • Posts: 80
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #18 on: February 18, 2022, 08:26:40 pm »
I’m not yet supporting the (x to y) syntax when dim-ing arrays.  Try this instead:
Code: QB64: [Select]

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #19 on: February 18, 2022, 08:28:50 pm »
Ah good! Thanks!

Oh while I'm here, will REDIM work from w(7) to say REDIM as string w(25) in the Sub probably.

Offline dbox

  • Newbie
  • Posts: 80
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #20 on: February 18, 2022, 08:32:43 pm »
Yes, ReDim is supported as well as ReDim _Preserve.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #21 on: February 18, 2022, 09:22:10 pm »
@dbox

OK I have some code running but as soon as I uncomment this function, it all dies!
Code: QB64: [Select]
  1. 'Function Reveal$ ' from remaining selected letters a * will cover unchosen letters in w$(round)
  2. '    Dim as integer i
  3. '    dim b$
  4. '    For i = 1 To Len(w$(round))
  5. '        If InStr(selectLetters$, Mid$(w$(round), i, 1)) > 0 Then b$ = b$ + "*" Else b$ = b$ + Mid$(w$(round), i, 1)
  6. '    Next
  7. '    Reveal$ = b$
  8. 'End Function
  9.  


selectLetters$ is shared (and tracks letters guessed by replacing with a -) and so is w$ and round

I am not even calling the function yet.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #22 on: February 18, 2022, 09:38:29 pm »
@dbox

More info, shouldn't Mid$( ) be color coded same as InStr?
 
Mid$.PNG


PS I started to break down the loaded call to InStr with c$= Mid$(...

Offline dbox

  • Newbie
  • Posts: 80
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #23 on: February 18, 2022, 09:39:41 pm »
Try putting the if/then/else on separate  lines.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #24 on: February 18, 2022, 09:42:13 pm »
Yep! that fixed it, I'll remember for other places I do that.

Offline dbox

  • Newbie
  • Posts: 80
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #25 on: February 18, 2022, 09:51:49 pm »
@dbox

More info, shouldn't Mid$( ) be color coded same as InStr?
 
Mid$.PNG



Yes, the syntax highlighting still needs a bit of refinement.  I started from a vbscript implementation that already existed for the codemirror editor.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #26 on: February 18, 2022, 10:31:12 pm »
I like the part of the console that shows the JavaScript conversion. It would be fun to make a small qb app, and then use the JS code on a server. I have made a few dozen JS snippets for various websites, but I do so at such an infrequent rate, I find myself constantly in need of refreshing my language skills. That's very time consuming. It would be much more intuitive to program in QB and convert. Thanks for this. It's a very neat project on a couple of levels!

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #27 on: February 18, 2022, 10:44:47 pm »
@dbox
 I almost have this guy roughed out but _delay is not working

Code: QB64: [Select]
  1. _Title "Hangman" 'for QB64 B+ (redo Hang it) restarted 2019-06-14 for strip down version
  2. Const hung = "Hanged!" ' allows 7 misses
  3. ReDim Shared w$(7) ' word list
  4. Dim Shared As Integer round, nHung, done
  5. Dim Shared selectLetters$
  6. Dim As Integer place
  7. Dim k$
  8. LoadWords
  9. For i = 0 To 7
  10.     cp i + 1, w$(i)
  11. Do ' main loop manages the letters to select from and removes letter when selected
  12.     selectLetters$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  13.     nHung = 0
  14.     done = 0
  15.     Cls
  16.     Update
  17.     While done = 0
  18.         k$ = ""
  19.         While Len(k$) = 0
  20.             k$ = UCase$(InKey$)
  21.             _Limit 60
  22.         Wend
  23.         place = InStr(selectLetters$, k$)
  24.         If place Then
  25.             selectLetters$ = Mid$(selectLetters$, 1, place - 1) + "-" + Mid$(selectLetters$, place + 1)
  26.         End If
  27.         If InStr(w$(round), k$) = 0 Then
  28.             nHung = nHung + 1
  29.         End If
  30.         Update
  31.         _Limit 30
  32.     Wend
  33. Loop Until round > 7
  34.  
  35. Function Reveal$ ' from remaining selected letters a * will cover unchosen letters in w$(round)
  36.     Dim i, b$
  37.     b$ = ""
  38.     For i = 1 To Len(w$(round))
  39.         If InStr(selectLetters$, Mid$(w$(round), i, 1)) > 0 Then
  40.             b$ = b$ + "*"
  41.         Else
  42.             b$ = b$ + Mid$(w$(round), i, 1)
  43.         End If
  44.     Next
  45.     Reveal$ = b$
  46.  
  47. Sub Update 'draw everything for screen 0 and while we're at it decide if word round is done
  48.     cp 8, "Hangman: " + Reveal$
  49.     cp 10, "Press a letter from: " + selectLetters$
  50.     cp 12, "Gallows Report: " + Mid$(hung, 1, nHung)
  51.     If Reveal$ = w$(round) Then
  52.         cp 14, "Congratulations! you got it."
  53.         done = -1
  54.     End If
  55.     If Len(hung) = nHung Then
  56.         cp 14, "Yikes! You were hung by: " + w$(round)
  57.         done = -1
  58.     End If
  59.     If done = -1 Then
  60.         If round + 1 > 7 Then
  61.             cp 16, "Hangman is out of words to play. Goodbye in 5"
  62.             _Delay 5
  63.             End
  64.         End If
  65.     End If
  66.     If done = -1 Then
  67.         If round + 1 <= 7 Then
  68.             round = round + 1
  69.             cp 16, "Next Round in 5 secs"
  70.             _Delay 5
  71.             'sleep2 ' sleeps suck!!!
  72.             '_KeyClear ' >>>>>>>>>>>> unsupported
  73.         End If
  74.     End If
  75.  
  76. Sub cp (row, s$)
  77.     Locate row, (80 - Len(s$)) / 2: Print s$
  78.  
  79. Sub LoadWords
  80.     w$(0) = "BPLUS GAMES"
  81.     w$(1) = "HANGMAN"
  82.     w$(2) = "GALLOWS"
  83.     w$(3) = "REVEAL"
  84.     w$(4) = "KEYPRESS"
  85.     w$(5) = "REPORT"
  86.     w$(6) = "LETTERS"
  87.     w$(7) = "BASIC"
  88.     'w$(8) = ""
  89.     'w$(9) = ""
  90.     'w$(10) = ""
  91.     'w$(11) = ""
  92.     'w$(12) = ""
  93.     'w$(13) = ""
  94.     'w$(14) = ""
  95.     'w$(15) = ""
  96.     'w$(16) = ""
  97.     'w$(17) = ""
  98.     'w$(18) = ""
  99.     'w$(19) = ""
  100.     'w$(20) = ""
  101.     'w$(21) = ""
  102.     'w$(22) = ""
  103.     'w$(23) = ""
  104.     'w$(24) = ""
  105.     'w$(25) = ""
  106.     'w$(26) = ""
  107.     'w$(27) = ""
  108.     'w$(28) = ""
  109.     'w$(29) = ""
  110.     'w$(30) = ""
  111.     'w$(31) = ""
  112.     'w$(32) = ""
  113.     'w$(33) = ""
  114.     'w$(34) = ""
  115.     'w$(35) = ""
  116.     'w$(36) = ""
  117.     'w$(37) = ""
  118.     'w$(38) = ""
  119.     'w$(39) = ""
  120.     'w$(40) = ""
  121.     'w$(41) = ""
  122.  
  123.  

Sleep is messing with Keypresses need a _KeyClear

I tried my own version of sleep and that worked perfect in QB64 keeping inkey$ clear for next letter but QBJS ignored code. So I am just trying _delay to give user time to read what happened before next round starts.

Sharing
https://v6p9d9t4.ssl.hwcdn.net/html/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7lTc4psdhRn2WJ7FNlsKtOyyfKKbPYYadl7wuKjnYYcdnBjZKPFhhz2cKIko+WGPPZ1cGSj7YY89nW1ZKP9hl52eHUSUQWGXvZ4/+kqgsN/e0wfCaqCw3+7T8eTVRWG/3af7bNVHQb/dtJYk10dBz/62zLTXSUHP/rccfNdJIdf+t9ZlV1Mh3/631v1XUyHf/rQ/jVhdId/+tGvVWWsh3/7FPuuy1kO4expcXZaz/ufVVN
« Last Edit: February 18, 2022, 10:51:32 pm by bplus »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #28 on: February 18, 2022, 10:57:50 pm »
It's a very neat project on a couple of levels!

I totally agree.  And Pete -- welcome back!  You were missed.

- Dav

Offline dbox

  • Newbie
  • Posts: 80
    • View Profile
Re: QBJS - QBasic for the Web
« Reply #29 on: February 18, 2022, 11:24:52 pm »
I like the part of the console that shows the JavaScript conversion. It would be fun to make a small qb app, and then use the JS code on a server. I have made a few dozen JS snippets for various websites, but I do so at such an infrequent rate, I find myself constantly in need of refreshing my language skills. That's very time consuming. It would be much more intuitive to program in QB and convert. Thanks for this. It's a very neat project on a couple of levels!

Pete

Thanks @Pete!  If you are interested in using just the converter you can check it out here: https://github.com/boxgaming/gx/blob/main/tools/qb2js.bas.  It's actually written in QB64 and can be run from the command line.  In fact, it actually converts itself to javascript to be included in the web version:

Code: [Select]
qb2js.exe qb2js.bas > qb2js.js