The BASIC language may be considered old-hat here in 2025, and the days when a computer came as a matter of course with a BASIC interpreter are far behind us, but it can still provide many hours of challenge and fun. Even with our love of all things 8-bit, though, we’re still somewhat blown away by [Matthew Begg]’s BASIC interpreter written in 10 lines of BASIC. It’s an entry in the BASIC 10-liner competition, and it’s written to run on a Sinclair ZX Spectrum.
The listing can be viewed as a PNG file on the linked page. It is enough to cause even the most seasoned retrocomputer enthusiasts a headache because, as you might expect, it pushes the limits of the language and the Sinclair interpreter. It implements Tiny Basic as a subset of the more full-featured BASICs, and he’s the first to admit it’s not fast by any means. He gives a line-by-line explanation, and yes, it’s about as far away from the simple Frogger clones we remember bashing in on our Sinclairs as it’s possible to get.
We love it that there are still boundaries to be pushed, even on machines over four decades old, and especially that this one exceeds what we thought was a pretty good knowledge of Sinclair BASIC. Does this language still have a place in the world? We always look forward to the BASIC 10-liner competition.
Header: background by Bill Bertram, CC BY-SA 2.5.
I remember that I wrote a Forth interpreter in Sinclair Basic :-D
I also once wrote a rudimentary Forth interpreter in ZX Spectrum Basic and called it North (for Not Forth) ;-) !
I tried riding my own custom programming language in basic as well. It would have been something unique. Instead, it just didn’t work! LOL!
I remember writing a LOGO interpreter in BASIC many years ago and (God help me!) it was turtle slow.
Come on … most 8-bitters’ BASIC already only was a Tiny-BASIC compared to the original. (Original) BASIC’s reputation probably will never recover from that!
+1
Especially the C64 Basic was such a horrible offender here, I think.
It created a generation of bad-Basic lovers that never progresssed to level of Amiga Basic, Locomotive Basic or Quick Basic/Turbo Basic.
Sure, it could be fixed by Simons’ Basic, but the plain v2 Basic was as bad as the Basic of that VC20.
In my opinion, Commodore should have included free copies of Simons’ Basic with all new C64/C128 in order to undo the harm it had done.
Jack Tramiell was the CEO of commodore at the time and he was a notorious penny pincher. You still have the license to PET Basic from Microsoft, so he just ported it to the commodore 64 for free. And his brain, if you were getting 64k of ram, which was massive at the time, you can afford to get a better BASIC on your own.
I was a developer for Playnet (which eventually became AOL) on the C-64 writing system internals. One of the coolest features I implemented was was a 6502 dynamic ROM swap. A flag on the IO chip would control if the ROM was mapped into the 64K address space, or if normal memory was. All calls to the ROM went through a jump table of the last page of ROM address space. If the ROM was swapped in, no problem. The magic happen when the ROM was swapped out. I wrote my own jump table at the same address space as the standard ROM. My jump table started at the start of the stack and would push a return address (the jump table entry point) onto the top of the stack. It would then use the IO chip to swap the ROM in the return to the ROM jump table, which would execute the ROM subroutine, then return to the stack, swap the ROM back out, then return to the calling 6502 address space. It worked seamlessly, and you could execute calls to the ROM (Address space $A000–$BFFF) from under the ROM. Thus providing an additional 8K of useable address space.
Well, I don’t know but I did great with the c64 and the programs made sense unlike C++. It was also fun, fun, fun.
The problem here is, however, that C64 had greatly contributed in ruining Basic’s reputation.
It was worse that PET Basic at some point.
(Okay, Commodore 128 and Plus4/C16 had better Basics,
but programmers for those platforms did port over from C64 software base,
so they were of little help in a grand picture.)
Before C64, I think, Basic had a neutral/non-bad reputation and Basic programmers had been tolerated as beginners or laymen.
After C64, Basic and its programmers got the stigma of being dumb or untalented.
Visual Basic programmers are still being laughed at because of this development.
Instead of associating QB/QBasic or VB and its programmers with prototyping and rapid application development, they’re being thought of as being poor/bad.
Because the majority, if not all people, out there do associate Basic with the crappy C64.
One word of Basic and –Ka-Ching!– C64 Basic comes to people’s mind.
That’s what leaves their mouths then: “Oh, yes! I had a C64!”. Sigh.
Because that’s what the boomers and Gen X had been grown up the most – C64/Basic.
That’s all they ever knew before they bought Windows PCs and shrink-wrapped software.
If it had been humble GW-BASIC, at least. Oh, well! 😮💨
That’s at least how it was or is here in former Western Europe, I think.
In the US, home computer market broken down earlier – thank god*.
People over there had memories of TRS-80 or Apple II or something.
Or Tandy 1000, as a game/home computer.
(*The 16-Bit “home computers” we had in Western Europe of 80s had proper Basics, I think.
Atari ST with GFA Basic or Amiga with Amiga Basic etc.
But these 16/32-Bit machines were functional closer to PCs, I think.)
My apologies if I was a bit too harsh here.
I didn’t mean to critise the C64 users or whatsoever,
they have a great community despite the breadbin being quite, um, limited from my point of view.
On bright side, the C64 also has the SID chip, which is interesting.
It had been used on early IBM PC sound cards, too.
The C64 Kernal is also, um, “okay” I think. Same goes for GEOS and the REUs.
But the C64 Basic (Basic v2) without extension (Simons’ Basic etc)
really was limited even by the standards of its own time
and lead to quite some spaghetti code and overuse of peek/poke.
It didn’t have graphics, sound/music or joystick routines or concepts such as DO..LOOP or ELSE,
which even humble GW-Basic in the MS-DOS 2.x era had.
Not to mention advanced things like GOSUB in conjunction with labels instead of line numbers as being common with Turbo Basic or QB/QBasic on PC.
Or use of SUBs (sub routines), to tidy up the program code even more.
C64 programmers never had them. So they had to write complex spaghetti code in order to make Basic v2 understand.
To see which useful things are all missing in Basic v2, a quick look at Commodore Basic v3.5 or v7 will suffice.
The Commodore PET still got Basic v4 as an upgrade, at least, if I’m not mistaken.
The PETs from the 70s also were more professional, more PC-like than C64.
Also, ZX81 and its related machines had used token for Basic and supported math functions.
GW-Basic had used token format, too. Saving in ASCII required adding an “A”: SAVE “FILE.BAS”,A
Again, nothing against the C64 users. It’s not their fault what had been done to them.
And things like Simons’ Basic were used by power users, too, I admit.
However, that doesn’t fix the circumstance that a lot of coding on C64 had been done in either assembly or plain Basic v2.
Because that’s what a stock C64 had to offer.
Using compilers in case of commercial software happened, as well, maybe, sure.
But that’s what software developers had used, rather than the bedroom programmers.
The majority of C64 listings printed in hobbyist magazines probably were in plain Basic v2.
And that’s what the majority of C64 users had been grown up and what had an lasting impression.
That’s what’s Basic being associated with, sadly.
Here’s a list of all the commands understood by C64 Basic v2. Pretty short, huh?
https://www.valoroso.it/en/basic-v2-commodore-64-command-list/
My apologies for my poor English, also. I’m not sure if my wording was well chosen here.
(I’m not affiliated with this site; speaking under correction here)
Posting source code listing as a png image makes it mostly gibberish. Looks like lots of statements are concatenated on those lines.
And what is a “line”? You probably can write a C compiler on one line of C source code. But I don’t endeavor in that direction, I prefer line breaks and readability over obfuscation and “puzzling”.
Looks like that png is designed to mimic the output of the Spectrum’s 32-column thermal printer.
Yeah – lots of nostalgic memories of that burning aluminium powder smell of the ZX Printer.
The lines are 256 characters long, and then when you factor in tokenization, that’s a lot of program.
Yeah – the rules of the BASIC 10 liner contest have various character limits. The ‘Tools’ category allows up to 256 characters per line. And Sinclair BASIC is tokenised, so it stores commands in only one character. Atari and Commodore BASIC can also use shortened statements like ‘?’ for PRINT, but it can make for some difficult to read code. Some of the other categories in the competition are even more limited – 80 or 120 characters per line. You can follow the links in the article and try TinyBasic in a Spectrum emulator!
Matthew, would love to contact you to talk about the BASIC 10-Liner competition – we did meet at CRASH! Live last year but I didn’t get any contact details.
I started with BASIC, and got quite good- and very efficient- at it. Altair, Imsai, PDP, Model I… I’ve always felt BASIC got a bad rap. Sure, it allows you to write terrible spaghetti code. But you don’t have to. And the fact that the terrible spaghetti code runs and works I think is a testament to just how GOOD the language is. Python seems to me like a semi-ripoff of BASIC.
MBASIC/BASIC-80 on CP/M was one of the better Basics already.
Together with BASCOM someone could easily create standalone executables for the platform. C64 Basic on other hand.. Ew.
It’s still my favorite language of all time. It’s probably just because it was my first, but I just feel very at home with it. I’m very comfortable with how it works, and it works well if you do it right.
Impressive + Retro = impressively retro! (And the “IDE” seems nearly air-gapped from the modern world.)
Yes, of course a bunch of BASIC statements were squeezed into one line. That was legal BASIC, and that’s how the author pulled off this extreme feat in “only 10 lines.” Presumably lines were of finite length.
According to various, surprisingly active Sinclaire forums, POKE was used to turn on Caps-lock or something less-than-devious.
The code would be most fun to analyze if someone (or the original author) could kindly turn the PNG image “listing” into text, so we can reformat it for readability. (I’d suggest OCR, but surely half of those lower-case b’s would turn into 6’s)
It is an entry for the annual BASIC 10-liner contest. The ‘tools’ category has a 256-characters per line limit, but tokenised commands are stored as 1 character so the lines can appear longer than that for Spectrum listings. Here’s the code in text format:
1 BRIGHT 0: BORDER 0: PAPER 0: INK 4: CLS : DEF FN n(w$)=w$>=”0″ AND w$<="9":
DEF FN c(x)=214+xCODE c$(1): DEF FN h()=h*(h> l)+l*(l>=h): POKE 23658,8: DIM
k$(32,6): DIM j(32): DIM j$(32): DIM l$(256,50): DIM p$(256,50): DIM b$(20): DIM
c(256): DIM v(36): DIM p(256): DIM f(26,2): READ t$,u,h,i: FOR n=1 TO 30: READ
z$,z: LET k$(n)=z$: LET j(n)=LEN z$: LET j$(n)=CHR$ (214+z): NEXT n: PRINT t$””
READY”
2 LET i=1: GO SUB 10: LET l=0: FOR z=s$=”” TO 0: FOR y=s$”RUN” TO 0: FOR l=
1 TO h: FOR x=l$(l,1)=” ” TO 0: LET c$=l$(l): GO SUB 3: NEXT x: NEXT l: PRINT :
GO TO 2: NEXT y: FOR y=FN n(s$(1)) TO 0: GO SUB 8: GO SUB 3 : GO TO 2: NEXT y: F
OR y=LEN s$>3 TO 0: LET l$(VAL s$)=””: GO TO 2: NEXT y: FOR y=1 TO LEN s$: FOR x
=FN n(s$(y)) TO 0: LET l=VAL s$( TO y-1): LET s$=s$(y TO ): LET y=1e9: NEXT x: N
EXT y: GO SUB 8: LET l$(l)=c$: LET p$(l)=s$: LET p(l)=LEN s$: LET h=FN h(): NEXT
z: GO TO 2
3 FOR n=FN c(35) TO 0: POKE 23692,255: PRINT ‘”COMMANDS””: FOR m=1 TO 21: PR
INT k$(m),: NEXT m: PRINT ” : NEXT n: FOR n=FN c(9) TO 0: POKE 23658,0: STOP :
NEXT n: FOR n=FN c(37) TO 0: CLS : NEXT n: FOR n=FN c(12) TO 0: PRINT “END “;l’:
GO TO 2: NEXT n: FOR n=FN c(22) TO 0: LET l=VAL c$(2 TO )-1: NEXT n: FOR n=FN c
(24) TO 0: LET i=2: GO SUB 10: FOR m=s$=”” TO 0: LET v(VAL c$(4 TO 4+(c$(5)”)”
)))=VAL s$: NEXT m: NEXT n: FOR n=FN c(39) TO 0: DIM v(36): NEXT n
4 FOR n=FN c(26) TO 0: POKE 23692,255: FOR m=1 TO h: FOR o=l$(m,1)=” ” OR m<V
AL ("0"+(c$+b$)(2 TO )) TO 0: PRINT m;p$(m)( TO p(m)): NEXT o: NEXT m: NEXT n: F
OR n=FN c(16) TO 0: RUN : NEXT n: FOR n=FN c(3) TO 0: INK VAL c$(2): CLS : NEXT
n: FOR n=FN c(23) TO 0: LET v(27)=v(27)+1: LET v(27+v(27))=l: LET l=VAL c$(2 TO
)-1: NEXT n: FOR n=FN c(40) OR v(27)<1 TO 0: LET l=v(v(27)+27): LET v(27)=v(27)-
1: NEXT l: NEXT n
5 FOR n=FN c(31) TO 0: LET c$=c$+" ": POKE 23692,255: FOR m=(LEN c$) TO 1 STE
P -1: LET z=m*(c$(m)” “): LET m=m*(c$(m)=” “): NEXT m : LET y=c$(z)”;”: FOR
m=NOT (c$(2)=CHR$ 34 OR z<2) TO 0: PRINT c$(3 TO z-2+y);: NEXT m : FOR m=NOT (c$
(2)CHR$ 34 AND z>1) TO 0: PRINT VAL c$(2 TO z-1+y);: NEXT m: FOR m=NOT y TO 0:
PRINT : NEXT m: NEXT n: FOR n=FN c(34) TO 0: SAVE “a” DATA l$(): SAVE “b” DATA
p$(): SAVE “c” DATA p(): DATA “TINYBASIC V1.0”
6 NEXT n: FOR n=FN c(25) TO 0: LOAD “a” DATA l$(): LOAD “b” DATA p$(): LOAD ”
c” DATA p(): CLS : LET h=256: NEXT n: FOR n=FN c(1) TO 0: BEEP .2,VAL c$(2 TO ):
NEXT n: FOR n=FN c(36) TO 0: FOR m=2 TO LEN c$: FOR o=c$(m)CHR$ 203 TO 0: LET
z=m: LET m=99: NEXT o: NEXT m: FOR m=NOT VAL c$(2 TO z-1) TO 0: LET c$=c$(z+1 T
O ): GO TO 3: NEXT m: NEXT n: FOR n=INKEY$CHR$ 226 TO 0: PRINT “BRK AT “;l’: G
O TO 2: NEXT n: DATA 0,1,1,”BEEP”,1,”CLEAR”
7 FOR n=FN c(27) TO 0: LET z=c$(5)”)”: LET v(VAL c$(4 TO 4+z))=VAL c$(7+z T
O ): NEXT n: FOR n=FN c(21) TO 0: LET u=u+1: LET z=c$(5)”)”: LET y=VAL c$(4 TO
4+z): LET v(y)=VAL c$(7+z): LET f(y,1)=VAL c$(9+z TO ): LET f(y,2)=l: NEXT n: F
OR n=FN c(29) TO 0: LET y=c$(5)”)”: LET z=VAL c$(4 TO 4+y): LET v(z)=v(z)+1: L
ET m=v(z)> f(z,1): LET l=l*m+f(z,2)*NOT m: LET u=u-m: NEXT n: RETURN : DATA 39,”
CLS”
8 LET d$=””: LET s=0: FOR n=1 TO LEN s$: LET z$=s$(n): FOR m=z$CHR$ 34 TO 0
LET s=NOT s: NEXT m: FOR m=z$=” ” AND NOT s TO 0: FOR o=1 TO 30: FOR q=(s$+b$)
(n TO n+j(o)-1)<>k$(o, TO j(o)) OR s=1 TO 0: LET d$=d$+j$(o): LET n=n+j(o)-1: LE
T z$=””: NEXT n: NEXT q: NEXT o: LET d$=d$+z$: NEXT m: NEXT n: LET c$=””: LET s=
0: FOR n=1 TO LEN d$: LET z$=d$(n): FOR m=z$<>CHR$ 34 TO 0: LET s=NOT s: NEXT m:
FOR m=NOT (s=0 AND z$>=”A” AND z$<=”Z”) TO 0
9 LET c$=c$+”V(“+STR$ (CODE z$-64)+”)”: LET z$=””: NEXT n: NEXT m: LET c$=c$+
z$: NEXT n: RETURN : DATA 37,”END”,12,”EXIT”,9,”FOR”,21,”GOTO”,22,”GOSUB”,23,”HE
LP”,35,”IF”,36,”INPUT”,24,”LET”,27,”LIST”,26,”LOAD”,25,”MODE”,3,”NEXT”,29,”NEW”,
16,”PRINT”,31,”REM”,20,”RETURN”,40,”SAVE”,34,”ABS”,-25,”AND”,-16
10 LET s$=””: PRINT FLASH 1;” “;: FOR n=1 TO 1e9: PAUSE 0: LET i$=INKEY$: FOR
m=(i$<” (“(1) OR i$>”`9″(i)) TO 0: LET s$=s$+i$: PRINT CHR$ 8+i$; FLASH 1;” “;:
NEXT m: FOR m=i$<>CHR$ 12 OR s$=”” TO 0: LET s$=s$( TO -1+LEN s$): PRINT CHR$ 8+
” “;CHR$ 8+CHR$ 8; FLASH 1;” “;: NEXT m: LET n=n+1e9*(i$=CHR$ 13): NEXT n: PRINT
CHR$ 8+” “: RETURN : DATA “INT”,-28,”NOT”,-19,”OR”,-17,”PI”,-47,”RND”,-49,”TO”,
-10,”THEN”,-11
Thanks for the OCR of the program listing. There were several typo that I fixed. <> didn’t recognized half the time, and the OCR software got confused about single and double quotes. What’s the best way to post my results?
That’s not an OCR of the listing – that’s the actual text of the listing. Enjoy.
You’re right about the POKE – it turns CAPS LOCK on or off. If you follow the link in the article, there’s a line by line description of how the code works.
I tried two free OCR tools I found on the web, and both failed miserably, even though the PNG “listing” is the cleanest, sharpest bitmapped character data you could every dream of. These days OCR vendors are pitching AI and language recognition–everything we don’t need to convert essentially a screenshot worth of code, where punctuation is more important than letters, into text (the original BASIC code).
Try using your phone.
The author included a .tap file which you can easily upload into an emulator and get it that way. There are also tools that will dissect that file and get the source code out of it.
Any computer that ships with Office still has a BASIC interpreter. I suspect that it is still by far the most widely available programming language, even if it’s rarely used.
I liked working in VBN BASIC, Visual BASIC is Not BASIC.
I just completed writing a dos like command line interface with a line based Basic interpreter mainly for use with the RPI Pico/Pico 2 . Written in Python. It uses syntax pulled from Commodore/gwbasic/radioshack/apple basic with a lot of my own commands. And when run on a Linux box, graphics is supported (via pygame as the back end library) for plotting (plot/hplot commands, as well as my extentions for circles, rectangles, text, etc.). Again mainly to support ‘old’ basic programmers. Obviously never truly complete, but it runs a lot of ‘old’ (Not vb style) text basic games, and graphics programs and such you can pull off the web. Great way to ‘test’ compatibility. Why? Just because it was fun to do. It all started with wanting to design a DOS like command line interface to serially access running Picos. With an A: and B: drives (directories behind the scenes) to hold files as needed like in the old days, then oh, lets get batch files working, then what about edlin to modify files on the fly? Wrote that. Then might as well implement all the dos File commands, and then … Hmmm, why not a Basic interpreter too? Haven’t wrote a interpreter in a long time… So it snowballed down hill. Usually does!
Typo: “Again mainly to support ‘old’ basic programmers.” -> Again mainly to support ‘old’ basic programs and my own little basic programs to interface to Pico hardware/disk.
Us nerds are an insecure lot. I think I saw one ‘good job’ comment above. The rest are ‘I did this other thing but’ and ‘it’s no good’ comments
ha – you’re right! I didn’t take it personally ;-)
gcc c supports machine language small codes development using high-level c.
These small modules are placed in a unsigned char array.
how man lines of code in gcc c compiler?
AI Overview.
As of 2019, the GCC (GNU Compiler Collection) contained roughly 15 million lines of code. It’s important to note that this number encompasses the entire GCC project, which supportsmultiple programming languages, including C, C++, Fortran, Java, Ada, and Go. The C compiler specifically would account for a significant portion of this total, but not the entirety.
I started with BASIC and became quite skilled—and highly efficient—at it. Altair, Imsai, PDP, Model I… I’ve always believed BASIC has been unfairly criticized. Yes, it allows for messy spaghetti code, but that doesn’t mean you have to write it that way. In fact, the fact that such code can still run and function speaks to the strength of the language. To me, Python feels like a partial reinvention of BASIC.