Skip to content

ioangp/PyMor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

135 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyMor - Môr Compiler

PyMor is a small hobby compiler for the Môr programming language written in Python. The language aims to provide C-like power with a friendlier syntax and some safety-oriented features.

Môr currently compiles to C for portability and interoperability with existing C code.

This is a hobby project and is still experimental. The compiler is usable for small programs but the language and implementation may change frequently. Features are still being improved.

Example program

Hello world

def main(u0) -> i32:
    print("Hello world")
    return 0

Bubble sort

#include <stdio.h>
extern def printf(u8[], i32) -> u0

def swap(i32[] arr, u32 i, u32 j) -> u0:
    i32 temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp

def bubble_sort(i32[] arr, u32 n) -> u0:
    for u32 i = 0; i < n - 1; i += 1:
        for u32 j = 0; j < n - i - 1; j += 1:
            if arr[j] > arr[j + 1]:
                swap(arr, j, j+1)

def dont_sort(i32[] arr, u32 n) -> u0:
    return

typedef fn(i32[], u32) -> u0 SortFunction

def main(u0) -> i32:
    i32[] arr = [5, 6, 1, 3]
    i32 n = (sizeof arr) / (sizeof arr[0])

    ptr SortFunction sort_function = &bubble_sort

    sort_function(arr, n)

    for u32 i = 0; i < n; i += 1:
        printf("%d ", arr[i])

    return 0

Installation

Run as a Python module

No installation required beyond Python.

python -m mor [options] files

Build executable

python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt
make

The executable will be generated in dist/.

Usage

usage: python -m mor [options] files
options:
  -h, --help            Display help message.
  -o, --out OUT         Output filename.
  -I INCLUDES           File includes directory.
  --emit-c, -c          Emit generated c code.
  --ast                 Print a representation of the AST.
  --typed-ast           Print a representation of the AST with type information.
  --crash               Crash on error with full stack trace.
  --run-immediately     Immediately run generated executable.
  --no-runtime          Do not include the Môr runtime library (will break features).

OR with an executable

mor.exe [options] file
mor [options] file

Language

Môr is a small programming language inspired by C, HolyC, and (syntactically) Python. It aims to match C functionality but with a friendlier syntax, some convenient constructs, and some improved security. It compiles to C and is intended for easy interoperability with C via the extern keyword.

Language documentation

WIP

Known Limitations

  • Parser error recovery is incomplete
  • Error collection is not implemented in all compilation stages
  • Generated C code currently produces several compiler warnings

Roadmap

Compiler

  • Preprocessor
  • Lexing
  • Parsing
  • Semantic analysis
    • statements
    • expressions
  • Security pass
  • Code generation

Language Features

Definite - to match C

  • while
  • if/else
  • return
  • for
  • break/continue
  • pointers/arrays
  • functions
  • structs
  • sizeof
  • variadic functions
  • switch
  • ternary operator
  • union
  • enum

Potential

  • bool
  • struct literals
  • unsafe
  • optional
  • auto
  • for x in y
  • defer
  • assert
  • dynamic array (list)
  • slices
  • class

Repository Structure

mor/            Compiler implementation
mor-extension/  VS Code syntax highlighting extension
examples/       Example programs
tests/          Compiler tests

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors