3 ms·
C4 really has some good ideas by its own. * It never bothers to produce a machine code. The use of stack-based VM simplifies the single-pass compilation, and i
by lifthrasiir 7y ago
C4 really has some good ideas by its own.
* It never bothers to produce a machine code. The use of stack-based VM simplifies the single-pass compilation, and it never makes use of unknown library functions so no machine-specific knowledge is required (like dlsym in Bellard's OTCC [1]).
* It chose its primitives wisely. It never implements structs and returning with values, for example, but the code is carefully structured that the lack of them doesn't make it harder to read.
* And yet it has tons of little tricks. Switching from r-value to l-value (triggered when infix `=` or postfix `++`/`--` are read) is a single opcode fix. Reserved words are initialized from an imaginary source code. The type is represented by a single number 2n+k where n is the number of indirections.
[1] https://bellard.org/otcc/ https://bellard.org/otcc/
- ash 7y ago> Reserved words are initialized from an imaginary source code. What does it mean? Could you point to a place in the source?
- lifthrasiir 7y agoI mean this part: p = "char else enum if int return sizeof while " "open read close printf malloc free memset memcmp exit void main"; i = Char; while (i <= While) { next(); id[Tk] = i++; } // add keywords to symbol table i = OPEN; while (i <= EXIT) { next(); id[Class] = Sys; id[Type] = INT; id[Val] = i++; } // add library to symbol table next(); id[Tk] = Char; // handle void type next(); idmain = id; // keep track of main Throughout the entire source code p is a source code pointer, but at the very beginning of the program it is a string containing all reserved words and library functions, and they are read with the same lexing function `next` to the symbol table before the memory for the actual source code is allocated.