implicit input, removed from function input

This commit is contained in:
gluax 2021-04-14 16:00:19 -04:00
parent b6c5f4d60f
commit 91634d6479
35 changed files with 30 additions and 325 deletions

View File

@ -137,13 +137,6 @@ impl<'a> FromAst<'a, leo_ast::Identifier> for &'a Expression<'a> {
expected_type: Option<PartialType<'a>>,
) -> Result<&'a Expression<'a>, AsgConvertError> {
let variable = if value.name.as_ref() == "input" {
if let Some(function) = scope.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.resolve_input() {
input.container
} else {

View File

@ -47,7 +47,6 @@ pub struct Function<'a> {
pub id: u32,
pub name: RefCell<Identifier>,
pub output: Type<'a>,
pub has_input: bool,
pub arguments: IndexMap<String, Cell<&'a Variable<'a>>>,
pub circuit: Cell<Option<&'a Circuit<'a>>>,
pub span: Option<Span>,
@ -77,16 +76,12 @@ impl<'a> Function<'a> {
.transpose()?
.unwrap_or_else(|| Type::Tuple(vec![]));
let mut qualifier = FunctionQualifier::Static;
let mut has_input = false;
let new_scope = scope.make_subscope();
let mut arguments = IndexMap::new();
{
for input in value.input.iter() {
match input {
FunctionInput::InputKeyword(_) => {
has_input = true;
}
FunctionInput::SelfKeyword(_) => {
qualifier = FunctionQualifier::SelfRef;
}
@ -125,7 +120,6 @@ impl<'a> Function<'a> {
id: scope.context.get_id(),
name: RefCell::new(value.identifier.clone()),
output,
has_input,
arguments,
circuit: Cell::new(None),
body: Cell::new(None),

View File

@ -70,13 +70,6 @@ impl<'a> FromAst<'a, leo_ast::AssignStatement> for &'a Statement<'a> {
let (name, span) = (&statement.assignee.identifier.name, &statement.assignee.identifier.span);
let variable = if name.as_ref() == "input" {
if let Some(function) = scope.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.resolve_input() {
input.container
} else {

View File

@ -1,3 +1,3 @@
function main(input) -> [u8; 3] {
function main() -> [u8; 3] {
return input.registers.r
}

View File

@ -1,3 +1,3 @@
function main(input) -> bool {
function main() -> bool {
return input.registers.r
}

View File

@ -1,3 +1,3 @@
function main(input) -> (bool, bool) {
function main() -> (bool, bool) {
return (input.registers.a, input.registers.b)
}

View File

@ -1,4 +1,4 @@
function main(input, data: [u8; 32]) {
function main(data: [u8; 32]) {
console.assert(input.registers.value_balance == 0u64);
console.assert(input.state.leaf_index == 0u32);

View File

@ -1,4 +1,4 @@
function main(input) {
function main() {
console.assert(input.state.root == [0u8; 32]);
const expected: address = aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8;

View File

@ -1,3 +1,3 @@
function main(input) {
function main() {
console.assert(input.state.root == [0u8; 32]);
}

View File

@ -1,4 +1,4 @@
function main(input) -> u32 {
function main() -> u32 {
if input.registers.a == 0u32 {
return 0u32
} else {

View File

@ -1,44 +0,0 @@
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{Identifier, Node, Span};
use serde::{Deserialize, Serialize};
use std::fmt;
/// The `input` keyword can view program register, record, and state values.
/// Values cannot be modified. The `input` keyword cannot be made mutable.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(transparent)]
pub struct InputKeyword {
pub identifier: Identifier,
}
impl fmt::Display for InputKeyword {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "input")
}
}
impl Node for InputKeyword {
fn span(&self) -> &Span {
&self.identifier.span
}
fn set_span(&mut self, span: Span) {
self.identifier.span = span;
}
}

View File

@ -23,9 +23,6 @@ pub use const_self_keyword::*;
pub mod identifier;
pub use identifier::*;
pub mod input_keyword;
pub use input_keyword::*;
pub mod mut_self_keyword;
pub use mut_self_keyword::*;

View File

@ -14,7 +14,7 @@
// 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::{ConstSelfKeyword, FunctionInputVariable, InputKeyword, MutSelfKeyword, Node, SelfKeyword, Span};
use crate::{ConstSelfKeyword, FunctionInputVariable, MutSelfKeyword, Node, SelfKeyword, Span};
use serde::{Deserialize, Serialize};
use std::fmt;
@ -22,7 +22,6 @@ use std::fmt;
/// Enumerates the possible inputs to a function.
#[derive(Clone, Serialize, Deserialize)]
pub enum FunctionInput {
InputKeyword(InputKeyword),
SelfKeyword(SelfKeyword),
ConstSelfKeyword(ConstSelfKeyword),
MutSelfKeyword(MutSelfKeyword),
@ -36,7 +35,6 @@ impl FunctionInput {
///
pub fn is_self(&self) -> bool {
match self {
FunctionInput::InputKeyword(_) => false,
FunctionInput::SelfKeyword(_) => true,
FunctionInput::ConstSelfKeyword(_) => true,
FunctionInput::MutSelfKeyword(_) => true,
@ -50,7 +48,6 @@ impl FunctionInput {
///
pub fn is_const_self(&self) -> bool {
match self {
FunctionInput::InputKeyword(_) => false,
FunctionInput::SelfKeyword(_) => false,
FunctionInput::ConstSelfKeyword(_) => true,
FunctionInput::MutSelfKeyword(_) => false,
@ -64,7 +61,6 @@ impl FunctionInput {
///
pub fn is_mut_self(&self) -> bool {
match self {
FunctionInput::InputKeyword(_) => false,
FunctionInput::SelfKeyword(_) => false,
FunctionInput::ConstSelfKeyword(_) => false,
FunctionInput::MutSelfKeyword(_) => true,
@ -74,7 +70,6 @@ impl FunctionInput {
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
FunctionInput::InputKeyword(keyword) => write!(f, "{}", keyword),
FunctionInput::SelfKeyword(keyword) => write!(f, "{}", keyword),
FunctionInput::ConstSelfKeyword(keyword) => write!(f, "{}", keyword),
FunctionInput::MutSelfKeyword(keyword) => write!(f, "{}", keyword),
@ -99,7 +94,6 @@ impl PartialEq for FunctionInput {
/// Returns true if `self == other`. Does not compare spans.
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(FunctionInput::InputKeyword(_), FunctionInput::InputKeyword(_)) => true,
(FunctionInput::SelfKeyword(_), FunctionInput::SelfKeyword(_)) => true,
(FunctionInput::ConstSelfKeyword(_), FunctionInput::ConstSelfKeyword(_)) => true,
(FunctionInput::MutSelfKeyword(_), FunctionInput::MutSelfKeyword(_)) => true,
@ -115,7 +109,6 @@ impl Node for FunctionInput {
fn span(&self) -> &Span {
use FunctionInput::*;
match self {
InputKeyword(keyword) => &keyword.identifier.span,
SelfKeyword(keyword) => &keyword.identifier.span,
ConstSelfKeyword(keyword) => &keyword.identifier.span,
MutSelfKeyword(keyword) => &keyword.identifier.span,
@ -126,7 +119,6 @@ impl Node for FunctionInput {
fn set_span(&mut self, span: Span) {
use FunctionInput::*;
match self {
InputKeyword(keyword) => keyword.identifier.span = span,
SelfKeyword(keyword) => keyword.identifier.span = span,
ConstSelfKeyword(keyword) => keyword.identifier.span = span,
MutSelfKeyword(keyword) => keyword.identifier.span = span,

View File

@ -35,17 +35,16 @@ impl<'a, F: PrimeField, G: GroupType<F>> ConstrainedProgram<'a, F, G> {
let registers = input.get_registers();
// Iterate over main function input variables and allocate new values
if function.has_input {
// let input_var = function.scope.
let asg_input = function
.scope
.resolve_input()
.expect("no input variable in scope when function is qualified");
let asg_input = function.scope.resolve_input();
let value =
self.allocate_input_keyword(cs, &function.name.borrow().span, &asg_input.container_circuit, input)?;
match asg_input {
Some(asg_input) => {
let value =
self.allocate_input_keyword(cs, &function.name.borrow().span, &asg_input.container_circuit, input)?;
self.store(asg_input.container.borrow().id, value);
self.store(asg_input.container.borrow().id, value);
}
None => (),
}
match function.qualifier {

View File

@ -1,3 +1,3 @@
function main(input) -> [u8; 3] {
function main() -> [u8; 3] {
return input.registers.r
}

View File

@ -1,3 +1,3 @@
function main(input) -> bool {
function main() -> bool {
return input.registers.r
}

View File

@ -1,3 +1,3 @@
function main(input) -> (bool, bool) {
function main() -> (bool, bool) {
return (input.registers.a, input.registers.b)
}

View File

@ -1,4 +1,4 @@
function main(input, data: [u8; 32]) {
function main(data: [u8; 32]) {
console.assert(input.registers.value_balance == 0u64);
console.assert(input.state.leaf_index == 0u32);

View File

@ -1,4 +1,4 @@
function main(input) {
function main() {
console.assert(input.state.root == [0u8; 32]);
const expected: address = aleo1qnr4dkkvkgfqph0vzc3y6z2eu975wnpz2925ntjccd5cfqxtyu8sta57j8;

View File

@ -1,3 +1,3 @@
function main(input) {
function main() {
console.assert(input.state.root == [0u8; 32]);
}

View File

@ -1,4 +1,4 @@
function main(input) -> u32 {
function main() -> u32 {
if input.registers.a == 0u32 {
return 0u32
} else {

View File

@ -973,10 +973,9 @@ function-declaration = *annotation %s"function" identifier
"(" [ function-parameters ] ")" [ "->" type ]
block
function-parameters = self-parameter [ "," input-parameter ]
/ self-parameter "," function-inputs [ "," input-parameter ]
/ function-inputs [ "," input-parameter ]
/ input-parameter
function-parameters = self-parameter
/ self-parameter "," function-inputs
/ function-inputs
self-parameter = [ %s"mut" / %s"const" ] %s"self"

View File

@ -312,14 +312,6 @@ impl ParserContext {
/// Returns a [`FunctionInput`] AST node if the next tokens represent a function parameter.
///
pub fn parse_function_parameters(&mut self) -> SyntaxResult<FunctionInput> {
if let Some(token) = self.eat(Token::Input) {
return Ok(FunctionInput::InputKeyword(InputKeyword {
identifier: Identifier {
name: token.token.to_string().into(),
span: token.span,
},
}));
}
let const_ = self.eat(Token::Const);
let mutable = self.eat(Token::Mut);
let mut name = if let Some(token) = self.eat(Token::LittleSelf) {

View File

@ -1,3 +1,3 @@
function main(input) -> [u8; 3] {
function main() -> [u8; 3] {
return input.registers.r
}

View File

@ -1,3 +1,3 @@
function main(input) -> bool {
function main() -> bool {
return input.registers.r
}

View File

@ -1,3 +1,3 @@
function main(input) -> (bool, bool) {
function main() -> (bool, bool) {
return (input.registers.a, input.registers.b)
}

View File

@ -1,4 +1,4 @@
function main(input) -> u32 {
function main() -> u32 {
if input.registers.a == 0u32 {
return 0u32
} else {

View File

@ -1,8 +0,0 @@
/*
namespace: Parse
expectation: Pass
*/
function x(input) {
return ();
}

View File

@ -1,49 +0,0 @@
---
namespace: Parse
expectation: Pass
outputs:
- name: ""
expected_input: []
imports: []
circuits: {}
functions:
"{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":10,\\\"col_stop\\\":11,\\\"path\\\":\\\"input.leo\\\",\\\"content\\\":\\\"function x(input) {\\\"}\"}":
annotations: []
identifier: "{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":10,\\\"col_stop\\\":11,\\\"path\\\":\\\"input.leo\\\",\\\"content\\\":\\\"function x(input) {\\\"}\"}"
input:
- InputKeyword: "{\"name\":\"input\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":12,\\\"col_stop\\\":17,\\\"path\\\":\\\"input.leo\\\",\\\"content\\\":\\\"function x(input) {\\\"}\"}"
output: ~
block:
statements:
- Return:
expression:
TupleInit:
elements: []
span:
line_start: 4
line_stop: 4
col_start: 12
col_stop: 14
path: input.leo
content: " return ();"
span:
line_start: 4
line_stop: 4
col_start: 5
col_stop: 14
path: input.leo
content: " return ();"
span:
line_start: 3
line_stop: 5
col_start: 19
col_stop: 2
path: input.leo
content: "function x(input) {\n...\n}"
span:
line_start: 3
line_stop: 5
col_start: 1
col_stop: 2
path: input.leo
content: "function x(input) {\n...\n}"

View File

@ -1,8 +0,0 @@
/*
namespace: Parse
expectation: Pass
*/
function x(x: u32, input) {
return ();
}

View File

@ -1,62 +0,0 @@
---
namespace: Parse
expectation: Pass
outputs:
- name: ""
expected_input: []
imports: []
circuits: {}
functions:
"{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":10,\\\"col_stop\\\":11,\\\"path\\\":\\\"input_order_bad.leo\\\",\\\"content\\\":\\\"function x(x: u32, input) {\\\"}\"}":
annotations: []
identifier: "{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":10,\\\"col_stop\\\":11,\\\"path\\\":\\\"input_order_bad.leo\\\",\\\"content\\\":\\\"function x(x: u32, input) {\\\"}\"}"
input:
- Variable:
identifier: "{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":12,\\\"col_stop\\\":13,\\\"path\\\":\\\"input_order_bad.leo\\\",\\\"content\\\":\\\"function x(x: u32, input) {\\\"}\"}"
const_: false
mutable: true
type_:
IntegerType: U32
span:
line_start: 3
line_stop: 3
col_start: 12
col_stop: 13
path: input_order_bad.leo
content: "function x(x: u32, input) {"
- InputKeyword: "{\"name\":\"input\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":20,\\\"col_stop\\\":25,\\\"path\\\":\\\"input_order_bad.leo\\\",\\\"content\\\":\\\"function x(x: u32, input) {\\\"}\"}"
output: ~
block:
statements:
- Return:
expression:
TupleInit:
elements: []
span:
line_start: 4
line_stop: 4
col_start: 12
col_stop: 14
path: input_order_bad.leo
content: " return ();"
span:
line_start: 4
line_stop: 4
col_start: 5
col_stop: 14
path: input_order_bad.leo
content: " return ();"
span:
line_start: 3
line_stop: 5
col_start: 27
col_stop: 2
path: input_order_bad.leo
content: "function x(x: u32, input) {\n...\n}"
span:
line_start: 3
line_stop: 5
col_start: 1
col_stop: 2
path: input_order_bad.leo
content: "function x(x: u32, input) {\n...\n}"

View File

@ -1,8 +0,0 @@
/*
namespace: Parse
expectation: Pass
*/
function x(input, x: u32) {
return ();
}

View File

@ -1,62 +0,0 @@
---
namespace: Parse
expectation: Pass
outputs:
- name: ""
expected_input: []
imports: []
circuits: {}
functions:
"{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":10,\\\"col_stop\\\":11,\\\"path\\\":\\\"input_params.leo\\\",\\\"content\\\":\\\"function x(input, x: u32) {\\\"}\"}":
annotations: []
identifier: "{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":10,\\\"col_stop\\\":11,\\\"path\\\":\\\"input_params.leo\\\",\\\"content\\\":\\\"function x(input, x: u32) {\\\"}\"}"
input:
- InputKeyword: "{\"name\":\"input\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":12,\\\"col_stop\\\":17,\\\"path\\\":\\\"input_params.leo\\\",\\\"content\\\":\\\"function x(input, x: u32) {\\\"}\"}"
- Variable:
identifier: "{\"name\":\"x\",\"span\":\"{\\\"line_start\\\":3,\\\"line_stop\\\":3,\\\"col_start\\\":19,\\\"col_stop\\\":20,\\\"path\\\":\\\"input_params.leo\\\",\\\"content\\\":\\\"function x(input, x: u32) {\\\"}\"}"
const_: false
mutable: true
type_:
IntegerType: U32
span:
line_start: 3
line_stop: 3
col_start: 19
col_stop: 20
path: input_params.leo
content: "function x(input, x: u32) {"
output: ~
block:
statements:
- Return:
expression:
TupleInit:
elements: []
span:
line_start: 4
line_stop: 4
col_start: 12
col_stop: 14
path: input_params.leo
content: " return ();"
span:
line_start: 4
line_stop: 4
col_start: 5
col_stop: 14
path: input_params.leo
content: " return ();"
span:
line_start: 3
line_stop: 5
col_start: 27
col_stop: 2
path: input_params.leo
content: "function x(input, x: u32) {\n...\n}"
span:
line_start: 3
line_stop: 5
col_start: 1
col_stop: 2
path: input_params.leo
content: "function x(input, x: u32) {\n...\n}"

View File

@ -1,8 +0,0 @@
/*
namespace: Parse
expectation: Fail
*/
function x(input: u32) {
return ();
}

View File

@ -1,5 +0,0 @@
---
namespace: Parse
expectation: Fail
outputs:
- " --> test: 3:17\n |\n 3 | function x(input: u32) {\n | ^\n |\n = expected ')' -- got ':'"