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(…
Writing a C compiler in 500 lines of Python (2023)
81–90 of 114 posts
Re: Writing a C compiler in 500 lines of Python (2023)
#82Earlier 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.
Now I write Python for fun.
Re: Writing a C compiler in 500 lines of Python (2023)
#83Earlier 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…
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)
#84After 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…
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)
#85Earlier 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?
Re: Writing a C compiler in 500 lines of Python (2023)
#86I 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.
Re: Writing a C compiler in 500 lines of Python (2023)
#87Earlier 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…
loop {
...body
if (condition)
break;
}Re: Writing a C compiler in 500 lines of Python (2023)
#88Earlier 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; }
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)
#89Re: Writing a C compiler in 500 lines of Python (2023)
#90Earlier 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; }
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?