Byte by Byte › Book 8: Real-World Coding, Data and AI › Chapter 1: How Code Runs: Compilers, Interpreters and Paradigms
Chapter 1: How Code Runs: Compilers, Interpreters and Paradigms

🔎 The Case
The Makers' Summit opens in Cloudhall. Frost projects a list of problems onto the glass wall: Kachui's riverbank floods without warning, Simbha's Games booking desk is chaos, the Truth Desk is drowning in reports. Then Glim walks in, holding a scroll. "I wrote a program," he says, "in a language nobody else here uses. Your machines can't run it." Vega tilts her head. "Every language can run. You just need the right translator." The first job of the Summit: understand how code written by people becomes something a machine can execute.
💡 The Concepts
Machine code. A CPU only understands machine code: binary instructions specific to its type of processor (its instruction set). Writing in binary is slow and error-prone, so people write in high-level languages such as Python, JavaScript and Java, which use words and symbols close to English and maths. Assembly language sits in between: short codes such as MOV and ADD , one per machine instruction, converted by an assembler.
Translators. High-level code must be translated into machine code. There are two main ways:
| Compiler | Interpreter |
|---|
| How it works | Translates the whole program first, producing a separate executable file | Translates and runs one statement at a time |
| Speed of the finished program | Fast | Slower |
| Errors | Reports all errors after compiling; nothing runs until they are fixed | Stops at the first error it reaches |
| Need the source code to run? | No, only the executable | Yes, every time |
| Examples | C, C++, Go | Classic Python, JavaScript in older browsers |
Many modern languages mix both. Java is compiled into bytecode, a halfway code that the Java Virtual Machine (JVM) runs on any computer. This is why Java's motto is "write once, run anywhere". Python also compiles to bytecode behind the scenes before its interpreter runs it. Modern browsers use JIT (just-in-time) compilation to make JavaScript fast.
Kinds of errors.
- Syntax error: breaks the grammar of the language (a missing bracket). The translator catches it.
- Runtime error: happens while the program runs (dividing by zero, opening a missing file).
- Logic error: the program runs but gives the wrong answer. Only testing catches it.
Programming paradigms. A paradigm is a style of organising a program.
- Procedural: a sequence of steps grouped into procedures (functions). You did this in Books 6 and 7.
- Object-oriented (OOP): programs built from objects that bundle data and behaviour (Chapter 5).
- Event-driven: code waits and reacts to events such as clicks and key presses (Chapter 4; Scratch's "when this sprite clicked" in Book 4 was event-driven).
- Declarative: you describe what you want, not how (HTML, CSS and SQL are declarative).
Most real languages are multi-paradigm: Python and JavaScript support procedural, object-oriented and event-driven styles.
Real World: Your phone's apps are mostly compiled for its processor (ARM). The same Java or Kotlin code runs on thousands of different Android phones because of a virtual machine layer.
🛠️ Build It
- Open Google Colab, click New notebook, and run:
import dis
def add(a, b):
return a + b
dis.dis(add)
You are looking at Python bytecode: lines such as LOAD_FAST and BINARY_OP . This is what the Python interpreter actually runs.
- Make three errors on purpose and name each type:
print("Summit" # 1
print(10 / 0) # 2
average = 10 + 20 / 2 # 3: we wanted the average of 10 and 20
- Open Edge, press F12, choose Console, type
2 + 3 * 4and press Enter. You just ran JavaScript through the browser's built-in engine.
✅ Case Closed
Glim's scroll is written in an old language called Pascal. Frost finds a Pascal compiler in Cloudhall's archives, compiles the scroll into an executable, and it runs. Glim's program is a flood-gauge reader for Kachui's river. "I broke things for long enough," he mutters. "I want to build one." Memoon decides to give him a chance, and to test his code properly.
❓ Test Yourself
- Why can't a CPU run Python code directly?
- Give two differences between a compiler and an interpreter.
- What is bytecode, and what runs Java bytecode?
- Classify each: (a)
pritn("hi")(b) a program that calculates area as length + breadth (c) a program crashing when a file is missing. - Name the paradigm: (a) a button that plays a sound when clicked (b) an HTML page (c) a
Dogobject with abark()method. - Challenge: Fix the three errors in Build It step 2 so the program prints the correct average.
📚 Key Terms
machine code · instruction set · assembly language · assembler · high-level language · compiler · interpreter · executable · bytecode · Java Virtual Machine (JVM) · JIT compilation · syntax error · runtime error · logic error · paradigm · procedural · object-oriented · event-driven · declarative · multi-paradigm
Keep learning with Byte by Byte
Explore Book 8: Real-World Coding, Data and AI. Find the ebook, purchase details and teaching slides on MemuniCity.
Comments
Post a Comment