Take your first step in writing a compiler. Implemented in Rust.

Overview

first-step-rust

Take your first step in writing a compiler, using Rust.

Building from Source

Make sure the Rust toolchain is installed on your computer, and then run:

$ git clone --recursive https://github.com/pku-minic/first-step-rust.git
$ cd first-step-rust
$ cargo build --release

Getting Started

There are some example first-step source programs in the examples directory, such as fib.fstep:

# calculate the nth term of the Fibonacci sequence
fib(n) {
  if n <= 2 {
    return 1
  }
  else {
    return fib(n - 1) + fib(n - 2)
  }
}

main() {
  print(fib(input()))
  return 0
}

You can evaluate this program using the interpreter by running:

$ cargo run --release -- examples/fib.fstep
20
6765

Or compile it to RISC-V assembly:

$ cargo run --release -- examples/fib.fstep -c -o out.S
$ cat out.S | less

EBNF of first-step

Program       ::= {FunctionDef};
FunctionDef   ::= IDENT "(" [ArgsDef] ")" Block;
ArgsDef       ::= IDENT {"," IDENT};

Block         ::= "{" {Statement} "}";
Statement     ::= IDENT ":=" Expression
                | IDENT "=" Expression
                | FunctionCall
                | IfElse
                | "return" Expression;
IfElse        ::= "if" Expression Block ["else" (IfElse | Block)];

Expression    ::= LOrExpr;
LOrExpr       ::= LAndExpr {"||" LAndExpr};
LAndExpr      ::= EqExpr {"&&" EqExpr};
EqExpr        ::= RelExpr {("==" | "!=") RelExpr};
RelExpr       ::= AddExpr {("<" | "<=") AddExpr};
AddExpr       ::= MulExpr {("+" | "-") MulExpr};
MulExpr       ::= UnaryExpr {("*" | "/" | "%") UnaryExpr};
UnaryExpr     ::= ["-" | "!"] Value;
Value         ::= INTEGER
                | IDENT
                | FunctionCall
                | "(" Expression ")";
FunctionCall  ::= IDENT "(" [Args] ")";
Args          ::= Expression {"," Expression};

License

Copyright (C) 2010-2021 MaxXing. License GPLv3.

You might also like...
Learn Rust by writing Entirely Too Many linked lists

Learn Rust by writing Entirely Too Many Linked Lists Read the pretty version at https://rust-unofficial.github.io/too-many-lists/. Building Building r

This project contains small exercises to get you used to reading and writing Rust code
This project contains small exercises to get you used to reading and writing Rust code

rustlings 🦀 ❤️ Greetings and welcome to rustlings. This project contains small exercises to get you used to reading and writing Rust code. This inclu

🕶 Assorted checks and validations for writing safer Solana programs.
🕶 Assorted checks and validations for writing safer Solana programs.

vipers 😎 Assorted checks and validations for writing safer Solana programs. Motivation Solana's fee mechanism is unlike Ethereum's, in that the numbe

A webring of people who make cool stuff. technology, music, art, writing, anything goes!
A webring of people who make cool stuff. technology, music, art, writing, anything goes!

a webring of people who make cool stuff. technology, music, art, writing, anything goes!

A language for writing swim practices.

swimscript A language for writing swim practices. Table of contents Purpose Language Spec Purpose The goal of swimscript is to create a universal lang

Thread-safe clone-on-write container for fast concurrent writing and reading.

sync_cow Thread-safe clone-on-write container for fast concurrent writing and reading. SyncCow is a container for concurrent writing and reading of da

The Rust Compiler Collection is a collection of compilers for various languages, written with The Rust Programming Language.

rcc The Rust Compiler Collection is a collection of compilers for various languages, written with The Rust Programming Language. Compilers Language Co

A additional Rust compiler pass to detect memory safe bugs of Rust programs.

SafeDrop A additional Rust compiler pass to detect memory safe bugs of Rust programs. SafeDrop performs path-sensitive and field-sensitive inter-proce

A minimal version of 'grep' implemented in Rust. Exercise in the "The Rust Programming Language" book.

Minigrep - A simple grep-like tool implemented in Rust This simple CLI tool searches for a given pattern in a specified file and as a result, it print

Owner
PKU Compiler Course
Learn the compiler step by step.
PKU Compiler Course
The first cesil transpiler/compiler in 50 years!

CesilC CESIL, or Computer Education in Schools Instruction Language, is a programming language designed to introduce pupils in British secondary schoo

null 5 Aug 6, 2022
Learn programming with Rust as a first language (book)

Learn programming with Rust as first language This is a book to learn programming from scratch. Read the book here: https://deavid.github.io/lprfl/ LI

David Martínez Martí 2 May 21, 2022
A rust crate can find first `Err` in `Iterator>` and iterating continuously, without allocation.

Api Document first-err Find the first Err in Iterator<Result<T, E>> and allow iterating continuously. This crate is specifically designed to replace t

null 3 Oct 28, 2023
Voluntarily contributed solutions for the first practical exercise for the Compilerbau lecture WiSe 2021/2022 of Ulm University

Solutions for the first practical exercise Compilerbau lecture WiSe 2021/2022 - Ulm University Contributing Want to add your solution? Great! Just add

null 5 Dec 4, 2021
Local-first linkspage generator

weird.one Local-first linkspage generator Usage You'll need Perseus and SurrealDB. Start the SurrealDB instance. surreal start -u root -p root --log d

Commune 9 Dec 16, 2022
The first compute-centric vector graphic video game

??️ Vong This repository contains source code for the first native use of a compute-centric vector graphics video game, inspired by Pong. ✍️ Authors @

Spencer C. Imbleau 29 Nov 24, 2023
:crab: Small exercises to get you used to reading and writing Rust code!

rustlings ?? ❤️ Greetings and welcome to rustlings. This project contains small exercises to get you used to reading and writing Rust code. This inclu

The Rust Programming Language 33.1k Jan 2, 2023
A Rust macro for writing nested loop expressions

loop_chain A Rust macro for writing nested loop expressions Usage | Examples | Docs Dependencies [dependencies] loop_chain = "0.1.1" Usage For express

Takayuki Maeda 5 Jul 30, 2021
S-expression parsing and writing in Rust

rsexp S-expression parsing and writing in Rust using nom parser combinators. This implemantion aims at being compatible with OCaml's sexplib. The main

Laurent Mazare 12 Oct 18, 2022
Writing Interpreters in Rust: a Guide

Writing Interpreters in Rust: a Guide This is an online book covering the lower level topics involved in writing an interpreter in Rust including: mem

Languages Hosted in Rust Working Groups 254 Jan 5, 2023