Resolve merge conflict

This commit is contained in:
howardwu 2021-02-26 23:04:15 -08:00
commit 897a775a9c
368 changed files with 13483 additions and 5346 deletions

195
.circleci/config.yml Normal file
View File

@ -0,0 +1,195 @@
version: 2.1
commands:
setup_environment:
description: "Setup environment"
parameters:
cache_key:
type: string
default: leo-stable-cache
steps:
- run: set -e
- setup_remote_docker
- run:
name: Prepare environment and install dependencies
command: |
export SCCACHE_CACHE_SIZE=200M
export WORK_DIR="$CIRCLE_WORKING_DIRECTORY/.cache/sccache"
export SCCACHE_DIR="$CIRCLE_WORKING_DIRECTORY/.cache/sccache"
mkdir -p "$CIRCLE_WORKING_DIRECTORY/.bin"
wget https://github.com/mozilla/sccache/releases/download/0.2.13/sccache-0.2.13-x86_64-unknown-linux-musl.tar.gz
tar -C "$CIRCLE_WORKING_DIRECTORY/.bin" -xvf sccache-0.2.13-x86_64-unknown-linux-musl.tar.gz
mv $CIRCLE_WORKING_DIRECTORY/.bin/sccache-0.2.13-x86_64-unknown-linux-musl/sccache $CIRCLE_WORKING_DIRECTORY/.bin/sccache
export PATH="$PATH:$CIRCLE_WORKING_DIRECTORY/.bin"
export RUSTC_WRAPPER="sccache"
rm -rf "$CIRCLE_WORKING_DIRECTORY/.cargo/registry"
sudo apt-get update && sudo apt-get install -y clang llvm-dev llvm pkg-config xz-utils make libssl-dev libssl-dev
- restore_cache:
keys:
- << parameters.cache_key >>
clear_environment:
description: "Clear environment"
parameters:
cache_key:
type: string
default: leo-stable-cache
steps:
- run: (sccache -s||true)
- run: set +e
- save_cache:
key: << parameters.cache_key >>
paths:
- .cache/sccache
- .cargo
jobs:
rust-stable:
docker:
- image: cimg/rust:1.50.0
resource_class: xlarge
steps:
- checkout
- setup_environment:
cache_key: leo-stable-cache
- run:
name: Build and run tests
no_output_timeout: 30m
command: cargo install --path . --root .
- persist_to_workspace:
root: ~/
paths: project/
- clear_environment:
cache_key: leo-stable-cache
leo-new:
docker:
- image: cimg/rust:1.50.0
resource_class: xlarge
steps:
- attach_workspace:
at: /home/circleci/project/
- run:
name: leo new
command: |
export LEO=/home/circleci/project/project/bin/leo
./project/.circleci/leo-new.sh
leo-init:
docker:
- image: cimg/rust:1.50.0
resource_class: xlarge
steps:
- attach_workspace:
at: /home/circleci/project/
- run:
name: leo init
command: |
export LEO=/home/circleci/project/project/bin/leo
./project/.circleci/leo-init.sh
leo-clean:
docker:
- image: cimg/rust:1.50.0
resource_class: xlarge
steps:
- attach_workspace:
at: /home/circleci/project/
- run:
name: leo clean
command: |
export LEO=/home/circleci/project/project/bin/leo
./project/.circleci/leo-clean.sh
leo-setup:
docker:
- image: cimg/rust:1.50.0
resource_class: xlarge
steps:
- attach_workspace:
at: /home/circleci/project/
- run:
name: leo setup
command: |
export LEO=/home/circleci/project/project/bin/leo
./project/.circleci/leo-setup.sh
leo-add-remove:
docker:
- image: cimg/rust:1.50.0
resource_class: xlarge
steps:
- attach_workspace:
at: /home/circleci/project/
- run:
name: leo add & remove
command: |
export LEO=/home/circleci/project/project/bin/leo
./project/.circleci/leo-add-remove.sh
leo-login-logout:
docker:
- image: cimg/rust:1.50.0
resource_class: xlarge
steps:
- attach_workspace:
at: /home/circleci/project/
- run:
name: leo login & logout
command: |
export LEO=/home/circleci/project/project/bin/leo
./project/.circleci/leo-login-logout.sh
leo-clone:
docker:
- image: cimg/rust:1.50.0
resource_class: xlarge
steps:
- attach_workspace:
at: /home/circleci/project/
- run:
name: leo clone
command: |
export LEO=/home/circleci/project/project/bin/leo
./project/.circleci/leo-clone.sh
leo-publish:
docker:
- image: cimg/rust:1.50.0
resource_class: xlarge
steps:
- attach_workspace:
at: /home/circleci/project/
- run:
name: leo publish
command: |
export LEO=/home/circleci/project/project/bin/leo
./project/.circleci/leo-publish.sh
workflows:
version: 2
main-workflow:
jobs:
- rust-stable
- leo-new:
requires:
- rust-stable
- leo-init:
requires:
- rust-stable
- leo-clean:
requires:
- rust-stable
- leo-setup:
requires:
- rust-stable
- leo-add-remove:
requires:
- rust-stable
- leo-login-logout:
requires:
- rust-stable
- leo-clone:
requires:
- rust-stable
- leo-publish:
requires:
- rust-stable

6
.circleci/leo-add-remove.sh Executable file
View File

@ -0,0 +1,6 @@
# leo add (w/o login) & remove
$LEO new my-app && cd my-app
$LEO add howard/silly-sudoku
$LEO remove silly-sudoku
$LEO clean

34
.circleci/leo-clean.sh Executable file
View File

@ -0,0 +1,34 @@
# leo new hello-world
$LEO new hello-world
ls -la
cd hello-world && ls -la
$LEO run
# Assert that the 'outputs' folder is not empty
cd outputs || exit 1
if [ "$(ls -A $DIR)" ]; then
echo "$DIR is not empty"
else
echo "$DIR is empty"
exit 1
fi
cd ..
# leo clean
$LEO clean
cd outputs && ls -la
cd ..
# Assert that the 'outputs' folder is empty
cd outputs || exit 1
if [ "$(ls -A $DIR)" ]; then
echo "$DIR is not empty"
exit 1
else
echo "$DIR is empty"
exit 0
fi

18
.circleci/leo-clone.sh Executable file
View File

@ -0,0 +1,18 @@
# leo clone
# Clone the test-app package.
export PACKAGE="$ALEO_PM_USERNAME/test-app"
$LEO clone $PACKAGE
# Assert that the 'test-app' folder is not empty
cd test-app || exit 1
if [ "$(ls -A $DIR)" ]; then
echo "$DIR is not empty"
else
echo "$DIR is empty"
exit 1
fi
ls -la
$LEO run

4
.circleci/leo-init.sh Executable file
View File

@ -0,0 +1,4 @@
mkdir hello-world && cd hello-world || exit 1
$LEO init
ls -la
$LEO run

7
.circleci/leo-login-logout.sh Executable file
View File

@ -0,0 +1,7 @@
# leo login & logout
$LEO new my-app && cd my-app || exit 1
$LEO login -u "$ALEO_PM_USERNAME" -p "$ALEO_PM_PASSWORD"
$LEO add howard/silly-sudoku
$LEO remove silly-sudoku
$LEO logout

4
.circleci/leo-new.sh Executable file
View File

@ -0,0 +1,4 @@
$LEO new hello-world
ls -la
cd hello-world && ls -la
$LEO run

56
.circleci/leo-publish.sh Executable file
View File

@ -0,0 +1,56 @@
# leo login, publish and logout
# Login
$LEO login -u "$ALEO_PM_USERNAME" -p "$ALEO_PM_PASSWORD"
# Clone the test-app package.
export PACKAGE="$ALEO_PM_USERNAME/test-app"
$LEO clone $PACKAGE
cd test-app || exit 1
# Fetch the current Leo package version number.
#
# 1. Print out the Leo.toml file.
# 2. Search for a line with the word "version".
# 3. Isolate that into a single line.
# 4. Split the line from the '=' sign and keep the right-hand side.
# 5. Remove the quotes around the version number.
# 6. Trim any excess whitespace.
export CURRENT=$(cat Leo.toml \
| grep version \
| head -1 \
| awk -F= '{ print $2 }' \
| sed 's/[",]//g' \
| xargs)
# Increment the current Leo package version number by 1.
#
# 1. Print out the Leo.toml file.
# 2. Search for a line with the word "version".
# 3. Isolate that into a single line.
# 4. Split the line from the '=' sign and keep the right-hand side.
# 5. Remove the quotes around the version number.
# 6. Trim any excess whitespace.
# 7. Increment the version number by 1 (on the semver patch).
#
# https://stackoverflow.com/questions/8653126/how-to-increment-version-number-in-a-shell-script
export UPDATED=$(cat Leo.toml \
| grep version \
| head -1 \
| awk -F= '{ print $2 }' \
| sed 's/[",]//g' \
| xargs \
| awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print}')
# Write the updated Leo package version number to the Leo.toml file.
export TOML=$(cat Leo.toml | sed "s/$CURRENT/$UPDATED/g")
echo "$TOML" > Leo.toml
# Run the package to confirm the manifest remains well-formed.
$LEO run
# Publish the package to Aleo.pm
$LEO publish
# Logout
$LEO logout

7
.circleci/leo-setup.sh Executable file
View File

@ -0,0 +1,7 @@
# leo setup
cd ./project/examples/pedersen-hash || exit 1
$LEO setup
$LEO setup
$LEO setup --skip-key-check
$LEO clean

View File

@ -4,6 +4,8 @@ on:
push: push:
branches: branches:
- master - master
- staging
- trying
paths-ignore: paths-ignore:
- 'docs/**' - 'docs/**'
- 'documentation/**' - 'documentation/**'

View File

@ -1,43 +0,0 @@
name: leo-add-remove
on:
pull_request:
push:
branches:
- master
paths-ignore:
- 'docs/**'
- 'documentation/**'
env:
RUST_BACKTRACE: 1
jobs:
add:
name: Add Package ('leo add')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: rustfmt
- name: Install Leo
uses: actions-rs/cargo@v1
env:
CARGO_NET_GIT_FETCH_WITH_CLI: true
with:
command: install
args: --path .
- name: 'leo add (w/o login) & remove'
run: |
cd .. && leo new my-app && cd my-app
leo add argus4130/xnor
leo remove xnor
leo clean

View File

@ -1,76 +0,0 @@
name: leo-clean
on:
pull_request:
push:
branches:
- master
paths-ignore:
- 'docs/**'
- 'documentation/**'
env:
RUST_BACKTRACE: 1
jobs:
new:
name: Hello Leo ('leo new hello-world')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: rustfmt
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Install Leo
uses: actions-rs/cargo@v1
with:
command: install
args: --path .
- name: 'leo new hello-world'
run: |
cd ..
leo new hello-world
ls -la
cd hello-world && ls -la
leo run
- name: Assert that the 'outputs' folder is not empty
run: |
cd ../hello-world/outputs
if [ "$(ls -A $DIR)" ]; then
echo "$DIR is not empty"
else
echo "$DIR is empty"
exit 1
fi
- name: 'leo clean'
run: |
cd ../hello-world
leo clean
cd outputs && ls -la
- name: Assert that the 'outputs' folder is empty
run: |
cd ../hello-world/outputs
if [ "$(ls -A $DIR)" ]; then
echo "$DIR is not empty"
exit 1
else
echo "$DIR is empty"
exit 0
fi

View File

@ -1,48 +0,0 @@
name: leo-init
on:
pull_request:
push:
branches:
- master
paths-ignore:
- 'docs/**'
- 'documentation/**'
env:
RUST_BACKTRACE: 1
jobs:
init:
name: Hello Leo ('leo init')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: rustfmt
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Install Leo
uses: actions-rs/cargo@v1
with:
command: install
args: --path .
- name: 'leo init'
run: |
cd .. && mkdir hello-world && cd hello-world
leo init
ls -la
leo run

View File

@ -1,48 +0,0 @@
name: leo-login-logout
on:
pull_request:
push:
branches:
- master
paths-ignore:
- 'docs/**'
- 'documentation/**'
env:
RUST_BACKTRACE: 1
jobs:
add:
name: Add Package ('leo add')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: rustfmt
- name: Install Leo
uses: actions-rs/cargo@v1
env:
CARGO_NET_GIT_FETCH_WITH_CLI: true
with:
command: install
args: --path .
- name: 'leo login & logout'
env:
USER: ${{ secrets.ALEO_PM_USERNAME }}
PASS: ${{ secrets.ALEO_PM_PASSWORD }}
run: |
cd .. && leo new my-app && cd my-app
leo login -u "$USER" -p "$PASS"
leo add argus4130/xnor
leo remove xnor
leo clean
leo logout

View File

@ -1,49 +0,0 @@
name: leo-new
on:
pull_request:
push:
branches:
- master
paths-ignore:
- 'docs/**'
- 'documentation/**'
env:
RUST_BACKTRACE: 1
jobs:
new:
name: Hello Leo ('leo new hello-world')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: rustfmt
- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Install Leo
uses: actions-rs/cargo@v1
with:
command: install
args: --path .
- name: 'leo new hello-world'
run: |
cd ..
leo new hello-world
ls -la
cd hello-world && ls -la
leo run

View File

@ -1,47 +0,0 @@
name: leo-setup
on:
pull_request:
push:
branches:
- master
paths-ignore:
- 'docs/**'
- 'documentation/**'
env:
RUST_BACKTRACE: 1
jobs:
add:
name: Add Package ('leo add')
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
components: rustfmt
- name: Install Leo
uses: actions-rs/cargo@v1
env:
CARGO_NET_GIT_FETCH_WITH_CLI: true
with:
command: install
args: --path .
- name: 'leo setup for examples'
env:
USER: ${{ secrets.ALEO_PM_USERNAME }}
PASS: ${{ secrets.ALEO_PM_PASSWORD }}
run: |
cd examples/pedersen-hash
leo setup
leo setup
leo setup --skip-key-check
leo clean

6
.gitignore vendored
View File

@ -2,3 +2,9 @@
/tmp/ /tmp/
**.idea/ **.idea/
*.DS_Store *.DS_Store
**/process.yml
**/.crates.toml
**/.crates2.json
**/bin/

View File

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 29 KiB

View File

@ -1 +1 @@
v1.2.1 v1.2.3

88
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,88 @@
# Contributing
Thank you for your interest in contributing to Leo! Below you can find some guidelines that the project strives to follow.
## Pull requests
Please follow the instructions below when filing pull requests:
- Ensure that your branch is forked from the current [master](https://github.com/AleoHQ/leo/tree/master) branch.
- Fill out the provided markdown template for the feature or proposal. Be sure to link the pull request to any issues by using keywords. Example: "closes #130".
- Run `cargo fmt` before you commit; we use the `nightly` version of `rustfmt` to format the code, so you'll need to have the `nightly` toolchain installed on your machine; there's a [git hook](https://git-scm.com/docs/githooks) that ensures proper formatting before any commits can be made, and [`.rustfmt.toml`](https://github.com/AleoHQ/Leo/blob/master/.rustfmt.toml) specifies some of the formatting conventions.
- Run `cargo clippy` to ensure that popular correctness and performance pitfalls are avoided.
## Style
These guidelines ensure consistent readable rust code within the Leo repository.
### Comments
Prefer line comments (//) to block comments (/* ... */).
When using single-line block comments there should be a single space after the opening sigil and before the closing sigil. Multi-line block comments should have a newline after the opening sigil and before the closing sigil.
Prefer to put a comment on its own line. Where a comment follows code, there should be a single space before it. Where a block comment is inline, there should be surrounding whitespace as if it were an identifier or keyword. There should be no trailing whitespace after a comment or at the end of any line in a multi-line comment. Examples:
```rust
// A comment on an item.
struct Foo { ... }
fn foo() {} // A comment after an item.
pub fn foo(/* a comment before an argument */ x: T) {...}
```
Comments should be complete sentences. Start with a capital letter, end with a period (.). An inline block comment may be treated as a note without punctuation.
### Imports
* Stated at the top of the file
* Ordered alphabetically
* Split in two sections
* First party: crate imports + Aleo imports (example: snarkVM)
* Third party: rust std + everything else
Example:
```rust
use crate::Circuit;
use leo_ast::IntegerType;
use serde::Serialize;
use std::{
fmt,
sync::{Arc, Weak},
};
```
`rust fmt` should automatically sort imports alphabetically after they are split into the appropriate sections.
## Coding conventions
Leo is a big project, so (non-)adherence to best practices related to performance can have a considerable impact; below are the rules we try to follow at all times in order to ensure high quality of the code:
### Memory handling
- If the final size is known, pre-allocate the collections (`Vec`, `HashMap` etc.) using `with_capacity` or `reserve` - this ensures that there are both fewer allocations (which involve system calls) and that the final allocated capacity is as close to the required size as possible.
- Create the collections right before they are populated/used, as opposed to e.g. creating a few big ones at the beginning of a function and only using them later on; this reduces the amount of time they occupy memory.
- If an intermediate vector is avoidable, use an `Iterator` instead; most of the time this just amounts to omitting the call to `.collect()` if a single-pass iteraton follows afterwards, or returning an `impl Iterator<Item = T>` from a function when the caller only needs to iterate over that result once.
- When possible, fill/resize collections "in bulk" instead of pushing a single element in a loop; this is usually (but not always) detected by `clippy`, suggesting to create vectors containing a repeated value with `vec![x; N]` or extending them with `.resize(N, x)`.
- When a value is to eventually be consumed in a chain of function calls, pass it by value instead of by reference; this has the following benefits:
* It makes the fact that the value is needed by value clear to the caller, who can then potentially reclaim it from the object afterwards if it is "heavy", limiting allocations.
* It often enables the value to be cloned fewer times (whenever it's no longer needed at the callsite).
* When the value is consumed and is not needed afterwards, the memory it occupies is freed, improving memory utilization.
- If a slice may or may _not_ be extended (which requires a promotion to a vector) and does not need to be consumed afterwards, consider using a [`Cow<'a, [T]>`](https://doc.rust-lang.org/std/borrow/enum.Cow.html) combined with `Cow::to_mut` instead to potentially avoid an extra allocation; an example in Leo could be conditional padding of bits.
- Prefer arrays and temporary slices to vectors where possible; arrays are often a good choice if their final size is known in advance and isn't too great (as they are stack-bound), and a small temporary slice `&[x, y, z]` is preferable to a `vec![x, y, z]` if it's applicable.
- If a reference is sufficient, don't use `.clone()`/`to_vec()`, which is often the case with methods on `struct`s that provide access to their contents; if they only need to be referenced, there's no need for the extra allocation.
- Use `into_iter()` instead of `iter().cloned()` where possible, i.e. whenever the values being iterated over can be consumed altogether.
- If possible, reuse collections; an example would be a loop that needs a clean vector on each iteration: instead of creating and allocating it over and over, create it _before_ the loop and use `.clear()` on every iteration instead.
- Try to keep the sizes of `enum` variants uniform; use `Box<T>` on ones that are large.
### Misc. performance
- Avoid the `format!()` macro; if it is used only to convert a single value to a `String`, use `.to_string()` instead, which is also available to all the implementors of `Display`.
- Don't check if an element belongs to a map (using `contains` or `get`) if you want to conditionally insert it too, as the return value of `insert` already indicates whether the value was present or not; use that or the `Entry` API instead.
- If a reference is sufficient as a function parameter, use:
* `&[T]` instead of `&Vec<T>`
* `&str` instead of `&String`
* `&Path` instead of `&PathBuf`
- For `struct`s that can be compared/discerned based on some specific field(s), consider hand-written implementations of `PartialEq` **and** `Hash` ([they must match](https://doc.rust-lang.org/std/hash/trait.Hash.html#hash-and-eq)) for faster comparison and hashing.

805
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package] [package]
name = "leo-lang" name = "leo-lang"
version = "1.2.1" version = "1.2.3"
authors = [ "The Aleo Team <hello@aleo.org>" ] authors = [ "The Aleo Team <hello@aleo.org>" ]
description = "The Leo programming language" description = "The Leo programming language"
homepage = "https://aleo.org" homepage = "https://aleo.org"
@ -41,54 +41,60 @@ members = [
[dependencies.leo-ast] [dependencies.leo-ast]
path = "./ast" path = "./ast"
version = "1.2.1" version = "1.2.3"
[dependencies.leo-compiler] [dependencies.leo-compiler]
path = "./compiler" path = "./compiler"
version = "1.2.1" version = "1.2.3"
[dependencies.leo-gadgets] [dependencies.leo-gadgets]
path = "./gadgets" path = "./gadgets"
version = "1.2.1" version = "1.2.3"
[dependencies.leo-imports] [dependencies.leo-imports]
path = "./imports" path = "./imports"
version = "1.2.1" version = "1.2.3"
[dependencies.leo-input] [dependencies.leo-input]
path = "./input" path = "./input"
version = "1.2.1" version = "1.2.3"
[dependencies.leo-package] [dependencies.leo-package]
path = "./package" path = "./package"
version = "1.2.1" version = "1.2.3"
[dependencies.leo-state] [dependencies.leo-state]
path = "./state" path = "./state"
version = "1.2.1" version = "1.2.3"
[dependencies.snarkvm-algorithms] [dependencies.snarkvm-algorithms]
version = "0.0.2" version = "0.0.5"
default-features = false default-features = false
[dependencies.snarkvm-curves] [dependencies.snarkvm-curves]
version = "0.0.2" version = "0.0.5"
default-features = false default-features = false
[dependencies.snarkvm-errors] [dependencies.snarkvm-errors]
version = "0.0.2" version = "0.0.5"
default-features = false default-features = false
[dependencies.snarkvm-gadgets] [dependencies.snarkvm-gadgets]
version = "0.0.2" version = "0.0.5"
default-features = false default-features = false
[dependencies.snarkvm-models] [dependencies.snarkvm-models]
version = "0.0.2" version = "0.0.5"
default-features = false default-features = false
[dependencies.snarkvm-utilities] [dependencies.snarkvm-utilities]
version = "0.0.2" version = "0.0.5"
[dependencies.anyhow]
version = "1.0"
[dependencies.structopt]
version = "0.3"
[dependencies.clap] [dependencies.clap]
version = "2.33.3" version = "2.33.3"
@ -115,17 +121,17 @@ version = "4.0.15"
version = "0.3" version = "0.3"
[dependencies.rand] [dependencies.rand]
version = "0.7" version = "0.8"
[dependencies.rand_core] [dependencies.rand_core]
version = "0.6.1" version = "0.6.2"
[dependencies.reqwest] [dependencies.reqwest]
version = "0.11.0" version = "0.11.1"
features = [ "blocking", "json", "multipart" ] features = [ "blocking", "json", "multipart" ]
[dependencies.self_update] [dependencies.self_update]
version = "0.23.0" version = "0.25.0"
features = [ "archive-zip" ] features = [ "archive-zip" ]
[dependencies.serde] [dependencies.serde]

31
DEVELOPMENT.md Normal file
View File

@ -0,0 +1,31 @@
# Development Guide
## Running CircleCI locally
### Step 1: Install CircleCI
If you wish to run CircleCI locally, start by installing it:
- macOS
```
brew install circleci
```
- Linux (via Snap)
```
sudo snap install docker circleci
sudo snap connect circleci:docker docker
```
- Windows (via Chocolatey)
```
choco install circleci-cli -y
```
### Step 2: Run CircleCI
To run a job, export the config to `process.yml`, and specify it when executing:
```shell
circleci config process .circleci/config.yml > process.yml
circleci local execute -c process.yml --job JOB_NAME
```

596
LICENSE.md Normal file
View File

@ -0,0 +1,596 @@
GNU General Public License
==========================
Version 3, 29 June 2007
Copyright © 2007 Free Software Foundation, Inc. &lt;<https://fsf.org/>&gt;
Everyone is permitted to copy and distribute verbatim copies of this license
document, but changing it is not allowed.
## Preamble
The GNU General Public License is a free, copyleft license for software and other
kinds of works.
The licenses for most software and other practical works are designed to take away
your freedom to share and change the works. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change all versions of a
program--to make sure it remains free software for all its users. We, the Free
Software Foundation, use the GNU General Public License for most of our software; it
applies also to any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not price. Our General
Public Licenses are designed to make sure that you have the freedom to distribute
copies of free software (and charge for them if you wish), that you receive source
code or can get it if you want it, that you can change the software or use pieces of
it in new free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you these rights or
asking you to surrender the rights. Therefore, you have certain responsibilities if
you distribute copies of the software, or if you modify it: responsibilities to
respect the freedom of others.
For example, if you distribute copies of such a program, whether gratis or for a fee,
you must pass on to the recipients the same freedoms that you received. You must make
sure that they, too, receive or can get the source code. And you must show them these
terms so they know their rights.
Developers that use the GNU GPL protect your rights with two steps: **(1)** assert
copyright on the software, and **(2)** offer you this License giving you legal permission
to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains that there is
no warranty for this free software. For both users' and authors' sake, the GPL
requires that modified versions be marked as changed, so that their problems will not
be attributed erroneously to authors of previous versions.
Some devices are designed to deny users access to install or run modified versions of
the software inside them, although the manufacturer can do so. This is fundamentally
incompatible with the aim of protecting users' freedom to change the software. The
systematic pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we have designed
this version of the GPL to prohibit the practice for those products. If such problems
arise substantially in other domains, we stand ready to extend this provision to
those domains in future versions of the GPL, as needed to protect the freedom of
users.
Finally, every program is threatened constantly by software patents. States should
not allow patents to restrict development and use of software on general-purpose
computers, but in those that do, we wish to avoid the special danger that patents
applied to a free program could make it effectively proprietary. To prevent this, the
GPL assures that patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and modification follow.
## TERMS AND CONDITIONS
### 0. Definitions
“This License” refers to version 3 of the GNU General Public License.
“Copyright” also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
“The Program” refers to any copyrightable work licensed under this
License. Each licensee is addressed as “you”. “Licensees” and
“recipients” may be individuals or organizations.
To “modify” a work means to copy from or adapt all or part of the work in
a fashion requiring copyright permission, other than the making of an exact copy. The
resulting work is called a “modified version” of the earlier work or a
work “based on” the earlier work.
A “covered work” means either the unmodified Program or a work based on
the Program.
To “propagate” a work means to do anything with it that, without
permission, would make you directly or secondarily liable for infringement under
applicable copyright law, except executing it on a computer or modifying a private
copy. Propagation includes copying, distribution (with or without modification),
making available to the public, and in some countries other activities as well.
To “convey” a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through a computer
network, with no transfer of a copy, is not conveying.
An interactive user interface displays “Appropriate Legal Notices” to the
extent that it includes a convenient and prominently visible feature that **(1)**
displays an appropriate copyright notice, and **(2)** tells the user that there is no
warranty for the work (except to the extent that warranties are provided), that
licensees may convey the work under this License, and how to view a copy of this
License. If the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
### 1. Source Code
The “source code” for a work means the preferred form of the work for
making modifications to it. “Object code” means any non-source form of a
work.
A “Standard Interface” means an interface that either is an official
standard defined by a recognized standards body, or, in the case of interfaces
specified for a particular programming language, one that is widely used among
developers working in that language.
The “System Libraries” of an executable work include anything, other than
the work as a whole, that **(a)** is included in the normal form of packaging a Major
Component, but which is not part of that Major Component, and **(b)** serves only to
enable use of the work with that Major Component, or to implement a Standard
Interface for which an implementation is available to the public in source code form.
A “Major Component”, in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system (if any) on which
the executable work runs, or a compiler used to produce the work, or an object code
interpreter used to run it.
The “Corresponding Source” for a work in object code form means all the
source code needed to generate, install, and (for an executable work) run the object
code and to modify the work, including scripts to control those activities. However,
it does not include the work's System Libraries, or general-purpose tools or
generally available free programs which are used unmodified in performing those
activities but which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for the work, and
the source code for shared libraries and dynamically linked subprograms that the work
is specifically designed to require, such as by intimate data communication or
control flow between those subprograms and other parts of the work.
The Corresponding Source need not include anything that users can regenerate
automatically from other parts of the Corresponding Source.
The Corresponding Source for a work in source code form is that same work.
### 2. Basic Permissions
All rights granted under this License are granted for the term of copyright on the
Program, and are irrevocable provided the stated conditions are met. This License
explicitly affirms your unlimited permission to run the unmodified Program. The
output from running a covered work is covered by this License only if the output,
given its content, constitutes a covered work. This License acknowledges your rights
of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not convey, without
conditions so long as your license otherwise remains in force. You may convey covered
works to others for the sole purpose of having them make modifications exclusively
for you, or provide you with facilities for running those works, provided that you
comply with the terms of this License in conveying all material for which you do not
control copyright. Those thus making or running the covered works for you must do so
exclusively on your behalf, under your direction and control, on terms that prohibit
them from making any copies of your copyrighted material outside their relationship
with you.
Conveying under any other circumstances is permitted solely under the conditions
stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
### 3. Protecting Users' Legal Rights From Anti-Circumvention Law
No covered work shall be deemed part of an effective technological measure under any
applicable law fulfilling obligations under article 11 of the WIPO copyright treaty
adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention
of such measures.
When you convey a covered work, you waive any legal power to forbid circumvention of
technological measures to the extent such circumvention is effected by exercising
rights under this License with respect to the covered work, and you disclaim any
intention to limit operation or modification of the work as a means of enforcing,
against the work's users, your or third parties' legal rights to forbid circumvention
of technological measures.
### 4. Conveying Verbatim Copies
You may convey verbatim copies of the Program's source code as you receive it, in any
medium, provided that you conspicuously and appropriately publish on each copy an
appropriate copyright notice; keep intact all notices stating that this License and
any non-permissive terms added in accord with section 7 apply to the code; keep
intact all notices of the absence of any warranty; and give all recipients a copy of
this License along with the Program.
You may charge any price or no price for each copy that you convey, and you may offer
support or warranty protection for a fee.
### 5. Conveying Modified Source Versions
You may convey a work based on the Program, or the modifications to produce it from
the Program, in the form of source code under the terms of section 4, provided that
you also meet all of these conditions:
* **a)** The work must carry prominent notices stating that you modified it, and giving a
relevant date.
* **b)** The work must carry prominent notices stating that it is released under this
License and any conditions added under section 7. This requirement modifies the
requirement in section 4 to “keep intact all notices”.
* **c)** You must license the entire work, as a whole, under this License to anyone who
comes into possession of a copy. This License will therefore apply, along with any
applicable section 7 additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no permission to license the
work in any other way, but it does not invalidate such permission if you have
separately received it.
* **d)** If the work has interactive user interfaces, each must display Appropriate Legal
Notices; however, if the Program has interactive interfaces that do not display
Appropriate Legal Notices, your work need not make them do so.
A compilation of a covered work with other separate and independent works, which are
not by their nature extensions of the covered work, and which are not combined with
it such as to form a larger program, in or on a volume of a storage or distribution
medium, is called an “aggregate” if the compilation and its resulting
copyright are not used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work in an aggregate
does not cause this License to apply to the other parts of the aggregate.
### 6. Conveying Non-Source Forms
You may convey a covered work in object code form under the terms of sections 4 and
5, provided that you also convey the machine-readable Corresponding Source under the
terms of this License, in one of these ways:
* **a)** Convey the object code in, or embodied in, a physical product (including a
physical distribution medium), accompanied by the Corresponding Source fixed on a
durable physical medium customarily used for software interchange.
* **b)** Convey the object code in, or embodied in, a physical product (including a
physical distribution medium), accompanied by a written offer, valid for at least
three years and valid for as long as you offer spare parts or customer support for
that product model, to give anyone who possesses the object code either **(1)** a copy of
the Corresponding Source for all the software in the product that is covered by this
License, on a durable physical medium customarily used for software interchange, for
a price no more than your reasonable cost of physically performing this conveying of
source, or **(2)** access to copy the Corresponding Source from a network server at no
charge.
* **c)** Convey individual copies of the object code with a copy of the written offer to
provide the Corresponding Source. This alternative is allowed only occasionally and
noncommercially, and only if you received the object code with such an offer, in
accord with subsection 6b.
* **d)** Convey the object code by offering access from a designated place (gratis or for
a charge), and offer equivalent access to the Corresponding Source in the same way
through the same place at no further charge. You need not require recipients to copy
the Corresponding Source along with the object code. If the place to copy the object
code is a network server, the Corresponding Source may be on a different server
(operated by you or a third party) that supports equivalent copying facilities,
provided you maintain clear directions next to the object code saying where to find
the Corresponding Source. Regardless of what server hosts the Corresponding Source,
you remain obligated to ensure that it is available for as long as needed to satisfy
these requirements.
* **e)** Convey the object code using peer-to-peer transmission, provided you inform
other peers where the object code and Corresponding Source of the work are being
offered to the general public at no charge under subsection 6d.
A separable portion of the object code, whose source code is excluded from the
Corresponding Source as a System Library, need not be included in conveying the
object code work.
A “User Product” is either **(1)** a “consumer product”, which
means any tangible personal property which is normally used for personal, family, or
household purposes, or **(2)** anything designed or sold for incorporation into a
dwelling. In determining whether a product is a consumer product, doubtful cases
shall be resolved in favor of coverage. For a particular product received by a
particular user, “normally used” refers to a typical or common use of
that class of product, regardless of the status of the particular user or of the way
in which the particular user actually uses, or expects or is expected to use, the
product. A product is a consumer product regardless of whether the product has
substantial commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
“Installation Information” for a User Product means any methods,
procedures, authorization keys, or other information required to install and execute
modified versions of a covered work in that User Product from a modified version of
its Corresponding Source. The information must suffice to ensure that the continued
functioning of the modified object code is in no case prevented or interfered with
solely because modification has been made.
If you convey an object code work under this section in, or with, or specifically for
use in, a User Product, and the conveying occurs as part of a transaction in which
the right of possession and use of the User Product is transferred to the recipient
in perpetuity or for a fixed term (regardless of how the transaction is
characterized), the Corresponding Source conveyed under this section must be
accompanied by the Installation Information. But this requirement does not apply if
neither you nor any third party retains the ability to install modified object code
on the User Product (for example, the work has been installed in ROM).
The requirement to provide Installation Information does not include a requirement to
continue to provide support service, warranty, or updates for a work that has been
modified or installed by the recipient, or for the User Product in which it has been
modified or installed. Access to a network may be denied when the modification itself
materially and adversely affects the operation of the network or violates the rules
and protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided, in accord with
this section must be in a format that is publicly documented (and with an
implementation available to the public in source code form), and must require no
special password or key for unpacking, reading or copying.
### 7. Additional Terms
“Additional permissions” are terms that supplement the terms of this
License by making exceptions from one or more of its conditions. Additional
permissions that are applicable to the entire Program shall be treated as though they
were included in this License, to the extent that they are valid under applicable
law. If additional permissions apply only to part of the Program, that part may be
used separately under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option remove any
additional permissions from that copy, or from any part of it. (Additional
permissions may be written to require their own removal in certain cases when you
modify the work.) You may place additional permissions on material, added by you to a
covered work, for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you add to a
covered work, you may (if authorized by the copyright holders of that material)
supplement the terms of this License with terms:
* **a)** Disclaiming warranty or limiting liability differently from the terms of
sections 15 and 16 of this License; or
* **b)** Requiring preservation of specified reasonable legal notices or author
attributions in that material or in the Appropriate Legal Notices displayed by works
containing it; or
* **c)** Prohibiting misrepresentation of the origin of that material, or requiring that
modified versions of such material be marked in reasonable ways as different from the
original version; or
* **d)** Limiting the use for publicity purposes of names of licensors or authors of the
material; or
* **e)** Declining to grant rights under trademark law for use of some trade names,
trademarks, or service marks; or
* **f)** Requiring indemnification of licensors and authors of that material by anyone
who conveys the material (or modified versions of it) with contractual assumptions of
liability to the recipient, for any liability that these contractual assumptions
directly impose on those licensors and authors.
All other non-permissive additional terms are considered “further
restrictions” within the meaning of section 10. If the Program as you received
it, or any part of it, contains a notice stating that it is governed by this License
along with a term that is a further restriction, you may remove that term. If a
license document contains a further restriction but permits relicensing or conveying
under this License, you may add to a covered work material governed by the terms of
that license document, provided that the further restriction does not survive such
relicensing or conveying.
If you add terms to a covered work in accord with this section, you must place, in
the relevant source files, a statement of the additional terms that apply to those
files, or a notice indicating where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the form of a
separately written license, or stated as exceptions; the above requirements apply
either way.
### 8. Termination
You may not propagate or modify a covered work except as expressly provided under
this License. Any attempt otherwise to propagate or modify it is void, and will
automatically terminate your rights under this License (including any patent licenses
granted under the third paragraph of section 11).
However, if you cease all violation of this License, then your license from a
particular copyright holder is reinstated **(a)** provisionally, unless and until the
copyright holder explicitly and finally terminates your license, and **(b)** permanently,
if the copyright holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is reinstated permanently
if the copyright holder notifies you of the violation by some reasonable means, this
is the first time you have received notice of violation of this License (for any
work) from that copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the licenses of
parties who have received copies or rights from you under this License. If your
rights have been terminated and not permanently reinstated, you do not qualify to
receive new licenses for the same material under section 10.
### 9. Acceptance Not Required for Having Copies
You are not required to accept this License in order to receive or run a copy of the
Program. Ancillary propagation of a covered work occurring solely as a consequence of
using peer-to-peer transmission to receive a copy likewise does not require
acceptance. However, nothing other than this License grants you permission to
propagate or modify any covered work. These actions infringe copyright if you do not
accept this License. Therefore, by modifying or propagating a covered work, you
indicate your acceptance of this License to do so.
### 10. Automatic Licensing of Downstream Recipients
Each time you convey a covered work, the recipient automatically receives a license
from the original licensors, to run, modify and propagate that work, subject to this
License. You are not responsible for enforcing compliance by third parties with this
License.
An “entity transaction” is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an organization, or
merging organizations. If propagation of a covered work results from an entity
transaction, each party to that transaction who receives a copy of the work also
receives whatever licenses to the work the party's predecessor in interest had or
could give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if the predecessor
has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the rights granted or
affirmed under this License. For example, you may not impose a license fee, royalty,
or other charge for exercise of rights granted under this License, and you may not
initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging
that any patent claim is infringed by making, using, selling, offering for sale, or
importing the Program or any portion of it.
### 11. Patents
A “contributor” is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The work thus
licensed is called the contributor's “contributor version”.
A contributor's “essential patent claims” are all patent claims owned or
controlled by the contributor, whether already acquired or hereafter acquired, that
would be infringed by some manner, permitted by this License, of making, using, or
selling its contributor version, but do not include claims that would be infringed
only as a consequence of further modification of the contributor version. For
purposes of this definition, “control” includes the right to grant patent
sublicenses in a manner consistent with the requirements of this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free patent license
under the contributor's essential patent claims, to make, use, sell, offer for sale,
import and otherwise run, modify and propagate the contents of its contributor
version.
In the following three paragraphs, a “patent license” is any express
agreement or commitment, however denominated, not to enforce a patent (such as an
express permission to practice a patent or covenant not to sue for patent
infringement). To “grant” such a patent license to a party means to make
such an agreement or commitment not to enforce a patent against the party.
If you convey a covered work, knowingly relying on a patent license, and the
Corresponding Source of the work is not available for anyone to copy, free of charge
and under the terms of this License, through a publicly available network server or
other readily accessible means, then you must either **(1)** cause the Corresponding
Source to be so available, or **(2)** arrange to deprive yourself of the benefit of the
patent license for this particular work, or **(3)** arrange, in a manner consistent with
the requirements of this License, to extend the patent license to downstream
recipients. “Knowingly relying” means you have actual knowledge that, but
for the patent license, your conveying the covered work in a country, or your
recipient's use of the covered work in a country, would infringe one or more
identifiable patents in that country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or arrangement, you
convey, or propagate by procuring conveyance of, a covered work, and grant a patent
license to some of the parties receiving the covered work authorizing them to use,
propagate, modify or convey a specific copy of the covered work, then the patent
license you grant is automatically extended to all recipients of the covered work and
works based on it.
A patent license is “discriminatory” if it does not include within the
scope of its coverage, prohibits the exercise of, or is conditioned on the
non-exercise of one or more of the rights that are specifically granted under this
License. You may not convey a covered work if you are a party to an arrangement with
a third party that is in the business of distributing software, under which you make
payment to the third party based on the extent of your activity of conveying the
work, and under which the third party grants, to any of the parties who would receive
the covered work from you, a discriminatory patent license **(a)** in connection with
copies of the covered work conveyed by you (or copies made from those copies), or **(b)**
primarily for and in connection with specific products or compilations that contain
the covered work, unless you entered into that arrangement, or that patent license
was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting any implied
license or other defenses to infringement that may otherwise be available to you
under applicable patent law.
### 12. No Surrender of Others' Freedom
If conditions are imposed on you (whether by court order, agreement or otherwise)
that contradict the conditions of this License, they do not excuse you from the
conditions of this License. If you cannot convey a covered work so as to satisfy
simultaneously your obligations under this License and any other pertinent
obligations, then as a consequence you may not convey it at all. For example, if you
agree to terms that obligate you to collect a royalty for further conveying from
those to whom you convey the Program, the only way you could satisfy both those terms
and this License would be to refrain entirely from conveying the Program.
### 13. Use with the GNU Affero General Public License
Notwithstanding any other provision of this License, you have permission to link or
combine any covered work with a work licensed under version 3 of the GNU Affero
General Public License into a single combined work, and to convey the resulting work.
The terms of this License will continue to apply to the part which is the covered
work, but the special requirements of the GNU Affero General Public License, section
13, concerning interaction through a network will apply to the combination as such.
### 14. Revised Versions of this License
The Free Software Foundation may publish revised and/or new versions of the GNU
General Public License from time to time. Such new versions will be similar in spirit
to the present version, but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program specifies that
a certain numbered version of the GNU General Public License “or any later
version” applies to it, you have the option of following the terms and
conditions either of that numbered version or of any later version published by the
Free Software Foundation. If the Program does not specify a version number of the GNU
General Public License, you may choose any version ever published by the Free
Software Foundation.
If the Program specifies that a proxy can decide which future versions of the GNU
General Public License can be used, that proxy's public statement of acceptance of a
version permanently authorizes you to choose that version for the Program.
Later license versions may give you additional or different permissions. However, no
additional obligations are imposed on any author or copyright holder as a result of
your choosing to follow a later version.
### 15. Disclaimer of Warranty
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE
QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
### 16. Limitation of Liability
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS
PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE
OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
### 17. Interpretation of Sections 15 and 16
If the disclaimer of warranty and limitation of liability provided above cannot be
given local legal effect according to their terms, reviewing courts shall apply local
law that most closely approximates an absolute waiver of all civil liability in
connection with the Program, unless a warranty or assumption of liability accompanies
a copy of the Program in return for a fee.
_END OF TERMS AND CONDITIONS_
## How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest possible use to
the public, the best way to achieve this is to make it free software which everyone
can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest to attach them
to the start of each source file to most effectively state the exclusion of warranty;
and each file should have at least the “copyright” line and a pointer to
where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short notice like this
when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type 'show c' for details.
The hypothetical commands `show w` and `show c` should show the appropriate parts of
the General Public License. Of course, your program's commands might be different;
for a GUI interface, you would use an “about box”.
You should also get your employer (if you work as a programmer) or school, if any, to
sign a “copyright disclaimer” for the program, if necessary. For more
information on this, and how to apply and follow the GNU GPL, see
&lt;<http://www.gnu.org/licenses/>&gt;.
The GNU General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may consider it
more useful to permit linking proprietary applications with the library. If this is
what you want to do, use the GNU Lesser General Public License instead of this
License. But first, please read
&lt;<http://www.gnu.org/philosophy/why-not-lgpl.html>&gt;.

View File

@ -1,5 +1,5 @@
<p align="center"> <p align="center">
<img width="1412" src="./.resources/leo.png"> <img width="1412" src=".resources/banner.png">
</p> </p>
<h1 align="center">The Leo Programming Language</h1> <h1 align="center">The Leo Programming Language</h1>
@ -7,6 +7,7 @@
<p align="center"> <p align="center">
<a href="https://github.com/AleoHQ/leo/actions"><img src="https://github.com/AleoHQ/leo/workflows/CI/badge.svg"></a> <a href="https://github.com/AleoHQ/leo/actions"><img src="https://github.com/AleoHQ/leo/workflows/CI/badge.svg"></a>
<a href="https://codecov.io/gh/AleoHQ/leo"><img src="https://codecov.io/gh/AleoHQ/leo/branch/master/graph/badge.svg?token=S6MWO60SYL"/></a> <a href="https://codecov.io/gh/AleoHQ/leo"><img src="https://codecov.io/gh/AleoHQ/leo/branch/master/graph/badge.svg?token=S6MWO60SYL"/></a>
<a href="https://app.bors.tech/repositories/31738"><img src="https://bors.tech/images/badge_small.svg" alt="Bors enabled"></a>
<a href="https://discord.gg/TTexWvt"><img src="https://img.shields.io/discord/700454073459015690?logo=discord"/></a> <a href="https://discord.gg/TTexWvt"><img src="https://img.shields.io/discord/700454073459015690?logo=discord"/></a>
</p> </p>
@ -26,6 +27,7 @@ Leo is a functional, statically-typed programming language built for writing pri
## 1. Overview ## 1. Overview
Welcome to the Leo programming language. Welcome to the Leo programming language.
Leo provides a high-level language that abstracts low-level cryptographic concepts and makes it easy to Leo provides a high-level language that abstracts low-level cryptographic concepts and makes it easy to

17
SECURITY.md Normal file
View File

@ -0,0 +1,17 @@
# Security
The following describes our procedure for addressing major and minor security concerns in Leo.
## Testnet I
As Aleo is currently in the prototype stage and does not operate a platform intended for production use,
our security procedures are designed to promote public disclosure and quick security resolution.
In preparation for the production stage, we will release new security guidelines and
issue new procedures for addressing the disclosure of sensitive security vulnerabilities.
### Reporting a Bug
During Testnet I, all software bugs should be reported by filing a Github issue.
If you are unsure and would like to reach out to us directly, please email security \_at\_ aleo.org to elaborate on the issue.

View File

@ -1,6 +1,6 @@
[package] [package]
name = "leo-asg" name = "leo-asg"
version = "1.2.1" version = "1.2.3"
authors = [ "The Aleo Team <hello@aleo.org>" ] authors = [ "The Aleo Team <hello@aleo.org>" ]
description = "ASG of the Leo programming language" description = "ASG of the Leo programming language"
homepage = "https://aleo.org" homepage = "https://aleo.org"
@ -30,19 +30,18 @@ version = "1.6"
version = "1.0" version = "1.0"
[dependencies.leo-ast] [dependencies.leo-ast]
version = "1.2.1" version = "1.2.3"
path = "../ast" path = "../ast"
[dependencies.leo-grammar] [dependencies.leo-grammar]
version = "1.2.1" version = "1.2.3"
path = "../grammar" path = "../grammar"
[dependencies.uuid]
version = "0.8"
features = [ "v4", "serde", "wasm-bindgen" ]
[dependencies.num-bigint] [dependencies.num-bigint]
version = "0.3" version = "0.3"
[dependencies.typed-arena]
version = "2.0"
[dev-dependencies.criterion] [dev-dependencies.criterion]
version = "0.3" version = "0.3"

596
asg/LICENSE.md Normal file
View File

@ -0,0 +1,596 @@
GNU General Public License
==========================
Version 3, 29 June 2007
Copyright © 2007 Free Software Foundation, Inc. &lt;<https://fsf.org/>&gt;
Everyone is permitted to copy and distribute verbatim copies of this license
document, but changing it is not allowed.
## Preamble
The GNU General Public License is a free, copyleft license for software and other
kinds of works.
The licenses for most software and other practical works are designed to take away
your freedom to share and change the works. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change all versions of a
program--to make sure it remains free software for all its users. We, the Free
Software Foundation, use the GNU General Public License for most of our software; it
applies also to any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not price. Our General
Public Licenses are designed to make sure that you have the freedom to distribute
copies of free software (and charge for them if you wish), that you receive source
code or can get it if you want it, that you can change the software or use pieces of
it in new free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you these rights or
asking you to surrender the rights. Therefore, you have certain responsibilities if
you distribute copies of the software, or if you modify it: responsibilities to
respect the freedom of others.
For example, if you distribute copies of such a program, whether gratis or for a fee,
you must pass on to the recipients the same freedoms that you received. You must make
sure that they, too, receive or can get the source code. And you must show them these
terms so they know their rights.
Developers that use the GNU GPL protect your rights with two steps: **(1)** assert
copyright on the software, and **(2)** offer you this License giving you legal permission
to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains that there is
no warranty for this free software. For both users' and authors' sake, the GPL
requires that modified versions be marked as changed, so that their problems will not
be attributed erroneously to authors of previous versions.
Some devices are designed to deny users access to install or run modified versions of
the software inside them, although the manufacturer can do so. This is fundamentally
incompatible with the aim of protecting users' freedom to change the software. The
systematic pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we have designed
this version of the GPL to prohibit the practice for those products. If such problems
arise substantially in other domains, we stand ready to extend this provision to
those domains in future versions of the GPL, as needed to protect the freedom of
users.
Finally, every program is threatened constantly by software patents. States should
not allow patents to restrict development and use of software on general-purpose
computers, but in those that do, we wish to avoid the special danger that patents
applied to a free program could make it effectively proprietary. To prevent this, the
GPL assures that patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and modification follow.
## TERMS AND CONDITIONS
### 0. Definitions
“This License” refers to version 3 of the GNU General Public License.
“Copyright” also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
“The Program” refers to any copyrightable work licensed under this
License. Each licensee is addressed as “you”. “Licensees” and
“recipients” may be individuals or organizations.
To “modify” a work means to copy from or adapt all or part of the work in
a fashion requiring copyright permission, other than the making of an exact copy. The
resulting work is called a “modified version” of the earlier work or a
work “based on” the earlier work.
A “covered work” means either the unmodified Program or a work based on
the Program.
To “propagate” a work means to do anything with it that, without
permission, would make you directly or secondarily liable for infringement under
applicable copyright law, except executing it on a computer or modifying a private
copy. Propagation includes copying, distribution (with or without modification),
making available to the public, and in some countries other activities as well.
To “convey” a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through a computer
network, with no transfer of a copy, is not conveying.
An interactive user interface displays “Appropriate Legal Notices” to the
extent that it includes a convenient and prominently visible feature that **(1)**
displays an appropriate copyright notice, and **(2)** tells the user that there is no
warranty for the work (except to the extent that warranties are provided), that
licensees may convey the work under this License, and how to view a copy of this
License. If the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
### 1. Source Code
The “source code” for a work means the preferred form of the work for
making modifications to it. “Object code” means any non-source form of a
work.
A “Standard Interface” means an interface that either is an official
standard defined by a recognized standards body, or, in the case of interfaces
specified for a particular programming language, one that is widely used among
developers working in that language.
The “System Libraries” of an executable work include anything, other than
the work as a whole, that **(a)** is included in the normal form of packaging a Major
Component, but which is not part of that Major Component, and **(b)** serves only to
enable use of the work with that Major Component, or to implement a Standard
Interface for which an implementation is available to the public in source code form.
A “Major Component”, in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system (if any) on which
the executable work runs, or a compiler used to produce the work, or an object code
interpreter used to run it.
The “Corresponding Source” for a work in object code form means all the
source code needed to generate, install, and (for an executable work) run the object
code and to modify the work, including scripts to control those activities. However,
it does not include the work's System Libraries, or general-purpose tools or
generally available free programs which are used unmodified in performing those
activities but which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for the work, and
the source code for shared libraries and dynamically linked subprograms that the work
is specifically designed to require, such as by intimate data communication or
control flow between those subprograms and other parts of the work.
The Corresponding Source need not include anything that users can regenerate
automatically from other parts of the Corresponding Source.
The Corresponding Source for a work in source code form is that same work.
### 2. Basic Permissions
All rights granted under this License are granted for the term of copyright on the
Program, and are irrevocable provided the stated conditions are met. This License
explicitly affirms your unlimited permission to run the unmodified Program. The
output from running a covered work is covered by this License only if the output,
given its content, constitutes a covered work. This License acknowledges your rights
of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not convey, without
conditions so long as your license otherwise remains in force. You may convey covered
works to others for the sole purpose of having them make modifications exclusively
for you, or provide you with facilities for running those works, provided that you
comply with the terms of this License in conveying all material for which you do not
control copyright. Those thus making or running the covered works for you must do so
exclusively on your behalf, under your direction and control, on terms that prohibit
them from making any copies of your copyrighted material outside their relationship
with you.
Conveying under any other circumstances is permitted solely under the conditions
stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
### 3. Protecting Users' Legal Rights From Anti-Circumvention Law
No covered work shall be deemed part of an effective technological measure under any
applicable law fulfilling obligations under article 11 of the WIPO copyright treaty
adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention
of such measures.
When you convey a covered work, you waive any legal power to forbid circumvention of
technological measures to the extent such circumvention is effected by exercising
rights under this License with respect to the covered work, and you disclaim any
intention to limit operation or modification of the work as a means of enforcing,
against the work's users, your or third parties' legal rights to forbid circumvention
of technological measures.
### 4. Conveying Verbatim Copies
You may convey verbatim copies of the Program's source code as you receive it, in any
medium, provided that you conspicuously and appropriately publish on each copy an
appropriate copyright notice; keep intact all notices stating that this License and
any non-permissive terms added in accord with section 7 apply to the code; keep
intact all notices of the absence of any warranty; and give all recipients a copy of
this License along with the Program.
You may charge any price or no price for each copy that you convey, and you may offer
support or warranty protection for a fee.
### 5. Conveying Modified Source Versions
You may convey a work based on the Program, or the modifications to produce it from
the Program, in the form of source code under the terms of section 4, provided that
you also meet all of these conditions:
* **a)** The work must carry prominent notices stating that you modified it, and giving a
relevant date.
* **b)** The work must carry prominent notices stating that it is released under this
License and any conditions added under section 7. This requirement modifies the
requirement in section 4 to “keep intact all notices”.
* **c)** You must license the entire work, as a whole, under this License to anyone who
comes into possession of a copy. This License will therefore apply, along with any
applicable section 7 additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no permission to license the
work in any other way, but it does not invalidate such permission if you have
separately received it.
* **d)** If the work has interactive user interfaces, each must display Appropriate Legal
Notices; however, if the Program has interactive interfaces that do not display
Appropriate Legal Notices, your work need not make them do so.
A compilation of a covered work with other separate and independent works, which are
not by their nature extensions of the covered work, and which are not combined with
it such as to form a larger program, in or on a volume of a storage or distribution
medium, is called an “aggregate” if the compilation and its resulting
copyright are not used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work in an aggregate
does not cause this License to apply to the other parts of the aggregate.
### 6. Conveying Non-Source Forms
You may convey a covered work in object code form under the terms of sections 4 and
5, provided that you also convey the machine-readable Corresponding Source under the
terms of this License, in one of these ways:
* **a)** Convey the object code in, or embodied in, a physical product (including a
physical distribution medium), accompanied by the Corresponding Source fixed on a
durable physical medium customarily used for software interchange.
* **b)** Convey the object code in, or embodied in, a physical product (including a
physical distribution medium), accompanied by a written offer, valid for at least
three years and valid for as long as you offer spare parts or customer support for
that product model, to give anyone who possesses the object code either **(1)** a copy of
the Corresponding Source for all the software in the product that is covered by this
License, on a durable physical medium customarily used for software interchange, for
a price no more than your reasonable cost of physically performing this conveying of
source, or **(2)** access to copy the Corresponding Source from a network server at no
charge.
* **c)** Convey individual copies of the object code with a copy of the written offer to
provide the Corresponding Source. This alternative is allowed only occasionally and
noncommercially, and only if you received the object code with such an offer, in
accord with subsection 6b.
* **d)** Convey the object code by offering access from a designated place (gratis or for
a charge), and offer equivalent access to the Corresponding Source in the same way
through the same place at no further charge. You need not require recipients to copy
the Corresponding Source along with the object code. If the place to copy the object
code is a network server, the Corresponding Source may be on a different server
(operated by you or a third party) that supports equivalent copying facilities,
provided you maintain clear directions next to the object code saying where to find
the Corresponding Source. Regardless of what server hosts the Corresponding Source,
you remain obligated to ensure that it is available for as long as needed to satisfy
these requirements.
* **e)** Convey the object code using peer-to-peer transmission, provided you inform
other peers where the object code and Corresponding Source of the work are being
offered to the general public at no charge under subsection 6d.
A separable portion of the object code, whose source code is excluded from the
Corresponding Source as a System Library, need not be included in conveying the
object code work.
A “User Product” is either **(1)** a “consumer product”, which
means any tangible personal property which is normally used for personal, family, or
household purposes, or **(2)** anything designed or sold for incorporation into a
dwelling. In determining whether a product is a consumer product, doubtful cases
shall be resolved in favor of coverage. For a particular product received by a
particular user, “normally used” refers to a typical or common use of
that class of product, regardless of the status of the particular user or of the way
in which the particular user actually uses, or expects or is expected to use, the
product. A product is a consumer product regardless of whether the product has
substantial commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
“Installation Information” for a User Product means any methods,
procedures, authorization keys, or other information required to install and execute
modified versions of a covered work in that User Product from a modified version of
its Corresponding Source. The information must suffice to ensure that the continued
functioning of the modified object code is in no case prevented or interfered with
solely because modification has been made.
If you convey an object code work under this section in, or with, or specifically for
use in, a User Product, and the conveying occurs as part of a transaction in which
the right of possession and use of the User Product is transferred to the recipient
in perpetuity or for a fixed term (regardless of how the transaction is
characterized), the Corresponding Source conveyed under this section must be
accompanied by the Installation Information. But this requirement does not apply if
neither you nor any third party retains the ability to install modified object code
on the User Product (for example, the work has been installed in ROM).
The requirement to provide Installation Information does not include a requirement to
continue to provide support service, warranty, or updates for a work that has been
modified or installed by the recipient, or for the User Product in which it has been
modified or installed. Access to a network may be denied when the modification itself
materially and adversely affects the operation of the network or violates the rules
and protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided, in accord with
this section must be in a format that is publicly documented (and with an
implementation available to the public in source code form), and must require no
special password or key for unpacking, reading or copying.
### 7. Additional Terms
“Additional permissions” are terms that supplement the terms of this
License by making exceptions from one or more of its conditions. Additional
permissions that are applicable to the entire Program shall be treated as though they
were included in this License, to the extent that they are valid under applicable
law. If additional permissions apply only to part of the Program, that part may be
used separately under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option remove any
additional permissions from that copy, or from any part of it. (Additional
permissions may be written to require their own removal in certain cases when you
modify the work.) You may place additional permissions on material, added by you to a
covered work, for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you add to a
covered work, you may (if authorized by the copyright holders of that material)
supplement the terms of this License with terms:
* **a)** Disclaiming warranty or limiting liability differently from the terms of
sections 15 and 16 of this License; or
* **b)** Requiring preservation of specified reasonable legal notices or author
attributions in that material or in the Appropriate Legal Notices displayed by works
containing it; or
* **c)** Prohibiting misrepresentation of the origin of that material, or requiring that
modified versions of such material be marked in reasonable ways as different from the
original version; or
* **d)** Limiting the use for publicity purposes of names of licensors or authors of the
material; or
* **e)** Declining to grant rights under trademark law for use of some trade names,
trademarks, or service marks; or
* **f)** Requiring indemnification of licensors and authors of that material by anyone
who conveys the material (or modified versions of it) with contractual assumptions of
liability to the recipient, for any liability that these contractual assumptions
directly impose on those licensors and authors.
All other non-permissive additional terms are considered “further
restrictions” within the meaning of section 10. If the Program as you received
it, or any part of it, contains a notice stating that it is governed by this License
along with a term that is a further restriction, you may remove that term. If a
license document contains a further restriction but permits relicensing or conveying
under this License, you may add to a covered work material governed by the terms of
that license document, provided that the further restriction does not survive such
relicensing or conveying.
If you add terms to a covered work in accord with this section, you must place, in
the relevant source files, a statement of the additional terms that apply to those
files, or a notice indicating where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the form of a
separately written license, or stated as exceptions; the above requirements apply
either way.
### 8. Termination
You may not propagate or modify a covered work except as expressly provided under
this License. Any attempt otherwise to propagate or modify it is void, and will
automatically terminate your rights under this License (including any patent licenses
granted under the third paragraph of section 11).
However, if you cease all violation of this License, then your license from a
particular copyright holder is reinstated **(a)** provisionally, unless and until the
copyright holder explicitly and finally terminates your license, and **(b)** permanently,
if the copyright holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is reinstated permanently
if the copyright holder notifies you of the violation by some reasonable means, this
is the first time you have received notice of violation of this License (for any
work) from that copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the licenses of
parties who have received copies or rights from you under this License. If your
rights have been terminated and not permanently reinstated, you do not qualify to
receive new licenses for the same material under section 10.
### 9. Acceptance Not Required for Having Copies
You are not required to accept this License in order to receive or run a copy of the
Program. Ancillary propagation of a covered work occurring solely as a consequence of
using peer-to-peer transmission to receive a copy likewise does not require
acceptance. However, nothing other than this License grants you permission to
propagate or modify any covered work. These actions infringe copyright if you do not
accept this License. Therefore, by modifying or propagating a covered work, you
indicate your acceptance of this License to do so.
### 10. Automatic Licensing of Downstream Recipients
Each time you convey a covered work, the recipient automatically receives a license
from the original licensors, to run, modify and propagate that work, subject to this
License. You are not responsible for enforcing compliance by third parties with this
License.
An “entity transaction” is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an organization, or
merging organizations. If propagation of a covered work results from an entity
transaction, each party to that transaction who receives a copy of the work also
receives whatever licenses to the work the party's predecessor in interest had or
could give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if the predecessor
has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the rights granted or
affirmed under this License. For example, you may not impose a license fee, royalty,
or other charge for exercise of rights granted under this License, and you may not
initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging
that any patent claim is infringed by making, using, selling, offering for sale, or
importing the Program or any portion of it.
### 11. Patents
A “contributor” is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The work thus
licensed is called the contributor's “contributor version”.
A contributor's “essential patent claims” are all patent claims owned or
controlled by the contributor, whether already acquired or hereafter acquired, that
would be infringed by some manner, permitted by this License, of making, using, or
selling its contributor version, but do not include claims that would be infringed
only as a consequence of further modification of the contributor version. For
purposes of this definition, “control” includes the right to grant patent
sublicenses in a manner consistent with the requirements of this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free patent license
under the contributor's essential patent claims, to make, use, sell, offer for sale,
import and otherwise run, modify and propagate the contents of its contributor
version.
In the following three paragraphs, a “patent license” is any express
agreement or commitment, however denominated, not to enforce a patent (such as an
express permission to practice a patent or covenant not to sue for patent
infringement). To “grant” such a patent license to a party means to make
such an agreement or commitment not to enforce a patent against the party.
If you convey a covered work, knowingly relying on a patent license, and the
Corresponding Source of the work is not available for anyone to copy, free of charge
and under the terms of this License, through a publicly available network server or
other readily accessible means, then you must either **(1)** cause the Corresponding
Source to be so available, or **(2)** arrange to deprive yourself of the benefit of the
patent license for this particular work, or **(3)** arrange, in a manner consistent with
the requirements of this License, to extend the patent license to downstream
recipients. “Knowingly relying” means you have actual knowledge that, but
for the patent license, your conveying the covered work in a country, or your
recipient's use of the covered work in a country, would infringe one or more
identifiable patents in that country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or arrangement, you
convey, or propagate by procuring conveyance of, a covered work, and grant a patent
license to some of the parties receiving the covered work authorizing them to use,
propagate, modify or convey a specific copy of the covered work, then the patent
license you grant is automatically extended to all recipients of the covered work and
works based on it.
A patent license is “discriminatory” if it does not include within the
scope of its coverage, prohibits the exercise of, or is conditioned on the
non-exercise of one or more of the rights that are specifically granted under this
License. You may not convey a covered work if you are a party to an arrangement with
a third party that is in the business of distributing software, under which you make
payment to the third party based on the extent of your activity of conveying the
work, and under which the third party grants, to any of the parties who would receive
the covered work from you, a discriminatory patent license **(a)** in connection with
copies of the covered work conveyed by you (or copies made from those copies), or **(b)**
primarily for and in connection with specific products or compilations that contain
the covered work, unless you entered into that arrangement, or that patent license
was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting any implied
license or other defenses to infringement that may otherwise be available to you
under applicable patent law.
### 12. No Surrender of Others' Freedom
If conditions are imposed on you (whether by court order, agreement or otherwise)
that contradict the conditions of this License, they do not excuse you from the
conditions of this License. If you cannot convey a covered work so as to satisfy
simultaneously your obligations under this License and any other pertinent
obligations, then as a consequence you may not convey it at all. For example, if you
agree to terms that obligate you to collect a royalty for further conveying from
those to whom you convey the Program, the only way you could satisfy both those terms
and this License would be to refrain entirely from conveying the Program.
### 13. Use with the GNU Affero General Public License
Notwithstanding any other provision of this License, you have permission to link or
combine any covered work with a work licensed under version 3 of the GNU Affero
General Public License into a single combined work, and to convey the resulting work.
The terms of this License will continue to apply to the part which is the covered
work, but the special requirements of the GNU Affero General Public License, section
13, concerning interaction through a network will apply to the combination as such.
### 14. Revised Versions of this License
The Free Software Foundation may publish revised and/or new versions of the GNU
General Public License from time to time. Such new versions will be similar in spirit
to the present version, but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program specifies that
a certain numbered version of the GNU General Public License “or any later
version” applies to it, you have the option of following the terms and
conditions either of that numbered version or of any later version published by the
Free Software Foundation. If the Program does not specify a version number of the GNU
General Public License, you may choose any version ever published by the Free
Software Foundation.
If the Program specifies that a proxy can decide which future versions of the GNU
General Public License can be used, that proxy's public statement of acceptance of a
version permanently authorizes you to choose that version for the Program.
Later license versions may give you additional or different permissions. However, no
additional obligations are imposed on any author or copyright holder as a result of
your choosing to follow a later version.
### 15. Disclaimer of Warranty
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE
QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
### 16. Limitation of Liability
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS
PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE
OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
### 17. Interpretation of Sections 15 and 16
If the disclaimer of warranty and limitation of liability provided above cannot be
given local legal effect according to their terms, reviewing courts shall apply local
law that most closely approximates an absolute waiver of all civil liability in
connection with the Program, unless a warranty or assumption of liability accompanies
a copy of the Program in return for a fee.
_END OF TERMS AND CONDITIONS_
## How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest possible use to
the public, the best way to achieve this is to make it free software which everyone
can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest to attach them
to the start of each source file to most effectively state the exclusion of warranty;
and each file should have at least the “copyright” line and a pointer to
where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short notice like this
when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type 'show c' for details.
The hypothetical commands `show w` and `show c` should show the appropriate parts of
the General Public License. Of course, your program's commands might be different;
for a GUI interface, you would use an “about box”.
You should also get your employer (if you work as a programmer) or school, if any, to
sign a “copyright disclaimer” for the program, if necessary. For more
information on this, and how to apply and follow the GNU GPL, see
&lt;<http://www.gnu.org/licenses/>&gt;.
The GNU General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may consider it
more useful to permit linking proprietary applications with the library. If this is
what you want to do, use the GNU Lesser General Public License instead of this
License. But first, please read
&lt;<http://www.gnu.org/philosophy/why-not-lgpl.html>&gt;.

5
asg/README.md Normal file
View File

@ -0,0 +1,5 @@
# leo-asg
[![Crates.io](https://img.shields.io/crates/v/leo-asg.svg?color=neon)](https://crates.io/crates/leo-asg)
[![Authors](https://img.shields.io/badge/authors-Aleo-orange.svg)](../AUTHORS)
[![License](https://img.shields.io/badge/License-GPLv3-blue.svg)](./LICENSE.md)

View File

@ -25,8 +25,6 @@ use crate::{
Span, Span,
}; };
use std::sync::Arc;
pub struct ReturnPathReducer { pub struct ReturnPathReducer {
pub errors: Vec<(Span, String)>, pub errors: Vec<(Span, String)>,
} }
@ -48,14 +46,14 @@ impl Default for ReturnPathReducer {
} }
#[allow(unused_variables)] #[allow(unused_variables)]
impl MonoidalReducerExpression<BoolAnd> for ReturnPathReducer { impl<'a> MonoidalReducerExpression<'a, BoolAnd> for ReturnPathReducer {
fn reduce_expression(&mut self, input: &Arc<Expression>, value: BoolAnd) -> BoolAnd { fn reduce_expression(&mut self, input: &'a Expression<'a>, value: BoolAnd) -> BoolAnd {
BoolAnd(false) BoolAnd(false)
} }
} }
#[allow(unused_variables)] #[allow(unused_variables)]
impl MonoidalReducerStatement<BoolAnd> for ReturnPathReducer { impl<'a> MonoidalReducerStatement<'a, BoolAnd> for ReturnPathReducer {
fn reduce_assign_access(&mut self, input: &AssignAccess, left: Option<BoolAnd>, right: Option<BoolAnd>) -> BoolAnd { fn reduce_assign_access(&mut self, input: &AssignAccess, left: Option<BoolAnd>, right: Option<BoolAnd>) -> BoolAnd {
BoolAnd(false) BoolAnd(false)
} }
@ -69,7 +67,7 @@ impl MonoidalReducerStatement<BoolAnd> for ReturnPathReducer {
BoolAnd(false) BoolAnd(false)
} else if let Some(index) = statements[..statements.len() - 1].iter().map(|x| x.0).position(|x| x) { } else if let Some(index) = statements[..statements.len() - 1].iter().map(|x| x.0).position(|x| x) {
self.record_error( self.record_error(
input.statements[index].span(), input.statements[index].get().span(),
"dead code due to unconditional early return".to_string(), "dead code due to unconditional early return".to_string(),
); );
BoolAnd(true) BoolAnd(true)

View File

@ -226,7 +226,7 @@ impl ConstInt {
} }
} }
pub fn get_type(&self) -> Type { pub fn get_type<'a>(&self) -> Type<'a> {
Type::Integer(self.get_int_type()) Type::Integer(self.get_int_type())
} }
@ -247,7 +247,7 @@ impl ConstInt {
} }
impl ConstValue { impl ConstValue {
pub fn get_type(&self) -> Option<Type> { pub fn get_type<'a>(&self) -> Option<Type<'a>> {
Some(match self { Some(match self {
ConstValue::Int(i) => i.get_type(), ConstValue::Int(i) => i.get_type(),
ConstValue::Group(_) => Type::Group, ConstValue::Group(_) => Type::Group,

View File

@ -14,18 +14,33 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
#[derive(Debug, Error)] use std::{cell::Cell, unimplemented};
pub enum UpdaterError {
#[error("{}: {}", _0, _1)]
Crate(&'static str, String),
#[error("The current version {} is more recent than the release version {}", _0, _1)] use typed_arena::Arena;
OldReleaseVersion(String, String),
use crate::ArenaNode;
pub struct AsgContextInner<'a> {
pub arena: &'a Arena<ArenaNode<'a>>,
pub next_id: Cell<u32>,
} }
impl From<self_update::errors::Error> for UpdaterError { impl<'a> AsgContextInner<'a> {
fn from(error: self_update::errors::Error) -> Self { pub fn new(arena: &'a Arena<ArenaNode<'a>>) -> &'a Self {
tracing::error!("{}\n", error); match arena.alloc(ArenaNode::Inner(AsgContextInner {
UpdaterError::Crate("self_update", error.to_string()) arena,
next_id: Cell::new(0),
})) {
ArenaNode::Inner(x) => x,
_ => unimplemented!(),
}
}
pub fn get_id(&self) -> u32 {
let next_id = self.next_id.get();
self.next_id.replace(next_id + 1);
next_id
} }
} }
pub type AsgContext<'a> = &'a AsgContextInner<'a>;

View File

@ -17,56 +17,53 @@
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
use leo_ast::IntegerType; use leo_ast::IntegerType;
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct ArrayAccessExpression { pub struct ArrayAccessExpression<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub array: Arc<Expression>, pub array: Cell<&'a Expression<'a>>,
pub index: Arc<Expression>, pub index: Cell<&'a Expression<'a>>,
} }
impl Node for ArrayAccessExpression { impl<'a> Node for ArrayAccessExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for ArrayAccessExpression { impl<'a> ExpressionNode<'a> for ArrayAccessExpression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.array.set_parent(Arc::downgrade(expr)); self.array.get().set_parent(expr);
self.index.set_parent(Arc::downgrade(expr)); self.index.get().set_parent(expr);
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
match self.array.get_type() { match self.array.get().get_type() {
Some(Type::Array(element, _)) => Some(*element), Some(Type::Array(element, _)) => Some(*element),
_ => None, _ => None,
} }
} }
fn is_mut_ref(&self) -> bool { fn is_mut_ref(&self) -> bool {
self.array.is_mut_ref() self.array.get().is_mut_ref()
} }
fn const_value(&self) -> Option<ConstValue> { fn const_value(&self) -> Option<ConstValue> {
let mut array = match self.array.const_value()? { let mut array = match self.array.get().const_value()? {
ConstValue::Array(values) => values, ConstValue::Array(values) => values,
_ => return None, _ => return None,
}; };
let const_index = match self.index.const_value()? { let const_index = match self.index.get().const_value()? {
ConstValue::Int(x) => x.to_usize()?, ConstValue::Int(x) => x.to_usize()?,
_ => return None, _ => return None,
}; };
@ -77,17 +74,17 @@ impl ExpressionNode for ArrayAccessExpression {
} }
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
self.array.is_consty() self.array.get().is_consty()
} }
} }
impl FromAst<leo_ast::ArrayAccessExpression> for ArrayAccessExpression { impl<'a> FromAst<'a, leo_ast::ArrayAccessExpression> for ArrayAccessExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::ArrayAccessExpression, value: &leo_ast::ArrayAccessExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<ArrayAccessExpression, AsgConvertError> { ) -> Result<ArrayAccessExpression<'a>, AsgConvertError> {
let array = Arc::<Expression>::from_ast( let array = <&Expression<'a>>::from_ast(
scope, scope,
&*value.array, &*value.array,
Some(PartialType::Array(expected_type.map(Box::new), None)), Some(PartialType::Array(expected_type.map(Box::new), None)),
@ -103,7 +100,7 @@ impl FromAst<leo_ast::ArrayAccessExpression> for ArrayAccessExpression {
} }
} }
let index = Arc::<Expression>::from_ast( let index = <&Expression<'a>>::from_ast(
scope, scope,
&*value.index, &*value.index,
Some(PartialType::Integer(None, Some(IntegerType::U32))), Some(PartialType::Integer(None, Some(IntegerType::U32))),
@ -116,19 +113,19 @@ impl FromAst<leo_ast::ArrayAccessExpression> for ArrayAccessExpression {
} }
Ok(ArrayAccessExpression { Ok(ArrayAccessExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
array, array: Cell::new(array),
index, index: Cell::new(index),
}) })
} }
} }
impl Into<leo_ast::ArrayAccessExpression> for &ArrayAccessExpression { impl<'a> Into<leo_ast::ArrayAccessExpression> for &ArrayAccessExpression<'a> {
fn into(self) -> leo_ast::ArrayAccessExpression { fn into(self) -> leo_ast::ArrayAccessExpression {
leo_ast::ArrayAccessExpression { leo_ast::ArrayAccessExpression {
array: Box::new(self.array.as_ref().into()), array: Box::new(self.array.get().into()),
index: Box::new(self.index.as_ref().into()), index: Box::new(self.index.get().into()),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -16,40 +16,37 @@
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct ArrayInitExpression { pub struct ArrayInitExpression<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub element: Arc<Expression>, pub element: Cell<&'a Expression<'a>>,
pub len: usize, pub len: usize,
} }
impl Node for ArrayInitExpression { impl<'a> Node for ArrayInitExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for ArrayInitExpression { impl<'a> ExpressionNode<'a> for ArrayInitExpression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.element.set_parent(Arc::downgrade(expr)); self.element.get().set_parent(expr);
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
Some(Type::Array(Box::new(self.element.get_type()?), self.len)) Some(Type::Array(Box::new(self.element.get().get_type()?), self.len))
} }
fn is_mut_ref(&self) -> bool { fn is_mut_ref(&self) -> bool {
@ -62,16 +59,16 @@ impl ExpressionNode for ArrayInitExpression {
} }
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
self.element.is_consty() self.element.get().is_consty()
} }
} }
impl FromAst<leo_ast::ArrayInitExpression> for ArrayInitExpression { impl<'a> FromAst<'a, leo_ast::ArrayInitExpression> for ArrayInitExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::ArrayInitExpression, value: &leo_ast::ArrayInitExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<ArrayInitExpression, AsgConvertError> { ) -> Result<ArrayInitExpression<'a>, AsgConvertError> {
let (mut expected_item, expected_len) = match expected_type { let (mut expected_item, expected_len) = match expected_type {
Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims), Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims),
None => (None, None), None => (None, None),
@ -130,17 +127,19 @@ impl FromAst<leo_ast::ArrayInitExpression> for ArrayInitExpression {
} }
} }
} }
let mut element = Some(Arc::<Expression>::from_ast(scope, &*value.element, expected_item)?); let mut element = Some(<&'a Expression<'a>>::from_ast(scope, &*value.element, expected_item)?);
let mut output = None; let mut output = None;
for dimension in dimensions.iter().rev().copied() { for dimension in dimensions.iter().rev().copied() {
output = Some(ArrayInitExpression { output = Some(ArrayInitExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
element: output element: Cell::new(
.map(Expression::ArrayInit) output
.map(Arc::new) .map(Expression::ArrayInit)
.unwrap_or_else(|| element.take().unwrap()), .map(|expr| &*scope.alloc_expression(expr))
.unwrap_or_else(|| element.take().unwrap()),
),
len: dimension, len: dimension,
}); });
} }
@ -148,10 +147,10 @@ impl FromAst<leo_ast::ArrayInitExpression> for ArrayInitExpression {
} }
} }
impl Into<leo_ast::ArrayInitExpression> for &ArrayInitExpression { impl<'a> Into<leo_ast::ArrayInitExpression> for &ArrayInitExpression<'a> {
fn into(self) -> leo_ast::ArrayInitExpression { fn into(self) -> leo_ast::ArrayInitExpression {
leo_ast::ArrayInitExpression { leo_ast::ArrayInitExpression {
element: Box::new(self.element.as_ref().into()), element: Box::new(self.element.get().into()),
dimensions: leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber { dimensions: leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber {
value: self.len.to_string(), value: self.len.to_string(),
}]), }]),

View File

@ -17,25 +17,22 @@
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
use leo_ast::SpreadOrExpression; use leo_ast::SpreadOrExpression;
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct ArrayInlineExpression { pub struct ArrayInlineExpression<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub elements: Vec<(Arc<Expression>, bool)>, // bool = if spread pub elements: Vec<(Cell<&'a Expression<'a>>, bool)>, // bool = if spread
} }
impl ArrayInlineExpression { impl<'a> ArrayInlineExpression<'a> {
pub fn expanded_length(&self) -> usize { pub fn expanded_length(&self) -> usize {
self.elements self.elements
.iter() .iter()
.map(|(expr, is_spread)| { .map(|(expr, is_spread)| {
if *is_spread { if *is_spread {
match expr.get_type() { match expr.get().get_type() {
Some(Type::Array(_item, len)) => len, Some(Type::Array(_item, len)) => len,
_ => 0, _ => 0,
} }
@ -47,30 +44,30 @@ impl ArrayInlineExpression {
} }
} }
impl Node for ArrayInlineExpression { impl<'a> Node for ArrayInlineExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for ArrayInlineExpression { impl<'a> ExpressionNode<'a> for ArrayInlineExpression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.elements.iter().for_each(|(element, _)| { self.elements.iter().for_each(|(element, _)| {
element.set_parent(Arc::downgrade(expr)); element.get().set_parent(expr);
}) })
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
Some(Type::Array( Some(Type::Array(
Box::new(self.elements.first()?.0.get_type()?), Box::new(self.elements.first()?.0.get().get_type()?),
self.expanded_length(), self.expanded_length(),
)) ))
} }
@ -83,28 +80,28 @@ impl ExpressionNode for ArrayInlineExpression {
let mut const_values = vec![]; let mut const_values = vec![];
for (expr, spread) in self.elements.iter() { for (expr, spread) in self.elements.iter() {
if *spread { if *spread {
match expr.const_value()? { match expr.get().const_value()? {
ConstValue::Array(items) => const_values.extend(items), ConstValue::Array(items) => const_values.extend(items),
_ => return None, _ => return None,
} }
} else { } else {
const_values.push(expr.const_value()?); const_values.push(expr.get().const_value()?);
} }
} }
Some(ConstValue::Array(const_values)) Some(ConstValue::Array(const_values))
} }
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
self.elements.iter().all(|x| x.0.is_consty()) self.elements.iter().all(|x| x.0.get().is_consty())
} }
} }
impl FromAst<leo_ast::ArrayInlineExpression> for ArrayInlineExpression { impl<'a> FromAst<'a, leo_ast::ArrayInlineExpression> for ArrayInlineExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::ArrayInlineExpression, value: &leo_ast::ArrayInlineExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<ArrayInlineExpression, AsgConvertError> { ) -> Result<ArrayInlineExpression<'a>, AsgConvertError> {
let (mut expected_item, expected_len) = match expected_type { let (mut expected_item, expected_len) = match expected_type {
Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims), Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims),
None => (None, None), None => (None, None),
@ -119,22 +116,22 @@ impl FromAst<leo_ast::ArrayInlineExpression> for ArrayInlineExpression {
let mut len = 0; let mut len = 0;
let output = ArrayInlineExpression { let output = ArrayInlineExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
elements: value elements: value
.elements .elements
.iter() .iter()
.map(|e| match e { .map(|e| match e {
SpreadOrExpression::Expression(e) => { SpreadOrExpression::Expression(e) => {
let expr = Arc::<Expression>::from_ast(scope, e, expected_item.clone())?; let expr = <&Expression<'a>>::from_ast(scope, e, expected_item.clone())?;
if expected_item.is_none() { if expected_item.is_none() {
expected_item = expr.get_type().map(Type::partial); expected_item = expr.get_type().map(Type::partial);
} }
len += 1; len += 1;
Ok((expr, false)) Ok((Cell::new(expr), false))
} }
SpreadOrExpression::Spread(e) => { SpreadOrExpression::Spread(e) => {
let expr = Arc::<Expression>::from_ast( let expr = <&Expression<'a>>::from_ast(
scope, scope,
e, e,
Some(PartialType::Array(expected_item.clone().map(Box::new), None)), Some(PartialType::Array(expected_item.clone().map(Box::new), None)),
@ -160,7 +157,7 @@ impl FromAst<leo_ast::ArrayInlineExpression> for ArrayInlineExpression {
)); ));
} }
} }
Ok((expr, true)) Ok((Cell::new(expr), true))
} }
}) })
.collect::<Result<Vec<_>, AsgConvertError>>()?, .collect::<Result<Vec<_>, AsgConvertError>>()?,
@ -178,14 +175,14 @@ impl FromAst<leo_ast::ArrayInlineExpression> for ArrayInlineExpression {
} }
} }
impl Into<leo_ast::ArrayInlineExpression> for &ArrayInlineExpression { impl<'a> Into<leo_ast::ArrayInlineExpression> for &ArrayInlineExpression<'a> {
fn into(self) -> leo_ast::ArrayInlineExpression { fn into(self) -> leo_ast::ArrayInlineExpression {
leo_ast::ArrayInlineExpression { leo_ast::ArrayInlineExpression {
elements: self elements: self
.elements .elements
.iter() .iter()
.map(|(element, spread)| { .map(|(element, spread)| {
let element = element.as_ref().into(); let element = element.get().into();
if *spread { if *spread {
SpreadOrExpression::Spread(element) SpreadOrExpression::Spread(element)
} else { } else {

View File

@ -17,57 +17,54 @@
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
use leo_ast::IntegerType; use leo_ast::IntegerType;
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct ArrayRangeAccessExpression { pub struct ArrayRangeAccessExpression<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub array: Arc<Expression>, pub array: Cell<&'a Expression<'a>>,
pub left: Option<Arc<Expression>>, pub left: Cell<Option<&'a Expression<'a>>>,
pub right: Option<Arc<Expression>>, pub right: Cell<Option<&'a Expression<'a>>>,
} }
impl Node for ArrayRangeAccessExpression { impl<'a> Node for ArrayRangeAccessExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for ArrayRangeAccessExpression { impl<'a> ExpressionNode<'a> for ArrayRangeAccessExpression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.array.set_parent(Arc::downgrade(expr)); self.array.get().set_parent(expr);
self.array.enforce_parents(&self.array); self.array.get().enforce_parents(self.array.get());
if let Some(left) = self.left.as_ref() { if let Some(left) = self.left.get() {
left.set_parent(Arc::downgrade(expr)); left.set_parent(expr);
} }
if let Some(right) = self.right.as_ref() { if let Some(right) = self.right.get() {
right.set_parent(Arc::downgrade(expr)); right.set_parent(expr);
} }
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
let (element, array_len) = match self.array.get_type() { let (element, array_len) = match self.array.get().get_type() {
Some(Type::Array(element, len)) => (element, len), Some(Type::Array(element, len)) => (element, len),
_ => return None, _ => return None,
}; };
let const_left = match self.left.as_ref().map(|x| x.const_value()) { let const_left = match self.left.get().map(|x| x.const_value()) {
Some(Some(ConstValue::Int(x))) => x.to_usize()?, Some(Some(ConstValue::Int(x))) => x.to_usize()?,
None => 0, None => 0,
_ => return None, _ => return None,
}; };
let const_right = match self.right.as_ref().map(|x| x.const_value()) { let const_right = match self.right.get().map(|x| x.const_value()) {
Some(Some(ConstValue::Int(x))) => x.to_usize()?, Some(Some(ConstValue::Int(x))) => x.to_usize()?,
None => array_len, None => array_len,
_ => return None, _ => return None,
@ -80,20 +77,20 @@ impl ExpressionNode for ArrayRangeAccessExpression {
} }
fn is_mut_ref(&self) -> bool { fn is_mut_ref(&self) -> bool {
self.array.is_mut_ref() self.array.get().is_mut_ref()
} }
fn const_value(&self) -> Option<ConstValue> { fn const_value(&self) -> Option<ConstValue> {
let mut array = match self.array.const_value()? { let mut array = match self.array.get().const_value()? {
ConstValue::Array(values) => values, ConstValue::Array(values) => values,
_ => return None, _ => return None,
}; };
let const_left = match self.left.as_ref().map(|x| x.const_value()) { let const_left = match self.left.get().map(|x| x.const_value()) {
Some(Some(ConstValue::Int(x))) => x.to_usize()?, Some(Some(ConstValue::Int(x))) => x.to_usize()?,
None => 0, None => 0,
_ => return None, _ => return None,
}; };
let const_right = match self.right.as_ref().map(|x| x.const_value()) { let const_right = match self.right.get().map(|x| x.const_value()) {
Some(Some(ConstValue::Int(x))) => x.to_usize()?, Some(Some(ConstValue::Int(x))) => x.to_usize()?,
None => array.len(), None => array.len(),
_ => return None, _ => return None,
@ -106,16 +103,16 @@ impl ExpressionNode for ArrayRangeAccessExpression {
} }
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
self.array.is_consty() self.array.get().is_consty()
} }
} }
impl FromAst<leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessExpression { impl<'a> FromAst<'a, leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::ArrayRangeAccessExpression, value: &leo_ast::ArrayRangeAccessExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<ArrayRangeAccessExpression, AsgConvertError> { ) -> Result<ArrayRangeAccessExpression<'a>, AsgConvertError> {
let expected_array = match expected_type { let expected_array = match expected_type {
Some(PartialType::Array(element, _len)) => Some(PartialType::Array(element, None)), Some(PartialType::Array(element, _len)) => Some(PartialType::Array(element, None)),
None => None, None => None,
@ -127,7 +124,7 @@ impl FromAst<leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessExpression
)); ));
} }
}; };
let array = Arc::<Expression>::from_ast(scope, &*value.array, expected_array)?; let array = <&Expression<'a>>::from_ast(scope, &*value.array, expected_array)?;
let array_type = array.get_type(); let array_type = array.get_type();
match array_type { match array_type {
Some(Type::Array(_, _)) => (), Some(Type::Array(_, _)) => (),
@ -143,14 +140,14 @@ impl FromAst<leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessExpression
.left .left
.as_deref() .as_deref()
.map(|left| { .map(|left| {
Arc::<Expression>::from_ast(scope, left, Some(PartialType::Integer(None, Some(IntegerType::U32)))) <&Expression<'a>>::from_ast(scope, left, Some(PartialType::Integer(None, Some(IntegerType::U32))))
}) })
.transpose()?; .transpose()?;
let right = value let right = value
.right .right
.as_deref() .as_deref()
.map(|right| { .map(|right| {
Arc::<Expression>::from_ast(scope, right, Some(PartialType::Integer(None, Some(IntegerType::U32)))) <&Expression<'a>>::from_ast(scope, right, Some(PartialType::Integer(None, Some(IntegerType::U32))))
}) })
.transpose()?; .transpose()?;
@ -169,21 +166,21 @@ impl FromAst<leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessExpression
} }
} }
Ok(ArrayRangeAccessExpression { Ok(ArrayRangeAccessExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
array, array: Cell::new(array),
left, left: Cell::new(left),
right, right: Cell::new(right),
}) })
} }
} }
impl Into<leo_ast::ArrayRangeAccessExpression> for &ArrayRangeAccessExpression { impl<'a> Into<leo_ast::ArrayRangeAccessExpression> for &ArrayRangeAccessExpression<'a> {
fn into(self) -> leo_ast::ArrayRangeAccessExpression { fn into(self) -> leo_ast::ArrayRangeAccessExpression {
leo_ast::ArrayRangeAccessExpression { leo_ast::ArrayRangeAccessExpression {
array: Box::new(self.array.as_ref().into()), array: Box::new(self.array.get().into()),
left: self.left.as_ref().map(|left| Box::new(left.as_ref().into())), left: self.left.get().map(|left| Box::new(left.into())),
right: self.right.as_ref().map(|right| Box::new(right.as_ref().into())), right: self.right.get().map(|right| Box::new(right.into())),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -17,44 +17,41 @@
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
pub use leo_ast::{BinaryOperation, BinaryOperationClass}; pub use leo_ast::{BinaryOperation, BinaryOperationClass};
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct BinaryExpression { pub struct BinaryExpression<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub operation: BinaryOperation, pub operation: BinaryOperation,
pub left: Arc<Expression>, pub left: Cell<&'a Expression<'a>>,
pub right: Arc<Expression>, pub right: Cell<&'a Expression<'a>>,
} }
impl Node for BinaryExpression { impl<'a> Node for BinaryExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for BinaryExpression { impl<'a> ExpressionNode<'a> for BinaryExpression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.left.set_parent(Arc::downgrade(expr)); self.left.get().set_parent(expr);
self.right.set_parent(Arc::downgrade(expr)); self.right.get().set_parent(expr);
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
match self.operation.class() { match self.operation.class() {
BinaryOperationClass::Boolean => Some(Type::Boolean), BinaryOperationClass::Boolean => Some(Type::Boolean),
BinaryOperationClass::Numeric => self.left.get_type(), BinaryOperationClass::Numeric => self.left.get().get_type(),
} }
} }
@ -64,8 +61,8 @@ impl ExpressionNode for BinaryExpression {
fn const_value(&self) -> Option<ConstValue> { fn const_value(&self) -> Option<ConstValue> {
use BinaryOperation::*; use BinaryOperation::*;
let left = self.left.const_value()?; let left = self.left.get().const_value()?;
let right = self.right.const_value()?; let right = self.right.get().const_value()?;
match (left, right) { match (left, right) {
(ConstValue::Int(left), ConstValue::Int(right)) => Some(match self.operation { (ConstValue::Int(left), ConstValue::Int(right)) => Some(match self.operation {
@ -110,16 +107,16 @@ impl ExpressionNode for BinaryExpression {
} }
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
self.left.is_consty() && self.right.is_consty() self.left.get().is_consty() && self.right.get().is_consty()
} }
} }
impl FromAst<leo_ast::BinaryExpression> for BinaryExpression { impl<'a> FromAst<'a, leo_ast::BinaryExpression> for BinaryExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::BinaryExpression, value: &leo_ast::BinaryExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<BinaryExpression, AsgConvertError> { ) -> Result<BinaryExpression<'a>, AsgConvertError> {
let class = value.op.class(); let class = value.op.class();
let expected_type = match class { let expected_type = match class {
BinaryOperationClass::Boolean => match expected_type { BinaryOperationClass::Boolean => match expected_type {
@ -134,10 +131,12 @@ impl FromAst<leo_ast::BinaryExpression> for BinaryExpression {
}, },
BinaryOperationClass::Numeric => match expected_type { BinaryOperationClass::Numeric => match expected_type {
Some(x @ PartialType::Integer(_, _)) => Some(x), Some(x @ PartialType::Integer(_, _)) => Some(x),
Some(x @ PartialType::Type(Type::Field)) => Some(x),
Some(x @ PartialType::Type(Type::Group)) => Some(x),
Some(x) => { Some(x) => {
return Err(AsgConvertError::unexpected_type( return Err(AsgConvertError::unexpected_type(
&x.to_string(), &x.to_string(),
Some("integer"), Some("integer, field, or group"),
&value.span, &value.span,
)); ));
} }
@ -146,16 +145,16 @@ impl FromAst<leo_ast::BinaryExpression> for BinaryExpression {
}; };
// left // left
let (left, right) = match Arc::<Expression>::from_ast(scope, &*value.left, expected_type.clone()) { let (left, right) = match <&Expression<'a>>::from_ast(scope, &*value.left, expected_type.clone()) {
Ok(left) => { Ok(left) => {
if let Some(left_type) = left.get_type() { if let Some(left_type) = left.get_type() {
let right = Arc::<Expression>::from_ast(scope, &*value.right, Some(left_type.partial()))?; let right = <&Expression<'a>>::from_ast(scope, &*value.right, Some(left_type.partial()))?;
(left, right) (left, right)
} else { } else {
let right = Arc::<Expression>::from_ast(scope, &*value.right, expected_type)?; let right = <&Expression<'a>>::from_ast(scope, &*value.right, expected_type)?;
if let Some(right_type) = right.get_type() { if let Some(right_type) = right.get_type() {
( (
Arc::<Expression>::from_ast(scope, &*value.left, Some(right_type.partial()))?, <&Expression<'a>>::from_ast(scope, &*value.left, Some(right_type.partial()))?,
right, right,
) )
} else { } else {
@ -164,10 +163,10 @@ impl FromAst<leo_ast::BinaryExpression> for BinaryExpression {
} }
} }
Err(e) => { Err(e) => {
let right = Arc::<Expression>::from_ast(scope, &*value.right, expected_type)?; let right = <&Expression<'a>>::from_ast(scope, &*value.right, expected_type)?;
if let Some(right_type) = right.get_type() { if let Some(right_type) = right.get_type() {
( (
Arc::<Expression>::from_ast(scope, &*value.left, Some(right_type.partial()))?, <&Expression<'a>>::from_ast(scope, &*value.left, Some(right_type.partial()))?,
right, right,
) )
} else { } else {
@ -242,21 +241,21 @@ impl FromAst<leo_ast::BinaryExpression> for BinaryExpression {
(_, _) => (), (_, _) => (),
} }
Ok(BinaryExpression { Ok(BinaryExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
operation: value.op.clone(), operation: value.op.clone(),
left, left: Cell::new(left),
right, right: Cell::new(right),
}) })
} }
} }
impl Into<leo_ast::BinaryExpression> for &BinaryExpression { impl<'a> Into<leo_ast::BinaryExpression> for &BinaryExpression<'a> {
fn into(self) -> leo_ast::BinaryExpression { fn into(self) -> leo_ast::BinaryExpression {
leo_ast::BinaryExpression { leo_ast::BinaryExpression {
op: self.operation.clone(), op: self.operation.clone(),
left: Box::new(self.left.as_ref().into()), left: Box::new(self.left.get().into()),
right: Box::new(self.right.as_ref().into()), right: Box::new(self.right.get().into()),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -29,48 +29,45 @@ use crate::{
Span, Span,
Type, Type,
}; };
pub use leo_ast::BinaryOperation; pub use leo_ast::{BinaryOperation, Node as AstNode};
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct CallExpression { pub struct CallExpression<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub function: Arc<Function>, pub function: Cell<&'a Function<'a>>,
pub target: Option<Arc<Expression>>, pub target: Cell<Option<&'a Expression<'a>>>,
pub arguments: Vec<Arc<Expression>>, pub arguments: Vec<Cell<&'a Expression<'a>>>,
} }
impl Node for CallExpression { impl<'a> Node for CallExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for CallExpression { impl<'a> ExpressionNode<'a> for CallExpression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
if let Some(target) = self.target.as_ref() { if let Some(target) = self.target.get() {
target.set_parent(Arc::downgrade(expr)); target.set_parent(expr);
} }
self.arguments.iter().for_each(|element| { self.arguments.iter().for_each(|element| {
element.set_parent(Arc::downgrade(expr)); element.get().set_parent(expr);
}) })
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
Some(self.function.output.clone().into()) Some(self.function.get().output.clone())
} }
fn is_mut_ref(&self) -> bool { fn is_mut_ref(&self) -> bool {
@ -83,21 +80,20 @@ impl ExpressionNode for CallExpression {
} }
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
self.target.as_ref().map(|x| x.is_consty()).unwrap_or(true) && self.arguments.iter().all(|x| x.is_consty()) self.target.get().map(|x| x.is_consty()).unwrap_or(true) && self.arguments.iter().all(|x| x.get().is_consty())
} }
} }
impl FromAst<leo_ast::CallExpression> for CallExpression { impl<'a> FromAst<'a, leo_ast::CallExpression> for CallExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::CallExpression, value: &leo_ast::CallExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<CallExpression, AsgConvertError> { ) -> Result<CallExpression<'a>, AsgConvertError> {
let (target, function) = match &*value.function { let (target, function) = match &*value.function {
leo_ast::Expression::Identifier(name) => ( leo_ast::Expression::Identifier(name) => (
None, None,
scope scope
.borrow()
.resolve_function(&name.name) .resolve_function(&name.name)
.ok_or_else(|| AsgConvertError::unresolved_function(&name.name, &name.span))?, .ok_or_else(|| AsgConvertError::unresolved_function(&name.name, &name.span))?,
), ),
@ -106,7 +102,7 @@ impl FromAst<leo_ast::CallExpression> for CallExpression {
name, name,
span, span,
}) => { }) => {
let target = Arc::<Expression>::from_ast(scope, &**ast_circuit, None)?; let target = <&Expression<'a>>::from_ast(scope, &**ast_circuit, None)?;
let circuit = match target.get_type() { let circuit = match target.get_type() {
Some(Type::Circuit(circuit)) => circuit, Some(Type::Circuit(circuit)) => circuit,
type_ => { type_ => {
@ -137,7 +133,7 @@ impl FromAst<leo_ast::CallExpression> for CallExpression {
&span, &span,
)); ));
} }
(Some(target), body.clone()) (Some(target), *body)
} }
CircuitMember::Variable(_) => { CircuitMember::Variable(_) => {
return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span)); return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span));
@ -151,7 +147,6 @@ impl FromAst<leo_ast::CallExpression> for CallExpression {
}) => { }) => {
let circuit = if let leo_ast::Expression::Identifier(circuit_name) = &**ast_circuit { let circuit = if let leo_ast::Expression::Identifier(circuit_name) = &**ast_circuit {
scope scope
.borrow()
.resolve_circuit(&circuit_name.name) .resolve_circuit(&circuit_name.name)
.ok_or_else(|| AsgConvertError::unresolved_circuit(&circuit_name.name, &circuit_name.span))? .ok_or_else(|| AsgConvertError::unresolved_circuit(&circuit_name.name, &circuit_name.span))?
} else { } else {
@ -172,7 +167,7 @@ impl FromAst<leo_ast::CallExpression> for CallExpression {
&span, &span,
)); ));
} }
(None, body.clone()) (None, *body)
} }
CircuitMember::Variable(_) => { CircuitMember::Variable(_) => {
return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span)); return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span));
@ -186,7 +181,7 @@ impl FromAst<leo_ast::CallExpression> for CallExpression {
} }
}; };
if let Some(expected) = expected_type { if let Some(expected) = expected_type {
let output: Type = function.output.clone().into(); let output: Type = function.output.clone();
if !expected.matches(&output) { if !expected.matches(&output) {
return Err(AsgConvertError::unexpected_type( return Err(AsgConvertError::unexpected_type(
&expected.to_string(), &expected.to_string(),
@ -195,50 +190,57 @@ impl FromAst<leo_ast::CallExpression> for CallExpression {
)); ));
} }
} }
if value.arguments.len() != function.argument_types.len() { if value.arguments.len() != function.arguments.len() {
return Err(AsgConvertError::unexpected_call_argument_count( return Err(AsgConvertError::unexpected_call_argument_count(
function.argument_types.len(), function.arguments.len(),
value.arguments.len(), value.arguments.len(),
&value.span, &value.span,
)); ));
} }
let arguments = value
.arguments
.iter()
.zip(function.arguments.iter())
.map(|(expr, (_, argument))| {
let argument = argument.get().borrow();
let converted = <&Expression<'a>>::from_ast(scope, expr, Some(argument.type_.clone().partial()))?;
if argument.const_ && !converted.is_consty() {
return Err(AsgConvertError::unexpected_nonconst(&expr.span()));
}
Ok(Cell::new(converted))
})
.collect::<Result<Vec<_>, AsgConvertError>>()?;
Ok(CallExpression { Ok(CallExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
arguments: value arguments,
.arguments function: Cell::new(function),
.iter() target: Cell::new(target),
.zip(function.argument_types.iter())
.map(|(expr, argument)| {
Arc::<Expression>::from_ast(scope, expr, Some(argument.clone().strong().partial()))
})
.collect::<Result<Vec<_>, AsgConvertError>>()?,
function,
target,
}) })
} }
} }
impl Into<leo_ast::CallExpression> for &CallExpression { impl<'a> Into<leo_ast::CallExpression> for &CallExpression<'a> {
fn into(self) -> leo_ast::CallExpression { fn into(self) -> leo_ast::CallExpression {
let target_function = if let Some(target) = &self.target { let target_function = if let Some(target) = self.target.get() {
target.as_ref().into() target.into()
} else { } else {
let circuit = self.function.circuit.borrow().as_ref().map(|x| x.upgrade()).flatten(); let circuit = self.function.get().circuit.get();
if let Some(circuit) = circuit { if let Some(circuit) = circuit {
leo_ast::Expression::CircuitStaticFunctionAccess(leo_ast::CircuitStaticFunctionAccessExpression { leo_ast::Expression::CircuitStaticFunctionAccess(leo_ast::CircuitStaticFunctionAccessExpression {
circuit: Box::new(leo_ast::Expression::Identifier(circuit.name.borrow().clone())), circuit: Box::new(leo_ast::Expression::Identifier(circuit.name.borrow().clone())),
name: self.function.name.borrow().clone(), name: self.function.get().name.borrow().clone(),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
}) })
} else { } else {
leo_ast::Expression::Identifier(self.function.name.borrow().clone()) leo_ast::Expression::Identifier(self.function.get().name.borrow().clone())
} }
}; };
leo_ast::CallExpression { leo_ast::CallExpression {
function: Box::new(target_function), function: Box::new(target_function),
arguments: self.arguments.iter().map(|arg| arg.as_ref().into()).collect(), arguments: self.arguments.iter().map(|arg| arg.get().into()).collect(),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -18,7 +18,6 @@ use crate::{
AsgConvertError, AsgConvertError,
Circuit, Circuit,
CircuitMember, CircuitMember,
CircuitMemberBody,
ConstValue, ConstValue,
Expression, Expression,
ExpressionNode, ExpressionNode,
@ -31,56 +30,53 @@ use crate::{
Type, Type,
}; };
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct CircuitAccessExpression { pub struct CircuitAccessExpression<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub circuit: Arc<Circuit>, pub circuit: Cell<&'a Circuit<'a>>,
pub target: Option<Arc<Expression>>, pub target: Cell<Option<&'a Expression<'a>>>,
pub member: Identifier, pub member: Identifier,
} }
impl Node for CircuitAccessExpression { impl<'a> Node for CircuitAccessExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for CircuitAccessExpression { impl<'a> ExpressionNode<'a> for CircuitAccessExpression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
if let Some(target) = self.target.as_ref() { if let Some(target) = self.target.get() {
target.set_parent(Arc::downgrade(expr)); target.set_parent(expr);
} }
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
if self.target.is_none() { if self.target.get().is_none() {
None // function target only for static None // function target only for static
} else { } else {
let members = self.circuit.members.borrow(); let members = self.circuit.get().members.borrow();
let member = members.get(&self.member.name)?; let member = members.get(&self.member.name)?;
match member { match member {
CircuitMember::Variable(type_) => Some(type_.clone().into()), CircuitMember::Variable(type_) => Some(type_.clone()),
CircuitMember::Function(_) => None, CircuitMember::Function(_) => None,
} }
} }
} }
fn is_mut_ref(&self) -> bool { fn is_mut_ref(&self) -> bool {
if let Some(target) = self.target.as_ref() { if let Some(target) = self.target.get() {
target.is_mut_ref() target.is_mut_ref()
} else { } else {
false false
@ -92,17 +88,17 @@ impl ExpressionNode for CircuitAccessExpression {
} }
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
self.target.as_ref().map(|x| x.is_consty()).unwrap_or(true) self.target.get().map(|x| x.is_consty()).unwrap_or(true)
} }
} }
impl FromAst<leo_ast::CircuitMemberAccessExpression> for CircuitAccessExpression { impl<'a> FromAst<'a, leo_ast::CircuitMemberAccessExpression> for CircuitAccessExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::CircuitMemberAccessExpression, value: &leo_ast::CircuitMemberAccessExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<CircuitAccessExpression, AsgConvertError> { ) -> Result<CircuitAccessExpression<'a>, AsgConvertError> {
let target = Arc::<Expression>::from_ast(scope, &*value.circuit, None)?; let target = <&'a Expression<'a>>::from_ast(scope, &*value.circuit, None)?;
let circuit = match target.get_type() { let circuit = match target.get_type() {
Some(Type::Circuit(circuit)) => circuit, Some(Type::Circuit(circuit)) => circuit,
x => { x => {
@ -119,7 +115,7 @@ impl FromAst<leo_ast::CircuitMemberAccessExpression> for CircuitAccessExpression
if let Some(member) = circuit.members.borrow().get(&value.name.name) { if let Some(member) = circuit.members.borrow().get(&value.name.name) {
if let Some(expected_type) = &expected_type { if let Some(expected_type) = &expected_type {
if let CircuitMember::Variable(type_) = &member { if let CircuitMember::Variable(type_) = &member {
let type_: Type = type_.clone().into(); let type_: Type = type_.clone();
if !expected_type.matches(&type_) { if !expected_type.matches(&type_) {
return Err(AsgConvertError::unexpected_type( return Err(AsgConvertError::unexpected_type(
&expected_type.to_string(), &expected_type.to_string(),
@ -140,15 +136,10 @@ impl FromAst<leo_ast::CircuitMemberAccessExpression> for CircuitAccessExpression
} else if circuit.is_input_pseudo_circuit() { } else if circuit.is_input_pseudo_circuit() {
// add new member to implicit input // add new member to implicit input
if let Some(expected_type) = expected_type.map(PartialType::full).flatten() { if let Some(expected_type) = expected_type.map(PartialType::full).flatten() {
circuit.members.borrow_mut().insert( circuit
value.name.name.clone(), .members
CircuitMember::Variable(expected_type.clone().into()),
);
let body = circuit.body.borrow().upgrade().expect("stale input circuit body");
body.members
.borrow_mut() .borrow_mut()
.insert(value.name.name.clone(), CircuitMemberBody::Variable(expected_type)); .insert(value.name.name.clone(), CircuitMember::Variable(expected_type.clone()));
} else { } else {
return Err(AsgConvertError::input_ref_needs_type( return Err(AsgConvertError::input_ref_needs_type(
&circuit.name.borrow().name, &circuit.name.borrow().name,
@ -165,24 +156,23 @@ impl FromAst<leo_ast::CircuitMemberAccessExpression> for CircuitAccessExpression
} }
Ok(CircuitAccessExpression { Ok(CircuitAccessExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
target: Some(target), target: Cell::new(Some(target)),
circuit, circuit: Cell::new(circuit),
member: value.name.clone(), member: value.name.clone(),
}) })
} }
} }
impl FromAst<leo_ast::CircuitStaticFunctionAccessExpression> for CircuitAccessExpression { impl<'a> FromAst<'a, leo_ast::CircuitStaticFunctionAccessExpression> for CircuitAccessExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &Scope<'a>,
value: &leo_ast::CircuitStaticFunctionAccessExpression, value: &leo_ast::CircuitStaticFunctionAccessExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType>,
) -> Result<CircuitAccessExpression, AsgConvertError> { ) -> Result<CircuitAccessExpression<'a>, AsgConvertError> {
let circuit = match &*value.circuit { let circuit = match &*value.circuit {
leo_ast::Expression::Identifier(name) => scope leo_ast::Expression::Identifier(name) => scope
.borrow()
.resolve_circuit(&name.name) .resolve_circuit(&name.name)
.ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?, .ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?,
_ => { _ => {
@ -213,26 +203,28 @@ impl FromAst<leo_ast::CircuitStaticFunctionAccessExpression> for CircuitAccessEx
} }
Ok(CircuitAccessExpression { Ok(CircuitAccessExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
target: None, target: Cell::new(None),
circuit, circuit: Cell::new(circuit),
member: value.name.clone(), member: value.name.clone(),
}) })
} }
} }
impl Into<leo_ast::Expression> for &CircuitAccessExpression { impl<'a> Into<leo_ast::Expression> for &CircuitAccessExpression<'a> {
fn into(self) -> leo_ast::Expression { fn into(self) -> leo_ast::Expression {
if let Some(target) = self.target.as_ref() { if let Some(target) = self.target.get() {
leo_ast::Expression::CircuitMemberAccess(leo_ast::CircuitMemberAccessExpression { leo_ast::Expression::CircuitMemberAccess(leo_ast::CircuitMemberAccessExpression {
circuit: Box::new(target.as_ref().into()), circuit: Box::new(target.into()),
name: self.member.clone(), name: self.member.clone(),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
}) })
} else { } else {
leo_ast::Expression::CircuitStaticFunctionAccess(leo_ast::CircuitStaticFunctionAccessExpression { leo_ast::Expression::CircuitStaticFunctionAccess(leo_ast::CircuitStaticFunctionAccessExpression {
circuit: Box::new(leo_ast::Expression::Identifier(self.circuit.name.borrow().clone())), circuit: Box::new(leo_ast::Expression::Identifier(
self.circuit.get().name.borrow().clone(),
)),
name: self.member.clone(), name: self.member.clone(),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
}) })

View File

@ -31,42 +31,39 @@ use crate::{
}; };
use indexmap::{IndexMap, IndexSet}; use indexmap::{IndexMap, IndexSet};
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct CircuitInitExpression { pub struct CircuitInitExpression<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub circuit: Arc<Circuit>, pub circuit: Cell<&'a Circuit<'a>>,
pub values: Vec<(Identifier, Arc<Expression>)>, pub values: Vec<(Identifier, Cell<&'a Expression<'a>>)>,
} }
impl Node for CircuitInitExpression { impl<'a> Node for CircuitInitExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for CircuitInitExpression { impl<'a> ExpressionNode<'a> for CircuitInitExpression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.values.iter().for_each(|(_, element)| { self.values.iter().for_each(|(_, element)| {
element.set_parent(Arc::downgrade(expr)); element.get().set_parent(expr);
}) })
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
Some(Type::Circuit(self.circuit.clone())) Some(Type::Circuit(self.circuit.get()))
} }
fn is_mut_ref(&self) -> bool { fn is_mut_ref(&self) -> bool {
@ -78,18 +75,17 @@ impl ExpressionNode for CircuitInitExpression {
} }
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
self.values.iter().all(|(_, value)| value.is_consty()) self.values.iter().all(|(_, value)| value.get().is_consty())
} }
} }
impl FromAst<leo_ast::CircuitInitExpression> for CircuitInitExpression { impl<'a> FromAst<'a, leo_ast::CircuitInitExpression> for CircuitInitExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::CircuitInitExpression, value: &leo_ast::CircuitInitExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<CircuitInitExpression, AsgConvertError> { ) -> Result<CircuitInitExpression<'a>, AsgConvertError> {
let circuit = scope let circuit = scope
.borrow()
.resolve_circuit(&value.name.name) .resolve_circuit(&value.name.name)
.ok_or_else(|| AsgConvertError::unresolved_circuit(&value.name.name, &value.name.span))?; .ok_or_else(|| AsgConvertError::unresolved_circuit(&value.name.name, &value.name.span))?;
match expected_type { match expected_type {
@ -109,7 +105,7 @@ impl FromAst<leo_ast::CircuitInitExpression> for CircuitInitExpression {
.map(|x| (&x.identifier.name, (&x.identifier, &x.expression))) .map(|x| (&x.identifier.name, (&x.identifier, &x.expression)))
.collect(); .collect();
let mut values: Vec<(Identifier, Arc<Expression>)> = vec![]; let mut values: Vec<(Identifier, Cell<&'a Expression<'a>>)> = vec![];
let mut defined_variables = IndexSet::<String>::new(); let mut defined_variables = IndexSet::<String>::new();
{ {
@ -124,13 +120,13 @@ impl FromAst<leo_ast::CircuitInitExpression> for CircuitInitExpression {
} }
defined_variables.insert(name.clone()); defined_variables.insert(name.clone());
let type_: Type = if let CircuitMember::Variable(type_) = &member { let type_: Type = if let CircuitMember::Variable(type_) = &member {
type_.clone().into() type_.clone()
} else { } else {
continue; continue;
}; };
if let Some((identifier, receiver)) = members.get(&name) { if let Some((identifier, receiver)) = members.get(&name) {
let received = Arc::<Expression>::from_ast(scope, *receiver, Some(type_.partial()))?; let received = <&Expression<'a>>::from_ast(scope, *receiver, Some(type_.partial()))?;
values.push(((*identifier).clone(), received)); values.push(((*identifier).clone(), Cell::new(received)));
} else { } else {
return Err(AsgConvertError::missing_circuit_member( return Err(AsgConvertError::missing_circuit_member(
&circuit.name.borrow().name, &circuit.name.borrow().name,
@ -152,24 +148,24 @@ impl FromAst<leo_ast::CircuitInitExpression> for CircuitInitExpression {
} }
Ok(CircuitInitExpression { Ok(CircuitInitExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
circuit, circuit: Cell::new(circuit),
values, values,
}) })
} }
} }
impl Into<leo_ast::CircuitInitExpression> for &CircuitInitExpression { impl<'a> Into<leo_ast::CircuitInitExpression> for &CircuitInitExpression<'a> {
fn into(self) -> leo_ast::CircuitInitExpression { fn into(self) -> leo_ast::CircuitInitExpression {
leo_ast::CircuitInitExpression { leo_ast::CircuitInitExpression {
name: self.circuit.name.borrow().clone(), name: self.circuit.get().name.borrow().clone(),
members: self members: self
.values .values
.iter() .iter()
.map(|(name, value)| leo_ast::CircuitVariableDefinition { .map(|(name, value)| leo_ast::CircuitImpliedVariableDefinition {
identifier: name.clone(), identifier: name.clone(),
expression: value.as_ref().into(), expression: value.get().into(),
}) })
.collect(), .collect(),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),

View File

@ -29,36 +29,33 @@ use crate::{
Type, Type,
}; };
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct Constant { pub struct Constant<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub value: ConstValue, // should not be compound constants pub value: ConstValue, // should not be compound constants
} }
impl Node for Constant { impl<'a> Node for Constant<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for Constant { impl<'a> ExpressionNode<'a> for Constant<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, _expr: &Arc<Expression>) {} fn enforce_parents(&self, _expr: &'a Expression<'a>) {}
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
self.value.get_type() self.value.get_type()
} }
@ -75,12 +72,12 @@ impl ExpressionNode for Constant {
} }
} }
impl FromAst<leo_ast::ValueExpression> for Constant { impl<'a> FromAst<'a, leo_ast::ValueExpression> for Constant<'a> {
fn from_ast( fn from_ast(
_scope: &Scope, _scope: &'a Scope<'a>,
value: &leo_ast::ValueExpression, value: &leo_ast::ValueExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<Constant, AsgConvertError> { ) -> Result<Constant<'a>, AsgConvertError> {
use leo_ast::ValueExpression::*; use leo_ast::ValueExpression::*;
Ok(match value { Ok(match value {
Address(value, span) => { Address(value, span) => {
@ -95,7 +92,7 @@ impl FromAst<leo_ast::ValueExpression> for Constant {
} }
} }
Constant { Constant {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(span.clone()), span: Some(span.clone()),
value: ConstValue::Address(value.clone()), value: ConstValue::Address(value.clone()),
} }
@ -112,7 +109,7 @@ impl FromAst<leo_ast::ValueExpression> for Constant {
} }
} }
Constant { Constant {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(span.clone()), span: Some(span.clone()),
value: ConstValue::Boolean( value: ConstValue::Boolean(
value value
@ -133,7 +130,7 @@ impl FromAst<leo_ast::ValueExpression> for Constant {
} }
} }
Constant { Constant {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(span.clone()), span: Some(span.clone()),
value: ConstValue::Field(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?), value: ConstValue::Field(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
} }
@ -150,7 +147,7 @@ impl FromAst<leo_ast::ValueExpression> for Constant {
} }
} }
Constant { Constant {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span().clone()), span: Some(value.span().clone()),
value: ConstValue::Group(match &**value { value: ConstValue::Group(match &**value {
leo_ast::GroupValue::Single(value, _) => GroupValue::Single(value.clone()), leo_ast::GroupValue::Single(value, _) => GroupValue::Single(value.clone()),
@ -164,23 +161,23 @@ impl FromAst<leo_ast::ValueExpression> for Constant {
None => return Err(AsgConvertError::unresolved_type("unknown", span)), None => return Err(AsgConvertError::unresolved_type("unknown", span)),
Some(PartialType::Integer(Some(sub_type), _)) | Some(PartialType::Integer(None, Some(sub_type))) => { Some(PartialType::Integer(Some(sub_type), _)) | Some(PartialType::Integer(None, Some(sub_type))) => {
Constant { Constant {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(span.clone()), span: Some(span.clone()),
value: ConstValue::Int(ConstInt::parse(&sub_type, value, span)?), value: ConstValue::Int(ConstInt::parse(&sub_type, value, span)?),
} }
} }
Some(PartialType::Type(Type::Field)) => Constant { Some(PartialType::Type(Type::Field)) => Constant {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(span.clone()), span: Some(span.clone()),
value: ConstValue::Field(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?), value: ConstValue::Field(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
}, },
Some(PartialType::Type(Type::Group)) => Constant { Some(PartialType::Type(Type::Group)) => Constant {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(span.clone()), span: Some(span.clone()),
value: ConstValue::Group(GroupValue::Single(value.to_string())), value: ConstValue::Group(GroupValue::Single(value.to_string())),
}, },
Some(PartialType::Type(Type::Address)) => Constant { Some(PartialType::Type(Type::Address)) => Constant {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(span.clone()), span: Some(span.clone()),
value: ConstValue::Address(value.to_string()), value: ConstValue::Address(value.to_string()),
}, },
@ -200,7 +197,7 @@ impl FromAst<leo_ast::ValueExpression> for Constant {
} }
} }
Constant { Constant {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(span.clone()), span: Some(span.clone()),
value: ConstValue::Int(ConstInt::parse(int_type, value, span)?), value: ConstValue::Int(ConstInt::parse(int_type, value, span)?),
} }
@ -209,7 +206,7 @@ impl FromAst<leo_ast::ValueExpression> for Constant {
} }
} }
impl Into<leo_ast::ValueExpression> for &Constant { impl<'a> Into<leo_ast::ValueExpression> for &Constant<'a> {
fn into(self) -> leo_ast::ValueExpression { fn into(self) -> leo_ast::ValueExpression {
match &self.value { match &self.value {
ConstValue::Address(value) => { ConstValue::Address(value) => {

View File

@ -64,31 +64,29 @@ pub use variable_ref::*;
use crate::{AsgConvertError, ConstValue, FromAst, Node, PartialType, Scope, Span, Type}; use crate::{AsgConvertError, ConstValue, FromAst, Node, PartialType, Scope, Span, Type};
use std::sync::{Arc, Weak}; #[derive(Clone)]
pub enum Expression<'a> {
VariableRef(VariableRef<'a>),
Constant(Constant<'a>),
Binary(BinaryExpression<'a>),
Unary(UnaryExpression<'a>),
Ternary(TernaryExpression<'a>),
#[derive(Debug)] ArrayInline(ArrayInlineExpression<'a>),
pub enum Expression { ArrayInit(ArrayInitExpression<'a>),
VariableRef(VariableRef), ArrayAccess(ArrayAccessExpression<'a>),
Constant(Constant), ArrayRangeAccess(ArrayRangeAccessExpression<'a>),
Binary(BinaryExpression),
Unary(UnaryExpression),
Ternary(TernaryExpression),
ArrayInline(ArrayInlineExpression), TupleInit(TupleInitExpression<'a>),
ArrayInit(ArrayInitExpression), TupleAccess(TupleAccessExpression<'a>),
ArrayAccess(ArrayAccessExpression),
ArrayRangeAccess(ArrayRangeAccessExpression),
TupleInit(TupleInitExpression), CircuitInit(CircuitInitExpression<'a>),
TupleAccess(TupleAccessExpression), CircuitAccess(CircuitAccessExpression<'a>),
CircuitInit(CircuitInitExpression), Call(CallExpression<'a>),
CircuitAccess(CircuitAccessExpression),
Call(CallExpression),
} }
impl Node for Expression { impl<'a> Node for Expression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
use Expression::*; use Expression::*;
match self { match self {
@ -110,19 +108,19 @@ impl Node for Expression {
} }
} }
pub trait ExpressionNode: Node { pub trait ExpressionNode<'a>: Node {
fn set_parent(&self, parent: Weak<Expression>); fn set_parent(&self, parent: &'a Expression<'a>);
fn get_parent(&self) -> Option<Arc<Expression>>; fn get_parent(&self) -> Option<&'a Expression<'a>>;
fn enforce_parents(&self, expr: &Arc<Expression>); fn enforce_parents(&self, expr: &'a Expression<'a>);
fn get_type(&self) -> Option<Type>; fn get_type(&self) -> Option<Type<'a>>;
fn is_mut_ref(&self) -> bool; fn is_mut_ref(&self) -> bool;
fn const_value(&self) -> Option<ConstValue>; // todo: memoize fn const_value(&self) -> Option<ConstValue>; // todo: memoize
fn is_consty(&self) -> bool; fn is_consty(&self) -> bool;
} }
impl ExpressionNode for Expression { impl<'a> ExpressionNode<'a> for Expression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
use Expression::*; use Expression::*;
match self { match self {
VariableRef(x) => x.set_parent(parent), VariableRef(x) => x.set_parent(parent),
@ -142,7 +140,7 @@ impl ExpressionNode for Expression {
} }
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
use Expression::*; use Expression::*;
match self { match self {
VariableRef(x) => x.get_parent(), VariableRef(x) => x.get_parent(),
@ -162,7 +160,7 @@ impl ExpressionNode for Expression {
} }
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
use Expression::*; use Expression::*;
match self { match self {
VariableRef(x) => x.enforce_parents(expr), VariableRef(x) => x.enforce_parents(expr),
@ -182,7 +180,7 @@ impl ExpressionNode for Expression {
} }
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
use Expression::*; use Expression::*;
match self { match self {
VariableRef(x) => x.get_type(), VariableRef(x) => x.get_type(),
@ -263,65 +261,70 @@ impl ExpressionNode for Expression {
} }
} }
impl FromAst<leo_ast::Expression> for Arc<Expression> { impl<'a> FromAst<'a, leo_ast::Expression> for &'a Expression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::Expression, value: &leo_ast::Expression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<Self, AsgConvertError> { ) -> Result<Self, AsgConvertError> {
use leo_ast::Expression::*; use leo_ast::Expression::*;
let expression = match value { let expression = match value {
Identifier(identifier) => Self::from_ast(scope, identifier, expected_type)?, Identifier(identifier) => Self::from_ast(scope, identifier, expected_type)?,
Value(value) => Arc::new(Constant::from_ast(scope, value, expected_type).map(Expression::Constant)?), Value(value) => {
Binary(binary) => { scope.alloc_expression(Constant::from_ast(scope, value, expected_type).map(Expression::Constant)?)
Arc::new(BinaryExpression::from_ast(scope, binary, expected_type).map(Expression::Binary)?)
} }
Unary(unary) => Arc::new(UnaryExpression::from_ast(scope, unary, expected_type).map(Expression::Unary)?), Binary(binary) => scope
Ternary(conditional) => { .alloc_expression(BinaryExpression::from_ast(scope, binary, expected_type).map(Expression::Binary)?),
Arc::new(TernaryExpression::from_ast(scope, conditional, expected_type).map(Expression::Ternary)?) Unary(unary) => {
scope.alloc_expression(UnaryExpression::from_ast(scope, unary, expected_type).map(Expression::Unary)?)
} }
Ternary(conditional) => scope.alloc_expression(
TernaryExpression::from_ast(scope, conditional, expected_type).map(Expression::Ternary)?,
),
ArrayInline(array_inline) => Arc::new( ArrayInline(array_inline) => scope.alloc_expression(
ArrayInlineExpression::from_ast(scope, array_inline, expected_type).map(Expression::ArrayInline)?, ArrayInlineExpression::from_ast(scope, array_inline, expected_type).map(Expression::ArrayInline)?,
), ),
ArrayInit(array_init) => { ArrayInit(array_init) => scope.alloc_expression(
Arc::new(ArrayInitExpression::from_ast(scope, array_init, expected_type).map(Expression::ArrayInit)?) ArrayInitExpression::from_ast(scope, array_init, expected_type).map(Expression::ArrayInit)?,
} ),
ArrayAccess(array_access) => Arc::new( ArrayAccess(array_access) => scope.alloc_expression(
ArrayAccessExpression::from_ast(scope, array_access, expected_type).map(Expression::ArrayAccess)?, ArrayAccessExpression::from_ast(scope, array_access, expected_type).map(Expression::ArrayAccess)?,
), ),
ArrayRangeAccess(array_range_access) => Arc::new( ArrayRangeAccess(array_range_access) => scope.alloc_expression(
ArrayRangeAccessExpression::from_ast(scope, array_range_access, expected_type) ArrayRangeAccessExpression::from_ast(scope, array_range_access, expected_type)
.map(Expression::ArrayRangeAccess)?, .map(Expression::ArrayRangeAccess)?,
), ),
TupleInit(tuple_init) => { TupleInit(tuple_init) => scope.alloc_expression(
Arc::new(TupleInitExpression::from_ast(scope, tuple_init, expected_type).map(Expression::TupleInit)?) TupleInitExpression::from_ast(scope, tuple_init, expected_type).map(Expression::TupleInit)?,
} ),
TupleAccess(tuple_access) => Arc::new( TupleAccess(tuple_access) => scope.alloc_expression(
TupleAccessExpression::from_ast(scope, tuple_access, expected_type).map(Expression::TupleAccess)?, TupleAccessExpression::from_ast(scope, tuple_access, expected_type).map(Expression::TupleAccess)?,
), ),
CircuitInit(circuit_init) => Arc::new( CircuitInit(circuit_init) => scope.alloc_expression(
CircuitInitExpression::from_ast(scope, circuit_init, expected_type).map(Expression::CircuitInit)?, CircuitInitExpression::from_ast(scope, circuit_init, expected_type).map(Expression::CircuitInit)?,
), ),
CircuitMemberAccess(circuit_member) => Arc::new( CircuitMemberAccess(circuit_member) => scope.alloc_expression(
CircuitAccessExpression::from_ast(scope, circuit_member, expected_type) CircuitAccessExpression::from_ast(scope, circuit_member, expected_type)
.map(Expression::CircuitAccess)?, .map(Expression::CircuitAccess)?,
), ),
CircuitStaticFunctionAccess(circuit_member) => Arc::new( CircuitStaticFunctionAccess(circuit_member) => scope.alloc_expression(
CircuitAccessExpression::from_ast(scope, circuit_member, expected_type) CircuitAccessExpression::from_ast(scope, circuit_member, expected_type)
.map(Expression::CircuitAccess)?, .map(Expression::CircuitAccess)?,
), ),
Call(call) => Arc::new(CallExpression::from_ast(scope, call, expected_type).map(Expression::Call)?), Call(call) => {
scope.alloc_expression(CallExpression::from_ast(scope, call, expected_type).map(Expression::Call)?)
}
}; };
expression.enforce_parents(&expression); expression.enforce_parents(&expression);
Ok(expression) Ok(expression)
} }
} }
impl Into<leo_ast::Expression> for &Expression { impl<'a> Into<leo_ast::Expression> for &Expression<'a> {
fn into(self) -> leo_ast::Expression { fn into(self) -> leo_ast::Expression {
use Expression::*; use Expression::*;
match self { match self {

View File

@ -16,55 +16,52 @@
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct TernaryExpression { pub struct TernaryExpression<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub condition: Arc<Expression>, pub condition: Cell<&'a Expression<'a>>,
pub if_true: Arc<Expression>, pub if_true: Cell<&'a Expression<'a>>,
pub if_false: Arc<Expression>, pub if_false: Cell<&'a Expression<'a>>,
} }
impl Node for TernaryExpression { impl<'a> Node for TernaryExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for TernaryExpression { impl<'a> ExpressionNode<'a> for TernaryExpression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.condition.set_parent(Arc::downgrade(expr)); self.condition.get().set_parent(expr);
self.if_true.set_parent(Arc::downgrade(expr)); self.if_true.get().set_parent(expr);
self.if_false.set_parent(Arc::downgrade(expr)); self.if_false.get().set_parent(expr);
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
self.if_true.get_type() self.if_true.get().get_type()
} }
fn is_mut_ref(&self) -> bool { fn is_mut_ref(&self) -> bool {
self.if_true.is_mut_ref() && self.if_false.is_mut_ref() self.if_true.get().is_mut_ref() && self.if_false.get().is_mut_ref()
} }
fn const_value(&self) -> Option<ConstValue> { fn const_value(&self) -> Option<ConstValue> {
if let Some(ConstValue::Boolean(switch)) = self.condition.const_value() { if let Some(ConstValue::Boolean(switch)) = self.condition.get().const_value() {
if switch { if switch {
self.if_true.const_value() self.if_true.get().const_value()
} else { } else {
self.if_false.const_value() self.if_false.get().const_value()
} }
} else { } else {
None None
@ -72,32 +69,40 @@ impl ExpressionNode for TernaryExpression {
} }
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
self.condition.is_consty() && self.if_true.is_consty() && self.if_false.is_consty() self.condition.get().is_consty() && self.if_true.get().is_consty() && self.if_false.get().is_consty()
} }
} }
impl FromAst<leo_ast::TernaryExpression> for TernaryExpression { impl<'a> FromAst<'a, leo_ast::TernaryExpression> for TernaryExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::TernaryExpression, value: &leo_ast::TernaryExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<TernaryExpression, AsgConvertError> { ) -> Result<TernaryExpression<'a>, AsgConvertError> {
Ok(TernaryExpression { Ok(TernaryExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
condition: Arc::<Expression>::from_ast(scope, &*value.condition, Some(Type::Boolean.partial()))?, condition: Cell::new(<&Expression<'a>>::from_ast(
if_true: Arc::<Expression>::from_ast(scope, &*value.if_true, expected_type.clone())?, scope,
if_false: Arc::<Expression>::from_ast(scope, &*value.if_false, expected_type)?, &*value.condition,
Some(Type::Boolean.partial()),
)?),
if_true: Cell::new(<&Expression<'a>>::from_ast(
scope,
&*value.if_true,
expected_type.clone(),
)?),
if_false: Cell::new(<&Expression<'a>>::from_ast(scope, &*value.if_false, expected_type)?),
}) })
} }
} }
impl Into<leo_ast::TernaryExpression> for &TernaryExpression { impl<'a> Into<leo_ast::TernaryExpression> for &TernaryExpression<'a> {
fn into(self) -> leo_ast::TernaryExpression { fn into(self) -> leo_ast::TernaryExpression {
leo_ast::TernaryExpression { leo_ast::TernaryExpression {
condition: Box::new(self.condition.as_ref().into()), condition: Box::new(self.condition.get().into()),
if_true: Box::new(self.if_true.as_ref().into()), if_true: Box::new(self.if_true.get().into()),
if_false: Box::new(self.if_false.as_ref().into()), if_false: Box::new(self.if_false.get().into()),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -16,51 +16,48 @@
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct TupleAccessExpression { pub struct TupleAccessExpression<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub tuple_ref: Arc<Expression>, pub tuple_ref: Cell<&'a Expression<'a>>,
pub index: usize, pub index: usize,
} }
impl Node for TupleAccessExpression { impl<'a> Node for TupleAccessExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for TupleAccessExpression { impl<'a> ExpressionNode<'a> for TupleAccessExpression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.tuple_ref.set_parent(Arc::downgrade(expr)); self.tuple_ref.get().set_parent(expr);
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
match self.tuple_ref.get_type()? { match self.tuple_ref.get().get_type()? {
Type::Tuple(subtypes) => subtypes.get(self.index).cloned(), Type::Tuple(subtypes) => subtypes.get(self.index).cloned(),
_ => None, _ => None,
} }
} }
fn is_mut_ref(&self) -> bool { fn is_mut_ref(&self) -> bool {
self.tuple_ref.is_mut_ref() self.tuple_ref.get().is_mut_ref()
} }
fn const_value(&self) -> Option<ConstValue> { fn const_value(&self) -> Option<ConstValue> {
let tuple_const = self.tuple_ref.const_value()?; let tuple_const = self.tuple_ref.get().const_value()?;
match tuple_const { match tuple_const {
ConstValue::Tuple(sub_consts) => sub_consts.get(self.index).cloned(), ConstValue::Tuple(sub_consts) => sub_consts.get(self.index).cloned(),
_ => None, _ => None,
@ -68,16 +65,16 @@ impl ExpressionNode for TupleAccessExpression {
} }
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
self.tuple_ref.is_consty() self.tuple_ref.get().is_consty()
} }
} }
impl FromAst<leo_ast::TupleAccessExpression> for TupleAccessExpression { impl<'a> FromAst<'a, leo_ast::TupleAccessExpression> for TupleAccessExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::TupleAccessExpression, value: &leo_ast::TupleAccessExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<TupleAccessExpression, AsgConvertError> { ) -> Result<TupleAccessExpression<'a>, AsgConvertError> {
let index = value let index = value
.index .index
.value .value
@ -87,7 +84,7 @@ impl FromAst<leo_ast::TupleAccessExpression> for TupleAccessExpression {
let mut expected_tuple = vec![None; index + 1]; let mut expected_tuple = vec![None; index + 1];
expected_tuple[index] = expected_type; expected_tuple[index] = expected_type;
let tuple = Arc::<Expression>::from_ast(scope, &*value.tuple, Some(PartialType::Tuple(expected_tuple)))?; let tuple = <&Expression<'a>>::from_ast(scope, &*value.tuple, Some(PartialType::Tuple(expected_tuple)))?;
let tuple_type = tuple.get_type(); let tuple_type = tuple.get_type();
if let Some(Type::Tuple(_items)) = tuple_type { if let Some(Type::Tuple(_items)) = tuple_type {
} else { } else {
@ -99,18 +96,18 @@ impl FromAst<leo_ast::TupleAccessExpression> for TupleAccessExpression {
} }
Ok(TupleAccessExpression { Ok(TupleAccessExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
tuple_ref: tuple, tuple_ref: Cell::new(tuple),
index, index,
}) })
} }
} }
impl Into<leo_ast::TupleAccessExpression> for &TupleAccessExpression { impl<'a> Into<leo_ast::TupleAccessExpression> for &TupleAccessExpression<'a> {
fn into(self) -> leo_ast::TupleAccessExpression { fn into(self) -> leo_ast::TupleAccessExpression {
leo_ast::TupleAccessExpression { leo_ast::TupleAccessExpression {
tuple: Box::new(self.tuple_ref.as_ref().into()), tuple: Box::new(self.tuple_ref.get().into()),
index: leo_ast::PositiveNumber { index: leo_ast::PositiveNumber {
value: self.index.to_string(), value: self.index.to_string(),
}, },

View File

@ -16,43 +16,40 @@
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct TupleInitExpression { pub struct TupleInitExpression<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub elements: Vec<Arc<Expression>>, pub elements: Vec<Cell<&'a Expression<'a>>>,
} }
impl Node for TupleInitExpression { impl<'a> Node for TupleInitExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for TupleInitExpression { impl<'a> ExpressionNode<'a> for TupleInitExpression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.elements.iter().for_each(|element| { self.elements.iter().for_each(|element| {
element.set_parent(Arc::downgrade(expr)); element.get().set_parent(expr);
}) })
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
let mut output = vec![]; let mut output = vec![];
for element in self.elements.iter() { for element in self.elements.iter() {
output.push(element.get_type()?); output.push(element.get().get_type()?);
} }
Some(Type::Tuple(output)) Some(Type::Tuple(output))
} }
@ -64,7 +61,7 @@ impl ExpressionNode for TupleInitExpression {
fn const_value(&self) -> Option<ConstValue> { fn const_value(&self) -> Option<ConstValue> {
let mut consts = vec![]; let mut consts = vec![];
for element in self.elements.iter() { for element in self.elements.iter() {
if let Some(const_value) = element.const_value() { if let Some(const_value) = element.get().const_value() {
consts.push(const_value); consts.push(const_value);
} else { } else {
return None; return None;
@ -74,16 +71,16 @@ impl ExpressionNode for TupleInitExpression {
} }
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
self.elements.iter().all(|x| x.is_consty()) self.elements.iter().all(|x| x.get().is_consty())
} }
} }
impl FromAst<leo_ast::TupleInitExpression> for TupleInitExpression { impl<'a> FromAst<'a, leo_ast::TupleInitExpression> for TupleInitExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::TupleInitExpression, value: &leo_ast::TupleInitExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<TupleInitExpression, AsgConvertError> { ) -> Result<TupleInitExpression<'a>, AsgConvertError> {
let tuple_types = match expected_type { let tuple_types = match expected_type {
Some(PartialType::Tuple(sub_types)) => Some(sub_types), Some(PartialType::Tuple(sub_types)) => Some(sub_types),
None => None, None => None,
@ -111,26 +108,27 @@ impl FromAst<leo_ast::TupleInitExpression> for TupleInitExpression {
.iter() .iter()
.enumerate() .enumerate()
.map(|(i, e)| { .map(|(i, e)| {
Arc::<Expression>::from_ast( <&Expression<'a>>::from_ast(
scope, scope,
e, e,
tuple_types.as_ref().map(|x| x.get(i)).flatten().cloned().flatten(), tuple_types.as_ref().map(|x| x.get(i)).flatten().cloned().flatten(),
) )
.map(Cell::new)
}) })
.collect::<Result<Vec<_>, AsgConvertError>>()?; .collect::<Result<Vec<_>, AsgConvertError>>()?;
Ok(TupleInitExpression { Ok(TupleInitExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
elements, elements,
}) })
} }
} }
impl Into<leo_ast::TupleInitExpression> for &TupleInitExpression { impl<'a> Into<leo_ast::TupleInitExpression> for &TupleInitExpression<'a> {
fn into(self) -> leo_ast::TupleInitExpression { fn into(self) -> leo_ast::TupleInitExpression {
leo_ast::TupleInitExpression { leo_ast::TupleInitExpression {
elements: self.elements.iter().map(|e| e.as_ref().into()).collect(), elements: self.elements.iter().map(|e| e.get().into()).collect(),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -17,40 +17,37 @@
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type}; use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
pub use leo_ast::UnaryOperation; pub use leo_ast::UnaryOperation;
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct UnaryExpression { pub struct UnaryExpression<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub operation: UnaryOperation, pub operation: UnaryOperation,
pub inner: Arc<Expression>, pub inner: Cell<&'a Expression<'a>>,
} }
impl Node for UnaryExpression { impl<'a> Node for UnaryExpression<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for UnaryExpression { impl<'a> ExpressionNode<'a> for UnaryExpression<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, expr: &Arc<Expression>) { fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.inner.set_parent(Arc::downgrade(expr)); self.inner.get().set_parent(expr);
} }
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
self.inner.get_type() self.inner.get().get_type()
} }
fn is_mut_ref(&self) -> bool { fn is_mut_ref(&self) -> bool {
@ -58,7 +55,7 @@ impl ExpressionNode for UnaryExpression {
} }
fn const_value(&self) -> Option<ConstValue> { fn const_value(&self) -> Option<ConstValue> {
if let Some(inner) = self.inner.const_value() { if let Some(inner) = self.inner.get().const_value() {
match self.operation { match self.operation {
UnaryOperation::Not => match inner { UnaryOperation::Not => match inner {
ConstValue::Boolean(value) => Some(ConstValue::Boolean(!value)), ConstValue::Boolean(value) => Some(ConstValue::Boolean(!value)),
@ -79,16 +76,16 @@ impl ExpressionNode for UnaryExpression {
} }
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
self.inner.is_consty() self.inner.get().is_consty()
} }
} }
impl FromAst<leo_ast::UnaryExpression> for UnaryExpression { impl<'a> FromAst<'a, leo_ast::UnaryExpression> for UnaryExpression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::UnaryExpression, value: &leo_ast::UnaryExpression,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<UnaryExpression, AsgConvertError> { ) -> Result<UnaryExpression<'a>, AsgConvertError> {
let expected_type = match value.op { let expected_type = match value.op {
UnaryOperation::Not => match expected_type.map(|x| x.full()).flatten() { UnaryOperation::Not => match expected_type.map(|x| x.full()).flatten() {
Some(Type::Boolean) | None => Some(Type::Boolean), Some(Type::Boolean) | None => Some(Type::Boolean),
@ -115,19 +112,23 @@ impl FromAst<leo_ast::UnaryExpression> for UnaryExpression {
}, },
}; };
Ok(UnaryExpression { Ok(UnaryExpression {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
operation: value.op.clone(), operation: value.op.clone(),
inner: Arc::<Expression>::from_ast(scope, &*value.inner, expected_type.map(Into::into))?, inner: Cell::new(<&Expression<'a>>::from_ast(
scope,
&*value.inner,
expected_type.map(Into::into),
)?),
}) })
} }
} }
impl Into<leo_ast::UnaryExpression> for &UnaryExpression { impl<'a> Into<leo_ast::UnaryExpression> for &UnaryExpression<'a> {
fn into(self) -> leo_ast::UnaryExpression { fn into(self) -> leo_ast::UnaryExpression {
leo_ast::UnaryExpression { leo_ast::UnaryExpression {
op: self.operation.clone(), op: self.operation.clone(),
inner: Box::new(self.inner.as_ref().into()), inner: Box::new(self.inner.get().into()),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -29,39 +29,35 @@ use crate::{
Statement, Statement,
Type, Type,
Variable, Variable,
VariableDeclaration,
}; };
use std::{ use std::cell::Cell;
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct VariableRef { pub struct VariableRef<'a> {
pub parent: RefCell<Option<Weak<Expression>>>, pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub variable: Variable, pub variable: &'a Variable<'a>,
} }
impl Node for VariableRef { impl<'a> Node for VariableRef<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl ExpressionNode for VariableRef { impl<'a> ExpressionNode<'a> for VariableRef<'a> {
fn set_parent(&self, parent: Weak<Expression>) { fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent)); self.parent.replace(Some(parent));
} }
fn get_parent(&self) -> Option<Arc<Expression>> { fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten() self.parent.get()
} }
fn enforce_parents(&self, _expr: &Arc<Expression>) {} fn enforce_parents(&self, _expr: &'a Expression<'a>) {}
fn get_type(&self) -> Option<Type> { fn get_type(&self) -> Option<Type<'a>> {
Some(self.variable.borrow().type_.clone()) Some(self.variable.borrow().type_.clone())
} }
@ -75,24 +71,19 @@ impl ExpressionNode for VariableRef {
if variable.mutable || variable.assignments.len() != 1 { if variable.mutable || variable.assignments.len() != 1 {
return None; return None;
} }
let assignment = variable let assignment = variable.assignments.get(0).unwrap();
.assignments
.get(0)
.unwrap()
.upgrade()
.expect("stale assignment for variable");
match &*assignment { match &*assignment {
Statement::Definition(DefinitionStatement { variables, value, .. }) => { Statement::Definition(DefinitionStatement { variables, value, .. }) => {
if variables.len() == 1 { if variables.len() == 1 {
let defined_variable = variables.get(0).unwrap().borrow(); let defined_variable = variables.get(0).unwrap().borrow();
assert_eq!(variable.id, defined_variable.id); assert_eq!(variable.id, defined_variable.id);
value.const_value() value.get().const_value()
} else { } else {
for defined_variable in variables.iter() { for defined_variable in variables.iter() {
let defined_variable = defined_variable.borrow(); let defined_variable = defined_variable.borrow();
if defined_variable.id == variable.id { if defined_variable.id == variable.id {
return value.const_value(); return value.get().const_value();
} }
} }
panic!("no corresponding tuple variable found during const destructuring (corrupt asg?)"); panic!("no corresponding tuple variable found during const destructuring (corrupt asg?)");
@ -104,18 +95,13 @@ impl ExpressionNode for VariableRef {
fn is_consty(&self) -> bool { fn is_consty(&self) -> bool {
let variable = self.variable.borrow(); let variable = self.variable.borrow();
if variable.declaration == VariableDeclaration::IterationDefinition { if variable.const_ {
return true; return true;
} }
if variable.mutable || variable.assignments.len() != 1 { if variable.mutable || variable.assignments.len() != 1 {
return false; return false;
} }
let assignment = variable let assignment = variable.assignments.get(0).unwrap();
.assignments
.get(0)
.unwrap()
.upgrade()
.expect("stale assignment for variable");
match &*assignment { match &*assignment {
Statement::Definition(DefinitionStatement { variables, value, .. }) => { Statement::Definition(DefinitionStatement { variables, value, .. }) => {
@ -123,12 +109,12 @@ impl ExpressionNode for VariableRef {
let defined_variable = variables.get(0).unwrap().borrow(); let defined_variable = variables.get(0).unwrap().borrow();
assert_eq!(variable.id, defined_variable.id); assert_eq!(variable.id, defined_variable.id);
value.is_consty() value.get().is_consty()
} else { } else {
for defined_variable in variables.iter() { for defined_variable in variables.iter() {
let defined_variable = defined_variable.borrow(); let defined_variable = defined_variable.borrow();
if defined_variable.id == variable.id { if defined_variable.id == variable.id {
return value.is_consty(); return value.get().is_consty();
} }
} }
panic!("no corresponding tuple variable found during const destructuring (corrupt asg?)"); panic!("no corresponding tuple variable found during const destructuring (corrupt asg?)");
@ -140,21 +126,21 @@ impl ExpressionNode for VariableRef {
} }
} }
impl FromAst<leo_ast::Identifier> for Arc<Expression> { impl<'a> FromAst<'a, leo_ast::Identifier> for &'a Expression<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::Identifier, value: &leo_ast::Identifier,
expected_type: Option<PartialType>, expected_type: Option<PartialType<'a>>,
) -> Result<Arc<Expression>, AsgConvertError> { ) -> Result<&'a Expression<'a>, AsgConvertError> {
let variable = if value.name == "input" { let variable = if value.name == "input" {
if let Some(function) = scope.borrow().resolve_current_function() { if let Some(function) = scope.resolve_current_function() {
if !function.has_input { if !function.has_input {
return Err(AsgConvertError::unresolved_reference(&value.name, &value.span)); return Err(AsgConvertError::unresolved_reference(&value.name, &value.span));
} }
} else { } else {
return Err(AsgConvertError::unresolved_reference(&value.name, &value.span)); return Err(AsgConvertError::unresolved_reference(&value.name, &value.span));
} }
if let Some(input) = scope.borrow().resolve_input() { if let Some(input) = scope.resolve_input() {
input.container input.container
} else { } else {
return Err(AsgConvertError::InternalError( return Err(AsgConvertError::InternalError(
@ -162,12 +148,12 @@ impl FromAst<leo_ast::Identifier> for Arc<Expression> {
)); ));
} }
} else { } else {
match scope.borrow().resolve_variable(&value.name) { match scope.resolve_variable(&value.name) {
Some(v) => v, Some(v) => v,
None => { None => {
if value.name.starts_with("aleo1") { if value.name.starts_with("aleo1") {
return Ok(Arc::new(Expression::Constant(Constant { return Ok(scope.alloc_expression(Expression::Constant(Constant {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
value: ConstValue::Address(value.name.clone()), value: ConstValue::Address(value.name.clone()),
}))); })));
@ -178,11 +164,11 @@ impl FromAst<leo_ast::Identifier> for Arc<Expression> {
}; };
let variable_ref = VariableRef { let variable_ref = VariableRef {
parent: RefCell::new(None), parent: Cell::new(None),
span: Some(value.span.clone()), span: Some(value.span.clone()),
variable: variable.clone(), variable,
}; };
let expression = Arc::new(Expression::VariableRef(variable_ref)); let expression = scope.alloc_expression(Expression::VariableRef(variable_ref));
if let Some(expected_type) = expected_type { if let Some(expected_type) = expected_type {
let type_ = expression let type_ = expression
@ -198,13 +184,13 @@ impl FromAst<leo_ast::Identifier> for Arc<Expression> {
} }
let mut variable_ref = variable.borrow_mut(); let mut variable_ref = variable.borrow_mut();
variable_ref.references.push(Arc::downgrade(&expression)); variable_ref.references.push(expression);
Ok(expression) Ok(expression)
} }
} }
impl Into<leo_ast::Identifier> for &VariableRef { impl<'a> Into<leo_ast::Identifier> for &VariableRef<'a> {
fn into(self) -> leo_ast::Identifier { fn into(self) -> leo_ast::Identifier {
self.variable.borrow().name.clone() self.variable.borrow().name.clone()
} }

View File

@ -16,56 +16,74 @@
//! Helper methods for resolving imported packages. //! Helper methods for resolving imported packages.
use crate::{AsgConvertError, Program, Span}; use std::marker::PhantomData;
use crate::{AsgContext, AsgConvertError, Program, Span};
use indexmap::IndexMap; use indexmap::IndexMap;
pub trait ImportResolver { pub trait ImportResolver<'a> {
fn resolve_package(&mut self, package_segments: &[&str], span: &Span) -> Result<Option<Program>, AsgConvertError>; fn resolve_package(
&mut self,
context: AsgContext<'a>,
package_segments: &[&str],
span: &Span,
) -> Result<Option<Program<'a>>, AsgConvertError>;
} }
pub struct NullImportResolver; pub struct NullImportResolver;
impl ImportResolver for NullImportResolver { impl<'a> ImportResolver<'a> for NullImportResolver {
fn resolve_package( fn resolve_package(
&mut self, &mut self,
_context: AsgContext<'a>,
_package_segments: &[&str], _package_segments: &[&str],
_span: &Span, _span: &Span,
) -> Result<Option<Program>, AsgConvertError> { ) -> Result<Option<Program<'a>>, AsgConvertError> {
Ok(None) Ok(None)
} }
} }
pub struct CoreImportResolver<'a, T: ImportResolver + 'static>(pub &'a mut T); pub struct CoreImportResolver<'a, 'b, T: ImportResolver<'b>> {
inner: &'a mut T,
lifetime: PhantomData<&'b ()>,
}
impl<'a, T: ImportResolver + 'static> ImportResolver for CoreImportResolver<'a, T> { impl<'a, 'b, T: ImportResolver<'b>> CoreImportResolver<'a, 'b, T> {
fn resolve_package(&mut self, package_segments: &[&str], span: &Span) -> Result<Option<Program>, AsgConvertError> { pub fn new(inner: &'a mut T) -> Self {
if !package_segments.is_empty() && package_segments.get(0).unwrap() == &"core" { CoreImportResolver {
Ok(crate::resolve_core_module(&*package_segments[1..].join("."))?) inner,
} else { lifetime: PhantomData,
self.0.resolve_package(package_segments, span)
} }
} }
} }
pub struct StandardImportResolver; impl<'a, 'b, T: ImportResolver<'b>> ImportResolver<'b> for CoreImportResolver<'a, 'b, T> {
impl ImportResolver for StandardImportResolver {
fn resolve_package( fn resolve_package(
&mut self, &mut self,
_package_segments: &[&str], context: AsgContext<'b>,
_span: &Span, package_segments: &[&str],
) -> Result<Option<Program>, AsgConvertError> { span: &Span,
Ok(None) ) -> Result<Option<Program<'b>>, AsgConvertError> {
if !package_segments.is_empty() && package_segments.get(0).unwrap() == &"core" {
Ok(crate::resolve_core_module(context, &*package_segments[1..].join("."))?)
} else {
self.inner.resolve_package(context, package_segments, span)
}
} }
} }
pub struct MockedImportResolver { pub struct MockedImportResolver<'a> {
pub packages: IndexMap<String, Program>, pub packages: IndexMap<String, Program<'a>>,
} }
impl ImportResolver for MockedImportResolver { impl<'a> ImportResolver<'a> for MockedImportResolver<'a> {
fn resolve_package(&mut self, package_segments: &[&str], _span: &Span) -> Result<Option<Program>, AsgConvertError> { fn resolve_package(
&mut self,
_context: AsgContext<'a>,
package_segments: &[&str],
_span: &Span,
) -> Result<Option<Program<'a>>, AsgConvertError> {
Ok(self.packages.get(&package_segments.join(".")).cloned()) Ok(self.packages.get(&package_segments.join(".")).cloned())
} }
} }

View File

@ -14,23 +14,20 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{Circuit, CircuitBody, CircuitMember, CircuitMemberBody, Identifier, Scope, Type, Variable, WeakType}; use crate::{Circuit, CircuitMember, Identifier, Scope, Type, Variable};
use indexmap::IndexMap; use indexmap::IndexMap;
use std::{ use std::cell::RefCell;
cell::RefCell,
sync::{Arc, Weak},
};
/// Stores program input values as asg nodes. /// Stores program input values as ASG nodes.
#[derive(Debug, Clone)] #[derive(Clone, Copy)]
pub struct Input { pub struct Input<'a> {
pub registers: Arc<CircuitBody>, pub registers: &'a Circuit<'a>,
pub state: Arc<CircuitBody>, pub state: &'a Circuit<'a>,
pub state_leaf: Arc<CircuitBody>, pub state_leaf: &'a Circuit<'a>,
pub record: Arc<CircuitBody>, pub record: &'a Circuit<'a>,
pub container_circuit: Arc<CircuitBody>, pub container_circuit: &'a Circuit<'a>,
pub container: Variable, pub container: &'a Variable<'a>,
} }
pub const CONTAINER_PSEUDO_CIRCUIT: &str = "$InputContainer"; pub const CONTAINER_PSEUDO_CIRCUIT: &str = "$InputContainer";
@ -39,96 +36,58 @@ pub const RECORD_PSEUDO_CIRCUIT: &str = "$InputRecord";
pub const STATE_PSEUDO_CIRCUIT: &str = "$InputState"; pub const STATE_PSEUDO_CIRCUIT: &str = "$InputState";
pub const STATE_LEAF_PSEUDO_CIRCUIT: &str = "$InputStateLeaf"; pub const STATE_LEAF_PSEUDO_CIRCUIT: &str = "$InputStateLeaf";
impl Input { impl<'a> Input<'a> {
fn make_header(name: &str) -> Arc<Circuit> { fn make_header(scope: &'a Scope<'a>, name: &str) -> &'a Circuit<'a> {
Arc::new(Circuit { scope.alloc_circuit(Circuit {
id: uuid::Uuid::new_v4(), id: scope.context.get_id(),
name: RefCell::new(Identifier::new(name.to_string())), name: RefCell::new(Identifier::new(name.to_string())),
body: RefCell::new(Weak::new()),
members: RefCell::new(IndexMap::new()), members: RefCell::new(IndexMap::new()),
core_mapping: RefCell::new(None), core_mapping: RefCell::new(None),
scope,
span: Default::default(),
}) })
} }
fn make_body(scope: &Scope, circuit: &Arc<Circuit>) -> Arc<CircuitBody> { pub fn new(scope: &'a Scope<'a>) -> Self {
let body = Arc::new(CircuitBody { let input_scope = scope.make_subscope();
scope: scope.clone(), let registers = Self::make_header(input_scope, REGISTERS_PSEUDO_CIRCUIT);
span: None, let record = Self::make_header(input_scope, RECORD_PSEUDO_CIRCUIT);
circuit: circuit.clone(), let state = Self::make_header(input_scope, STATE_PSEUDO_CIRCUIT);
members: RefCell::new(IndexMap::new()), let state_leaf = Self::make_header(input_scope, STATE_LEAF_PSEUDO_CIRCUIT);
});
circuit.body.replace(Arc::downgrade(&body));
body
}
pub fn new(scope: &Scope) -> Self {
let registers = Self::make_header(REGISTERS_PSEUDO_CIRCUIT);
let record = Self::make_header(RECORD_PSEUDO_CIRCUIT);
let state = Self::make_header(STATE_PSEUDO_CIRCUIT);
let state_leaf = Self::make_header(STATE_LEAF_PSEUDO_CIRCUIT);
let mut container_members = IndexMap::new(); let mut container_members = IndexMap::new();
container_members.insert( container_members.insert(
"registers".to_string(), "registers".to_string(),
CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&registers))), CircuitMember::Variable(Type::Circuit(registers)),
);
container_members.insert(
"record".to_string(),
CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&record))),
);
container_members.insert(
"state".to_string(),
CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&state))),
); );
container_members.insert("record".to_string(), CircuitMember::Variable(Type::Circuit(record)));
container_members.insert("state".to_string(), CircuitMember::Variable(Type::Circuit(state)));
container_members.insert( container_members.insert(
"state_leaf".to_string(), "state_leaf".to_string(),
CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&state_leaf))), CircuitMember::Variable(Type::Circuit(state_leaf)),
); );
let container_circuit = Arc::new(Circuit { let container_circuit = input_scope.alloc_circuit(Circuit {
id: uuid::Uuid::new_v4(), id: scope.context.get_id(),
name: RefCell::new(Identifier::new(CONTAINER_PSEUDO_CIRCUIT.to_string())), name: RefCell::new(Identifier::new(CONTAINER_PSEUDO_CIRCUIT.to_string())),
body: RefCell::new(Weak::new()),
members: RefCell::new(container_members), members: RefCell::new(container_members),
core_mapping: RefCell::new(None), core_mapping: RefCell::new(None),
scope: input_scope,
span: Default::default(),
}); });
let registers_body = Self::make_body(scope, &registers);
let record_body = Self::make_body(scope, &record);
let state_body = Self::make_body(scope, &state);
let state_leaf_body = Self::make_body(scope, &state_leaf);
let mut container_body_members = IndexMap::new();
container_body_members.insert(
"registers".to_string(),
CircuitMemberBody::Variable(Type::Circuit(registers)),
);
container_body_members.insert("record".to_string(), CircuitMemberBody::Variable(Type::Circuit(record)));
container_body_members.insert("state".to_string(), CircuitMemberBody::Variable(Type::Circuit(state)));
container_body_members.insert(
"state_leaf".to_string(),
CircuitMemberBody::Variable(Type::Circuit(state_leaf)),
);
let container_circuit_body = Arc::new(CircuitBody {
scope: scope.clone(),
span: None,
circuit: container_circuit.clone(),
members: RefCell::new(container_body_members),
});
container_circuit.body.replace(Arc::downgrade(&container_circuit_body));
Input { Input {
registers: registers_body, registers,
record: record_body, record,
state: state_body, state,
state_leaf: state_leaf_body, state_leaf,
container_circuit: container_circuit_body, container_circuit,
container: Arc::new(RefCell::new(crate::InnerVariable { container: input_scope.alloc_variable(RefCell::new(crate::InnerVariable {
id: uuid::Uuid::new_v4(), id: scope.context.get_id(),
name: Identifier::new("input".to_string()), name: Identifier::new("input".to_string()),
type_: Type::Circuit(container_circuit), type_: Type::Circuit(container_circuit),
mutable: false, mutable: false,
const_: false,
declaration: crate::VariableDeclaration::Input, declaration: crate::VariableDeclaration::Input,
references: vec![], references: vec![],
assignments: vec![], assignments: vec![],
@ -137,7 +96,7 @@ impl Input {
} }
} }
impl Circuit { impl<'a> Circuit<'a> {
pub fn is_input_pseudo_circuit(&self) -> bool { pub fn is_input_pseudo_circuit(&self) -> bool {
matches!( matches!(
&*self.name.borrow().name, &*self.name.borrow().name,

View File

@ -65,11 +65,18 @@ pub mod type_;
pub use type_::*; pub use type_::*;
pub mod variable; pub mod variable;
use typed_arena::Arena;
pub use variable::*; pub use variable::*;
pub mod pass;
pub use pass::*;
pub mod context;
pub use context::*;
pub use leo_ast::{Ast, Identifier, Span}; pub use leo_ast::{Ast, Identifier, Span};
use std::{cell::RefCell, path::Path, sync::Arc}; use std::path::Path;
/// The abstract semantic graph (ASG) for a Leo program. /// The abstract semantic graph (ASG) for a Leo program.
/// ///
@ -77,21 +84,27 @@ use std::{cell::RefCell, path::Path, sync::Arc};
/// These data types form a graph that begins from a [`Program`] type node. /// These data types form a graph that begins from a [`Program`] type node.
/// ///
/// A new [`Asg`] can be created from an [`Ast`] generated in the `ast` module. /// A new [`Asg`] can be created from an [`Ast`] generated in the `ast` module.
#[derive(Debug, Clone)] #[derive(Clone)]
pub struct Asg { pub struct Asg<'a> {
asg: Arc<RefCell<InternalProgram>>, context: AsgContext<'a>,
asg: Program<'a>,
} }
impl Asg { impl<'a> Asg<'a> {
/// Creates a new ASG from a given AST and import resolver. /// Creates a new ASG from a given AST and import resolver.
pub fn new<T: ImportResolver + 'static>(ast: &Ast, resolver: &mut T) -> Result<Self, AsgConvertError> { pub fn new<T: ImportResolver<'a>>(
context: AsgContext<'a>,
ast: &Ast,
resolver: &mut T,
) -> Result<Self, AsgConvertError> {
Ok(Self { Ok(Self {
asg: InternalProgram::new(&ast.as_repr(), resolver)?, context,
asg: InternalProgram::new(context, &ast.as_repr(), resolver)?,
}) })
} }
/// Returns the internal program ASG representation. /// Returns the internal program ASG representation.
pub fn as_repr(&self) -> Arc<RefCell<InternalProgram>> { pub fn as_repr(&self) -> Program<'a> {
self.asg.clone() self.asg.clone()
} }
@ -108,10 +121,22 @@ impl Asg {
} }
// TODO (howardwu): Remove this. // TODO (howardwu): Remove this.
pub fn load_asg<T: ImportResolver + 'static>(content: &str, resolver: &mut T) -> Result<Program, AsgConvertError> { pub fn load_asg<'a, T: ImportResolver<'a>>(
context: AsgContext<'a>,
content: &str,
resolver: &mut T,
) -> Result<Program<'a>, AsgConvertError> {
// Parses the Leo file and constructs a grammar ast. // Parses the Leo file and constructs a grammar ast.
let ast = leo_grammar::Grammar::new(&Path::new("input.leo"), content) let ast = leo_grammar::Grammar::new(&Path::new("input.leo"), content)
.map_err(|e| AsgConvertError::InternalError(format!("ast: {:?}", e)))?; .map_err(|e| AsgConvertError::InternalError(format!("ast: {:?}", e)))?;
InternalProgram::new(leo_ast::Ast::new("load_ast", &ast)?.as_repr(), resolver) InternalProgram::new(context, leo_ast::Ast::new("load_ast", &ast)?.as_repr(), resolver)
}
pub fn new_alloc_context<'a>() -> Arena<ArenaNode<'a>> {
Arena::new()
}
pub fn new_context<'a>(arena: &'a Arena<ArenaNode<'a>>) -> AsgContext<'a> {
AsgContextInner::new(arena)
} }

View File

@ -14,15 +14,40 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{AsgConvertError, PartialType, Scope, Span}; use crate::{
AsgContextInner,
AsgConvertError,
Circuit,
Expression,
Function,
PartialType,
Scope,
Span,
Statement,
Variable,
};
/// A node in the abstract semantic graph. /// A node in the abstract semantic graph.
pub trait Node { pub trait Node {
fn span(&self) -> Option<&Span>; fn span(&self) -> Option<&Span>;
} }
pub(super) trait FromAst<T: leo_ast::Node + 'static>: Sized + 'static { pub(super) trait FromAst<'a, T: leo_ast::Node + 'static>: Sized {
// expected_type contract: if present, output expression must be of type expected_type. // expected_type contract: if present, output expression must be of type expected_type.
// type of an element may NEVER be None unless it is functionally a non-expression. (static call targets, function ref call targets are not expressions) // type of an element may NEVER be None unless it is functionally a non-expression. (static call targets, function ref call targets are not expressions)
fn from_ast(scope: &Scope, value: &T, expected_type: Option<PartialType>) -> Result<Self, AsgConvertError>; fn from_ast(
scope: &'a Scope<'a>,
value: &T,
expected_type: Option<PartialType<'a>>,
) -> Result<Self, AsgConvertError>;
}
pub enum ArenaNode<'a> {
Expression(Expression<'a>),
Scope(Scope<'a>),
Statement(Statement<'a>),
Variable(Variable<'a>),
Circuit(Circuit<'a>),
Function(Function<'a>),
Inner(AsgContextInner<'a>),
} }

View File

@ -14,11 +14,9 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
pub mod cli; use crate::Program;
pub use self::cli::*; pub use leo_ast::Error as FormattedError;
pub mod commands; pub trait AsgPass {
pub use self::commands::*; fn do_pass(asg: &Program) -> Result<(), FormattedError>;
}
pub mod updater;
pub use self::updater::*;

View File

@ -16,15 +16,16 @@
// TODO (protryon): We should merge this with core // TODO (protryon): We should merge this with core
use crate::{AsgConvertError, Program}; use crate::{AsgContext, AsgConvertError, Program};
// TODO (protryon): Make asg deep copy so we can cache resolved core modules // TODO (protryon): Make asg deep copy so we can cache resolved core modules
// TODO (protryon): Figure out how to do headers without bogus returns // TODO (protryon): Figure out how to do headers without bogus returns
pub fn resolve_core_module(module: &str) -> Result<Option<Program>, AsgConvertError> { pub fn resolve_core_module<'a>(context: AsgContext<'a>, module: &str) -> Result<Option<Program<'a>>, AsgConvertError> {
match module { match module {
"unstable.blake2s" => { "unstable.blake2s" => {
let asg = crate::load_asg( let asg = crate::load_asg(
context,
r#" r#"
circuit Blake2s { circuit Blake2s {
function hash(seed: [u8; 32], message: [u8; 32]) -> [u8; 32] { function hash(seed: [u8; 32], message: [u8; 32]) -> [u8; 32] {
@ -34,7 +35,7 @@ pub fn resolve_core_module(module: &str) -> Result<Option<Program>, AsgConvertEr
"#, "#,
&mut crate::NullImportResolver, &mut crate::NullImportResolver,
)?; )?;
asg.borrow().set_core_mapping("blake2s"); asg.set_core_mapping("blake2s");
Ok(Some(asg)) Ok(Some(asg))
} }
_ => Ok(None), _ => Ok(None),

View File

@ -14,37 +14,28 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{AsgConvertError, Function, FunctionBody, Identifier, InnerScope, Node, Scope, Span, Type, WeakType}; use crate::{AsgConvertError, Function, Identifier, Node, Scope, Span, Type};
use indexmap::IndexMap; use indexmap::IndexMap;
use std::{ use std::cell::RefCell;
cell::RefCell,
sync::{Arc, Weak},
};
use uuid::Uuid;
#[derive(Debug)] #[derive(Clone)]
pub enum CircuitMemberBody { pub enum CircuitMember<'a> {
Variable(Type), Variable(Type<'a>),
Function(Arc<FunctionBody>), Function(&'a Function<'a>),
} }
#[derive(Debug)] #[derive(Clone)]
pub enum CircuitMember { pub struct Circuit<'a> {
Variable(WeakType), pub id: u32,
Function(Arc<Function>),
}
#[derive(Debug)]
pub struct Circuit {
pub id: Uuid,
pub name: RefCell<Identifier>, pub name: RefCell<Identifier>,
pub core_mapping: RefCell<Option<String>>, pub core_mapping: RefCell<Option<String>>,
pub body: RefCell<Weak<CircuitBody>>, pub scope: &'a Scope<'a>,
pub members: RefCell<IndexMap<String, CircuitMember>>, pub span: Option<Span>,
pub members: RefCell<IndexMap<String, CircuitMember<'a>>>,
} }
impl PartialEq for Circuit { impl<'a> PartialEq for Circuit<'a> {
fn eq(&self, other: &Circuit) -> bool { fn eq(&self, other: &Circuit) -> bool {
if self.name != other.name { if self.name != other.name {
return false; return false;
@ -52,81 +43,30 @@ impl PartialEq for Circuit {
self.id == other.id self.id == other.id
} }
} }
impl Eq for Circuit {}
#[derive(Debug)] impl<'a> Eq for Circuit<'a> {}
pub struct CircuitBody {
pub scope: Scope,
pub span: Option<Span>,
pub circuit: Arc<Circuit>,
pub members: RefCell<IndexMap<String, CircuitMemberBody>>,
}
impl PartialEq for CircuitBody { impl<'a> Node for Circuit<'a> {
fn eq(&self, other: &CircuitBody) -> bool {
self.circuit == other.circuit
}
}
impl Eq for CircuitBody {}
impl Node for CircuitMemberBody {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
None self.span.as_ref()
} }
} }
impl Circuit { impl<'a> Circuit<'a> {
pub(super) fn init(value: &leo_ast::Circuit) -> Arc<Circuit> { pub(super) fn init(scope: &'a Scope<'a>, value: &leo_ast::Circuit) -> Result<&'a Circuit<'a>, AsgConvertError> {
Arc::new(Circuit { let new_scope = scope.make_subscope();
id: Uuid::new_v4(),
let circuit = scope.alloc_circuit(Circuit {
id: scope.context.get_id(),
name: RefCell::new(value.circuit_name.clone()), name: RefCell::new(value.circuit_name.clone()),
body: RefCell::new(Weak::new()),
members: RefCell::new(IndexMap::new()), members: RefCell::new(IndexMap::new()),
core_mapping: RefCell::new(None), core_mapping: RefCell::new(None),
}) span: Some(value.circuit_name.span.clone()),
} scope: new_scope,
});
pub(super) fn from_ast(self: Arc<Circuit>, scope: &Scope, value: &leo_ast::Circuit) -> Result<(), AsgConvertError> { new_scope.circuit_self.replace(Some(circuit));
let new_scope = InnerScope::make_subscope(scope); // temporary scope for function headers
new_scope.borrow_mut().circuit_self = Some(self.clone());
let mut members = self.members.borrow_mut();
for member in value.members.iter() {
match member {
leo_ast::CircuitMember::CircuitVariable(name, type_) => {
members.insert(
name.name.clone(),
CircuitMember::Variable(new_scope.borrow().resolve_ast_type(type_)?.into()),
);
}
leo_ast::CircuitMember::CircuitFunction(function) => {
let asg_function = Arc::new(Function::from_ast(&new_scope, function)?);
members.insert(function.identifier.name.clone(), CircuitMember::Function(asg_function));
}
}
}
for (_, member) in members.iter() {
if let CircuitMember::Function(func) = member {
func.circuit.borrow_mut().replace(Arc::downgrade(&self));
}
}
Ok(())
}
}
impl CircuitBody {
pub(super) fn from_ast(
scope: &Scope,
value: &leo_ast::Circuit,
circuit: Arc<Circuit>,
) -> Result<CircuitBody, AsgConvertError> {
let mut members = IndexMap::new();
let new_scope = InnerScope::make_subscope(scope);
new_scope.borrow_mut().circuit_self = Some(circuit.clone());
let mut members = circuit.members.borrow_mut();
for member in value.members.iter() { for member in value.members.iter() {
match member { match member {
leo_ast::CircuitMember::CircuitVariable(name, type_) => { leo_ast::CircuitMember::CircuitVariable(name, type_) => {
@ -139,7 +79,7 @@ impl CircuitBody {
} }
members.insert( members.insert(
name.name.clone(), name.name.clone(),
CircuitMemberBody::Variable(new_scope.borrow().resolve_ast_type(type_)?), CircuitMember::Variable(new_scope.resolve_ast_type(type_)?),
); );
} }
leo_ast::CircuitMember::CircuitFunction(function) => { leo_ast::CircuitMember::CircuitFunction(function) => {
@ -150,51 +90,51 @@ impl CircuitBody {
&function.identifier.span, &function.identifier.span,
)); ));
} }
let asg_function = { let asg_function = Function::init(new_scope, function)?;
let circuit_members = circuit.members.borrow(); asg_function.circuit.replace(Some(circuit));
match circuit_members.get(&function.identifier.name).unwrap() { members.insert(function.identifier.name.clone(), CircuitMember::Function(asg_function));
CircuitMember::Function(f) => f.clone(),
_ => unimplemented!(),
}
};
let function_body = Arc::new(FunctionBody::from_ast(&new_scope, function, asg_function.clone())?);
asg_function.body.replace(Arc::downgrade(&function_body));
members.insert(
function.identifier.name.clone(),
CircuitMemberBody::Function(function_body),
);
} }
} }
} }
Ok(CircuitBody { Ok(circuit)
span: Some(value.circuit_name.span.clone()), }
circuit,
members: RefCell::new(members), pub(super) fn fill_from_ast(self: &'a Circuit<'a>, value: &leo_ast::Circuit) -> Result<(), AsgConvertError> {
scope: scope.clone(), for member in value.members.iter() {
}) match member {
leo_ast::CircuitMember::CircuitVariable(..) => {}
leo_ast::CircuitMember::CircuitFunction(function) => {
let asg_function = match *self
.members
.borrow()
.get(&function.identifier.name)
.expect("missing header for defined circuit function")
{
CircuitMember::Function(f) => f,
_ => unimplemented!(),
};
Function::fill_from_ast(asg_function, function)?;
}
}
}
Ok(())
} }
} }
impl Into<leo_ast::Circuit> for &Circuit { impl<'a> Into<leo_ast::Circuit> for &Circuit<'a> {
fn into(self) -> leo_ast::Circuit { fn into(self) -> leo_ast::Circuit {
let members = match self.body.borrow().upgrade() { let members = self
Some(body) => body .members
.members .borrow()
.borrow() .iter()
.iter() .map(|(name, member)| match &member {
.map(|(name, member)| match &member { CircuitMember::Variable(type_) => {
CircuitMemberBody::Variable(type_) => { leo_ast::CircuitMember::CircuitVariable(Identifier::new(name.clone()), type_.into())
leo_ast::CircuitMember::CircuitVariable(Identifier::new(name.clone()), type_.into()) }
} CircuitMember::Function(func) => leo_ast::CircuitMember::CircuitFunction((*func).into()),
CircuitMemberBody::Function(func) => { })
leo_ast::CircuitMember::CircuitFunction(func.function.as_ref().into()) .collect();
}
})
.collect(),
None => vec![],
};
leo_ast::Circuit { leo_ast::Circuit {
circuit_name: self.name.borrow().clone(), circuit_name: self.name.borrow().clone(),
members, members,

View File

@ -20,7 +20,6 @@ use crate::{
Circuit, Circuit,
FromAst, FromAst,
Identifier, Identifier,
InnerScope,
MonoidalDirector, MonoidalDirector,
ReturnPathReducer, ReturnPathReducer,
Scope, Scope,
@ -28,73 +27,57 @@ use crate::{
Statement, Statement,
Type, Type,
Variable, Variable,
WeakType,
}; };
use indexmap::IndexMap;
use leo_ast::FunctionInput; use leo_ast::FunctionInput;
use std::{ use std::cell::{Cell, RefCell};
cell::RefCell,
sync::{Arc, Weak},
};
use uuid::Uuid;
#[derive(Debug, PartialEq)] #[derive(Clone, Copy, PartialEq)]
pub enum FunctionQualifier { pub enum FunctionQualifier {
SelfRef, SelfRef,
MutSelfRef, MutSelfRef,
Static, Static,
} }
#[derive(Debug)] #[derive(Clone)]
pub struct Function { pub struct Function<'a> {
pub id: Uuid, pub id: u32,
pub name: RefCell<Identifier>, pub name: RefCell<Identifier>,
pub output: WeakType, pub output: Type<'a>,
pub has_input: bool, pub has_input: bool,
pub argument_types: Vec<WeakType>, pub arguments: IndexMap<String, Cell<&'a Variable<'a>>>,
pub circuit: RefCell<Option<Weak<Circuit>>>, pub circuit: Cell<Option<&'a Circuit<'a>>>,
pub body: RefCell<Weak<FunctionBody>>, pub span: Option<Span>,
pub body: Cell<Option<&'a Statement<'a>>>,
pub scope: &'a Scope<'a>,
pub qualifier: FunctionQualifier, pub qualifier: FunctionQualifier,
} }
impl PartialEq for Function { impl<'a> PartialEq for Function<'a> {
fn eq(&self, other: &Function) -> bool { fn eq(&self, other: &Function<'a>) -> bool {
if self.name.borrow().name != other.name.borrow().name { if self.name.borrow().name != other.name.borrow().name {
return false; return false;
} }
self.id == other.id self.id == other.id
} }
} }
impl Eq for Function {}
#[derive(Debug)] impl<'a> Eq for Function<'a> {}
pub struct FunctionBody {
pub span: Option<Span>,
pub function: Arc<Function>,
pub arguments: Vec<Variable>,
pub body: Arc<Statement>,
pub scope: Scope,
}
impl PartialEq for FunctionBody { impl<'a> Function<'a> {
fn eq(&self, other: &FunctionBody) -> bool { pub(crate) fn init(scope: &'a Scope<'a>, value: &leo_ast::Function) -> Result<&'a Function<'a>, AsgConvertError> {
self.function == other.function let output: Type<'a> = value
}
}
impl Eq for FunctionBody {}
impl Function {
pub(crate) fn from_ast(scope: &Scope, value: &leo_ast::Function) -> Result<Function, AsgConvertError> {
let output: Type = value
.output .output
.as_ref() .as_ref()
.map(|t| scope.borrow().resolve_ast_type(t)) .map(|t| scope.resolve_ast_type(t))
.transpose()? .transpose()?
.unwrap_or_else(|| Type::Tuple(vec![])); .unwrap_or_else(|| Type::Tuple(vec![]));
let mut qualifier = FunctionQualifier::Static; let mut qualifier = FunctionQualifier::Static;
let mut has_input = false; let mut has_input = false;
let new_scope = scope.make_subscope();
let mut argument_types = vec![]; let mut arguments = IndexMap::new();
{ {
for input in value.input.iter() { for input in value.input.iter() {
match input { match input {
@ -107,83 +90,75 @@ impl Function {
FunctionInput::MutSelfKeyword(_) => { FunctionInput::MutSelfKeyword(_) => {
qualifier = FunctionQualifier::MutSelfRef; qualifier = FunctionQualifier::MutSelfRef;
} }
FunctionInput::Variable(leo_ast::FunctionInputVariable { type_, .. }) => {
argument_types.push(scope.borrow().resolve_ast_type(&type_)?.into());
}
}
}
}
if qualifier != FunctionQualifier::Static && scope.borrow().circuit_self.is_none() {
return Err(AsgConvertError::invalid_self_in_global(&value.span));
}
Ok(Function {
id: Uuid::new_v4(),
name: RefCell::new(value.identifier.clone()),
output: output.into(),
has_input,
argument_types,
circuit: RefCell::new(None),
body: RefCell::new(Weak::new()),
qualifier,
})
}
}
impl FunctionBody {
pub(super) fn from_ast(
scope: &Scope,
value: &leo_ast::Function,
function: Arc<Function>,
) -> Result<FunctionBody, AsgConvertError> {
let new_scope = InnerScope::make_subscope(scope);
let mut arguments = vec![];
{
let mut scope_borrow = new_scope.borrow_mut();
if function.qualifier != FunctionQualifier::Static {
let circuit = function.circuit.borrow();
let self_variable = Arc::new(RefCell::new(crate::InnerVariable {
id: Uuid::new_v4(),
name: Identifier::new("self".to_string()),
type_: Type::Circuit(circuit.as_ref().unwrap().upgrade().unwrap()),
mutable: function.qualifier == FunctionQualifier::MutSelfRef,
declaration: crate::VariableDeclaration::Parameter,
references: vec![],
assignments: vec![],
}));
scope_borrow.variables.insert("self".to_string(), self_variable);
}
scope_borrow.function = Some(function.clone());
for input in value.input.iter() {
match input {
FunctionInput::InputKeyword(_) => {}
FunctionInput::SelfKeyword(_) => {}
FunctionInput::MutSelfKeyword(_) => {}
FunctionInput::Variable(leo_ast::FunctionInputVariable { FunctionInput::Variable(leo_ast::FunctionInputVariable {
identifier,
mutable,
type_, type_,
span: _span, identifier,
const_,
mutable,
..
}) => { }) => {
let variable = Arc::new(RefCell::new(crate::InnerVariable { let variable = scope.alloc_variable(RefCell::new(crate::InnerVariable {
id: Uuid::new_v4(), id: scope.context.get_id(),
name: identifier.clone(), name: identifier.clone(),
type_: scope_borrow.resolve_ast_type(&type_)?, type_: scope.resolve_ast_type(&type_)?,
mutable: *mutable, mutable: *mutable,
const_: *const_,
declaration: crate::VariableDeclaration::Parameter, declaration: crate::VariableDeclaration::Parameter,
references: vec![], references: vec![],
assignments: vec![], assignments: vec![],
})); }));
arguments.push(variable.clone()); arguments.insert(identifier.name.clone(), Cell::new(&*variable));
scope_borrow.variables.insert(identifier.name.clone(), variable);
} }
} }
} }
} }
let main_block = BlockStatement::from_ast(&new_scope, &value.block, None)?; if qualifier != FunctionQualifier::Static && scope.circuit_self.get().is_none() {
return Err(AsgConvertError::invalid_self_in_global(&value.span));
}
let function = scope.alloc_function(Function {
id: scope.context.get_id(),
name: RefCell::new(value.identifier.clone()),
output,
has_input,
arguments,
circuit: Cell::new(None),
body: Cell::new(None),
qualifier,
scope: new_scope,
span: Some(value.span.clone()),
});
function.scope.function.replace(Some(function));
Ok(function)
}
pub(super) fn fill_from_ast(self: &'a Function<'a>, value: &leo_ast::Function) -> Result<(), AsgConvertError> {
if self.qualifier != FunctionQualifier::Static {
let circuit = self.circuit.get();
let self_variable = self.scope.alloc_variable(RefCell::new(crate::InnerVariable {
id: self.scope.context.get_id(),
name: Identifier::new("self".to_string()),
type_: Type::Circuit(circuit.as_ref().unwrap()),
mutable: self.qualifier == FunctionQualifier::MutSelfRef,
const_: false,
declaration: crate::VariableDeclaration::Parameter,
references: vec![],
assignments: vec![],
}));
self.scope
.variables
.borrow_mut()
.insert("self".to_string(), self_variable);
}
for (name, argument) in self.arguments.iter() {
self.scope.variables.borrow_mut().insert(name.clone(), argument.get());
}
let main_block = BlockStatement::from_ast(self.scope, &value.block, None)?;
let mut director = MonoidalDirector::new(ReturnPathReducer::new()); let mut director = MonoidalDirector::new(ReturnPathReducer::new());
if !director.reduce_block(&main_block).0 && !function.output.is_unit() { if !director.reduce_block(&main_block).0 && !self.output.is_unit() {
return Err(AsgConvertError::function_missing_return( return Err(AsgConvertError::function_missing_return(
&function.name.borrow().name, &self.name.borrow().name,
&value.span, &value.span,
)); ));
} }
@ -191,46 +166,39 @@ impl FunctionBody {
#[allow(clippy::never_loop)] // TODO @Protryon: How should we return multiple errors? #[allow(clippy::never_loop)] // TODO @Protryon: How should we return multiple errors?
for (span, error) in director.reducer().errors { for (span, error) in director.reducer().errors {
return Err(AsgConvertError::function_return_validation( return Err(AsgConvertError::function_return_validation(
&function.name.borrow().name, &self.name.borrow().name,
&error, &error,
&span, &span,
)); ));
} }
Ok(FunctionBody { self.body
span: Some(value.span.clone()), .replace(Some(self.scope.alloc_statement(Statement::Block(main_block))));
function,
arguments, Ok(())
body: Arc::new(Statement::Block(main_block)),
scope: new_scope,
})
} }
} }
impl Into<leo_ast::Function> for &Function { impl<'a> Into<leo_ast::Function> for &Function<'a> {
fn into(self) -> leo_ast::Function { fn into(self) -> leo_ast::Function {
let (input, body, span) = match self.body.borrow().upgrade() { let input = self
Some(body) => ( .arguments
body.arguments .iter()
.iter() .map(|(_, variable)| {
.map(|variable| { let variable = variable.get().borrow();
let variable = variable.borrow(); leo_ast::FunctionInput::Variable(leo_ast::FunctionInputVariable {
leo_ast::FunctionInput::Variable(leo_ast::FunctionInputVariable { identifier: variable.name.clone(),
identifier: variable.name.clone(), mutable: variable.mutable,
mutable: variable.mutable, const_: variable.const_,
type_: (&variable.type_).into(), type_: (&variable.type_).into(),
span: Span::default(), span: Span::default(),
}) })
}) })
.collect(), .collect();
match body.body.as_ref() { let (body, span) = match self.body.get() {
Statement::Block(block) => block.into(), Some(Statement::Block(block)) => (block.into(), block.span.clone().unwrap_or_default()),
_ => unimplemented!(), Some(_) => unimplemented!(),
},
body.span.clone().unwrap_or_default(),
),
None => ( None => (
vec![],
leo_ast::Block { leo_ast::Block {
statements: vec![], statements: vec![],
span: Default::default(), span: Default::default(),
@ -238,7 +206,7 @@ impl Into<leo_ast::Function> for &Function {
Default::default(), Default::default(),
), ),
}; };
let output: Type = self.output.clone().into(); let output: Type = self.output.clone();
leo_ast::Function { leo_ast::Function {
identifier: self.name.borrow().clone(), identifier: self.name.borrow().clone(),
input, input,

View File

@ -24,42 +24,44 @@ pub use circuit::*;
mod function; mod function;
pub use function::*; pub use function::*;
use crate::{AsgConvertError, ImportResolver, InnerScope, Input, Scope}; use crate::{ArenaNode, AsgContext, AsgConvertError, ImportResolver, Input, Scope};
use leo_ast::{Identifier, Package, PackageAccess, Span}; use leo_ast::{Identifier, PackageAccess, PackageOrPackages, Span};
use indexmap::IndexMap; use indexmap::IndexMap;
use std::{cell::RefCell, sync::Arc}; use std::cell::{Cell, RefCell};
use uuid::Uuid;
/// Stores the Leo program abstract semantic graph (ASG). /// Stores the Leo program abstract semantic graph (ASG).
#[derive(Debug, Clone)] #[derive(Clone)]
pub struct InternalProgram { pub struct InternalProgram<'a> {
pub context: AsgContext<'a>,
/// The unique id of the program. /// The unique id of the program.
pub id: Uuid, pub id: u32,
/// The program file name. /// The program file name.
pub name: String, pub name: String,
/// The packages imported by this program. /// The packages imported by this program.
/// these should generally not be accessed directly, but through scoped imports /// these should generally not be accessed directly, but through scoped imports
pub imported_modules: IndexMap<String, Program>, pub imported_modules: IndexMap<String, Program<'a>>,
/// Maps test name => test code block. /// Maps test name => test code block.
pub test_functions: IndexMap<String, (Arc<FunctionBody>, Option<Identifier>)>, // identifier = test input file pub test_functions: IndexMap<String, (&'a Function<'a>, Option<Identifier>)>, // identifier = test input file
/// Maps function name => function code block. /// Maps function name => function code block.
pub functions: IndexMap<String, Arc<FunctionBody>>, pub functions: IndexMap<String, &'a Function<'a>>,
/// Maps circuit name => circuit code block. /// Maps circuit name => circuit code block.
pub circuits: IndexMap<String, Arc<CircuitBody>>, pub circuits: IndexMap<String, &'a Circuit<'a>>,
/// Bindings for names and additional program context. /// Bindings for names and additional program context.
pub scope: Scope, pub scope: &'a Scope<'a>,
} }
pub type Program = Arc<RefCell<InternalProgram>>; pub type Program<'a> = InternalProgram<'a>;
/// Enumerates what names are imported from a package. /// Enumerates what names are imported from a package.
#[derive(Clone)]
enum ImportSymbol { enum ImportSymbol {
/// Import the symbol by name. /// Import the symbol by name.
Direct(String), Direct(String),
@ -74,16 +76,25 @@ enum ImportSymbol {
fn resolve_import_package( fn resolve_import_package(
output: &mut Vec<(Vec<String>, ImportSymbol, Span)>, output: &mut Vec<(Vec<String>, ImportSymbol, Span)>,
mut package_segments: Vec<String>, mut package_segments: Vec<String>,
package: &Package, package_or_packages: &PackageOrPackages,
) { ) {
package_segments.push(package.name.name.clone()); match package_or_packages {
PackageOrPackages::Package(package) => {
resolve_import_package_access(output, package_segments, &package.access); package_segments.push(package.name.name.clone());
resolve_import_package_access(output, package_segments, &package.access);
}
PackageOrPackages::Packages(packages) => {
package_segments.push(packages.name.name.clone());
for access in packages.accesses.clone() {
resolve_import_package_access(output, package_segments.clone(), &access);
}
}
}
} }
fn resolve_import_package_access( fn resolve_import_package_access(
output: &mut Vec<(Vec<String>, ImportSymbol, Span)>, output: &mut Vec<(Vec<String>, ImportSymbol, Span)>,
package_segments: Vec<String>, mut package_segments: Vec<String>,
package: &PackageAccess, package: &PackageAccess,
) { ) {
match package { match package {
@ -91,7 +102,11 @@ fn resolve_import_package_access(
output.push((package_segments, ImportSymbol::All, span.clone())); output.push((package_segments, ImportSymbol::All, span.clone()));
} }
PackageAccess::SubPackage(subpackage) => { PackageAccess::SubPackage(subpackage) => {
resolve_import_package(output, package_segments, &*subpackage); resolve_import_package(
output,
package_segments,
&PackageOrPackages::Package(*(*subpackage).clone()),
);
} }
PackageAccess::Symbol(symbol) => { PackageAccess::Symbol(symbol) => {
let span = symbol.symbol.span.clone(); let span = symbol.symbol.span.clone();
@ -102,15 +117,16 @@ fn resolve_import_package_access(
}; };
output.push((package_segments, symbol, span)); output.push((package_segments, symbol, span));
} }
PackageAccess::Multiple(subaccesses) => { PackageAccess::Multiple(packages) => {
for subaccess in subaccesses.iter() { package_segments.push(packages.name.name.clone());
for subaccess in packages.accesses.iter() {
resolve_import_package_access(output, package_segments.clone(), &subaccess); resolve_import_package_access(output, package_segments.clone(), &subaccess);
} }
} }
} }
} }
impl InternalProgram { impl<'a> InternalProgram<'a> {
/// Returns a new Leo program ASG from the given Leo program AST and its imports. /// Returns a new Leo program ASG from the given Leo program AST and its imports.
/// ///
/// Stages: /// Stages:
@ -119,14 +135,15 @@ impl InternalProgram {
/// 3. finalize declared functions /// 3. finalize declared functions
/// 4. resolve all asg nodes /// 4. resolve all asg nodes
/// ///
pub fn new<T: ImportResolver + 'static>( pub fn new<T: ImportResolver<'a>>(
context: AsgContext<'a>,
program: &leo_ast::Program, program: &leo_ast::Program,
import_resolver: &mut T, import_resolver: &mut T,
) -> Result<Program, AsgConvertError> { ) -> Result<Program<'a>, AsgConvertError> {
// Recursively extract imported symbols. // Recursively extract imported symbols.
let mut imported_symbols: Vec<(Vec<String>, ImportSymbol, Span)> = vec![]; let mut imported_symbols: Vec<(Vec<String>, ImportSymbol, Span)> = vec![];
for import in program.imports.iter() { for import in program.imports.iter() {
resolve_import_package(&mut imported_symbols, vec![], &import.package); resolve_import_package(&mut imported_symbols, vec![], &import.package_or_packages);
} }
// Create package list. // Create package list.
@ -135,24 +152,27 @@ impl InternalProgram {
deduplicated_imports.insert(package.clone(), span.clone()); deduplicated_imports.insert(package.clone(), span.clone());
} }
let mut wrapped_resolver = crate::CoreImportResolver(import_resolver); let mut wrapped_resolver = crate::CoreImportResolver::new(import_resolver);
// Load imported programs. // Load imported programs.
let mut resolved_packages: IndexMap<Vec<String>, Program> = IndexMap::new(); let mut resolved_packages: IndexMap<Vec<String>, Program> = IndexMap::new();
for (package, span) in deduplicated_imports.iter() { for (package, span) in deduplicated_imports.iter() {
let pretty_package = package.join("."); let pretty_package = package.join(".");
let resolved_package = let resolved_package = match wrapped_resolver.resolve_package(
match wrapped_resolver.resolve_package(&package.iter().map(|x| &**x).collect::<Vec<_>>()[..], span)? { context,
Some(x) => x, &package.iter().map(|x| &**x).collect::<Vec<_>>()[..],
None => return Err(AsgConvertError::unresolved_import(&*pretty_package, &Span::default())), span,
}; )? {
Some(x) => x,
None => return Err(AsgConvertError::unresolved_import(&*pretty_package, &Span::default())),
};
resolved_packages.insert(package.clone(), resolved_package); resolved_packages.insert(package.clone(), resolved_package);
} }
let mut imported_functions: IndexMap<String, Arc<FunctionBody>> = IndexMap::new(); let mut imported_functions: IndexMap<String, &'a Function<'a>> = IndexMap::new();
let mut imported_circuits: IndexMap<String, Arc<CircuitBody>> = IndexMap::new(); let mut imported_circuits: IndexMap<String, &'a Circuit<'a>> = IndexMap::new();
// Prepare locally relevant scope of imports. // Prepare locally relevant scope of imports.
for (package, symbol, span) in imported_symbols.into_iter() { for (package, symbol, span) in imported_symbols.into_iter() {
@ -161,7 +181,6 @@ impl InternalProgram {
let resolved_package = resolved_packages let resolved_package = resolved_packages
.get(&package) .get(&package)
.expect("could not find preloaded package"); .expect("could not find preloaded package");
let resolved_package = resolved_package.borrow();
match symbol { match symbol {
ImportSymbol::All => { ImportSymbol::All => {
imported_functions.extend(resolved_package.functions.clone().into_iter()); imported_functions.extend(resolved_package.functions.clone().into_iter());
@ -169,9 +188,9 @@ impl InternalProgram {
} }
ImportSymbol::Direct(name) => { ImportSymbol::Direct(name) => {
if let Some(function) = resolved_package.functions.get(&name) { if let Some(function) = resolved_package.functions.get(&name) {
imported_functions.insert(name.clone(), function.clone()); imported_functions.insert(name.clone(), *function);
} else if let Some(function) = resolved_package.circuits.get(&name) { } else if let Some(circuit) = resolved_package.circuits.get(&name) {
imported_circuits.insert(name.clone(), function.clone()); imported_circuits.insert(name.clone(), *circuit);
} else { } else {
return Err(AsgConvertError::unresolved_import( return Err(AsgConvertError::unresolved_import(
&*format!("{}.{}", pretty_package, name), &*format!("{}.{}", pretty_package, name),
@ -181,9 +200,9 @@ impl InternalProgram {
} }
ImportSymbol::Alias(name, alias) => { ImportSymbol::Alias(name, alias) => {
if let Some(function) = resolved_package.functions.get(&name) { if let Some(function) = resolved_package.functions.get(&name) {
imported_functions.insert(alias.clone(), function.clone()); imported_functions.insert(alias.clone(), *function);
} else if let Some(function) = resolved_package.circuits.get(&name) { } else if let Some(circuit) = resolved_package.circuits.get(&name) {
imported_circuits.insert(alias.clone(), function.clone()); imported_circuits.insert(alias.clone(), *circuit);
} else { } else {
return Err(AsgConvertError::unresolved_import( return Err(AsgConvertError::unresolved_import(
&*format!("{}.{}", pretty_package, name), &*format!("{}.{}", pretty_package, name),
@ -194,71 +213,54 @@ impl InternalProgram {
} }
} }
let import_scope = Arc::new(RefCell::new(InnerScope { let import_scope = match context.arena.alloc(ArenaNode::Scope(Scope {
id: uuid::Uuid::new_v4(), context,
parent_scope: None, id: context.get_id(),
circuit_self: None, parent_scope: Cell::new(None),
variables: IndexMap::new(), circuit_self: Cell::new(None),
functions: imported_functions variables: RefCell::new(IndexMap::new()),
.iter() functions: RefCell::new(imported_functions),
.map(|(name, func)| (name.clone(), func.function.clone())) circuits: RefCell::new(imported_circuits),
.collect(), function: Cell::new(None),
circuits: imported_circuits input: Cell::new(None),
.iter() })) {
.map(|(name, circuit)| (name.clone(), circuit.circuit.clone())) ArenaNode::Scope(c) => c,
.collect(), _ => unimplemented!(),
function: None, };
input: None,
})); let scope = import_scope.alloc_scope(Scope {
context,
input: Cell::new(Some(Input::new(import_scope))), // we use import_scope to avoid recursive scope ref here
id: context.get_id(),
parent_scope: Cell::new(Some(import_scope)),
circuit_self: Cell::new(None),
variables: RefCell::new(IndexMap::new()),
functions: RefCell::new(IndexMap::new()),
circuits: RefCell::new(IndexMap::new()),
function: Cell::new(None),
});
// Prepare header-like scope entries. // Prepare header-like scope entries.
let mut proto_circuits = IndexMap::new();
for (name, circuit) in program.circuits.iter() { for (name, circuit) in program.circuits.iter() {
assert_eq!(name.name, circuit.circuit_name.name); assert_eq!(name.name, circuit.circuit_name.name);
let asg_circuit = Circuit::init(circuit); let asg_circuit = Circuit::init(scope, circuit)?;
proto_circuits.insert(name.name.clone(), asg_circuit); scope.circuits.borrow_mut().insert(name.name.clone(), asg_circuit);
}
let scope = Arc::new(RefCell::new(InnerScope {
input: Some(Input::new(&import_scope)), // we use import_scope to avoid recursive scope ref here
id: uuid::Uuid::new_v4(),
parent_scope: Some(import_scope),
circuit_self: None,
variables: IndexMap::new(),
functions: IndexMap::new(),
circuits: proto_circuits
.iter()
.map(|(name, circuit)| (name.clone(), circuit.clone()))
.collect(),
function: None,
}));
for (name, circuit) in program.circuits.iter() {
assert_eq!(name.name, circuit.circuit_name.name);
let asg_circuit = proto_circuits.get(&name.name).unwrap();
asg_circuit.clone().from_ast(&scope, &circuit)?;
} }
let mut proto_test_functions = IndexMap::new(); let mut proto_test_functions = IndexMap::new();
for (name, test_function) in program.tests.iter() { for (name, test_function) in program.tests.iter() {
assert_eq!(name.name, test_function.function.identifier.name); assert_eq!(name.name, test_function.function.identifier.name);
let function = Arc::new(Function::from_ast(&scope, &test_function.function)?); let function = Function::init(scope, &test_function.function)?;
proto_test_functions.insert(name.name.clone(), function); proto_test_functions.insert(name.name.clone(), function);
} }
let mut proto_functions = IndexMap::new();
for (name, function) in program.functions.iter() { for (name, function) in program.functions.iter() {
assert_eq!(name.name, function.identifier.name); assert_eq!(name.name, function.identifier.name);
let asg_function = Arc::new(Function::from_ast(&scope, function)?); let function = Function::init(scope, function)?;
scope scope.functions.borrow_mut().insert(name.name.clone(), function);
.borrow_mut()
.functions
.insert(name.name.clone(), asg_function.clone());
proto_functions.insert(name.name.clone(), asg_function);
} }
// Load concrete definitions. // Load concrete definitions.
@ -267,39 +269,34 @@ impl InternalProgram {
assert_eq!(name.name, test_function.function.identifier.name); assert_eq!(name.name, test_function.function.identifier.name);
let function = proto_test_functions.get(&name.name).unwrap(); let function = proto_test_functions.get(&name.name).unwrap();
let body = Arc::new(FunctionBody::from_ast( function.fill_from_ast(&test_function.function)?;
&scope,
&test_function.function,
function.clone(),
)?);
function.body.replace(Arc::downgrade(&body));
test_functions.insert(name.name.clone(), (body, test_function.input_file.clone())); test_functions.insert(name.name.clone(), (*function, test_function.input_file.clone()));
} }
let mut functions = IndexMap::new(); let mut functions = IndexMap::new();
for (name, function) in program.functions.iter() { for (name, function) in program.functions.iter() {
assert_eq!(name.name, function.identifier.name); assert_eq!(name.name, function.identifier.name);
let asg_function = proto_functions.get(&name.name).unwrap(); let asg_function = *scope.functions.borrow().get(&name.name).unwrap();
let body = Arc::new(FunctionBody::from_ast(&scope, function, asg_function.clone())?); asg_function.fill_from_ast(function)?;
asg_function.body.replace(Arc::downgrade(&body));
functions.insert(name.name.clone(), body); functions.insert(name.name.clone(), asg_function);
} }
let mut circuits = IndexMap::new(); let mut circuits = IndexMap::new();
for (name, circuit) in program.circuits.iter() { for (name, circuit) in program.circuits.iter() {
assert_eq!(name.name, circuit.circuit_name.name); assert_eq!(name.name, circuit.circuit_name.name);
let asg_circuit = proto_circuits.get(&name.name).unwrap(); let asg_circuit = *scope.circuits.borrow().get(&name.name).unwrap();
let body = Arc::new(CircuitBody::from_ast(&scope, circuit, asg_circuit.clone())?);
asg_circuit.body.replace(Arc::downgrade(&body));
circuits.insert(name.name.clone(), body); asg_circuit.fill_from_ast(circuit)?;
circuits.insert(name.name.clone(), asg_circuit);
} }
Ok(Arc::new(RefCell::new(InternalProgram { Ok(InternalProgram {
id: Uuid::new_v4(), context,
id: context.get_id(),
name: program.name.clone(), name: program.name.clone(),
test_functions, test_functions,
functions, functions,
@ -309,12 +306,12 @@ impl InternalProgram {
.map(|(package, program)| (package.join("."), program)) .map(|(package, program)| (package.join("."), program))
.collect(), .collect(),
scope, scope,
}))) })
} }
pub(crate) fn set_core_mapping(&self, mapping: &str) { pub(crate) fn set_core_mapping(&self, mapping: &str) {
for (_, circuit) in self.circuits.iter() { for (_, circuit) in self.circuits.iter() {
circuit.circuit.core_mapping.replace(Some(mapping.to_string())); circuit.core_mapping.replace(Some(mapping.to_string()));
} }
} }
} }
@ -333,15 +330,15 @@ impl Iterator for InternalIdentifierGenerator {
} }
} }
/// Returns an AST from the given ASG program. /// Returns an AST from the given ASG program.
pub fn reform_ast(program: &Program) -> leo_ast::Program { pub fn reform_ast<'a>(program: &Program<'a>) -> leo_ast::Program {
let mut all_programs: IndexMap<String, Program> = IndexMap::new(); let mut all_programs: IndexMap<String, Program> = IndexMap::new();
let mut program_stack = program.borrow().imported_modules.clone(); let mut program_stack = program.imported_modules.clone();
while let Some((module, program)) = program_stack.pop() { while let Some((module, program)) = program_stack.pop() {
if all_programs.contains_key(&module) { if all_programs.contains_key(&module) {
continue; continue;
} }
all_programs.insert(module, program.clone()); all_programs.insert(module, program.clone());
program_stack.extend(program.borrow().imported_modules.clone()); program_stack.extend(program.imported_modules.clone());
} }
all_programs.insert("".to_string(), program.clone()); all_programs.insert("".to_string(), program.clone());
let core_programs: Vec<_> = all_programs let core_programs: Vec<_> = all_programs
@ -351,16 +348,15 @@ pub fn reform_ast(program: &Program) -> leo_ast::Program {
.collect(); .collect();
all_programs.retain(|module, _| !module.starts_with("core.")); all_programs.retain(|module, _| !module.starts_with("core."));
let mut all_circuits: IndexMap<String, Arc<CircuitBody>> = IndexMap::new(); let mut all_circuits: IndexMap<String, &'a Circuit<'a>> = IndexMap::new();
let mut all_functions: IndexMap<String, Arc<FunctionBody>> = IndexMap::new(); let mut all_functions: IndexMap<String, &'a Function<'a>> = IndexMap::new();
let mut all_test_functions: IndexMap<String, (Arc<FunctionBody>, Option<Identifier>)> = IndexMap::new(); let mut all_test_functions: IndexMap<String, (&'a Function<'a>, Option<Identifier>)> = IndexMap::new();
let mut identifiers = InternalIdentifierGenerator { next: 0 }; let mut identifiers = InternalIdentifierGenerator { next: 0 };
for (_, program) in all_programs.into_iter() { for (_, program) in all_programs.into_iter() {
let program = program.borrow();
for (name, circuit) in program.circuits.iter() { for (name, circuit) in program.circuits.iter() {
let identifier = format!("{}{}", identifiers.next().unwrap(), name); let identifier = format!("{}{}", identifiers.next().unwrap(), name);
circuit.circuit.name.borrow_mut().name = identifier.clone(); circuit.name.borrow_mut().name = identifier.clone();
all_circuits.insert(identifier, circuit.clone()); all_circuits.insert(identifier, *circuit);
} }
for (name, function) in program.functions.iter() { for (name, function) in program.functions.iter() {
let identifier = if name == "main" { let identifier = if name == "main" {
@ -368,12 +364,12 @@ pub fn reform_ast(program: &Program) -> leo_ast::Program {
} else { } else {
format!("{}{}", identifiers.next().unwrap(), name) format!("{}{}", identifiers.next().unwrap(), name)
}; };
function.function.name.borrow_mut().name = identifier.clone(); function.name.borrow_mut().name = identifier.clone();
all_functions.insert(identifier, function.clone()); all_functions.insert(identifier, *function);
} }
for (name, function) in program.test_functions.iter() { for (name, function) in program.test_functions.iter() {
let identifier = format!("{}{}", identifiers.next().unwrap(), name); let identifier = format!("{}{}", identifiers.next().unwrap(), name);
function.0.function.name.borrow_mut().name = identifier.clone(); function.0.name.borrow_mut().name = identifier.clone();
all_test_functions.insert(identifier, function.clone()); all_test_functions.insert(identifier, function.clone());
} }
} }
@ -383,11 +379,11 @@ pub fn reform_ast(program: &Program) -> leo_ast::Program {
imports: core_programs imports: core_programs
.iter() .iter()
.map(|(module, _)| leo_ast::ImportStatement { .map(|(module, _)| leo_ast::ImportStatement {
package: leo_ast::Package { package_or_packages: leo_ast::PackageOrPackages::Package(leo_ast::Package {
name: Identifier::new(module.clone()), name: Identifier::new(module.clone()),
access: leo_ast::PackageAccess::Star(Span::default()), access: leo_ast::PackageAccess::Star(Span::default()),
span: Default::default(), span: Default::default(),
}, }),
span: Span::default(), span: Span::default(),
}) })
.collect(), .collect(),
@ -395,29 +391,24 @@ pub fn reform_ast(program: &Program) -> leo_ast::Program {
tests: all_test_functions tests: all_test_functions
.into_iter() .into_iter()
.map(|(_, (function, ident))| { .map(|(_, (function, ident))| {
(function.function.name.borrow().clone(), leo_ast::TestFunction { (function.name.borrow().clone(), leo_ast::TestFunction {
function: function.function.as_ref().into(), function: function.into(),
input_file: ident, input_file: ident,
}) })
}) })
.collect(), .collect(),
functions: all_functions functions: all_functions
.into_iter() .into_iter()
.map(|(_, function)| { .map(|(_, function)| (function.name.borrow().clone(), function.into()))
(
function.function.name.borrow().clone(),
function.function.as_ref().into(),
)
})
.collect(), .collect(),
circuits: all_circuits circuits: all_circuits
.into_iter() .into_iter()
.map(|(_, circuit)| (circuit.circuit.name.borrow().clone(), circuit.circuit.as_ref().into())) .map(|(_, circuit)| (circuit.name.borrow().clone(), circuit.into()))
.collect(), .collect(),
} }
} }
impl Into<leo_ast::Program> for &InternalProgram { impl<'a> Into<leo_ast::Program> for &InternalProgram<'a> {
fn into(self) -> leo_ast::Program { fn into(self) -> leo_ast::Program {
leo_ast::Program { leo_ast::Program {
name: self.name.clone(), name: self.name.clone(),
@ -426,24 +417,19 @@ impl Into<leo_ast::Program> for &InternalProgram {
circuits: self circuits: self
.circuits .circuits
.iter() .iter()
.map(|(_, circuit)| (circuit.circuit.name.borrow().clone(), circuit.circuit.as_ref().into())) .map(|(_, circuit)| (circuit.name.borrow().clone(), (*circuit).into()))
.collect(), .collect(),
functions: self functions: self
.functions .functions
.iter() .iter()
.map(|(_, function)| { .map(|(_, function)| (function.name.borrow().clone(), (*function).into()))
(
function.function.name.borrow().clone(),
function.function.as_ref().into(),
)
})
.collect(), .collect(),
tests: self tests: self
.test_functions .test_functions
.iter() .iter()
.map(|(_, function)| { .map(|(_, function)| {
(function.0.function.name.borrow().clone(), leo_ast::TestFunction { (function.0.name.borrow().clone(), leo_ast::TestFunction {
function: function.0.function.as_ref().into(), function: function.0.into(),
input_file: function.1.clone(), input_file: function.1.clone(),
}) })
}) })

View File

@ -25,3 +25,9 @@ pub use monoidal_director::*;
mod monoidal_reducer; mod monoidal_reducer;
pub use monoidal_reducer::*; pub use monoidal_reducer::*;
mod visitor;
pub use visitor::*;
mod visitor_director;
pub use visitor_director::*;

View File

@ -17,14 +17,14 @@
use super::*; use super::*;
use crate::{expression::*, program::*, statement::*}; use crate::{expression::*, program::*, statement::*};
use std::{marker::PhantomData, sync::Arc}; use std::marker::PhantomData;
pub struct MonoidalDirector<T: Monoid, R: MonoidalReducerExpression<T>> { pub struct MonoidalDirector<'a, T: Monoid, R: MonoidalReducerExpression<'a, T>> {
reducer: R, reducer: R,
_monoid: PhantomData<T>, _monoid: PhantomData<&'a T>,
} }
impl<T: Monoid, R: MonoidalReducerExpression<T>> MonoidalDirector<T, R> { impl<'a, T: Monoid, R: MonoidalReducerExpression<'a, T>> MonoidalDirector<'a, T, R> {
pub fn new(reducer: R) -> Self { pub fn new(reducer: R) -> Self {
Self { Self {
reducer, reducer,
@ -36,8 +36,8 @@ impl<T: Monoid, R: MonoidalReducerExpression<T>> MonoidalDirector<T, R> {
self.reducer self.reducer
} }
pub fn reduce_expression(&mut self, input: &Arc<Expression>) -> T { pub fn reduce_expression(&mut self, input: &'a Expression<'a>) -> T {
match &**input { let value = match input {
Expression::ArrayAccess(e) => self.reduce_array_access(e), Expression::ArrayAccess(e) => self.reduce_array_access(e),
Expression::ArrayInit(e) => self.reduce_array_init(e), Expression::ArrayInit(e) => self.reduce_array_init(e),
Expression::ArrayInline(e) => self.reduce_array_inline(e), Expression::ArrayInline(e) => self.reduce_array_inline(e),
@ -52,101 +52,115 @@ impl<T: Monoid, R: MonoidalReducerExpression<T>> MonoidalDirector<T, R> {
Expression::TupleInit(e) => self.reduce_tuple_init(e), Expression::TupleInit(e) => self.reduce_tuple_init(e),
Expression::Unary(e) => self.reduce_unary(e), Expression::Unary(e) => self.reduce_unary(e),
Expression::VariableRef(e) => self.reduce_variable_ref(e), Expression::VariableRef(e) => self.reduce_variable_ref(e),
} };
self.reducer.reduce_expression(input, value)
} }
pub fn reduce_array_access(&mut self, input: &ArrayAccessExpression) -> T { pub fn reduce_array_access(&mut self, input: &ArrayAccessExpression<'a>) -> T {
let array = self.reduce_expression(&input.array); let array = self.reduce_expression(input.array.get());
let index = self.reduce_expression(&input.index); let index = self.reduce_expression(input.index.get());
self.reducer.reduce_array_access(input, array, index) self.reducer.reduce_array_access(input, array, index)
} }
pub fn reduce_array_init(&mut self, input: &ArrayInitExpression) -> T { pub fn reduce_array_init(&mut self, input: &ArrayInitExpression<'a>) -> T {
let element = self.reduce_expression(&input.element); let element = self.reduce_expression(input.element.get());
self.reducer.reduce_array_init(input, element) self.reducer.reduce_array_init(input, element)
} }
pub fn reduce_array_inline(&mut self, input: &ArrayInlineExpression) -> T { pub fn reduce_array_inline(&mut self, input: &ArrayInlineExpression<'a>) -> T {
let elements = input.elements.iter().map(|(x, _)| self.reduce_expression(x)).collect(); let elements = input
.elements
.iter()
.map(|(x, _)| self.reduce_expression(x.get()))
.collect();
self.reducer.reduce_array_inline(input, elements) self.reducer.reduce_array_inline(input, elements)
} }
pub fn reduce_array_range_access(&mut self, input: &ArrayRangeAccessExpression) -> T { pub fn reduce_array_range_access(&mut self, input: &ArrayRangeAccessExpression<'a>) -> T {
let array = self.reduce_expression(&input.array); let array = self.reduce_expression(input.array.get());
let left = input.left.as_ref().map(|e| self.reduce_expression(e)); let left = input.left.get().map(|e| self.reduce_expression(e));
let right = input.right.as_ref().map(|e| self.reduce_expression(e)); let right = input.right.get().map(|e| self.reduce_expression(e));
self.reducer.reduce_array_range_access(input, array, left, right) self.reducer.reduce_array_range_access(input, array, left, right)
} }
pub fn reduce_binary(&mut self, input: &BinaryExpression) -> T { pub fn reduce_binary(&mut self, input: &BinaryExpression<'a>) -> T {
let left = self.reduce_expression(&input.left); let left = self.reduce_expression(input.left.get());
let right = self.reduce_expression(&input.right); let right = self.reduce_expression(input.right.get());
self.reducer.reduce_binary(input, left, right) self.reducer.reduce_binary(input, left, right)
} }
pub fn reduce_call(&mut self, input: &CallExpression) -> T { pub fn reduce_call(&mut self, input: &CallExpression<'a>) -> T {
let target = input.target.as_ref().map(|e| self.reduce_expression(e)); let target = input.target.get().map(|e| self.reduce_expression(e));
let arguments = input.arguments.iter().map(|e| self.reduce_expression(e)).collect(); let arguments = input
.arguments
.iter()
.map(|e| self.reduce_expression(e.get()))
.collect();
self.reducer.reduce_call(input, target, arguments) self.reducer.reduce_call(input, target, arguments)
} }
pub fn reduce_circuit_access(&mut self, input: &CircuitAccessExpression) -> T { pub fn reduce_circuit_access(&mut self, input: &CircuitAccessExpression<'a>) -> T {
let target = input.target.as_ref().map(|e| self.reduce_expression(e)); let target = input.target.get().map(|e| self.reduce_expression(e));
self.reducer.reduce_circuit_access(input, target) self.reducer.reduce_circuit_access(input, target)
} }
pub fn reduce_circuit_init(&mut self, input: &CircuitInitExpression) -> T { pub fn reduce_circuit_init(&mut self, input: &CircuitInitExpression<'a>) -> T {
let values = input.values.iter().map(|(_, e)| self.reduce_expression(e)).collect(); let values = input
.values
.iter()
.map(|(_, e)| self.reduce_expression(e.get()))
.collect();
self.reducer.reduce_circuit_init(input, values) self.reducer.reduce_circuit_init(input, values)
} }
pub fn reduce_ternary_expression(&mut self, input: &TernaryExpression) -> T { pub fn reduce_ternary_expression(&mut self, input: &TernaryExpression<'a>) -> T {
let condition = self.reduce_expression(&input.condition); let condition = self.reduce_expression(input.condition.get());
let if_true = self.reduce_expression(&input.if_true); let if_true = self.reduce_expression(input.if_true.get());
let if_false = self.reduce_expression(&input.if_false); let if_false = self.reduce_expression(input.if_false.get());
self.reducer self.reducer
.reduce_ternary_expression(input, condition, if_true, if_false) .reduce_ternary_expression(input, condition, if_true, if_false)
} }
pub fn reduce_constant(&mut self, input: &Constant) -> T { pub fn reduce_constant(&mut self, input: &Constant<'a>) -> T {
self.reducer.reduce_constant(input) self.reducer.reduce_constant(input)
} }
pub fn reduce_tuple_access(&mut self, input: &TupleAccessExpression) -> T { pub fn reduce_tuple_access(&mut self, input: &TupleAccessExpression<'a>) -> T {
let tuple_ref = self.reduce_expression(&input.tuple_ref); let tuple_ref = self.reduce_expression(input.tuple_ref.get());
self.reducer.reduce_tuple_access(input, tuple_ref) self.reducer.reduce_tuple_access(input, tuple_ref)
} }
pub fn reduce_tuple_init(&mut self, input: &TupleInitExpression) -> T { pub fn reduce_tuple_init(&mut self, input: &TupleInitExpression<'a>) -> T {
let values = input.elements.iter().map(|e| self.reduce_expression(e)).collect(); let values = input.elements.iter().map(|e| self.reduce_expression(e.get())).collect();
self.reducer.reduce_tuple_init(input, values) self.reducer.reduce_tuple_init(input, values)
} }
pub fn reduce_unary(&mut self, input: &UnaryExpression) -> T { pub fn reduce_unary(&mut self, input: &UnaryExpression<'a>) -> T {
let inner = self.reduce_expression(&input.inner); let inner = self.reduce_expression(input.inner.get());
self.reducer.reduce_unary(input, inner) self.reducer.reduce_unary(input, inner)
} }
pub fn reduce_variable_ref(&mut self, input: &VariableRef) -> T { pub fn reduce_variable_ref(&mut self, input: &VariableRef<'a>) -> T {
self.reducer.reduce_variable_ref(input) self.reducer.reduce_variable_ref(input)
} }
} }
impl<T: Monoid, R: MonoidalReducerStatement<T>> MonoidalDirector<T, R> { impl<'a, T: Monoid, R: MonoidalReducerStatement<'a, T>> MonoidalDirector<'a, T, R> {
pub fn reduce_statement(&mut self, input: &Arc<Statement>) -> T { pub fn reduce_statement(&mut self, input: &'a Statement<'a>) -> T {
match &**input { let value = match input {
Statement::Assign(s) => self.reduce_assign(s), Statement::Assign(s) => self.reduce_assign(s),
Statement::Block(s) => self.reduce_block(s), Statement::Block(s) => self.reduce_block(s),
Statement::Conditional(s) => self.reduce_conditional_statement(s), Statement::Conditional(s) => self.reduce_conditional_statement(s),
@ -155,57 +169,67 @@ impl<T: Monoid, R: MonoidalReducerStatement<T>> MonoidalDirector<T, R> {
Statement::Expression(s) => self.reduce_expression_statement(s), Statement::Expression(s) => self.reduce_expression_statement(s),
Statement::Iteration(s) => self.reduce_iteration(s), Statement::Iteration(s) => self.reduce_iteration(s),
Statement::Return(s) => self.reduce_return(s), Statement::Return(s) => self.reduce_return(s),
} };
self.reducer.reduce_statement(input, value)
} }
pub fn reduce_assign_access(&mut self, input: &AssignAccess) -> T { pub fn reduce_assign_access(&mut self, input: &AssignAccess<'a>) -> T {
let (left, right) = match input { let (left, right) = match input {
AssignAccess::ArrayRange(left, right) => ( AssignAccess::ArrayRange(left, right) => (
left.as_ref().map(|e| self.reduce_expression(e)), left.get().map(|e| self.reduce_expression(e)),
right.as_ref().map(|e| self.reduce_expression(e)), right.get().map(|e| self.reduce_expression(e)),
), ),
AssignAccess::ArrayIndex(index) => (Some(self.reduce_expression(index)), None), AssignAccess::ArrayIndex(index) => (Some(self.reduce_expression(index.get())), None),
_ => (None, None), _ => (None, None),
}; };
self.reducer.reduce_assign_access(input, left, right) self.reducer.reduce_assign_access(input, left, right)
} }
pub fn reduce_assign(&mut self, input: &AssignStatement) -> T { pub fn reduce_assign(&mut self, input: &AssignStatement<'a>) -> T {
let accesses = input let accesses = input
.target_accesses .target_accesses
.iter() .iter()
.map(|x| self.reduce_assign_access(x)) .map(|x| self.reduce_assign_access(x))
.collect(); .collect();
let value = self.reduce_expression(&input.value); let value = self.reduce_expression(input.value.get());
self.reducer.reduce_assign(input, accesses, value) self.reducer.reduce_assign(input, accesses, value)
} }
pub fn reduce_block(&mut self, input: &BlockStatement) -> T { pub fn reduce_block(&mut self, input: &BlockStatement<'a>) -> T {
let statements = input.statements.iter().map(|x| self.reduce_statement(x)).collect(); let statements = input
.statements
.iter()
.map(|x| self.reduce_statement(x.get()))
.collect();
self.reducer.reduce_block(input, statements) self.reducer.reduce_block(input, statements)
} }
pub fn reduce_conditional_statement(&mut self, input: &ConditionalStatement) -> T { pub fn reduce_conditional_statement(&mut self, input: &ConditionalStatement<'a>) -> T {
let condition = self.reduce_expression(&input.condition); let condition = self.reduce_expression(input.condition.get());
let if_true = self.reduce_statement(&input.result); let if_true = self.reduce_statement(input.result.get());
let if_false = input.next.as_ref().map(|s| self.reduce_statement(s)); let if_false = input.next.get().map(|s| self.reduce_statement(s));
self.reducer self.reducer
.reduce_conditional_statement(input, condition, if_true, if_false) .reduce_conditional_statement(input, condition, if_true, if_false)
} }
pub fn reduce_formatted_string(&mut self, input: &FormattedString) -> T { pub fn reduce_formatted_string(&mut self, input: &FormattedString<'a>) -> T {
let parameters = input.parameters.iter().map(|e| self.reduce_expression(e)).collect(); let parameters = input
.parameters
.iter()
.map(|e| self.reduce_expression(e.get()))
.collect();
self.reducer.reduce_formatted_string(input, parameters) self.reducer.reduce_formatted_string(input, parameters)
} }
pub fn reduce_console(&mut self, input: &ConsoleStatement) -> T { pub fn reduce_console(&mut self, input: &ConsoleStatement<'a>) -> T {
let argument = match &input.function { let argument = match &input.function {
ConsoleFunction::Assert(e) => self.reduce_expression(e), ConsoleFunction::Assert(e) => self.reduce_expression(e.get()),
ConsoleFunction::Debug(f) | ConsoleFunction::Error(f) | ConsoleFunction::Log(f) => { ConsoleFunction::Debug(f) | ConsoleFunction::Error(f) | ConsoleFunction::Log(f) => {
self.reduce_formatted_string(f) self.reduce_formatted_string(f)
} }
@ -214,51 +238,51 @@ impl<T: Monoid, R: MonoidalReducerStatement<T>> MonoidalDirector<T, R> {
self.reducer.reduce_console(input, argument) self.reducer.reduce_console(input, argument)
} }
pub fn reduce_definition(&mut self, input: &DefinitionStatement) -> T { pub fn reduce_definition(&mut self, input: &DefinitionStatement<'a>) -> T {
let value = self.reduce_expression(&input.value); let value = self.reduce_expression(input.value.get());
self.reducer.reduce_definition(input, value) self.reducer.reduce_definition(input, value)
} }
pub fn reduce_expression_statement(&mut self, input: &ExpressionStatement) -> T { pub fn reduce_expression_statement(&mut self, input: &ExpressionStatement<'a>) -> T {
let value = self.reduce_expression(&input.expression); let value = self.reduce_expression(input.expression.get());
self.reducer.reduce_expression_statement(input, value) self.reducer.reduce_expression_statement(input, value)
} }
pub fn reduce_iteration(&mut self, input: &IterationStatement) -> T { pub fn reduce_iteration(&mut self, input: &IterationStatement<'a>) -> T {
let start = self.reduce_expression(&input.start); let start = self.reduce_expression(input.start.get());
let stop = self.reduce_expression(&input.stop); let stop = self.reduce_expression(input.stop.get());
let body = self.reduce_statement(&input.body); let body = self.reduce_statement(input.body.get());
self.reducer.reduce_iteration(input, start, stop, body) self.reducer.reduce_iteration(input, start, stop, body)
} }
pub fn reduce_return(&mut self, input: &ReturnStatement) -> T { pub fn reduce_return(&mut self, input: &ReturnStatement<'a>) -> T {
let value = self.reduce_expression(&input.expression); let value = self.reduce_expression(input.expression.get());
self.reducer.reduce_return(input, value) self.reducer.reduce_return(input, value)
} }
} }
#[allow(dead_code)] #[allow(dead_code)]
impl<T: Monoid, R: MonoidalReducerProgram<T>> MonoidalDirector<T, R> { impl<'a, T: Monoid, R: MonoidalReducerProgram<'a, T>> MonoidalDirector<'a, T, R> {
fn reduce_function(&mut self, input: &Arc<FunctionBody>) -> T { fn reduce_function(&mut self, input: &'a Function<'a>) -> T {
let body = self.reduce_statement(&input.body); let body = input.body.get().map(|s| self.reduce_statement(s)).unwrap_or_default();
self.reducer.reduce_function(input, body) self.reducer.reduce_function(input, body)
} }
fn reduce_circuit_member(&mut self, input: &CircuitMemberBody) -> T { fn reduce_circuit_member(&mut self, input: &CircuitMember<'a>) -> T {
let function = match input { let function = match input {
CircuitMemberBody::Function(f) => Some(self.reduce_function(f)), CircuitMember::Function(f) => Some(self.reduce_function(f)),
_ => None, _ => None,
}; };
self.reducer.reduce_circuit_member(input, function) self.reducer.reduce_circuit_member(input, function)
} }
fn reduce_circuit(&mut self, input: &Arc<CircuitBody>) -> T { fn reduce_circuit(&mut self, input: &'a Circuit<'a>) -> T {
let members = input let members = input
.members .members
.borrow() .borrow()
@ -269,8 +293,7 @@ impl<T: Monoid, R: MonoidalReducerProgram<T>> MonoidalDirector<T, R> {
self.reducer.reduce_circuit(input, members) self.reducer.reduce_circuit(input, members)
} }
fn reduce_program(&mut self, input: &Program) -> T { fn reduce_program(&mut self, input: &Program<'a>) -> T {
let input = input.borrow();
let imported_modules = input let imported_modules = input
.imported_modules .imported_modules
.iter() .iter()

View File

@ -16,29 +16,27 @@
use crate::{expression::*, program::*, statement::*, Monoid}; use crate::{expression::*, program::*, statement::*, Monoid};
use std::sync::Arc;
#[allow(unused_variables)] #[allow(unused_variables)]
pub trait MonoidalReducerExpression<T: Monoid> { pub trait MonoidalReducerExpression<'a, T: Monoid> {
fn reduce_expression(&mut self, input: &Arc<Expression>, value: T) -> T { fn reduce_expression(&mut self, input: &'a Expression<'a>, value: T) -> T {
value value
} }
fn reduce_array_access(&mut self, input: &ArrayAccessExpression, array: T, index: T) -> T { fn reduce_array_access(&mut self, input: &ArrayAccessExpression<'a>, array: T, index: T) -> T {
array.append(index) array.append(index)
} }
fn reduce_array_init(&mut self, input: &ArrayInitExpression, element: T) -> T { fn reduce_array_init(&mut self, input: &ArrayInitExpression<'a>, element: T) -> T {
element element
} }
fn reduce_array_inline(&mut self, input: &ArrayInlineExpression, elements: Vec<T>) -> T { fn reduce_array_inline(&mut self, input: &ArrayInlineExpression<'a>, elements: Vec<T>) -> T {
T::default().append_all(elements.into_iter()) T::default().append_all(elements.into_iter())
} }
fn reduce_array_range_access( fn reduce_array_range_access(
&mut self, &mut self,
input: &ArrayRangeAccessExpression, input: &ArrayRangeAccessExpression<'a>,
array: T, array: T,
left: Option<T>, left: Option<T>,
right: Option<T>, right: Option<T>,
@ -46,69 +44,69 @@ pub trait MonoidalReducerExpression<T: Monoid> {
array.append_option(left).append_option(right) array.append_option(left).append_option(right)
} }
fn reduce_binary(&mut self, input: &BinaryExpression, left: T, right: T) -> T { fn reduce_binary(&mut self, input: &BinaryExpression<'a>, left: T, right: T) -> T {
left.append(right) left.append(right)
} }
fn reduce_call(&mut self, input: &CallExpression, target: Option<T>, arguments: Vec<T>) -> T { fn reduce_call(&mut self, input: &CallExpression<'a>, target: Option<T>, arguments: Vec<T>) -> T {
target.unwrap_or_default().append_all(arguments.into_iter()) target.unwrap_or_default().append_all(arguments.into_iter())
} }
fn reduce_circuit_access(&mut self, input: &CircuitAccessExpression, target: Option<T>) -> T { fn reduce_circuit_access(&mut self, input: &CircuitAccessExpression<'a>, target: Option<T>) -> T {
target.unwrap_or_default() target.unwrap_or_default()
} }
fn reduce_circuit_init(&mut self, input: &CircuitInitExpression, values: Vec<T>) -> T { fn reduce_circuit_init(&mut self, input: &CircuitInitExpression<'a>, values: Vec<T>) -> T {
T::default().append_all(values.into_iter()) T::default().append_all(values.into_iter())
} }
fn reduce_ternary_expression(&mut self, input: &TernaryExpression, condition: T, if_true: T, if_false: T) -> T { fn reduce_ternary_expression(&mut self, input: &TernaryExpression<'a>, condition: T, if_true: T, if_false: T) -> T {
condition.append(if_true).append(if_false) condition.append(if_true).append(if_false)
} }
fn reduce_constant(&mut self, input: &Constant) -> T { fn reduce_constant(&mut self, input: &Constant<'a>) -> T {
T::default() T::default()
} }
fn reduce_tuple_access(&mut self, input: &TupleAccessExpression, tuple_ref: T) -> T { fn reduce_tuple_access(&mut self, input: &TupleAccessExpression<'a>, tuple_ref: T) -> T {
tuple_ref tuple_ref
} }
fn reduce_tuple_init(&mut self, input: &TupleInitExpression, values: Vec<T>) -> T { fn reduce_tuple_init(&mut self, input: &TupleInitExpression<'a>, values: Vec<T>) -> T {
T::default().append_all(values.into_iter()) T::default().append_all(values.into_iter())
} }
fn reduce_unary(&mut self, input: &UnaryExpression, inner: T) -> T { fn reduce_unary(&mut self, input: &UnaryExpression<'a>, inner: T) -> T {
inner inner
} }
fn reduce_variable_ref(&mut self, input: &VariableRef) -> T { fn reduce_variable_ref(&mut self, input: &VariableRef<'a>) -> T {
T::default() T::default()
} }
} }
#[allow(unused_variables)] #[allow(unused_variables)]
pub trait MonoidalReducerStatement<T: Monoid>: MonoidalReducerExpression<T> { pub trait MonoidalReducerStatement<'a, T: Monoid>: MonoidalReducerExpression<'a, T> {
fn reduce_statement(&mut self, input: &Arc<Statement>, value: T) -> T { fn reduce_statement(&mut self, input: &'a Statement<'a>, value: T) -> T {
value value
} }
// left = Some(ArrayIndex.0) always if AssignAccess::ArrayIndex. if member/tuple, always None // left = Some(ArrayIndex.0) always if AssignAccess::ArrayIndex. if member/tuple, always None
fn reduce_assign_access(&mut self, input: &AssignAccess, left: Option<T>, right: Option<T>) -> T { fn reduce_assign_access(&mut self, input: &AssignAccess<'a>, left: Option<T>, right: Option<T>) -> T {
left.unwrap_or_default().append_option(right) left.unwrap_or_default().append_option(right)
} }
fn reduce_assign(&mut self, input: &AssignStatement, accesses: Vec<T>, value: T) -> T { fn reduce_assign(&mut self, input: &AssignStatement<'a>, accesses: Vec<T>, value: T) -> T {
T::default().append_all(accesses.into_iter()).append(value) T::default().append_all(accesses.into_iter()).append(value)
} }
fn reduce_block(&mut self, input: &BlockStatement, statements: Vec<T>) -> T { fn reduce_block(&mut self, input: &BlockStatement<'a>, statements: Vec<T>) -> T {
T::default().append_all(statements.into_iter()) T::default().append_all(statements.into_iter())
} }
fn reduce_conditional_statement( fn reduce_conditional_statement(
&mut self, &mut self,
input: &ConditionalStatement, input: &ConditionalStatement<'a>,
condition: T, condition: T,
if_true: T, if_true: T,
if_false: Option<T>, if_false: Option<T>,
@ -116,42 +114,42 @@ pub trait MonoidalReducerStatement<T: Monoid>: MonoidalReducerExpression<T> {
condition.append(if_true).append_option(if_false) condition.append(if_true).append_option(if_false)
} }
fn reduce_formatted_string(&mut self, input: &FormattedString, parameters: Vec<T>) -> T { fn reduce_formatted_string(&mut self, input: &FormattedString<'a>, parameters: Vec<T>) -> T {
T::default().append_all(parameters.into_iter()) T::default().append_all(parameters.into_iter())
} }
fn reduce_console(&mut self, input: &ConsoleStatement, argument: T) -> T { fn reduce_console(&mut self, input: &ConsoleStatement<'a>, argument: T) -> T {
argument argument
} }
fn reduce_definition(&mut self, input: &DefinitionStatement, value: T) -> T { fn reduce_definition(&mut self, input: &DefinitionStatement<'a>, value: T) -> T {
value value
} }
fn reduce_expression_statement(&mut self, input: &ExpressionStatement, expression: T) -> T { fn reduce_expression_statement(&mut self, input: &ExpressionStatement<'a>, expression: T) -> T {
expression expression
} }
fn reduce_iteration(&mut self, input: &IterationStatement, start: T, stop: T, body: T) -> T { fn reduce_iteration(&mut self, input: &IterationStatement<'a>, start: T, stop: T, body: T) -> T {
start.append(stop).append(body) start.append(stop).append(body)
} }
fn reduce_return(&mut self, input: &ReturnStatement, value: T) -> T { fn reduce_return(&mut self, input: &ReturnStatement<'a>, value: T) -> T {
value value
} }
} }
#[allow(unused_variables)] #[allow(unused_variables)]
pub trait MonoidalReducerProgram<T: Monoid>: MonoidalReducerStatement<T> { pub trait MonoidalReducerProgram<'a, T: Monoid>: MonoidalReducerStatement<'a, T> {
fn reduce_function(&mut self, input: &Arc<FunctionBody>, body: T) -> T { fn reduce_function(&mut self, input: &'a Function<'a>, body: T) -> T {
body body
} }
fn reduce_circuit_member(&mut self, input: &CircuitMemberBody, function: Option<T>) -> T { fn reduce_circuit_member(&mut self, input: &CircuitMember<'a>, function: Option<T>) -> T {
function.unwrap_or_default() function.unwrap_or_default()
} }
fn reduce_circuit(&mut self, input: &Arc<CircuitBody>, members: Vec<T>) -> T { fn reduce_circuit(&mut self, input: &'a Circuit<'a>, members: Vec<T>) -> T {
T::default().append_all(members.into_iter()) T::default().append_all(members.into_iter())
} }

161
asg/src/reducer/visitor.rs Normal file
View File

@ -0,0 +1,161 @@
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use std::cell::Cell;
use crate::{expression::*, program::*, statement::*};
pub enum VisitResult {
VisitChildren,
SkipChildren,
Exit,
}
impl Default for VisitResult {
fn default() -> Self {
VisitResult::VisitChildren
}
}
#[allow(unused_variables)]
pub trait ExpressionVisitor<'a> {
fn visit_expression(&mut self, input: &Cell<&'a Expression<'a>>) -> VisitResult {
Default::default()
}
fn visit_array_access(&mut self, input: &ArrayAccessExpression<'a>) -> VisitResult {
Default::default()
}
fn visit_array_init(&mut self, input: &ArrayInitExpression<'a>) -> VisitResult {
Default::default()
}
fn visit_array_inline(&mut self, input: &ArrayInlineExpression<'a>) -> VisitResult {
Default::default()
}
fn visit_array_range_access(&mut self, input: &ArrayRangeAccessExpression<'a>) -> VisitResult {
Default::default()
}
fn visit_binary(&mut self, input: &BinaryExpression<'a>) -> VisitResult {
Default::default()
}
fn visit_call(&mut self, input: &CallExpression<'a>) -> VisitResult {
Default::default()
}
fn visit_circuit_access(&mut self, input: &CircuitAccessExpression<'a>) -> VisitResult {
Default::default()
}
fn visit_circuit_init(&mut self, input: &CircuitInitExpression<'a>) -> VisitResult {
Default::default()
}
fn visit_ternary_expression(&mut self, input: &TernaryExpression<'a>) -> VisitResult {
Default::default()
}
fn visit_constant(&mut self, input: &Constant<'a>) -> VisitResult {
Default::default()
}
fn visit_tuple_access(&mut self, input: &TupleAccessExpression<'a>) -> VisitResult {
Default::default()
}
fn visit_tuple_init(&mut self, input: &TupleInitExpression<'a>) -> VisitResult {
Default::default()
}
fn visit_unary(&mut self, input: &UnaryExpression<'a>) -> VisitResult {
Default::default()
}
fn visit_variable_ref(&mut self, input: &VariableRef<'a>) -> VisitResult {
Default::default()
}
}
#[allow(unused_variables)]
pub trait StatementVisitor<'a>: ExpressionVisitor<'a> {
fn visit_statement(&mut self, input: &Cell<&'a Statement<'a>>) -> VisitResult {
Default::default()
}
// left = Some(ArrayIndex.0) always if AssignAccess::ArrayIndex. if member/tuple, always None
fn visit_assign_access(&mut self, input: &AssignAccess<'a>) -> VisitResult {
Default::default()
}
fn visit_assign(&mut self, input: &AssignStatement<'a>) -> VisitResult {
Default::default()
}
fn visit_block(&mut self, input: &BlockStatement<'a>) -> VisitResult {
Default::default()
}
fn visit_conditional_statement(&mut self, input: &ConditionalStatement<'a>) -> VisitResult {
Default::default()
}
fn visit_formatted_string(&mut self, input: &FormattedString<'a>) -> VisitResult {
Default::default()
}
fn visit_console(&mut self, input: &ConsoleStatement<'a>) -> VisitResult {
Default::default()
}
fn visit_definition(&mut self, input: &DefinitionStatement<'a>) -> VisitResult {
Default::default()
}
fn visit_expression_statement(&mut self, input: &ExpressionStatement<'a>) -> VisitResult {
Default::default()
}
fn visit_iteration(&mut self, input: &IterationStatement<'a>) -> VisitResult {
Default::default()
}
fn visit_return(&mut self, input: &ReturnStatement<'a>) -> VisitResult {
Default::default()
}
}
#[allow(unused_variables)]
pub trait ProgramVisitor<'a>: StatementVisitor<'a> {
fn visit_function(&mut self, input: &'a Function<'a>) -> VisitResult {
Default::default()
}
fn visit_circuit_member(&mut self, input: &CircuitMember<'a>) -> VisitResult {
Default::default()
}
fn visit_circuit(&mut self, input: &'a Circuit<'a>) -> VisitResult {
Default::default()
}
fn visit_program(&mut self, input: &Program<'a>) -> VisitResult {
Default::default()
}
}

View File

@ -0,0 +1,436 @@
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use super::*;
use crate::{expression::*, program::*, statement::*};
use std::{cell::Cell, marker::PhantomData};
pub struct VisitorDirector<'a, R: ExpressionVisitor<'a>> {
visitor: R,
lifetime: PhantomData<&'a ()>,
}
pub type ConcreteVisitResult = Result<(), ()>;
impl Into<ConcreteVisitResult> for VisitResult {
fn into(self) -> ConcreteVisitResult {
match self {
VisitResult::VisitChildren => Ok(()),
VisitResult::SkipChildren => Ok(()),
VisitResult::Exit => Err(()),
}
}
}
impl<'a, R: ExpressionVisitor<'a>> VisitorDirector<'a, R> {
pub fn new(visitor: R) -> Self {
Self {
visitor,
lifetime: PhantomData,
}
}
pub fn visitor(self) -> R {
self.visitor
}
pub fn visit_expression(&mut self, input: &Cell<&'a Expression<'a>>) -> ConcreteVisitResult {
match self.visitor.visit_expression(input) {
VisitResult::VisitChildren => match input.get() {
Expression::ArrayAccess(e) => self.visit_array_access(e),
Expression::ArrayInit(e) => self.visit_array_init(e),
Expression::ArrayInline(e) => self.visit_array_inline(e),
Expression::ArrayRangeAccess(e) => self.visit_array_range_access(e),
Expression::Binary(e) => self.visit_binary(e),
Expression::Call(e) => self.visit_call(e),
Expression::CircuitAccess(e) => self.visit_circuit_access(e),
Expression::CircuitInit(e) => self.visit_circuit_init(e),
Expression::Ternary(e) => self.visit_ternary_expression(e),
Expression::Constant(e) => self.visit_constant(e),
Expression::TupleAccess(e) => self.visit_tuple_access(e),
Expression::TupleInit(e) => self.visit_tuple_init(e),
Expression::Unary(e) => self.visit_unary(e),
Expression::VariableRef(e) => self.visit_variable_ref(e),
},
x => x.into(),
}
}
fn visit_opt_expression(&mut self, input: &Cell<Option<&'a Expression<'a>>>) -> ConcreteVisitResult {
let interior = input.get().map(Cell::new);
if let Some(interior) = interior.as_ref() {
let result = self.visit_expression(interior);
input.replace(Some(interior.get()));
result
} else {
Ok(())
}
}
pub fn visit_array_access(&mut self, input: &ArrayAccessExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_array_access(input) {
VisitResult::VisitChildren => {
self.visit_expression(&input.array)?;
self.visit_expression(&input.index)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_array_init(&mut self, input: &ArrayInitExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_array_init(input) {
VisitResult::VisitChildren => {
self.visit_expression(&input.element)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_array_inline(&mut self, input: &ArrayInlineExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_array_inline(input) {
VisitResult::VisitChildren => {
for (element, _) in input.elements.iter() {
self.visit_expression(element)?;
}
Ok(())
}
x => x.into(),
}
}
pub fn visit_array_range_access(&mut self, input: &ArrayRangeAccessExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_array_range_access(input) {
VisitResult::VisitChildren => {
self.visit_expression(&input.array)?;
self.visit_opt_expression(&input.left)?;
self.visit_opt_expression(&input.right)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_binary(&mut self, input: &BinaryExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_binary(input) {
VisitResult::VisitChildren => {
self.visit_expression(&input.left)?;
self.visit_expression(&input.right)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_call(&mut self, input: &CallExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_call(input) {
VisitResult::VisitChildren => {
self.visit_opt_expression(&input.target)?;
for argument in input.arguments.iter() {
self.visit_expression(argument)?;
}
Ok(())
}
x => x.into(),
}
}
pub fn visit_circuit_access(&mut self, input: &CircuitAccessExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_circuit_access(input) {
VisitResult::VisitChildren => {
self.visit_opt_expression(&input.target)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_circuit_init(&mut self, input: &CircuitInitExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_circuit_init(input) {
VisitResult::VisitChildren => {
for (_, argument) in input.values.iter() {
self.visit_expression(argument)?;
}
Ok(())
}
x => x.into(),
}
}
pub fn visit_ternary_expression(&mut self, input: &TernaryExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_ternary_expression(input) {
VisitResult::VisitChildren => {
self.visit_expression(&input.condition)?;
self.visit_expression(&input.if_true)?;
self.visit_expression(&input.if_false)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_constant(&mut self, input: &Constant<'a>) -> ConcreteVisitResult {
self.visitor.visit_constant(input).into()
}
pub fn visit_tuple_access(&mut self, input: &TupleAccessExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_tuple_access(input) {
VisitResult::VisitChildren => {
self.visit_expression(&input.tuple_ref)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_tuple_init(&mut self, input: &TupleInitExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_tuple_init(input) {
VisitResult::VisitChildren => {
for argument in input.elements.iter() {
self.visit_expression(argument)?;
}
Ok(())
}
x => x.into(),
}
}
pub fn visit_unary(&mut self, input: &UnaryExpression<'a>) -> ConcreteVisitResult {
match self.visitor.visit_unary(input) {
VisitResult::VisitChildren => {
self.visit_expression(&input.inner)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_variable_ref(&mut self, input: &VariableRef<'a>) -> ConcreteVisitResult {
self.visitor.visit_variable_ref(input).into()
}
}
impl<'a, R: StatementVisitor<'a>> VisitorDirector<'a, R> {
pub fn visit_statement(&mut self, input: &Cell<&'a Statement<'a>>) -> ConcreteVisitResult {
match self.visitor.visit_statement(input) {
VisitResult::VisitChildren => match input.get() {
Statement::Assign(s) => self.visit_assign(s),
Statement::Block(s) => self.visit_block(s),
Statement::Conditional(s) => self.visit_conditional_statement(s),
Statement::Console(s) => self.visit_console(s),
Statement::Definition(s) => self.visit_definition(s),
Statement::Expression(s) => self.visit_expression_statement(s),
Statement::Iteration(s) => self.visit_iteration(s),
Statement::Return(s) => self.visit_return(s),
},
x => x.into(),
}
}
fn visit_opt_statement(&mut self, input: &Cell<Option<&'a Statement<'a>>>) -> ConcreteVisitResult {
let interior = input.get().map(Cell::new);
if let Some(interior) = interior.as_ref() {
let result = self.visit_statement(interior);
input.replace(Some(interior.get()));
result
} else {
Ok(())
}
}
pub fn visit_assign_access(&mut self, input: &AssignAccess<'a>) -> ConcreteVisitResult {
match self.visitor.visit_assign_access(input) {
VisitResult::VisitChildren => {
match input {
AssignAccess::ArrayRange(left, right) => {
self.visit_opt_expression(left)?;
self.visit_opt_expression(right)?;
}
AssignAccess::ArrayIndex(index) => self.visit_expression(index)?,
_ => (),
}
Ok(())
}
x => x.into(),
}
}
pub fn visit_assign(&mut self, input: &AssignStatement<'a>) -> ConcreteVisitResult {
match self.visitor.visit_assign(input) {
VisitResult::VisitChildren => {
for access in input.target_accesses.iter() {
self.visit_assign_access(access)?;
}
self.visit_expression(&input.value)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_block(&mut self, input: &BlockStatement<'a>) -> ConcreteVisitResult {
match self.visitor.visit_block(input) {
VisitResult::VisitChildren => {
for statement in input.statements.iter() {
self.visit_statement(statement)?;
}
Ok(())
}
x => x.into(),
}
}
pub fn visit_conditional_statement(&mut self, input: &ConditionalStatement<'a>) -> ConcreteVisitResult {
match self.visitor.visit_conditional_statement(input) {
VisitResult::VisitChildren => {
self.visit_expression(&input.condition)?;
self.visit_statement(&input.result)?;
self.visit_opt_statement(&input.next)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_formatted_string(&mut self, input: &FormattedString<'a>) -> ConcreteVisitResult {
match self.visitor.visit_formatted_string(input) {
VisitResult::VisitChildren => {
for parameter in input.parameters.iter() {
self.visit_expression(parameter)?;
}
Ok(())
}
x => x.into(),
}
}
pub fn visit_console(&mut self, input: &ConsoleStatement<'a>) -> ConcreteVisitResult {
match self.visitor.visit_console(input) {
VisitResult::VisitChildren => {
match &input.function {
ConsoleFunction::Assert(e) => self.visit_expression(e)?,
ConsoleFunction::Debug(f) | ConsoleFunction::Error(f) | ConsoleFunction::Log(f) => {
self.visit_formatted_string(f)?
}
}
Ok(())
}
x => x.into(),
}
}
pub fn visit_definition(&mut self, input: &DefinitionStatement<'a>) -> ConcreteVisitResult {
match self.visitor.visit_definition(input) {
VisitResult::VisitChildren => {
self.visit_expression(&input.value)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_expression_statement(&mut self, input: &ExpressionStatement<'a>) -> ConcreteVisitResult {
match self.visitor.visit_expression_statement(input) {
VisitResult::VisitChildren => {
self.visit_expression(&input.expression)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_iteration(&mut self, input: &IterationStatement<'a>) -> ConcreteVisitResult {
match self.visitor.visit_iteration(input) {
VisitResult::VisitChildren => {
self.visit_expression(&input.start)?;
self.visit_expression(&input.stop)?;
self.visit_statement(&input.body)?;
Ok(())
}
x => x.into(),
}
}
pub fn visit_return(&mut self, input: &ReturnStatement<'a>) -> ConcreteVisitResult {
match self.visitor.visit_return(input) {
VisitResult::VisitChildren => {
self.visit_expression(&input.expression)?;
Ok(())
}
x => x.into(),
}
}
}
#[allow(dead_code)]
impl<'a, R: ProgramVisitor<'a>> VisitorDirector<'a, R> {
fn visit_function(&mut self, input: &'a Function<'a>) -> ConcreteVisitResult {
match self.visitor.visit_function(input) {
VisitResult::VisitChildren => {
self.visit_opt_statement(&input.body)?;
Ok(())
}
x => x.into(),
}
}
fn visit_circuit_member(&mut self, input: &CircuitMember<'a>) -> ConcreteVisitResult {
match self.visitor.visit_circuit_member(input) {
VisitResult::VisitChildren => {
if let CircuitMember::Function(f) = input {
self.visit_function(f)?;
}
Ok(())
}
x => x.into(),
}
}
fn visit_circuit(&mut self, input: &'a Circuit<'a>) -> ConcreteVisitResult {
match self.visitor.visit_circuit(input) {
VisitResult::VisitChildren => {
for (_, member) in input.members.borrow().iter() {
self.visit_circuit_member(member)?;
}
Ok(())
}
x => x.into(),
}
}
fn visit_program(&mut self, input: &Program<'a>) -> ConcreteVisitResult {
match self.visitor.visit_program(input) {
VisitResult::VisitChildren => {
for (_, import) in input.imported_modules.iter() {
self.visit_program(import)?;
}
for (_, (function, _)) in input.test_functions.iter() {
self.visit_function(function)?;
}
for (_, function) in input.functions.iter() {
self.visit_function(function)?;
}
for (_, circuit) in input.circuits.iter() {
self.visit_circuit(circuit)?;
}
Ok(())
}
x => x.into(),
}
}
}

View File

@ -14,58 +14,96 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{AsgConvertError, Circuit, Function, Input, Type, Variable}; use crate::{ArenaNode, AsgContext, AsgConvertError, Circuit, Expression, Function, Input, Statement, Type, Variable};
use indexmap::IndexMap; use indexmap::IndexMap;
use std::{cell::RefCell, sync::Arc}; use std::cell::{Cell, RefCell};
use uuid::Uuid;
/// An abstract data type that track the current bindings for variables, functions, and circuits. /// An abstract data type that track the current bindings for variables, functions, and circuits.
#[derive(Debug)] #[derive(Clone)]
pub struct InnerScope { pub struct Scope<'a> {
pub context: AsgContext<'a>,
/// The unique id of the scope. /// The unique id of the scope.
pub id: Uuid, pub id: u32,
/// The parent scope that this scope inherits. /// The parent scope that this scope inherits.
pub parent_scope: Option<Scope>, pub parent_scope: Cell<Option<&'a Scope<'a>>>,
/// The function definition that this scope occurs in. /// The function definition that this scope occurs in.
pub function: Option<Arc<Function>>, pub function: Cell<Option<&'a Function<'a>>>,
/// The circuit definition that this scope occurs in. /// The circuit definition that this scope occurs in.
pub circuit_self: Option<Arc<Circuit>>, pub circuit_self: Cell<Option<&'a Circuit<'a>>>,
/// Maps variable name => variable. /// Maps variable name => variable.
pub variables: IndexMap<String, Variable>, pub variables: RefCell<IndexMap<String, &'a Variable<'a>>>,
/// Maps function name => function. /// Maps function name => function.
pub functions: IndexMap<String, Arc<Function>>, pub functions: RefCell<IndexMap<String, &'a Function<'a>>>,
/// Maps circuit name => circuit. /// Maps circuit name => circuit.
pub circuits: IndexMap<String, Arc<Circuit>>, pub circuits: RefCell<IndexMap<String, &'a Circuit<'a>>>,
/// The main input to the program. /// The main input to the program.
pub input: Option<Input>, pub input: Cell<Option<Input<'a>>>,
} }
pub type Scope = Arc<RefCell<InnerScope>>; #[allow(clippy::mut_from_ref)]
impl<'a> Scope<'a> {
pub fn alloc_expression(&'a self, expr: Expression<'a>) -> &'a mut Expression<'a> {
match self.context.arena.alloc(ArenaNode::Expression(expr)) {
ArenaNode::Expression(e) => e,
_ => unimplemented!(),
}
}
pub fn alloc_statement(&'a self, statement: Statement<'a>) -> &'a mut Statement<'a> {
match self.context.arena.alloc(ArenaNode::Statement(statement)) {
ArenaNode::Statement(e) => e,
_ => unimplemented!(),
}
}
pub fn alloc_variable(&'a self, variable: Variable<'a>) -> &'a mut Variable<'a> {
match self.context.arena.alloc(ArenaNode::Variable(variable)) {
ArenaNode::Variable(e) => e,
_ => unimplemented!(),
}
}
pub fn alloc_scope(&'a self, scope: Scope<'a>) -> &'a mut Scope<'a> {
match self.context.arena.alloc(ArenaNode::Scope(scope)) {
ArenaNode::Scope(e) => e,
_ => unimplemented!(),
}
}
pub fn alloc_circuit(&'a self, circuit: Circuit<'a>) -> &'a mut Circuit<'a> {
match self.context.arena.alloc(ArenaNode::Circuit(circuit)) {
ArenaNode::Circuit(e) => e,
_ => unimplemented!(),
}
}
pub fn alloc_function(&'a self, function: Function<'a>) -> &'a mut Function<'a> {
match self.context.arena.alloc(ArenaNode::Function(function)) {
ArenaNode::Function(e) => e,
_ => unimplemented!(),
}
}
impl InnerScope {
/// ///
/// Returns a reference to the variable corresponding to the name. /// Returns a reference to the variable corresponding to the name.
/// ///
/// If the current scope did not have this name present, then the parent scope is checked. /// If the current scope did not have this name present, then the parent scope is checked.
/// If there is no parent scope, then `None` is returned. /// If there is no parent scope, then `None` is returned.
/// ///
pub fn resolve_variable(&self, name: &str) -> Option<Variable> { pub fn resolve_variable(&self, name: &str) -> Option<&'a Variable<'a>> {
if let Some(resolved) = self.variables.get(name) { if let Some(resolved) = self.variables.borrow().get(name) {
Some(resolved.clone()) Some(*resolved)
} else if let Some(resolved) = self.parent_scope.as_ref() { } else if let Some(scope) = self.parent_scope.get() {
if let Some(resolved) = resolved.borrow().resolve_variable(name) { scope.resolve_variable(name)
Some(resolved)
} else {
None
}
} else { } else {
None None
} }
@ -77,15 +115,11 @@ impl InnerScope {
/// If the current scope did not have a function present, then the parent scope is checked. /// If the current scope did not have a function present, then the parent scope is checked.
/// If there is no parent scope, then `None` is returned. /// If there is no parent scope, then `None` is returned.
/// ///
pub fn resolve_current_function(&self) -> Option<Arc<Function>> { pub fn resolve_current_function(&self) -> Option<&'a Function> {
if let Some(resolved) = self.function.as_ref() { if let Some(resolved) = self.function.get() {
Some(resolved.clone()) Some(resolved)
} else if let Some(resolved) = self.parent_scope.as_ref() { } else if let Some(scope) = self.parent_scope.get() {
if let Some(resolved) = resolved.borrow().resolve_current_function() { scope.resolve_current_function()
Some(resolved)
} else {
None
}
} else { } else {
None None
} }
@ -97,15 +131,11 @@ impl InnerScope {
/// If the current scope did not have an input present, then the parent scope is checked. /// If the current scope did not have an input present, then the parent scope is checked.
/// If there is no parent scope, then `None` is returned. /// If there is no parent scope, then `None` is returned.
/// ///
pub fn resolve_input(&self) -> Option<Input> { pub fn resolve_input(&self) -> Option<Input<'a>> {
if let Some(input) = self.input.as_ref() { if let Some(input) = self.input.get() {
Some(input.clone()) Some(input)
} else if let Some(resolved) = self.parent_scope.as_ref() { } else if let Some(resolved) = self.parent_scope.get() {
if let Some(resolved) = resolved.borrow().resolve_input() { resolved.resolve_input()
Some(resolved)
} else {
None
}
} else { } else {
None None
} }
@ -117,15 +147,11 @@ impl InnerScope {
/// If the current scope did not have this name present, then the parent scope is checked. /// If the current scope did not have this name present, then the parent scope is checked.
/// If there is no parent scope, then `None` is returned. /// If there is no parent scope, then `None` is returned.
/// ///
pub fn resolve_function(&self, name: &str) -> Option<Arc<Function>> { pub fn resolve_function(&self, name: &str) -> Option<&'a Function<'a>> {
if let Some(resolved) = self.functions.get(name) { if let Some(resolved) = self.functions.borrow().get(name) {
Some(resolved.clone()) Some(*resolved)
} else if let Some(resolved) = self.parent_scope.as_ref() { } else if let Some(resolved) = self.parent_scope.get() {
if let Some(resolved) = resolved.borrow().resolve_function(name) { resolved.resolve_function(name)
Some(resolved)
} else {
None
}
} else { } else {
None None
} }
@ -137,17 +163,13 @@ impl InnerScope {
/// If the current scope did not have this name present, then the parent scope is checked. /// If the current scope did not have this name present, then the parent scope is checked.
/// If there is no parent scope, then `None` is returned. /// If there is no parent scope, then `None` is returned.
/// ///
pub fn resolve_circuit(&self, name: &str) -> Option<Arc<Circuit>> { pub fn resolve_circuit(&self, name: &str) -> Option<&'a Circuit<'a>> {
if let Some(resolved) = self.circuits.get(name) { if let Some(resolved) = self.circuits.borrow().get(name) {
Some(resolved.clone()) Some(*resolved)
} else if name == "Self" && self.circuit_self.is_some() { } else if name == "Self" && self.circuit_self.get().is_some() {
self.circuit_self.clone() self.circuit_self.get()
} else if let Some(resolved) = self.parent_scope.as_ref() { } else if let Some(resolved) = self.parent_scope.get() {
if let Some(resolved) = resolved.borrow().resolve_circuit(name) { resolved.resolve_circuit(name)
Some(resolved)
} else {
None
}
} else { } else {
None None
} }
@ -159,15 +181,11 @@ impl InnerScope {
/// If the current scope did not have a circuit self present, then the parent scope is checked. /// If the current scope did not have a circuit self present, then the parent scope is checked.
/// If there is no parent scope, then `None` is returned. /// If there is no parent scope, then `None` is returned.
/// ///
pub fn resolve_circuit_self(&self) -> Option<Arc<Circuit>> { pub fn resolve_circuit_self(&self) -> Option<&'a Circuit<'a>> {
if let Some(resolved) = self.circuit_self.as_ref() { if let Some(resolved) = self.circuit_self.get() {
Some(resolved.clone()) Some(resolved)
} else if let Some(resolved) = self.parent_scope.as_ref() { } else if let Some(resolved) = self.parent_scope.get() {
if let Some(resolved) = resolved.borrow().resolve_circuit_self() { resolved.resolve_circuit_self()
Some(resolved)
} else {
None
}
} else { } else {
None None
} }
@ -176,23 +194,24 @@ impl InnerScope {
/// ///
/// Returns a new scope given a parent scope. /// Returns a new scope given a parent scope.
/// ///
pub fn make_subscope(scope: &Scope) -> Scope { pub fn make_subscope(self: &'a Scope<'a>) -> &'a Scope<'a> {
Arc::new(RefCell::new(InnerScope { self.alloc_scope(Scope::<'a> {
id: Uuid::new_v4(), context: self.context,
parent_scope: Some(scope.clone()), id: self.context.get_id(),
circuit_self: None, parent_scope: Cell::new(Some(self)),
variables: IndexMap::new(), circuit_self: Cell::new(None),
functions: IndexMap::new(), variables: RefCell::new(IndexMap::new()),
circuits: IndexMap::new(), functions: RefCell::new(IndexMap::new()),
function: None, circuits: RefCell::new(IndexMap::new()),
input: None, function: Cell::new(None),
})) input: Cell::new(None),
})
} }
/// ///
/// Returns the type returned by the current scope. /// Returns the type returned by the current scope.
/// ///
pub fn resolve_ast_type(&self, type_: &leo_ast::Type) -> Result<Type, AsgConvertError> { pub fn resolve_ast_type(&self, type_: &leo_ast::Type) -> Result<Type<'a>, AsgConvertError> {
use leo_ast::Type::*; use leo_ast::Type::*;
Ok(match type_ { Ok(match type_ {
Address => Type::Address, Address => Type::Address,
@ -218,21 +237,17 @@ impl InnerScope {
.collect::<Result<Vec<_>, AsgConvertError>>()?, .collect::<Result<Vec<_>, AsgConvertError>>()?,
), ),
Circuit(name) if name.name == "Self" => Type::Circuit( Circuit(name) if name.name == "Self" => Type::Circuit(
self.circuit_self self.resolve_circuit_self()
.clone()
.ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?, .ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?,
), ),
Circuit(name) => Type::Circuit(
self.circuits
.get(&name.name)
.ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?
.clone(),
),
SelfType => Type::Circuit( SelfType => Type::Circuit(
self.circuit_self self.resolve_circuit_self()
.clone()
.ok_or_else(AsgConvertError::reference_self_outside_circuit)?, .ok_or_else(AsgConvertError::reference_self_outside_circuit)?,
), ),
Circuit(name) => Type::Circuit(
self.resolve_circuit(&name.name)
.ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?,
),
}) })
} }
} }

View File

@ -35,49 +35,49 @@ use crate::{
pub use leo_ast::AssignOperation; pub use leo_ast::AssignOperation;
use leo_ast::AssigneeAccess as AstAssigneeAccess; use leo_ast::AssigneeAccess as AstAssigneeAccess;
use std::sync::{Arc, Weak}; use std::cell::Cell;
#[derive(Debug)] #[derive(Clone)]
pub enum AssignAccess { pub enum AssignAccess<'a> {
ArrayRange(Option<Arc<Expression>>, Option<Arc<Expression>>), ArrayRange(Cell<Option<&'a Expression<'a>>>, Cell<Option<&'a Expression<'a>>>),
ArrayIndex(Arc<Expression>), ArrayIndex(Cell<&'a Expression<'a>>),
Tuple(usize), Tuple(usize),
Member(Identifier), Member(Identifier),
} }
#[derive(Debug)] #[derive(Clone)]
pub struct AssignStatement { pub struct AssignStatement<'a> {
pub parent: Option<Weak<Statement>>, pub parent: Cell<Option<&'a Statement<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub operation: AssignOperation, pub operation: AssignOperation,
pub target_variable: Variable, pub target_variable: Cell<&'a Variable<'a>>,
pub target_accesses: Vec<AssignAccess>, pub target_accesses: Vec<AssignAccess<'a>>,
pub value: Arc<Expression>, pub value: Cell<&'a Expression<'a>>,
} }
impl Node for AssignStatement { impl<'a> Node for AssignStatement<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl FromAst<leo_ast::AssignStatement> for Arc<Statement> { impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
statement: &leo_ast::AssignStatement, statement: &leo_ast::AssignStatement,
_expected_type: Option<PartialType>, _expected_type: Option<PartialType<'a>>,
) -> Result<Arc<Statement>, AsgConvertError> { ) -> Result<Self, AsgConvertError> {
let (name, span) = (&statement.assignee.identifier.name, &statement.assignee.identifier.span); let (name, span) = (&statement.assignee.identifier.name, &statement.assignee.identifier.span);
let variable = if name == "input" { let variable = if name == "input" {
if let Some(function) = scope.borrow().resolve_current_function() { if let Some(function) = scope.resolve_current_function() {
if !function.has_input { if !function.has_input {
return Err(AsgConvertError::unresolved_reference(name, span)); return Err(AsgConvertError::unresolved_reference(name, span));
} }
} else { } else {
return Err(AsgConvertError::unresolved_reference(name, span)); return Err(AsgConvertError::unresolved_reference(name, span));
} }
if let Some(input) = scope.borrow().resolve_input() { if let Some(input) = scope.resolve_input() {
input.container input.container
} else { } else {
return Err(AsgConvertError::InternalError( return Err(AsgConvertError::InternalError(
@ -86,7 +86,6 @@ impl FromAst<leo_ast::AssignStatement> for Arc<Statement> {
} }
} else { } else {
scope scope
.borrow()
.resolve_variable(&name) .resolve_variable(&name)
.ok_or_else(|| AsgConvertError::unresolved_reference(name, span))? .ok_or_else(|| AsgConvertError::unresolved_reference(name, span))?
}; };
@ -104,16 +103,16 @@ impl FromAst<leo_ast::AssignStatement> for Arc<Statement> {
let left = left let left = left
.as_ref() .as_ref()
.map( .map(
|left: &leo_ast::Expression| -> Result<Arc<Expression>, AsgConvertError> { |left: &leo_ast::Expression| -> Result<&'a Expression<'a>, AsgConvertError> {
Arc::<Expression>::from_ast(scope, left, index_type.clone()) <&Expression<'a>>::from_ast(scope, left, index_type.clone())
}, },
) )
.transpose()?; .transpose()?;
let right = right let right = right
.as_ref() .as_ref()
.map( .map(
|right: &leo_ast::Expression| -> Result<Arc<Expression>, AsgConvertError> { |right: &leo_ast::Expression| -> Result<&'a Expression<'a>, AsgConvertError> {
Arc::<Expression>::from_ast(scope, right, index_type) <&Expression<'a>>::from_ast(scope, right, index_type)
}, },
) )
.transpose()?; .transpose()?;
@ -156,18 +155,18 @@ impl FromAst<leo_ast::AssignStatement> for Arc<Statement> {
_ => return Err(AsgConvertError::index_into_non_array(&name, &statement.span)), _ => return Err(AsgConvertError::index_into_non_array(&name, &statement.span)),
} }
AssignAccess::ArrayRange(left, right) AssignAccess::ArrayRange(Cell::new(left), Cell::new(right))
} }
AstAssigneeAccess::ArrayIndex(index) => { AstAssigneeAccess::ArrayIndex(index) => {
target_type = match target_type.clone() { target_type = match target_type.clone() {
Some(PartialType::Array(item, _)) => item.map(|x| *x), Some(PartialType::Array(item, _)) => item.map(|x| *x),
_ => return Err(AsgConvertError::index_into_non_array(&name, &statement.span)), _ => return Err(AsgConvertError::index_into_non_array(&name, &statement.span)),
}; };
AssignAccess::ArrayIndex(Arc::<Expression>::from_ast( AssignAccess::ArrayIndex(Cell::new(<&Expression<'a>>::from_ast(
scope, scope,
index, index,
Some(PartialType::Integer(None, Some(IntegerType::U32))), Some(PartialType::Integer(None, Some(IntegerType::U32))),
)?) )?))
} }
AstAssigneeAccess::Tuple(index, _) => { AstAssigneeAccess::Tuple(index, _) => {
let index = index let index = index
@ -203,7 +202,7 @@ impl FromAst<leo_ast::AssignStatement> for Arc<Statement> {
return Err(AsgConvertError::illegal_function_assign(&name.name, &statement.span)); return Err(AsgConvertError::illegal_function_assign(&name.name, &statement.span));
} }
}; };
Some(x.strong().partial()) Some(x.partial())
} }
_ => { _ => {
return Err(AsgConvertError::index_into_non_tuple( return Err(AsgConvertError::index_into_non_tuple(
@ -216,41 +215,40 @@ impl FromAst<leo_ast::AssignStatement> for Arc<Statement> {
} }
}); });
} }
let value = Arc::<Expression>::from_ast(scope, &statement.value, target_type)?; let value = <&Expression<'a>>::from_ast(scope, &statement.value, target_type)?;
let statement = Arc::new(Statement::Assign(AssignStatement { let statement = scope.alloc_statement(Statement::Assign(AssignStatement {
parent: None, parent: Cell::new(None),
span: Some(statement.span.clone()), span: Some(statement.span.clone()),
operation: statement.operation.clone(), operation: statement.operation.clone(),
target_variable: variable.clone(), target_variable: Cell::new(variable),
target_accesses, target_accesses,
value, value: Cell::new(value),
})); }));
{ {
let mut variable = variable.borrow_mut(); let mut variable = variable.borrow_mut();
variable.assignments.push(Arc::downgrade(&statement)); variable.assignments.push(statement);
} }
Ok(statement) Ok(statement)
} }
} }
impl Into<leo_ast::AssignStatement> for &AssignStatement { impl<'a> Into<leo_ast::AssignStatement> for &AssignStatement<'a> {
fn into(self) -> leo_ast::AssignStatement { fn into(self) -> leo_ast::AssignStatement {
leo_ast::AssignStatement { leo_ast::AssignStatement {
operation: self.operation.clone(), operation: self.operation.clone(),
assignee: leo_ast::Assignee { assignee: leo_ast::Assignee {
identifier: self.target_variable.borrow().name.clone(), identifier: self.target_variable.get().borrow().name.clone(),
accesses: self accesses: self
.target_accesses .target_accesses
.iter() .iter()
.map(|access| match access { .map(|access| match access {
AssignAccess::ArrayRange(left, right) => AstAssigneeAccess::ArrayRange( AssignAccess::ArrayRange(left, right) => {
left.as_ref().map(|e| e.as_ref().into()), AstAssigneeAccess::ArrayRange(left.get().map(|e| e.into()), right.get().map(|e| e.into()))
right.as_ref().map(|e| e.as_ref().into()), }
), AssignAccess::ArrayIndex(index) => AstAssigneeAccess::ArrayIndex(index.get().into()),
AssignAccess::ArrayIndex(index) => AstAssigneeAccess::ArrayIndex(index.as_ref().into()),
AssignAccess::Tuple(index) => AstAssigneeAccess::Tuple( AssignAccess::Tuple(index) => AstAssigneeAccess::Tuple(
leo_ast::PositiveNumber { leo_ast::PositiveNumber {
value: index.to_string(), value: index.to_string(),
@ -262,7 +260,7 @@ impl Into<leo_ast::AssignStatement> for &AssignStatement {
.collect(), .collect(),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
}, },
value: self.value.as_ref().into(), value: self.value.get().into(),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -14,38 +14,38 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{AsgConvertError, FromAst, InnerScope, Node, PartialType, Scope, Span, Statement}; use crate::{AsgConvertError, FromAst, Node, PartialType, Scope, Span, Statement};
use std::sync::{Arc, Weak}; use std::cell::Cell;
#[derive(Debug)] #[derive(Clone)]
pub struct BlockStatement { pub struct BlockStatement<'a> {
pub parent: Option<Weak<Statement>>, pub parent: Cell<Option<&'a Statement<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub statements: Vec<Arc<Statement>>, pub statements: Vec<Cell<&'a Statement<'a>>>,
pub scope: Scope, pub scope: &'a Scope<'a>,
} }
impl Node for BlockStatement { impl<'a> Node for BlockStatement<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl FromAst<leo_ast::Block> for BlockStatement { impl<'a> FromAst<'a, leo_ast::Block> for BlockStatement<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
statement: &leo_ast::Block, statement: &leo_ast::Block,
_expected_type: Option<PartialType>, _expected_type: Option<PartialType<'a>>,
) -> Result<Self, AsgConvertError> { ) -> Result<Self, AsgConvertError> {
let new_scope = InnerScope::make_subscope(scope); let new_scope = scope.make_subscope();
let mut output = vec![]; let mut output = vec![];
for item in statement.statements.iter() { for item in statement.statements.iter() {
output.push(Arc::<Statement>::from_ast(&new_scope, item, None)?); output.push(Cell::new(<&'a Statement<'a>>::from_ast(&new_scope, item, None)?));
} }
Ok(BlockStatement { Ok(BlockStatement {
parent: None, parent: Cell::new(None),
span: Some(statement.span.clone()), span: Some(statement.span.clone()),
statements: output, statements: output,
scope: new_scope, scope: new_scope,
@ -53,14 +53,10 @@ impl FromAst<leo_ast::Block> for BlockStatement {
} }
} }
impl Into<leo_ast::Block> for &BlockStatement { impl<'a> Into<leo_ast::Block> for &BlockStatement<'a> {
fn into(self) -> leo_ast::Block { fn into(self) -> leo_ast::Block {
leo_ast::Block { leo_ast::Block {
statements: self statements: self.statements.iter().map(|statement| statement.get().into()).collect(),
.statements
.iter()
.map(|statement| statement.as_ref().into())
.collect(),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -16,31 +16,31 @@
use crate::{AsgConvertError, BlockStatement, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type}; use crate::{AsgConvertError, BlockStatement, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type};
use std::sync::{Arc, Weak}; use std::cell::Cell;
#[derive(Debug)] #[derive(Clone)]
pub struct ConditionalStatement { pub struct ConditionalStatement<'a> {
pub parent: Option<Weak<Statement>>, pub parent: Cell<Option<&'a Statement<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub condition: Arc<Expression>, pub condition: Cell<&'a Expression<'a>>,
pub result: Arc<Statement>, pub result: Cell<&'a Statement<'a>>,
pub next: Option<Arc<Statement>>, pub next: Cell<Option<&'a Statement<'a>>>,
} }
impl Node for ConditionalStatement { impl<'a> Node for ConditionalStatement<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl FromAst<leo_ast::ConditionalStatement> for ConditionalStatement { impl<'a> FromAst<'a, leo_ast::ConditionalStatement> for ConditionalStatement<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
statement: &leo_ast::ConditionalStatement, statement: &leo_ast::ConditionalStatement,
_expected_type: Option<PartialType>, _expected_type: Option<PartialType<'a>>,
) -> Result<Self, AsgConvertError> { ) -> Result<Self, AsgConvertError> {
let condition = Arc::<Expression>::from_ast(scope, &statement.condition, Some(Type::Boolean.into()))?; let condition = <&Expression<'a>>::from_ast(scope, &statement.condition, Some(Type::Boolean.into()))?;
let result = Arc::new(Statement::Block(BlockStatement::from_ast( let result = scope.alloc_statement(Statement::Block(BlockStatement::from_ast(
scope, scope,
&statement.block, &statement.block,
None, None,
@ -48,28 +48,30 @@ impl FromAst<leo_ast::ConditionalStatement> for ConditionalStatement {
let next = statement let next = statement
.next .next
.as_deref() .as_deref()
.map(|next| -> Result<Arc<Statement>, AsgConvertError> { Arc::<Statement>::from_ast(scope, next, None) }) .map(|next| -> Result<&'a Statement<'a>, AsgConvertError> {
<&'a Statement<'a>>::from_ast(scope, next, None)
})
.transpose()?; .transpose()?;
Ok(ConditionalStatement { Ok(ConditionalStatement {
parent: None, parent: Cell::new(None),
span: Some(statement.span.clone()), span: Some(statement.span.clone()),
condition, condition: Cell::new(condition),
result, result: Cell::new(result),
next, next: Cell::new(next),
}) })
} }
} }
impl Into<leo_ast::ConditionalStatement> for &ConditionalStatement { impl<'a> Into<leo_ast::ConditionalStatement> for &ConditionalStatement<'a> {
fn into(self) -> leo_ast::ConditionalStatement { fn into(self) -> leo_ast::ConditionalStatement {
leo_ast::ConditionalStatement { leo_ast::ConditionalStatement {
condition: self.condition.as_ref().into(), condition: self.condition.get().into(),
block: match self.result.as_ref() { block: match self.result.get() {
Statement::Block(block) => block.into(), Statement::Block(block) => block.into(),
_ => unimplemented!(), _ => unimplemented!(),
}, },
next: self.next.as_deref().map(|e| Box::new(e.into())), next: self.next.get().as_deref().map(|e| Box::new(e.into())),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -17,43 +17,43 @@
use crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type}; use crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type};
use leo_ast::ConsoleFunction as AstConsoleFunction; use leo_ast::ConsoleFunction as AstConsoleFunction;
use std::sync::{Arc, Weak}; use std::cell::Cell;
// TODO (protryon): Refactor to not require/depend on span // TODO (protryon): Refactor to not require/depend on span
#[derive(Debug)] #[derive(Clone)]
pub struct FormattedString { pub struct FormattedString<'a> {
pub string: String, pub string: String,
pub containers: Vec<Span>, pub containers: Vec<Span>,
pub parameters: Vec<Arc<Expression>>, pub parameters: Vec<Cell<&'a Expression<'a>>>,
pub span: Span, pub span: Span,
} }
#[derive(Debug)] #[derive(Clone)]
pub enum ConsoleFunction { pub enum ConsoleFunction<'a> {
Assert(Arc<Expression>), Assert(Cell<&'a Expression<'a>>),
Debug(FormattedString), Debug(FormattedString<'a>),
Error(FormattedString), Error(FormattedString<'a>),
Log(FormattedString), Log(FormattedString<'a>),
} }
#[derive(Debug)] #[derive(Clone)]
pub struct ConsoleStatement { pub struct ConsoleStatement<'a> {
pub parent: Option<Weak<Statement>>, pub parent: Cell<Option<&'a Statement<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub function: ConsoleFunction, pub function: ConsoleFunction<'a>,
} }
impl Node for ConsoleStatement { impl<'a> Node for ConsoleStatement<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl FromAst<leo_ast::FormattedString> for FormattedString { impl<'a> FromAst<'a, leo_ast::FormattedString> for FormattedString<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::FormattedString, value: &leo_ast::FormattedString,
_expected_type: Option<PartialType>, _expected_type: Option<PartialType<'a>>,
) -> Result<Self, AsgConvertError> { ) -> Result<Self, AsgConvertError> {
if value.parameters.len() != value.containers.len() { if value.parameters.len() != value.containers.len() {
// + 1 for formatting string as to not confuse user // + 1 for formatting string as to not confuse user
@ -65,7 +65,7 @@ impl FromAst<leo_ast::FormattedString> for FormattedString {
} }
let mut parameters = vec![]; let mut parameters = vec![];
for parameter in value.parameters.iter() { for parameter in value.parameters.iter() {
parameters.push(Arc::<Expression>::from_ast(scope, parameter, None)?); parameters.push(Cell::new(<&Expression<'a>>::from_ast(scope, parameter, None)?));
} }
Ok(FormattedString { Ok(FormattedString {
string: value.string.clone(), string: value.string.clone(),
@ -76,7 +76,7 @@ impl FromAst<leo_ast::FormattedString> for FormattedString {
} }
} }
impl Into<leo_ast::FormattedString> for &FormattedString { impl<'a> Into<leo_ast::FormattedString> for &FormattedString<'a> {
fn into(self) -> leo_ast::FormattedString { fn into(self) -> leo_ast::FormattedString {
leo_ast::FormattedString { leo_ast::FormattedString {
string: self.string.clone(), string: self.string.clone(),
@ -85,27 +85,25 @@ impl Into<leo_ast::FormattedString> for &FormattedString {
.iter() .iter()
.map(|span| leo_ast::FormattedContainer { span: span.clone() }) .map(|span| leo_ast::FormattedContainer { span: span.clone() })
.collect(), .collect(),
parameters: self.parameters.iter().map(|e| e.as_ref().into()).collect(), parameters: self.parameters.iter().map(|e| e.get().into()).collect(),
span: self.span.clone(), span: self.span.clone(),
} }
} }
} }
impl FromAst<leo_ast::ConsoleStatement> for ConsoleStatement { impl<'a> FromAst<'a, leo_ast::ConsoleStatement> for ConsoleStatement<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
statement: &leo_ast::ConsoleStatement, statement: &leo_ast::ConsoleStatement,
_expected_type: Option<PartialType>, _expected_type: Option<PartialType<'a>>,
) -> Result<Self, AsgConvertError> { ) -> Result<Self, AsgConvertError> {
Ok(ConsoleStatement { Ok(ConsoleStatement {
parent: None, parent: Cell::new(None),
span: Some(statement.span.clone()), span: Some(statement.span.clone()),
function: match &statement.function { function: match &statement.function {
AstConsoleFunction::Assert(expression) => ConsoleFunction::Assert(Arc::<Expression>::from_ast( AstConsoleFunction::Assert(expression) => ConsoleFunction::Assert(Cell::new(
scope, <&Expression<'a>>::from_ast(scope, expression, Some(Type::Boolean.into()))?,
expression, )),
Some(Type::Boolean.into()),
)?),
AstConsoleFunction::Debug(formatted_string) => { AstConsoleFunction::Debug(formatted_string) => {
ConsoleFunction::Debug(FormattedString::from_ast(scope, formatted_string, None)?) ConsoleFunction::Debug(FormattedString::from_ast(scope, formatted_string, None)?)
} }
@ -120,12 +118,12 @@ impl FromAst<leo_ast::ConsoleStatement> for ConsoleStatement {
} }
} }
impl Into<leo_ast::ConsoleStatement> for &ConsoleStatement { impl<'a> Into<leo_ast::ConsoleStatement> for &ConsoleStatement<'a> {
fn into(self) -> leo_ast::ConsoleStatement { fn into(self) -> leo_ast::ConsoleStatement {
use ConsoleFunction::*; use ConsoleFunction::*;
leo_ast::ConsoleStatement { leo_ast::ConsoleStatement {
function: match &self.function { function: match &self.function {
Assert(e) => AstConsoleFunction::Assert(e.as_ref().into()), Assert(e) => AstConsoleFunction::Assert(e.get().into()),
Debug(formatted_string) => AstConsoleFunction::Debug(formatted_string.into()), Debug(formatted_string) => AstConsoleFunction::Debug(formatted_string.into()),
Error(formatted_string) => AstConsoleFunction::Error(formatted_string.into()), Error(formatted_string) => AstConsoleFunction::Error(formatted_string.into()),
Log(formatted_string) => AstConsoleFunction::Log(formatted_string.into()), Log(formatted_string) => AstConsoleFunction::Log(formatted_string.into()),

View File

@ -28,39 +28,37 @@ use crate::{
Type, Type,
Variable, Variable,
}; };
use leo_ast::{AstError, DeprecatedError};
use std::{ use std::cell::{Cell, RefCell};
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct DefinitionStatement { pub struct DefinitionStatement<'a> {
pub parent: Option<Weak<Statement>>, pub parent: Cell<Option<&'a Statement<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub variables: Vec<Variable>, pub variables: Vec<&'a Variable<'a>>,
pub value: Arc<Expression>, pub value: Cell<&'a Expression<'a>>,
} }
impl Node for DefinitionStatement { impl<'a> Node for DefinitionStatement<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl FromAst<leo_ast::DefinitionStatement> for Arc<Statement> { impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
statement: &leo_ast::DefinitionStatement, statement: &leo_ast::DefinitionStatement,
_expected_type: Option<PartialType>, _expected_type: Option<PartialType<'a>>,
) -> Result<Arc<Statement>, AsgConvertError> { ) -> Result<Self, AsgConvertError> {
let type_ = statement let type_ = statement
.type_ .type_
.as_ref() .as_ref()
.map(|x| scope.borrow().resolve_ast_type(&x)) .map(|x| scope.resolve_ast_type(&x))
.transpose()?; .transpose()?;
let value = Arc::<Expression>::from_ast(scope, &statement.value, type_.clone().map(Into::into))?; let value = <&Expression<'a>>::from_ast(scope, &statement.value, type_.clone().map(Into::into))?;
let type_ = type_.or_else(|| value.get_type()); let type_ = type_.or_else(|| value.get_type());
@ -92,46 +90,47 @@ impl FromAst<leo_ast::DefinitionStatement> for Arc<Statement> {
} }
for (variable, type_) in statement.variable_names.iter().zip(output_types.into_iter()) { for (variable, type_) in statement.variable_names.iter().zip(output_types.into_iter()) {
if statement.declaration_type == leo_ast::Declare::Const && variable.mutable { if statement.declaration_type == leo_ast::Declare::Const {
return Err(AsgConvertError::illegal_ast_structure("cannot have const mut")); return Err(AsgConvertError::AstError(AstError::DeprecatedError(
DeprecatedError::const_statement(&statement.span),
)));
} }
variables.push(Arc::new(RefCell::new(InnerVariable { variables.push(&*scope.alloc_variable(RefCell::new(InnerVariable {
id: uuid::Uuid::new_v4(), id: scope.context.get_id(),
name: variable.identifier.clone(), name: variable.identifier.clone(),
type_: type_ type_:
.ok_or_else(|| AsgConvertError::unresolved_type(&variable.identifier.name, &statement.span))?, type_.ok_or_else(|| AsgConvertError::unresolved_type(&variable.identifier.name, &statement.span))?,
mutable: variable.mutable, mutable: variable.mutable,
const_: false,
declaration: crate::VariableDeclaration::Definition, declaration: crate::VariableDeclaration::Definition,
references: vec![], references: vec![],
assignments: vec![], assignments: vec![],
}))); })));
} }
{ for variable in variables.iter() {
let mut scope_borrow = scope.borrow_mut(); scope
for variable in variables.iter() { .variables
scope_borrow .borrow_mut()
.variables .insert(variable.borrow().name.name.clone(), *variable);
.insert(variable.borrow().name.name.clone(), variable.clone());
}
} }
let statement = Arc::new(Statement::Definition(DefinitionStatement { let statement = scope.alloc_statement(Statement::Definition(DefinitionStatement {
parent: None, parent: Cell::new(None),
span: Some(statement.span.clone()), span: Some(statement.span.clone()),
variables: variables.clone(), variables: variables.clone(),
value, value: Cell::new(value),
})); }));
variables.iter().for_each(|variable| { for variable in variables {
variable.borrow_mut().assignments.push(Arc::downgrade(&statement)); variable.borrow_mut().assignments.push(statement);
}); }
Ok(statement) Ok(statement)
} }
} }
impl Into<leo_ast::DefinitionStatement> for &DefinitionStatement { impl<'a> Into<leo_ast::DefinitionStatement> for &DefinitionStatement<'a> {
fn into(self) -> leo_ast::DefinitionStatement { fn into(self) -> leo_ast::DefinitionStatement {
assert!(!self.variables.is_empty()); assert!(!self.variables.is_empty());
@ -145,7 +144,7 @@ impl Into<leo_ast::DefinitionStatement> for &DefinitionStatement {
span: variable.name.span.clone(), span: variable.name.span.clone(),
}); });
if type_.is_none() { if type_.is_none() {
type_ = Some((&variable.type_).into()); type_ = Some((&variable.type_.clone()).into());
} }
} }
@ -153,7 +152,7 @@ impl Into<leo_ast::DefinitionStatement> for &DefinitionStatement {
declaration_type: leo_ast::Declare::Let, declaration_type: leo_ast::Declare::Let,
variable_names, variable_names,
type_, type_,
value: self.value.as_ref().into(), value: self.value.get().into(),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -16,41 +16,41 @@
use crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement}; use crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement};
use std::sync::{Arc, Weak}; use std::cell::Cell;
#[derive(Debug)] #[derive(Clone)]
pub struct ExpressionStatement { pub struct ExpressionStatement<'a> {
pub parent: Option<Weak<Statement>>, pub parent: Cell<Option<&'a Statement<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub expression: Arc<Expression>, pub expression: Cell<&'a Expression<'a>>,
} }
impl Node for ExpressionStatement { impl<'a> Node for ExpressionStatement<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl FromAst<leo_ast::ExpressionStatement> for ExpressionStatement { impl<'a> FromAst<'a, leo_ast::ExpressionStatement> for ExpressionStatement<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
statement: &leo_ast::ExpressionStatement, statement: &leo_ast::ExpressionStatement,
_expected_type: Option<PartialType>, _expected_type: Option<PartialType<'a>>,
) -> Result<Self, AsgConvertError> { ) -> Result<Self, AsgConvertError> {
let expression = Arc::<Expression>::from_ast(scope, &statement.expression, None)?; let expression = <&Expression<'a>>::from_ast(scope, &statement.expression, None)?;
Ok(ExpressionStatement { Ok(ExpressionStatement {
parent: None, parent: Cell::new(None),
span: Some(statement.span.clone()), span: Some(statement.span.clone()),
expression, expression: Cell::new(expression),
}) })
} }
} }
impl Into<leo_ast::ExpressionStatement> for &ExpressionStatement { impl<'a> Into<leo_ast::ExpressionStatement> for &ExpressionStatement<'a> {
fn into(self) -> leo_ast::ExpressionStatement { fn into(self) -> leo_ast::ExpressionStatement {
leo_ast::ExpressionStatement { leo_ast::ExpressionStatement {
expression: self.expression.as_ref().into(), expression: self.expression.get().into(),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -30,76 +30,74 @@ use crate::{
Variable, Variable,
}; };
use std::{ use std::cell::{Cell, RefCell};
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)] #[derive(Clone)]
pub struct IterationStatement { pub struct IterationStatement<'a> {
pub parent: Option<Weak<Statement>>, pub parent: Cell<Option<&'a Statement<'a>>>,
pub span: Option<Span>, pub span: Option<Span>,
pub variable: Variable, pub variable: &'a Variable<'a>,
pub start: Arc<Expression>, pub start: Cell<&'a Expression<'a>>,
pub stop: Arc<Expression>, pub stop: Cell<&'a Expression<'a>>,
pub body: Arc<Statement>, pub body: Cell<&'a Statement<'a>>,
} }
impl Node for IterationStatement { impl<'a> Node for IterationStatement<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl FromAst<leo_ast::IterationStatement> for Arc<Statement> { impl<'a> FromAst<'a, leo_ast::IterationStatement> for &'a Statement<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
statement: &leo_ast::IterationStatement, statement: &leo_ast::IterationStatement,
_expected_type: Option<PartialType>, _expected_type: Option<PartialType<'a>>,
) -> Result<Arc<Statement>, AsgConvertError> { ) -> Result<Self, AsgConvertError> {
let expected_index_type = Some(PartialType::Integer(None, Some(IntegerType::U32))); let expected_index_type = Some(PartialType::Integer(None, Some(IntegerType::U32)));
let start = Arc::<Expression>::from_ast(scope, &statement.start, expected_index_type.clone())?; let start = <&Expression<'a>>::from_ast(scope, &statement.start, expected_index_type.clone())?;
let stop = Arc::<Expression>::from_ast(scope, &statement.stop, expected_index_type)?; let stop = <&Expression<'a>>::from_ast(scope, &statement.stop, expected_index_type)?;
let variable = Arc::new(RefCell::new(InnerVariable { let variable = scope.alloc_variable(RefCell::new(InnerVariable {
id: uuid::Uuid::new_v4(), id: scope.context.get_id(),
name: statement.variable.clone(), name: statement.variable.clone(),
type_: start type_: start
.get_type() .get_type()
.ok_or_else(|| AsgConvertError::unresolved_type(&statement.variable.name, &statement.span))?, .ok_or_else(|| AsgConvertError::unresolved_type(&statement.variable.name, &statement.span))?,
mutable: false, mutable: false,
const_: true,
declaration: crate::VariableDeclaration::IterationDefinition, declaration: crate::VariableDeclaration::IterationDefinition,
references: vec![], references: vec![],
assignments: vec![], assignments: vec![],
})); }));
scope scope
.borrow_mut()
.variables .variables
.insert(statement.variable.name.clone(), variable.clone()); .borrow_mut()
.insert(statement.variable.name.clone(), variable);
let statement = Arc::new(Statement::Iteration(IterationStatement { let statement = scope.alloc_statement(Statement::Iteration(IterationStatement {
parent: None, parent: Cell::new(None),
span: Some(statement.span.clone()), span: Some(statement.span.clone()),
variable: variable.clone(), variable,
stop, stop: Cell::new(stop),
start, start: Cell::new(start),
body: Arc::new(Statement::Block(crate::BlockStatement::from_ast( body: Cell::new(scope.alloc_statement(Statement::Block(crate::BlockStatement::from_ast(
scope, scope,
&statement.block, &statement.block,
None, None,
)?)), )?))),
})); }));
variable.borrow_mut().assignments.push(Arc::downgrade(&statement)); variable.borrow_mut().assignments.push(statement);
Ok(statement) Ok(statement)
} }
} }
impl Into<leo_ast::IterationStatement> for &IterationStatement { impl<'a> Into<leo_ast::IterationStatement> for &IterationStatement<'a> {
fn into(self) -> leo_ast::IterationStatement { fn into(self) -> leo_ast::IterationStatement {
leo_ast::IterationStatement { leo_ast::IterationStatement {
variable: self.variable.borrow().name.clone(), variable: self.variable.borrow().name.clone(),
start: self.start.as_ref().into(), start: self.start.get().into(),
stop: self.stop.as_ref().into(), stop: self.stop.get().into(),
block: match self.body.as_ref() { block: match self.body.get() {
Statement::Block(block) => block.into(), Statement::Block(block) => block.into(),
_ => unimplemented!(), _ => unimplemented!(),
}, },

View File

@ -44,21 +44,19 @@ pub use return_::*;
use crate::{AsgConvertError, FromAst, Node, PartialType, Scope, Span}; use crate::{AsgConvertError, FromAst, Node, PartialType, Scope, Span};
use std::sync::Arc; #[derive(Clone)]
pub enum Statement<'a> {
#[derive(Debug)] Return(ReturnStatement<'a>),
pub enum Statement { Definition(DefinitionStatement<'a>),
Return(ReturnStatement), Assign(AssignStatement<'a>),
Definition(DefinitionStatement), Conditional(ConditionalStatement<'a>),
Assign(AssignStatement), Iteration(IterationStatement<'a>),
Conditional(ConditionalStatement), Console(ConsoleStatement<'a>),
Iteration(IterationStatement), Expression(ExpressionStatement<'a>),
Console(ConsoleStatement), Block(BlockStatement<'a>),
Expression(ExpressionStatement),
Block(BlockStatement),
} }
impl Node for Statement { impl<'a> Node for Statement<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
use Statement::*; use Statement::*;
match self { match self {
@ -74,31 +72,37 @@ impl Node for Statement {
} }
} }
impl FromAst<leo_ast::Statement> for Arc<Statement> { impl<'a> FromAst<'a, leo_ast::Statement> for &'a Statement<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
value: &leo_ast::Statement, value: &leo_ast::Statement,
_expected_type: Option<PartialType>, _expected_type: Option<PartialType<'a>>,
) -> Result<Arc<Statement>, AsgConvertError> { ) -> Result<&'a Statement<'a>, AsgConvertError> {
use leo_ast::Statement::*; use leo_ast::Statement::*;
Ok(match value { Ok(match value {
Return(statement) => Arc::new(Statement::Return(ReturnStatement::from_ast(scope, statement, None)?)), Return(statement) => {
Definition(statement) => Arc::<Statement>::from_ast(scope, statement, None)?, scope.alloc_statement(Statement::Return(ReturnStatement::from_ast(scope, statement, None)?))
Assign(statement) => Arc::<Statement>::from_ast(scope, statement, None)?, }
Conditional(statement) => Arc::new(Statement::Conditional(ConditionalStatement::from_ast( Definition(statement) => Self::from_ast(scope, statement, None)?,
Assign(statement) => Self::from_ast(scope, statement, None)?,
Conditional(statement) => scope.alloc_statement(Statement::Conditional(ConditionalStatement::from_ast(
scope, statement, None, scope, statement, None,
)?)), )?)),
Iteration(statement) => Arc::<Statement>::from_ast(scope, statement, None)?, Iteration(statement) => Self::from_ast(scope, statement, None)?,
Console(statement) => Arc::new(Statement::Console(ConsoleStatement::from_ast(scope, statement, None)?)), Console(statement) => {
Expression(statement) => Arc::new(Statement::Expression(ExpressionStatement::from_ast( scope.alloc_statement(Statement::Console(ConsoleStatement::from_ast(scope, statement, None)?))
}
Expression(statement) => scope.alloc_statement(Statement::Expression(ExpressionStatement::from_ast(
scope, statement, None, scope, statement, None,
)?)), )?)),
Block(statement) => Arc::new(Statement::Block(BlockStatement::from_ast(scope, statement, None)?)), Block(statement) => {
scope.alloc_statement(Statement::Block(BlockStatement::from_ast(scope, statement, None)?))
}
}) })
} }
} }
impl Into<leo_ast::Statement> for &Statement { impl<'a> Into<leo_ast::Statement> for &Statement<'a> {
fn into(self) -> leo_ast::Statement { fn into(self) -> leo_ast::Statement {
use Statement::*; use Statement::*;
match self { match self {

View File

@ -16,44 +16,46 @@
use crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type}; use crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type};
use std::sync::{Arc, Weak}; use std::cell::Cell;
#[derive(Clone)]
#[derive(Debug)] pub struct ReturnStatement<'a> {
pub struct ReturnStatement { pub parent: Cell<Option<&'a Statement<'a>>>,
pub parent: Option<Weak<Statement>>,
pub span: Option<Span>, pub span: Option<Span>,
pub expression: Arc<Expression>, pub expression: Cell<&'a Expression<'a>>,
} }
impl Node for ReturnStatement { impl<'a> Node for ReturnStatement<'a> {
fn span(&self) -> Option<&Span> { fn span(&self) -> Option<&Span> {
self.span.as_ref() self.span.as_ref()
} }
} }
impl FromAst<leo_ast::ReturnStatement> for ReturnStatement { impl<'a> FromAst<'a, leo_ast::ReturnStatement> for ReturnStatement<'a> {
fn from_ast( fn from_ast(
scope: &Scope, scope: &'a Scope<'a>,
statement: &leo_ast::ReturnStatement, statement: &leo_ast::ReturnStatement,
_expected_type: Option<PartialType>, _expected_type: Option<PartialType<'a>>,
) -> Result<Self, AsgConvertError> { ) -> Result<Self, AsgConvertError> {
let return_type: Option<Type> = scope let return_type: Option<Type> = scope
.borrow()
.resolve_current_function() .resolve_current_function()
.map(|x| x.output.clone()) .map(|x| x.output.clone())
.map(Into::into); .map(Into::into);
Ok(ReturnStatement { Ok(ReturnStatement {
parent: None, parent: Cell::new(None),
span: Some(statement.span.clone()), span: Some(statement.span.clone()),
expression: Arc::<Expression>::from_ast(scope, &statement.expression, return_type.map(Into::into))?, expression: Cell::new(<&Expression<'a>>::from_ast(
scope,
&statement.expression,
return_type.map(Into::into),
)?),
}) })
} }
} }
impl Into<leo_ast::ReturnStatement> for &ReturnStatement { impl<'a> Into<leo_ast::ReturnStatement> for &ReturnStatement<'a> {
fn into(self) -> leo_ast::ReturnStatement { fn into(self) -> leo_ast::ReturnStatement {
leo_ast::ReturnStatement { leo_ast::ReturnStatement {
expression: self.expression.as_ref().into(), expression: self.expression.get().into(),
span: self.span.clone().unwrap_or_default(), span: self.span.clone().unwrap_or_default(),
} }
} }

View File

@ -17,14 +17,11 @@
use crate::Circuit; use crate::Circuit;
pub use leo_ast::IntegerType; pub use leo_ast::IntegerType;
use std::{ use std::fmt;
fmt,
sync::{Arc, Weak},
};
/// A type in an ASG. /// A type in an asg.
#[derive(Debug, Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub enum Type { pub enum Type<'a> {
// Data types // Data types
Address, Address,
Boolean, Boolean,
@ -33,55 +30,21 @@ pub enum Type {
Integer(IntegerType), Integer(IntegerType),
// Data type wrappers // Data type wrappers
Array(Box<Type>, usize), Array(Box<Type<'a>>, usize),
Tuple(Vec<Type>), Tuple(Vec<Type<'a>>),
Circuit(Arc<Circuit>), Circuit(&'a Circuit<'a>),
} }
#[derive(Debug, Clone)] #[derive(Clone, PartialEq)]
pub enum WeakType { pub enum PartialType<'a> {
Type(Type), // circuit not allowed Type(Type<'a>), // non-array or tuple
Circuit(Weak<Circuit>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum PartialType {
Type(Type), // non-array or tuple
Integer(Option<IntegerType>, Option<IntegerType>), // specific, context-specific Integer(Option<IntegerType>, Option<IntegerType>), // specific, context-specific
Array(Option<Box<PartialType>>, Option<usize>), Array(Option<Box<PartialType<'a>>>, Option<usize>),
Tuple(Vec<Option<PartialType>>), Tuple(Vec<Option<PartialType<'a>>>),
} }
impl Into<Type> for WeakType { impl<'a> Into<Option<Type<'a>>> for PartialType<'a> {
fn into(self) -> Type { fn into(self) -> Option<Type<'a>> {
match self {
WeakType::Type(t) => t,
WeakType::Circuit(circuit) => Type::Circuit(circuit.upgrade().unwrap()),
}
}
}
impl WeakType {
pub fn strong(self) -> Type {
self.into()
}
pub fn is_unit(&self) -> bool {
matches!(self, WeakType::Type(Type::Tuple(t)) if t.is_empty())
}
}
impl Into<WeakType> for Type {
fn into(self) -> WeakType {
match self {
Type::Circuit(circuit) => WeakType::Circuit(Arc::downgrade(&circuit)),
t => WeakType::Type(t),
}
}
}
impl Into<Option<Type>> for PartialType {
fn into(self) -> Option<Type> {
match self { match self {
PartialType::Type(t) => Some(t), PartialType::Type(t) => Some(t),
PartialType::Integer(sub_type, contextual_type) => Some(Type::Integer(sub_type.or(contextual_type)?)), PartialType::Integer(sub_type, contextual_type) => Some(Type::Integer(sub_type.or(contextual_type)?)),
@ -96,12 +59,12 @@ impl Into<Option<Type>> for PartialType {
} }
} }
impl PartialType { impl<'a> PartialType<'a> {
pub fn full(self) -> Option<Type> { pub fn full(self) -> Option<Type<'a>> {
self.into() self.into()
} }
pub fn matches(&self, other: &Type) -> bool { pub fn matches(&self, other: &Type<'a>) -> bool {
match (self, other) { match (self, other) {
(PartialType::Type(t), other) => t.is_assignable_from(other), (PartialType::Type(t), other) => t.is_assignable_from(other),
(PartialType::Integer(self_sub_type, _), Type::Integer(sub_type)) => { (PartialType::Integer(self_sub_type, _), Type::Integer(sub_type)) => {
@ -137,8 +100,8 @@ impl PartialType {
} }
} }
impl Into<PartialType> for Type { impl<'a> Into<PartialType<'a>> for Type<'a> {
fn into(self) -> PartialType { fn into(self) -> PartialType<'a> {
match self { match self {
Type::Integer(sub_type) => PartialType::Integer(Some(sub_type), None), Type::Integer(sub_type) => PartialType::Integer(Some(sub_type), None),
Type::Array(element, len) => PartialType::Array(Some(Box::new((*element).into())), Some(len)), Type::Array(element, len) => PartialType::Array(Some(Box::new((*element).into())), Some(len)),
@ -148,12 +111,12 @@ impl Into<PartialType> for Type {
} }
} }
impl Type { impl<'a> Type<'a> {
pub fn is_assignable_from(&self, from: &Type) -> bool { pub fn is_assignable_from(&self, from: &Type<'a>) -> bool {
self == from self == from
} }
pub fn partial(self) -> PartialType { pub fn partial(self) -> PartialType<'a> {
self.into() self.into()
} }
@ -162,7 +125,7 @@ impl Type {
} }
} }
impl fmt::Display for Type { impl<'a> fmt::Display for Type<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Type::Address => write!(f, "address"), Type::Address => write!(f, "address"),
@ -186,7 +149,7 @@ impl fmt::Display for Type {
} }
} }
impl fmt::Display for PartialType { impl<'a> fmt::Display for PartialType<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
PartialType::Type(t) => t.fmt(f), PartialType::Type(t) => t.fmt(f),
@ -226,7 +189,7 @@ impl fmt::Display for PartialType {
} }
} }
impl Into<leo_ast::Type> for &Type { impl<'a> Into<leo_ast::Type> for &Type<'a> {
fn into(self) -> leo_ast::Type { fn into(self) -> leo_ast::Type {
use Type::*; use Type::*;
match self { match self {

View File

@ -14,17 +14,13 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use std::cell::RefCell;
use crate::{Expression, Statement, Type}; use crate::{Expression, Statement, Type};
use leo_ast::Identifier; use leo_ast::Identifier;
use std::{
cell::RefCell,
sync::{Arc, Weak},
};
use uuid::Uuid;
/// Specifies how a program variable was declared. /// Specifies how a program variable was declared.
#[derive(Debug, PartialEq)] #[derive(Clone, Copy, PartialEq)]
pub enum VariableDeclaration { pub enum VariableDeclaration {
Definition, Definition,
IterationDefinition, IterationDefinition,
@ -33,16 +29,16 @@ pub enum VariableDeclaration {
} }
/// Stores information on a program variable. /// Stores information on a program variable.
#[derive(Debug)] #[derive(Clone)]
pub struct InnerVariable { pub struct InnerVariable<'a> {
pub id: Uuid, pub id: u32,
pub name: Identifier, pub name: Identifier,
pub type_: Type, pub type_: Type<'a>,
pub mutable: bool, pub mutable: bool,
pub const_: bool, // only function arguments, const var definitions NOT included
pub declaration: VariableDeclaration, pub declaration: VariableDeclaration,
pub references: Vec<Weak<Expression>>, // all Expression::VariableRef or panic pub references: Vec<&'a Expression<'a>>, // all Expression::VariableRef or panic
pub assignments: Vec<Weak<Statement>>, // all Statement::Assign or panic -- must be 1 if not mutable, or 0 if declaration == input | parameter pub assignments: Vec<&'a Statement<'a>>, // all Statement::Assign or panic -- must be 1 if not mutable, or 0 if declaration == input | parameter
} }
pub type Variable = Arc<RefCell<InnerVariable>>; pub type Variable<'a> = RefCell<InnerVariable<'a>>;
pub type WeakVariable = Weak<RefCell<InnerVariable>>;

View File

@ -1,4 +1,4 @@
// Multidimensional array syntax in leo // Multidimensional array syntax in leo
function main() { function main() {
const a: [u32; (3, 2)] = [[0; 3]; 2]; // initializer (incorrectly reversed ordering) let a: [u32; (3, 2)] = [[0; 3]; 2]; // initializer (incorrectly reversed ordering)
} }

View File

@ -1,4 +1,4 @@
// Multidimensional array syntax in leo // Multidimensional array syntax in leo
function main() { function main() {
const a: [u32; (3, 2)] = [0; (2, 3)]; // initializer (incorrectly reversed ordering) let a: [u32; (3, 2)] = [0; (2, 3)]; // initializer (incorrectly reversed ordering)
} }

View File

@ -1,3 +1,3 @@
function main() { function main() {
const b: [[u8; 2]; 3] = [[0; 3]; 2]; // initializer (incorrectly reversed ordering) let b: [[u8; 2]; 3] = [[0; 3]; 2]; // initializer (incorrectly reversed ordering)
} }

View File

@ -1,3 +1,3 @@
function main() { function main() {
const b: [[[u8; 2]; 3]; 4] = [[[0; 4]; 3]; 2]; // initializer (incorrectly reversed ordering) let b: [[[u8; 2]; 3]; 4] = [[[0; 4]; 3]; 2]; // initializer (incorrectly reversed ordering)
} }

View File

@ -1,3 +1,3 @@
function main() { function main() {
const b: [[u8; 2]; 3] = [0; (2, 3)]; // initializer (incorrectly reversed ordering) let b: [[u8; 2]; 3] = [0; (2, 3)]; // initializer (incorrectly reversed ordering)
} }

View File

@ -1,3 +1,3 @@
function main() { function main() {
const b: [[[u8; 2]; 3]; 4] = [0; (2, 3, 4)]; // initializer (incorrectly reversed ordering) let b: [[[u8; 2]; 3]; 4] = [0; (2, 3, 4)]; // initializer (incorrectly reversed ordering)
} }

View File

@ -1,3 +1,3 @@
function main() { function main() {
const b: [u8; (2, 3)] = [[0; 2]; 3]; // initializer (incorrectly reversed ordering) let b: [u8; (2, 3)] = [[0; 2]; 3]; // initializer (incorrectly reversed ordering)
} }

View File

@ -1,7 +1,7 @@
function main() { function main() {
const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline let a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
const b: [u8; (2, 3)] = [[0; 3]; 2]; // initializer let b: [u8; (2, 3)] = [[0; 3]; 2]; // initializer
console.assert(a == b); console.assert(a == b);
} }

View File

@ -1,3 +1,3 @@
function main() { function main() {
const b: [u8; (4, 3, 2)] = [[[0; 4]; 3]; 2]; // initializer (incorrectly reversed ordering) let b: [u8; (4, 3, 2)] = [[[0; 4]; 3]; 2]; // initializer (incorrectly reversed ordering)
} }

View File

@ -1,3 +1,3 @@
function main() { function main() {
const b: [u8; (2, 3)] = [0; (3, 2)]; // initializer (incorrectly reversed ordering) let b: [u8; (2, 3)] = [0; (3, 2)]; // initializer (incorrectly reversed ordering)
} }

View File

@ -1,7 +1,7 @@
function main() { function main() {
const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline let a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
const b: [u8; (2, 3)] = [0; (2, 3)]; // initializer let b: [u8; (2, 3)] = [0; (2, 3)]; // initializer
console.assert(a == b); console.assert(a == b);
} }

View File

@ -1,3 +1,3 @@
function main() { function main() {
const b: [u8; (4, 3, 2)] = [0; (2, 3, 4)]; // initializer (incorrectly reversed order) let b: [u8; (4, 3, 2)] = [0; (2, 3, 4)]; // initializer (incorrectly reversed order)
} }

View File

@ -3,6 +3,6 @@ function foo() -> field {
} }
function main() { function main() {
const myGlobal = 42field; let myGlobal = 42field;
let err = foo(); let err = foo();
} }

View File

@ -1,5 +1,5 @@
// Constant variables are immutable by default. // Let variables are immutable by default.
function main() { function main() {
const a = 1u32; let a = 1u32;
a = 0; a = 0;
} }

View File

@ -1,4 +0,0 @@
// Adding the `mut` keyword to a constant variable is illegal
function main() {
const mut a = 1u32;
}

View File

@ -28,12 +28,6 @@ fn test_const_fail() {
load_asg(program_string).err().unwrap(); load_asg(program_string).err().unwrap();
} }
#[test]
fn test_const_mut_fail() {
let program_string = include_str!("const_mut.leo");
load_asg(program_string).err().unwrap();
}
#[test] #[test]
fn test_array() { fn test_array() {
let program_string = include_str!("array.leo"); let program_string = include_str!("array.leo");

View File

@ -0,0 +1,3 @@
function main() {
const x = 1u32;
}

View File

@ -21,3 +21,9 @@ fn test_num_returns_fail() {
let program_string = include_str!("num_returns_fail.leo"); let program_string = include_str!("num_returns_fail.leo");
load_asg(program_string).err().unwrap(); load_asg(program_string).err().unwrap();
} }
#[test]
fn test_const_declaration_fail() {
let program_string = include_str!("const_declaration_fail.leo");
load_asg(program_string).err().unwrap();
}

View File

@ -26,20 +26,27 @@ mod pass;
const TESTING_FILEPATH: &str = "input.leo"; const TESTING_FILEPATH: &str = "input.leo";
const TESTING_PROGRAM_NAME: &str = "test_program"; const TESTING_PROGRAM_NAME: &str = "test_program";
fn load_asg(program_string: &str) -> Result<Program, AsgConvertError> { fn load_asg(program_string: &str) -> Result<Program<'static>, AsgConvertError> {
load_asg_imports(program_string, &mut NullImportResolver) load_asg_imports(make_test_context(), program_string, &mut NullImportResolver)
} }
fn load_asg_imports<T: ImportResolver + 'static>( fn load_asg_imports<'a, T: ImportResolver<'a>>(
context: AsgContext<'a>,
program_string: &str, program_string: &str,
imports: &mut T, imports: &mut T,
) -> Result<Program, AsgConvertError> { ) -> Result<Program<'a>, AsgConvertError> {
let grammar = Grammar::new(Path::new(&TESTING_FILEPATH), program_string)?; let grammar = Grammar::new(Path::new(&TESTING_FILEPATH), program_string)?;
let ast = Ast::new(TESTING_PROGRAM_NAME, &grammar)?; let ast = Ast::new(TESTING_PROGRAM_NAME, &grammar)?;
InternalProgram::new(&ast.as_repr(), imports) InternalProgram::new(context, &ast.as_repr(), imports)
} }
fn mocked_resolver() -> MockedImportResolver { fn mocked_resolver<'a>(_context: AsgContext<'a>) -> MockedImportResolver<'a> {
let packages = indexmap::IndexMap::new(); let packages = indexmap::IndexMap::new();
MockedImportResolver { packages } MockedImportResolver { packages }
} }
//convenience function for tests, leaks memory
pub(crate) fn make_test_context() -> AsgContext<'static> {
let allocator = Box::leak(Box::new(new_alloc_context()));
new_context(allocator)
}

View File

@ -1,7 +1,7 @@
function main() { function main() {
const a: [u8; (2, 2, 2)] = [1u8; (2, 2, 2)]; let a: [u8; (2, 2, 2)] = [1u8; (2, 2, 2)];
const b: [u8; (2, 2, 2)] = [[[1u8; 2]; 2]; 2]; let b: [u8; (2, 2, 2)] = [[[1u8; 2]; 2]; 2];
console.assert(a == b); console.assert(a == b);
} }

View File

@ -1,8 +1,8 @@
// Multidimensional array syntax in leo // Multidimensional array syntax in leo
function main() { function main() {
const a = [[0u32, 0u32], [0u32, 0u32], [0u32, 0u32]]; // inline let a = [[0u32, 0u32], [0u32, 0u32], [0u32, 0u32]]; // inline
const b: [u32; (3, 2)] = [[0; 2]; 3]; // initializer let b: [u32; (3, 2)] = [[0; 2]; 3]; // initializer
console.assert(a == b); console.assert(a == b);
} }

View File

@ -1,8 +1,8 @@
// Multidimensional array syntax in leo // Multidimensional array syntax in leo
function main() { function main() {
const a = [[0u32, 0u32], [0u32, 0u32], [0u32, 0u32]]; // inline let a = [[0u32, 0u32], [0u32, 0u32], [0u32, 0u32]]; // inline
const b: [u32; (3, 2)] = [0; (3, 2)]; // initializer let b: [u32; (3, 2)] = [0; (3, 2)]; // initializer
console.assert(a == b); console.assert(a == b);
} }

View File

@ -1,5 +1,5 @@
function main(a: [[u8; 2]; 3]) { function main(a: [[u8; 2]; 3]) {
const b = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline let b = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
console.assert(a == b); console.assert(a == b);
} }

View File

@ -1,5 +1,5 @@
function main(a: [[[u8; 2]; 3]; 4]) { function main(a: [[[u8; 2]; 3]; 4]) {
const b = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]], let b = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]], [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]], [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]]; // inline [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]]; // inline

View File

@ -1,7 +1,7 @@
function main() { function main() {
const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline let a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
const b: [[u8; 2]; 3] = [[0; 2]; 3]; // initializer let b: [[u8; 2]; 3] = [[0; 2]; 3]; // initializer
console.assert(a == b); console.assert(a == b);
} }

View File

@ -1,10 +1,10 @@
function main() { function main() {
const a = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]], let a = [[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]], [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]], [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]],
[[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]]; // inline [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]]; // inline
const b: [[[u8; 2]; 3]; 4] = [[[0; 2]; 3]; 4]; // initializer let b: [[[u8; 2]; 3]; 4] = [[[0; 2]; 3]; 4]; // initializer
console.assert(a == b); console.assert(a == b);
} }

View File

@ -1,7 +1,7 @@
function main() { function main() {
const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline let a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
const b: [[u8; 2]; 3] = [0; (3, 2)]; // initializer let b: [[u8; 2]; 3] = [0; (3, 2)]; // initializer
console.assert(a == b); console.assert(a == b);
} }

Some files were not shown because too many files have changed in this diff Show More