9 ms·
Show HN: I wrote a BASIC interpreter in Go
- mikivujkovic 8y agoThat's how Bill Gates started
- hsnewman 8y agoTechnically, I believe they (Paul Allen & Gates) ported the Basic from the Harvard Dec-10 using a cross compiler. This was before there was licenses in code.
- rustcharm 8y agoNo. They wrote a fresh version, but they did use an 8080 assembler on a Dec-10.
- wedowhatwedo 8y agoHis first program he wrote as a professional was a payroll program. Altair BASIC came later.
- jimnotgym 8y agoI still have fond memories of the various BASICs. I first used BBC basic at school (unofficially), and it seemed like magic. I had an Atari ST and a got a copy of STOS at home. Whilst BASIC encourages all kinds of spaghetti, I'm still convinced it is a better language than Python to start on. Control flow is so obvious to non-programmers when it has line numbers.
- jacquesm 8y agoNot all BASIC dialects have line numbers, there are some very elegant version of basic out there. GFA basic for instance and BBC Basic. Both have named functions and do not use line numbers.
- jimnotgym 8y agoI know, and they (like Python) all make excellent second steps. The line numbers are bad for structure, but good for stage 1 learning is what I am saying. In fact, letting people find the limitations of line numbers and GOTO statements is an excellent way to show them why you would want something more structured, rather than just telling them.
- jacquesm 8y agoI do not know about that. Teaching people something bad first only to show them a better way later still wastes their time and ingrains bad habits. Personally I prefer teaching the better way right from the beginning (assuming I know what the better way is).
- jimnotgym 8y agoSo we should skip Scratch for kids and go straight to Java? Static types and Object Oriented Programming are better ways, I think most of us will agree? I want to give noobs a taste of the magic of programming immediately 10 Print "hello" 20 GOTO 10 Is something anyone who can read could understand. Even the relatively simple while True: print("Hello") Has several concepts to explain before you can understand it.
- jacquesm 8y agoI do not think the Scratch vs Java comparison is a fair one here. There is nothing wrong with Scratch. But bad Scratch vs good Scratch or bad Java vs good Java would be a fair comparison and in those cases I would advocate for teaching the right thing from the start.
- protomyth 8y ago> Static types and Object Oriented Programming are better ways, I think most of us will agree? I'm pretty sure you won't find near universal agreement on either.
- 8y ago
- pjmlp 8y agoBASIC dialects with support for structured programming without line numbers, and compilation to native code, were already available on CP/M.
- jimnotgym 8y agoGreat, and I'm saying that was worse for beginners than line numbers (like the BASIC in the linked article) Letting people crawl is a great way to teach them the advantages of walking.
- pjmlp 8y agoWhat I meant is that people were able to crawl, walk and run without switching toolchain.
- jacquesm 8y agoCould someone that has the ability to compile this check the output of PRINT 3+4*5 please?
- tyingq 8y ago$ echo 'PRINT 3+4∗5' > test.bas $ go run main.go test.bas 34 $ echo 'PRINT (3+4*5)' > test.bas $ go run main.go test.bas 23 $ echo 'PRINT (3+4)*5' > test.bas $ go run main.go test.bas 35 No idea where it's getting 34 from though.
- stevekemp 8y agoGrr. Expressions are hard: 10 LET a = 3+4*5 20 PRINT a, "\n" Produces the expected outcome: -> 23 Bug filed: https://github.com/skx/gobasic/issues/41 https://github.com/skx/gobasic/issues/41
- jacquesm 8y agoYes, but it should not require that LET statement. The expression evaluator looks subtly broken.
- stevekemp 8y agoBug filed, as per the edited comment above. I definitely agree this is wrong!
- tyingq 8y agoDon't know if it's the same bug, but "PRINT 1/0" returns 1. "PRINT (1/0)" prints nothing. Using "LET a=1/0" raises a div by zero error. Also, "LET a=1%0" panics.
- stevekemp 8y agoThe issue with the "PRINT 1/0" and "PRINT (1/0)" are the same as the previous one. LET a = 1/0 SHOULD produce a division by error, so I think that's correct. But 1%0 shouldn't panic. I'll fix that.
- vmlinuz 8y agoI once wrote a Logo interpreter in BASIC... For my Computer Studies GCSE (UK national exams at 16) final project. It never actually worked, but it was a heroic effort!
- jonsen 8y agoI once wrote an assembler in BASIC. We extended the instruction set of a microprocessor by some external circuitry, so the standard assembler was not very useful. BASIC was fine for the job. And it actually worked :)
- mrspeaker 8y agoA while ago I started making a game (https://www.youtube.com/watch?v=GwBiJR_rj_w https://www.youtube.com/watch?v=GwBiJR_rj_w) that was going to be about typing in program listings from magazines and books (like "back in the day")... but the "twist" was going to be that the whole world turned out to be alterable and scriptable with BASIC. The WIP title was "BASIC Instincts". The problem was I spent way to much time having fun making the interpreter (in JavaScript) and then Else.HeartBreak() was released and it took the wind out of my sails (because it was great!)... I might go back to it now though, writing interpreters is really enjoyable!
- JoeAltmaier 8y agoA fun project! I had a buddy tried this, wrote it in C++ over a week. Had console, graphics, formatting and networking (I think). The trick was first a parser, then use C++ objects for each construct and instantiate one for each parsed element. The objects had a 'list' and a 'run' method. They were linked in programmatic order. So run the first one, which returns the next one to run (since it may vary with IF, GOTO etc) Here it is! http://www.moondew.com/basic/ http://www.moondew.com/basic/
- marktangotango 8y agoSounds like interpreting from an instance of the AST. Pretty easy but there are gotchas; like handling break/continue in loops. It’s an interesting excercise everyone ought to try IMO.
- deckarep 8y agoDoes anyone remember some installs shipped with a game called gorilla.bas? A fun little game where you had two gorillas across a cityscape and the whole idea was to take turns throwing bananas at each other to see who can get a critical hit. What made it hard is you had to enter both an angle and speed and hope you calculated the right combination to hit the other player. Eventually I wrote my own version of the game in flash/actionscript....but it’s long gone.
- tyingq 8y agoHere's a js/html5 port: http://www.kylem.net/stuff/gorilla.html http://www.kylem.net/stuff/gorilla.html
- deckarep 8y agoYep that looks like a legit clone of it. Thanks for the link.
- xigency 8y agoThat's a very common game or example often with tanks. I like this gorilla/banana theme.
- MrBuddyCasino 8y agoYes! This was the best BASIC game I ever knew.
- xtagon 8y agoWhen I was first learning about programming, I had hours and hours of enjoyment just from modifying that game even though I didn't quite know what I was doing. Just things like changing the banana speed or making bananas spawn other bananas when they hit. Another fun one was called Labyrinth (I think it was spelled LABRNTH.BAS or something). I liked it so much that years later I started making two remakes (one in HTML canvas/JS, one in DCPU-16). QBasic was a great introductory language for a kid wanting to learn programming IMO.
- Intermernet 8y ago
- vfinn 8y agoPeter Norvig did one for Python, and it's pretty cool: https://github.com/norvig/pytudes/blob/master/ipynb/BASIC.ipynb https://github.com/norvig/pytudes/blob/master/ipynb/BASIC.ip...
- tyingq 8y agoInteresting abuse of a regex as the tokenizer. Cool.
- elcomet 8y agoHow is it an abuse? I thought using regex as the tokenizer was the main approach
- tyingq 8y agoI've never seen a tokenizer as one big regex that finds all the types/functions/keywords all at once.
- dboon 8y agoTo get a bit theoretical, one way to write a lexer is to use a state machine called a deterministic finite automata - a fancy term for a state machine that is finite and always gives the same output given an input. Each new character moves the state, and there are terminal states when you reach e.g. a space which will spit out what kind of token you just read. You can show that the things a regex can compute are the same things that this kind of state machine can compute. I believe that most regex under the hood use such a state machine, so it's very natural to use regex to tokenize.
- gnulinux 8y agoYou can't do it in general, e.g. to tokenize Python you'd need a layer that'll count tabs (because INDENT and DEDENT are tokens). That it, unless your regex dialect is Turing complete, and if my memory serves, some of them are? I can't really remember. In general though, you can't do it with "normal" regex.
- donatj 8y agoOooh wonderful! I wrote hundreds of games as a kid in QBasic and have had the idea for years to write a QBasic to JavaScript transpiler so I could actually showcase some of them online. With Go adding its WASM support, I’ve actually been considering a Go interpreter as an option. This is super inspiring. Nice work.
- lixtra 8y agoDid you search for existing ones? https://github.com/smhanov/qb.js/blob/master/README.md https://github.com/smhanov/qb.js/blob/master/README.md https://news.ycombinator.com/item?id=1042896 https://news.ycombinator.com/item?id=1042896
- tniemi 8y agoI think BASIC is something that everyone has to write at least once in their lifetime. My personal take is here (over 7 year old code, beware): http://tech.nimbus.fi/minibasic/ http://tech.nimbus.fi/minibasic/ And I wrote that just to make this functional: http://tech.nimbus.fi/c128/ http://tech.nimbus.fi/c128/ (Try the "help" command.)
- abecedarius 8y agoYep, and here was mine: https://github.com/darius/parson/blob/master/eg_basic.py https://github.com/darius/parson/blob/master/eg_basic.py written to exercise a parsing library. It's super-slow.
- peteri 8y agoMine in c# is here https://github.com/peteri/ClassicBasic https://github.com/peteri/ClassicBasic
- tonyedgecombe 8y agoYes, https://github.com/tonyedgecombe/pytinybasic https://github.com/tonyedgecombe/pytinybasic which encouraged my sons to do the same https://github.com/jonathanedgecombe/tiny-basic-interpreter https://github.com/jonathanedgecombe/tiny-basic-interpreter and https://github.com/grahamedgecombe/tiny-basic-compiler https://github.com/grahamedgecombe/tiny-basic-compiler
- jhallenworld 8y agoI wrote a BASIC interpreter in VAX assembly language (in 1986). I feel old.. I enjoyed Atari 800 BASIC, so it was modeled after that, tokenized during line entry so that it would not allow you to enter lines with bad syntax. VAX assembly had instructions for spanning over characters given in a set or scanning to a character in a set, so string processing was very nice. You should use floating point for the line numbers so that you can always insert more statements between existing lines :-)