This means each value at runtime has a large memory footprint, as each instance of a value contains slots for all possible types even though only one will ever be filled. Sum types solve this problem.
2. The author intermixes parsing and code generation. This makes it difficult to implement any kind of optimizations, even for an interpreter. For example, in order to do function inlining in the author's language, you would have to manually inline calls in every single part of the code generator instead of having IR transformations as separate modules (see: LLVM). It also tightly couples the two pieces of the compiler, making it difficult to change parsing strategies without rewriting the code generator for instance.
3. It took the author nearly 5000 lines of C++ to write an interpreter for a simple Javascript-like scripting language. Some of this is due to the verbosity of C++, and some is due to the needless effort of reimplementing parsing from scratch. I implemented a similar language in <1k lines of Rust [1], and I'm sure that others could do even better in Haskell or OCaml. On top of that, the author's claimed benefit of extensibility requires writing large amounts of boilerplate.
Re:Programming Your Own Language in C++
by wcrichton on Monday August 08, 2016 @01:03AM
(#12245601)
They solve the memory issue, although they are inconvenient to use vs. pattern matching and aren't type safe. Additionally, I've had no luck using classes (e.g. string) inside of unions because the compiler reasonably complains about initializers. I believe this is somewhat solved with boost::variant (and that's also getting put into C++17, iirc), but ideally it would be a part of the type system.
Re:Programming Your Own Language in C++
by clappski on Monday August 08, 2016 @04:04AM
(#12246143)
Which would introduce the overhead of a vtable, which would vary depending on how deep your inheritance goes. I haven't read the article, but I would maybe use an enum if this is just use to parse variable declarations.
Re:Programming Your Own Language in C++
by beached_whale on Sunday August 14, 2016 @09:24PM
(#12288102)
Generally the vtable is not much slower. I believe around the cost of an extra pointer derefence vs the alternative of branching. Also the compiler can sometimes optimize the cost to be the same as a normal call(e.g. final)
Re:Programming Your Own Language in C++
by jcbeard on Sunday August 07, 2016 @04:26PM
(#12243506)
If in C++, LLVM + Flex/Bison still the most productive for me. I've tried lots of other compiler compilers, but they're still the best once you learn them. If anybody is bored, I put together a simple word count example a long time ago that has taken on a life of its own: https://github.com/jonathan-beard/simple_wc_example
Re:Programming Your Own Language in C++
by spriggan3 on Monday August 08, 2016 @07:48AM
(#12246955)
> If you are serious about making your own programming language, I would strongly suggest to target the LLVM.
Strange advice given the state of LLVM on platforms such as windows. Both Swift and Crystal, LLVM based don't run on windows for instance. LLVM gives you more quickly but at the cost of portability.
Re:Programming Your Own Language in C++
by jfoutz on Sunday August 07, 2016 @05:30PM
(#12243763)
ANTLR generates a recursive descent parser. The value is, it's easier to compare (hopefully) your EBNF to the antlr spec than it is to compare your EBNF to lower level code.
Re:Programming Your Own Language in C++
by PeCaN on Sunday August 07, 2016 @11:53PM
(#12245405)
Doesn't ANTLR usually generate a bottom-up parser? I've never used ANTLR but it would be very odd to generate a recursive descent parser; LR grammars are parsed bottom-up (presumably the ‘LR’ in ‘ANTLR’).
Re:Programming Your Own Language in C++
by chubot on Monday August 08, 2016 @12:34AM
(#12245523)
ANTLR generates top down parsers which use a few different algorithms, including Parr's LL(star) algorithm and the new ALL(star) algorithm for antlr 4:
1. Their definition of values in the language (unfortunately called "Variables") is a product (class) over all possible types:
This means each value at runtime has a large memory footprint, as each instance of a value contains slots for all possible types even though only one will ever be filled. Sum types solve this problem.2. The author intermixes parsing and code generation. This makes it difficult to implement any kind of optimizations, even for an interpreter. For example, in order to do function inlining in the author's language, you would have to manually inline calls in every single part of the code generator instead of having IR transformations as separate modules (see: LLVM). It also tightly couples the two pieces of the compiler, making it difficult to change parsing strategies without rewriting the code generator for instance.
3. It took the author nearly 5000 lines of C++ to write an interpreter for a simple Javascript-like scripting language. Some of this is due to the verbosity of C++, and some is due to the needless effort of reimplementing parsing from scratch. I implemented a similar language in <1k lines of Rust [1], and I'm sure that others could do even better in Haskell or OCaml. On top of that, the author's claimed benefit of extensibility requires writing large amounts of boilerplate.
[1] https://github.com/willcrichton/lia
http://web.archive.org/web/20160616053900/http://accu.org/in...
Strange advice given the state of LLVM on platforms such as windows. Both Swift and Crystal, LLVM based don't run on windows for instance. LLVM gives you more quickly but at the cost of portability.
Bottom-up parsers are nigh unreadable by mortals.
https://scholar.google.com/scholar?cluster=11340764028758422...
https://scholar.google.com/scholar?cluster=70305927860962254...
The ANTLR acronym is explained here and doesn't have anything to do with LR grammars: http://www.antlr.org/