GithubHelp home page GithubHelp logo

rust-compiler's Introduction

Rust compiler

Description

This training project!

Compiler for the Rust written on C++. Tokenizing almost complete set of the Rust. Syntax analyzing a set described in comments in SyntaxParser.cpp. Semantic analyzing a almost complete subset of the implemented syntax (but not always correct in terms of Rust). Compiling a ultra small subset of the Rust (declaration variable with following types: i32, i64, f32 and f64. basic operations with this types, loop {}, while ... {}, if ... {} else if ... {} ... else {} and functions. there is a way for import external symbols using additional json file) into the WASM.

usage: .\rust-compiler.exe .\main.rs

input: main.rs, main.json

output: index.html and index.wasm

The index.html file contains the template for editing. See below for an example of the edited index.html.

example of main.rs:

fn calc_fib() -> i32 {
    let mut a = 1i32;
    let mut b = 1i32;
    let mut i = 2i32;

    while i != 22i32 {
        a += b;
        b = a - b;
        i += 1i32;
    }

    return a;
}

fn abs(x: f64) -> f64 {
    if x < 0f64 {
        return -x;
    }
    return x;
}

fn f(x: f64) -> f64 {
    return x * x;
}

fn int(mut from: f64, to: f64, frags: f64) -> f64 {
    let eps = 1e-6;

    let h = (to - from) / frags;
    let mut result = 0f64;
    while abs(from - to) >= eps {
        result += f(from) * h;
        from += h;
    }

    return result;
}

fn main() {
    print_f64(int(0f64, 2f64, 5000f64));
    print_i32(calc_fib());
}

example of main.json:

{
  "imports": [
    {
      "module": "imports",
      "field": "print_f64",
      "type": {
        "params": [ "f64" ],
        "return": []
      },
      "associate": "print_f64"
    },
    {
      "module": "imports",
      "field": "print_i32",
      "type": {
        "params": [ "i32" ],
        "return": []
      },
      "associate": "print_i32"
    }
  ],
  "exports": [
    {
      "field": "exported_func",
      "type": {
        "params": [],
        "return": []
      },
      "associate": "main"
    }
  ]
}

example of index.html:

<!doctype html>
<html>
  <body>
    <script>
      var importObject = {
          imports: {
              print_f64: (arg0) => { console.log(arg0); },
              print_i32: (arg0) => { console.log(arg0); },
          },
      };

      WebAssembly.instantiateStreaming(fetch('index.wasm'), importObject).then(obj => {
        obj.instance.exports.exported_func();
      });
    </script>
  </body>
</html>

License

This repository is licensed with the MIT license.

rust-compiler's People

Contributors

unkorunk avatar

Stargazers

 avatar  avatar  avatar  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.