Settings

Theme

WwwBasic – An implementation of BASIC designed to be easy to run on the Web

github.com

161 points by lorenzleutgeb 7 years ago · 90 comments

Reader

pedasmith 7 years ago

New versions of BASIC are surprisingly common (heck, I even have one that's gotten some super-nice comments).

Mind-blowing BASIC example from the 1970's:

    100 A$="ABCDEF"
    110 A$ = REP ("123", 4, 0)
    120 PRINT A$
The result is ABC123DEF -- at position 4 (strings start at 1), replace 0 characters with "123". So the REP function looks at the statement it's part of, finds the variable that is being assigned to, and uses that as the starting string (!). It's kind of like a function, only with a weird implied variable that only works in an assignment.

You can't just PRINT the result directly, because then how would REP know what the string to change is?

Nowadays "weird" syntax has some solid foundational reason why it's useful or important. This not-really-a-function, though, simply has no justification whatsoever.

(This example is from the BASIC embedded in the Tektronix 4050 terminal).

There are enough variants of BASIC that there's a really nifty handbook with the differences at https://archive.org/details/Basic_Handbook_2nd_Edition_1981_...

  • tralarpa 7 years ago

    I did my first serious programming steps with BASIC on the C64 and the Amiga.

    The (actually second) BASIC interpreter on the Amiga was made by Microsoft. It was great and horrible at the same time. Great because it had while-loops, labels instead of line numbers, and basic (no pun intended) support for the Amiga GUI, i.e you could open windows and create menus. Horrible because it was super slow and, for some reasons unknown to me, Microsoft had decided to not implement stack frames for subroutines. So, all local variables in subroutines were static and recursions were not allowed.

    The weird thing: You still had to write the "STATIC" keyword although all subroutines were static anyway! (I assume the STATIC keyword was optional in the other Microsoft BASIC version and non-static subroutines were supported there)

  • pvg 7 years ago

    Isn't that just someone putting some weird thing in some oddball BASIC implementation? I don't think any of the popular ones did anything like that.

    • rzzzt 7 years ago

      Some of them allowed to use MID$ for the left-hand side of an assignment to replace a substring, which is also unexpected.

      • pvg 7 years ago

        MID$ still took a string as an argument, though, right?

      • lucio 7 years ago

        That syntax was irregular but very useful. It was not surprising or confusing.

        A$ = "ABCD"

        MID(A$,2,2)="12"

        PRINT $A

        >A123

        • bbernoulli 7 years ago

          Assuming a typo here, but I would be surprised if the output contained "3"

        • quietbritishjim 7 years ago

          This is still going strong today in list slice notation in Python (and other languages, no doubt):

              x = [1,2,5,6]
              x[2:2] = [3,4]
              print(x)
          
          (Slice notation can be used on any collection, not just lists, but you can't use it in assignment like that for strings because they're immutable, so you'd have to convert to a list of characters and back.)
  • thaumasiotes 7 years ago

    > So the REP function looks at the statement it's part of, finds the variable that is being assigned to, and uses that as the starting string (!). It's kind of like a function, only with a weird implied variable that only works in an assignment.

    As DonHopkins notes, this appears substantially identical to the assign-equals operators which are generally thought of as "easy and convenient" rather than "weird". How is the line of code "a *= 7" meaning

    "whatever the current value of a is, multiply that by 7 and assign the result to a"

    weirder than the line 'a$ = REP("123",4,0)' meaning

    "whatever the current value of a$ is, do a string replacement on it and assign the result to a$"?

  • DonHopkins 7 years ago

    Speaking of weird BASIC features, does anyone know why DECSYSTEM 20's BASIC had a "LISTREVERSE" command?

    Yes, it actually did exactly what it sounds like!

    Chalk one up for DEC and BASIC. What other programming languages support that feature, huh?

    https://imgur.com/a/Bt40M

        DECSYSTEM 20 BASIC User's Guide: LISTREVERSE command
    
        LISTREVERSE
        LISTNHREVERSE
    
        LISTREVERSE and LISTNHREVERSE print the contents of the
        user's memory area in order of descending line numbers. 
        LISTREVERSE precedes the output with a heading,
        LISTNHREVERSE eliminates the heading.
    
        LISTREVERSE
    
        EQUIV             10:53                      13-NOV-75
    
        40    END
        35    PRINT "THE EQUIVALENT CURRENT IS",I, " AMPERES"
        25    I=E1/R
        10    INPUT R
        5     INPUT E1
    
        READY
    
    http://www.bitsavers.org/www.computer.museum.uq.edu.au/pdf/D...
    • tom_ 7 years ago

      If you're using a printer, presumably you might sometimes want the stuff at the top of the program to be last on the printout, so it's easy to get at without having to flip through a pile of paper?

      Same might go for a terminal with no scrollback...

      Come to think of it, this sounds actually rather useful. Had this been available when I was programming on the BBC Micro, I bet I'd have used it.

  • kijin 7 years ago

    An object-oriented version (assuming mutable strings) might be:

        A$.REP("123", 4, 0)
    
    So the syntax is weird, but the concept isn't totally unusual.
    • pedasmith 7 years ago

      Exactly. It's a perfectly reasonable concept with a completely outlandish syntax. I don't know of any current programming language where the RHS of an assignment will even know what the LHS is.

  • DonHopkins 7 years ago

    In C++ that would be called "operator REP="

  • exikyut 7 years ago

    Hmm.

      perl -le '$A = "ABCDEF"; substr($A, 3, 0) = 123; print $A'
      ABC123DEF
lioeters 7 years ago

Just a couple days ago, I was reading an article by David Brin called "Why Johnny Can't Code" (2016) [0] - where the author laments how difficult it was to find an accessible version of BASIC for his son to play with / learn on (they ended up buying a Commodore 64). Well, now the language runs on the web!

[0] https://www.salon.com/2006/09/14/basic_2/

xrd 7 years ago

Why is Google publishing this?

It's late but I'm confused why anyone would spend time doing this, and since it is in the official Google repository, presumably someone at Google was actually doing it on paid time.

  • xrd 7 years ago

    The author has a fascinating discussion of basic on his blog.

    https://flagxor.com/article/source-code

    I might be coming around to the why of this.

    • bcherny 7 years ago

      Sidenote, that is one obnoxious color scheme :/

      • TomMarius 7 years ago

        And width. I have an ultrawide display, really not a good reading experience. Newspapers put text in columns for a reason.

      • jlg23 7 years ago

        Looks fine in lynx on a 80x25 terminal ;)

      • zem 7 years ago

        I spent a few seconds thinking it was one of those popup overlays and looking for the close button

      • jkabrg 7 years ago

        If you're using Firefox, use the reader mode.

  • zokier 7 years ago

    > since it is in the official Google repository, presumably someone at Google was actually doing it on paid time

    Unlikely. Google (like many companies) assert ownership of everything their employees create, even on their free time. There are plenty of projects under Google organization that have no other affiliation with Google.

  • jlg23 7 years ago

    > I'm confused why anyone would spend time doing this

    Plain, good, old fun?

bencollier49 7 years ago

One of the reasons that BASIC was such an effective beginners' language was that it often appeared at bootup, so all you needed to do was turn on your machine and type a couple of lines to run a program.

This isn't that, but withering comments from Dijkstra aside, BASIC is still a great introduction to programming, and this is a fantastic idea; it'll be great to run loads of old type-ins in the browser.

Could have done with a crisper font, though.

  • pedasmith 7 years ago

    I have to both agree and disagree here.

    I love the idea of a language that has the absolute fewest number of decisions between "I need a little program" and being able to share the program with a friend. The current trend where there's a seemingly bazillion steps before you can write a trivial utility is unfortunate.

    However, the microcomputer BASICs had big usability problems. Us modern programmers are so used to block oriented languages that we assume every language just had them. For example, in C you can replace any statement with a list of statements enclosed in { }. In Python where blocks are indented statements, or Pascal where you can make a block of statements with a BEGIN/END.

    But many microcomputer BASICs don't have that at all. The IFs often don't have the concept of an ELSE, and often the only thing you can do from an IF is a GOTO.

    The end result is a mess of jumps where the programmer is "emulating" real blocks with GOTOs. I've had the recent fun of transcribing programs from a bunch of different early BASIC languages. The complaint that BASIC leads to spaghetti code is real. There are bazillions of GOTOs, and it's a real drain on your mental processing to keep them all straight.

    • pjmlp 7 years ago

      Sure, but BASIC doesn't need to be the 8 bit versions or GW.

      Something like Turbo Basic or QBasic, with a REPL as well would do quite nicely.

      On the other hand maybe what is missing is making those little boards with JavaScript or Python more widespread.

  • tokyodude 7 years ago

    Why is BASIC a great introduction to programming? Genuinely curious. It's a bad language by pretty much all computer science standards. It only has global variables. In the past it used line numbers not labels. Many Basics only allowed 2 letter variable names. It has almost no library. (I suppose those could be added but they're already available in other languages). AFAIK there is little to no tooling for BASIC (meaning no auto0completion or popup docs that many languages have now)

    I grew up on basic on TRS-80/Apple II/Atari 800 and I have super fond memories of those times. You can even find lots of my BASIC code in old magazines from the 80s. But, I don't really believe those were better than today with kids with Scratch or Unity or other alternatives. Python or JavaScript (runs everywhere) etc... Maybe even PICO-8/Lua etc...

    Are there some concrete reason BASIC is actually a good place to start?

    • pjmlp 7 years ago

      I wouldn't advise unstructured BASIC, there are plenty of structured improvements to choose from.

      It is easy to learn because it was the first language to come with batteries.

      In some 8 bit micros it was as powerful as the underlying Assembly, only slower. So it was relatively easy to prototype.

      Also the the runtime was the OS.

      Nowadays a kid needs to learn how to access the development environment, probably configured by an adult and to do more complicated tasks might need some help getting libraries.

      Maybe stuff like boards with MicroPython will help here.

    • tree_of_item 7 years ago

      It's completely nostalgia. BASIC doesn't have any redeeming features except being in the right place at the right time.

  • toxik 7 years ago

    I think the font quality is due to pixel density. It looks upscaled.

crazygringo 7 years ago

Wow this takes me back.

BASIC was how I started programming, 30 years ago.

Is this the first time someone's implemented BASIC in JavaScript? It seems so... obvious in hindsight.

I wonder if this has any potential for kids who want to start to learn programming? Or if there are other better, more modern, places to start these days?

Also... why is this from Google of all places? Seems like more of a personal hobby project.

  • themodelplumber 7 years ago

    I had some similar nostalgia and tried out FreeBASIC the other day. It was dead easy to get started. I couldn't believe how simple it was to get up and running, and while I mostly worked on some command line programs, it was really cool to see how GTK and Qt and other GUI tools were made usable as well.

    After this experience I can easily see how someone could still be writing BASIC for a living in 2018, whether for standalone desktop apps or for web CGI, etc.

    • bitwize 7 years ago

      There was a dialect called Business BASIC that was popular for business apps in the 1970s. It supported things like record-based file I/O (creating a navigational database), the creation of multiple concurrent tasks (on a 64k machine!), as well as exception handling in the form of additional params that specified which line numbers to branch to on error for all I/O operations.

      Business BASIC was ported to mainstream PCs and other micros as the market for dedicated Business BASIC minicomputer systems waned. It still lives on today in the form of two dialects: ProvideX and BBj. And people are still writing/maintaining applications in it.

      • exikyut 7 years ago

        oooh.

        > In the 1980s, Business Basics were ported from their original proprietary environments to many Unix platforms, CP/M, and to DOS. In the 1990s, some Business Basics were ported to Linux and Windows, ... [https://en.wikipedia.org/wiki/Business_Basic]

        Know where I might be able to find some of the earlier versions? The DOS and CP/M versions simply for the archival/historic value, but I'd love to play with the Linux version in particular, early Linux software is virtually impossible to find. Early Windows versions would certainly be fascinating as well.

  • cm2187 7 years ago

    Well, with .net being ported to wasm, you should soon be able to use vb.net in the browser too.

kerng 7 years ago

Cool idea! I always thought of Python being the modern Basic. Its interesting to see this - could there even be potential for something like this?

  • int_19h 7 years ago

    Not really. Like you said, we have better beginner languages now, Python being the most prominent (especially seeing how it has a turtle graphics library out of the box). BASIC is extremely inconsistent for a modern language.

    • cm2187 7 years ago

      Python has its own inconsistencies, like any language that has been around for a very long time. Like some collection properties have to be accessed with a function (len()) while other as an instance function. It doesn’t seem to follow any logic.

      • int_19h 7 years ago

        BASIC is far worse than that. Like, at least in Python, len() is a regular function. In BASIC, tons of core features are implemented as statements, with rather weird syntax that got even more weird over time.

           LINE (x1, y1) - (x2, y2), 5, FB
        
        Can you remember what it does? (no, it doesn't draw a line; it would, if not for "FB").

        Or, for example, MID$ is a function, except when it's not:

           MID$(s$, 1) = "foo"
        
        Then there's stuff like DEFINT and GOSUB and ON STRIG and ...

        The reason for all this is that BASIC was never designed as a language in which the standard library could mostly be written, or even described, in that language. Thus, most features were implemented as intrinsics, with magic syntax and/or semantics, which varies drastically from feature to feature. This goes even for the standard functions - e.g. MID$ is a function (except when it's not, as above) - but it can take either 2 or 3 arguments, which is not something that you can declare yourself. As a result, when language evolved, it also did so in a haphazard way, as with LINE re-purposed for other reasons.

        The original BASIC was very simple (but also non-extensible). The BASIC that most people remember - at its peak - was actually more complicated than an equivalent subset of Python.

        • DonHopkins 7 years ago

          And then there was Applesoft's assembly language extension mechanism: "&".

          Enhancing Applesoft using ampersand routines: https://link.springer.com/content/pdf/10.3758/BF03203836.pdf

          >Applesoft BASIC's ampersand (&) jump feature gives the programmer an easy and fast way to connect machine language routines to BASIC. When the Applesoft interpreter encounters an "&," it does an immediate and unconditional jump to location $3F5 (decimal 1013). This feature, originally described as being "intended for the computer's internal use only" and "not a proper APPLESOFT command" (Apple Computer, 1978, p. 123), has become the favorite procedure for executing machine language routines from a running Applesoft program.

          • int_19h 7 years ago

            Most BASICs had something like that. Here's something from QBASIC documentation:

               Transfers control to a machine-language procedure.
            
               CALL ABSOLUTE ([argumentlist,] offset%)
            
                   ▪ argumentlist    Arguments passed to a machine-language procedure
                                     as offsets from the current data segment.
                   ▪ offset%         The offset from the current code segment, set by
                                     DEF SEG, to the starting location of the procedure.
            
               Example:
                   'Calls routine for printing the screen to a local printer.
                   DIM a%(2)
                   DEF SEG = VARSEG(a%(0))
                   FOR i% = 0 TO 2
                      READ d%
                      POKE VARPTR(a%(0)) + i%, d%
                   NEXT i%
                   DATA 205, 5, 203  : ' int 5  retf  'Machine-language code
                                                      'for printing screen.
                   CALL ABSOLUTE(VARPTR(a%(0)))
                   DEF SEG
            
            And before that, there was DEF USR: http://www.antonis.de/qbebooks/gwbasman/defusr.html
        • DonHopkins 7 years ago

              >>> s = 'foobar'
              >>> s[1:2] = 'xxx'
              Traceback (most recent call last):
                File "<stdin>", line 1, in <module>
              TypeError: 'str' object does not support item assignment
          
          Dammit Python! Immutable strings.
        • Jaruzel 7 years ago

          > LINE (x1, y1) - (x2, y2), 5, FB

          > Can you remember what it does? (no, it doesn't draw a line; it would, if not for "FB").

          Yes. Draws a filled box (FB), from top-left (x1,y1) to bottom right (x2,y2) in Purple (colour 5).

          Although thinking about it... I'm pretty sure that the co-ords of 0,0 were bottom left, not top left on DOS based graphic screens, so is quite probably bottom-left to top-right.

          Source: One of my first 'paid' jobs was as a QB45 programmer for an accountancy firm in the mid 1990s.

TickleSteve 7 years ago

Missed naming opportunity!

"gwwwbasic" anyone??

mdhughes 7 years ago

[Chipmunk BASIC](http://www.nicholson.com/rhn/basic/) is a nice console-usable BASIC on most platforms. You can work with it line-numbered, or structured, and has some graphics and sound support in the Mac desktop version.

I started on BASIC on the TRS-80 Model I in 1980, and it was a great introduction.

That said, newbies are probably better off starting Python (use IDLE for a decent REPL & editor), type `from turtle import *; reset()` to get a graphics window, and learning a nice language from the start.

pankajdoharey 7 years ago

Doesnt even have a repl now. This one will surely take many people back https://github.com/robhagemans/pcbasic

orionblastar 7 years ago

Is this similar to VBScript that Microsoft's Internet Explorer used? I used to program in that for one of my former employers using Active Server Pages as well. JavaScript was a lot faster so we switched to that.

  • tomcam 7 years ago

    This is a fun toy program. VBScript was a pretty comprehensive language built by some of Microsoft’s top compiler people back in the day. It was infinitely more powerful than this and was often used for serious development.

    • exikyut 7 years ago

      What happened to VBScript? It feels like a kind of BeOS or Amiga within the scripting-on-Windows domain. It's just... disappeared, and tends to either fulfill "it MUST run EVERYWHERE" OCD-backward-compatibility requirements - basically Win32 Perl - or it's used for viruses.

      Outside highly narrow application/context-specific situations, that is.

      • kyberias 7 years ago

        > What happened to VBScript?

        Nothing.

          Microsoft Windows [Version 10.0.17134.285]
          (c) 2018 Microsoft Corporation. All rights reserved.
        
          C:\Windows\SysWOW64>cscript
          Microsoft (R) Windows Script Host Version 5.812
          Copyright (C) Microsoft Corporation. All rights reserved.
        
          Usage: CScript scriptname.extension [option...] [arguments...]
        
          Options:
           //B         Batch mode: Suppresses script errors and prompts from displaying
           //D         Enable Active Debugging
           //E:engine  Use engine for executing script
           //H:CScript Changes the default script host to CScript.exe
           //H:WScript Changes the default script host to WScript.exe (default)
           //I         Interactive mode (default, opposite of //B)
           //Job:xxxx  Execute a WSF job
           //Logo      Display logo (default)
           //Nologo    Prevent logo display: No banner will be shown at execution time
           //S         Save current command line options for this user
           //T:nn      Time out in seconds:  Maximum time a script is permitted to run
           //X         Execute script in debugger
           //U         Use Unicode for redirected I/O from the console
        • Jaruzel 7 years ago

          Internet Explorer also still supports it. You can write a full, albeit basic, application in a text file, rename it as 'whatever.hta' and run it by double clicking on it. It launches in its own window using the IE html control as its form. The MSDN pages have a clone of Notepad.exe as a vbscript .hta as an example.

          In some ways it's like the grandparent of Electron or React apps, just a decade too early.

          The last .hta I wrote was several years ago - a [mostly] complete clone of 'Active Directory Users & Computers' that anyone could use.

        • mysterydip 7 years ago

          To expand, it's still there and used, though Microsoft has pushed Powershell scripts in its place.

      • ptx 7 years ago

        VBScript and JScript were very much tied to COM, as was the document object model in Internet Explorer, where these scripting implementations were used, up until version 6/7.

        What happened was that Internet Explorer moved towards a more standards-compliant JavaScript implementation, leaving the kind-of-ES3-compliant JScript behind, while at the same time COM was being generally deemphasized in favor of .NET.

        It's still there, but its usefulness is limited by several things:

        * It can only make use of libraries that expose dynamically bound COM objects. No C libraries (so no win32 API) and no .NET.

        * Even then, it can't subscribe to COM events, as far as I can tell. (There is some weird mechanism with some external XML file in certain contexts.)

        * As for JScript, I haven't found any transpilers that target ECMAScript 3, so it would be difficult to use modern libraries with it. (And it would probably be slow anyway.)

ilaksh 7 years ago

It would be neat if someone could port some classic basic games to this.

netsharc 7 years ago

What a neat way to encode the "DOS" font at the end of the wwwbasic.js file, including the characters that you can use to draw boxes.

Timucin 7 years ago

Not sure if anyone mentioned that but WBasic would be a much better name I think. Just like GWBasic and QBasic.

ourmandave 7 years ago

Aw, it doesn't have line numbers.

Is there at least GOTO?

andreygrehov 7 years ago

One word. Why? Serious question.

  • jacquesm 7 years ago

    Two words: Why Not?

    Just as serious. Programming does not always require a solid reason before you can engage in it, quite a bit of it is like playing. If you actually enjoy programming then it is much nicer to do than if everything needs a financial justification.

    There is a similar analogy with music, you could make money with it, but you can also simply enjoy it.

  • khazhou 7 years ago

    Because with this javascript library, you can run BASIC programs on the web.

    BASIC was for many people their introduction to programming, and it has a simplicity which is nearly unmatched today.

    By providing a way to run the language inside the ubiquitous web browse, one may hope to spread the usage of BASIC as a learning tool, especially for youngsters.

    • andreygrehov 7 years ago

      Why would Google want to spread the usage of BASIC? From an engineering perspective, this is an interesting project to work on, but I can't find a reason for Google to publish it. I also don't think that BASIC is a good learning tool nowadays. I'm not the only one who thinks so. A quote by Edsger W. Dijkstra:

      > It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.

bernardlunn 7 years ago

I think this falls between the cracks. Serious programmers (most HN readers) think it is too basic (sic). Non-programmers prefer hacking together cloud apps using Zap or IFTT where no code is needed. Enterprise just want thousands offshore maintaining legacy code

xaduha 7 years ago

There are much better languages than BASIC, for whatever purpose.

  • jacquesm 7 years ago

    There are now, but BASIC was invented in the 70's, and some very powerful versions of it were used to build just about everything in the 80's and even the 90's. There was BBC Basic on the BBC micro, GFA Basic on the ST and of course MSVB on the PC, which if you have windows is most likely still lurking in your system somewhere.

    Each of those could be used to write very complete applications including GUIs and such.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection