Hasty Briefsbeta

Writing Micro Compiler in OCaml (2014)

5 days ago
  • #programming
  • #OCaml
  • #compiler
  • Many software developers aspire to create their own programming language.
  • Creating a compiler is complex and requires research, often starting with resources like 'Crafting Compilers in C'.
  • A simple micro language is described, which includes basic operations like assignment, read, and write.
  • The micro compiler is rewritten in OCaml, chosen for its clean syntax and functional programming features.
  • The compiler outputs NASM assembly code, which is then compiled into a binary executable.
  • The micro language supports integer variables, basic arithmetic operations, and simple I/O functions.
  • Parsing is done with an LL(1) parser, requiring only one character lookahead.
  • Utility functions for character and token handling are defined to aid in parsing.
  • The compiler includes semantic checking and generates assembly code for operations like addition and subtraction.
  • The final compiler can generate binary executables from source code written in the micro language.