3 ms·
Very cool. Knowing little about programming language implementation, I wonder what it would take to make a compiler. For so simple a language, I would think it
by weberc2 10y ago
Very cool. Knowing little about programming language implementation, I wonder what it would take to make a compiler. For so simple a language, I would think it would be not much harder, no?
- felixangell1024 10y agoDepends what you compile to. Writing a register allocator may be a little tricky (if you compile to a VM/Assembly/..), though if you compile to something like C, it's very easy. LLVM is another alternative which is also relatively simple to learn.
- 0x07c0 10y agoNot looked at OPs code, but if you build this stuff how a compiler book tells you to... The front end of a interpreter and a compiler is the same. You build a AST, if you interpret you well interpret that. If you have a compiler, the AST is turned into the target language, in most cases asm. The front end is the scanner/Lexer, the parser and most likely semantic analyser. So to answer you question :), if you make a non optimizing compiler, turning the AST into asm is trivial. The hard part of making compilers is the optimization. Most of the front end you do not need to write actually! There are compiler-compilers for that. https://en.wikipedia.org/wiki/Abstract_syntax_tree https://en.wikipedia.org/wiki/Abstract_syntax_tree https://en.wikipedia.org/wiki/Compiler#Front_end https://en.wikipedia.org/wiki/Compiler#Front_end https://en.wikipedia.org/wiki/Lexical_analysis https://en.wikipedia.org/wiki/Lexical_analysis https://en.wikipedia.org/wiki/Parsing https://en.wikipedia.org/wiki/Parsing https://en.wikipedia.org/wiki/Semantic_analysis_%28compilers%29 https://en.wikipedia.org/wiki/Semantic_analysis_%28compilers...
- groovy2shoes 10y agoIn fact, you can write a naïve interpreter or compiler completely in lex and yacc :)
- sklogic 10y agoCompilers are in general far simpler than interpreters, especially if you're trying to build an interpreter with a reasonable performance. For a simple language compiler is nothing more than a chain "parse -> strip off syntax sugar -> resolve lexical scope -> propagate types -> flatten the code -> emit target code", with each step being independent and trivial, nothing more than a set of tree rewrite rules.