Giter Club home page Giter Club logo

compiler's Introduction

About

A simple toy compiler project.

How to build

To build this project, you'll need Rust version 1.72 or higher installed.

cargo build

Frontend

This frontend uses a language syntax similar to Rust and follows the operator precedence of C++. The language was designed mainly to avoid having to generate an intermediate representation (IR) by hand. There's no semantic analysis incorporated; it relies on the assumption that the provided code is error-free. Parsing is done using a simple recursive descent parser.

Code:

fn test(a: u32, b: u32) -> u32 {
    if (10 * 13) > b {
        for i in 0..10 {
            if i > 5 {
                a += i * b;
            } else {
                a += i * b * b;
            }
        }
    }

    return a;
}

AST:

Function {
  name: "test",
  ty: Function {
    ret: Builtin(
      U32,
    ),
    params: [
      FunctionParam {
        name: "a",
        param_ref: Val,
        ty: Builtin(
          U32,
        ),
      },
      FunctionParam {
        name: "b",
        param_ref: Val,
        ty: Builtin(
          U32,
        ),
      },
    ],
  },
  body: Block {
    nodes: [
      If {
        condition: Binary {
          lhs: Binary {
            lhs: Literal(
              10,
            ),
            op: Mul,
            rhs: Literal(
              13,
            ),
          },
          op: Greater,
          rhs: Identifier {
            name: "b",
          },
        },
        on_true: Block {
          nodes: [
            For {
              value: Identifier {
                name: "i",
              },
              range: Range {
                from: Literal(
                  0,
                ),
                to: Literal(
                  10,
                ),
              },
              body: Block {
                nodes: [
                  If {
                    condition: Binary {
                      lhs: Identifier {
                        name: "i",
                      },
                      op: Greater,
                      rhs: Literal(
                        5,
                      ),
                    },
                    on_true: Block {
                      nodes: [
                        CompoundAssign {
                          dst: Identifier {
                            name: "a",
                          },
                          op: Add,
                          src: Binary {
                            lhs: Identifier {
                              name: "i",
                            },
                            op: Mul,
                            rhs: Identifier {
                              name: "b",
                            },
                          },
                        },
                      ],
                    },
                    on_false: Some(
                      Block {
                        nodes: [
                          CompoundAssign {
                            dst: Identifier {
                              name: "a",
                            },
                            op: Add,
                            src: Binary {
                              lhs: Binary {
                                lhs: Identifier {
                                  name: "i",
                                },
                                op: Mul,
                                rhs: Identifier {
                                  name: "b",
                                },
                              },
                              op: Mul,
                              rhs: Identifier {
                                name: "b",
                              },
                            },
                          },
                        ],
                      },
                    ),
                  },
                ],
              },
            },
          ],
        },
        on_false: None,
      },
      Return {
        expr: Some(
          Identifier {
            name: "a",
          },
        ),
      },
    ],
  },
}

Backend

The compiler backend includes basic optimizations like constant folding, dead code elimination, and branch elimination.

The original, unoptimized code:

Unoptimized CFG

The control flow graph (CFG) after the currently implemented optimization passes:

Optimized CFG

compiler's People

Contributors

2aecfff4 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.