Skip to main content

MyLang — a statically-typed language that compiles to Python

Project description

MyLang Compiler

A statically-typed programming language that compiles to Python — built from scratch in Python with zero dependencies.

Install

pip install mylang-compiler

Quick Start

Create a file hello.myl:

program HelloWorld

  let name: text = "World"
  print "Hello, " + name + "!"

  thinkstep 2 ** 10

endprogram

Compile and run:

mylang hello.myl

Output:

Hello, World!
  [thinkstep] 2 ** 10  ==>  1024

CLI Flags

mylang hello.myl                  # compile + run (clean output)
mylang hello.myl --phases         # show all 7 compiler phases
mylang hello.myl --show-tokens    # Phase 1: token stream
mylang hello.myl --show-ast       # Phase 2: Abstract Syntax Tree
mylang hello.myl --show-symbols   # Phase 3: symbol table
mylang hello.myl --show-tac       # Phase 4: Three Address Code
mylang hello.myl --show-python    # Phase 5: generated Python source
mylang hello.myl --compile-only   # compile only, don't execute

The 7 Compiler Phases

Source (.myl)
    │
    ▼  Phase 1 — Lexical Analysis     tokens from raw text
    ▼  Phase 2 — Syntax Analysis      Abstract Syntax Tree
    ▼  Phase 3 — Semantic Analysis    type & scope checking
    ▼  Phase 4 — Intermediate Rep.    Three Address Code
    ▼  Phase 5 — Code Generation      emit Python source
    ▼  Phase 6 — Optimisation         constant folding, dead code
    ▼  Phase 7 — Execute              run via Python runtime

Language Features

Feature Syntax
Variable let age: num = 25
Function fun add(a: num, b: num) as num
If / else if x > 0 then ... else ... endif
For loop for i from 1 to 10 step 1
While while x > 0 begin ... end
Repeat N repeat 5 times begin ... end
Do-while do ... while cond enddo
Switch switch x begin case 1: ... endswitch
Struct struct Point begin x: num endstruct
Pointer let p: ptr = &x then *p = 10
Type check type(expression)
Debug step thinkstep expression

Run Without Installing

python -m mylang hello.myl

Built For

Compiler Design coursework. All 7 phases are in separate, self-contained files under mylang/. 36/36 tests passing across all phases.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mylang_compiler-1.0.0.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mylang_compiler-1.0.0-py3-none-any.whl (27.4 kB view details)

Uploaded Python 3

File details

Details for the file mylang_compiler-1.0.0.tar.gz.

File metadata

  • Download URL: mylang_compiler-1.0.0.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for mylang_compiler-1.0.0.tar.gz
Algorithm Hash digest
SHA256 83bba005bb653596e7e1b41fd34ccf943fc9a206a3bad4b45283e77a00a8e54f
MD5 fc4214617d33d660b460fec3b6fc8d34
BLAKE2b-256 59f06201f3fb1e643a7963ed4744adac239506236e64b7374ea133d32ebee975

See more details on using hashes here.

File details

Details for the file mylang_compiler-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mylang_compiler-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e4d5300ebf51208fb1e0b9273531295025cdfeb49c4213b779389d65aecc365
MD5 f10aa0c0af1b85689706da367fb27a6f
BLAKE2b-256 7cfdce0c1696e1f767e11aee376b9d34dce97f2edea79ffbd5e97fc710aec45e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page