Live data from Hacker News

Writing a C compiler in 500 lines of Python (2023)

vgel.me

81–90 of 114 posts

Re: Writing a C compiler in 500 lines of Python (2023)

#81
post #73
post #12

Earlier quoted context omitted.

I could probably do it - but you wouldn't like it. My dictionaries would be a linked-list, looking for a key becomes a linear search... (if you gave me C++ I'd use std::map) I'm assuming you will allow me to use the C standard library, if I have to implement strlen or malloc in that 500 lines of C I'm not sure I can pull that off. 500 lines is aggressive, but IOCCC gives me plenty of tricks to get the line count down…

If you can search (and do other operations) on a linked list, and the lists are long enough to matter, you can trivially speed it up to a fixed-size hash table with hardly any code changes. This: entry = list_search(key, list); becomes: entry = list_search(key, lists[hash(key) % (sizeof(lists)/sizeof(lists[0]))]); This: list_add(key, entry, list); becomes: list_add(key, entry, lists[hash(key) % (sizeof(lists)/sizeof(…

Those would be useful optimizations once the simplest thing works. However the goal here is 500 lines- not fast, correct, maintainable code. If I was to write this for real world use I wouldn't start with C in the first place.

Re: Writing a C compiler in 500 lines of Python (2023)

#82

Earlier quoted context omitted.

A single-pass compiler is easier to implement in part because you're not going to do any of that optimization. You're writing a single-pass compiler either because you're banging out a quick sketch of an idea, and you don't care about production use, or because you've time-traveled back to the '70s or the '80s, where processors were so painfully slow and memory so eye-wateringly expensive that you might not even be a…

I used to write software on my HP calculator, when I was bored in classes, before smartphones existed. It used a tokenized language called RPL, and the editor would read and parse just enough tokens to fill up the screen. I miss how fast it was, compared to modern computers. VS Code is much laggier in comparison.

I used to dream about owning a programmable calculator in the late 70s/early 80s, but by 1980 we had Apple ][s in my high school and in 1982, I was able to use the PCs in college, first programming in BASIC and later Turbo Pascal, which was the best PC development tool in the 1980s. Most of my classes were in Pascal. My first job was in C++ and I was doing C++ by 1993 and still am.

Now I write Python for fun.

Re: Writing a C compiler in 500 lines of Python (2023)

#83
post #44

Earlier quoted context omitted.

"Generic functions" is the Common Lisp name for writing a separate method for each class, the same as in Python except that you also have to define the generic function itself before you can define the methods. I'm not sure if that's what you meant; the ML approach is quite different. This is Common Lisp, which I am not an expert in: ;;; Stupid CLOS example. (defgeneric x (point)) ; make X a method (defgeneric y (poi…

> I'm not sure if that's what you meant; the ML approach is quite different. There is a difference in approach because in Common Lisp each method is a separate function definition (though macros can alleviate this), but my point is that both CL and ML are more function-oriented, if you will; i.e. "methods" (or whatever you want to call ML pattern-matched functions) aren't defined in a class body and are just ordinary…

Oh, thanks! I didn't know that about defgeneric.

How would you classify Ruby? You can reopen a class and add more methods to it at any time.

Re: Writing a C compiler in 500 lines of Python (2023)

#84
post #55

After many years of programming in other languages, I finally learned C, and came to realize that there aren't actually any compilers that implement all of the C spec. Even GCC and Clang have their grey areas and their bugs. Before this, I had thought that C was a simple language. An idea propped up by articles likes this, as well as the oft touted fact that nearly every embedded system has a C compiler; no matter wh…

More real life C compilers is always a good thing. But remember: to be able to build a linux kernel, you will need zillions of gcc extensions... (I have a sweet spot for the alignment attribute of stack variables in printk). That said, "C" (C99 with benign bits of c11 required for modern hardware programming) is only the "less worse" compromise for a computer language: its syntax is already way too rich (I let you th…

> C should have only one loop keyword loop{}

How do you implement do while loops? Also I find the niceties of for loops a good improvement. You are able to limit the scope of variables to only the loop, while still being able to use it in the condition, and it separates the transition from the real loop code. I think idiomatic in C is the overuse of for, resulting in while used really seldomly.

What else do you think is really excessive in syntax alone?

Re: Writing a C compiler in 500 lines of Python (2023)

#85

Earlier quoted context omitted.

I think it depends on the language. I heard Turbo Pascal was pretty fast because 1) Pascal’s language features, 2) no optimization in TP 1.0 at least.

Fast to run or fast to compile?

Well given that often the alternative was BASIC which was interpreted, it also felt fast to run..

Re: Writing a C compiler in 500 lines of Python (2023)

#86

I thought I had learned a new word reading this, but instead I just have something that seems like it should be a word given the context it was discovered in. Perhaps that in itself should be considered cremement. A word that looks like it should be a word but isn't.

Huh? So what is the meaning of the word???

Re: Writing a C compiler in 500 lines of Python (2023)

#87
post #55

Earlier quoted context omitted.

More real life C compilers is always a good thing. But remember: to be able to build a linux kernel, you will need zillions of gcc extensions... (I have a sweet spot for the alignment attribute of stack variables in printk). That said, "C" (C99 with benign bits of c11 required for modern hardware programming) is only the "less worse" compromise for a computer language: its syntax is already way too rich (I let you th…

> C should have only one loop keyword loop{} How do you implement do while loops? Also I find the niceties of for loops a good improvement. You are able to limit the scope of variables to only the loop, while still being able to use it in the condition, and it separates the transition from the real loop code. I think idiomatic in C is the overuse of for, resulting in while used really seldomly. What else do you think…

> How do you implement do while loops?

    loop {
        ...body
        if (condition)
            break;
    }

Re: Writing a C compiler in 500 lines of Python (2023)

#88
post #87

Earlier quoted context omitted.

> C should have only one loop keyword loop{} How do you implement do while loops? Also I find the niceties of for loops a good improvement. You are able to limit the scope of variables to only the loop, while still being able to use it in the condition, and it separates the transition from the real loop code. I think idiomatic in C is the overuse of for, resulting in while used really seldomly. What else do you think…

> How do you implement do while loops? loop { ...body if (condition) break; }

Shouldn't your body be after the condition check?

Otherwise you get one iteration even if your condition was false to begin with?

Re: Writing a C compiler in 500 lines of Python (2023)

#89
post #88
post #87

Earlier quoted context omitted.

> How do you implement do while loops? loop { ...body if (condition) break; }

Shouldn't your body be after the condition check? Otherwise you get one iteration even if your condition was false to begin with?

I specifically asked for a do while loop.

Re: Writing a C compiler in 500 lines of Python (2023)

#90
post #87

Earlier quoted context omitted.

> C should have only one loop keyword loop{} How do you implement do while loops? Also I find the niceties of for loops a good improvement. You are able to limit the scope of variables to only the loop, while still being able to use it in the condition, and it separates the transition from the real loop code. I think idiomatic in C is the overuse of for, resulting in while used really seldomly. What else do you think…

> How do you implement do while loops? loop { ...body if (condition) break; }

What is the benefit over this:

    loop: {
        ...body
        if (!conditon)
            goto loop;
    }
What separates loop syntax from goto is explicit syntax for the condition. When you give that up, why do you have loop at all?
Post reply on HN