Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimenting with a version written in Rust #53

Open
wants to merge 85 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
af69d13
Stub out Rust compiler
captbaritone Apr 7, 2021
a51d274
Subtraction
captbaritone Apr 7, 2021
86e08d9
Add multiply
captbaritone Apr 7, 2021
4b3e192
Support division
captbaritone Apr 7, 2021
a1788c9
Stub out test that evaluates generated Wasm
captbaritone Apr 7, 2021
827e6e8
Allow dead opcodes
captbaritone Apr 7, 2021
1ce622e
Compile using parity-wasm
captbaritone Apr 7, 2021
1bbb745
Clean up some unused code
captbaritone Apr 7, 2021
32144b6
wasm-bindgen and compatibility test suite
captbaritone Apr 7, 2021
02c8c74
Hack in assignment
captbaritone Apr 7, 2021
7dfb6ad
Proper globals
captbaritone Apr 7, 2021
5efc6a5
Start to play with imported values
captbaritone Apr 7, 2021
51b520c
Multiple user defined functions
captbaritone Apr 7, 2021
8e033cf
Add function call int()
captbaritone Apr 7, 2021
e9d7012
Custom result types
captbaritone Apr 7, 2021
d4540f4
Improve error reporting
captbaritone Apr 7, 2021
7f422dc
Add snapshot tests
captbaritone Apr 7, 2021
e31466e
Add parser snapshot tests
captbaritone Apr 7, 2021
3fa446e
Clean up comment
captbaritone Apr 7, 2021
551b913
Add pools to programs
captbaritone Apr 7, 2021
298a64e
Add token snapshot tests
captbaritone Apr 7, 2021
2ac0297
Add Cargo tests to GitHub actions
captbaritone Apr 7, 2021
8d442b6
Set CI working directory
captbaritone Apr 7, 2021
10f54af
Another attempt at working directory
captbaritone Apr 7, 2021
d1a01a7
Shims
captbaritone Apr 7, 2021
c0fba27
Add support for reg/d/d
captbaritone Apr 7, 2021
b5add55
Build up a single instruction vec per program
captbaritone Apr 7, 2021
d4cb279
Improve naming
captbaritone Apr 7, 2021
d019ab7
Work toward getting shims working in tests
captbaritone Apr 7, 2021
bf74d5c
Get tests working for globals and shims
captbaritone Apr 7, 2021
f64de0e
Abstract test util and improved signature for globals
captbaritone Apr 7, 2021
210dc60
Support whitespace
captbaritone Apr 7, 2021
fbc044a
Support builtin functions (div)
captbaritone Apr 7, 2021
d24fef1
Improve naming
captbaritone Apr 7, 2021
bf0949b
Move variable access to prefix parsing
captbaritone Apr 7, 2021
74e1612
Unary expressions
captbaritone Apr 7, 2021
94680e2
Read global g in tests
captbaritone Apr 7, 2021
668b07a
Add basic semicolon support
captbaritone Apr 7, 2021
d94ddca
Spelling
captbaritone Apr 7, 2021
ca2b251
Fix unary precedence
captbaritone Apr 7, 2021
d9b67f9
Make variables case insensitive
captbaritone Apr 7, 2021
e91ffd3
Fix peek_prefix
captbaritone Apr 7, 2021
80b7a75
Decimals and parens
captbaritone Apr 7, 2021
a57659b
Support numbers on either side of decimal point
captbaritone Apr 7, 2021
cebbda5
Fix unary !
captbaritone Apr 7, 2021
892bd3b
Commets and lexer cleanup
captbaritone Apr 7, 2021
21a8450
Add expression blocks
captbaritone Apr 7, 2021
3b11134
Don't return from eel functions
captbaritone Apr 7, 2021
85117ad
Variable access
captbaritone Apr 7, 2021
e8e60f1
If function
captbaritone Apr 7, 2021
3a487b4
Allow reading (g)megabuf
captbaritone Apr 7, 2021
27ceca3
Split emitter in two
captbaritone Apr 7, 2021
8f9e9ef
==
captbaritone Apr 7, 2021
a3ceda7
Support mod
captbaritone Apr 7, 2021
e8a79ae
Bitwise operators
captbaritone Apr 7, 2021
b3326c9
Pow
captbaritone Apr 7, 2021
31c8d0f
More inlined functions
captbaritone Apr 7, 2021
95f25b9
Simplify return values
captbaritone Apr 7, 2021
4885f7d
More builtin functions
captbaritone Apr 9, 2021
12538df
Implement all shims
captbaritone Apr 9, 2021
9becdb7
sign()
captbaritone Apr 9, 2021
3835fed
Backslash comments
captbaritone Apr 9, 2021
e315708
Lex +=
captbaritone Apr 9, 2021
609b5dc
Document how to get gzipped size of wasm
captbaritone Apr 9, 2021
4cd01c5
Stub out logical and
captbaritone Apr 9, 2021
17fba38
Unify function index
captbaritone Apr 9, 2021
a376e45
Remove explicit offset
captbaritone Apr 9, 2021
beb437c
Setup the ability to call shims and builtins from bultins
captbaritone Apr 9, 2021
cabfcc3
Clean up builtin type gen
captbaritone Apr 9, 2021
aa2d2de
Move list of shims to shims enum
captbaritone Apr 9, 2021
cb94738
Less than, greater than
captbaritone Apr 9, 2021
4254060
<= >=
captbaritone Apr 9, 2021
08b085b
!=
captbaritone Apr 9, 2021
f7a0653
exec2/3
captbaritone Apr 9, 2021
1867147
While
captbaritone Apr 9, 2021
659f446
Loop
captbaritone Apr 9, 2021
e34468e
||
captbaritone Apr 9, 2021
ab876d6
Update assignment
captbaritone Apr 9, 2021
7b58b8a
Assign
captbaritone Apr 10, 2021
27fd103
Write to buffers
captbaritone Apr 10, 2021
e35acab
+= etc for buffer
captbaritone Apr 11, 2021
f0e9b0f
Enable passing test
captbaritone Apr 11, 2021
ccb5ad9
Clean up CLI
captbaritone Apr 11, 2021
afd7ae7
Allow function args to be expression blocks
captbaritone Apr 12, 2021
baf216c
Cleanup
captbaritone Apr 12, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Rust

on:
push:
branches: [ master, ci-execution ]
pull_request:
branches: [ master ]

env:
CARGO_TERM_COLOR: always

defaults:
run:
working-directory: ./compiler-rs

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Ensure formatted
run: cargo fmt --all -- --check
- name: Run Cargo tests
run: cargo test
11 changes: 11 additions & 0 deletions compiler-rs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
45 changes: 45 additions & 0 deletions compiler-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "eel_wasm"
version = "0.1.0"
authors = ["Jordan Eldredge <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
wasm-bindgen = "0.2.63"
parity-wasm = "0.42.2"
structopt = "0.3.21"
indexmap = "1.6.2"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.6", optional = true }

# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true }


[dev-dependencies]
# https://github.com/paritytech/wasmi/issues/252
# wasmi = { path = "../../wasmi"}
wasmi = { git = "https://github.com/paritytech/wasmi", branch = "master" }
wabt = "0.9.0"
wasm-bindgen-test = "0.3.13"
wasmprinter = "0.2.0"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

19 changes: 19 additions & 0 deletions compiler-rs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Build

To build the Wasm module:
```bash
wasm-pack build
```

You can find the output in `pkg/`.

To check the resulting wasm size
```bash
gzip -9 < pkg/optimized.wasm | wc -c
```

## TODO

- [ ] Add AST node for arguments list so that we can show it as the error node when arg count is wrong.
- [ ] Run `wasm-pack build` in CI.
- [ ] Should the magicness of reg10 values be case insensitive? (It is in the JS version)
102 changes: 102 additions & 0 deletions compiler-rs/src/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use crate::span::Span;

#[derive(Debug, PartialEq)]
pub struct EelFunction {
pub expressions: ExpressionBlock,
}

#[derive(Debug, PartialEq)]
pub struct ExpressionBlock {
pub expressions: Vec<Expression>,
}

#[derive(Debug, PartialEq)]
pub enum Expression {
BinaryExpression(BinaryExpression),
UnaryExpression(UnaryExpression),
NumberLiteral(NumberLiteral),
Assignment(Assignment),
FunctionCall(FunctionCall),
ExpressionBlock(ExpressionBlock),
Identifier(Identifier),
}

#[derive(Debug, PartialEq)]
pub struct UnaryExpression {
pub right: Box<Expression>,
pub op: UnaryOperator,
}

#[derive(Debug, PartialEq)]
pub struct BinaryExpression {
pub left: Box<Expression>,
pub right: Box<Expression>,
pub op: BinaryOperator,
}

#[derive(Debug, PartialEq)]
pub struct NumberLiteral {
pub value: f64,
}

#[derive(Debug, PartialEq)]
pub enum BinaryOperator {
Add,
Subtract,
Multiply,
Divide,
Mod,
Eq,
BitwiseAnd,
BitwiseOr,
LogicalAnd,
LogicalOr,
Pow,
LessThan,
GreaterThan,
LessThanEqual,
GreaterThanEqual,
NotEqual,
}

#[derive(Debug, PartialEq)]
pub enum UnaryOperator {
Plus,
Minus,
Not,
}

#[derive(Debug, PartialEq)]
pub struct Identifier {
pub name: String,
pub span: Span,
}

#[derive(Debug, PartialEq)]
pub enum AssignmentOperator {
Equal,
PlusEqual,
MinusEqual,
TimesEqual,
DivEqual,
ModEqual,
}

#[derive(Debug, PartialEq)]
pub struct Assignment {
pub left: AssignmentTarget,
pub operator: AssignmentOperator,
pub right: Box<Expression>,
}

#[derive(Debug, PartialEq)]
pub enum AssignmentTarget {
Identifier(Identifier),
FunctionCall(FunctionCall),
}

#[derive(Debug, PartialEq)]
pub struct FunctionCall {
pub name: Identifier,
pub arguments: Vec<Expression>,
}
52 changes: 52 additions & 0 deletions compiler-rs/src/bin/compiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use eel_wasm::compile;
use std::io::{self, Write};
use std::process;
use std::{collections::HashMap, fs};

use std::path::PathBuf;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(name = "eel-wasm", about = "Compile Eel code to WebAssembly.")]
struct Opt {
/// Input file
#[structopt(parse(from_os_str))]
input: PathBuf,

/// Output file, stdout if not present
#[structopt(parse(from_os_str))]
output: Option<PathBuf>,
}

fn main() {
let opt = Opt::from_args();
let filename = opt.input;
let source = fs::read_to_string(filename).unwrap_or_else(|err| {
eprintln!("Error reading file: {}", err);
process::exit(1);
});

let result = compile(
vec![("test".to_string(), &source, "pool".to_string())],
HashMap::default(),
)
.unwrap_or_else(|err| {
eprintln!("{:?}", err);
process::exit(1);
});

match opt.output {
Some(output) => {
fs::write(output, result).unwrap_or_else(|err| {
eprintln!("Error writing output: {}", err);
process::exit(1);
});
}
None => {
io::stdout().write_all(&result).unwrap_or_else(|err| {
eprintln!("Error writing to stdout: {}", err);
process::exit(1);
});
}
}
}
Loading