Skip to content

itxkarthik/tiny-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Tiny Compiler (Go)

A small educational compiler written in Go.

It reads a tiny Lisp-like input and converts it into a JavaScript-like function call output.

Example:

Input: (mix 4 (blend 9 1))

Output: mix(4, blend(9, 1));

Project Structure

  • compiler.go: Main compiler implementation and demo run in main()
  • compiler_test.go: Unit tests and benchmark for tokenizer/parser/compiler

Compiler Workflow

The compiler runs in four steps:

  1. Tokenizer
  • Converts source text into tokens such as paren, name, and number.
  1. Parser
  • Converts tokens into an AST (abstract syntax tree).
  1. Transformer
  • Converts the first AST shape into a JS-style AST shape.
  1. Code Generator
  • Converts the transformed AST into the final output string.

How To Run

From this folder:

go run compiler.go

How To Test

Run all tests with:

go test ./...

What To Learn From This Project

  • Basic compiler pipeline design
  • Recursive descent style parsing
  • AST traversal and transformation
  • Code generation from structured trees

Possible Next Improvements

  • Add string literals
  • Add better parser error messages for missing parentheses
  • Add support for uppercase letters in identifiers

About

Tiny educational compiler in Go that tokenizes, parses, transforms AST, and generates JavaScript-like code from a Lisp-style input.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages