Learn forward engineering
Learn C
Become language hacker
Become Reverse engineer
Single-pass compilers
Tree-walk interpreters
Transpilers
Just-in-time compilation
A High-Level Language
Dynamic typing
Automatic memory management
Closures
object oriented
Inheritance
typedef struct {
int count;
int capacity;
uint8_t* code;
} Chunk;
1.Allocate a new array with more capacity.
2.Copy the existing elements from the old array to the new one.
3.Store the new capacity.
4.Delete the old array.
5.Update code to point to the new array.
6.Store the element in the new array now that there is room.
7.Update the count.
converts Source code into tokens
static void repl() {
char line[1024];
for (;;) {
printf("> ");
if (!fgets(line, sizeof(line), stdin)) {
printf("\n");
break;
}
interpret(line);
}
}
int main(int argc, const char* argv[]) {
initVM();
if (argc == 1) {
repl();
} else if (argc == 2) {
runFile(argv[1]);
} else {
fprintf(stderr, "Usage: clox [path]\n");
exit(64);
}
freeVM();
return 0;
}
#include "common.h"
#include "scanner.h"
typedef struct {
const char* start;
const char* current;
int line;
} Scanner;
Scanner scanner;
#ifndef clox_vm_h
#define clox_vm_h
#include "chunk.h"
typedef struct {
Chunk* chunk;
uint8_t* ip;
Value stack[STACK_MAX];
Value* stackTop;
} VM;
void initVM();
void freeVM();
#endif
Single-Pass Compilation
It parses the user’s source code to understand what it means.Then it takes that knowledge and outputs low-level instructions that produce the same semantics
A parser produces an AST
then a code generator traverses the AST and outputs target code
Mark-Sweep Garbage Collection