5 ms·
Show HN: Build your own programming language in C++
- turndown 6y agoEither the title is incorrect or the OP has linked the incorrect github page on this user's account. [0] seems to be written by the same person, and generally covers what the current title claims. 0. https://github.com/codr7/liblgpp https://github.com/codr7/liblgpp
- codr7 6y agoI ensure that the title is correct and was written by me; actually they're both me, just different emails. Don't let your employer into your GitHub account, kids; it will never be the same. The linked project is an example of how to do just what it claims using the project you linked as a foundation.
- dave_sid 6y agoWould this work for Java?
- deleted 6y ago[deleted]
- codr7 6y agoIt's not a lot of code, a couple of thousand lines tops including the relevant parts of liblgpp. Translating that into sane Java takes some experience but it's totally doable.
- chrisseaton 6y agoI guess they mean for interpreting Java, not in Java.
- codr7 6y agoI can't see why not, though I honestly fail to see the point.
- chrisseaton 6y ago> I honestly fail to see the point I don't know why you'd post a Show HN and then being so dismissive of someone chiming in asking if it's possible to use your project to do something they'd be interested in! The constructive answer to the original question is - yes both this system and Java bytecode are fundamentally stack-based languages, so they should match up well.
- deleted 6y ago[deleted]
- codr7 6y agoI was being honest. If you're reimplementing Java, this project isn't going to take you very far and it's probably not the optimal approach.
- koolba 6y agoWhen did HN start showing the GitHub username in the URL? It’s a great improvement given how many such links there are. Never noticed it till now though.
- macintux 6y agoWithin the last month or so, I believe. Also does it for Twitter.
- WalterBright 6y agoThe original D compiler was written in C++ and used bits of the C++ compiler (like its optimizer and code generator), and was eventually converted to 100% D. It still kinda looks like C++. It's like my early Fortran code looked like Basic, my early C code looked like Fortran, my C++ code looked like C, and my D code looked like C++. It takes a while to write code idiomatic to the language your using.
- johnisgood 6y agoYeah, I had a bumpy transition from C to Forth. Now I can write idiomatic C, Forth, Erlang, OCaml, and Ada/SPARK, but C -> Forth was the most difficult one, I would say. As for C++: I have zero clue about it. Occasionally a student who learns C++ comes to me for help and I end up being useful and help her (by RTFM), but other than that, nothing much really.
- winrid 6y agoOut of the list you gave, when would you chose to use Ada vs the others?
- bitwize 6y agoJust think of Ada as "boomer Rust". It's great for when you have a well-specified project that can be coded in a top-down fashion, and correctness is a paramount concern. It enforces correctness through a very strong type system and a pointer model that forbids aliasing by default (pointers must be declared as 'aliased' to alias them), and the SPARK extensions provide even more guarantees, allowing you to formally specify contracts which the compiler will ensure are fulfilled.
- winrid 6y agoThanks for the explanation. I heard about its type safety, definitely want to try it on some problem at some point.
- fjfaase 6y agoIf you want to implement an interpreter for a small language, in my experience, it is better to compile it to create an Abstract Syntax Tree with an eval function. I have tried this approach in my Crystal Edit MySample clone with extended functionality. The build in script can be used to manipulate images. The scripting language has no functions and procedures, but for image manipulation scripts, this is usually no big deal. See more details, source code and example script (in the entries of my blog): http://www.iwriteiam.nl/MySample.html http://www.iwriteiam.nl/MySample.html
- vladharbuz 6y agoThank you for posting, OP! I have a tangential question about your programming style. I've noticed you define quite simple structs, that nevertheless have constructors and are not POD (e.g. they include a deque<>), and define the methods that work on them inside a namespace. [0] The two main styles I've seen are (1) the classic full-fledged OOP-y C++ classes with methods, and (2) POD structs with functions that work on them defined in namespaces, in a more C-like style. Your style seems to be a mix. You don't seem to get the big downsides of OOP, as you're grouping functions per namespace instead of having to make everything a class and ending up with a contrived architecture. At the same time, you're still going to be using many C++-isms due to your use of non-POD structs, which some people may appreciate (for uniform initialisation and memory management), and some people may enjoy less (due to the limitations and complexities of constructors, being forced to use exceptions etc.). I apologise for my extremely superficial description, but I think it's enough background for my question, namely: could you speak a bit about this programming style, what led you to choose it, and what you enjoy/don't enjoy about it? Thank you! :) [0]: https://github.com/codr7/liblgpp/blob/main/src/lgpp/parser.hpp https://github.com/codr7/liblgpp/blob/main/src/lgpp/parser.h...
- codr7 6y agoYou're welcome, the isolation is not without upsides :) That's correct, I tend to drift towards the same style when the language allows it. This is the result of 35 years of trial and error, not so much careful planning. I've found that structuring my code like this makes it more enjoyable to deal with. C++ being C++, simplifying and cutting it down to its core is essential from my experience. But I like constructors and destructors, and the standard library is pretty nice these days.
- jimsmart 6y agoI like this programming style, personally. The pattern of having helper funcs as well as class/struct methods, which is most appropriate, is a pattern I seem to see quite frequently with Go code.
- rurban 6y agoShow HN: Build your own programming language in your own language. Why on earth would someone seriously want to use C++ for the compiler, when you created a much better language? Bootstrap it once and your problems are gone. Memory safety, tree management, proper matching, high level. All extremely important for a compiler.
- dmytrish 6y agoOne reason could be that most real-world compiler/runtime codebases are in C++ (gcc, llvm/clang, v8, most of Java JITs) and C++ has just enough high-level features to make a large code base somewhat manageable. So, if one wants to work on a industrial-strength compiler at some point, it might be not the most expressive and helpful choice, but it makes sense.