Skip to content

msakuta/rusty-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rusty-parser

A self-learning project for making a new language using Rust and nom.

Try it now on your browser! https://msakuta.github.io/rusty-parser/

Requirements

  • rust 1.44
  • npm 7.0.2 (for WebAssembly browser, any web server should work)

How to build

There are 2 ways to build this project.

  • Command line interpreter
  • WebAssembly browser application

Command line interpreter

One is for native command line application. It can read a text file, parse it to an AST and run it.

cd cli
cargo run --release -- <script-file>.dragon

You can type check the script before running with -t switch. It will ensure that the declared types are correct before running the script. The language itself is still dynamically typed, but it will help writing robust software.

cargo run --release -- -t

We have our own bytecode format that you can compile.

cargo run --release -- -c <script-file>.dragon

It will create an output file "out.cdragon" which is a pre-compiled bytecode that can run faster than AST interpreter. It is similart to ".pyc" file against ".py" in Python.

You can also compile and run at the same time.

cargo run --release -- -cR <script-file>.dragon

If you have a pre-compiled bytecode file, you can just run it without compiling as:

cargo run --release -- -b <bytecode>.cdragon

WebAssembly browser application

You can also build a wasm package and run the interpreter on the browser.

cd wasm
wasm-pack build --target web

To launch the application, you can use npx

npx serve

and browse http://localhost:5000.

TODOs

In ascending order of difficulty.

  • Arithmetic operators (+, -, *, /)
  • Functions, recursive calls
  • Loops (for, while, loop)
  • Proper expression statements (brace expressions)
  • Variable definition initializer
  • Type declaration
  • Primitive types (i32, i64, f32, f64)
  • String type
  • Logical operators (||, &&, !)
  • Bitwise operators (|, &, ^, ~)
  • String manipulations
  • Array types
  • WebAssembly build target
  • Function return types
  • Static type checking (instead of runtime coercion)
  • Type cast operator as
  • Named arguments in function calls
  • Default argument
  • Type casting in bytecode
  • Proper error handling
  • Tuple types
  • Multi-dimensional arrays
  • Function types (first class function variables)
  • Lambda expressions
  • Mutability qualifiers
  • Array slice syntax
  • Array shape constraints
  • Broadcasting operators
  • Custom operators
  • Run on VM (not directly on AST)
  • Compile to bytecode

Ideas

I want to make it a complementary DSL for data manipulation, such as numpy.

  • First-class array and matrix operations - from Matlab
  • Statically typed array shapes - from Futhark
  • Organized broadcasting operators (dot prefix) - from Julia
  • Adapt to LLVM backend to make it a native compiler

About

Learning project of parsers with Rust and nom

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages