mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-24 02:31:44 +03:00
add token transfer example, fix record cast codegen
This commit is contained in:
parent
13b9261063
commit
7d604748bd
@ -24,19 +24,12 @@ pub struct CircuitVariableInitializer {
|
||||
/// The expression to initialize the field with.
|
||||
/// When `None`, a binding, in scope, with the name will be used instead.
|
||||
pub expression: Option<Expression>,
|
||||
/// `true` if the circuit is a `record` type.
|
||||
pub is_record: bool,
|
||||
}
|
||||
|
||||
impl fmt::Display for CircuitVariableInitializer {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
if let Some(expr) = &self.expression {
|
||||
write!(f, "{}: {}", self.identifier, expr)?;
|
||||
if self.is_record {
|
||||
write!(f, "private")
|
||||
} else {
|
||||
write!(f, "")
|
||||
}
|
||||
write!(f, "{}: {}", self.identifier, expr)
|
||||
} else {
|
||||
write!(f, "{}", self.identifier)
|
||||
}
|
||||
|
@ -467,12 +467,9 @@ impl ParserContext<'_> {
|
||||
None
|
||||
};
|
||||
|
||||
let is_record = identifier.to_string().eq("Record");
|
||||
|
||||
Ok(CircuitVariableInitializer {
|
||||
identifier,
|
||||
expression,
|
||||
is_record,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -154,6 +154,13 @@ impl<'a> CodeGenerator<'a> {
|
||||
}
|
||||
|
||||
fn visit_circuit_init(&mut self, input: &'a CircuitExpression) -> (String, String) {
|
||||
// Lookup circuit or record.
|
||||
let name = if let Some(type_) = self.composite_mapping.get(&input.name.name) {
|
||||
format!("{}.{}", input.name.to_string().to_lowercase(), type_)
|
||||
} else {
|
||||
unreachable!("All composite types should be known at this phase of compilation")
|
||||
};
|
||||
|
||||
// Initialize instruction builder strings.
|
||||
let mut instructions = String::new();
|
||||
let mut circuit_init_instruction = format!(" cast ");
|
||||
@ -180,7 +187,13 @@ impl<'a> CodeGenerator<'a> {
|
||||
|
||||
// Push destination register to circuit init instruction.
|
||||
let destination_register = format!("r{}", self.next_register);
|
||||
writeln!(circuit_init_instruction, "into {} as {};", destination_register, input.name.to_string().to_lowercase()).expect("failed to write to string");
|
||||
writeln!(
|
||||
circuit_init_instruction,
|
||||
"into {dest} as {name};",
|
||||
dest = destination_register,
|
||||
name = name,
|
||||
).expect("failed to write to string");
|
||||
|
||||
instructions.push_str(&circuit_init_instruction);
|
||||
|
||||
// Increment the register counter.
|
||||
|
2
examples/transfer/.gitignore
vendored
Normal file
2
examples/transfer/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
outputs/
|
||||
build/
|
8
examples/transfer/README.md
Normal file
8
examples/transfer/README.md
Normal file
@ -0,0 +1,8 @@
|
||||
# transfer.aleo
|
||||
|
||||
## Build Guide
|
||||
|
||||
To compile this Aleo program, run:
|
||||
```bash
|
||||
aleo build
|
||||
```
|
4
examples/transfer/inputs/transfer.in
Normal file
4
examples/transfer/inputs/transfer.in
Normal file
@ -0,0 +1,4 @@
|
||||
// The program input for transfer/src/main.leo
|
||||
[main]
|
||||
owner: address = aleo12aw0kcnzyn5xj46z9u6mzpa67tzuqnvmwe0q2ejfjm8c2ue4pgys3877fr;
|
||||
amount: u64 = 100u64;
|
21
examples/transfer/main.aleo
Normal file
21
examples/transfer/main.aleo
Normal file
@ -0,0 +1,21 @@
|
||||
program transfer.aleo;
|
||||
record token:
|
||||
owner as address.private;
|
||||
balance as u64.private;
|
||||
amount as u64.private;
|
||||
|
||||
function main:
|
||||
input r0 as address.private;
|
||||
input r1 as u64.private;
|
||||
cast r0 0u64 r1 into r2 as token.record;
|
||||
output r2 as token.record;
|
||||
|
||||
function transfer:
|
||||
input r0 as token.record;
|
||||
input r1 as address.private;
|
||||
input r2 as u64.private;
|
||||
sub r0.amount r2 into r3;
|
||||
cast r1 0u64 r2 into r4 as token.record;
|
||||
cast r0.owner r0.balance r3 into r5 as token.record;
|
||||
output r4 as token.record;
|
||||
output r5 as token.record;
|
10
examples/transfer/program.json
Normal file
10
examples/transfer/program.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"program": "transfer.aleo",
|
||||
"version": "0.0.0",
|
||||
"description": "",
|
||||
"development": {
|
||||
"private_key": "APrivateKey1zkp3FxmbSYjkHsRBzZbCMGXY5u2sGCkgddNKVL7CDwy7KSe",
|
||||
"address": "aleo12aw0kcnzyn5xj46z9u6mzpa67tzuqnvmwe0q2ejfjm8c2ue4pgys3877fr"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
35
examples/transfer/src/main.leo
Normal file
35
examples/transfer/src/main.leo
Normal file
@ -0,0 +1,35 @@
|
||||
record Token {
|
||||
owner: address,
|
||||
balance: u64,
|
||||
amount: u64,
|
||||
}
|
||||
|
||||
function main(owner: address, amount: u64) -> Token {
|
||||
return Token {
|
||||
owner: owner,
|
||||
balance: 0u64,
|
||||
amount: amount,
|
||||
};
|
||||
}
|
||||
|
||||
function transfer(
|
||||
t: Token,
|
||||
r: address,
|
||||
a: u64
|
||||
) -> (Token, Token) {
|
||||
let change: u64 = t.amount - a;
|
||||
|
||||
let t1: Token = Token {
|
||||
owner: r,
|
||||
balance: 0u64,
|
||||
amount: a,
|
||||
};
|
||||
|
||||
let t2: Token = Token {
|
||||
owner: t.owner,
|
||||
balance: t.balance,
|
||||
amount: change
|
||||
};
|
||||
|
||||
return (t1, t2);
|
||||
}
|
Loading…
Reference in New Issue
Block a user