fix examples

This commit is contained in:
collin 2022-07-12 18:30:55 -07:00
parent 266fdd7a5e
commit c0e968206b
7 changed files with 30 additions and 52 deletions

View File

@ -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", input.name, input.network); let mut program_string = format!("program {}.{};\n\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(

View File

@ -411,7 +411,7 @@ create_messages!(
@backtraced @backtraced
failed_to_load_instructions { failed_to_load_instructions {
args: (error: impl Display), args: (error: impl Display),
msg: format!("Failed to load compiled Aleo instructions into an Aleo file: {}", error), msg: format!("Failed to load compiled Aleo instructions into an Aleo file:\n{}", error),
help: Some("This is a Leo code generation bug. Please report it to https://github.com/AleoHQ/leo/issues/new/choose".to_string()), help: Some("This is a Leo code generation bug. Please report it to https://github.com/AleoHQ/leo/issues/new/choose".to_string()),
} }

View File

@ -1,5 +1,12 @@
program helloworld.aleo; program helloworld.aleo;
function compute:
input r0 as u32.private;
input r1 as u32.private;
add r0 r1 into r2;
output r2 as u32.private;
function main: function main:
input r0 as u32.public; input r0 as u32.public;
input r1 as u32.private; input r1 as u32.private;

View File

@ -1,5 +1,7 @@
// The 'helloworld' main function. // The 'helloworld' main function.
function main(public a: u32, b: u32) -> u32 { function compute(a: u32, b: u32) -> u32 {
let c: u32 = a + b; return a + b;
return c;
} }
function main(public a: u32, b: u32) -> u32 {
return a + b;
}

View File

@ -1,4 +1,3 @@
// The program input for token/src/main.leo // The program input for token/src/main.leo
[main] [main]
a: u32 = 1u32; a: u32 = 1u32;
b: u32 = 2u32;

11
examples/token/main.aleo Normal file
View File

@ -0,0 +1,11 @@
program token.aleo;
record token:
owner as address.private;
balance as u64.private;
token_amount as u64.private;
function compute:
input r0 as token.record;
add r0.token_amount r0.token_amount into r1;
output r1 as u64.private;

View File

@ -1,50 +1,9 @@
// CAUTION: Work in progress
record Token { record Token {
// The token owner. owner: address,
owner: address, balance: u64,
// The Aleo balance (in gates). token_amount: u64,
balance: u64,
// The token amount.
amount: u64,
} }
circuit Receiver { function compute(t: Token) -> u64 {
// The token owner. return t.token_amount + t.token_amount;
owner: address,
// The token amount.
amount: u64,
}
function mint(r0: address, r1: u64) -> Token {
let t: Token = Token {
owner: r0,
balance: 0u64,
amount: r1,
};
return t;
}
// The `transfer` function sends the specified number of tokens
// to the receiver from the provided token record.
function transfer(r0: Token, r1: Receiver) -> (Token, Token) {
// Checks the given token record has sufficient balance.
// This `sub` operation is safe, and the proof will fail
// if an overflow occurs. The output register `r2` holds
// the change amount to be returned to the sender.
let r2: u64 = r0.amount - r1.amount;
// Calls the `mint` function to produce a token record
// for the specified receiver.
let r3: Token = mint(r1.owner, r1.amount);
// Calls the `mint` function to produce a token record
// with the change amount for the sender.
let r4: Token = mint(r0.owner, r0.amount);
return (r3, r4);
}
function main() -> u8 {
return 1u8;
} }