add record type

This commit is contained in:
collin 2022-06-23 21:44:06 -10:00
parent 13254ef4a3
commit 9d0fd00072
8 changed files with 149 additions and 4 deletions

View File

@ -42,6 +42,9 @@ pub use self::groups::*;
pub mod input;
pub use self::input::*;
pub mod records;
pub use self::records::*;
pub mod passes;
pub use self::passes::*;

View File

@ -0,0 +1,21 @@
// Copyright (C) 2019-2022 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/>.
pub mod record;
pub use record::*;
pub mod record_variable;
pub use record_variable::*;

View File

@ -0,0 +1,77 @@
// Copyright (C) 2019-2022 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, RecordVariable};
use leo_span::{Span, Symbol};
use serde::{Deserialize, Serialize};
use std::fmt;
/// A record type definition.
/// `record Token { owner: address, balance: u64, balance: u64 }`.
/// Records are constructed similar to `circuit` types but with variables specific to Aleo.
#[derive(Clone, Serialize, Deserialize)]
pub struct Record {
/// The name of the type in the type system in this module.
pub identifier: Identifier,
/// The owner of the program record.
pub owner: RecordVariable,
/// The balance of the program record.
pub balance: RecordVariable,
/// The program data,
pub data: Vec<RecordVariable>,
/// The entire span of the circuit definition.
pub span: Span,
}
impl PartialEq for Record {
fn eq(&self, other: &Self) -> bool {
self.identifier == other.identifier
}
}
impl Eq for Record {}
impl Record {
/// Returns the record name as a Symbol.
pub fn name(&self) -> Symbol {
self.identifier.name
}
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "record {} {{ ", self.identifier)?;
writeln!(f, " {}", self.owner)?;
writeln!(f, " {}", self.balance)?;
for var in self.data.iter() {
writeln!(f, " {}", var)?;
}
write!(f, "}}")
}
}
impl fmt::Debug for Record {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f)
}
}
impl fmt::Display for Record {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f)
}
}
crate::simple_node_impl!(Record);

View File

@ -0,0 +1,37 @@
// Copyright (C) 2019-2022 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, Type};
use serde::{Deserialize, Serialize};
use std::fmt;
#[allow(clippy::large_enum_variant)]
/// A variable definition in a record;
/// For example: `foobar: u8;`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RecordVariable {
/// The identifier of the constant.
ident: Identifier,
/// The type the constant has.
type_: Type
}
impl fmt::Display for RecordVariable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}: {}", self.ident, self.type_)
}
}

View File

@ -316,6 +316,7 @@ impl Token {
"in" => Token::In,
"let" => Token::Let,
"public" => Token::Public,
"record" => Token::Record,
"return" => Token::Return,
"scalar" => Token::Scalar,
"string" => Token::String,

View File

@ -90,6 +90,7 @@ pub enum Token {
U64,
U128,
SelfUpper,
Record,
// Regular Keywords
Circuit,
@ -140,6 +141,7 @@ pub const KEYWORD_TOKENS: &[Token] = &[
Token::In,
Token::Let,
Token::Public,
Token::Record,
Token::Return,
Token::SelfLower,
Token::SelfUpper,
@ -184,6 +186,7 @@ impl Token {
Token::In => sym::In,
Token::Let => sym::Let,
Token::Public => sym::Public,
Token::Record => sym::record,
Token::Return => sym::Return,
Token::Scalar => sym::scalar,
Token::SelfLower => sym::SelfLower,
@ -268,6 +271,7 @@ impl fmt::Display for Token {
U64 => write!(f, "u64"),
U128 => write!(f, "u128"),
SelfUpper => write!(f, "Self"),
Record => write!(f, "record"),
Circuit => write!(f, "circuit"),
Console => write!(f, "console"),

View File

@ -1,6 +1,6 @@
// CAUTION: Work in progress
circuit Token {
record Token {
// The token owner.
owner: address,
// The Aleo balance (in gates).

View File

@ -164,6 +164,7 @@ symbols! {
i32,
i64,
i128,
record,
scalar,
string,
u8,
@ -207,6 +208,10 @@ symbols! {
test,
Type: "type",
public,
private,
// todo: remove these.
CONTAINER_PSEUDO_CIRCUIT: "$InputContainer",
REGISTERS_PSEUDO_CIRCUIT: "$InputRegister",
RECORD_PSEUDO_CIRCUIT: "$InputRecord",
@ -215,11 +220,8 @@ symbols! {
// input file
registers,
record,
state,
state_leaf,
public,
private,
}
/// An interned string.