mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-25 03:04:13 +03:00
Merge pull request #594 from AleoHQ/asg-merge
[Feature] Abstract Syntax Graph (ASG) - Base Implementation
This commit is contained in:
commit
f2c5f3b266
14
.github/workflows/ci.yml
vendored
14
.github/workflows/ci.yml
vendored
@ -43,7 +43,7 @@ jobs:
|
||||
matrix:
|
||||
rust:
|
||||
- stable
|
||||
- nightly
|
||||
# - nightly
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
@ -70,12 +70,12 @@ jobs:
|
||||
args: --examples --all-features --all
|
||||
if: matrix.rust == 'stable'
|
||||
|
||||
- name: Check benchmarks on nightly
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: clippy
|
||||
args: --all-features --examples --all --benches
|
||||
if: matrix.rust == 'nightly'
|
||||
# - name: Check benchmarks on nightly
|
||||
# uses: actions-rs/cargo@v1
|
||||
# with:
|
||||
# command: clippy
|
||||
# args: --all-features --examples --all --benches
|
||||
# if: matrix.rust == 'nightly'
|
||||
|
||||
test:
|
||||
name: Test
|
||||
|
@ -10,7 +10,7 @@ use_try_shorthand = true
|
||||
# Nightly configurations
|
||||
imports_layout = "HorizontalVertical"
|
||||
license_template_path = ".resources/license_header"
|
||||
merge_imports = true
|
||||
imports_granularity = "Crate"
|
||||
overflow_delimited_expr = true
|
||||
reorder_impl_items = true
|
||||
version = "Two"
|
||||
|
42
Cargo.lock
generated
42
Cargo.lock
generated
@ -917,6 +917,17 @@ dependencies = [
|
||||
"wasi 0.9.0+wasi-snapshot-preview1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"libc",
|
||||
"wasi 0.10.0+wasi-snapshot-preview1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gimli"
|
||||
version = "0.23.0"
|
||||
@ -1219,6 +1230,21 @@ version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
||||
|
||||
[[package]]
|
||||
name = "leo-asg"
|
||||
version = "1.0.8"
|
||||
dependencies = [
|
||||
"criterion",
|
||||
"indexmap",
|
||||
"leo-ast",
|
||||
"leo-grammar",
|
||||
"num-bigint",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "leo-ast"
|
||||
version = "1.0.8"
|
||||
@ -2070,7 +2096,7 @@ version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"getrandom 0.1.15",
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core 0.5.1",
|
||||
@ -2093,7 +2119,7 @@ version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"getrandom 0.1.15",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2157,7 +2183,7 @@ version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"getrandom 0.1.15",
|
||||
"redox_syscall",
|
||||
"rust-argon2",
|
||||
]
|
||||
@ -3106,6 +3132,16 @@ dependencies = [
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
|
||||
dependencies = [
|
||||
"getrandom 0.2.2",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.11"
|
||||
|
@ -36,6 +36,7 @@ members = [
|
||||
"linter",
|
||||
"package",
|
||||
"state",
|
||||
"asg",
|
||||
"symbol-table",
|
||||
"type-inference"
|
||||
]
|
||||
|
48
asg/Cargo.toml
Normal file
48
asg/Cargo.toml
Normal file
@ -0,0 +1,48 @@
|
||||
[package]
|
||||
name = "leo-asg"
|
||||
version = "1.0.8"
|
||||
authors = [ "The Aleo Team <hello@aleo.org>" ]
|
||||
description = "ASG of the Leo programming language"
|
||||
homepage = "https://aleo.org"
|
||||
repository = "https://github.com/AleoHQ/leo"
|
||||
keywords = [
|
||||
"aleo",
|
||||
"cryptography",
|
||||
"leo",
|
||||
"programming-language",
|
||||
"zero-knowledge"
|
||||
]
|
||||
categories = [ "cryptography::cryptocurrencies", "web-programming" ]
|
||||
include = [ "Cargo.toml", "src", "README.md", "LICENSE.md" ]
|
||||
license = "GPL-3.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies.serde]
|
||||
version = "1.0"
|
||||
|
||||
[dependencies.serde_json]
|
||||
version = "1.0"
|
||||
|
||||
[dev-dependencies.criterion]
|
||||
version = "0.3"
|
||||
|
||||
[dependencies.indexmap]
|
||||
version = "1.6"
|
||||
|
||||
[dependencies.thiserror]
|
||||
version = "1.0"
|
||||
|
||||
[dependencies.leo-ast]
|
||||
version = "1.0"
|
||||
path = "../ast"
|
||||
|
||||
[dependencies.leo-grammar]
|
||||
version = "1.0"
|
||||
path = "../grammar"
|
||||
|
||||
[dependencies.uuid]
|
||||
version = "0.8"
|
||||
features = ["v4", "serde"]
|
||||
|
||||
[dependencies.num-bigint]
|
||||
version = "0.3"
|
20
asg/src/checks/mod.rs
Normal file
20
asg/src/checks/mod.rs
Normal file
@ -0,0 +1,20 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
//! Helper methods to determine the correct return value path in an asg.
|
||||
|
||||
mod return_path;
|
||||
pub use return_path::*;
|
127
asg/src/checks/return_path.rs
Normal file
127
asg/src/checks/return_path.rs
Normal file
@ -0,0 +1,127 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{
|
||||
statement::*,
|
||||
BoolAnd,
|
||||
Expression,
|
||||
Monoid,
|
||||
MonoidalReducerExpression,
|
||||
MonoidalReducerStatement,
|
||||
Node,
|
||||
Span,
|
||||
};
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct ReturnPathReducer {
|
||||
pub errors: Vec<(Span, String)>,
|
||||
}
|
||||
|
||||
impl ReturnPathReducer {
|
||||
fn record_error(&mut self, span: Option<&Span>, error: String) {
|
||||
self.errors.push((span.cloned().unwrap_or_default(), error));
|
||||
}
|
||||
|
||||
pub fn new() -> ReturnPathReducer {
|
||||
ReturnPathReducer { errors: vec![] }
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ReturnPathReducer {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
impl MonoidalReducerExpression<BoolAnd> for ReturnPathReducer {
|
||||
fn reduce_expression(&mut self, input: &Arc<Expression>, value: BoolAnd) -> BoolAnd {
|
||||
BoolAnd(false)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
impl MonoidalReducerStatement<BoolAnd> for ReturnPathReducer {
|
||||
fn reduce_assign_access(&mut self, input: &AssignAccess, left: Option<BoolAnd>, right: Option<BoolAnd>) -> BoolAnd {
|
||||
BoolAnd(false)
|
||||
}
|
||||
|
||||
fn reduce_assign(&mut self, input: &AssignStatement, accesses: Vec<BoolAnd>, value: BoolAnd) -> BoolAnd {
|
||||
BoolAnd(false)
|
||||
}
|
||||
|
||||
fn reduce_block(&mut self, input: &BlockStatement, statements: Vec<BoolAnd>) -> BoolAnd {
|
||||
if statements.is_empty() {
|
||||
BoolAnd(false)
|
||||
} else if let Some(index) = statements[..statements.len() - 1].iter().map(|x| x.0).position(|x| x) {
|
||||
self.record_error(
|
||||
input.statements[index].span(),
|
||||
"dead code due to unconditional early return".to_string(),
|
||||
);
|
||||
BoolAnd(true)
|
||||
} else {
|
||||
BoolAnd(statements[statements.len() - 1].0)
|
||||
}
|
||||
}
|
||||
|
||||
fn reduce_conditional_statement(
|
||||
&mut self,
|
||||
input: &ConditionalStatement,
|
||||
condition: BoolAnd,
|
||||
if_true: BoolAnd,
|
||||
if_false: Option<BoolAnd>,
|
||||
) -> BoolAnd {
|
||||
if if_false.as_ref().map(|x| x.0).unwrap_or(false) != if_true.0 {
|
||||
self.record_error(
|
||||
input.span(),
|
||||
"cannot have asymmetrical return in if statement".to_string(),
|
||||
);
|
||||
}
|
||||
if_true.append(if_false.unwrap_or(BoolAnd(false)))
|
||||
}
|
||||
|
||||
fn reduce_formatted_string(&mut self, input: &FormattedString, parameters: Vec<BoolAnd>) -> BoolAnd {
|
||||
BoolAnd(false)
|
||||
}
|
||||
|
||||
fn reduce_console(&mut self, input: &ConsoleStatement, argument: BoolAnd) -> BoolAnd {
|
||||
BoolAnd(false)
|
||||
}
|
||||
|
||||
fn reduce_definition(&mut self, input: &DefinitionStatement, value: BoolAnd) -> BoolAnd {
|
||||
BoolAnd(false)
|
||||
}
|
||||
|
||||
fn reduce_expression_statement(&mut self, input: &ExpressionStatement, expression: BoolAnd) -> BoolAnd {
|
||||
BoolAnd(false)
|
||||
}
|
||||
|
||||
fn reduce_iteration(
|
||||
&mut self,
|
||||
input: &IterationStatement,
|
||||
start: BoolAnd,
|
||||
stop: BoolAnd,
|
||||
body: BoolAnd,
|
||||
) -> BoolAnd {
|
||||
// loops are const defined ranges, so we could probably check if they run one and emit here
|
||||
BoolAnd(false)
|
||||
}
|
||||
|
||||
fn reduce_return(&mut self, input: &ReturnStatement, value: BoolAnd) -> BoolAnd {
|
||||
BoolAnd(true)
|
||||
}
|
||||
}
|
277
asg/src/const_value.rs
Normal file
277
asg/src/const_value.rs
Normal file
@ -0,0 +1,277 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, IntegerType, Span, Type};
|
||||
|
||||
use num_bigint::BigInt;
|
||||
use std::{convert::TryInto, fmt};
|
||||
|
||||
/// Constant integer values in a program.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum ConstInt {
|
||||
I8(i8),
|
||||
I16(i16),
|
||||
I32(i32),
|
||||
I64(i64),
|
||||
I128(i128),
|
||||
U8(u8),
|
||||
U16(u16),
|
||||
U32(u32),
|
||||
U64(u64),
|
||||
U128(u128),
|
||||
}
|
||||
|
||||
/// Specifies how to calculate a group coordinate in a program.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum GroupCoordinate {
|
||||
/// Explicit field element number string.
|
||||
Number(String),
|
||||
|
||||
/// Attempt to recover with a sign high bit.
|
||||
SignHigh,
|
||||
|
||||
/// Attempt to recover with a sign low bit.
|
||||
SignLow,
|
||||
|
||||
/// Try recovering with a sign low - upon failure try sign high.
|
||||
Inferred,
|
||||
}
|
||||
|
||||
impl fmt::Display for GroupCoordinate {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
GroupCoordinate::Number(number) => write!(f, "{}", number),
|
||||
GroupCoordinate::SignHigh => write!(f, "+"),
|
||||
GroupCoordinate::SignLow => write!(f, "-"),
|
||||
GroupCoordinate::Inferred => write!(f, "_"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&leo_ast::GroupCoordinate> for GroupCoordinate {
|
||||
fn from(other: &leo_ast::GroupCoordinate) -> GroupCoordinate {
|
||||
use leo_ast::GroupCoordinate::*;
|
||||
match other {
|
||||
Number(value, _) => GroupCoordinate::Number(value.clone()),
|
||||
SignHigh => GroupCoordinate::SignHigh,
|
||||
SignLow => GroupCoordinate::SignLow,
|
||||
Inferred => GroupCoordinate::Inferred,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::GroupCoordinate> for &GroupCoordinate {
|
||||
fn into(self) -> leo_ast::GroupCoordinate {
|
||||
use GroupCoordinate::*;
|
||||
match self {
|
||||
Number(value) => leo_ast::GroupCoordinate::Number(value.clone(), Default::default()),
|
||||
SignHigh => leo_ast::GroupCoordinate::SignHigh,
|
||||
SignLow => leo_ast::GroupCoordinate::SignLow,
|
||||
Inferred => leo_ast::GroupCoordinate::Inferred,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum GroupValue {
|
||||
Single(String),
|
||||
Tuple(GroupCoordinate, GroupCoordinate),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum ConstValue {
|
||||
Int(ConstInt),
|
||||
Group(GroupValue),
|
||||
Field(BigInt),
|
||||
Address(String),
|
||||
Boolean(bool),
|
||||
|
||||
// compounds
|
||||
Tuple(Vec<ConstValue>),
|
||||
Array(Vec<ConstValue>),
|
||||
}
|
||||
|
||||
macro_rules! const_int_op {
|
||||
($name: ident, $retType: ty, $x: ident, $transform: expr) => {
|
||||
pub fn $name(&self) -> $retType {
|
||||
match self {
|
||||
ConstInt::I8($x) => $transform,
|
||||
ConstInt::I16($x) => $transform,
|
||||
ConstInt::I32($x) => $transform,
|
||||
ConstInt::I64($x) => $transform,
|
||||
ConstInt::I128($x) => $transform,
|
||||
ConstInt::U8($x) => $transform,
|
||||
ConstInt::U16($x) => $transform,
|
||||
ConstInt::U32($x) => $transform,
|
||||
ConstInt::U64($x) => $transform,
|
||||
ConstInt::U128($x) => $transform,
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! const_int_biop {
|
||||
($name: ident, $retType: ty, $x: ident, $y: ident, $transform: expr) => {
|
||||
pub fn $name(&self, other: &ConstInt) -> Option<$retType> {
|
||||
match (self, other) {
|
||||
(ConstInt::I8($x), ConstInt::I8($y)) => $transform,
|
||||
(ConstInt::I16($x), ConstInt::I16($y)) => $transform,
|
||||
(ConstInt::I32($x), ConstInt::I32($y)) => $transform,
|
||||
(ConstInt::I64($x), ConstInt::I64($y)) => $transform,
|
||||
(ConstInt::I128($x), ConstInt::I128($y)) => $transform,
|
||||
(ConstInt::U8($x), ConstInt::U8($y)) => $transform,
|
||||
(ConstInt::U16($x), ConstInt::U16($y)) => $transform,
|
||||
(ConstInt::U32($x), ConstInt::U32($y)) => $transform,
|
||||
(ConstInt::U64($x), ConstInt::U64($y)) => $transform,
|
||||
(ConstInt::U128($x), ConstInt::U128($y)) => $transform,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! const_int_map {
|
||||
($name: ident, $x: ident, $transform: expr) => {
|
||||
pub fn $name(&self) -> Option<ConstInt> {
|
||||
Some(match self {
|
||||
ConstInt::I8($x) => ConstInt::I8($transform),
|
||||
ConstInt::I16($x) => ConstInt::I16($transform),
|
||||
ConstInt::I32($x) => ConstInt::I32($transform),
|
||||
ConstInt::I64($x) => ConstInt::I64($transform),
|
||||
ConstInt::I128($x) => ConstInt::I128($transform),
|
||||
ConstInt::U8($x) => ConstInt::U8($transform),
|
||||
ConstInt::U16($x) => ConstInt::U16($transform),
|
||||
ConstInt::U32($x) => ConstInt::U32($transform),
|
||||
ConstInt::U64($x) => ConstInt::U64($transform),
|
||||
ConstInt::U128($x) => ConstInt::U128($transform),
|
||||
})
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! const_int_bimap {
|
||||
($name: ident, $x: ident, $y: ident, $transform: expr) => {
|
||||
pub fn $name(&self, other: &ConstInt) -> Option<ConstInt> {
|
||||
Some(match (self, other) {
|
||||
(ConstInt::I8($x), ConstInt::I8($y)) => ConstInt::I8($transform),
|
||||
(ConstInt::I16($x), ConstInt::I16($y)) => ConstInt::I16($transform),
|
||||
(ConstInt::I32($x), ConstInt::I32($y)) => ConstInt::I32($transform),
|
||||
(ConstInt::I64($x), ConstInt::I64($y)) => ConstInt::I64($transform),
|
||||
(ConstInt::I128($x), ConstInt::I128($y)) => ConstInt::I128($transform),
|
||||
(ConstInt::U8($x), ConstInt::U8($y)) => ConstInt::U8($transform),
|
||||
(ConstInt::U16($x), ConstInt::U16($y)) => ConstInt::U16($transform),
|
||||
(ConstInt::U32($x), ConstInt::U32($y)) => ConstInt::U32($transform),
|
||||
(ConstInt::U64($x), ConstInt::U64($y)) => ConstInt::U64($transform),
|
||||
(ConstInt::U128($x), ConstInt::U128($y)) => ConstInt::U128($transform),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[allow(clippy::useless_conversion)]
|
||||
impl ConstInt {
|
||||
const_int_op!(raw_value, String, x, format!("{}", x));
|
||||
|
||||
const_int_map!(value_negate, x, x.checked_neg()?);
|
||||
|
||||
const_int_op!(to_usize, Option<usize>, x, (*x).try_into().ok());
|
||||
|
||||
const_int_op!(to_string, String, x, (*x).to_string());
|
||||
|
||||
const_int_bimap!(value_add, x, y, x.checked_add(*y)?);
|
||||
|
||||
const_int_bimap!(value_sub, x, y, x.checked_sub(*y)?);
|
||||
|
||||
const_int_bimap!(value_mul, x, y, x.checked_mul(*y)?);
|
||||
|
||||
const_int_bimap!(value_div, x, y, x.checked_div(*y)?);
|
||||
|
||||
// TODO: limited to 32 bit exponents
|
||||
const_int_bimap!(value_pow, x, y, x.checked_pow((*y).try_into().ok()?)?);
|
||||
|
||||
const_int_biop!(value_lt, bool, x, y, Some(x < y));
|
||||
|
||||
const_int_biop!(value_le, bool, x, y, Some(x <= y));
|
||||
|
||||
const_int_biop!(value_gt, bool, x, y, Some(x > y));
|
||||
|
||||
const_int_biop!(value_ge, bool, x, y, Some(x >= y));
|
||||
|
||||
pub fn get_int_type(&self) -> IntegerType {
|
||||
match self {
|
||||
ConstInt::I8(_) => IntegerType::I8,
|
||||
ConstInt::I16(_) => IntegerType::I16,
|
||||
ConstInt::I32(_) => IntegerType::I32,
|
||||
ConstInt::I64(_) => IntegerType::I64,
|
||||
ConstInt::I128(_) => IntegerType::I128,
|
||||
ConstInt::U8(_) => IntegerType::U8,
|
||||
ConstInt::U16(_) => IntegerType::U16,
|
||||
ConstInt::U32(_) => IntegerType::U32,
|
||||
ConstInt::U64(_) => IntegerType::U64,
|
||||
ConstInt::U128(_) => IntegerType::U128,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_type(&self) -> Type {
|
||||
Type::Integer(self.get_int_type())
|
||||
}
|
||||
|
||||
pub fn parse(int_type: &IntegerType, value: &str, span: &Span) -> Result<ConstInt, AsgConvertError> {
|
||||
Ok(match int_type {
|
||||
IntegerType::I8 => ConstInt::I8(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::I16 => ConstInt::I16(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::I32 => ConstInt::I32(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::I64 => ConstInt::I64(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::I128 => ConstInt::I128(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::U8 => ConstInt::U8(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::U16 => ConstInt::U16(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::U32 => ConstInt::U32(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::U64 => ConstInt::U64(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
IntegerType::U128 => ConstInt::U128(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ConstValue {
|
||||
pub fn get_type(&self) -> Option<Type> {
|
||||
Some(match self {
|
||||
ConstValue::Int(i) => i.get_type(),
|
||||
ConstValue::Group(_) => Type::Group,
|
||||
ConstValue::Field(_) => Type::Field,
|
||||
ConstValue::Address(_) => Type::Address,
|
||||
ConstValue::Boolean(_) => Type::Boolean,
|
||||
ConstValue::Tuple(sub_consts) => {
|
||||
Type::Tuple(sub_consts.iter().map(|x| x.get_type()).collect::<Option<Vec<Type>>>()?)
|
||||
}
|
||||
ConstValue::Array(values) => Type::Array(Box::new(values.get(0)?.get_type()?), values.len()),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn int(&self) -> Option<&ConstInt> {
|
||||
match self {
|
||||
ConstValue::Int(x) => Some(x),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn field(&self) -> Option<&BigInt> {
|
||||
match self {
|
||||
ConstValue::Field(x) => Some(x),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
274
asg/src/error/mod.rs
Normal file
274
asg/src/error/mod.rs
Normal file
@ -0,0 +1,274 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
//! Errors encountered when attempting to convert to an asg from an ast.
|
||||
|
||||
use crate::Span;
|
||||
use leo_ast::{AstError, Error as FormattedError};
|
||||
use leo_grammar::ParserError;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum AsgConvertError {
|
||||
#[error("{}", _0)]
|
||||
AstError(#[from] AstError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
Error(#[from] FormattedError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ImportError(FormattedError),
|
||||
|
||||
#[error("{}", _0)]
|
||||
InternalError(String),
|
||||
|
||||
#[error("{}", _0)]
|
||||
ParserError(#[from] ParserError),
|
||||
}
|
||||
|
||||
impl AsgConvertError {
|
||||
fn new_from_span(message: String, span: &Span) -> Self {
|
||||
AsgConvertError::Error(FormattedError::new_from_span(message, span.clone()))
|
||||
}
|
||||
|
||||
pub fn unresolved_circuit(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to resolve circuit: '{}'", name), span)
|
||||
}
|
||||
|
||||
pub fn unresolved_import(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to resolve import: '{}'", name), span)
|
||||
}
|
||||
|
||||
pub fn unresolved_circuit_member(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"illegal reference to non-existant member '{}' of circuit '{}'",
|
||||
name, circuit_name
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn missing_circuit_member(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"missing circuit member '{}' for initialization of circuit '{}'",
|
||||
name, circuit_name
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn overridden_circuit_member(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"cannot declare circuit member '{}' more than once for initialization of circuit '{}'",
|
||||
name, circuit_name
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn redefined_circuit_member(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"cannot declare circuit member '{}' multiple times in circuit '{}'",
|
||||
name, circuit_name
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn extra_circuit_member(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"extra circuit member '{}' for initialization of circuit '{}' is not allowed",
|
||||
name, circuit_name
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn illegal_function_assign(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("attempt to assign to function '{}'", name), span)
|
||||
}
|
||||
|
||||
pub fn circuit_variable_call(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!("cannot call variable member '{}' of circuit '{}'", name, circuit_name),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn circuit_static_call_invalid(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"cannot call static function '{}' of circuit '{}' from target",
|
||||
name, circuit_name
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn circuit_member_mut_call_invalid(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"cannot call mutable member function '{}' of circuit '{}' from immutable context",
|
||||
name, circuit_name
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn circuit_member_call_invalid(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"cannot call member function '{}' of circuit '{}' from static context",
|
||||
name, circuit_name
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn circuit_function_ref(circuit_name: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"cannot reference function member '{}' of circuit '{}' as value",
|
||||
name, circuit_name
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn index_into_non_array(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to index into non-array '{}'", name), span)
|
||||
}
|
||||
|
||||
pub fn invalid_assign_index(name: &str, num: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!("failed to index array with invalid integer '{}'[{}]", name, num),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn invalid_backwards_assignment(name: &str, left: usize, right: usize, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"failed to index array range for assignment with left > right '{}'[{}..{}]",
|
||||
name, left, right
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn index_into_non_tuple(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to index into non-tuple '{}'", name), span)
|
||||
}
|
||||
|
||||
pub fn tuple_index_out_of_bounds(index: usize, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("tuple index out of bounds: '{}'", index), span)
|
||||
}
|
||||
|
||||
pub fn unexpected_call_argument_count(expected: usize, got: usize, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!("function call expected {} arguments, got {}", expected, got),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn unresolved_function(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to resolve function: '{}'", name), span)
|
||||
}
|
||||
|
||||
pub fn unresolved_type(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!("failed to resolve type for variable definition '{}'", name),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn unexpected_type(expected: &str, received: Option<&str>, span: &Span) -> Self {
|
||||
// panic!(format!("unexpected type, expected: '{}', received: '{}'", expected, received.unwrap_or("unknown")));
|
||||
Self::new_from_span(
|
||||
format!(
|
||||
"unexpected type, expected: '{}', received: '{}'",
|
||||
expected,
|
||||
received.unwrap_or("unknown")
|
||||
),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn unexpected_nonconst(span: &Span) -> Self {
|
||||
Self::new_from_span("expected const, found non-const value".to_string(), span)
|
||||
}
|
||||
|
||||
pub fn unresolved_reference(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to resolve variable reference '{}'", name), span)
|
||||
}
|
||||
|
||||
pub fn invalid_boolean(value: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to parse boolean value '{}'", value), span)
|
||||
}
|
||||
|
||||
pub fn invalid_int(value: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("failed to parse int value '{}'", value), span)
|
||||
}
|
||||
|
||||
pub fn immutable_assignment(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("illegal assignment to immutable variable '{}'", name), span)
|
||||
}
|
||||
|
||||
pub fn function_missing_return(name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(format!("function '{}' missing return for all paths", name), span)
|
||||
}
|
||||
|
||||
pub fn function_return_validation(name: &str, description: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!("function '{}' failed to validate return path: '{}'", name, description),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn input_ref_needs_type(category: &str, name: &str, span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
format!("could not infer type for input in '{}': '{}'", category, name),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn invalid_self_in_global(span: &Span) -> Self {
|
||||
Self::new_from_span(
|
||||
"cannot have `mut self` or `self` arguments in global functions".to_string(),
|
||||
span,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn parse_index_error() -> Self {
|
||||
AsgConvertError::InternalError("failed to parse index".to_string())
|
||||
}
|
||||
|
||||
pub fn parse_dimension_error() -> Self {
|
||||
AsgConvertError::InternalError("failed to parse dimension".to_string())
|
||||
}
|
||||
|
||||
pub fn reference_self_outside_circuit() -> Self {
|
||||
AsgConvertError::InternalError("referenced self outside of circuit function".to_string())
|
||||
}
|
||||
|
||||
pub fn illegal_ast_structure(details: &str) -> Self {
|
||||
AsgConvertError::InternalError(format!("illegal ast structure: {}", details))
|
||||
}
|
||||
}
|
134
asg/src/expression/array_access.rs
Normal file
134
asg/src/expression/array_access.rs
Normal file
@ -0,0 +1,134 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use leo_ast::IntegerType;
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct ArrayAccessExpression {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub array: Arc<Expression>,
|
||||
pub index: Arc<Expression>,
|
||||
}
|
||||
|
||||
impl Node for ArrayAccessExpression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for ArrayAccessExpression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
self.array.set_parent(Arc::downgrade(expr));
|
||||
self.index.set_parent(Arc::downgrade(expr));
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
match self.array.get_type() {
|
||||
Some(Type::Array(element, _)) => Some(*element),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
self.array.is_mut_ref()
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
let mut array = match self.array.const_value()? {
|
||||
ConstValue::Array(values) => values,
|
||||
_ => return None,
|
||||
};
|
||||
let const_index = match self.index.const_value()? {
|
||||
ConstValue::Int(x) => x.to_usize()?,
|
||||
_ => return None,
|
||||
};
|
||||
if const_index >= array.len() {
|
||||
return None;
|
||||
}
|
||||
Some(array.remove(const_index))
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
self.array.is_consty()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::ArrayAccessExpression> for ArrayAccessExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::ArrayAccessExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<ArrayAccessExpression, AsgConvertError> {
|
||||
let array = Arc::<Expression>::from_ast(
|
||||
scope,
|
||||
&*value.array,
|
||||
Some(PartialType::Array(expected_type.map(Box::new), None)),
|
||||
)?;
|
||||
match array.get_type() {
|
||||
Some(Type::Array(..)) => (),
|
||||
type_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
"array",
|
||||
type_.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let index = Arc::<Expression>::from_ast(
|
||||
scope,
|
||||
&*value.index,
|
||||
Some(PartialType::Integer(None, Some(IntegerType::U32))),
|
||||
)?;
|
||||
|
||||
if !index.is_consty() {
|
||||
return Err(AsgConvertError::unexpected_nonconst(
|
||||
&index.span().cloned().unwrap_or_default(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(ArrayAccessExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
array,
|
||||
index,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::ArrayAccessExpression> for &ArrayAccessExpression {
|
||||
fn into(self) -> leo_ast::ArrayAccessExpression {
|
||||
leo_ast::ArrayAccessExpression {
|
||||
array: Box::new(self.array.as_ref().into()),
|
||||
index: Box::new(self.index.as_ref().into()),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
160
asg/src/expression/array_init.rs
Normal file
160
asg/src/expression/array_init.rs
Normal file
@ -0,0 +1,160 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct ArrayInitExpression {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub element: Arc<Expression>,
|
||||
pub len: usize,
|
||||
}
|
||||
|
||||
impl Node for ArrayInitExpression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for ArrayInitExpression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
self.element.set_parent(Arc::downgrade(expr));
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
Some(Type::Array(Box::new(self.element.get_type()?), self.len))
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
// not implemented due to performance concerns
|
||||
None
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
self.element.is_consty()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::ArrayInitExpression> for ArrayInitExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::ArrayInitExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<ArrayInitExpression, AsgConvertError> {
|
||||
let (mut expected_item, expected_len) = match expected_type {
|
||||
Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims),
|
||||
None => (None, None),
|
||||
Some(type_) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&type_.to_string(),
|
||||
Some("array"),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
};
|
||||
let dimensions = value
|
||||
.dimensions
|
||||
.0
|
||||
.iter()
|
||||
.map(|x| {
|
||||
x.value
|
||||
.parse::<usize>()
|
||||
.map_err(|_| AsgConvertError::parse_dimension_error())
|
||||
})
|
||||
.collect::<Result<Vec<_>, AsgConvertError>>()?;
|
||||
|
||||
let len = *dimensions.get(0).ok_or_else(AsgConvertError::parse_dimension_error)?;
|
||||
if let Some(expected_len) = expected_len {
|
||||
if expected_len != len {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&*format!("array of length {}", expected_len),
|
||||
Some(&*format!("array of length {}", len)),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
for dimension in (&dimensions[1..]).iter().copied() {
|
||||
expected_item = match expected_item {
|
||||
Some(PartialType::Array(item, len)) => {
|
||||
if let Some(len) = len {
|
||||
if len != dimension {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&*format!("array of length {}", dimension),
|
||||
Some(&*format!("array of length {}", len)),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
item.map(|x| *x)
|
||||
}
|
||||
None => None,
|
||||
Some(type_) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
"array",
|
||||
Some(&type_.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut element = Some(Arc::<Expression>::from_ast(scope, &*value.element, expected_item)?);
|
||||
let mut output = None;
|
||||
|
||||
for dimension in dimensions.iter().rev().copied() {
|
||||
output = Some(ArrayInitExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
element: output
|
||||
.map(Expression::ArrayInit)
|
||||
.map(Arc::new)
|
||||
.unwrap_or_else(|| element.take().unwrap()),
|
||||
len: dimension,
|
||||
});
|
||||
}
|
||||
Ok(output.unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::ArrayInitExpression> for &ArrayInitExpression {
|
||||
fn into(self) -> leo_ast::ArrayInitExpression {
|
||||
leo_ast::ArrayInitExpression {
|
||||
element: Box::new(self.element.as_ref().into()),
|
||||
dimensions: leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber {
|
||||
value: self.len.to_string(),
|
||||
}]),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
198
asg/src/expression/array_inline.rs
Normal file
198
asg/src/expression/array_inline.rs
Normal file
@ -0,0 +1,198 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use leo_ast::SpreadOrExpression;
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct ArrayInlineExpression {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub elements: Vec<(Arc<Expression>, bool)>, // bool = if spread
|
||||
}
|
||||
|
||||
impl ArrayInlineExpression {
|
||||
pub fn expanded_length(&self) -> usize {
|
||||
self.elements
|
||||
.iter()
|
||||
.map(|(expr, is_spread)| {
|
||||
if *is_spread {
|
||||
match expr.get_type() {
|
||||
Some(Type::Array(_item, len)) => len,
|
||||
_ => 0,
|
||||
}
|
||||
} else {
|
||||
1
|
||||
}
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
}
|
||||
|
||||
impl Node for ArrayInlineExpression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for ArrayInlineExpression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
self.elements.iter().for_each(|(element, _)| {
|
||||
element.set_parent(Arc::downgrade(expr));
|
||||
})
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
Some(Type::Array(
|
||||
Box::new(self.elements.first()?.0.get_type()?),
|
||||
self.expanded_length(),
|
||||
))
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
let mut const_values = vec![];
|
||||
for (expr, spread) in self.elements.iter() {
|
||||
if *spread {
|
||||
match expr.const_value()? {
|
||||
ConstValue::Array(items) => const_values.extend(items),
|
||||
_ => return None,
|
||||
}
|
||||
} else {
|
||||
const_values.push(expr.const_value()?);
|
||||
}
|
||||
}
|
||||
Some(ConstValue::Array(const_values))
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
self.elements.iter().all(|x| x.0.is_consty())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::ArrayInlineExpression> for ArrayInlineExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::ArrayInlineExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<ArrayInlineExpression, AsgConvertError> {
|
||||
let (mut expected_item, expected_len) = match expected_type {
|
||||
Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims),
|
||||
None => (None, None),
|
||||
Some(type_) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&type_.to_string(),
|
||||
Some("array"),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let mut len = 0;
|
||||
let output = ArrayInlineExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
elements: value
|
||||
.elements
|
||||
.iter()
|
||||
.map(|e| match e {
|
||||
SpreadOrExpression::Expression(e) => {
|
||||
let expr = Arc::<Expression>::from_ast(scope, e, expected_item.clone())?;
|
||||
if expected_item.is_none() {
|
||||
expected_item = expr.get_type().map(Type::partial);
|
||||
}
|
||||
len += 1;
|
||||
Ok((expr, false))
|
||||
}
|
||||
SpreadOrExpression::Spread(e) => {
|
||||
let expr = Arc::<Expression>::from_ast(
|
||||
scope,
|
||||
e,
|
||||
Some(PartialType::Array(expected_item.clone().map(Box::new), None)),
|
||||
)?;
|
||||
|
||||
match expr.get_type() {
|
||||
Some(Type::Array(item, spread_len)) => {
|
||||
if expected_item.is_none() {
|
||||
expected_item = Some((*item).partial());
|
||||
}
|
||||
|
||||
len += spread_len;
|
||||
}
|
||||
type_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
expected_item
|
||||
.as_ref()
|
||||
.map(|x| x.to_string())
|
||||
.as_deref()
|
||||
.unwrap_or("unknown"),
|
||||
type_.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok((expr, true))
|
||||
}
|
||||
})
|
||||
.collect::<Result<Vec<_>, AsgConvertError>>()?,
|
||||
};
|
||||
if let Some(expected_len) = expected_len {
|
||||
if len != expected_len {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&*format!("array of length {}", expected_len),
|
||||
Some(&*format!("array of length {}", len)),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::ArrayInlineExpression> for &ArrayInlineExpression {
|
||||
fn into(self) -> leo_ast::ArrayInlineExpression {
|
||||
leo_ast::ArrayInlineExpression {
|
||||
elements: self
|
||||
.elements
|
||||
.iter()
|
||||
.map(|(element, spread)| {
|
||||
let element = element.as_ref().into();
|
||||
if *spread {
|
||||
SpreadOrExpression::Spread(element)
|
||||
} else {
|
||||
SpreadOrExpression::Expression(element)
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
189
asg/src/expression/array_range_access.rs
Normal file
189
asg/src/expression/array_range_access.rs
Normal file
@ -0,0 +1,189 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
use leo_ast::IntegerType;
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct ArrayRangeAccessExpression {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub array: Arc<Expression>,
|
||||
pub left: Option<Arc<Expression>>,
|
||||
pub right: Option<Arc<Expression>>,
|
||||
}
|
||||
|
||||
impl Node for ArrayRangeAccessExpression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for ArrayRangeAccessExpression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
self.array.set_parent(Arc::downgrade(expr));
|
||||
self.array.enforce_parents(&self.array);
|
||||
if let Some(left) = self.left.as_ref() {
|
||||
left.set_parent(Arc::downgrade(expr));
|
||||
}
|
||||
if let Some(right) = self.right.as_ref() {
|
||||
right.set_parent(Arc::downgrade(expr));
|
||||
}
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
let (element, array_len) = match self.array.get_type() {
|
||||
Some(Type::Array(element, len)) => (element, len),
|
||||
_ => return None,
|
||||
};
|
||||
let const_left = match self.left.as_ref().map(|x| x.const_value()) {
|
||||
Some(Some(ConstValue::Int(x))) => x.to_usize()?,
|
||||
None => 0,
|
||||
_ => return None,
|
||||
};
|
||||
let const_right = match self.right.as_ref().map(|x| x.const_value()) {
|
||||
Some(Some(ConstValue::Int(x))) => x.to_usize()?,
|
||||
None => array_len,
|
||||
_ => return None,
|
||||
};
|
||||
if const_left > const_right || const_right > array_len {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Type::Array(element, const_right - const_left))
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
self.array.is_mut_ref()
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
let mut array = match self.array.const_value()? {
|
||||
ConstValue::Array(values) => values,
|
||||
_ => return None,
|
||||
};
|
||||
let const_left = match self.left.as_ref().map(|x| x.const_value()) {
|
||||
Some(Some(ConstValue::Int(x))) => x.to_usize()?,
|
||||
None => 0,
|
||||
_ => return None,
|
||||
};
|
||||
let const_right = match self.right.as_ref().map(|x| x.const_value()) {
|
||||
Some(Some(ConstValue::Int(x))) => x.to_usize()?,
|
||||
None => array.len(),
|
||||
_ => return None,
|
||||
};
|
||||
if const_left > const_right || const_right as usize > array.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(ConstValue::Array(array.drain(const_left..const_right).collect()))
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
self.array.is_consty()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::ArrayRangeAccessExpression> for ArrayRangeAccessExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::ArrayRangeAccessExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<ArrayRangeAccessExpression, AsgConvertError> {
|
||||
let expected_array = match expected_type {
|
||||
Some(PartialType::Array(element, _len)) => Some(PartialType::Array(element, None)),
|
||||
None => None,
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some("array"),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
};
|
||||
let array = Arc::<Expression>::from_ast(scope, &*value.array, expected_array)?;
|
||||
let array_type = array.get_type();
|
||||
match array_type {
|
||||
Some(Type::Array(_, _)) => (),
|
||||
type_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
"array",
|
||||
type_.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
let left = value
|
||||
.left
|
||||
.as_deref()
|
||||
.map(|left| {
|
||||
Arc::<Expression>::from_ast(scope, left, Some(PartialType::Integer(None, Some(IntegerType::U32))))
|
||||
})
|
||||
.transpose()?;
|
||||
let right = value
|
||||
.right
|
||||
.as_deref()
|
||||
.map(|right| {
|
||||
Arc::<Expression>::from_ast(scope, right, Some(PartialType::Integer(None, Some(IntegerType::U32))))
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
if let Some(left) = left.as_ref() {
|
||||
if !left.is_consty() {
|
||||
return Err(AsgConvertError::unexpected_nonconst(
|
||||
&left.span().cloned().unwrap_or_default(),
|
||||
));
|
||||
}
|
||||
}
|
||||
if let Some(right) = right.as_ref() {
|
||||
if !right.is_consty() {
|
||||
return Err(AsgConvertError::unexpected_nonconst(
|
||||
&right.span().cloned().unwrap_or_default(),
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(ArrayRangeAccessExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
array,
|
||||
left,
|
||||
right,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::ArrayRangeAccessExpression> for &ArrayRangeAccessExpression {
|
||||
fn into(self) -> leo_ast::ArrayRangeAccessExpression {
|
||||
leo_ast::ArrayRangeAccessExpression {
|
||||
array: Box::new(self.array.as_ref().into()),
|
||||
left: self.left.as_ref().map(|left| Box::new(left.as_ref().into())),
|
||||
right: self.right.as_ref().map(|right| Box::new(right.as_ref().into())),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
262
asg/src/expression/binary.rs
Normal file
262
asg/src/expression/binary.rs
Normal file
@ -0,0 +1,262 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
pub use leo_ast::{BinaryOperation, BinaryOperationClass};
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct BinaryExpression {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub operation: BinaryOperation,
|
||||
pub left: Arc<Expression>,
|
||||
pub right: Arc<Expression>,
|
||||
}
|
||||
|
||||
impl Node for BinaryExpression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for BinaryExpression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
self.left.set_parent(Arc::downgrade(expr));
|
||||
self.right.set_parent(Arc::downgrade(expr));
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
match self.operation.class() {
|
||||
BinaryOperationClass::Boolean => Some(Type::Boolean),
|
||||
BinaryOperationClass::Numeric => self.left.get_type(),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
use BinaryOperation::*;
|
||||
let left = self.left.const_value()?;
|
||||
let right = self.right.const_value()?;
|
||||
|
||||
match (left, right) {
|
||||
(ConstValue::Int(left), ConstValue::Int(right)) => Some(match self.operation {
|
||||
Add => ConstValue::Int(left.value_add(&right)?),
|
||||
Sub => ConstValue::Int(left.value_sub(&right)?),
|
||||
Mul => ConstValue::Int(left.value_mul(&right)?),
|
||||
Div => ConstValue::Int(left.value_div(&right)?),
|
||||
Pow => ConstValue::Int(left.value_pow(&right)?),
|
||||
Eq => ConstValue::Boolean(left == right),
|
||||
Ne => ConstValue::Boolean(left != right),
|
||||
Ge => ConstValue::Boolean(left.value_ge(&right)?),
|
||||
Gt => ConstValue::Boolean(left.value_gt(&right)?),
|
||||
Le => ConstValue::Boolean(left.value_le(&right)?),
|
||||
Lt => ConstValue::Boolean(left.value_lt(&right)?),
|
||||
_ => return None,
|
||||
}),
|
||||
// (ConstValue::Field(left), ConstValue::Field(right)) => {
|
||||
// Some(match self.operation {
|
||||
// Add => ConstValue::Field(left.checked_add(&right)?),
|
||||
// Sub => ConstValue::Field(left.checked_sub(&right)?),
|
||||
// Mul => ConstValue::Field(left.checked_mul(&right)?),
|
||||
// Div => ConstValue::Field(left.checked_div(&right)?),
|
||||
// Eq => ConstValue::Boolean(left == right),
|
||||
// Ne => ConstValue::Boolean(left != right),
|
||||
// _ => return None,
|
||||
// })
|
||||
// },
|
||||
(ConstValue::Boolean(left), ConstValue::Boolean(right)) => Some(match self.operation {
|
||||
Eq => ConstValue::Boolean(left == right),
|
||||
Ne => ConstValue::Boolean(left != right),
|
||||
And => ConstValue::Boolean(left && right),
|
||||
Or => ConstValue::Boolean(left || right),
|
||||
_ => return None,
|
||||
}),
|
||||
//todo: group?
|
||||
(left, right) => Some(match self.operation {
|
||||
Eq => ConstValue::Boolean(left == right),
|
||||
Ne => ConstValue::Boolean(left != right),
|
||||
_ => return None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
self.left.is_consty() && self.right.is_consty()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::BinaryExpression> for BinaryExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::BinaryExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<BinaryExpression, AsgConvertError> {
|
||||
let class = value.op.class();
|
||||
let expected_type = match class {
|
||||
BinaryOperationClass::Boolean => match expected_type {
|
||||
Some(PartialType::Type(Type::Boolean)) | None => None,
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*Type::Boolean.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
},
|
||||
BinaryOperationClass::Numeric => match expected_type {
|
||||
Some(x @ PartialType::Integer(_, _)) => Some(x),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some("integer"),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
None => None,
|
||||
},
|
||||
};
|
||||
|
||||
// left
|
||||
let (left, right) = match Arc::<Expression>::from_ast(scope, &*value.left, expected_type.clone()) {
|
||||
Ok(left) => {
|
||||
if let Some(left_type) = left.get_type() {
|
||||
let right = Arc::<Expression>::from_ast(scope, &*value.right, Some(left_type.partial()))?;
|
||||
(left, right)
|
||||
} else {
|
||||
let right = Arc::<Expression>::from_ast(scope, &*value.right, expected_type)?;
|
||||
if let Some(right_type) = right.get_type() {
|
||||
(
|
||||
Arc::<Expression>::from_ast(scope, &*value.left, Some(right_type.partial()))?,
|
||||
right,
|
||||
)
|
||||
} else {
|
||||
(left, right)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
let right = Arc::<Expression>::from_ast(scope, &*value.right, expected_type)?;
|
||||
if let Some(right_type) = right.get_type() {
|
||||
(
|
||||
Arc::<Expression>::from_ast(scope, &*value.left, Some(right_type.partial()))?,
|
||||
right,
|
||||
)
|
||||
} else {
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let left_type = left.get_type();
|
||||
#[allow(clippy::unused_unit)]
|
||||
match class {
|
||||
BinaryOperationClass::Numeric => match left_type {
|
||||
Some(Type::Integer(_)) => (),
|
||||
Some(Type::Group) | Some(Type::Field)
|
||||
if value.op == BinaryOperation::Add || value.op == BinaryOperation::Sub =>
|
||||
{
|
||||
()
|
||||
}
|
||||
Some(Type::Field) if value.op == BinaryOperation::Mul || value.op == BinaryOperation::Div => (),
|
||||
type_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
"integer",
|
||||
type_.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
},
|
||||
BinaryOperationClass::Boolean => match &value.op {
|
||||
BinaryOperation::And | BinaryOperation::Or => match left_type {
|
||||
Some(Type::Boolean) | None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*Type::Boolean.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
},
|
||||
BinaryOperation::Eq | BinaryOperation::Ne => (), // all types allowed
|
||||
_ => match left_type {
|
||||
Some(Type::Integer(_)) | None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some("integer"),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
let right_type = right.get_type();
|
||||
|
||||
match (left_type, right_type) {
|
||||
(Some(left_type), Some(right_type)) => {
|
||||
if !left_type.is_assignable_from(&right_type) {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&left_type.to_string(),
|
||||
Some(&*right_type.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
(None, None) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
"any type",
|
||||
Some("unknown type"),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
(_, _) => (),
|
||||
}
|
||||
Ok(BinaryExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
operation: value.op.clone(),
|
||||
left,
|
||||
right,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::BinaryExpression> for &BinaryExpression {
|
||||
fn into(self) -> leo_ast::BinaryExpression {
|
||||
leo_ast::BinaryExpression {
|
||||
op: self.operation.clone(),
|
||||
left: Box::new(self.left.as_ref().into()),
|
||||
right: Box::new(self.right.as_ref().into()),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
244
asg/src/expression/call.rs
Normal file
244
asg/src/expression/call.rs
Normal file
@ -0,0 +1,244 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{
|
||||
AsgConvertError,
|
||||
CircuitMember,
|
||||
ConstValue,
|
||||
Expression,
|
||||
ExpressionNode,
|
||||
FromAst,
|
||||
Function,
|
||||
FunctionQualifier,
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Type,
|
||||
};
|
||||
pub use leo_ast::BinaryOperation;
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct CallExpression {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub function: Arc<Function>,
|
||||
pub target: Option<Arc<Expression>>,
|
||||
pub arguments: Vec<Arc<Expression>>,
|
||||
}
|
||||
|
||||
impl Node for CallExpression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for CallExpression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
if let Some(target) = self.target.as_ref() {
|
||||
target.set_parent(Arc::downgrade(expr));
|
||||
}
|
||||
self.arguments.iter().for_each(|element| {
|
||||
element.set_parent(Arc::downgrade(expr));
|
||||
})
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
Some(self.function.output.clone().into())
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
// static function const evaluation
|
||||
None
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::CallExpression> for CallExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::CallExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<CallExpression, AsgConvertError> {
|
||||
let (target, function) = match &*value.function {
|
||||
leo_ast::Expression::Identifier(name) => (
|
||||
None,
|
||||
scope
|
||||
.borrow()
|
||||
.resolve_function(&name.name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_function(&name.name, &name.span))?,
|
||||
),
|
||||
leo_ast::Expression::CircuitMemberAccess(leo_ast::CircuitMemberAccessExpression {
|
||||
circuit: ast_circuit,
|
||||
name,
|
||||
span,
|
||||
}) => {
|
||||
let target = Arc::<Expression>::from_ast(scope, &**ast_circuit, None)?;
|
||||
let circuit = match target.get_type() {
|
||||
Some(Type::Circuit(circuit)) => circuit,
|
||||
type_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
"circuit",
|
||||
type_.map(|x| x.to_string()).as_deref(),
|
||||
&span,
|
||||
));
|
||||
}
|
||||
};
|
||||
let circuit_name = circuit.name.borrow().name.clone();
|
||||
let member = circuit.members.borrow();
|
||||
let member = member
|
||||
.get(&name.name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, &span))?;
|
||||
match member {
|
||||
CircuitMember::Function(body) => {
|
||||
if body.qualifier == FunctionQualifier::Static {
|
||||
return Err(AsgConvertError::circuit_static_call_invalid(
|
||||
&circuit_name,
|
||||
&name.name,
|
||||
&span,
|
||||
));
|
||||
} else if body.qualifier == FunctionQualifier::MutSelfRef && !target.is_mut_ref() {
|
||||
return Err(AsgConvertError::circuit_member_mut_call_invalid(
|
||||
&circuit_name,
|
||||
&name.name,
|
||||
&span,
|
||||
));
|
||||
}
|
||||
(Some(target), body.clone())
|
||||
}
|
||||
CircuitMember::Variable(_) => {
|
||||
return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span));
|
||||
}
|
||||
}
|
||||
}
|
||||
leo_ast::Expression::CircuitStaticFunctionAccess(leo_ast::CircuitStaticFunctionAccessExpression {
|
||||
circuit: ast_circuit,
|
||||
name,
|
||||
span,
|
||||
}) => {
|
||||
let circuit = if let leo_ast::Expression::Identifier(circuit_name) = &**ast_circuit {
|
||||
scope
|
||||
.borrow()
|
||||
.resolve_circuit(&circuit_name.name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_circuit(&circuit_name.name, &circuit_name.span))?
|
||||
} else {
|
||||
return Err(AsgConvertError::unexpected_type("circuit", None, &span));
|
||||
};
|
||||
let circuit_name = circuit.name.borrow().name.clone();
|
||||
|
||||
let member = circuit.members.borrow();
|
||||
let member = member
|
||||
.get(&name.name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_circuit_member(&circuit_name, &name.name, &span))?;
|
||||
match member {
|
||||
CircuitMember::Function(body) => {
|
||||
if body.qualifier != FunctionQualifier::Static {
|
||||
return Err(AsgConvertError::circuit_member_call_invalid(
|
||||
&circuit_name,
|
||||
&name.name,
|
||||
&span,
|
||||
));
|
||||
}
|
||||
(None, body.clone())
|
||||
}
|
||||
CircuitMember::Variable(_) => {
|
||||
return Err(AsgConvertError::circuit_variable_call(&circuit_name, &name.name, &span));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(AsgConvertError::illegal_ast_structure(
|
||||
"non Identifier/CircuitMemberAccess/CircuitStaticFunctionAccess as call target",
|
||||
));
|
||||
}
|
||||
};
|
||||
if let Some(expected) = expected_type {
|
||||
let output: Type = function.output.clone().into();
|
||||
if !expected.matches(&output) {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&expected.to_string(),
|
||||
Some(&*output.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
if value.arguments.len() != function.argument_types.len() {
|
||||
return Err(AsgConvertError::unexpected_call_argument_count(
|
||||
function.argument_types.len(),
|
||||
value.arguments.len(),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(CallExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
arguments: value
|
||||
.arguments
|
||||
.iter()
|
||||
.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 {
|
||||
fn into(self) -> leo_ast::CallExpression {
|
||||
let target_function = if let Some(target) = &self.target {
|
||||
target.as_ref().into()
|
||||
} else {
|
||||
let circuit = self.function.circuit.borrow().as_ref().map(|x| x.upgrade()).flatten();
|
||||
if let Some(circuit) = circuit {
|
||||
leo_ast::Expression::CircuitStaticFunctionAccess(leo_ast::CircuitStaticFunctionAccessExpression {
|
||||
circuit: Box::new(leo_ast::Expression::Identifier(circuit.name.borrow().clone())),
|
||||
name: self.function.name.borrow().clone(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
})
|
||||
} else {
|
||||
leo_ast::Expression::Identifier(self.function.name.borrow().clone())
|
||||
}
|
||||
};
|
||||
leo_ast::CallExpression {
|
||||
function: Box::new(target_function),
|
||||
arguments: self.arguments.iter().map(|arg| arg.as_ref().into()).collect(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
256
asg/src/expression/circuit_access.rs
Normal file
256
asg/src/expression/circuit_access.rs
Normal file
@ -0,0 +1,256 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
// Copyright (C) 2019-2020 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 crate::{
|
||||
AsgConvertError,
|
||||
Circuit,
|
||||
CircuitMember,
|
||||
CircuitMemberBody,
|
||||
ConstValue,
|
||||
Expression,
|
||||
ExpressionNode,
|
||||
FromAst,
|
||||
Identifier,
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Type,
|
||||
};
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct CircuitAccessExpression {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub circuit: Arc<Circuit>,
|
||||
pub target: Option<Arc<Expression>>,
|
||||
pub member: Identifier,
|
||||
}
|
||||
|
||||
impl Node for CircuitAccessExpression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for CircuitAccessExpression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
if let Some(target) = self.target.as_ref() {
|
||||
target.set_parent(Arc::downgrade(expr));
|
||||
}
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
if self.target.is_none() {
|
||||
None // function target only for static
|
||||
} else {
|
||||
let members = self.circuit.members.borrow();
|
||||
let member = members.get(&self.member.name)?;
|
||||
match member {
|
||||
CircuitMember::Variable(type_) => Some(type_.clone().into()),
|
||||
CircuitMember::Function(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
if let Some(target) = self.target.as_ref() {
|
||||
target.is_mut_ref()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
None
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
self.target.as_ref().map(|x| x.is_consty()).unwrap_or(true)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::CircuitMemberAccessExpression> for CircuitAccessExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::CircuitMemberAccessExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<CircuitAccessExpression, AsgConvertError> {
|
||||
let target = Arc::<Expression>::from_ast(scope, &*value.circuit, None)?;
|
||||
let circuit = match target.get_type() {
|
||||
Some(Type::Circuit(circuit)) => circuit,
|
||||
x => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
"circuit",
|
||||
x.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
// scoping refcell reference
|
||||
let found_member = {
|
||||
if let Some(member) = circuit.members.borrow().get(&value.name.name) {
|
||||
if let Some(expected_type) = &expected_type {
|
||||
if let CircuitMember::Variable(type_) = &member {
|
||||
let type_: Type = type_.clone().into();
|
||||
if !expected_type.matches(&type_) {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&expected_type.to_string(),
|
||||
Some(&type_.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
} // used by call expression
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
if found_member {
|
||||
// skip
|
||||
} else if circuit.is_input_pseudo_circuit() {
|
||||
// add new member to implicit input
|
||||
if let Some(expected_type) = expected_type.map(PartialType::full).flatten() {
|
||||
circuit.members.borrow_mut().insert(
|
||||
value.name.name.clone(),
|
||||
CircuitMember::Variable(expected_type.clone().into()),
|
||||
);
|
||||
let body = circuit.body.borrow().upgrade().expect("stale input circuit body");
|
||||
|
||||
body.members
|
||||
.borrow_mut()
|
||||
.insert(value.name.name.clone(), CircuitMemberBody::Variable(expected_type));
|
||||
} else {
|
||||
return Err(AsgConvertError::input_ref_needs_type(
|
||||
&circuit.name.borrow().name,
|
||||
&value.name.name,
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
} else {
|
||||
return Err(AsgConvertError::unresolved_circuit_member(
|
||||
&circuit.name.borrow().name,
|
||||
&value.name.name,
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(CircuitAccessExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
target: Some(target),
|
||||
circuit,
|
||||
member: value.name.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::CircuitStaticFunctionAccessExpression> for CircuitAccessExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::CircuitStaticFunctionAccessExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<CircuitAccessExpression, AsgConvertError> {
|
||||
let circuit = match &*value.circuit {
|
||||
leo_ast::Expression::Identifier(name) => scope
|
||||
.borrow()
|
||||
.resolve_circuit(&name.name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_circuit(&name.name, &name.span))?,
|
||||
_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
"circuit",
|
||||
Some("unknown"),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(expected_type) = expected_type {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&expected_type.to_string(),
|
||||
Some("none"),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
|
||||
if let Some(CircuitMember::Function(_)) = circuit.members.borrow().get(&value.name.name) {
|
||||
// okay
|
||||
} else {
|
||||
return Err(AsgConvertError::unresolved_circuit_member(
|
||||
&circuit.name.borrow().name,
|
||||
&value.name.name,
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(CircuitAccessExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
target: None,
|
||||
circuit,
|
||||
member: value.name.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::Expression> for &CircuitAccessExpression {
|
||||
fn into(self) -> leo_ast::Expression {
|
||||
if let Some(target) = self.target.as_ref() {
|
||||
leo_ast::Expression::CircuitMemberAccess(leo_ast::CircuitMemberAccessExpression {
|
||||
circuit: Box::new(target.as_ref().into()),
|
||||
name: self.member.clone(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
})
|
||||
} else {
|
||||
leo_ast::Expression::CircuitStaticFunctionAccess(leo_ast::CircuitStaticFunctionAccessExpression {
|
||||
circuit: Box::new(leo_ast::Expression::Identifier(self.circuit.name.borrow().clone())),
|
||||
name: self.member.clone(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
177
asg/src/expression/circuit_init.rs
Normal file
177
asg/src/expression/circuit_init.rs
Normal file
@ -0,0 +1,177 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{
|
||||
AsgConvertError,
|
||||
Circuit,
|
||||
CircuitMember,
|
||||
ConstValue,
|
||||
Expression,
|
||||
ExpressionNode,
|
||||
FromAst,
|
||||
Identifier,
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Type,
|
||||
};
|
||||
|
||||
use indexmap::{IndexMap, IndexSet};
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct CircuitInitExpression {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub circuit: Arc<Circuit>,
|
||||
pub values: Vec<(Identifier, Arc<Expression>)>,
|
||||
}
|
||||
|
||||
impl Node for CircuitInitExpression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for CircuitInitExpression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
self.values.iter().for_each(|(_, element)| {
|
||||
element.set_parent(Arc::downgrade(expr));
|
||||
})
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
Some(Type::Circuit(self.circuit.clone()))
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
None
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
self.values.iter().all(|(_, value)| value.is_consty())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::CircuitInitExpression> for CircuitInitExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::CircuitInitExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<CircuitInitExpression, AsgConvertError> {
|
||||
let circuit = scope
|
||||
.borrow()
|
||||
.resolve_circuit(&value.name.name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_circuit(&value.name.name, &value.name.span))?;
|
||||
match expected_type {
|
||||
Some(PartialType::Type(Type::Circuit(expected_circuit))) if expected_circuit == circuit => (),
|
||||
None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&circuit.name.borrow().name),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
let members: IndexMap<&String, (&Identifier, &leo_ast::Expression)> = value
|
||||
.members
|
||||
.iter()
|
||||
.map(|x| (&x.identifier.name, (&x.identifier, &x.expression)))
|
||||
.collect();
|
||||
|
||||
let mut values: Vec<(Identifier, Arc<Expression>)> = vec![];
|
||||
let mut defined_variables = IndexSet::<String>::new();
|
||||
|
||||
{
|
||||
let circuit_members = circuit.members.borrow();
|
||||
for (name, member) in circuit_members.iter() {
|
||||
if defined_variables.contains(name) {
|
||||
return Err(AsgConvertError::overridden_circuit_member(
|
||||
&circuit.name.borrow().name,
|
||||
name,
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
defined_variables.insert(name.clone());
|
||||
let type_: Type = if let CircuitMember::Variable(type_) = &member {
|
||||
type_.clone().into()
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
if let Some((identifier, receiver)) = members.get(&name) {
|
||||
let received = Arc::<Expression>::from_ast(scope, *receiver, Some(type_.partial()))?;
|
||||
values.push(((*identifier).clone(), received));
|
||||
} else {
|
||||
return Err(AsgConvertError::missing_circuit_member(
|
||||
&circuit.name.borrow().name,
|
||||
name,
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
for (name, (identifier, _expression)) in members.iter() {
|
||||
if circuit_members.get(*name).is_none() {
|
||||
return Err(AsgConvertError::extra_circuit_member(
|
||||
&circuit.name.borrow().name,
|
||||
*name,
|
||||
&identifier.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(CircuitInitExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
circuit,
|
||||
values,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::CircuitInitExpression> for &CircuitInitExpression {
|
||||
fn into(self) -> leo_ast::CircuitInitExpression {
|
||||
leo_ast::CircuitInitExpression {
|
||||
name: self.circuit.name.borrow().clone(),
|
||||
members: self
|
||||
.values
|
||||
.iter()
|
||||
.map(|(name, value)| leo_ast::CircuitVariableDefinition {
|
||||
identifier: name.clone(),
|
||||
expression: value.as_ref().into(),
|
||||
})
|
||||
.collect(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
237
asg/src/expression/constant.rs
Normal file
237
asg/src/expression/constant.rs
Normal file
@ -0,0 +1,237 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{
|
||||
AsgConvertError,
|
||||
ConstInt,
|
||||
ConstValue,
|
||||
Expression,
|
||||
ExpressionNode,
|
||||
FromAst,
|
||||
GroupValue,
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Type,
|
||||
};
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct Constant {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub value: ConstValue, // should not be compound constants
|
||||
}
|
||||
|
||||
impl Node for Constant {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for Constant {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, _expr: &Arc<Expression>) {}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
self.value.get_type()
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
Some(self.value.clone())
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::ValueExpression> for Constant {
|
||||
fn from_ast(
|
||||
_scope: &Scope,
|
||||
value: &leo_ast::ValueExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<Constant, AsgConvertError> {
|
||||
use leo_ast::ValueExpression::*;
|
||||
Ok(match value {
|
||||
Address(value, span) => {
|
||||
match expected_type.map(PartialType::full).flatten() {
|
||||
Some(Type::Address) | None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*Type::Address.to_string()),
|
||||
span,
|
||||
));
|
||||
}
|
||||
}
|
||||
Constant {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(span.clone()),
|
||||
value: ConstValue::Address(value.clone()),
|
||||
}
|
||||
}
|
||||
Boolean(value, span) => {
|
||||
match expected_type.map(PartialType::full).flatten() {
|
||||
Some(Type::Boolean) | None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*Type::Boolean.to_string()),
|
||||
span,
|
||||
));
|
||||
}
|
||||
}
|
||||
Constant {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(span.clone()),
|
||||
value: ConstValue::Boolean(
|
||||
value
|
||||
.parse::<bool>()
|
||||
.map_err(|_| AsgConvertError::invalid_boolean(&value, span))?,
|
||||
),
|
||||
}
|
||||
}
|
||||
Field(value, span) => {
|
||||
match expected_type.map(PartialType::full).flatten() {
|
||||
Some(Type::Field) | None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*Type::Field.to_string()),
|
||||
span,
|
||||
));
|
||||
}
|
||||
}
|
||||
Constant {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(span.clone()),
|
||||
value: ConstValue::Field(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
}
|
||||
}
|
||||
Group(value) => {
|
||||
match expected_type.map(PartialType::full).flatten() {
|
||||
Some(Type::Group) | None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*Type::Group.to_string()),
|
||||
value.span(),
|
||||
));
|
||||
}
|
||||
}
|
||||
Constant {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span().clone()),
|
||||
value: ConstValue::Group(match &**value {
|
||||
leo_ast::GroupValue::Single(value, _) => GroupValue::Single(value.clone()),
|
||||
leo_ast::GroupValue::Tuple(leo_ast::GroupTuple { x, y, .. }) => {
|
||||
GroupValue::Tuple(x.into(), y.into())
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
Implicit(value, span) => match expected_type {
|
||||
None => return Err(AsgConvertError::unresolved_type("unknown", span)),
|
||||
Some(PartialType::Integer(Some(sub_type), _)) | Some(PartialType::Integer(None, Some(sub_type))) => {
|
||||
Constant {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(span.clone()),
|
||||
value: ConstValue::Int(ConstInt::parse(&sub_type, value, span)?),
|
||||
}
|
||||
}
|
||||
Some(PartialType::Type(Type::Field)) => Constant {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(span.clone()),
|
||||
value: ConstValue::Field(value.parse().map_err(|_| AsgConvertError::invalid_int(&value, span))?),
|
||||
},
|
||||
Some(PartialType::Type(Type::Address)) => Constant {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(span.clone()),
|
||||
value: ConstValue::Address(value.to_string()),
|
||||
},
|
||||
Some(x) => return Err(AsgConvertError::unexpected_type(&x.to_string(), Some("unknown"), span)),
|
||||
},
|
||||
Integer(int_type, value, span) => {
|
||||
match expected_type {
|
||||
Some(PartialType::Integer(Some(sub_type), _)) if &sub_type == int_type => (),
|
||||
Some(PartialType::Integer(None, Some(_))) => (),
|
||||
None => (),
|
||||
Some(x) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&x.to_string(),
|
||||
Some(&*int_type.to_string()),
|
||||
span,
|
||||
));
|
||||
}
|
||||
}
|
||||
Constant {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(span.clone()),
|
||||
value: ConstValue::Int(ConstInt::parse(int_type, value, span)?),
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::ValueExpression> for &Constant {
|
||||
fn into(self) -> leo_ast::ValueExpression {
|
||||
match &self.value {
|
||||
ConstValue::Address(value) => {
|
||||
leo_ast::ValueExpression::Address(value.clone(), self.span.clone().unwrap_or_default())
|
||||
}
|
||||
ConstValue::Boolean(value) => {
|
||||
leo_ast::ValueExpression::Boolean(value.to_string(), self.span.clone().unwrap_or_default())
|
||||
}
|
||||
ConstValue::Field(value) => {
|
||||
leo_ast::ValueExpression::Field(value.to_string(), self.span.clone().unwrap_or_default())
|
||||
}
|
||||
ConstValue::Group(value) => leo_ast::ValueExpression::Group(Box::new(match value {
|
||||
GroupValue::Single(single) => {
|
||||
leo_ast::GroupValue::Single(single.clone(), self.span.clone().unwrap_or_default())
|
||||
}
|
||||
GroupValue::Tuple(left, right) => leo_ast::GroupValue::Tuple(leo_ast::GroupTuple {
|
||||
x: left.into(),
|
||||
y: right.into(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}),
|
||||
})),
|
||||
ConstValue::Int(int) => leo_ast::ValueExpression::Integer(
|
||||
int.get_int_type(),
|
||||
int.raw_value(),
|
||||
self.span.clone().unwrap_or_default(),
|
||||
),
|
||||
ConstValue::Tuple(_) => unimplemented!(),
|
||||
ConstValue::Array(_) => unimplemented!(),
|
||||
}
|
||||
}
|
||||
}
|
343
asg/src/expression/mod.rs
Normal file
343
asg/src/expression/mod.rs
Normal file
@ -0,0 +1,343 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
//! This module defines an expression node in an asg.
|
||||
//!
|
||||
//! Notable differences after conversion from an ast expression include:
|
||||
//! 1. Storing variable references instead of variable identifiers - better history tracking and mutability
|
||||
//! 2. Resolving constant values - optimizes execution of program circuit.
|
||||
|
||||
mod array_access;
|
||||
pub use array_access::*;
|
||||
|
||||
mod array_inline;
|
||||
pub use array_inline::*;
|
||||
|
||||
mod array_init;
|
||||
pub use array_init::*;
|
||||
|
||||
mod array_range_access;
|
||||
pub use array_range_access::*;
|
||||
|
||||
mod binary;
|
||||
pub use binary::*;
|
||||
|
||||
mod call;
|
||||
pub use call::*;
|
||||
|
||||
mod circuit_access;
|
||||
pub use circuit_access::*;
|
||||
|
||||
mod circuit_init;
|
||||
pub use circuit_init::*;
|
||||
|
||||
mod constant;
|
||||
pub use constant::*;
|
||||
|
||||
mod ternary;
|
||||
pub use ternary::*;
|
||||
|
||||
mod tuple_access;
|
||||
pub use tuple_access::*;
|
||||
|
||||
mod tuple_init;
|
||||
pub use tuple_init::*;
|
||||
|
||||
mod unary;
|
||||
pub use unary::*;
|
||||
|
||||
mod variable_ref;
|
||||
pub use variable_ref::*;
|
||||
|
||||
use crate::{AsgConvertError, ConstValue, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
pub enum Expression {
|
||||
VariableRef(VariableRef),
|
||||
Constant(Constant),
|
||||
Binary(BinaryExpression),
|
||||
Unary(UnaryExpression),
|
||||
Ternary(TernaryExpression),
|
||||
|
||||
ArrayInline(ArrayInlineExpression),
|
||||
ArrayInit(ArrayInitExpression),
|
||||
ArrayAccess(ArrayAccessExpression),
|
||||
ArrayRangeAccess(ArrayRangeAccessExpression),
|
||||
|
||||
TupleInit(TupleInitExpression),
|
||||
TupleAccess(TupleAccessExpression),
|
||||
|
||||
CircuitInit(CircuitInitExpression),
|
||||
CircuitAccess(CircuitAccessExpression),
|
||||
|
||||
Call(CallExpression),
|
||||
}
|
||||
|
||||
impl Node for Expression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
use Expression::*;
|
||||
match self {
|
||||
VariableRef(x) => x.span(),
|
||||
Constant(x) => x.span(),
|
||||
Binary(x) => x.span(),
|
||||
Unary(x) => x.span(),
|
||||
Ternary(x) => x.span(),
|
||||
ArrayInline(x) => x.span(),
|
||||
ArrayInit(x) => x.span(),
|
||||
ArrayAccess(x) => x.span(),
|
||||
ArrayRangeAccess(x) => x.span(),
|
||||
TupleInit(x) => x.span(),
|
||||
TupleAccess(x) => x.span(),
|
||||
CircuitInit(x) => x.span(),
|
||||
CircuitAccess(x) => x.span(),
|
||||
Call(x) => x.span(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ExpressionNode: Node {
|
||||
fn set_parent(&self, parent: Weak<Expression>);
|
||||
fn get_parent(&self) -> Option<Arc<Expression>>;
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>);
|
||||
|
||||
fn get_type(&self) -> Option<Type>;
|
||||
fn is_mut_ref(&self) -> bool;
|
||||
fn const_value(&self) -> Option<ConstValue>; // todo: memoize
|
||||
fn is_consty(&self) -> bool;
|
||||
}
|
||||
|
||||
impl ExpressionNode for Expression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
use Expression::*;
|
||||
match self {
|
||||
VariableRef(x) => x.set_parent(parent),
|
||||
Constant(x) => x.set_parent(parent),
|
||||
Binary(x) => x.set_parent(parent),
|
||||
Unary(x) => x.set_parent(parent),
|
||||
Ternary(x) => x.set_parent(parent),
|
||||
ArrayInline(x) => x.set_parent(parent),
|
||||
ArrayInit(x) => x.set_parent(parent),
|
||||
ArrayAccess(x) => x.set_parent(parent),
|
||||
ArrayRangeAccess(x) => x.set_parent(parent),
|
||||
TupleInit(x) => x.set_parent(parent),
|
||||
TupleAccess(x) => x.set_parent(parent),
|
||||
CircuitInit(x) => x.set_parent(parent),
|
||||
CircuitAccess(x) => x.set_parent(parent),
|
||||
Call(x) => x.set_parent(parent),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
use Expression::*;
|
||||
match self {
|
||||
VariableRef(x) => x.get_parent(),
|
||||
Constant(x) => x.get_parent(),
|
||||
Binary(x) => x.get_parent(),
|
||||
Unary(x) => x.get_parent(),
|
||||
Ternary(x) => x.get_parent(),
|
||||
ArrayInline(x) => x.get_parent(),
|
||||
ArrayInit(x) => x.get_parent(),
|
||||
ArrayAccess(x) => x.get_parent(),
|
||||
ArrayRangeAccess(x) => x.get_parent(),
|
||||
TupleInit(x) => x.get_parent(),
|
||||
TupleAccess(x) => x.get_parent(),
|
||||
CircuitInit(x) => x.get_parent(),
|
||||
CircuitAccess(x) => x.get_parent(),
|
||||
Call(x) => x.get_parent(),
|
||||
}
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
use Expression::*;
|
||||
match self {
|
||||
VariableRef(x) => x.enforce_parents(expr),
|
||||
Constant(x) => x.enforce_parents(expr),
|
||||
Binary(x) => x.enforce_parents(expr),
|
||||
Unary(x) => x.enforce_parents(expr),
|
||||
Ternary(x) => x.enforce_parents(expr),
|
||||
ArrayInline(x) => x.enforce_parents(expr),
|
||||
ArrayInit(x) => x.enforce_parents(expr),
|
||||
ArrayAccess(x) => x.enforce_parents(expr),
|
||||
ArrayRangeAccess(x) => x.enforce_parents(expr),
|
||||
TupleInit(x) => x.enforce_parents(expr),
|
||||
TupleAccess(x) => x.enforce_parents(expr),
|
||||
CircuitInit(x) => x.enforce_parents(expr),
|
||||
CircuitAccess(x) => x.enforce_parents(expr),
|
||||
Call(x) => x.enforce_parents(expr),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
use Expression::*;
|
||||
match self {
|
||||
VariableRef(x) => x.get_type(),
|
||||
Constant(x) => x.get_type(),
|
||||
Binary(x) => x.get_type(),
|
||||
Unary(x) => x.get_type(),
|
||||
Ternary(x) => x.get_type(),
|
||||
ArrayInline(x) => x.get_type(),
|
||||
ArrayInit(x) => x.get_type(),
|
||||
ArrayAccess(x) => x.get_type(),
|
||||
ArrayRangeAccess(x) => x.get_type(),
|
||||
TupleInit(x) => x.get_type(),
|
||||
TupleAccess(x) => x.get_type(),
|
||||
CircuitInit(x) => x.get_type(),
|
||||
CircuitAccess(x) => x.get_type(),
|
||||
Call(x) => x.get_type(),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
use Expression::*;
|
||||
match self {
|
||||
VariableRef(x) => x.is_mut_ref(),
|
||||
Constant(x) => x.is_mut_ref(),
|
||||
Binary(x) => x.is_mut_ref(),
|
||||
Unary(x) => x.is_mut_ref(),
|
||||
Ternary(x) => x.is_mut_ref(),
|
||||
ArrayInline(x) => x.is_mut_ref(),
|
||||
ArrayInit(x) => x.is_mut_ref(),
|
||||
ArrayAccess(x) => x.is_mut_ref(),
|
||||
ArrayRangeAccess(x) => x.is_mut_ref(),
|
||||
TupleInit(x) => x.is_mut_ref(),
|
||||
TupleAccess(x) => x.is_mut_ref(),
|
||||
CircuitInit(x) => x.is_mut_ref(),
|
||||
CircuitAccess(x) => x.is_mut_ref(),
|
||||
Call(x) => x.is_mut_ref(),
|
||||
}
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
use Expression::*;
|
||||
match self {
|
||||
VariableRef(x) => x.const_value(),
|
||||
Constant(x) => x.const_value(),
|
||||
Binary(x) => x.const_value(),
|
||||
Unary(x) => x.const_value(),
|
||||
Ternary(x) => x.const_value(),
|
||||
ArrayInline(x) => x.const_value(),
|
||||
ArrayInit(x) => x.const_value(),
|
||||
ArrayAccess(x) => x.const_value(),
|
||||
ArrayRangeAccess(x) => x.const_value(),
|
||||
TupleInit(x) => x.const_value(),
|
||||
TupleAccess(x) => x.const_value(),
|
||||
CircuitInit(x) => x.const_value(),
|
||||
CircuitAccess(x) => x.const_value(),
|
||||
Call(x) => x.const_value(),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
use Expression::*;
|
||||
match self {
|
||||
VariableRef(x) => x.is_consty(),
|
||||
Constant(x) => x.is_consty(),
|
||||
Binary(x) => x.is_consty(),
|
||||
Unary(x) => x.is_consty(),
|
||||
Ternary(x) => x.is_consty(),
|
||||
ArrayInline(x) => x.is_consty(),
|
||||
ArrayInit(x) => x.is_consty(),
|
||||
ArrayAccess(x) => x.is_consty(),
|
||||
ArrayRangeAccess(x) => x.is_consty(),
|
||||
TupleInit(x) => x.is_consty(),
|
||||
TupleAccess(x) => x.is_consty(),
|
||||
CircuitInit(x) => x.is_consty(),
|
||||
CircuitAccess(x) => x.is_consty(),
|
||||
Call(x) => x.is_consty(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::Expression> for Arc<Expression> {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::Expression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
use leo_ast::Expression::*;
|
||||
let expression = match value {
|
||||
Identifier(identifier) => Self::from_ast(scope, identifier, expected_type)?,
|
||||
Value(value) => Arc::new(Constant::from_ast(scope, value, expected_type).map(Expression::Constant)?),
|
||||
Binary(binary) => {
|
||||
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)?),
|
||||
Ternary(conditional) => {
|
||||
Arc::new(TernaryExpression::from_ast(scope, conditional, expected_type).map(Expression::Ternary)?)
|
||||
}
|
||||
|
||||
ArrayInline(array_inline) => Arc::new(
|
||||
ArrayInlineExpression::from_ast(scope, array_inline, expected_type).map(Expression::ArrayInline)?,
|
||||
),
|
||||
ArrayInit(array_init) => {
|
||||
Arc::new(ArrayInitExpression::from_ast(scope, array_init, expected_type).map(Expression::ArrayInit)?)
|
||||
}
|
||||
ArrayAccess(array_access) => Arc::new(
|
||||
ArrayAccessExpression::from_ast(scope, array_access, expected_type).map(Expression::ArrayAccess)?,
|
||||
),
|
||||
ArrayRangeAccess(array_range_access) => Arc::new(
|
||||
ArrayRangeAccessExpression::from_ast(scope, array_range_access, expected_type)
|
||||
.map(Expression::ArrayRangeAccess)?,
|
||||
),
|
||||
|
||||
TupleInit(tuple_init) => {
|
||||
Arc::new(TupleInitExpression::from_ast(scope, tuple_init, expected_type).map(Expression::TupleInit)?)
|
||||
}
|
||||
TupleAccess(tuple_access) => Arc::new(
|
||||
TupleAccessExpression::from_ast(scope, tuple_access, expected_type).map(Expression::TupleAccess)?,
|
||||
),
|
||||
|
||||
CircuitInit(circuit_init) => Arc::new(
|
||||
CircuitInitExpression::from_ast(scope, circuit_init, expected_type).map(Expression::CircuitInit)?,
|
||||
),
|
||||
CircuitMemberAccess(circuit_member) => Arc::new(
|
||||
CircuitAccessExpression::from_ast(scope, circuit_member, expected_type)
|
||||
.map(Expression::CircuitAccess)?,
|
||||
),
|
||||
CircuitStaticFunctionAccess(circuit_member) => Arc::new(
|
||||
CircuitAccessExpression::from_ast(scope, circuit_member, expected_type)
|
||||
.map(Expression::CircuitAccess)?,
|
||||
),
|
||||
|
||||
Call(call) => Arc::new(CallExpression::from_ast(scope, call, expected_type).map(Expression::Call)?),
|
||||
};
|
||||
expression.enforce_parents(&expression);
|
||||
Ok(expression)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::Expression> for &Expression {
|
||||
fn into(self) -> leo_ast::Expression {
|
||||
use Expression::*;
|
||||
match self {
|
||||
VariableRef(x) => leo_ast::Expression::Identifier(x.into()),
|
||||
Constant(x) => leo_ast::Expression::Value(x.into()),
|
||||
Binary(x) => leo_ast::Expression::Binary(x.into()),
|
||||
Unary(x) => leo_ast::Expression::Unary(x.into()),
|
||||
Ternary(x) => leo_ast::Expression::Ternary(x.into()),
|
||||
ArrayInline(x) => leo_ast::Expression::ArrayInline(x.into()),
|
||||
ArrayInit(x) => leo_ast::Expression::ArrayInit(x.into()),
|
||||
ArrayAccess(x) => leo_ast::Expression::ArrayAccess(x.into()),
|
||||
ArrayRangeAccess(x) => leo_ast::Expression::ArrayRangeAccess(x.into()),
|
||||
TupleInit(x) => leo_ast::Expression::TupleInit(x.into()),
|
||||
TupleAccess(x) => leo_ast::Expression::TupleAccess(x.into()),
|
||||
CircuitInit(x) => leo_ast::Expression::CircuitInit(x.into()),
|
||||
CircuitAccess(x) => x.into(),
|
||||
Call(x) => leo_ast::Expression::Call(x.into()),
|
||||
}
|
||||
}
|
||||
}
|
103
asg/src/expression/ternary.rs
Normal file
103
asg/src/expression/ternary.rs
Normal file
@ -0,0 +1,103 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct TernaryExpression {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub condition: Arc<Expression>,
|
||||
pub if_true: Arc<Expression>,
|
||||
pub if_false: Arc<Expression>,
|
||||
}
|
||||
|
||||
impl Node for TernaryExpression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for TernaryExpression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
self.condition.set_parent(Arc::downgrade(expr));
|
||||
self.if_true.set_parent(Arc::downgrade(expr));
|
||||
self.if_false.set_parent(Arc::downgrade(expr));
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
self.if_true.get_type()
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
self.if_true.is_mut_ref() && self.if_false.is_mut_ref()
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
if let Some(ConstValue::Boolean(switch)) = self.condition.const_value() {
|
||||
if switch {
|
||||
self.if_true.const_value()
|
||||
} else {
|
||||
self.if_false.const_value()
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
self.condition.is_consty() && self.if_true.is_consty() && self.if_false.is_consty()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::TernaryExpression> for TernaryExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::TernaryExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<TernaryExpression, AsgConvertError> {
|
||||
Ok(TernaryExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
condition: Arc::<Expression>::from_ast(scope, &*value.condition, Some(Type::Boolean.partial()))?,
|
||||
if_true: Arc::<Expression>::from_ast(scope, &*value.if_true, expected_type.clone())?,
|
||||
if_false: Arc::<Expression>::from_ast(scope, &*value.if_false, expected_type)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::TernaryExpression> for &TernaryExpression {
|
||||
fn into(self) -> leo_ast::TernaryExpression {
|
||||
leo_ast::TernaryExpression {
|
||||
condition: Box::new(self.condition.as_ref().into()),
|
||||
if_true: Box::new(self.if_true.as_ref().into()),
|
||||
if_false: Box::new(self.if_false.as_ref().into()),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
119
asg/src/expression/tuple_access.rs
Normal file
119
asg/src/expression/tuple_access.rs
Normal file
@ -0,0 +1,119 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct TupleAccessExpression {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub tuple_ref: Arc<Expression>,
|
||||
pub index: usize,
|
||||
}
|
||||
|
||||
impl Node for TupleAccessExpression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for TupleAccessExpression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
self.tuple_ref.set_parent(Arc::downgrade(expr));
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
match self.tuple_ref.get_type()? {
|
||||
Type::Tuple(subtypes) => subtypes.get(self.index).cloned(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
self.tuple_ref.is_mut_ref()
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
let tuple_const = self.tuple_ref.const_value()?;
|
||||
match tuple_const {
|
||||
ConstValue::Tuple(sub_consts) => sub_consts.get(self.index).cloned(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
self.tuple_ref.is_consty()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::TupleAccessExpression> for TupleAccessExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::TupleAccessExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<TupleAccessExpression, AsgConvertError> {
|
||||
let index = value
|
||||
.index
|
||||
.value
|
||||
.parse::<usize>()
|
||||
.map_err(|_| AsgConvertError::parse_index_error())?;
|
||||
|
||||
let mut expected_tuple = vec![None; index + 1];
|
||||
expected_tuple[index] = expected_type;
|
||||
|
||||
let tuple = Arc::<Expression>::from_ast(scope, &*value.tuple, Some(PartialType::Tuple(expected_tuple)))?;
|
||||
let tuple_type = tuple.get_type();
|
||||
if let Some(Type::Tuple(_items)) = tuple_type {
|
||||
} else {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
"a tuple",
|
||||
tuple_type.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(TupleAccessExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
tuple_ref: tuple,
|
||||
index,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::TupleAccessExpression> for &TupleAccessExpression {
|
||||
fn into(self) -> leo_ast::TupleAccessExpression {
|
||||
leo_ast::TupleAccessExpression {
|
||||
tuple: Box::new(self.tuple_ref.as_ref().into()),
|
||||
index: leo_ast::PositiveNumber {
|
||||
value: self.index.to_string(),
|
||||
},
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
136
asg/src/expression/tuple_init.rs
Normal file
136
asg/src/expression/tuple_init.rs
Normal file
@ -0,0 +1,136 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct TupleInitExpression {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub elements: Vec<Arc<Expression>>,
|
||||
}
|
||||
|
||||
impl Node for TupleInitExpression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for TupleInitExpression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
self.elements.iter().for_each(|element| {
|
||||
element.set_parent(Arc::downgrade(expr));
|
||||
})
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
let mut output = vec![];
|
||||
for element in self.elements.iter() {
|
||||
output.push(element.get_type()?);
|
||||
}
|
||||
Some(Type::Tuple(output))
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
let mut consts = vec![];
|
||||
for element in self.elements.iter() {
|
||||
if let Some(const_value) = element.const_value() {
|
||||
consts.push(const_value);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(ConstValue::Tuple(consts))
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
self.elements.iter().all(|x| x.is_consty())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::TupleInitExpression> for TupleInitExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::TupleInitExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<TupleInitExpression, AsgConvertError> {
|
||||
let tuple_types = match expected_type {
|
||||
Some(PartialType::Tuple(sub_types)) => Some(sub_types),
|
||||
None => None,
|
||||
x => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
"tuple",
|
||||
x.map(|x| x.to_string()).as_deref(),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(tuple_types) = tuple_types.as_ref() {
|
||||
if tuple_types.len() != value.elements.len() {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&*format!("tuple of length {}", tuple_types.len()),
|
||||
Some(&*format!("tuple of length {}", value.elements.len())),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let elements = value
|
||||
.elements
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, e)| {
|
||||
Arc::<Expression>::from_ast(
|
||||
scope,
|
||||
e,
|
||||
tuple_types.as_ref().map(|x| x.get(i)).flatten().cloned().flatten(),
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, AsgConvertError>>()?;
|
||||
|
||||
Ok(TupleInitExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
elements,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::TupleInitExpression> for &TupleInitExpression {
|
||||
fn into(self) -> leo_ast::TupleInitExpression {
|
||||
leo_ast::TupleInitExpression {
|
||||
elements: self.elements.iter().map(|e| e.as_ref().into()).collect(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
133
asg/src/expression/unary.rs
Normal file
133
asg/src/expression/unary.rs
Normal file
@ -0,0 +1,133 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
|
||||
pub use leo_ast::UnaryOperation;
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct UnaryExpression {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub operation: UnaryOperation,
|
||||
pub inner: Arc<Expression>,
|
||||
}
|
||||
|
||||
impl Node for UnaryExpression {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for UnaryExpression {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, expr: &Arc<Expression>) {
|
||||
self.inner.set_parent(Arc::downgrade(expr));
|
||||
}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
self.inner.get_type()
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
if let Some(inner) = self.inner.const_value() {
|
||||
match self.operation {
|
||||
UnaryOperation::Not => match inner {
|
||||
ConstValue::Boolean(value) => Some(ConstValue::Boolean(!value)),
|
||||
_ => None,
|
||||
},
|
||||
UnaryOperation::Negate => {
|
||||
match inner {
|
||||
ConstValue::Int(value) => Some(ConstValue::Int(value.value_negate()?)),
|
||||
// ConstValue::Group(value) => Some(ConstValue::Group(value)), TODO: groups
|
||||
// ConstValue::Field(value) => Some(ConstValue::Field(-value)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
self.inner.is_consty()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::UnaryExpression> for UnaryExpression {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::UnaryExpression,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<UnaryExpression, AsgConvertError> {
|
||||
let expected_type = match value.op {
|
||||
UnaryOperation::Not => match expected_type.map(|x| x.full()).flatten() {
|
||||
Some(Type::Boolean) | None => Some(Type::Boolean),
|
||||
Some(type_) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&type_.to_string(),
|
||||
Some(&*Type::Boolean.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
},
|
||||
UnaryOperation::Negate => match expected_type.map(|x| x.full()).flatten() {
|
||||
Some(type_ @ Type::Integer(_)) => Some(type_),
|
||||
Some(Type::Group) => Some(Type::Group),
|
||||
Some(Type::Field) => Some(Type::Field),
|
||||
None => None,
|
||||
Some(type_) => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&type_.to_string(),
|
||||
Some("integer, group, field"),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
},
|
||||
};
|
||||
Ok(UnaryExpression {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
operation: value.op.clone(),
|
||||
inner: Arc::<Expression>::from_ast(scope, &*value.inner, expected_type.map(Into::into))?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::UnaryExpression> for &UnaryExpression {
|
||||
fn into(self) -> leo_ast::UnaryExpression {
|
||||
leo_ast::UnaryExpression {
|
||||
op: self.operation.clone(),
|
||||
inner: Box::new(self.inner.as_ref().into()),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
210
asg/src/expression/variable_ref.rs
Normal file
210
asg/src/expression/variable_ref.rs
Normal file
@ -0,0 +1,210 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{
|
||||
AsgConvertError,
|
||||
ConstValue,
|
||||
Constant,
|
||||
DefinitionStatement,
|
||||
Expression,
|
||||
ExpressionNode,
|
||||
FromAst,
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Statement,
|
||||
Type,
|
||||
Variable,
|
||||
VariableDeclaration,
|
||||
};
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct VariableRef {
|
||||
pub parent: RefCell<Option<Weak<Expression>>>,
|
||||
pub span: Option<Span>,
|
||||
pub variable: Variable,
|
||||
}
|
||||
|
||||
impl Node for VariableRef {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExpressionNode for VariableRef {
|
||||
fn set_parent(&self, parent: Weak<Expression>) {
|
||||
self.parent.replace(Some(parent));
|
||||
}
|
||||
|
||||
fn get_parent(&self) -> Option<Arc<Expression>> {
|
||||
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
|
||||
}
|
||||
|
||||
fn enforce_parents(&self, _expr: &Arc<Expression>) {}
|
||||
|
||||
fn get_type(&self) -> Option<Type> {
|
||||
Some(self.variable.borrow().type_.clone())
|
||||
}
|
||||
|
||||
fn is_mut_ref(&self) -> bool {
|
||||
self.variable.borrow().mutable
|
||||
}
|
||||
|
||||
// todo: we can use use hacky ssa here to catch more cases, or just enforce ssa before asg generation finished
|
||||
fn const_value(&self) -> Option<ConstValue> {
|
||||
let variable = self.variable.borrow();
|
||||
if variable.mutable || variable.assignments.len() != 1 {
|
||||
return None;
|
||||
}
|
||||
let assignment = variable
|
||||
.assignments
|
||||
.get(0)
|
||||
.unwrap()
|
||||
.upgrade()
|
||||
.expect("stale assignment for variable");
|
||||
match &*assignment {
|
||||
Statement::Definition(DefinitionStatement { variables, value, .. }) => {
|
||||
if variables.len() == 1 {
|
||||
let defined_variable = variables.get(0).unwrap().borrow();
|
||||
assert_eq!(variable.id, defined_variable.id);
|
||||
|
||||
value.const_value()
|
||||
} else {
|
||||
for defined_variable in variables.iter() {
|
||||
let defined_variable = defined_variable.borrow();
|
||||
if defined_variable.id == variable.id {
|
||||
return value.const_value();
|
||||
}
|
||||
}
|
||||
panic!("no corresponding tuple variable found during const destructuring (corrupt asg?)");
|
||||
}
|
||||
}
|
||||
_ => None, //todo unroll loops during asg phase
|
||||
}
|
||||
}
|
||||
|
||||
fn is_consty(&self) -> bool {
|
||||
let variable = self.variable.borrow();
|
||||
if variable.declaration == VariableDeclaration::IterationDefinition {
|
||||
return true;
|
||||
}
|
||||
if variable.mutable || variable.assignments.len() != 1 {
|
||||
return false;
|
||||
}
|
||||
let assignment = variable
|
||||
.assignments
|
||||
.get(0)
|
||||
.unwrap()
|
||||
.upgrade()
|
||||
.expect("stale assignment for variable");
|
||||
|
||||
match &*assignment {
|
||||
Statement::Definition(DefinitionStatement { variables, value, .. }) => {
|
||||
if variables.len() == 1 {
|
||||
let defined_variable = variables.get(0).unwrap().borrow();
|
||||
assert_eq!(variable.id, defined_variable.id);
|
||||
|
||||
value.is_consty()
|
||||
} else {
|
||||
for defined_variable in variables.iter() {
|
||||
let defined_variable = defined_variable.borrow();
|
||||
if defined_variable.id == variable.id {
|
||||
return value.is_consty();
|
||||
}
|
||||
}
|
||||
panic!("no corresponding tuple variable found during const destructuring (corrupt asg?)");
|
||||
}
|
||||
}
|
||||
Statement::Iteration(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::Identifier> for Arc<Expression> {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::Identifier,
|
||||
expected_type: Option<PartialType>,
|
||||
) -> Result<Arc<Expression>, AsgConvertError> {
|
||||
let variable = if value.name == "input" {
|
||||
if let Some(function) = scope.borrow().resolve_current_function() {
|
||||
if !function.has_input {
|
||||
return Err(AsgConvertError::unresolved_reference(&value.name, &value.span));
|
||||
}
|
||||
} else {
|
||||
return Err(AsgConvertError::unresolved_reference(&value.name, &value.span));
|
||||
}
|
||||
if let Some(input) = scope.borrow().resolve_input() {
|
||||
input.container
|
||||
} else {
|
||||
return Err(AsgConvertError::InternalError(
|
||||
"attempted to reference input when none is in scope".to_string(),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
match scope.borrow().resolve_variable(&value.name) {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
if value.name.starts_with("aleo1") {
|
||||
return Ok(Arc::new(Expression::Constant(Constant {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
value: ConstValue::Address(value.name.clone()),
|
||||
})));
|
||||
}
|
||||
return Err(AsgConvertError::unresolved_reference(&value.name, &value.span));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let variable_ref = VariableRef {
|
||||
parent: RefCell::new(None),
|
||||
span: Some(value.span.clone()),
|
||||
variable: variable.clone(),
|
||||
};
|
||||
let expression = Arc::new(Expression::VariableRef(variable_ref));
|
||||
|
||||
if let Some(expected_type) = expected_type {
|
||||
let type_ = expression
|
||||
.get_type()
|
||||
.ok_or_else(|| AsgConvertError::unresolved_reference(&value.name, &value.span))?;
|
||||
if !expected_type.matches(&type_) {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&expected_type.to_string(),
|
||||
Some(&*type_.to_string()),
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let mut variable_ref = variable.borrow_mut();
|
||||
variable_ref.references.push(Arc::downgrade(&expression));
|
||||
|
||||
Ok(expression)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::Identifier> for &VariableRef {
|
||||
fn into(self) -> leo_ast::Identifier {
|
||||
self.variable.borrow().name.clone()
|
||||
}
|
||||
}
|
71
asg/src/import.rs
Normal file
71
asg/src/import.rs
Normal file
@ -0,0 +1,71 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
//! Helper methods for resolving imported packages.
|
||||
|
||||
use crate::{AsgConvertError, Program, Span};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
|
||||
pub trait ImportResolver {
|
||||
fn resolve_package(&mut self, package_segments: &[&str], span: &Span) -> Result<Option<Program>, AsgConvertError>;
|
||||
}
|
||||
|
||||
pub struct NullImportResolver;
|
||||
|
||||
impl ImportResolver for NullImportResolver {
|
||||
fn resolve_package(
|
||||
&mut self,
|
||||
_package_segments: &[&str],
|
||||
_span: &Span,
|
||||
) -> Result<Option<Program>, AsgConvertError> {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CoreImportResolver<'a, T: ImportResolver + 'static>(pub &'a mut T);
|
||||
|
||||
impl<'a, T: ImportResolver + 'static> ImportResolver for CoreImportResolver<'a, T> {
|
||||
fn resolve_package(&mut self, package_segments: &[&str], span: &Span) -> Result<Option<Program>, AsgConvertError> {
|
||||
if !package_segments.is_empty() && package_segments.get(0).unwrap() == &"core" {
|
||||
Ok(crate::resolve_core_module(&*package_segments[1..].join("."))?)
|
||||
} else {
|
||||
self.0.resolve_package(package_segments, span)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StandardImportResolver;
|
||||
|
||||
impl ImportResolver for StandardImportResolver {
|
||||
fn resolve_package(
|
||||
&mut self,
|
||||
_package_segments: &[&str],
|
||||
_span: &Span,
|
||||
) -> Result<Option<Program>, AsgConvertError> {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MockedImportResolver {
|
||||
pub packages: IndexMap<String, Program>,
|
||||
}
|
||||
|
||||
impl ImportResolver for MockedImportResolver {
|
||||
fn resolve_package(&mut self, package_segments: &[&str], _span: &Span) -> Result<Option<Program>, AsgConvertError> {
|
||||
Ok(self.packages.get(&package_segments.join(".")).cloned())
|
||||
}
|
||||
}
|
147
asg/src/input.rs
Normal file
147
asg/src/input.rs
Normal file
@ -0,0 +1,147 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{Circuit, CircuitBody, CircuitMember, CircuitMemberBody, Identifier, Scope, Type, Variable, WeakType};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
/// Stores program input values as asg nodes.
|
||||
#[derive(Clone)]
|
||||
pub struct Input {
|
||||
pub registers: Arc<CircuitBody>,
|
||||
pub state: Arc<CircuitBody>,
|
||||
pub state_leaf: Arc<CircuitBody>,
|
||||
pub record: Arc<CircuitBody>,
|
||||
pub container_circuit: Arc<CircuitBody>,
|
||||
pub container: Variable,
|
||||
}
|
||||
|
||||
pub const CONTAINER_PSEUDO_CIRCUIT: &str = "$InputContainer";
|
||||
pub const REGISTERS_PSEUDO_CIRCUIT: &str = "$InputRegister";
|
||||
pub const RECORD_PSEUDO_CIRCUIT: &str = "$InputRecord";
|
||||
pub const STATE_PSEUDO_CIRCUIT: &str = "$InputState";
|
||||
pub const STATE_LEAF_PSEUDO_CIRCUIT: &str = "$InputStateLeaf";
|
||||
|
||||
impl Input {
|
||||
fn make_header(name: &str) -> Arc<Circuit> {
|
||||
Arc::new(Circuit {
|
||||
id: uuid::Uuid::new_v4(),
|
||||
name: RefCell::new(Identifier::new(name.to_string())),
|
||||
body: RefCell::new(Weak::new()),
|
||||
members: RefCell::new(IndexMap::new()),
|
||||
core_mapping: RefCell::new(None),
|
||||
})
|
||||
}
|
||||
|
||||
fn make_body(scope: &Scope, circuit: &Arc<Circuit>) -> Arc<CircuitBody> {
|
||||
let body = Arc::new(CircuitBody {
|
||||
scope: scope.clone(),
|
||||
span: None,
|
||||
circuit: circuit.clone(),
|
||||
members: RefCell::new(IndexMap::new()),
|
||||
});
|
||||
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();
|
||||
container_members.insert(
|
||||
"registers".to_string(),
|
||||
CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(®isters))),
|
||||
);
|
||||
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(
|
||||
"state_leaf".to_string(),
|
||||
CircuitMember::Variable(WeakType::Circuit(Arc::downgrade(&state_leaf))),
|
||||
);
|
||||
|
||||
let container_circuit = Arc::new(Circuit {
|
||||
id: uuid::Uuid::new_v4(),
|
||||
name: RefCell::new(Identifier::new(CONTAINER_PSEUDO_CIRCUIT.to_string())),
|
||||
body: RefCell::new(Weak::new()),
|
||||
members: RefCell::new(container_members),
|
||||
core_mapping: RefCell::new(None),
|
||||
});
|
||||
|
||||
let registers_body = Self::make_body(scope, ®isters);
|
||||
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 {
|
||||
registers: registers_body,
|
||||
record: record_body,
|
||||
state: state_body,
|
||||
state_leaf: state_leaf_body,
|
||||
container_circuit: container_circuit_body,
|
||||
container: Arc::new(RefCell::new(crate::InnerVariable {
|
||||
id: uuid::Uuid::new_v4(),
|
||||
name: Identifier::new("input".to_string()),
|
||||
type_: Type::Circuit(container_circuit),
|
||||
mutable: false,
|
||||
declaration: crate::VariableDeclaration::Input,
|
||||
references: vec![],
|
||||
assignments: vec![],
|
||||
})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Circuit {
|
||||
pub fn is_input_pseudo_circuit(&self) -> bool {
|
||||
matches!(
|
||||
&*self.name.borrow().name,
|
||||
REGISTERS_PSEUDO_CIRCUIT | RECORD_PSEUDO_CIRCUIT | STATE_PSEUDO_CIRCUIT | STATE_LEAF_PSEUDO_CIRCUIT
|
||||
)
|
||||
}
|
||||
}
|
119
asg/src/lib.rs
Normal file
119
asg/src/lib.rs
Normal file
@ -0,0 +1,119 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
//! The abstract semantic graph (asg) for a Leo program.
|
||||
//!
|
||||
//! This module contains the [`Asg`] type, an abstract data type that represents a Leo program
|
||||
//! as a series of graph nodes. The [`Asg`] type is at a greater level of abstraction than an [`Ast`].
|
||||
//!
|
||||
//! A new [`Asg`] type can be created from an [`Ast`].
|
||||
//! Converting to an [`Asg`] provides greater type safety by canonicalizing and checking program types.
|
||||
|
||||
#![allow(clippy::from_over_into)]
|
||||
#[macro_use]
|
||||
extern crate thiserror;
|
||||
|
||||
pub mod checks;
|
||||
pub use checks::*;
|
||||
|
||||
pub mod const_value;
|
||||
pub use const_value::*;
|
||||
|
||||
pub mod error;
|
||||
pub use error::*;
|
||||
|
||||
pub mod expression;
|
||||
pub use expression::*;
|
||||
|
||||
pub mod import;
|
||||
pub use import::*;
|
||||
|
||||
mod input;
|
||||
pub use input::*;
|
||||
|
||||
pub mod node;
|
||||
pub use node::*;
|
||||
|
||||
pub mod prelude;
|
||||
pub use prelude::*;
|
||||
|
||||
pub mod program;
|
||||
pub use program::*;
|
||||
|
||||
pub mod reducer;
|
||||
pub use reducer::*;
|
||||
|
||||
pub mod scope;
|
||||
pub use scope::*;
|
||||
|
||||
pub mod statement;
|
||||
pub use statement::*;
|
||||
|
||||
pub mod type_;
|
||||
pub use type_::*;
|
||||
|
||||
pub mod variable;
|
||||
pub use variable::*;
|
||||
|
||||
pub use leo_ast::{Identifier, Span};
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
/// The abstract semantic graph (asg) for a Leo program.
|
||||
///
|
||||
/// The [`Asg`] type represents a Leo program as a series of recursive data types.
|
||||
/// 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.
|
||||
// #[derive(Debug, Eq, PartialEq)]
|
||||
// pub struct Asg {
|
||||
// asg: InnerProgram,
|
||||
// }
|
||||
//
|
||||
// impl Asg {
|
||||
// /// Creates a new asg from a given ast tree and import resolver.
|
||||
// pub fn new<T: ImportResolver + 'static>(
|
||||
// content: leo_ast::Program,
|
||||
// resolver: &mut T,
|
||||
// ) -> Result<Program, AsgConvertError> {
|
||||
// InnerProgram::new(&content, resolver)
|
||||
// }
|
||||
//
|
||||
// /// Returns a reference to the inner program ast representation.
|
||||
// pub fn into_repr(self) -> Program {
|
||||
// self.asg
|
||||
// }
|
||||
// }
|
||||
|
||||
pub fn load_ast<T: AsRef<Path>, Y: AsRef<str>>(path: T, content: Y) -> Result<leo_ast::Program, AsgConvertError> {
|
||||
// Parses the Leo file and constructs a grammar ast.
|
||||
let ast = leo_grammar::Grammar::new(path.as_ref(), content.as_ref())
|
||||
.map_err(|e| AsgConvertError::InternalError(format!("ast: {:?}", e)))?;
|
||||
|
||||
// Parses the pest ast and constructs a Leo ast.
|
||||
Ok(leo_ast::Ast::new("load_ast", &ast)?.into_repr())
|
||||
}
|
||||
|
||||
pub fn load_asg_from_ast<T: ImportResolver + 'static>(
|
||||
content: leo_ast::Program,
|
||||
resolver: &mut T,
|
||||
) -> Result<Program, AsgConvertError> {
|
||||
InnerProgram::new(&content, resolver)
|
||||
}
|
||||
|
||||
pub fn load_asg<T: ImportResolver + 'static>(content: &str, resolver: &mut T) -> Result<Program, AsgConvertError> {
|
||||
InnerProgram::new(&load_ast("input.leo", content)?, resolver)
|
||||
}
|
28
asg/src/node.rs
Normal file
28
asg/src/node.rs
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, PartialType, Scope, Span};
|
||||
|
||||
/// A node in the abstract semantic graph.
|
||||
pub trait Node {
|
||||
fn span(&self) -> Option<&Span>;
|
||||
}
|
||||
|
||||
pub(super) trait FromAst<T: leo_ast::Node + 'static>: Sized + 'static {
|
||||
// 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)
|
||||
fn from_ast(scope: &Scope, value: &T, expected_type: Option<PartialType>) -> Result<Self, AsgConvertError>;
|
||||
}
|
42
asg/src/prelude.rs
Normal file
42
asg/src/prelude.rs
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
//todo: we should merge this with core
|
||||
|
||||
use crate::{AsgConvertError, Program};
|
||||
|
||||
// todo: make asg deep copy so we can cache resolved core modules
|
||||
// todo: figure out how to do headers without bogus returns
|
||||
|
||||
pub fn resolve_core_module(module: &str) -> Result<Option<Program>, AsgConvertError> {
|
||||
match module {
|
||||
"unstable.blake2s" => {
|
||||
let asg = crate::load_asg(
|
||||
r#"
|
||||
circuit Blake2s {
|
||||
function hash(seed: [u8; 32], message: [u8; 32]) -> [u8; 32] {
|
||||
return [0; 32]
|
||||
}
|
||||
}
|
||||
"#,
|
||||
&mut crate::NullImportResolver,
|
||||
)?;
|
||||
asg.borrow().set_core_mapping("blake2s");
|
||||
Ok(Some(asg))
|
||||
}
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
199
asg/src/program/circuit.rs
Normal file
199
asg/src/program/circuit.rs
Normal file
@ -0,0 +1,199 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, Function, FunctionBody, Identifier, InnerScope, Node, Scope, Span, Type, WeakType};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub enum CircuitMemberBody {
|
||||
Variable(Type),
|
||||
Function(Arc<FunctionBody>),
|
||||
}
|
||||
|
||||
pub enum CircuitMember {
|
||||
Variable(WeakType),
|
||||
Function(Arc<Function>),
|
||||
}
|
||||
|
||||
pub struct Circuit {
|
||||
pub id: Uuid,
|
||||
pub name: RefCell<Identifier>,
|
||||
pub core_mapping: RefCell<Option<String>>,
|
||||
pub body: RefCell<Weak<CircuitBody>>,
|
||||
pub members: RefCell<IndexMap<String, CircuitMember>>,
|
||||
}
|
||||
|
||||
impl PartialEq for Circuit {
|
||||
fn eq(&self, other: &Circuit) -> bool {
|
||||
if self.name != other.name {
|
||||
return false;
|
||||
}
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
||||
impl Eq for Circuit {}
|
||||
|
||||
pub struct CircuitBody {
|
||||
pub scope: Scope,
|
||||
pub span: Option<Span>,
|
||||
pub circuit: Arc<Circuit>,
|
||||
pub members: RefCell<IndexMap<String, CircuitMemberBody>>,
|
||||
}
|
||||
|
||||
impl PartialEq for CircuitBody {
|
||||
fn eq(&self, other: &CircuitBody) -> bool {
|
||||
self.circuit == other.circuit
|
||||
}
|
||||
}
|
||||
impl Eq for CircuitBody {}
|
||||
|
||||
impl Node for CircuitMemberBody {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl Circuit {
|
||||
pub(super) fn init(value: &leo_ast::Circuit) -> Arc<Circuit> {
|
||||
Arc::new(Circuit {
|
||||
id: Uuid::new_v4(),
|
||||
name: RefCell::new(value.circuit_name.clone()),
|
||||
body: RefCell::new(Weak::new()),
|
||||
members: RefCell::new(IndexMap::new()),
|
||||
core_mapping: RefCell::new(None),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn from_ast(self: Arc<Circuit>, scope: &Scope, value: &leo_ast::Circuit) -> Result<(), AsgConvertError> {
|
||||
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());
|
||||
|
||||
for member in value.members.iter() {
|
||||
match member {
|
||||
leo_ast::CircuitMember::CircuitVariable(name, type_) => {
|
||||
if members.contains_key(&name.name) {
|
||||
return Err(AsgConvertError::redefined_circuit_member(
|
||||
&value.circuit_name.name,
|
||||
&name.name,
|
||||
&name.span,
|
||||
));
|
||||
}
|
||||
members.insert(
|
||||
name.name.clone(),
|
||||
CircuitMemberBody::Variable(new_scope.borrow().resolve_ast_type(type_)?),
|
||||
);
|
||||
}
|
||||
leo_ast::CircuitMember::CircuitFunction(function) => {
|
||||
if members.contains_key(&function.identifier.name) {
|
||||
return Err(AsgConvertError::redefined_circuit_member(
|
||||
&value.circuit_name.name,
|
||||
&function.identifier.name,
|
||||
&function.identifier.span,
|
||||
));
|
||||
}
|
||||
let asg_function = {
|
||||
let circuit_members = circuit.members.borrow();
|
||||
match circuit_members.get(&function.identifier.name).unwrap() {
|
||||
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 {
|
||||
span: Some(value.circuit_name.span.clone()),
|
||||
circuit,
|
||||
members: RefCell::new(members),
|
||||
scope: scope.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::Circuit> for &Circuit {
|
||||
fn into(self) -> leo_ast::Circuit {
|
||||
let members = match self.body.borrow().upgrade() {
|
||||
Some(body) => body
|
||||
.members
|
||||
.borrow()
|
||||
.iter()
|
||||
.map(|(name, member)| match &member {
|
||||
CircuitMemberBody::Variable(type_) => {
|
||||
leo_ast::CircuitMember::CircuitVariable(Identifier::new(name.clone()), type_.into())
|
||||
}
|
||||
CircuitMemberBody::Function(func) => {
|
||||
leo_ast::CircuitMember::CircuitFunction(func.function.as_ref().into())
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
None => vec![],
|
||||
};
|
||||
leo_ast::Circuit {
|
||||
circuit_name: self.name.borrow().clone(),
|
||||
members,
|
||||
}
|
||||
}
|
||||
}
|
248
asg/src/program/function.rs
Normal file
248
asg/src/program/function.rs
Normal file
@ -0,0 +1,248 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{
|
||||
AsgConvertError,
|
||||
BlockStatement,
|
||||
Circuit,
|
||||
FromAst,
|
||||
Identifier,
|
||||
InnerScope,
|
||||
MonoidalDirector,
|
||||
ReturnPathReducer,
|
||||
Scope,
|
||||
Span,
|
||||
Statement,
|
||||
Type,
|
||||
Variable,
|
||||
WeakType,
|
||||
};
|
||||
use leo_ast::FunctionInput;
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum FunctionQualifier {
|
||||
SelfRef,
|
||||
MutSelfRef,
|
||||
Static,
|
||||
}
|
||||
|
||||
pub struct Function {
|
||||
pub id: Uuid,
|
||||
pub name: RefCell<Identifier>,
|
||||
pub output: WeakType,
|
||||
pub has_input: bool,
|
||||
pub argument_types: Vec<WeakType>,
|
||||
pub circuit: RefCell<Option<Weak<Circuit>>>,
|
||||
pub body: RefCell<Weak<FunctionBody>>,
|
||||
pub qualifier: FunctionQualifier,
|
||||
}
|
||||
|
||||
impl PartialEq for Function {
|
||||
fn eq(&self, other: &Function) -> bool {
|
||||
if self.name.borrow().name != other.name.borrow().name {
|
||||
return false;
|
||||
}
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
||||
impl Eq for Function {}
|
||||
|
||||
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 {
|
||||
fn eq(&self, other: &FunctionBody) -> bool {
|
||||
self.function == other.function
|
||||
}
|
||||
}
|
||||
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
|
||||
.as_ref()
|
||||
.map(|t| scope.borrow().resolve_ast_type(t))
|
||||
.transpose()?
|
||||
.unwrap_or_else(|| Type::Tuple(vec![]));
|
||||
let mut qualifier = FunctionQualifier::Static;
|
||||
let mut has_input = false;
|
||||
|
||||
let mut argument_types = vec![];
|
||||
{
|
||||
for input in value.input.iter() {
|
||||
match input {
|
||||
FunctionInput::InputKeyword(_) => {
|
||||
has_input = true;
|
||||
}
|
||||
FunctionInput::SelfKeyword(_) => {
|
||||
qualifier = FunctionQualifier::SelfRef;
|
||||
}
|
||||
FunctionInput::MutSelfKeyword(_) => {
|
||||
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 {
|
||||
identifier,
|
||||
mutable,
|
||||
type_,
|
||||
span: _span,
|
||||
}) => {
|
||||
let variable = Arc::new(RefCell::new(crate::InnerVariable {
|
||||
id: Uuid::new_v4(),
|
||||
name: identifier.clone(),
|
||||
type_: scope_borrow.resolve_ast_type(&type_)?,
|
||||
mutable: *mutable,
|
||||
declaration: crate::VariableDeclaration::Parameter,
|
||||
references: vec![],
|
||||
assignments: vec![],
|
||||
}));
|
||||
arguments.push(variable.clone());
|
||||
scope_borrow.variables.insert(identifier.name.clone(), variable);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let main_block = BlockStatement::from_ast(&new_scope, &value.block, None)?;
|
||||
let mut director = MonoidalDirector::new(ReturnPathReducer::new());
|
||||
if !director.reduce_block(&main_block).0 && !function.output.is_unit() {
|
||||
return Err(AsgConvertError::function_missing_return(
|
||||
&function.name.borrow().name,
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
|
||||
#[allow(clippy::never_loop)] // TODO @Protryon: How should we return multiple errors?
|
||||
for (span, error) in director.reducer().errors {
|
||||
return Err(AsgConvertError::function_return_validation(
|
||||
&function.name.borrow().name,
|
||||
&error,
|
||||
&span,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(FunctionBody {
|
||||
span: Some(value.span.clone()),
|
||||
function,
|
||||
arguments,
|
||||
body: Arc::new(Statement::Block(main_block)),
|
||||
scope: new_scope,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::Function> for &Function {
|
||||
fn into(self) -> leo_ast::Function {
|
||||
let (input, body, span) = match self.body.borrow().upgrade() {
|
||||
Some(body) => (
|
||||
body.arguments
|
||||
.iter()
|
||||
.map(|variable| {
|
||||
let variable = variable.borrow();
|
||||
leo_ast::FunctionInput::Variable(leo_ast::FunctionInputVariable {
|
||||
identifier: variable.name.clone(),
|
||||
mutable: variable.mutable,
|
||||
type_: (&variable.type_).into(),
|
||||
span: Span::default(),
|
||||
})
|
||||
})
|
||||
.collect(),
|
||||
match body.body.as_ref() {
|
||||
Statement::Block(block) => block.into(),
|
||||
_ => unimplemented!(),
|
||||
},
|
||||
body.span.clone().unwrap_or_default(),
|
||||
),
|
||||
None => (
|
||||
vec![],
|
||||
leo_ast::Block {
|
||||
statements: vec![],
|
||||
span: Default::default(),
|
||||
},
|
||||
Default::default(),
|
||||
),
|
||||
};
|
||||
let output: Type = self.output.clone().into();
|
||||
leo_ast::Function {
|
||||
identifier: self.name.borrow().clone(),
|
||||
input,
|
||||
block: body,
|
||||
output: Some((&output).into()),
|
||||
span,
|
||||
}
|
||||
}
|
||||
}
|
453
asg/src/program/mod.rs
Normal file
453
asg/src/program/mod.rs
Normal file
@ -0,0 +1,453 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
//! This module defines the program node for an asg.
|
||||
//!
|
||||
//!
|
||||
|
||||
mod circuit;
|
||||
pub use circuit::*;
|
||||
|
||||
mod function;
|
||||
pub use function::*;
|
||||
|
||||
use crate::{AsgConvertError, ImportResolver, InnerScope, Input, Scope};
|
||||
use leo_ast::{Identifier, Package, PackageAccess, Span};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use std::{cell::RefCell, sync::Arc};
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Stores the Leo program abstract semantic graph (asg).
|
||||
#[derive(Clone)]
|
||||
pub struct InnerProgram {
|
||||
/// The unique id of the program.
|
||||
pub id: Uuid,
|
||||
|
||||
/// The program file name.
|
||||
pub name: String,
|
||||
|
||||
/// The packages imported by this program.
|
||||
/// these should generally not be accessed directly, but through scoped imports
|
||||
pub imported_modules: IndexMap<String, Program>,
|
||||
|
||||
/// Maps test name => test code block.
|
||||
pub test_functions: IndexMap<String, (Arc<FunctionBody>, Option<Identifier>)>, // identifier = test input file
|
||||
|
||||
/// Maps function name => function code block.
|
||||
pub functions: IndexMap<String, Arc<FunctionBody>>,
|
||||
|
||||
/// Maps circuit name => circuit code block.
|
||||
pub circuits: IndexMap<String, Arc<CircuitBody>>,
|
||||
|
||||
/// Bindings for names and additional program context.
|
||||
pub scope: Scope,
|
||||
}
|
||||
|
||||
pub type Program = Arc<RefCell<InnerProgram>>;
|
||||
|
||||
/// Enumerates what names are imported from a package.
|
||||
enum ImportSymbol {
|
||||
/// Import the symbol by name.
|
||||
Direct(String),
|
||||
|
||||
/// Import the symbol by name and store it under an alias.
|
||||
Alias(String, String), // from remote -> to local
|
||||
|
||||
/// Import all symbols from the package.
|
||||
All,
|
||||
}
|
||||
|
||||
fn resolve_import_package(
|
||||
output: &mut Vec<(Vec<String>, ImportSymbol, Span)>,
|
||||
mut package_segments: Vec<String>,
|
||||
package: &Package,
|
||||
) {
|
||||
package_segments.push(package.name.name.clone());
|
||||
|
||||
resolve_import_package_access(output, package_segments, &package.access);
|
||||
}
|
||||
|
||||
fn resolve_import_package_access(
|
||||
output: &mut Vec<(Vec<String>, ImportSymbol, Span)>,
|
||||
package_segments: Vec<String>,
|
||||
package: &PackageAccess,
|
||||
) {
|
||||
match package {
|
||||
PackageAccess::Star(span) => {
|
||||
output.push((package_segments, ImportSymbol::All, span.clone()));
|
||||
}
|
||||
PackageAccess::SubPackage(subpackage) => {
|
||||
resolve_import_package(output, package_segments, &*subpackage);
|
||||
}
|
||||
PackageAccess::Symbol(symbol) => {
|
||||
let span = symbol.symbol.span.clone();
|
||||
let symbol = if let Some(alias) = symbol.alias.as_ref() {
|
||||
ImportSymbol::Alias(symbol.symbol.name.clone(), alias.name.clone())
|
||||
} else {
|
||||
ImportSymbol::Direct(symbol.symbol.name.clone())
|
||||
};
|
||||
output.push((package_segments, symbol, span));
|
||||
}
|
||||
PackageAccess::Multiple(subaccesses) => {
|
||||
for subaccess in subaccesses.iter() {
|
||||
resolve_import_package_access(output, package_segments.clone(), &subaccess);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl InnerProgram {
|
||||
/// Returns a new Leo program asg from the given Leo program ast and imports.
|
||||
///
|
||||
/// stages:
|
||||
/// 1. resolve imports into super scope
|
||||
/// 2. finalize declared types
|
||||
/// 3. finalize declared functions
|
||||
/// 4. resolve all asg nodes
|
||||
///
|
||||
pub fn new<T: ImportResolver + 'static>(
|
||||
value: &leo_ast::Program,
|
||||
import_resolver: &mut T,
|
||||
) -> Result<Program, AsgConvertError> {
|
||||
// Recursively extract imported symbols.
|
||||
let mut imported_symbols: Vec<(Vec<String>, ImportSymbol, Span)> = vec![];
|
||||
for import in value.imports.iter() {
|
||||
resolve_import_package(&mut imported_symbols, vec![], &import.package);
|
||||
}
|
||||
|
||||
// Create package list.
|
||||
let mut deduplicated_imports: IndexMap<Vec<String>, Span> = IndexMap::new();
|
||||
for (package, _symbol, span) in imported_symbols.iter() {
|
||||
deduplicated_imports.insert(package.clone(), span.clone());
|
||||
}
|
||||
|
||||
let mut wrapped_resolver = crate::CoreImportResolver(import_resolver);
|
||||
|
||||
// Load imported programs.
|
||||
let mut resolved_packages: IndexMap<Vec<String>, Program> = IndexMap::new();
|
||||
for (package, span) in deduplicated_imports.iter() {
|
||||
let pretty_package = package.join(".");
|
||||
|
||||
let resolved_package =
|
||||
match wrapped_resolver.resolve_package(&package.iter().map(|x| &**x).collect::<Vec<_>>()[..], span)? {
|
||||
Some(x) => x,
|
||||
None => return Err(AsgConvertError::unresolved_import(&*pretty_package, &Span::default())),
|
||||
};
|
||||
|
||||
resolved_packages.insert(package.clone(), resolved_package);
|
||||
}
|
||||
|
||||
let mut imported_functions: IndexMap<String, Arc<FunctionBody>> = IndexMap::new();
|
||||
let mut imported_circuits: IndexMap<String, Arc<CircuitBody>> = IndexMap::new();
|
||||
|
||||
// Prepare locally relevant scope of imports.
|
||||
for (package, symbol, span) in imported_symbols.into_iter() {
|
||||
let pretty_package = package.join(".");
|
||||
|
||||
let resolved_package = resolved_packages
|
||||
.get(&package)
|
||||
.expect("could not find preloaded package");
|
||||
let resolved_package = resolved_package.borrow();
|
||||
match symbol {
|
||||
ImportSymbol::All => {
|
||||
imported_functions.extend(resolved_package.functions.clone().into_iter());
|
||||
imported_circuits.extend(resolved_package.circuits.clone().into_iter());
|
||||
}
|
||||
ImportSymbol::Direct(name) => {
|
||||
if let Some(function) = resolved_package.functions.get(&name) {
|
||||
imported_functions.insert(name.clone(), function.clone());
|
||||
} else if let Some(function) = resolved_package.circuits.get(&name) {
|
||||
imported_circuits.insert(name.clone(), function.clone());
|
||||
} else {
|
||||
return Err(AsgConvertError::unresolved_import(
|
||||
&*format!("{}.{}", pretty_package, name),
|
||||
&span,
|
||||
));
|
||||
}
|
||||
}
|
||||
ImportSymbol::Alias(name, alias) => {
|
||||
if let Some(function) = resolved_package.functions.get(&name) {
|
||||
imported_functions.insert(alias.clone(), function.clone());
|
||||
} else if let Some(function) = resolved_package.circuits.get(&name) {
|
||||
imported_circuits.insert(alias.clone(), function.clone());
|
||||
} else {
|
||||
return Err(AsgConvertError::unresolved_import(
|
||||
&*format!("{}.{}", pretty_package, name),
|
||||
&span,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let import_scope = Arc::new(RefCell::new(InnerScope {
|
||||
id: uuid::Uuid::new_v4(),
|
||||
parent_scope: None,
|
||||
circuit_self: None,
|
||||
variables: IndexMap::new(),
|
||||
functions: imported_functions
|
||||
.iter()
|
||||
.map(|(name, func)| (name.clone(), func.function.clone()))
|
||||
.collect(),
|
||||
circuits: imported_circuits
|
||||
.iter()
|
||||
.map(|(name, circuit)| (name.clone(), circuit.circuit.clone()))
|
||||
.collect(),
|
||||
function: None,
|
||||
input: None,
|
||||
}));
|
||||
|
||||
// Prepare header-like scope entries.
|
||||
let mut proto_circuits = IndexMap::new();
|
||||
for (name, circuit) in value.circuits.iter() {
|
||||
assert_eq!(name.name, circuit.circuit_name.name);
|
||||
let asg_circuit = Circuit::init(circuit);
|
||||
|
||||
proto_circuits.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 value.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();
|
||||
for (name, test_function) in value.tests.iter() {
|
||||
assert_eq!(name.name, test_function.function.identifier.name);
|
||||
let function = Arc::new(Function::from_ast(&scope, &test_function.function)?);
|
||||
|
||||
proto_test_functions.insert(name.name.clone(), function);
|
||||
}
|
||||
|
||||
let mut proto_functions = IndexMap::new();
|
||||
for (name, function) in value.functions.iter() {
|
||||
assert_eq!(name.name, function.identifier.name);
|
||||
let asg_function = Arc::new(Function::from_ast(&scope, function)?);
|
||||
|
||||
scope
|
||||
.borrow_mut()
|
||||
.functions
|
||||
.insert(name.name.clone(), asg_function.clone());
|
||||
proto_functions.insert(name.name.clone(), asg_function);
|
||||
}
|
||||
|
||||
// Load concrete definitions.
|
||||
let mut test_functions = IndexMap::new();
|
||||
for (name, test_function) in value.tests.iter() {
|
||||
assert_eq!(name.name, test_function.function.identifier.name);
|
||||
let function = proto_test_functions.get(&name.name).unwrap();
|
||||
|
||||
let body = Arc::new(FunctionBody::from_ast(
|
||||
&scope,
|
||||
&test_function.function,
|
||||
function.clone(),
|
||||
)?);
|
||||
function.body.replace(Arc::downgrade(&body));
|
||||
|
||||
test_functions.insert(name.name.clone(), (body, test_function.input_file.clone()));
|
||||
}
|
||||
|
||||
let mut functions = IndexMap::new();
|
||||
for (name, function) in value.functions.iter() {
|
||||
assert_eq!(name.name, function.identifier.name);
|
||||
let asg_function = proto_functions.get(&name.name).unwrap();
|
||||
|
||||
let body = Arc::new(FunctionBody::from_ast(&scope, function, asg_function.clone())?);
|
||||
asg_function.body.replace(Arc::downgrade(&body));
|
||||
|
||||
functions.insert(name.name.clone(), body);
|
||||
}
|
||||
|
||||
let mut circuits = IndexMap::new();
|
||||
for (name, circuit) in value.circuits.iter() {
|
||||
assert_eq!(name.name, circuit.circuit_name.name);
|
||||
let asg_circuit = proto_circuits.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);
|
||||
}
|
||||
|
||||
Ok(Arc::new(RefCell::new(InnerProgram {
|
||||
id: Uuid::new_v4(),
|
||||
name: value.name.clone(),
|
||||
test_functions,
|
||||
functions,
|
||||
circuits,
|
||||
imported_modules: resolved_packages
|
||||
.into_iter()
|
||||
.map(|(package, program)| (package.join("."), program))
|
||||
.collect(),
|
||||
scope,
|
||||
})))
|
||||
}
|
||||
|
||||
pub(crate) fn set_core_mapping(&self, mapping: &str) {
|
||||
for (_, circuit) in self.circuits.iter() {
|
||||
circuit.circuit.core_mapping.replace(Some(mapping.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct InternalIdentifierGenerator {
|
||||
next: usize,
|
||||
}
|
||||
|
||||
impl Iterator for InternalIdentifierGenerator {
|
||||
type Item = String;
|
||||
|
||||
fn next(&mut self) -> Option<String> {
|
||||
let out = format!("$_{}_", self.next);
|
||||
self.next += 1;
|
||||
Some(out)
|
||||
}
|
||||
}
|
||||
/// Returns an ast from the given asg program.
|
||||
pub fn reform_ast(program: &Program) -> leo_ast::Program {
|
||||
let mut all_programs: IndexMap<String, Program> = IndexMap::new();
|
||||
let mut program_stack = program.borrow().imported_modules.clone();
|
||||
while let Some((module, program)) = program_stack.pop() {
|
||||
if all_programs.contains_key(&module) {
|
||||
continue;
|
||||
}
|
||||
all_programs.insert(module, program.clone());
|
||||
program_stack.extend(program.borrow().imported_modules.clone());
|
||||
}
|
||||
all_programs.insert("".to_string(), program.clone());
|
||||
let core_programs: Vec<_> = all_programs
|
||||
.iter()
|
||||
.filter(|(module, _)| module.starts_with("core."))
|
||||
.map(|(module, program)| (module.clone(), program.clone()))
|
||||
.collect();
|
||||
all_programs.retain(|module, _| !module.starts_with("core."));
|
||||
|
||||
let mut all_circuits: IndexMap<String, Arc<CircuitBody>> = IndexMap::new();
|
||||
let mut all_functions: IndexMap<String, Arc<FunctionBody>> = IndexMap::new();
|
||||
let mut all_test_functions: IndexMap<String, (Arc<FunctionBody>, Option<Identifier>)> = IndexMap::new();
|
||||
let mut identifiers = InternalIdentifierGenerator { next: 0 };
|
||||
for (_, program) in all_programs.into_iter() {
|
||||
let program = program.borrow();
|
||||
for (name, circuit) in program.circuits.iter() {
|
||||
let identifier = format!("{}{}", identifiers.next().unwrap(), name);
|
||||
circuit.circuit.name.borrow_mut().name = identifier.clone();
|
||||
all_circuits.insert(identifier, circuit.clone());
|
||||
}
|
||||
for (name, function) in program.functions.iter() {
|
||||
let identifier = if name == "main" {
|
||||
"main".to_string()
|
||||
} else {
|
||||
format!("{}{}", identifiers.next().unwrap(), name)
|
||||
};
|
||||
function.function.name.borrow_mut().name = identifier.clone();
|
||||
all_functions.insert(identifier, function.clone());
|
||||
}
|
||||
for (name, function) in program.test_functions.iter() {
|
||||
let identifier = format!("{}{}", identifiers.next().unwrap(), name);
|
||||
function.0.function.name.borrow_mut().name = identifier.clone();
|
||||
all_test_functions.insert(identifier, function.clone());
|
||||
}
|
||||
}
|
||||
|
||||
leo_ast::Program {
|
||||
name: "ast_aggregate".to_string(),
|
||||
imports: core_programs
|
||||
.iter()
|
||||
.map(|(module, _)| leo_ast::ImportStatement {
|
||||
package: leo_ast::Package {
|
||||
name: Identifier::new(module.clone()),
|
||||
access: leo_ast::PackageAccess::Star(Span::default()),
|
||||
span: Default::default(),
|
||||
},
|
||||
span: Span::default(),
|
||||
})
|
||||
.collect(),
|
||||
expected_input: vec![],
|
||||
tests: all_test_functions
|
||||
.into_iter()
|
||||
.map(|(_, (function, ident))| {
|
||||
(function.function.name.borrow().clone(), leo_ast::TestFunction {
|
||||
function: function.function.as_ref().into(),
|
||||
input_file: ident,
|
||||
})
|
||||
})
|
||||
.collect(),
|
||||
functions: all_functions
|
||||
.into_iter()
|
||||
.map(|(_, function)| {
|
||||
(
|
||||
function.function.name.borrow().clone(),
|
||||
function.function.as_ref().into(),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
circuits: all_circuits
|
||||
.into_iter()
|
||||
.map(|(_, circuit)| (circuit.circuit.name.borrow().clone(), circuit.circuit.as_ref().into()))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::Program> for &InnerProgram {
|
||||
fn into(self) -> leo_ast::Program {
|
||||
leo_ast::Program {
|
||||
name: self.name.clone(),
|
||||
imports: vec![],
|
||||
expected_input: vec![],
|
||||
circuits: self
|
||||
.circuits
|
||||
.iter()
|
||||
.map(|(_, circuit)| (circuit.circuit.name.borrow().clone(), circuit.circuit.as_ref().into()))
|
||||
.collect(),
|
||||
functions: self
|
||||
.functions
|
||||
.iter()
|
||||
.map(|(_, function)| {
|
||||
(
|
||||
function.function.name.borrow().clone(),
|
||||
function.function.as_ref().into(),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
tests: self
|
||||
.test_functions
|
||||
.iter()
|
||||
.map(|(_, function)| {
|
||||
(function.0.function.name.borrow().clone(), leo_ast::TestFunction {
|
||||
function: function.0.function.as_ref().into(),
|
||||
input_file: function.1.clone(),
|
||||
})
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
27
asg/src/reducer/mod.rs
Normal file
27
asg/src/reducer/mod.rs
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
//! This module contains the reducer which iterates through ast nodes - converting them into
|
||||
//! asg nodes and saving relevant information.
|
||||
|
||||
mod monoid;
|
||||
pub use monoid::*;
|
||||
|
||||
mod monoidal_director;
|
||||
pub use monoidal_director::*;
|
||||
|
||||
mod monoidal_reducer;
|
||||
pub use monoidal_reducer::*;
|
40
asg/src/reducer/monoid/bool_and.rs
Normal file
40
asg/src/reducer/monoid/bool_and.rs
Normal file
@ -0,0 +1,40 @@
|
||||
// Copyright (C) 2019-2020 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::*;
|
||||
|
||||
pub struct BoolAnd(pub bool);
|
||||
|
||||
impl Default for BoolAnd {
|
||||
fn default() -> Self {
|
||||
BoolAnd(false)
|
||||
}
|
||||
}
|
||||
|
||||
impl Monoid for BoolAnd {
|
||||
fn append(self, other: Self) -> Self {
|
||||
BoolAnd(self.0 && other.0)
|
||||
}
|
||||
|
||||
fn append_all(self, others: impl Iterator<Item = Self>) -> Self {
|
||||
for item in others {
|
||||
if !item.0 {
|
||||
return BoolAnd(false);
|
||||
}
|
||||
}
|
||||
BoolAnd(true)
|
||||
}
|
||||
}
|
43
asg/src/reducer/monoid/mod.rs
Normal file
43
asg/src/reducer/monoid/mod.rs
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
mod bool_and;
|
||||
pub use bool_and::*;
|
||||
|
||||
mod set_append;
|
||||
pub use set_append::*;
|
||||
|
||||
mod vec_append;
|
||||
pub use vec_append::*;
|
||||
|
||||
pub trait Monoid: Default {
|
||||
fn append(self, other: Self) -> Self;
|
||||
|
||||
fn append_all(self, others: impl Iterator<Item = Self>) -> Self {
|
||||
let mut current = self;
|
||||
for item in others {
|
||||
current = current.append(item);
|
||||
}
|
||||
current
|
||||
}
|
||||
|
||||
fn append_option(self, other: Option<Self>) -> Self {
|
||||
match other {
|
||||
None => self,
|
||||
Some(other) => self.append(other),
|
||||
}
|
||||
}
|
||||
}
|
51
asg/src/reducer/monoid/set_append.rs
Normal file
51
asg/src/reducer/monoid/set_append.rs
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright (C) 2019-2020 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 indexmap::IndexSet;
|
||||
use std::hash::Hash;
|
||||
|
||||
pub struct SetAppend<T: Hash + Eq + 'static>(IndexSet<T>);
|
||||
|
||||
impl<T: Hash + Eq + 'static> Default for SetAppend<T> {
|
||||
fn default() -> Self {
|
||||
Self(IndexSet::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq + 'static> Monoid for SetAppend<T> {
|
||||
fn append(mut self, other: Self) -> Self {
|
||||
self.0.extend(other.0);
|
||||
SetAppend(self.0)
|
||||
}
|
||||
|
||||
fn append_all(mut self, others: impl Iterator<Item = Self>) -> Self {
|
||||
let all: Vec<IndexSet<T>> = others.map(|x| x.0).collect();
|
||||
let total_size = all.iter().fold(0, |acc, v| acc + v.len());
|
||||
self.0.reserve(total_size);
|
||||
for item in all.into_iter() {
|
||||
self.0.extend(item);
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hash + Eq + 'static> Into<IndexSet<T>> for SetAppend<T> {
|
||||
fn into(self) -> IndexSet<T> {
|
||||
self.0
|
||||
}
|
||||
}
|
48
asg/src/reducer/monoid/vec_append.rs
Normal file
48
asg/src/reducer/monoid/vec_append.rs
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright (C) 2019-2020 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::*;
|
||||
|
||||
pub struct VecAppend<T>(Vec<T>);
|
||||
|
||||
impl<T> Default for VecAppend<T> {
|
||||
fn default() -> Self {
|
||||
Self(vec![])
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Monoid for VecAppend<T> {
|
||||
fn append(mut self, other: Self) -> Self {
|
||||
self.0.extend(other.0);
|
||||
VecAppend(self.0)
|
||||
}
|
||||
|
||||
fn append_all(mut self, others: impl Iterator<Item = Self>) -> Self {
|
||||
let all: Vec<Vec<T>> = others.map(|x| x.0).collect();
|
||||
let total_size = all.iter().fold(0, |acc, v| acc + v.len());
|
||||
self.0.reserve(total_size);
|
||||
for item in all.into_iter() {
|
||||
self.0.extend(item);
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Into<Vec<T>> for VecAppend<T> {
|
||||
fn into(self) -> Vec<T> {
|
||||
self.0
|
||||
}
|
||||
}
|
290
asg/src/reducer/monoidal_director.rs
Normal file
290
asg/src/reducer/monoidal_director.rs
Normal file
@ -0,0 +1,290 @@
|
||||
// Copyright (C) 2019-2020 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::{marker::PhantomData, sync::Arc};
|
||||
|
||||
pub struct MonoidalDirector<T: Monoid, R: MonoidalReducerExpression<T>> {
|
||||
reducer: R,
|
||||
_monoid: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T: Monoid, R: MonoidalReducerExpression<T>> MonoidalDirector<T, R> {
|
||||
pub fn new(reducer: R) -> Self {
|
||||
Self {
|
||||
reducer,
|
||||
_monoid: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reducer(self) -> R {
|
||||
self.reducer
|
||||
}
|
||||
|
||||
pub fn reduce_expression(&mut self, input: &Arc<Expression>) -> T {
|
||||
match &**input {
|
||||
Expression::ArrayAccess(e) => self.reduce_array_access(e),
|
||||
Expression::ArrayInit(e) => self.reduce_array_init(e),
|
||||
Expression::ArrayInline(e) => self.reduce_array_inline(e),
|
||||
Expression::ArrayRangeAccess(e) => self.reduce_array_range_access(e),
|
||||
Expression::Binary(e) => self.reduce_binary(e),
|
||||
Expression::Call(e) => self.reduce_call(e),
|
||||
Expression::CircuitAccess(e) => self.reduce_circuit_access(e),
|
||||
Expression::CircuitInit(e) => self.reduce_circuit_init(e),
|
||||
Expression::Ternary(e) => self.reduce_ternary_expression(e),
|
||||
Expression::Constant(e) => self.reduce_constant(e),
|
||||
Expression::TupleAccess(e) => self.reduce_tuple_access(e),
|
||||
Expression::TupleInit(e) => self.reduce_tuple_init(e),
|
||||
Expression::Unary(e) => self.reduce_unary(e),
|
||||
Expression::VariableRef(e) => self.reduce_variable_ref(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reduce_array_access(&mut self, input: &ArrayAccessExpression) -> T {
|
||||
let array = self.reduce_expression(&input.array);
|
||||
let index = self.reduce_expression(&input.index);
|
||||
|
||||
self.reducer.reduce_array_access(input, array, index)
|
||||
}
|
||||
|
||||
pub fn reduce_array_init(&mut self, input: &ArrayInitExpression) -> T {
|
||||
let element = self.reduce_expression(&input.element);
|
||||
|
||||
self.reducer.reduce_array_init(input, element)
|
||||
}
|
||||
|
||||
pub fn reduce_array_inline(&mut self, input: &ArrayInlineExpression) -> T {
|
||||
let elements = input.elements.iter().map(|(x, _)| self.reduce_expression(x)).collect();
|
||||
|
||||
self.reducer.reduce_array_inline(input, elements)
|
||||
}
|
||||
|
||||
pub fn reduce_array_range_access(&mut self, input: &ArrayRangeAccessExpression) -> T {
|
||||
let array = self.reduce_expression(&input.array);
|
||||
let left = input.left.as_ref().map(|e| self.reduce_expression(e));
|
||||
let right = input.right.as_ref().map(|e| self.reduce_expression(e));
|
||||
|
||||
self.reducer.reduce_array_range_access(input, array, left, right)
|
||||
}
|
||||
|
||||
pub fn reduce_binary(&mut self, input: &BinaryExpression) -> T {
|
||||
let left = self.reduce_expression(&input.left);
|
||||
let right = self.reduce_expression(&input.right);
|
||||
|
||||
self.reducer.reduce_binary(input, left, right)
|
||||
}
|
||||
|
||||
pub fn reduce_call(&mut self, input: &CallExpression) -> T {
|
||||
let target = input.target.as_ref().map(|e| self.reduce_expression(e));
|
||||
let arguments = input.arguments.iter().map(|e| self.reduce_expression(e)).collect();
|
||||
|
||||
self.reducer.reduce_call(input, target, arguments)
|
||||
}
|
||||
|
||||
pub fn reduce_circuit_access(&mut self, input: &CircuitAccessExpression) -> T {
|
||||
let target = input.target.as_ref().map(|e| self.reduce_expression(e));
|
||||
|
||||
self.reducer.reduce_circuit_access(input, target)
|
||||
}
|
||||
|
||||
pub fn reduce_circuit_init(&mut self, input: &CircuitInitExpression) -> T {
|
||||
let values = input.values.iter().map(|(_, e)| self.reduce_expression(e)).collect();
|
||||
|
||||
self.reducer.reduce_circuit_init(input, values)
|
||||
}
|
||||
|
||||
pub fn reduce_ternary_expression(&mut self, input: &TernaryExpression) -> T {
|
||||
let condition = self.reduce_expression(&input.condition);
|
||||
let if_true = self.reduce_expression(&input.if_true);
|
||||
let if_false = self.reduce_expression(&input.if_false);
|
||||
|
||||
self.reducer
|
||||
.reduce_ternary_expression(input, condition, if_true, if_false)
|
||||
}
|
||||
|
||||
pub fn reduce_constant(&mut self, input: &Constant) -> T {
|
||||
self.reducer.reduce_constant(input)
|
||||
}
|
||||
|
||||
pub fn reduce_tuple_access(&mut self, input: &TupleAccessExpression) -> T {
|
||||
let tuple_ref = self.reduce_expression(&input.tuple_ref);
|
||||
|
||||
self.reducer.reduce_tuple_access(input, tuple_ref)
|
||||
}
|
||||
|
||||
pub fn reduce_tuple_init(&mut self, input: &TupleInitExpression) -> T {
|
||||
let values = input.elements.iter().map(|e| self.reduce_expression(e)).collect();
|
||||
|
||||
self.reducer.reduce_tuple_init(input, values)
|
||||
}
|
||||
|
||||
pub fn reduce_unary(&mut self, input: &UnaryExpression) -> T {
|
||||
let inner = self.reduce_expression(&input.inner);
|
||||
|
||||
self.reducer.reduce_unary(input, inner)
|
||||
}
|
||||
|
||||
pub fn reduce_variable_ref(&mut self, input: &VariableRef) -> T {
|
||||
self.reducer.reduce_variable_ref(input)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Monoid, R: MonoidalReducerStatement<T>> MonoidalDirector<T, R> {
|
||||
pub fn reduce_statement(&mut self, input: &Arc<Statement>) -> T {
|
||||
match &**input {
|
||||
Statement::Assign(s) => self.reduce_assign(s),
|
||||
Statement::Block(s) => self.reduce_block(s),
|
||||
Statement::Conditional(s) => self.reduce_conditional_statement(s),
|
||||
Statement::Console(s) => self.reduce_console(s),
|
||||
Statement::Definition(s) => self.reduce_definition(s),
|
||||
Statement::Expression(s) => self.reduce_expression_statement(s),
|
||||
Statement::Iteration(s) => self.reduce_iteration(s),
|
||||
Statement::Return(s) => self.reduce_return(s),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reduce_assign_access(&mut self, input: &AssignAccess) -> T {
|
||||
let (left, right) = match input {
|
||||
AssignAccess::ArrayRange(left, right) => (
|
||||
left.as_ref().map(|e| self.reduce_expression(e)),
|
||||
right.as_ref().map(|e| self.reduce_expression(e)),
|
||||
),
|
||||
AssignAccess::ArrayIndex(index) => (Some(self.reduce_expression(index)), None),
|
||||
_ => (None, None),
|
||||
};
|
||||
|
||||
self.reducer.reduce_assign_access(input, left, right)
|
||||
}
|
||||
|
||||
pub fn reduce_assign(&mut self, input: &AssignStatement) -> T {
|
||||
let accesses = input
|
||||
.target_accesses
|
||||
.iter()
|
||||
.map(|x| self.reduce_assign_access(x))
|
||||
.collect();
|
||||
let value = self.reduce_expression(&input.value);
|
||||
|
||||
self.reducer.reduce_assign(input, accesses, value)
|
||||
}
|
||||
|
||||
pub fn reduce_block(&mut self, input: &BlockStatement) -> T {
|
||||
let statements = input.statements.iter().map(|x| self.reduce_statement(x)).collect();
|
||||
|
||||
self.reducer.reduce_block(input, statements)
|
||||
}
|
||||
|
||||
pub fn reduce_conditional_statement(&mut self, input: &ConditionalStatement) -> T {
|
||||
let condition = self.reduce_expression(&input.condition);
|
||||
let if_true = self.reduce_statement(&input.result);
|
||||
let if_false = input.next.as_ref().map(|s| self.reduce_statement(s));
|
||||
|
||||
self.reducer
|
||||
.reduce_conditional_statement(input, condition, if_true, if_false)
|
||||
}
|
||||
|
||||
pub fn reduce_formatted_string(&mut self, input: &FormattedString) -> T {
|
||||
let parameters = input.parameters.iter().map(|e| self.reduce_expression(e)).collect();
|
||||
|
||||
self.reducer.reduce_formatted_string(input, parameters)
|
||||
}
|
||||
|
||||
pub fn reduce_console(&mut self, input: &ConsoleStatement) -> T {
|
||||
let argument = match &input.function {
|
||||
ConsoleFunction::Assert(e) => self.reduce_expression(e),
|
||||
ConsoleFunction::Debug(f) | ConsoleFunction::Error(f) | ConsoleFunction::Log(f) => {
|
||||
self.reduce_formatted_string(f)
|
||||
}
|
||||
};
|
||||
|
||||
self.reducer.reduce_console(input, argument)
|
||||
}
|
||||
|
||||
pub fn reduce_definition(&mut self, input: &DefinitionStatement) -> T {
|
||||
let value = self.reduce_expression(&input.value);
|
||||
|
||||
self.reducer.reduce_definition(input, value)
|
||||
}
|
||||
|
||||
pub fn reduce_expression_statement(&mut self, input: &ExpressionStatement) -> T {
|
||||
let value = self.reduce_expression(&input.expression);
|
||||
|
||||
self.reducer.reduce_expression_statement(input, value)
|
||||
}
|
||||
|
||||
pub fn reduce_iteration(&mut self, input: &IterationStatement) -> T {
|
||||
let start = self.reduce_expression(&input.start);
|
||||
let stop = self.reduce_expression(&input.stop);
|
||||
let body = self.reduce_statement(&input.body);
|
||||
|
||||
self.reducer.reduce_iteration(input, start, stop, body)
|
||||
}
|
||||
|
||||
pub fn reduce_return(&mut self, input: &ReturnStatement) -> T {
|
||||
let value = self.reduce_expression(&input.expression);
|
||||
|
||||
self.reducer.reduce_return(input, value)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl<T: Monoid, R: MonoidalReducerProgram<T>> MonoidalDirector<T, R> {
|
||||
fn reduce_function(&mut self, input: &Arc<FunctionBody>) -> T {
|
||||
let body = self.reduce_statement(&input.body);
|
||||
|
||||
self.reducer.reduce_function(input, body)
|
||||
}
|
||||
|
||||
fn reduce_circuit_member(&mut self, input: &CircuitMemberBody) -> T {
|
||||
let function = match input {
|
||||
CircuitMemberBody::Function(f) => Some(self.reduce_function(f)),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
self.reducer.reduce_circuit_member(input, function)
|
||||
}
|
||||
|
||||
fn reduce_circuit(&mut self, input: &Arc<CircuitBody>) -> T {
|
||||
let members = input
|
||||
.members
|
||||
.borrow()
|
||||
.iter()
|
||||
.map(|(_, member)| self.reduce_circuit_member(member))
|
||||
.collect();
|
||||
|
||||
self.reducer.reduce_circuit(input, members)
|
||||
}
|
||||
|
||||
fn reduce_program(&mut self, input: &Program) -> T {
|
||||
let input = input.borrow();
|
||||
let imported_modules = input
|
||||
.imported_modules
|
||||
.iter()
|
||||
.map(|(_, import)| self.reduce_program(import))
|
||||
.collect();
|
||||
let test_functions = input
|
||||
.test_functions
|
||||
.iter()
|
||||
.map(|(_, (f, _))| self.reduce_function(f))
|
||||
.collect();
|
||||
let functions = input.functions.iter().map(|(_, f)| self.reduce_function(f)).collect();
|
||||
let circuits = input.circuits.iter().map(|(_, c)| self.reduce_circuit(c)).collect();
|
||||
|
||||
self.reducer
|
||||
.reduce_program(&input, imported_modules, test_functions, functions, circuits)
|
||||
}
|
||||
}
|
172
asg/src/reducer/monoidal_reducer.rs
Normal file
172
asg/src/reducer/monoidal_reducer.rs
Normal file
@ -0,0 +1,172 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{expression::*, program::*, statement::*, Monoid};
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub trait MonoidalReducerExpression<T: Monoid> {
|
||||
fn reduce_expression(&mut self, input: &Arc<Expression>, value: T) -> T {
|
||||
value
|
||||
}
|
||||
|
||||
fn reduce_array_access(&mut self, input: &ArrayAccessExpression, array: T, index: T) -> T {
|
||||
array.append(index)
|
||||
}
|
||||
|
||||
fn reduce_array_init(&mut self, input: &ArrayInitExpression, element: T) -> T {
|
||||
element
|
||||
}
|
||||
|
||||
fn reduce_array_inline(&mut self, input: &ArrayInlineExpression, elements: Vec<T>) -> T {
|
||||
T::default().append_all(elements.into_iter())
|
||||
}
|
||||
|
||||
fn reduce_array_range_access(
|
||||
&mut self,
|
||||
input: &ArrayRangeAccessExpression,
|
||||
array: T,
|
||||
left: Option<T>,
|
||||
right: Option<T>,
|
||||
) -> T {
|
||||
array.append_option(left).append_option(right)
|
||||
}
|
||||
|
||||
fn reduce_binary(&mut self, input: &BinaryExpression, left: T, right: T) -> T {
|
||||
left.append(right)
|
||||
}
|
||||
|
||||
fn reduce_call(&mut self, input: &CallExpression, target: Option<T>, arguments: Vec<T>) -> T {
|
||||
target.unwrap_or_default().append_all(arguments.into_iter())
|
||||
}
|
||||
|
||||
fn reduce_circuit_access(&mut self, input: &CircuitAccessExpression, target: Option<T>) -> T {
|
||||
target.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn reduce_circuit_init(&mut self, input: &CircuitInitExpression, values: Vec<T>) -> T {
|
||||
T::default().append_all(values.into_iter())
|
||||
}
|
||||
|
||||
fn reduce_ternary_expression(&mut self, input: &TernaryExpression, condition: T, if_true: T, if_false: T) -> T {
|
||||
condition.append(if_true).append(if_false)
|
||||
}
|
||||
|
||||
fn reduce_constant(&mut self, input: &Constant) -> T {
|
||||
T::default()
|
||||
}
|
||||
|
||||
fn reduce_tuple_access(&mut self, input: &TupleAccessExpression, tuple_ref: T) -> T {
|
||||
tuple_ref
|
||||
}
|
||||
|
||||
fn reduce_tuple_init(&mut self, input: &TupleInitExpression, values: Vec<T>) -> T {
|
||||
T::default().append_all(values.into_iter())
|
||||
}
|
||||
|
||||
fn reduce_unary(&mut self, input: &UnaryExpression, inner: T) -> T {
|
||||
inner
|
||||
}
|
||||
|
||||
fn reduce_variable_ref(&mut self, input: &VariableRef) -> T {
|
||||
T::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub trait MonoidalReducerStatement<T: Monoid>: MonoidalReducerExpression<T> {
|
||||
fn reduce_statement(&mut self, input: &Arc<Statement>, value: T) -> T {
|
||||
value
|
||||
}
|
||||
|
||||
// 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 {
|
||||
left.unwrap_or_default().append_option(right)
|
||||
}
|
||||
|
||||
fn reduce_assign(&mut self, input: &AssignStatement, accesses: Vec<T>, value: T) -> T {
|
||||
T::default().append_all(accesses.into_iter()).append(value)
|
||||
}
|
||||
|
||||
fn reduce_block(&mut self, input: &BlockStatement, statements: Vec<T>) -> T {
|
||||
T::default().append_all(statements.into_iter())
|
||||
}
|
||||
|
||||
fn reduce_conditional_statement(
|
||||
&mut self,
|
||||
input: &ConditionalStatement,
|
||||
condition: T,
|
||||
if_true: T,
|
||||
if_false: Option<T>,
|
||||
) -> T {
|
||||
condition.append(if_true).append_option(if_false)
|
||||
}
|
||||
|
||||
fn reduce_formatted_string(&mut self, input: &FormattedString, parameters: Vec<T>) -> T {
|
||||
T::default().append_all(parameters.into_iter())
|
||||
}
|
||||
|
||||
fn reduce_console(&mut self, input: &ConsoleStatement, argument: T) -> T {
|
||||
argument
|
||||
}
|
||||
|
||||
fn reduce_definition(&mut self, input: &DefinitionStatement, value: T) -> T {
|
||||
value
|
||||
}
|
||||
|
||||
fn reduce_expression_statement(&mut self, input: &ExpressionStatement, expression: T) -> T {
|
||||
expression
|
||||
}
|
||||
|
||||
fn reduce_iteration(&mut self, input: &IterationStatement, start: T, stop: T, body: T) -> T {
|
||||
start.append(stop).append(body)
|
||||
}
|
||||
|
||||
fn reduce_return(&mut self, input: &ReturnStatement, value: T) -> T {
|
||||
value
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub trait MonoidalReducerProgram<T: Monoid>: MonoidalReducerStatement<T> {
|
||||
fn reduce_function(&mut self, input: &Arc<FunctionBody>, body: T) -> T {
|
||||
body
|
||||
}
|
||||
|
||||
fn reduce_circuit_member(&mut self, input: &CircuitMemberBody, function: Option<T>) -> T {
|
||||
function.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn reduce_circuit(&mut self, input: &Arc<CircuitBody>, members: Vec<T>) -> T {
|
||||
T::default().append_all(members.into_iter())
|
||||
}
|
||||
|
||||
fn reduce_program(
|
||||
&mut self,
|
||||
input: &InnerProgram,
|
||||
imported_modules: Vec<T>,
|
||||
test_functions: Vec<T>,
|
||||
functions: Vec<T>,
|
||||
circuits: Vec<T>,
|
||||
) -> T {
|
||||
T::default()
|
||||
.append_all(imported_modules.into_iter())
|
||||
.append_all(test_functions.into_iter())
|
||||
.append_all(functions.into_iter())
|
||||
.append_all(circuits.into_iter())
|
||||
}
|
||||
}
|
237
asg/src/scope.rs
Normal file
237
asg/src/scope.rs
Normal file
@ -0,0 +1,237 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, Circuit, Function, Input, Type, Variable};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use std::{cell::RefCell, sync::Arc};
|
||||
use uuid::Uuid;
|
||||
|
||||
/// An abstract data type that track the current bindings for variables, functions, and circuits.
|
||||
pub struct InnerScope {
|
||||
/// The unique id of the scope.
|
||||
pub id: Uuid,
|
||||
|
||||
/// The parent scope that this scope inherits.
|
||||
pub parent_scope: Option<Scope>,
|
||||
|
||||
/// The function definition that this scope occurs in.
|
||||
pub function: Option<Arc<Function>>,
|
||||
|
||||
/// The circuit definition that this scope occurs in.
|
||||
pub circuit_self: Option<Arc<Circuit>>,
|
||||
|
||||
/// Maps variable name => variable.
|
||||
pub variables: IndexMap<String, Variable>,
|
||||
|
||||
/// Maps function name => function.
|
||||
pub functions: IndexMap<String, Arc<Function>>,
|
||||
|
||||
/// Maps circuit name => circuit.
|
||||
pub circuits: IndexMap<String, Arc<Circuit>>,
|
||||
|
||||
/// The main input to the program.
|
||||
pub input: Option<Input>,
|
||||
}
|
||||
|
||||
pub type Scope = Arc<RefCell<InnerScope>>;
|
||||
|
||||
impl InnerScope {
|
||||
///
|
||||
/// 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 there is no parent scope, then `None` is returned.
|
||||
///
|
||||
pub fn resolve_variable(&self, name: &str) -> Option<Variable> {
|
||||
if let Some(resolved) = self.variables.get(name) {
|
||||
Some(resolved.clone())
|
||||
} else if let Some(resolved) = self.parent_scope.as_ref() {
|
||||
if let Some(resolved) = resolved.borrow().resolve_variable(name) {
|
||||
Some(resolved)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a reference to the current function.
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
pub fn resolve_current_function(&self) -> Option<Arc<Function>> {
|
||||
if let Some(resolved) = self.function.as_ref() {
|
||||
Some(resolved.clone())
|
||||
} else if let Some(resolved) = self.parent_scope.as_ref() {
|
||||
if let Some(resolved) = resolved.borrow().resolve_current_function() {
|
||||
Some(resolved)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a reference to the current input.
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
pub fn resolve_input(&self) -> Option<Input> {
|
||||
if let Some(input) = self.input.as_ref() {
|
||||
Some(input.clone())
|
||||
} else if let Some(resolved) = self.parent_scope.as_ref() {
|
||||
if let Some(resolved) = resolved.borrow().resolve_input() {
|
||||
Some(resolved)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a reference to the function corresponding to the name.
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
pub fn resolve_function(&self, name: &str) -> Option<Arc<Function>> {
|
||||
if let Some(resolved) = self.functions.get(name) {
|
||||
Some(resolved.clone())
|
||||
} else if let Some(resolved) = self.parent_scope.as_ref() {
|
||||
if let Some(resolved) = resolved.borrow().resolve_function(name) {
|
||||
Some(resolved)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a reference to the circuit corresponding to the name.
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
pub fn resolve_circuit(&self, name: &str) -> Option<Arc<Circuit>> {
|
||||
if let Some(resolved) = self.circuits.get(name) {
|
||||
Some(resolved.clone())
|
||||
} else if name == "Self" && self.circuit_self.is_some() {
|
||||
self.circuit_self.clone()
|
||||
} else if let Some(resolved) = self.parent_scope.as_ref() {
|
||||
if let Some(resolved) = resolved.borrow().resolve_circuit(name) {
|
||||
Some(resolved)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a reference to the current circuit.
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
pub fn resolve_circuit_self(&self) -> Option<Arc<Circuit>> {
|
||||
if let Some(resolved) = self.circuit_self.as_ref() {
|
||||
Some(resolved.clone())
|
||||
} else if let Some(resolved) = self.parent_scope.as_ref() {
|
||||
if let Some(resolved) = resolved.borrow().resolve_circuit_self() {
|
||||
Some(resolved)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns a new scope given a parent scope.
|
||||
///
|
||||
pub fn make_subscope(scope: &Scope) -> Scope {
|
||||
Arc::new(RefCell::new(InnerScope {
|
||||
id: Uuid::new_v4(),
|
||||
parent_scope: Some(scope.clone()),
|
||||
circuit_self: None,
|
||||
variables: IndexMap::new(),
|
||||
functions: IndexMap::new(),
|
||||
circuits: IndexMap::new(),
|
||||
function: None,
|
||||
input: None,
|
||||
}))
|
||||
}
|
||||
|
||||
///
|
||||
/// Returns the type returned by the current scope.
|
||||
///
|
||||
pub fn resolve_ast_type(&self, type_: &leo_ast::Type) -> Result<Type, AsgConvertError> {
|
||||
use leo_ast::Type::*;
|
||||
Ok(match type_ {
|
||||
Address => Type::Address,
|
||||
Boolean => Type::Boolean,
|
||||
Field => Type::Field,
|
||||
Group => Type::Group,
|
||||
IntegerType(int_type) => Type::Integer(int_type.clone()),
|
||||
Array(sub_type, dimensions) => {
|
||||
let mut item = Box::new(self.resolve_ast_type(&*sub_type)?);
|
||||
for dimension in dimensions.0.iter().rev() {
|
||||
let dimension = dimension
|
||||
.value
|
||||
.parse::<usize>()
|
||||
.map_err(|_| AsgConvertError::parse_index_error())?;
|
||||
item = Box::new(Type::Array(item, dimension));
|
||||
}
|
||||
*item
|
||||
}
|
||||
Tuple(sub_types) => Type::Tuple(
|
||||
sub_types
|
||||
.iter()
|
||||
.map(|x| self.resolve_ast_type(x))
|
||||
.collect::<Result<Vec<_>, AsgConvertError>>()?,
|
||||
),
|
||||
Circuit(name) if name.name == "Self" => Type::Circuit(
|
||||
self.circuit_self
|
||||
.clone()
|
||||
.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(
|
||||
self.circuit_self
|
||||
.clone()
|
||||
.ok_or_else(AsgConvertError::reference_self_outside_circuit)?,
|
||||
),
|
||||
})
|
||||
}
|
||||
}
|
267
asg/src/statement/assign.rs
Normal file
267
asg/src/statement/assign.rs
Normal file
@ -0,0 +1,267 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{
|
||||
AsgConvertError,
|
||||
CircuitMember,
|
||||
ConstInt,
|
||||
ConstValue,
|
||||
Expression,
|
||||
ExpressionNode,
|
||||
FromAst,
|
||||
Identifier,
|
||||
IntegerType,
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Statement,
|
||||
Type,
|
||||
Variable,
|
||||
};
|
||||
pub use leo_ast::AssignOperation;
|
||||
use leo_ast::AssigneeAccess as AstAssigneeAccess;
|
||||
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
pub enum AssignAccess {
|
||||
ArrayRange(Option<Arc<Expression>>, Option<Arc<Expression>>),
|
||||
ArrayIndex(Arc<Expression>),
|
||||
Tuple(usize),
|
||||
Member(Identifier),
|
||||
}
|
||||
|
||||
pub struct AssignStatement {
|
||||
pub parent: Option<Weak<Statement>>,
|
||||
pub span: Option<Span>,
|
||||
pub operation: AssignOperation,
|
||||
pub target_variable: Variable,
|
||||
pub target_accesses: Vec<AssignAccess>,
|
||||
pub value: Arc<Expression>,
|
||||
}
|
||||
|
||||
impl Node for AssignStatement {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::AssignStatement> for Arc<Statement> {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
statement: &leo_ast::AssignStatement,
|
||||
_expected_type: Option<PartialType>,
|
||||
) -> Result<Arc<Statement>, AsgConvertError> {
|
||||
let (name, span) = (&statement.assignee.identifier.name, &statement.assignee.identifier.span);
|
||||
|
||||
let variable = if name == "input" {
|
||||
if let Some(function) = scope.borrow().resolve_current_function() {
|
||||
if !function.has_input {
|
||||
return Err(AsgConvertError::unresolved_reference(name, span));
|
||||
}
|
||||
} else {
|
||||
return Err(AsgConvertError::unresolved_reference(name, span));
|
||||
}
|
||||
if let Some(input) = scope.borrow().resolve_input() {
|
||||
input.container
|
||||
} else {
|
||||
return Err(AsgConvertError::InternalError(
|
||||
"attempted to reference input when none is in scope".to_string(),
|
||||
));
|
||||
}
|
||||
} else {
|
||||
scope
|
||||
.borrow()
|
||||
.resolve_variable(&name)
|
||||
.ok_or_else(|| AsgConvertError::unresolved_reference(name, span))?
|
||||
};
|
||||
|
||||
if !variable.borrow().mutable {
|
||||
return Err(AsgConvertError::immutable_assignment(&name, &statement.span));
|
||||
}
|
||||
let mut target_type: Option<PartialType> = Some(variable.borrow().type_.clone().into());
|
||||
|
||||
let mut target_accesses = vec![];
|
||||
for access in statement.assignee.accesses.iter() {
|
||||
target_accesses.push(match access {
|
||||
AstAssigneeAccess::ArrayRange(left, right) => {
|
||||
let index_type = Some(PartialType::Integer(None, Some(IntegerType::U32)));
|
||||
let left = left
|
||||
.as_ref()
|
||||
.map(
|
||||
|left: &leo_ast::Expression| -> Result<Arc<Expression>, AsgConvertError> {
|
||||
Arc::<Expression>::from_ast(scope, left, index_type.clone())
|
||||
},
|
||||
)
|
||||
.transpose()?;
|
||||
let right = right
|
||||
.as_ref()
|
||||
.map(
|
||||
|right: &leo_ast::Expression| -> Result<Arc<Expression>, AsgConvertError> {
|
||||
Arc::<Expression>::from_ast(scope, right, index_type)
|
||||
},
|
||||
)
|
||||
.transpose()?;
|
||||
|
||||
match &target_type {
|
||||
Some(PartialType::Array(item, len)) => {
|
||||
if let (Some(left), Some(right)) = (
|
||||
left.as_ref()
|
||||
.map(|x| x.const_value())
|
||||
.unwrap_or_else(|| Some(ConstValue::Int(ConstInt::U32(0)))),
|
||||
right
|
||||
.as_ref()
|
||||
.map(|x| x.const_value())
|
||||
.unwrap_or_else(|| Some(ConstValue::Int(ConstInt::U32(len.map(|x| x as u32)?)))),
|
||||
) {
|
||||
let left = match left {
|
||||
ConstValue::Int(x) => x.to_usize().ok_or_else(|| {
|
||||
AsgConvertError::invalid_assign_index(&name, &x.to_string(), &statement.span)
|
||||
})?,
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
let right = match right {
|
||||
ConstValue::Int(x) => x.to_usize().ok_or_else(|| {
|
||||
AsgConvertError::invalid_assign_index(&name, &x.to_string(), &statement.span)
|
||||
})?,
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
if right >= left {
|
||||
target_type = Some(PartialType::Array(item.clone(), Some((right - left) as usize)))
|
||||
} else {
|
||||
return Err(AsgConvertError::invalid_backwards_assignment(
|
||||
&name,
|
||||
left,
|
||||
right,
|
||||
&statement.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => return Err(AsgConvertError::index_into_non_array(&name, &statement.span)),
|
||||
}
|
||||
|
||||
AssignAccess::ArrayRange(left, right)
|
||||
}
|
||||
AstAssigneeAccess::ArrayIndex(index) => {
|
||||
target_type = match target_type.clone() {
|
||||
Some(PartialType::Array(item, _)) => item.map(|x| *x),
|
||||
_ => return Err(AsgConvertError::index_into_non_array(&name, &statement.span)),
|
||||
};
|
||||
AssignAccess::ArrayIndex(Arc::<Expression>::from_ast(
|
||||
scope,
|
||||
index,
|
||||
Some(PartialType::Integer(None, Some(IntegerType::U32))),
|
||||
)?)
|
||||
}
|
||||
AstAssigneeAccess::Tuple(index, _) => {
|
||||
let index = index
|
||||
.value
|
||||
.parse::<usize>()
|
||||
.map_err(|_| AsgConvertError::parse_index_error())?;
|
||||
target_type = match target_type {
|
||||
Some(PartialType::Tuple(types)) => types
|
||||
.get(index)
|
||||
.cloned()
|
||||
.ok_or_else(|| AsgConvertError::tuple_index_out_of_bounds(index, &statement.span))?,
|
||||
_ => return Err(AsgConvertError::index_into_non_tuple(&name, &statement.span)),
|
||||
};
|
||||
AssignAccess::Tuple(index)
|
||||
}
|
||||
AstAssigneeAccess::Member(name) => {
|
||||
target_type = match target_type {
|
||||
Some(PartialType::Type(Type::Circuit(circuit))) => {
|
||||
let circuit = circuit;
|
||||
|
||||
let members = circuit.members.borrow();
|
||||
let member = members.get(&name.name).ok_or_else(|| {
|
||||
AsgConvertError::unresolved_circuit_member(
|
||||
&circuit.name.borrow().name,
|
||||
&name.name,
|
||||
&statement.span,
|
||||
)
|
||||
})?;
|
||||
|
||||
let x = match &member {
|
||||
CircuitMember::Variable(type_) => type_.clone(),
|
||||
CircuitMember::Function(_) => {
|
||||
return Err(AsgConvertError::illegal_function_assign(&name.name, &statement.span));
|
||||
}
|
||||
};
|
||||
Some(x.strong().partial())
|
||||
}
|
||||
_ => {
|
||||
return Err(AsgConvertError::index_into_non_tuple(
|
||||
&statement.assignee.identifier.name,
|
||||
&statement.span,
|
||||
));
|
||||
}
|
||||
};
|
||||
AssignAccess::Member(name.clone())
|
||||
}
|
||||
});
|
||||
}
|
||||
let value = Arc::<Expression>::from_ast(scope, &statement.value, target_type)?;
|
||||
|
||||
let statement = Arc::new(Statement::Assign(AssignStatement {
|
||||
parent: None,
|
||||
span: Some(statement.span.clone()),
|
||||
operation: statement.operation.clone(),
|
||||
target_variable: variable.clone(),
|
||||
target_accesses,
|
||||
value,
|
||||
}));
|
||||
|
||||
{
|
||||
let mut variable = variable.borrow_mut();
|
||||
variable.assignments.push(Arc::downgrade(&statement));
|
||||
}
|
||||
|
||||
Ok(statement)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::AssignStatement> for &AssignStatement {
|
||||
fn into(self) -> leo_ast::AssignStatement {
|
||||
leo_ast::AssignStatement {
|
||||
operation: self.operation.clone(),
|
||||
assignee: leo_ast::Assignee {
|
||||
identifier: self.target_variable.borrow().name.clone(),
|
||||
accesses: self
|
||||
.target_accesses
|
||||
.iter()
|
||||
.map(|access| match access {
|
||||
AssignAccess::ArrayRange(left, right) => AstAssigneeAccess::ArrayRange(
|
||||
left.as_ref().map(|e| e.as_ref().into()),
|
||||
right.as_ref().map(|e| e.as_ref().into()),
|
||||
),
|
||||
AssignAccess::ArrayIndex(index) => AstAssigneeAccess::ArrayIndex(index.as_ref().into()),
|
||||
AssignAccess::Tuple(index) => AstAssigneeAccess::Tuple(
|
||||
leo_ast::PositiveNumber {
|
||||
value: index.to_string(),
|
||||
},
|
||||
self.span.clone().unwrap_or_default(),
|
||||
),
|
||||
AssignAccess::Member(name) => AstAssigneeAccess::Member(name.clone()),
|
||||
})
|
||||
.collect(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
},
|
||||
value: self.value.as_ref().into(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
66
asg/src/statement/block.rs
Normal file
66
asg/src/statement/block.rs
Normal file
@ -0,0 +1,66 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, FromAst, InnerScope, Node, PartialType, Scope, Span, Statement};
|
||||
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
pub struct BlockStatement {
|
||||
pub parent: Option<Weak<Statement>>,
|
||||
pub span: Option<Span>,
|
||||
pub statements: Vec<Arc<Statement>>,
|
||||
pub scope: Scope,
|
||||
}
|
||||
|
||||
impl Node for BlockStatement {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::Block> for BlockStatement {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
statement: &leo_ast::Block,
|
||||
_expected_type: Option<PartialType>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
let new_scope = InnerScope::make_subscope(scope);
|
||||
|
||||
let mut output = vec![];
|
||||
for item in statement.statements.iter() {
|
||||
output.push(Arc::<Statement>::from_ast(&new_scope, item, None)?);
|
||||
}
|
||||
Ok(BlockStatement {
|
||||
parent: None,
|
||||
span: Some(statement.span.clone()),
|
||||
statements: output,
|
||||
scope: new_scope,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::Block> for &BlockStatement {
|
||||
fn into(self) -> leo_ast::Block {
|
||||
leo_ast::Block {
|
||||
statements: self
|
||||
.statements
|
||||
.iter()
|
||||
.map(|statement| statement.as_ref().into())
|
||||
.collect(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
75
asg/src/statement/conditional.rs
Normal file
75
asg/src/statement/conditional.rs
Normal file
@ -0,0 +1,75 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, BlockStatement, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type};
|
||||
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
pub struct ConditionalStatement {
|
||||
pub parent: Option<Weak<Statement>>,
|
||||
pub span: Option<Span>,
|
||||
pub condition: Arc<Expression>,
|
||||
pub result: Arc<Statement>,
|
||||
pub next: Option<Arc<Statement>>,
|
||||
}
|
||||
|
||||
impl Node for ConditionalStatement {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::ConditionalStatement> for ConditionalStatement {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
statement: &leo_ast::ConditionalStatement,
|
||||
_expected_type: Option<PartialType>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
let condition = Arc::<Expression>::from_ast(scope, &statement.condition, Some(Type::Boolean.into()))?;
|
||||
let result = Arc::new(Statement::Block(BlockStatement::from_ast(
|
||||
scope,
|
||||
&statement.block,
|
||||
None,
|
||||
)?));
|
||||
let next = statement
|
||||
.next
|
||||
.as_deref()
|
||||
.map(|next| -> Result<Arc<Statement>, AsgConvertError> { Arc::<Statement>::from_ast(scope, next, None) })
|
||||
.transpose()?;
|
||||
|
||||
Ok(ConditionalStatement {
|
||||
parent: None,
|
||||
span: Some(statement.span.clone()),
|
||||
condition,
|
||||
result,
|
||||
next,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::ConditionalStatement> for &ConditionalStatement {
|
||||
fn into(self) -> leo_ast::ConditionalStatement {
|
||||
leo_ast::ConditionalStatement {
|
||||
condition: self.condition.as_ref().into(),
|
||||
block: match self.result.as_ref() {
|
||||
Statement::Block(block) => block.into(),
|
||||
_ => unimplemented!(),
|
||||
},
|
||||
next: self.next.as_deref().map(|e| Box::new(e.into())),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
133
asg/src/statement/console.rs
Normal file
133
asg/src/statement/console.rs
Normal file
@ -0,0 +1,133 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type};
|
||||
use leo_ast::ConsoleFunction as AstConsoleFunction;
|
||||
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
//todo: refactor to not require/depend on span
|
||||
pub struct FormattedString {
|
||||
pub string: String,
|
||||
pub containers: Vec<Span>,
|
||||
pub parameters: Vec<Arc<Expression>>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
pub enum ConsoleFunction {
|
||||
Assert(Arc<Expression>),
|
||||
Debug(FormattedString),
|
||||
Error(FormattedString),
|
||||
Log(FormattedString),
|
||||
}
|
||||
|
||||
pub struct ConsoleStatement {
|
||||
pub parent: Option<Weak<Statement>>,
|
||||
pub span: Option<Span>,
|
||||
pub function: ConsoleFunction,
|
||||
}
|
||||
|
||||
impl Node for ConsoleStatement {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::FormattedString> for FormattedString {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::FormattedString,
|
||||
_expected_type: Option<PartialType>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
if value.parameters.len() != value.containers.len() {
|
||||
// + 1 for formatting string as to not confuse user
|
||||
return Err(AsgConvertError::unexpected_call_argument_count(
|
||||
value.containers.len() + 1,
|
||||
value.parameters.len() + 1,
|
||||
&value.span,
|
||||
));
|
||||
}
|
||||
let mut parameters = vec![];
|
||||
for parameter in value.parameters.iter() {
|
||||
parameters.push(Arc::<Expression>::from_ast(scope, parameter, None)?);
|
||||
}
|
||||
Ok(FormattedString {
|
||||
string: value.string.clone(),
|
||||
containers: value.containers.iter().map(|x| x.span.clone()).collect(),
|
||||
parameters,
|
||||
span: value.span.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::FormattedString> for &FormattedString {
|
||||
fn into(self) -> leo_ast::FormattedString {
|
||||
leo_ast::FormattedString {
|
||||
string: self.string.clone(),
|
||||
containers: self
|
||||
.containers
|
||||
.iter()
|
||||
.map(|span| leo_ast::FormattedContainer { span: span.clone() })
|
||||
.collect(),
|
||||
parameters: self.parameters.iter().map(|e| e.as_ref().into()).collect(),
|
||||
span: self.span.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::ConsoleStatement> for ConsoleStatement {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
statement: &leo_ast::ConsoleStatement,
|
||||
_expected_type: Option<PartialType>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
Ok(ConsoleStatement {
|
||||
parent: None,
|
||||
span: Some(statement.span.clone()),
|
||||
function: match &statement.function {
|
||||
AstConsoleFunction::Assert(expression) => ConsoleFunction::Assert(Arc::<Expression>::from_ast(
|
||||
scope,
|
||||
expression,
|
||||
Some(Type::Boolean.into()),
|
||||
)?),
|
||||
AstConsoleFunction::Debug(formatted_string) => {
|
||||
ConsoleFunction::Debug(FormattedString::from_ast(scope, formatted_string, None)?)
|
||||
}
|
||||
AstConsoleFunction::Error(formatted_string) => {
|
||||
ConsoleFunction::Error(FormattedString::from_ast(scope, formatted_string, None)?)
|
||||
}
|
||||
AstConsoleFunction::Log(formatted_string) => {
|
||||
ConsoleFunction::Log(FormattedString::from_ast(scope, formatted_string, None)?)
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::ConsoleStatement> for &ConsoleStatement {
|
||||
fn into(self) -> leo_ast::ConsoleStatement {
|
||||
use ConsoleFunction::*;
|
||||
leo_ast::ConsoleStatement {
|
||||
function: match &self.function {
|
||||
Assert(e) => AstConsoleFunction::Assert(e.as_ref().into()),
|
||||
Debug(formatted_string) => AstConsoleFunction::Debug(formatted_string.into()),
|
||||
Error(formatted_string) => AstConsoleFunction::Error(formatted_string.into()),
|
||||
Log(formatted_string) => AstConsoleFunction::Log(formatted_string.into()),
|
||||
},
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
159
asg/src/statement/definition.rs
Normal file
159
asg/src/statement/definition.rs
Normal file
@ -0,0 +1,159 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{
|
||||
AsgConvertError,
|
||||
Expression,
|
||||
ExpressionNode,
|
||||
FromAst,
|
||||
InnerVariable,
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Statement,
|
||||
Type,
|
||||
Variable,
|
||||
};
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct DefinitionStatement {
|
||||
pub parent: Option<Weak<Statement>>,
|
||||
pub span: Option<Span>,
|
||||
pub variables: Vec<Variable>,
|
||||
pub value: Arc<Expression>,
|
||||
}
|
||||
|
||||
impl Node for DefinitionStatement {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::DefinitionStatement> for Arc<Statement> {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
statement: &leo_ast::DefinitionStatement,
|
||||
_expected_type: Option<PartialType>,
|
||||
) -> Result<Arc<Statement>, AsgConvertError> {
|
||||
let type_ = statement
|
||||
.type_
|
||||
.as_ref()
|
||||
.map(|x| scope.borrow().resolve_ast_type(&x))
|
||||
.transpose()?;
|
||||
|
||||
let value = Arc::<Expression>::from_ast(scope, &statement.value, type_.clone().map(Into::into))?;
|
||||
|
||||
let type_ = type_.or_else(|| value.get_type());
|
||||
|
||||
let mut output_types = vec![];
|
||||
|
||||
let mut variables = vec![];
|
||||
if statement.variable_names.is_empty() {
|
||||
return Err(AsgConvertError::illegal_ast_structure(
|
||||
"cannot have 0 variable names in destructuring tuple",
|
||||
));
|
||||
}
|
||||
if statement.variable_names.len() == 1 {
|
||||
// any return type is fine
|
||||
output_types.push(type_);
|
||||
} else {
|
||||
// tuple destructure
|
||||
match type_.as_ref() {
|
||||
Some(Type::Tuple(sub_types)) if sub_types.len() == statement.variable_names.len() => {
|
||||
output_types.extend(sub_types.clone().into_iter().map(Some).collect::<Vec<_>>());
|
||||
}
|
||||
type_ => {
|
||||
return Err(AsgConvertError::unexpected_type(
|
||||
&*format!("{}-ary tuple", statement.variable_names.len()),
|
||||
type_.map(|x| x.to_string()).as_deref(),
|
||||
&statement.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (variable, type_) in statement.variable_names.iter().zip(output_types.into_iter()) {
|
||||
if statement.declaration_type == leo_ast::Declare::Const && variable.mutable {
|
||||
return Err(AsgConvertError::illegal_ast_structure("cannot have const mut"));
|
||||
}
|
||||
variables.push(Arc::new(RefCell::new(InnerVariable {
|
||||
id: uuid::Uuid::new_v4(),
|
||||
name: variable.identifier.clone(),
|
||||
type_: type_
|
||||
.ok_or_else(|| AsgConvertError::unresolved_type(&variable.identifier.name, &statement.span))?,
|
||||
mutable: variable.mutable,
|
||||
declaration: crate::VariableDeclaration::Definition,
|
||||
references: vec![],
|
||||
assignments: vec![],
|
||||
})));
|
||||
}
|
||||
|
||||
{
|
||||
let mut scope_borrow = scope.borrow_mut();
|
||||
for variable in variables.iter() {
|
||||
scope_borrow
|
||||
.variables
|
||||
.insert(variable.borrow().name.name.clone(), variable.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let statement = Arc::new(Statement::Definition(DefinitionStatement {
|
||||
parent: None,
|
||||
span: Some(statement.span.clone()),
|
||||
variables: variables.clone(),
|
||||
value,
|
||||
}));
|
||||
|
||||
variables.iter().for_each(|variable| {
|
||||
variable.borrow_mut().assignments.push(Arc::downgrade(&statement));
|
||||
});
|
||||
|
||||
Ok(statement)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::DefinitionStatement> for &DefinitionStatement {
|
||||
fn into(self) -> leo_ast::DefinitionStatement {
|
||||
assert!(!self.variables.is_empty());
|
||||
|
||||
let mut variable_names = vec![];
|
||||
let mut type_ = None::<leo_ast::Type>;
|
||||
for variable in self.variables.iter() {
|
||||
let variable = variable.borrow();
|
||||
variable_names.push(leo_ast::VariableName {
|
||||
mutable: variable.mutable,
|
||||
identifier: variable.name.clone(),
|
||||
span: variable.name.span.clone(),
|
||||
});
|
||||
if type_.is_none() {
|
||||
type_ = Some((&variable.type_).into());
|
||||
}
|
||||
}
|
||||
|
||||
leo_ast::DefinitionStatement {
|
||||
declaration_type: leo_ast::Declare::Let,
|
||||
variable_names,
|
||||
type_,
|
||||
value: self.value.as_ref().into(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
56
asg/src/statement/expression.rs
Normal file
56
asg/src/statement/expression.rs
Normal file
@ -0,0 +1,56 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement};
|
||||
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
pub struct ExpressionStatement {
|
||||
pub parent: Option<Weak<Statement>>,
|
||||
pub span: Option<Span>,
|
||||
pub expression: Arc<Expression>,
|
||||
}
|
||||
|
||||
impl Node for ExpressionStatement {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::ExpressionStatement> for ExpressionStatement {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
statement: &leo_ast::ExpressionStatement,
|
||||
_expected_type: Option<PartialType>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
let expression = Arc::<Expression>::from_ast(scope, &statement.expression, None)?;
|
||||
|
||||
Ok(ExpressionStatement {
|
||||
parent: None,
|
||||
span: Some(statement.span.clone()),
|
||||
expression,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::ExpressionStatement> for &ExpressionStatement {
|
||||
fn into(self) -> leo_ast::ExpressionStatement {
|
||||
leo_ast::ExpressionStatement {
|
||||
expression: self.expression.as_ref().into(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
108
asg/src/statement/iteration.rs
Normal file
108
asg/src/statement/iteration.rs
Normal file
@ -0,0 +1,108 @@
|
||||
// Copyright (C) 2019-2020 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 leo_ast::IntegerType;
|
||||
|
||||
use crate::{
|
||||
AsgConvertError,
|
||||
Expression,
|
||||
ExpressionNode,
|
||||
FromAst,
|
||||
InnerVariable,
|
||||
Node,
|
||||
PartialType,
|
||||
Scope,
|
||||
Span,
|
||||
Statement,
|
||||
Variable,
|
||||
};
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
pub struct IterationStatement {
|
||||
pub parent: Option<Weak<Statement>>,
|
||||
pub span: Option<Span>,
|
||||
pub variable: Variable,
|
||||
pub start: Arc<Expression>,
|
||||
pub stop: Arc<Expression>,
|
||||
pub body: Arc<Statement>,
|
||||
}
|
||||
|
||||
impl Node for IterationStatement {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::IterationStatement> for Arc<Statement> {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
statement: &leo_ast::IterationStatement,
|
||||
_expected_type: Option<PartialType>,
|
||||
) -> Result<Arc<Statement>, AsgConvertError> {
|
||||
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 stop = Arc::<Expression>::from_ast(scope, &statement.stop, expected_index_type)?;
|
||||
let variable = Arc::new(RefCell::new(InnerVariable {
|
||||
id: uuid::Uuid::new_v4(),
|
||||
name: statement.variable.clone(),
|
||||
type_: start
|
||||
.get_type()
|
||||
.ok_or_else(|| AsgConvertError::unresolved_type(&statement.variable.name, &statement.span))?,
|
||||
mutable: false,
|
||||
declaration: crate::VariableDeclaration::IterationDefinition,
|
||||
references: vec![],
|
||||
assignments: vec![],
|
||||
}));
|
||||
scope
|
||||
.borrow_mut()
|
||||
.variables
|
||||
.insert(statement.variable.name.clone(), variable.clone());
|
||||
|
||||
let statement = Arc::new(Statement::Iteration(IterationStatement {
|
||||
parent: None,
|
||||
span: Some(statement.span.clone()),
|
||||
variable: variable.clone(),
|
||||
stop,
|
||||
start,
|
||||
body: Arc::new(Statement::Block(crate::BlockStatement::from_ast(
|
||||
scope,
|
||||
&statement.block,
|
||||
None,
|
||||
)?)),
|
||||
}));
|
||||
variable.borrow_mut().assignments.push(Arc::downgrade(&statement));
|
||||
Ok(statement)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::IterationStatement> for &IterationStatement {
|
||||
fn into(self) -> leo_ast::IterationStatement {
|
||||
leo_ast::IterationStatement {
|
||||
variable: self.variable.borrow().name.clone(),
|
||||
start: self.start.as_ref().into(),
|
||||
stop: self.stop.as_ref().into(),
|
||||
block: match self.body.as_ref() {
|
||||
Statement::Block(block) => block.into(),
|
||||
_ => unimplemented!(),
|
||||
},
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
114
asg/src/statement/mod.rs
Normal file
114
asg/src/statement/mod.rs
Normal file
@ -0,0 +1,114 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
//! This module defines a statement node in an asg.
|
||||
//!
|
||||
//! Ast statement nodes can be directly converted into asg nodes with no major differences.
|
||||
|
||||
mod assign;
|
||||
pub use assign::*;
|
||||
|
||||
mod block;
|
||||
pub use block::*;
|
||||
|
||||
mod conditional;
|
||||
pub use conditional::*;
|
||||
|
||||
mod console;
|
||||
pub use console::*;
|
||||
|
||||
mod definition;
|
||||
pub use definition::*;
|
||||
|
||||
mod expression;
|
||||
pub use expression::*;
|
||||
|
||||
mod iteration;
|
||||
pub use iteration::*;
|
||||
|
||||
mod return_;
|
||||
pub use return_::*;
|
||||
|
||||
use crate::{AsgConvertError, FromAst, Node, PartialType, Scope, Span};
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
pub enum Statement {
|
||||
Return(ReturnStatement),
|
||||
Definition(DefinitionStatement),
|
||||
Assign(AssignStatement),
|
||||
Conditional(ConditionalStatement),
|
||||
Iteration(IterationStatement),
|
||||
Console(ConsoleStatement),
|
||||
Expression(ExpressionStatement),
|
||||
Block(BlockStatement),
|
||||
}
|
||||
|
||||
impl Node for Statement {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
use Statement::*;
|
||||
match self {
|
||||
Return(s) => s.span(),
|
||||
Definition(s) => s.span(),
|
||||
Assign(s) => s.span(),
|
||||
Conditional(s) => s.span(),
|
||||
Iteration(s) => s.span(),
|
||||
Console(s) => s.span(),
|
||||
Expression(s) => s.span(),
|
||||
Block(s) => s.span(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::Statement> for Arc<Statement> {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
value: &leo_ast::Statement,
|
||||
_expected_type: Option<PartialType>,
|
||||
) -> Result<Arc<Statement>, AsgConvertError> {
|
||||
use leo_ast::Statement::*;
|
||||
Ok(match value {
|
||||
Return(statement) => Arc::new(Statement::Return(ReturnStatement::from_ast(scope, statement, None)?)),
|
||||
Definition(statement) => Arc::<Statement>::from_ast(scope, statement, None)?,
|
||||
Assign(statement) => Arc::<Statement>::from_ast(scope, statement, None)?,
|
||||
Conditional(statement) => Arc::new(Statement::Conditional(ConditionalStatement::from_ast(
|
||||
scope, statement, None,
|
||||
)?)),
|
||||
Iteration(statement) => Arc::<Statement>::from_ast(scope, statement, None)?,
|
||||
Console(statement) => Arc::new(Statement::Console(ConsoleStatement::from_ast(scope, statement, None)?)),
|
||||
Expression(statement) => Arc::new(Statement::Expression(ExpressionStatement::from_ast(
|
||||
scope, statement, None,
|
||||
)?)),
|
||||
Block(statement) => Arc::new(Statement::Block(BlockStatement::from_ast(scope, statement, None)?)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::Statement> for &Statement {
|
||||
fn into(self) -> leo_ast::Statement {
|
||||
use Statement::*;
|
||||
match self {
|
||||
Return(statement) => leo_ast::Statement::Return(statement.into()),
|
||||
Definition(statement) => leo_ast::Statement::Definition(statement.into()),
|
||||
Assign(statement) => leo_ast::Statement::Assign(statement.into()),
|
||||
Conditional(statement) => leo_ast::Statement::Conditional(statement.into()),
|
||||
Iteration(statement) => leo_ast::Statement::Iteration(statement.into()),
|
||||
Console(statement) => leo_ast::Statement::Console(statement.into()),
|
||||
Expression(statement) => leo_ast::Statement::Expression(statement.into()),
|
||||
Block(statement) => leo_ast::Statement::Block(statement.into()),
|
||||
}
|
||||
}
|
||||
}
|
59
asg/src/statement/return_.rs
Normal file
59
asg/src/statement/return_.rs
Normal file
@ -0,0 +1,59 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{AsgConvertError, Expression, FromAst, Node, PartialType, Scope, Span, Statement, Type};
|
||||
|
||||
use std::sync::{Arc, Weak};
|
||||
|
||||
pub struct ReturnStatement {
|
||||
pub parent: Option<Weak<Statement>>,
|
||||
pub span: Option<Span>,
|
||||
pub expression: Arc<Expression>,
|
||||
}
|
||||
|
||||
impl Node for ReturnStatement {
|
||||
fn span(&self) -> Option<&Span> {
|
||||
self.span.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromAst<leo_ast::ReturnStatement> for ReturnStatement {
|
||||
fn from_ast(
|
||||
scope: &Scope,
|
||||
statement: &leo_ast::ReturnStatement,
|
||||
_expected_type: Option<PartialType>,
|
||||
) -> Result<Self, AsgConvertError> {
|
||||
let return_type: Option<Type> = scope
|
||||
.borrow()
|
||||
.resolve_current_function()
|
||||
.map(|x| x.output.clone())
|
||||
.map(Into::into);
|
||||
Ok(ReturnStatement {
|
||||
parent: None,
|
||||
span: Some(statement.span.clone()),
|
||||
expression: Arc::<Expression>::from_ast(scope, &statement.expression, return_type.map(Into::into))?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::ReturnStatement> for &ReturnStatement {
|
||||
fn into(self) -> leo_ast::ReturnStatement {
|
||||
leo_ast::ReturnStatement {
|
||||
expression: self.expression.as_ref().into(),
|
||||
span: self.span.clone().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
246
asg/src/type_.rs
Normal file
246
asg/src/type_.rs
Normal file
@ -0,0 +1,246 @@
|
||||
// Copyright (C) 2019-2020 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 crate::Circuit;
|
||||
pub use leo_ast::IntegerType;
|
||||
|
||||
use std::{
|
||||
fmt,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
/// A type in an asg.
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum Type {
|
||||
// Data types
|
||||
Address,
|
||||
Boolean,
|
||||
Field,
|
||||
Group,
|
||||
Integer(IntegerType),
|
||||
|
||||
// Data type wrappers
|
||||
Array(Box<Type>, usize),
|
||||
Tuple(Vec<Type>),
|
||||
Circuit(Arc<Circuit>),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum WeakType {
|
||||
Type(Type), // circuit not allowed
|
||||
Circuit(Weak<Circuit>),
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum PartialType {
|
||||
Type(Type), // non-array or tuple
|
||||
Integer(Option<IntegerType>, Option<IntegerType>), // specific, context-specific
|
||||
Array(Option<Box<PartialType>>, Option<usize>),
|
||||
Tuple(Vec<Option<PartialType>>),
|
||||
}
|
||||
|
||||
impl Into<Type> for WeakType {
|
||||
fn into(self) -> Type {
|
||||
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 {
|
||||
PartialType::Type(t) => Some(t),
|
||||
PartialType::Integer(sub_type, contextual_type) => Some(Type::Integer(sub_type.or(contextual_type)?)),
|
||||
PartialType::Array(element, len) => Some(Type::Array(Box::new((*element?).full()?), len?)),
|
||||
PartialType::Tuple(sub_types) => Some(Type::Tuple(
|
||||
sub_types
|
||||
.into_iter()
|
||||
.map(|x| x.map(|x| x.full()).flatten())
|
||||
.collect::<Option<Vec<Type>>>()?,
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialType {
|
||||
pub fn full(self) -> Option<Type> {
|
||||
self.into()
|
||||
}
|
||||
|
||||
pub fn matches(&self, other: &Type) -> bool {
|
||||
match (self, other) {
|
||||
(PartialType::Type(t), other) => t.is_assignable_from(other),
|
||||
(PartialType::Integer(self_sub_type, _), Type::Integer(sub_type)) => {
|
||||
self_sub_type.as_ref().map(|x| x == sub_type).unwrap_or(true)
|
||||
}
|
||||
(PartialType::Array(element, len), Type::Array(other_element, other_len)) => {
|
||||
if let Some(element) = element {
|
||||
if !element.matches(&*other_element) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if let Some(len) = len {
|
||||
return len == other_len;
|
||||
}
|
||||
true
|
||||
}
|
||||
(PartialType::Tuple(sub_types), Type::Tuple(other_sub_types)) => {
|
||||
// we dont enforce exact length for tuples here (relying on prior type checking) to allow for full-context-free tuple indexing
|
||||
if sub_types.len() > other_sub_types.len() {
|
||||
return false;
|
||||
}
|
||||
for (sub_type, other_sub_type) in sub_types.iter().zip(other_sub_types.iter()) {
|
||||
if let Some(sub_type) = sub_type {
|
||||
if !sub_type.matches(other_sub_type) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<PartialType> for Type {
|
||||
fn into(self) -> PartialType {
|
||||
match self {
|
||||
Type::Integer(sub_type) => PartialType::Integer(Some(sub_type), None),
|
||||
Type::Array(element, len) => PartialType::Array(Some(Box::new((*element).into())), Some(len)),
|
||||
Type::Tuple(sub_types) => PartialType::Tuple(sub_types.into_iter().map(Into::into).map(Some).collect()),
|
||||
x => PartialType::Type(x),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Type {
|
||||
pub fn is_assignable_from(&self, from: &Type) -> bool {
|
||||
self == from
|
||||
}
|
||||
|
||||
pub fn partial(self) -> PartialType {
|
||||
self.into()
|
||||
}
|
||||
|
||||
pub fn is_unit(&self) -> bool {
|
||||
matches!(self, Type::Tuple(t) if t.is_empty())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Type {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Type::Address => write!(f, "address"),
|
||||
Type::Boolean => write!(f, "bool"),
|
||||
Type::Field => write!(f, "field"),
|
||||
Type::Group => write!(f, "group"),
|
||||
Type::Integer(sub_type) => sub_type.fmt(f),
|
||||
Type::Array(sub_type, len) => write!(f, "[{}; {}]", sub_type, len),
|
||||
Type::Tuple(sub_types) => {
|
||||
write!(f, "(")?;
|
||||
for (i, sub_type) in sub_types.iter().enumerate() {
|
||||
write!(f, "{}", sub_type)?;
|
||||
if i < sub_types.len() - 1 {
|
||||
write!(f, ", ")?;
|
||||
}
|
||||
}
|
||||
write!(f, ")")
|
||||
}
|
||||
Type::Circuit(circuit) => write!(f, "{}", &circuit.name.borrow().name),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for PartialType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
PartialType::Type(t) => t.fmt(f),
|
||||
PartialType::Integer(Some(sub_type), _) => write!(f, "{}", sub_type),
|
||||
PartialType::Integer(_, Some(sub_type)) => write!(f, "<{}>", sub_type),
|
||||
PartialType::Integer(_, _) => write!(f, "integer"),
|
||||
PartialType::Array(sub_type, len) => {
|
||||
write!(f, "[")?;
|
||||
if let Some(sub_type) = sub_type {
|
||||
write!(f, "{}", *sub_type)?;
|
||||
} else {
|
||||
write!(f, "?")?;
|
||||
}
|
||||
write!(f, "; ")?;
|
||||
if let Some(len) = len {
|
||||
write!(f, "{}", len)?;
|
||||
} else {
|
||||
write!(f, "?")?;
|
||||
}
|
||||
write!(f, "]")
|
||||
}
|
||||
PartialType::Tuple(sub_types) => {
|
||||
write!(f, "(")?;
|
||||
for (i, sub_type) in sub_types.iter().enumerate() {
|
||||
if let Some(sub_type) = sub_type {
|
||||
write!(f, "{}", *sub_type)?;
|
||||
} else {
|
||||
write!(f, "?")?;
|
||||
}
|
||||
if i < sub_types.len() - 1 {
|
||||
write!(f, ", ")?;
|
||||
}
|
||||
}
|
||||
write!(f, ")")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<leo_ast::Type> for &Type {
|
||||
fn into(self) -> leo_ast::Type {
|
||||
use Type::*;
|
||||
match self {
|
||||
Address => leo_ast::Type::Address,
|
||||
Boolean => leo_ast::Type::Boolean,
|
||||
Field => leo_ast::Type::Field,
|
||||
Group => leo_ast::Type::Group,
|
||||
Integer(int_type) => leo_ast::Type::IntegerType(int_type.clone()),
|
||||
Array(type_, len) => leo_ast::Type::Array(
|
||||
Box::new(type_.as_ref().into()),
|
||||
leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber { value: len.to_string() }]),
|
||||
),
|
||||
Tuple(subtypes) => leo_ast::Type::Tuple(subtypes.iter().map(Into::into).collect()),
|
||||
Circuit(circuit) => leo_ast::Type::Circuit(circuit.name.borrow().clone()),
|
||||
}
|
||||
}
|
||||
}
|
47
asg/src/variable.rs
Normal file
47
asg/src/variable.rs
Normal file
@ -0,0 +1,47 @@
|
||||
// Copyright (C) 2019-2020 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 crate::{Expression, Statement, Type};
|
||||
use leo_ast::Identifier;
|
||||
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Specifies how a program variable was declared.
|
||||
#[derive(PartialEq)]
|
||||
pub enum VariableDeclaration {
|
||||
Definition,
|
||||
IterationDefinition,
|
||||
Parameter,
|
||||
Input,
|
||||
}
|
||||
|
||||
/// Stores information on a program variable.
|
||||
pub struct InnerVariable {
|
||||
pub id: Uuid,
|
||||
pub name: Identifier,
|
||||
pub type_: Type,
|
||||
pub mutable: bool,
|
||||
pub declaration: VariableDeclaration,
|
||||
pub references: Vec<Weak<Expression>>, // 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 type Variable = Arc<RefCell<InnerVariable>>;
|
||||
pub type WeakVariable = Weak<RefCell<InnerVariable>>;
|
3
asg/tests/fail/address/implicit_invalid.leo
Normal file
3
asg/tests/fail/address/implicit_invalid.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
let public_key_string: address = zleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8;
|
||||
}
|
39
asg/tests/fail/address/mod.rs
Normal file
39
asg/tests/fail/address/mod.rs
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
// Copyright (C) 2019-2020 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 crate::load_asg;
|
||||
|
||||
#[test]
|
||||
fn test_implicit_invalid() {
|
||||
let program_string = include_str!("implicit_invalid.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
3
asg/tests/fail/array/initializer_fail.leo
Normal file
3
asg/tests/fail/array/initializer_fail.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main(a: [u8; 3]) {
|
||||
console.assert(a == [1u8; -3]);
|
||||
}
|
3
asg/tests/fail/array/input_nested_3x2_fail.leo
Normal file
3
asg/tests/fail/array/input_nested_3x2_fail.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main(a: [u8; (3, 2)]) {
|
||||
console.assert(a == [[0u8; 2]; 3)]); // This should be written the right way as this test is for the input file.
|
||||
}
|
3
asg/tests/fail/array/input_tuple_3x2_fail.leo
Normal file
3
asg/tests/fail/array/input_tuple_3x2_fail.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main(a: [u8; (3, 2)]) {
|
||||
console.assert(a == [0u8; (2, 3)]);
|
||||
}
|
151
asg/tests/fail/array/mod.rs
Normal file
151
asg/tests/fail/array/mod.rs
Normal file
@ -0,0 +1,151 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
// Copyright (C) 2019-2020 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 crate::load_asg;
|
||||
|
||||
// Expressions
|
||||
|
||||
#[test]
|
||||
fn test_initializer_fail() {
|
||||
let program_string = include_str!("initializer_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_input_nested_3x2_fail() {
|
||||
let program_string = include_str!("input_nested_3x2_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_input_tuple_3x2_fail() {
|
||||
let program_string = include_str!("input_tuple_3x2_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multi_fail_initializer() {
|
||||
let program_string = include_str!("multi_fail_initializer.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multi_inline_fail() {
|
||||
let program_string = include_str!("multi_fail_inline.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multi_initializer_fail() {
|
||||
let program_string = include_str!("multi_initializer_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nested_3x2_value_fail() {
|
||||
let program_string = include_str!("nested_3x2_value_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tuple_3x2_value_fail() {
|
||||
let program_string = include_str!("tuple_3x2_value_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
// Array type tests
|
||||
|
||||
#[test]
|
||||
fn test_type_fail() {
|
||||
let program_string = include_str!("type_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_nested_value_nested_3x2_fail() {
|
||||
let program_string = include_str!("type_nested_value_nested_3x2_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_nested_value_nested_4x3x2_fail() {
|
||||
let program_string = include_str!("type_nested_value_nested_4x3x2_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_nested_value_tuple_3x2_fail() {
|
||||
let program_string = include_str!("type_nested_value_tuple_3x2_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_nested_value_tuple_4x3x2_fail() {
|
||||
let program_string = include_str!("type_nested_value_tuple_4x3x2_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_tuple_value_nested_3x2_fail() {
|
||||
let program_string = include_str!("type_tuple_value_nested_3x2_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_tuple_value_nested_3x2_swap_fail() {
|
||||
let program_string = include_str!("type_tuple_value_nested_3x2_swap_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_tuple_value_nested_4x3x2_fail() {
|
||||
let program_string = include_str!("type_tuple_value_nested_4x3x2_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_tuple_value_tuple_3x2_fail() {
|
||||
let program_string = include_str!("type_tuple_value_tuple_3x2_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_tuple_value_tuple_3x2_swap_fail() {
|
||||
let program_string = include_str!("type_tuple_value_tuple_3x2_swap_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_type_tuple_value_tuple_4x3x2_fail() {
|
||||
let program_string = include_str!("type_tuple_value_tuple_4x3x2_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
3
asg/tests/fail/array/multi_fail_initializer.leo
Normal file
3
asg/tests/fail/array/multi_fail_initializer.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
let arr: [u8; (2, 2)] = [[1u8; 2]; 1]; // incorrect dimensions
|
||||
}
|
4
asg/tests/fail/array/multi_fail_inline.leo
Normal file
4
asg/tests/fail/array/multi_fail_inline.leo
Normal file
@ -0,0 +1,4 @@
|
||||
function main() {
|
||||
let arr: [u8; (2, 2)] = [[1u8, 1u8],
|
||||
[1u8]]; // incorrect dimensions
|
||||
}
|
3
asg/tests/fail/array/multi_initializer_fail.leo
Normal file
3
asg/tests/fail/array/multi_initializer_fail.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
let arr: [u8; (2, 2)] = [1u8; (2, 1)]; // incorrect dimensions
|
||||
}
|
4
asg/tests/fail/array/nested_3x2_value_fail.leo
Normal file
4
asg/tests/fail/array/nested_3x2_value_fail.leo
Normal file
@ -0,0 +1,4 @@
|
||||
// Multidimensional array syntax in leo
|
||||
function main() {
|
||||
const a: [u32; (3, 2)] = [[0; 3]; 2]; // initializer (incorrectly reversed ordering)
|
||||
}
|
4
asg/tests/fail/array/tuple_3x2_value_fail.leo
Normal file
4
asg/tests/fail/array/tuple_3x2_value_fail.leo
Normal file
@ -0,0 +1,4 @@
|
||||
// Multidimensional array syntax in leo
|
||||
function main() {
|
||||
const a: [u32; (3, 2)] = [0; (2, 3)]; // initializer (incorrectly reversed ordering)
|
||||
}
|
3
asg/tests/fail/array/type_fail.leo
Normal file
3
asg/tests/fail/array/type_fail.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
let a: [u8; -2] = [0u32; 2];
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
const b: [[u8; 2]; 3] = [[0; 3]; 2]; // initializer (incorrectly reversed ordering)
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
const b: [[[u8; 2]; 3]; 4] = [[[0; 4]; 3]; 2]; // initializer (incorrectly reversed ordering)
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
const b: [[u8; 2]; 3] = [0; (2, 3)]; // initializer (incorrectly reversed ordering)
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
const b: [[[u8; 2]; 3]; 4] = [0; (2, 3, 4)]; // initializer (incorrectly reversed ordering)
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
const b: [u8; (2, 3)] = [[0; 2]; 3]; // initializer (incorrectly reversed ordering)
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
function main() {
|
||||
const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
|
||||
|
||||
const b: [u8; (2, 3)] = [[0; 3]; 2]; // initializer
|
||||
|
||||
console.assert(a == b);
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
const b: [u8; (4, 3, 2)] = [[[0; 4]; 3]; 2]; // initializer (incorrectly reversed ordering)
|
||||
}
|
3
asg/tests/fail/array/type_tuple_value_tuple_3x2_fail.leo
Normal file
3
asg/tests/fail/array/type_tuple_value_tuple_3x2_fail.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
const b: [u8; (2, 3)] = [0; (3, 2)]; // initializer (incorrectly reversed ordering)
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
function main() {
|
||||
const a = [[0u8, 0u8], [0u8, 0u8], [0u8, 0u8]]; // inline
|
||||
|
||||
const b: [u8; (2, 3)] = [0; (2, 3)]; // initializer
|
||||
|
||||
console.assert(a == b);
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
const b: [u8; (4, 3, 2)] = [0; (2, 3, 4)]; // initializer (incorrectly reversed order)
|
||||
}
|
51
asg/tests/fail/boolean/mod.rs
Normal file
51
asg/tests/fail/boolean/mod.rs
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
// Copyright (C) 2019-2020 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 crate::load_asg;
|
||||
|
||||
#[test]
|
||||
fn test_not_u32() {
|
||||
let program_string = include_str!("not_u32.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_true_or_u32() {
|
||||
let program_string = include_str!("true_or_u32.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_true_and_u32() {
|
||||
let program_string = include_str!("true_and_u32.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
3
asg/tests/fail/boolean/not_u32.leo
Normal file
3
asg/tests/fail/boolean/not_u32.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() -> bool {
|
||||
console.assert(!1u32 == 0u32);
|
||||
}
|
3
asg/tests/fail/boolean/true_and_u32.leo
Normal file
3
asg/tests/fail/boolean/true_and_u32.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
let a = true && 1u32;
|
||||
}
|
3
asg/tests/fail/boolean/true_or_u32.leo
Normal file
3
asg/tests/fail/boolean/true_or_u32.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
let a = true || 1u32;
|
||||
}
|
7
asg/tests/fail/circuits/inline_fail.leo
Normal file
7
asg/tests/fail/circuits/inline_fail.leo
Normal file
@ -0,0 +1,7 @@
|
||||
circuit Foo {
|
||||
x: u32
|
||||
}
|
||||
|
||||
function main() {
|
||||
let a = Foo { y: 0u32 };
|
||||
}
|
3
asg/tests/fail/circuits/inline_undefined.leo
Normal file
3
asg/tests/fail/circuits/inline_undefined.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
let a = Foo { };
|
||||
}
|
10
asg/tests/fail/circuits/member_function_fail.leo
Normal file
10
asg/tests/fail/circuits/member_function_fail.leo
Normal file
@ -0,0 +1,10 @@
|
||||
circuit Foo {
|
||||
function echo(x: u32) -> u32 {
|
||||
return x
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
let a = Foo { };
|
||||
let err = a.echoed(1u32);
|
||||
}
|
10
asg/tests/fail/circuits/member_function_invalid.leo
Normal file
10
asg/tests/fail/circuits/member_function_invalid.leo
Normal file
@ -0,0 +1,10 @@
|
||||
circuit Foo {
|
||||
function echo(x: u32) -> u32 {
|
||||
return x
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
let a = Foo { };
|
||||
let err = a.echo(1u32); // echo is a static function and must be accessed using `::`
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
circuit Foo {
|
||||
function echo(x: u32) -> u32 {
|
||||
return x
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
let err = Foo.echo(1u32); // Invalid, echo is a static function and must be accessed using `::`
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
circuit Foo {
|
||||
function echo(x: u32) -> u32 {
|
||||
return x
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
let err = Foo::echoed(1u32);
|
||||
}
|
9
asg/tests/fail/circuits/member_variable_fail.leo
Normal file
9
asg/tests/fail/circuits/member_variable_fail.leo
Normal file
@ -0,0 +1,9 @@
|
||||
circuit Foo {
|
||||
x: u32
|
||||
}
|
||||
|
||||
function main() {
|
||||
let a = Foo { x: 1u32 };
|
||||
|
||||
let err = a.y;
|
||||
}
|
154
asg/tests/fail/circuits/mod.rs
Normal file
154
asg/tests/fail/circuits/mod.rs
Normal file
@ -0,0 +1,154 @@
|
||||
// Copyright (C) 2019-2020 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/>.
|
||||
|
||||
// Copyright (C) 2019-2020 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 crate::load_asg;
|
||||
|
||||
// Expressions
|
||||
|
||||
#[test]
|
||||
fn test_inline_fail() {
|
||||
let program_string = include_str!("inline_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_inline_undefined() {
|
||||
let program_string = include_str!("inline_undefined.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
// Members
|
||||
|
||||
#[test]
|
||||
fn test_member_variable_fail() {
|
||||
let program_string = include_str!("member_variable_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_member_function_fail() {
|
||||
let program_string = include_str!("member_function_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_member_function_invalid() {
|
||||
let program_string = include_str!("member_function_invalid.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mut_member_function_fail() {
|
||||
let program_string = r#"
|
||||
circuit Foo {
|
||||
function echo(mut self, x: u32) -> u32 {
|
||||
return x
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
let a = Foo { };
|
||||
|
||||
console.assert(a.echo(1u32) == 1u32);
|
||||
}"#;
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_member_static_function_invalid() {
|
||||
let program_string = include_str!("member_static_function_invalid.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_member_static_function_undefined() {
|
||||
let program_string = include_str!("member_static_function_undefined.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
// Mutability
|
||||
|
||||
#[test]
|
||||
fn test_mutate_function_fail() {
|
||||
let program_string = include_str!("mut_function_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mutate_self_variable_fail() {
|
||||
let program_string = include_str!("mut_self_variable_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mutate_self_function_fail() {
|
||||
let program_string = include_str!("mut_self_function_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mutate_self_static_function_fail() {
|
||||
let program_string = include_str!("mut_self_static_function_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mutate_static_function_fail() {
|
||||
let program_string = include_str!("mut_static_function_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mutate_variable_fail() {
|
||||
let program_string = include_str!("mut_variable_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
// Self
|
||||
|
||||
#[test]
|
||||
fn test_self_fail() {
|
||||
let program_string = include_str!("self_fail.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_self_member_invalid() {
|
||||
let program_string = include_str!("self_member_invalid.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_self_member_undefined() {
|
||||
let program_string = include_str!("self_member_undefined.leo");
|
||||
load_asg(program_string).err().unwrap();
|
||||
}
|
11
asg/tests/fail/circuits/mut_function_fail.leo
Normal file
11
asg/tests/fail/circuits/mut_function_fail.leo
Normal file
@ -0,0 +1,11 @@
|
||||
circuit Foo {
|
||||
a: u8,
|
||||
|
||||
function bar() {}
|
||||
}
|
||||
|
||||
function main() {
|
||||
let mut f = Foo { a: 0u8 };
|
||||
|
||||
f.bar = 1u8;
|
||||
}
|
15
asg/tests/fail/circuits/mut_self_function_fail.leo
Normal file
15
asg/tests/fail/circuits/mut_self_function_fail.leo
Normal file
@ -0,0 +1,15 @@
|
||||
circuit Foo {
|
||||
a: u8,
|
||||
|
||||
function bar() {}
|
||||
|
||||
function set_a(mut self, new: u8) {
|
||||
self.bar = new;
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
let mut f = Foo { a: 0u8 };
|
||||
|
||||
f.set_a(1u8);
|
||||
}
|
15
asg/tests/fail/circuits/mut_self_static_function_fail.leo
Normal file
15
asg/tests/fail/circuits/mut_self_static_function_fail.leo
Normal file
@ -0,0 +1,15 @@
|
||||
circuit Foo {
|
||||
a: u8,
|
||||
|
||||
function bar() {}
|
||||
|
||||
function set_a(mut self, new: u8) {
|
||||
self.bar = new;
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
let mut f = Foo { a: 0u8 };
|
||||
|
||||
f.set_a(1u8);
|
||||
}
|
13
asg/tests/fail/circuits/mut_self_variable_fail.leo
Normal file
13
asg/tests/fail/circuits/mut_self_variable_fail.leo
Normal file
@ -0,0 +1,13 @@
|
||||
circuit Foo {
|
||||
a: u8,
|
||||
|
||||
function set_a(self, new: u8) {
|
||||
self.a = new;
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
let mut f = Foo { a: 0u8 };
|
||||
|
||||
f.set_a(1u8);
|
||||
}
|
9
asg/tests/fail/circuits/mut_static_function_fail.leo
Normal file
9
asg/tests/fail/circuits/mut_static_function_fail.leo
Normal file
@ -0,0 +1,9 @@
|
||||
circuit Foo {
|
||||
function bar() {}
|
||||
}
|
||||
|
||||
function main() {
|
||||
let mut f = Foo { a: 0u8 };
|
||||
|
||||
f.bar = 1u8;
|
||||
}
|
9
asg/tests/fail/circuits/mut_variable_fail.leo
Normal file
9
asg/tests/fail/circuits/mut_variable_fail.leo
Normal file
@ -0,0 +1,9 @@
|
||||
circuit Foo {
|
||||
a: u8,
|
||||
}
|
||||
|
||||
function main() {
|
||||
let f = Foo { a: 0u8 };
|
||||
|
||||
f.a = 1u8;
|
||||
}
|
3
asg/tests/fail/circuits/self_fail.leo
Normal file
3
asg/tests/fail/circuits/self_fail.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
Self::main();
|
||||
}
|
12
asg/tests/fail/circuits/self_member_invalid.leo
Normal file
12
asg/tests/fail/circuits/self_member_invalid.leo
Normal file
@ -0,0 +1,12 @@
|
||||
circuit Foo {
|
||||
f: u32,
|
||||
|
||||
function bar() -> u32 {
|
||||
return f
|
||||
}
|
||||
}
|
||||
|
||||
function main() -> u32 {
|
||||
let foo = Foo { f: 1u32 };
|
||||
let err = foo.bar();
|
||||
}
|
10
asg/tests/fail/circuits/self_member_undefined.leo
Normal file
10
asg/tests/fail/circuits/self_member_undefined.leo
Normal file
@ -0,0 +1,10 @@
|
||||
circuit Foo {
|
||||
function bar() -> u32 {
|
||||
return self.f
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
let foo = Foo { };
|
||||
let err = foo.bar();
|
||||
}
|
3
asg/tests/fail/console/assert.leo
Normal file
3
asg/tests/fail/console/assert.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main(a: bool) {
|
||||
console.assert(a == true);
|
||||
}
|
7
asg/tests/fail/console/conditional_assert.leo
Normal file
7
asg/tests/fail/console/conditional_assert.leo
Normal file
@ -0,0 +1,7 @@
|
||||
function main(a: bool) {
|
||||
if a {
|
||||
console.assert(a == true);
|
||||
} else {
|
||||
console.assert(a == false);
|
||||
}
|
||||
}
|
3
asg/tests/fail/console/debug.leo
Normal file
3
asg/tests/fail/console/debug.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
console.debug("hello debug");
|
||||
}
|
3
asg/tests/fail/console/error.leo
Normal file
3
asg/tests/fail/console/error.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
console.error("hello error");
|
||||
}
|
3
asg/tests/fail/console/log.leo
Normal file
3
asg/tests/fail/console/log.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
console.log("hello world");
|
||||
}
|
3
asg/tests/fail/console/log_fail.leo
Normal file
3
asg/tests/fail/console/log_fail.leo
Normal file
@ -0,0 +1,3 @@
|
||||
function main() {
|
||||
console.log( hello );
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user