Live data from Hacker News

TinyCompiler: A compiler in a week-end

ssloy.github.io

51–60 of 120 posts

Re: TinyCompiler: A compiler in a week-end

#51
post #38
post #3

I don't think one can understand compilers in a "week-end".

All a compiler does is take a well defined data structure and transform it into another, sometimes by encoding or sometimes by decoding. Really it's bread and butter stuff that every engineer does everyday but with the benefit that you can say something is undefined/out of scope way easier than talking to APIs you don't control. The fun part are the magic algorithms you sometimes get to use

> All a compiler does is take a well defined data structure and transform it into another

Sorry, having worked on language semantics topics, "well-defined" is not how I would describe either a programming language or the behavior of machine code.

Re: TinyCompiler: A compiler in a week-end

#52
post #18

Earlier quoted context omitted.

The hardest part (IMHO) about getting a simple language running, particularly for beginners, is the “expression”. Mostly for your typical algebraic style infix expressions with precedence. Just getting the grammar straight on the naturally recursive structures can be a trick. They also touch a large portion of the code generation and run time. Get: (a + c/2) * sqrt(b) working and you’re 80% there.

Is that really the hard part? I figured that part out before I knew much programming: made a simple Excel type formula parser. (A horrible, horrible solution based around replacing strings and manually counting parentheses... but it worked!) I never got much farther than that though, I've looked into making a compiler many times and got overwhelmed every time.

It's hard if:

1. You dont recognise the recursive nature of the problem,

Or

2. You don't recognise the stack-based nature in the problem (shunting yard algo, etc).

IOW, you did it the hard way :-).

Re: TinyCompiler: A compiler in a week-end

#53
post #38

Earlier quoted context omitted.

All a compiler does is take a well defined data structure and transform it into another, sometimes by encoding or sometimes by decoding. Really it's bread and butter stuff that every engineer does everyday but with the benefit that you can say something is undefined/out of scope way easier than talking to APIs you don't control. The fun part are the magic algorithms you sometimes get to use

> All a compiler does is take a well defined data structure and transform it into another Sorry, having worked on language semantics topics, "well-defined" is not how I would describe either a programming language or the behavior of machine code.

There should be a well-defined list of undefined behaviors.

Re: TinyCompiler: A compiler in a week-end

#54
post #8

Earlier quoted context omitted.

Compilers are some of the simplest "complicated" programs out there if you start by throwing yacc/bison/antlr/parser generators in the garbage. Production compilers are complicated because of the feature set of languages and performance requirements. You can write a lexer + parser + treewalk interpreter for a simple language in a day if you know what you are doing.

The hardest part (IMHO) about getting a simple language running, particularly for beginners, is the “expression”. Mostly for your typical algebraic style infix expressions with precedence. Just getting the grammar straight on the naturally recursive structures can be a trick. They also touch a large portion of the code generation and run time. Get: (a + c/2) * sqrt(b) working and you’re 80% there.

Pratt parsers are a really nice way to handle mathematical expressions like this vs. the usual recursive descent.

https://journal.stuffwithstuff.com/2011/03/19/pratt-parsers-...

Re: TinyCompiler: A compiler in a week-end

#56
post #4

Only a happy customer, but if you want an amazing experience building a compiler I highly recommend David Beazley's Compiler Course (or anything else you can sign up for) https://www.dabeaz.com/

While I loved this week-long course, I was a bit disappointed that we offloaded all of the code generation to LLVM. I think it would be fantastic to stretch this into a two-week course and actually emit assembly.

When I took the course, each person did their own thing when it came to code generation. I don't recall anyone using LLVM. I think emitting assembly is easier than using LLVM.

I would love a a longer course.

Re: TinyCompiler: A compiler in a week-end

#58
post #39

Earlier quoted context omitted.

From wikipedia: "Pascal was influenced by the ALGOL W efforts, with the explicit goals of teaching programming in a structured fashion and for the development of system software.[5] A generation of students used Pascal as an introductory language in undergraduate courses." Its grammar made it relatively easy to write compilers for the language, which could be done as undergraduate exercises. Of course, this did not m…

I have the greatest respect for Prof. Kernighan, but history hasn't been kind to C's unbounded arrays and strings, however convenient they may be for the programmer. Moreover, just two years after his critique, Turbo Pascal would come out with an environment that is still revered as a pioneering and exceptionally productive IDE. It outsold C compilers by multiple orders of magnitude in the mid-1980s. (And that's igno…

His critice was already outdated by 1978, because Modula-2, Pascal's sucessor, explicitly designed for systems programming, after Niklaus Wirth sabatical year at Xerox PARC, where he learned about Mesa, Xerox Star written in Mesa, the IDE like experience of Tajo and XDE, he came back to ETHZ and created Modula-2, Lilith personal workstation and related OS in Modula-2.

Modula-2, designed for memory safe systems programming, in 1978, succedding Pascal, had support for unbounded arrays and strings.

By the way, GCC nowadays has GNU Modula-2 integrated in the official set of supported languages.

Re: TinyCompiler: A compiler in a week-end

#59
post #44

Earlier quoted context omitted.

I have the greatest respect for Prof. Kernighan, but history hasn't been kind to C's unbounded arrays and strings, however convenient they may be for the programmer. Moreover, just two years after his critique, Turbo Pascal would come out with an environment that is still revered as a pioneering and exceptionally productive IDE. It outsold C compilers by multiple orders of magnitude in the mid-1980s. (And that's igno…

I used to be a professional Delphi programmer, but I would not have described Delphi as "extremely successful", partially due to its mismanagement by the various owners of the IP. Its noticeable that Delphi's inventor went on to far greater success with a C-like language - C#.

It surely was all over the place in Europe, and still has some spots, hence why in Germany, you can keep coming to yearly Delphi conferences.

https://entwickler-konferenz.de/program-en/

Re: TinyCompiler: A compiler in a week-end

#60
post #22

On the same way than cproc+QBE I guess, 70% of gcc speed in my benchmarks. The more real-life alternatives to those abominations of gcc and clang, the merrier. I wish the linux kernel devs did care to keep the door _reasonably_ open for such compilers (with assembler source files as alternative to inline assembly, some extensions avoidance and niche expensive compiler features). This is another case of why super comp…

Circle frontend, that includes all C++17 (when it was current) and the Rust related extensions, was implemented by a single guy.
Post reply on HN