8000 GitHub - williamalexakis/phase: A statically-typed bytecode-interpreted programming language handwritten in C with zero dependencies. · GitHub
[go: up one dir, main page]

Skip to content

williamalexakis/phase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

110 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Phase

A statically-typed language bridging high-level clarity with low-level control



              

For detailed insight into the development of Phase, read my blog post.

Features

  • Supports functions, conditionals, loops (while), tuple destructuring.
  • 5 primitive types (str, int, float, bool, and void).
  • 21 error types with detailed diagnostics including visual markers and diff-style fix suggestions.
  • Implemented fully in C17.

Architecture

Interpreter Pipeline

flowchart LR
    A(Source Code) --> B(Lexer)
    B --> C(Parser)
    C --> D(Type Checker)
    D --> E(Bytecode Generator)
    E --> F(Virtual Machine)
Loading

Virtual Machine

Phase compiles programs into bytecode, executed by a handwritten stack-based VM supporting 25 opcodes.

So this source code:

entry {
    out("Hello world!")
}

Will compile into this bytecode:

00 00 00   ; OP_PUSH_CONST 0
01         ; OP_PRINT
18         ; OP_HALT

Installation

Prerequisites

  • CMake 3.20+
  • Compiler that supports C17

Building

  1. Clone the repo:

    git clone https://github.com/williamalexakis/phase.git
    cd phase
  2. Run the automated build script:

    Quick Build

    ./build.sh

    Run with --debug for the Debug Build or --run to immediately run after building.

    Or build it manually:

    mkdir build
    cd build
    cmake ..
    cmake --build .

Usage

Command Description
phase --help Display available commands and flags
phase <file.phase> --tokens Display token stream
phase <file.phase> --ast Display AST
phase <file.phase> --loud Display a success message when programs finish

Syntax Examples

Hello World

entry {
    out("Hello world!")
}

Variables

entry {
    let (var1, var2): str = ("Hello", "World")
    let var3: bool = true
    let var4: int = 1
    let var5: float = 1.0
}

Functions and Logic

func add(num1: int, num2: int): void {
    out(num1 + num2)
}

entry {
    add(5, 5)
}

Conditionals and Arithmetic

entry {
    let score: int = 9

    if score >= 10 {
        out("Excellent")
    } else if score > 5 {
        out("Good")
    } else {
        out("Bad")
    }
}

Loops (while)

entry {
    let counter: int = 0
    
    while counter < 11 {
        out(counter)
        counter += 1
    }
}

See examples/ for more

Diagnostics Examples

Error Reporting

Token Display

AST Display

Roadmap

  • Lexer
  • Parser
  • Bytecode generator
  • VM execution loop
  • Token and AST diagnostics
  • Floats and booleans
  • Error manager
  • Declaration keywords & annotations
  • Functions
  • Conditionals
  • Basic loops

License

Phase is released under the MIT License.

See the LICENSE for full details.

0