Reading — step 1 of 5
Read
~1 min readType Checking & IR
Type Checking
After parsing, the compiler walks the AST and checks types.
Symbol table per scope:
python
Type system (simplified C):
python
Type checking rules:
- Variable use: look up in scope; type is the declared type.
- Function call: check arg count + types match; result is return type.
- Binary op
+: both sides must be int (or pointer + int for ptr arith); result is int. - Assignment: lhs must be lvalue; rhs type compatible.
- Pointer dereference
*p: p must be pointer; result is base type. - Array index
a[i]: a is array or pointer; i is integer; result is base type.
python
Implicit conversions in C:
char↔int: char promoted to int in arithmetic.int↔pointer: NOT (cast required, except 0 → null pointer).- arrays decay to pointers in most contexts.
Type errors caught here would be runtime crashes otherwise. Modern languages (Rust, Go) do MORE checking; old C is permissive.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…