Crust Programming Language
- 63Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is Crust, and what programming-language safety philosophy does it adopt?
63 questions
- 1
What is Crust, and what programming-language safety philosophy does it adopt?
Model answer
Crust is an unsafe variant of Rust designed to be more comfortable for C developers. It prioritizes programmer control and convenience over Rust's usual safety guarantees.
Explanation
The project treats Crust as a recreational language rather than a safety-oriented production language.
- 2
What are the main rules proposed for Crust?
Model answer
Every function is unsafe, references are replaced with pointers, and the standard library is avoided in favor of libc. A possible additional rule is making mutability the default.
Explanation
The first three rules are presented as the important core rules; default mutability is described as optional.
- 3
Why might Crust be attractive to C developers?
Model answer
Crust provides Rust's language environment while allowing pointer-based, explicitly unsafe programming and direct interaction with C libraries. This reduces restrictions that C developers may find inconvenient in ordinary Rust.
Explanation
The goal is to make Rust-like programming feel more familiar and usable to programmers accustomed to C.
- 4
Why is Crust not recommended for security-critical applications?
Model answer
Because it deliberately removes or bypasses many of Rust's safety protections, making memory and correctness errors easier to introduce.
Explanation
The project is intended mainly for recreational programming and experimentation.
- 5
What kinds of programs had already been implemented in Crust before the compiler project?
Model answer
Earlier experiments included a hello-world program, dynamic arrays, and calls to array-related functions.
Explanation
The B compiler was chosen as a more complex project than these initial demonstrations.
- 6
What is the relationship between the B and C programming languages?
Model answer
B was created by Ken Thompson before C, so C was named as the language that followed B alphabetically.
Explanation
B is presented as a predecessor or prequel to C.
- 7
Why is B described as a genuinely dead programming language?
Model answer
Unlike languages such as COBOL, B has no significant surviving legacy codebase that keeps it in practical use.
Explanation
The discussion distinguishes B from languages that remain active because organizations still depend on old programs written in them.
- 8
What makes implementing a compiler for B difficult?
Model answer
B has no known official compiler and apparently never had a formal language specification, so its behavior must be reconstructed from historical documentation.
Explanation
The available user reference and examples may also contain uncertainty because the document was digitized using OCR.
- 9
What type system does B use according to the reference discussed?
Model answer
B effectively uses one type: the machine word. Variables are therefore treated as word-sized values rather than being declared with different explicit types.
Explanation
The examples resemble C syntax but omit type declarations.
- 10
How are B programs described syntactically in comparison with C?
Model answer
B programs look broadly similar to C programs, but B omits explicit type specifications and uses machine words for variables.
Explanation
This similarity makes B a natural language to study when working with C-like compiler techniques.
- 11
What was the purpose of the B compiler project?
Model answer
The goal was to implement a small subset of B in Crust as a programming exercise, initially targeting one simple example rather than the complete language.
Explanation
The approach was incremental: compile one minimal program first, then add more features.
- 12
What does the B keyword
autorepresent?Model answer
It declares an automatic variable whose storage is allocated on the stack for a function invocation and released when the function exits.
Explanation
This is essentially the traditional stack-variable behavior that modern languages usually provide automatically.
- 13
Why is
autolargely redundant in modern C and C-like languages?Model answer
Modern languages normally allocate ordinary local variables on the stack automatically, so explicitly marking them as automatic often adds no information.
Explanation
In C++,
autolater acquired a different meaning related to type inference. - 14
What are the three storage classes described for B?
Model answer
They are automatic storage, external storage, and internal storage. Automatic storage is per invocation, external storage exists before execution and is shared across functions, and internal storage is local to a function but persists across its invocations.
Explanation
Internal storage corresponds roughly to the role of static local variables in C.
- 15
What is the purpose of an external declaration in B?
Model answer
It declares that named variables or functions use external storage and are defined elsewhere, allowing the linker to resolve them.
Explanation
External declarations must occur before the first use of the declared name.
- 16
What does an external declaration require regarding the declared entity?
Model answer
Each externally declared entity must also be defined externally, and the declaration must precede its first use.
Explanation
This separates informing the compiler about a name from providing the actual definition.
- 17
How are arrays represented in the B reference discussed?
Model answer
B supports arrays, although the reference uses a syntax whose exact details are uncertain because the source was processed with OCR.
Explanation
The apparent missing or unusual punctuation means the implementation must be checked against original source material.
- 18
What is the purpose of
putcharin the example B program?Model answer
putcharaccepts an integer and outputs the character associated with that integer's ASCII code.Explanation
The example assigns values to variables, adds them, and passes the result to
putchar. - 19
What simple B program was used as the initial compiler target?
Model answer
The program externally declares
putchar, assigns values to two automatic variables, adds them, and passes the result toputchar.Explanation
This small example tests declarations, assignments, arithmetic, and a function call.
- 20
Why was the compiler developed incrementally from one example?
Model answer
Starting with one very small program allows the compiler to implement only the features needed for that example, after which additional language features can be added gradually.
Explanation
This approach reduces the initial complexity of compiler development.
- 21
What are the usual major stages of compilation mentioned in the project?
Model answer
The compiler first reads the source file, tokenizes it, parses its structure, and generates lower-level code such as assembly.
Explanation
The project first implemented file reading and lexing before moving toward parsing and code generation.
- 22
What role does tokenization play in compilation?
Model answer
Tokenization converts the character sequence of a source file into meaningful tokens such as identifiers, literals, punctuation, and keywords.
Explanation
The B compiler used a lexer before attempting to parse declarations and statements.
- 23
What library was used to tokenize the B source?
Model answer
The project used the single-header C library
stb_c_lexer.Explanation
The lexer provides tokens, token values, and location information that the Crust compiler can consume through bindings.
- 24
What is a single-pass compiler?
Model answer
A single-pass compiler processes the source once and generates output as it encounters declarations and expressions, rather than building and revisiting a complete intermediate representation.
Explanation
B was considered suitable for this approach because the compiler could emit assembly while parsing.
- 25
How did the compiler distinguish among top-level B constructs?
Model answer
It examined the first token of a construct:
externindicated an external declaration,autoindicated an automatic variable, and an identifier indicated an assignment or function call.Explanation
This classification was sufficient for the small subset being implemented.
- 26
Why was a helper such as
get and expectuseful in the parser?Model answer
It combined retrieving the next token with checking whether it had the expected kind or value, simplifying repetitive parsing code.
Explanation
The implementation still kept token retrieval and expectation separate when lookahead or variable-length constructs required it.
- 27
What information should a compiler maintain for an automatic variable?
Model answer
It should record the variable's name and its offset relative to the function's stack frame.
Explanation
This information allows later assignments and reads to locate the variable in generated assembly.
- 28
Why must local-variable information be reset for each function?
Model answer
Local variables belong to individual function stack frames, so variables and stack offsets from one function must not be reused by another function.
Explanation
Sharing the same variable table across functions caused incorrect generated code until the state was reset per function.
- 29
How can a compiler detect a duplicate local-variable declaration?
Model answer
Before adding a new automatic variable, it searches the current function's variable table for the same name and reports an error if one is found.
Explanation
The implementation used a linear search because the variable set was small and a hash-table integration was not pursued.
- 30
How can a compiler report an undeclared-variable error?
Model answer
It searches the current variable table for the requested name and, if no match is found, reports an error using the token's source location.
Explanation
Location data improves diagnostics by identifying the relevant file position.
- 31
Why should lexer location queries be used selectively with the chosen lexer?
Model answer
The location-query function scans the source from the beginning, making it relatively slow. It is therefore suitable for errors but inefficient for every token.
Explanation
The project considered storing token positions during lexing as a possible optimization.
- 32
Why was a C implementation compiled separately rather than included directly in Crust?
Model answer
Crust could not directly compile the C source, so the C implementation was compiled into an object file and linked with the Crust object file.
Explanation
Crust then used manually written foreign-function bindings to access the C functions and structures.
- 33
What is the purpose of compiling a C source file with the compiler-only option?
Model answer
It produces an object file without attempting to link a final executable.
Explanation
The object file can later be linked with code generated from Crust.
- 34
What does a Crust foreign-function binding need to describe?
Model answer
It must describe the external function's calling convention, linked symbol name, parameter types, return type, and any compatible C data structures.
Explanation
Accurate bindings allow Crust code to call C functions safely at the ABI level, even though the operations remain unsafe.
- 35
Why was C representation specified for imported structures?
Model answer
C representation ensures that the Crust structure has the same field layout and ABI-compatible memory representation as the corresponding C structure.
Explanation
This is required when passing structures such as string builders or lexer state between C and Crust.
- 36
What was the purpose of the
link_nameattribute in the bindings?Model answer
It mapped a Crust declaration to the actual symbol name emitted by the C library, including names with library-specific prefixes.
Explanation
The source-level Crust name did not always match the linker symbol exactly.
- 37
How was an entire source file read into the compiler?
Model answer
A C helper function read the file into a string-builder structure, which was exposed to Crust through an external binding.
Explanation
The compiler then used the string builder's buffer and count as the lexer input.
- 38
What is a string builder in the supporting C library?
Model answer
It is a dynamically growing array that stores bytes or characters along with its current count and capacity.
Explanation
The compiler used it both for file contents and for lexer-related storage.
- 39
Why did the Crust code use explicit pointer operations when calling C functions?
Model answer
Crust follows the project's pointer-only rule and does not use ordinary Rust references, so C data is passed through raw mutable or constant pointers.
Explanation
This makes the code resemble low-level C and avoids the borrow-checking model.
- 40
What compiler configuration was needed to build a minimal Crust executable?
Model answer
The build disabled the standard library and normal entry-point handling, emitted an object file, and supplied a panic handler before linking the final executable.
Explanation
Without these components, the minimal Crust program could not link successfully.
- 41
Why was a panic handler required in the no-standard-library build?
Model answer
The compiler still requires a defined response if a panic occurs, even when the standard library is unavailable.
Explanation
A custom panic handler supplied the missing runtime behavior.
- 42
What was the purpose of a build file such as a Makefile in the project?
Model answer
It automated compiling Crust and C sources into object files and linking those objects into the final compiler executable.
Explanation
The build process involved multiple languages, libraries, compiler flags, and link stages.
- 43
What backend was chosen for generating machine-level output from B?
Model answer
The compiler generated FASM assembly for the x86-64 target.
Explanation
FASM was chosen because it offered a relatively direct and simple way to emit assembly.
- 44
What is the difference between producing an assembly object file and a final executable?
Model answer
Assembling an object file translates assembly into relocatable machine code, while linking combines object files and libraries and resolves external symbols into an executable.
Explanation
The project first generated assembly, assembled it, and then linked the resulting object.
- 45
Why was the C runtime linked with the generated B program?
Model answer
Linking the C runtime provided functions such as
putcharand supplied the normal program-startup and termination behavior.Explanation
Although the source language was B, B historically influenced C and could use C runtime facilities.
- 46
How does returning from
maininteract with the C runtime?Model answer
The C runtime receives the value returned by
mainas the program's exit status and performs the required termination handling.Explanation
This avoids manually issuing system calls for normal program termination.
- 47
Where does an x86-64 function return an integer value according to the calling convention used?
Model answer
An integer return value is placed in the
raxregister.Explanation
The generated assembly placed the desired exit code in
raxbefore returning. - 48
What is the purpose of the Procedure Linkage Table?
Model answer
The Procedure Linkage Table provides an indirection mechanism for calling externally linked functions, especially in position-independent or dynamically linked code.
Explanation
The project used the assembler's PLT mechanism to call an external C function.
- 49
What is position-independent code?
Model answer
Position-independent code can execute correctly regardless of the memory address at which it is loaded because it avoids fixed absolute addresses or accesses them through relocation mechanisms.
Explanation
The discussion connected position independence with PLT and GOT-based external symbol access.
- 50
What is the Global Offset Table used for?
Model answer
The Global Offset Table stores or helps locate addresses used by position-independent code, including addresses of external symbols.
Explanation
The assembler documentation described GOT-relative addressing as one way to create position-independent code.
- 51
Why was a function prologue needed for generated functions?
Model answer
The prologue saved the previous base pointer and established the current stack pointer as the new base pointer, creating a stable frame for local variables.
Explanation
Using
rbpas a frame reference prevents changes torspfrom invalidating local-variable offsets. - 52
Why is
rbpuseful for addressing local variables?Model answer
Unlike
rsp, which changes as values are pushed or stack space is allocated,rbpremains fixed for the function's frame and provides stable relative offsets.Explanation
The compiler stored each local variable at a known offset from
rbp. - 53
What did the generated code do for an automatic variable declaration?
Model answer
It decreased the stack pointer by one machine-word-sized slot and recorded the variable's name and offset for later access.
Explanation
The implementation used 64-bit words because it targeted x86-64, even though historical B used machine words of its own target system.
- 54
How was a variable assignment represented in generated assembly?
Model answer
The compiler loaded the assigned literal into a register or directly addressed it and stored it at the variable's offset relative to the function's base pointer.
Explanation
The initial subset supported assigning integer literals to local variables.
- 55
How was a variable passed as a function argument on x86-64?
Model answer
The compiler loaded the variable's value into the register required for the first integer argument,
rdi, and then emitted a call to the target function.Explanation
This follows the System V x86-64 calling convention used in the implementation.
- 56
What minimal subset of B was successfully compiled?
Model answer
The subset supported external declarations, automatic local variables, integer assignments, variable reads, function calls, and multiple functions.
Explanation
It did not yet implement the full B language, including broad expression support, conditionals, or loops.
- 57
How was successful compilation of the initial B program verified?
Model answer
The generated executable was run and printed the character whose ASCII code was the sum or assigned value produced by the program.
Explanation
For example, the value associated with ASCII
Ewas used as a simple observable test result. - 58
What limitation did the first function-call implementation have?
Model answer
It initially supported only a function call with one variable argument, and later it was extended to support calls with no arguments.
Explanation
Supporting arbitrary numbers of arguments would require additional parsing and register-assignment logic.
- 59
How was support for multiple functions added?
Model answer
An outer parsing loop was added for functions, while an inner loop continued to parse statements within each function.
Explanation
This separates function-level parsing from function-body parsing.
- 60
Why did compiling multiple functions initially produce incorrect behavior?
Model answer
The compiler reused one function's local-variable table and stack offset across all functions, causing variables from different functions to interfere.
Explanation
Resetting the local state for each function corrected the problem.
- 61
What caused recursive calls to overflow the stack?
Model answer
A function called itself without a terminating condition, so each call created another stack frame indefinitely until the available stack space was exhausted.
Explanation
The compiler was capable of generating recursive calls, but the source program still needed a base case.
- 62
Why was a linear array used to store local-variable records?
Model answer
The compiler's small subset had few variables, so a simple linear search was easier to implement than integrating a hash table.
Explanation
A hash table was considered through an external single-header C library but abandoned because its macro and type interactions were too time-consuming to bind.
- 63
What is the main lesson of the incremental compiler experiment?
Model answer
A compiler can be developed effectively by first translating a tiny, concrete program and then gradually adding language features, data structures, diagnostics, and code-generation cases.
Explanation
The resulting compiler was incomplete but already served as a foundation for future extensions.

Ask anything
We've read this whole study set, so questions about it get the best answers.