Hacker News new | past | comments | ask | show | jobs | submit login
Compiling a Functional Language Using C++, Part 9 – Garbage Collection (danilafe.com)
103 points by goranmoomin on Feb 12, 2020 | hide | past | favorite | 23 comments



is there a "cpp: the good parts" book that anyone can recommend for someone who has done c/cpp a long time ago?


Bjarne Stroustrup's "A Tour of C++" (http://www.stroustrup.com/Tour.html) is close. It's a slim volume that is roughly a survey of the language as if it had begun with C++11.


I wish he’d update them (both books) for C++20 (C++2a), or even just C++17.


A Tour of C++ 2nd edition covers C++17, AFAIK.


Yes it does and also mentions parts of C++20 like concepts and modules.


thanks


Besides "A Tour of C++", you might enjoy the following:

- "The C++ Standard Library, 2nd Edition" from Rainer Grimm

- "C++ Templates, The Complete Guide, 2nd Edition" from David Vandevoorde, Nicolai M. Josuttis, Douglas Gregor

- "Functional Programming in C++" from Ivan Čukić

- "Concurrency with Modern C++" from Rainer Grimm

And any talk from Bjarne or Herb at CppCon or C++Now.


My comment might be unwarranted but at this point, considering Rust might be also an option.


I like Rust, but it still has to grow up a bit to be an option for GUIs, iOS/Android, game engine middleware, Windows/macOS systems programming.

It will eventually get there, but it isn't there yet.


You make them yourself. With Rust you might not need those things as much, because you roll these things yourself.

What are the GUI frameworks in other languages? Qt? wxWidgets? GTK? These are all so, so, so bad.

I'm saying this as I'm writing my own GPU-rendered immediate-mode GUI toolkit with multiple rendering backends. I can't do this in any other language, like...I would legit get stuck on the build systems. You are right, it would be better if there were somewhat better frameworks but I'm ok writing my own.

As an aside, my long term dream is to write a next-gen OS in Rust. GPU-first, new application paradigms, compiler comes with the OS, first-class networking support.

The languages empowers you to an insane degree.

What Windows/macOS systems programming would you want?


No I don't, I rather be productive doing what I am supposed to do in first place.

Some examples, DriverKit, COM/UWP, WinUI, Metal Shaders, CUDA, SYSCL, Treble, Unreal/Unity.

For all of those I already have a good mix of Swift, .NET, Java/Kotlin and C++, as they are the official OS SDK languages, writing in Rust would just slow me down in its present state.

Bad UI toolkits?! Best that Rust currently has is Gtk-rs and that is being nice versus what those toolkits and GUI tooling are capable of.


> Metal Shaders Write a wrapper, it's easy in Rust.

Follow this example https://github.com/gfx-rs/metal-rs

> CUDA There's a wrapper.

https://docs.rs/cuda/0.1.0/cuda/

> DriverKit

There's a wrapper https://github.com/dcuddeback/iokit-sys.

> Gtk-rs GTK is terrible, Gtk-rs is putting a lipstick on a pig. I need 2D rendering but also power to touch the gpu to write a shader, to use gl_VertexID when I feel like it.

The thing about rust is that that I can legit write my own bindings for any lib written in c/c++, anything else.

Like you go from zero to dependency included, building in less than 10sec. The build process is insane.

Check out this port of nanovg https://github.com/sunli829/nvg. NanoVG is a popular project with many contributors, this port is a single person project that's better in many ways.

With rust you literally just do

"git clone https://github.com/sunli829/nvg"

"cd nvg/nvg-gl"

"cargo run -p nvg-gl --example demo-cutout"

You go from git clone to drawing something interesting to screen in 10 seconds of human time (there might be some time spent building dependencies but whatever, I don't need to touch anything).


All those projects are toy examples, barely with any usable code on them.

As for compile speeds, I very much doubt it takes 10 seconds, given the current state of being forced to compile the world from scratch without any support for binary libraries and the continuous posts regarding compiler slowness.

If I want to draw a SVG, I just drop it into my application, no need to compile anything.

https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.med...


All right, same question: Is there a "Rust: the good bits" book that someone can recommend?


There is the Klabnik one from NoStarch and the Blandy one from O'Reilly. The first one is more towards beginners, the second one is pretty advanced, you build some relatively advanced things. I own both.

Part of the appeal of Rust is that there aren't as many bad parts as with C++.


These blog posts are golden, thanks. I definitely will peruse them when creating my language.


Does anyone know anything like this, but for an indentation-based language? Writing a Python-like parser was always hard for me, ending up with a generic INDENT/DEDENT counter mess.


You need to use the "lexer trick". While lexing your input data, instead of ignoring whitespace or producing token WS or TAB or whatever, keep track of your indents and newlines. Then it's simple to decide if it's an indent (newline followed by at least 1 more space or tab than previous, add this to your indents stack) or dedent (newline followed by the previous indent (look at your indent stack)). Then produce INDENT and DEDENT seperate tokens. Once you have these tokens produced by tokenizer, it's easy to parse. Do the usual recursive descent. Source: I built my own python compiler (python->C++) as a university projects.


Pretty much this. You need a stack because an single NEWLINE might result in 2 DEDENT tokens:

    if x:
      if y:
        f()
    g()
You only need to keep track of whitespace (spaces and/or tabs) after a newline (and you can skip lines with only whitespace & comments). It's also useful to keep tracks of delimiters (like parentheses and brackets), if you want to ignore newlines/indents inside expressions (like Python).

The easiest IMO is writing a lexer using a lexer generator (Lex, Flex, ...) and outputting a NEWLINE(spaces) tokens, then have another class that keeps track of indents, parentheses, etc.

I wrote a lexer/parser combo that (1) tracks whitespace in top (file) context, (2) ignores whitespace in expressions (parentheses, [] arrays, {} records), except (3) restarts indentation-tracking in {} blocks (even if they're in parentheses themselves!) - now that's hard to code (and debug LOL).


The trick used by some parsec parsers is interesting. Instead of trying to keep track of balanced indents and unindents, they just check the intent of the first token of a statement. If it's not the "right" indent, the parse fails. When that happens, it usually ends the current block and tries again with the next indent level. It's not that inefficient because the backtracking happens on the first token of the statement.

https://hackage.haskell.org/package/indents


Edward Kmett has a talk about parsing with monoids [0]. Somehow that seems an elegant way of doing layout based syntax. I have never done it myself, though.

[0] https://www.youtube.com/watch?v=Txf7swrcLYs



I wrote a simple excel formula parser a while ago, and I used a counter too -- just for parentheses. Your comment made me realize I might have been going about it the hard way.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: