4 ms·
Writing a compiler is not too difficult. Most of the techniques and procedures are well researched and documented. The best thing is that you can pick the par
by ww520 3y ago
Writing a compiler is not too difficult. Most of the techniques and procedures are well researched and documented. The best thing is that you can pick the parts that are useful and stop instead of implementing the full blown end to end pipeline.
Recently in an app, instead of supporting the bare minimum I wanted to support a bit more sophisticated search expression, like embedded commands and compounded conditional expression. I just hand-rolled the lexical tokenizer and the parser, and stopped without going the full mile. The tokenizer used simple regex to break up the terms and tagged them with token types. Came up with a grammar for the mini language. The parser used the standard recursive descent parsing technique on the grammar production. It's simple to handle errors and do auto-completion of the missing syntax because the parser knew what to expect at the point of error and could fill in the default nodes in the tree as needed. The generated AST was enough to implement the search tree and the embedded commands. The whole thing was only couple files and done in couple days, but it's very useful for the app.
- 13of40 3y agoI wrote a parser/interpreter for VBA a few years back, and even though the core concepts were straightforward, conforming to a language that had been in development for decades turned out to be brain-wreckingly tedious. One of the biggest parts of it was a block of thousands(?) of conditional statements to figure out the result type of every combination of type+operator+type. At the end of the ordeal, though, I threw the engine into a test app that looked like GWBASIC, and got a little nostalgia kick from being able to run BASIC like it was 1983.
- sancarn 3y agoIs that really needed? You can use VBA to do all the heavy lifting. That's what we do here: https://github.com/sancarn/stdVBA/blob/master/src/stdLambda.cls#L939-L962 https://github.com/sancarn/stdVBA/blob/master/src/stdLambda....
- tomcam 3y agoThat and there were never official grammars published for any version of any basic from Microsoft afaik
- makeset 3y agoI doubt they ever existed. I had to write a VBScript parser some 25 years ago (to syntax-check ASP files before they would crash live) and to get it to parse valid syntax correctly, I ended up with a messy patchwork of logic which was also letting through some really pathological edge cases. While trying to fix it, to my utter delight, I found that Microsoft’s parsers also allowed the exact same cases which looked nothing like valid code otherwise. I don’t recall specific examples anymore, but they were far from obvious without looking at the parser code, and I made and won bets with other programmers on whether those snippets would throw syntax errors or not.
- jakobson14 3y ago> I doubt they ever existed. Probably right, since Microsoft's BASIC interpreter is probably derived from the one that Bill Gates wrote as a teenage slacker. He wrote it, tried to sell it at the homebrew computer club, and then threw a hissy fit when people kept copying his paper tapes for their friends. Thus, Microsoft was born. (He got lucky later when Microsoft were chosen by IBM to develop a DOS, then bought a working DOS from somebody else. The rest as they say is history)
- vasac 3y agoBy the time Microsoft secured a deal with IBM, they were already a multimillion-dollar company, which was quite significant in 1980. Judging by their future moves, it's evident that luck wasn't the only factor contributing to their massive success.
- adrian_b 3y agoMicrosoft was the most important vendor of programming language compilers and interpreters for the CP/M operating system, for computers based on Intel 8080 or Zilog Z80. They had compilers for many programming languages, but before buying the operating system that they have rebranded proudly as MS-DOS and which they have resold to IBM at an overprice, they had nothing to do with operating systems. So the deal had nothing to do with their previous technical competence, but they happened to know the right information about who is selling and who is buying, which allowed them to gain the most as the middlemen.
- pests 3y agoDid you need thousands of conditionals? Couldn't you map types and operators into an efficient representation for a single lookup?!
- ww520 3y agoThat's amazing to implement a full interpreter for VBA. I would imagine a lot of the type resolution are redundant. E.g. +, -, *, and / would have the same type resolution for numbers. Those can reuse the same type resolver. May be + also work on string. Then call the string type resolver on it in addition to see if it can be resolved. Anyway, kudos for the amazing work.
- sancarn 3y agoI have an open source compiler-interpreter for VBA here: https://github.com/sancarn/stdVBA/blob/master/src/stdLambda.cls https://github.com/sancarn/stdVBA/blob/master/src/stdLambda.... In case you were interested :P (I say compiler, it compiled to byte code, not asm.)
- userbinator 3y agoOne of the biggest parts of it was a block of thousands(?) of conditional statements to figure out the result type of every combination of type+operator+type This is not something uncommon for a dynamically typed language; I think the simplest approach is a multidimensional array lookup.
- luismedel 3y agoSame boat here. I wrote a VB to C++ transpiler to make VB6-like IDE for PalmOS (basically a clone of the existing Handheld Basic++). The type system was a mess. After one year of development on my free time I had the compiler and a form editor, but lots of out of memory errors when running the code on the simulator (and real devices) :-| At the same time, Palm sold the OS licenses to ACCESS inc., IIRC, and the future of the platform was sealed. Long story short, I shelved the project and I lost it during one of my cyclical "move backups to to a bigger disk" migration.
- WalterBright 3y ago> Writing a compiler is not too difficult. Wait till you're faced with the endless .h files with their inevitable use of nutburger undocumented C extensions.
- habibur 3y agoDon't write a compiler for C. Write compilers that convert your sources to C. You create the header files.
- Apocryphon 3y agoWorks for Swift!
- WalterBright 3y agoD's ImportC compiler can convert C code to D!
- FeepingCreature 3y agoHere's a secret trick to parsing C headers: whenever you hit an error, discard the line and restart parsing. If you have a fallback explicit FFI, most ~mystery extensions~ can be done without.
- iamflimflam1 3y agoEveryone should learn about bison/yacc/lex.