add type variable

This commit is contained in:
collin 2020-10-12 11:33:42 -07:00
parent a66cc3afda
commit e0750158f5
10 changed files with 101 additions and 19 deletions

View File

@ -0,0 +1,24 @@
// Copyright (C) 2019-2020 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 serde::{Deserialize, Serialize};
/// Indicates that a program variable has additional functionality.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Attribute {
Mutable,
Static,
}

View File

@ -14,5 +14,5 @@
// 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 variable;
pub use self::variable::*;
pub mod attribute;
pub use self::attribute::*;

View File

@ -17,6 +17,9 @@
#[macro_use]
extern crate thiserror;
pub mod attributes;
pub use self::attributes::*;
pub mod errors;
pub use self::errors::*;

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::{FunctionInputVariableType, ResolvedNode, SymbolTable, Type, TypeError, VariableType};
use crate::{FunctionInputVariableType, ParameterType, ResolvedNode, SymbolTable, Type, TypeError};
use leo_typed::{FunctionInput, Identifier};
use serde::{Deserialize, Serialize};
@ -93,7 +93,7 @@ impl FunctionInputType {
///
/// If the symbol table did not have this name present, `None` is returned.
///
pub fn insert(&self, table: &mut SymbolTable) -> Option<VariableType> {
pub fn insert(&self, table: &mut SymbolTable) -> Option<ParameterType> {
match self {
FunctionInputType::Variable(variable) => variable.insert(table),
FunctionInputType::InputKeyword(_identifier) => {

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::{Attribute, ResolvedNode, SymbolTable, Type, TypeError, VariableType};
use crate::{Attribute, ParameterType, ResolvedNode, SymbolTable, Type, TypeError};
use leo_typed::{FunctionInputVariable, Identifier, Span};
use serde::{Deserialize, Serialize};
@ -94,9 +94,9 @@ impl FunctionInputVariableType {
///
/// If the symbol table did not have this name present, `None` is returned.
///
pub fn insert(&self, table: &mut SymbolTable) -> Option<VariableType> {
pub fn insert(&self, table: &mut SymbolTable) -> Option<ParameterType> {
let key = self.identifier.name.clone();
let value = VariableType::from(self.clone());
let value = ParameterType::from(self.clone());
table.insert_variable(key, value)
}

View File

@ -23,5 +23,8 @@ pub use self::functions::*;
pub mod type_;
pub use self::type_::*;
pub mod variables;
pub use self::variables::*;
pub mod type_variable;
pub use self::type_variable::*;
pub mod parameters;
pub use self::parameters::*;

View File

@ -0,0 +1,18 @@
// Copyright (C) 2019-2020 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 parameter;
pub use self::parameter::*;

View File

@ -23,13 +23,13 @@ use std::fmt;
///
/// This type should be added to the variable symbol table for a resolved syntax tree.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct VariableType {
pub struct ParameterType {
pub identifier: Identifier,
pub type_: Type,
pub attributes: Vec<Attribute>,
}
impl VariableType {
impl ParameterType {
///
/// Returns `true` if this variable's value can be modified.
///
@ -38,11 +38,11 @@ impl VariableType {
}
}
impl From<Circuit> for VariableType {
impl From<Circuit> for ParameterType {
fn from(value: Circuit) -> Self {
let identifier = value.circuit_name;
VariableType {
ParameterType {
identifier: identifier.clone(),
type_: Type::Circuit(identifier),
attributes: vec![],
@ -50,11 +50,11 @@ impl From<Circuit> for VariableType {
}
}
impl From<Function> for VariableType {
impl From<Function> for ParameterType {
fn from(value: Function) -> Self {
let identifier = value.identifier;
VariableType {
ParameterType {
identifier: identifier.clone(),
type_: Type::Function(identifier.clone()),
attributes: vec![],
@ -62,9 +62,9 @@ impl From<Function> for VariableType {
}
}
impl From<FunctionInputVariableType> for VariableType {
impl From<FunctionInputVariableType> for ParameterType {
fn from(value: FunctionInputVariableType) -> Self {
VariableType {
ParameterType {
identifier: value.identifier,
type_: value.type_,
attributes: value.attributes,
@ -72,7 +72,7 @@ impl From<FunctionInputVariableType> for VariableType {
}
}
impl fmt::Display for VariableType {
impl fmt::Display for ParameterType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.identifier)
}

View File

@ -13,7 +13,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::{ResolvedNode, SymbolTable, TypeError};
use crate::{ResolvedNode, SymbolTable, TypeError, TypeVariable};
use leo_typed::{Identifier, IntegerType, Span, Type as UnresolvedType};
use serde::{Deserialize, Serialize};
@ -36,6 +36,9 @@ pub enum Type {
// User defined types
Circuit(Identifier),
Function(Identifier),
// Unknown type variables
TypeVariable(TypeVariable),
}
impl Type {
@ -204,6 +207,7 @@ impl fmt::Display for Type {
Type::Circuit(identifier) => write!(f, "circuit {}", identifier),
Type::Function(identifier) => write!(f, "function {}", identifier),
Type::TypeVariable(type_variable) => write!(f, "{}", type_variable),
}
}
}

View File

@ -0,0 +1,30 @@
// Copyright (C) 2019-2020 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 serde::{Deserialize, Serialize};
use std::fmt;
/// An unknown type in a Leo program.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TypeVariable {
name: String,
}
impl fmt::Display for TypeVariable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)
}
}