mirror of
https://github.com/AleoHQ/leo.git
synced 2024-09-20 19:47:22 +03:00
add more examples, fix circuits as input
This commit is contained in:
parent
6b561fc38f
commit
3d9c8dd0e4
@ -24,12 +24,19 @@ pub struct CircuitVariableInitializer {
|
|||||||
/// The expression to initialize the field with.
|
/// The expression to initialize the field with.
|
||||||
/// When `None`, a binding, in scope, with the name will be used instead.
|
/// When `None`, a binding, in scope, with the name will be used instead.
|
||||||
pub expression: Option<Expression>,
|
pub expression: Option<Expression>,
|
||||||
|
/// `true` if the circuit is a `record` type.
|
||||||
|
pub is_record: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for CircuitVariableInitializer {
|
impl fmt::Display for CircuitVariableInitializer {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
if let Some(expr) = &self.expression {
|
if let Some(expr) = &self.expression {
|
||||||
write!(f, "{}: {}", self.identifier, expr)
|
write!(f, "{}: {}", self.identifier, expr)?;
|
||||||
|
if self.is_record {
|
||||||
|
write!(f, "private")
|
||||||
|
} else {
|
||||||
|
write!(f, "")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
write!(f, "{}", self.identifier)
|
write!(f, "{}", self.identifier)
|
||||||
}
|
}
|
||||||
@ -52,12 +59,15 @@ pub struct CircuitExpression {
|
|||||||
|
|
||||||
impl fmt::Display for CircuitExpression {
|
impl fmt::Display for CircuitExpression {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{} {{ ", self.name)?;
|
write!(
|
||||||
for member in self.members.iter() {
|
f,
|
||||||
write!(f, "{}", member)?;
|
"{{{}}}",
|
||||||
write!(f, ", ")?;
|
self.members
|
||||||
}
|
.iter()
|
||||||
write!(f, "}}")
|
.map(|x| x.to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,10 +46,10 @@ impl fmt::Display for Literal {
|
|||||||
match &self {
|
match &self {
|
||||||
Self::Address(address, _) => write!(f, "{}", address),
|
Self::Address(address, _) => write!(f, "{}", address),
|
||||||
Self::Boolean(boolean, _) => write!(f, "{}", boolean),
|
Self::Boolean(boolean, _) => write!(f, "{}", boolean),
|
||||||
Self::Field(field, _) => write!(f, "{}", field),
|
Self::Field(field, _) => write!(f, "{}field", field),
|
||||||
Self::Group(group) => write!(f, "{}", group),
|
Self::Group(group) => write!(f, "{}group", group),
|
||||||
Self::Integer(type_, value, _) => write!(f, "{}{}", value, type_),
|
Self::Integer(type_, value, _) => write!(f, "{}{}", value, type_),
|
||||||
Self::Scalar(scalar, _) => write!(f, "{}", scalar),
|
Self::Scalar(scalar, _) => write!(f, "{}scalar", scalar),
|
||||||
Self::String(string, _) => write!(f, "{}", string),
|
Self::String(string, _) => write!(f, "{}", string),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -467,7 +467,13 @@ impl ParserContext<'_> {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(CircuitVariableInitializer { identifier, expression })
|
let is_record = identifier.to_string().eq("Record");
|
||||||
|
|
||||||
|
Ok(CircuitVariableInitializer {
|
||||||
|
identifier,
|
||||||
|
expression,
|
||||||
|
is_record,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an [`Expression`] AST node if the next tokens represent a
|
/// Returns an [`Expression`] AST node if the next tokens represent a
|
||||||
|
@ -65,7 +65,7 @@ impl ParserContext<'_> {
|
|||||||
|
|
||||||
let name = self.expect_identifier()?;
|
let name = self.expect_identifier()?;
|
||||||
self.expect(&Token::Colon)?;
|
self.expect(&Token::Colon)?;
|
||||||
let (type_, span) = self.parse_non_ident_types()?;
|
let (type_, span) = self.parse_single_type()?;
|
||||||
self.expect(&Token::Assign)?;
|
self.expect(&Token::Assign)?;
|
||||||
let value = self.parse_unary_expression()?;
|
let value = self.parse_unary_expression()?;
|
||||||
self.expect(&Token::Semicolon)?;
|
self.expect(&Token::Semicolon)?;
|
||||||
|
@ -23,7 +23,7 @@ use std::{collections::HashMap, fmt::Write as _};
|
|||||||
|
|
||||||
impl<'a> CodeGenerator<'a> {
|
impl<'a> CodeGenerator<'a> {
|
||||||
pub(crate) fn visit_program(&mut self, input: &'a Program) -> String {
|
pub(crate) fn visit_program(&mut self, input: &'a Program) -> String {
|
||||||
let mut program_string = format!("program {}.{};\n\n", input.name, input.network);
|
let mut program_string = format!("program {}.{};\n", input.name, input.network);
|
||||||
|
|
||||||
// Visit each `Circuit` or `Record` in the Leo AST and produce a bytecode circuit.
|
// Visit each `Circuit` or `Record` in the Leo AST and produce a bytecode circuit.
|
||||||
program_string.push_str(
|
program_string.push_str(
|
||||||
|
2
examples/message/.gitignore
vendored
Normal file
2
examples/message/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
outputs/
|
||||||
|
build/
|
6
examples/message/README.md
Normal file
6
examples/message/README.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# message.aleo
|
||||||
|
## Build Guide
|
||||||
|
To compile this Aleo program, run:
|
||||||
|
```bash
|
||||||
|
aleo build
|
||||||
|
```
|
3
examples/message/inputs/message.in
Normal file
3
examples/message/inputs/message.in
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
// The program input for message/src/main.leo
|
||||||
|
[main]
|
||||||
|
a: Message = Message { first: 2field, second: 3field};
|
9
examples/message/main.aleo
Normal file
9
examples/message/main.aleo
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
program message.aleo;
|
||||||
|
interface message:
|
||||||
|
first as field;
|
||||||
|
second as field;
|
||||||
|
|
||||||
|
function main:
|
||||||
|
input r0 as message.private;
|
||||||
|
add r0.first r0.second into r1;
|
||||||
|
output r1 as field.private;
|
10
examples/message/program.json
Normal file
10
examples/message/program.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"program": "message.aleo",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"description": "",
|
||||||
|
"development": {
|
||||||
|
"private_key": "APrivateKey1zkp4YvAU9D17zZDxrt6WKE2wVrNGgfqheLVXqJ6WZADtKS1",
|
||||||
|
"address": "aleo1fw4xmr8yv88scg20gmzqn2t7ye22wqk2rq22d2ayq6j952v0n5psw7ztqp"
|
||||||
|
},
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
9
examples/message/src/main.leo
Normal file
9
examples/message/src/main.leo
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// The 'message' main function.
|
||||||
|
circuit Message {
|
||||||
|
first: field,
|
||||||
|
second: field,
|
||||||
|
}
|
||||||
|
|
||||||
|
function main(m: Message) -> field {
|
||||||
|
return m.first + m.second;
|
||||||
|
}
|
@ -1,3 +1,7 @@
|
|||||||
// The program input for token/src/main.leo
|
// The program input for token/src/main.leo
|
||||||
[main]
|
[main]
|
||||||
a: u32 = 1u32;
|
a: Record = Record {
|
||||||
|
owner: aleo1d5hg2z3ma00382pngntdp68e74zv54jdxy249qhaujhks9c72yrs33ddah,
|
||||||
|
balance: 5u64,
|
||||||
|
token_amount: 100u64
|
||||||
|
};
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
program token.aleo;
|
program token.aleo;
|
||||||
|
|
||||||
record token:
|
record token:
|
||||||
owner as address.private;
|
owner as address.private;
|
||||||
balance as u64.private;
|
balance as u64.private;
|
||||||
token_amount as u64.private;
|
token_amount as u64.private;
|
||||||
|
|
||||||
function compute:
|
function main:
|
||||||
input r0 as token.record;
|
input r0 as token.record;
|
||||||
add r0.token_amount r0.token_amount into r1;
|
add r0.token_amount r0.token_amount into r1;
|
||||||
output r1 as u64.private;
|
output r1 as u64.private;
|
||||||
|
@ -4,6 +4,6 @@ record Token {
|
|||||||
token_amount: u64,
|
token_amount: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
function compute(t: Token) -> u64 {
|
function main(t: Token) -> u64 {
|
||||||
return t.token_amount + t.token_amount;
|
return t.token_amount + t.token_amount;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user