remove zokrates progam

This commit is contained in:
collin 2020-04-20 18:53:27 -07:00
parent ff4fc0cc20
commit 26d8010080
5 changed files with 0 additions and 416 deletions

View File

@ -1,17 +0,0 @@
//! Module containing structs and types that make up a zokrates_program.
//!
//! @file zokrates_program.rs
//! @author Collin Chin <collin@aleo.org>
//! @date 2020
pub mod program;
pub use self::program::*;
pub mod types;
pub use self::types::*;
pub mod types_display;
pub use self::types_display::*;
pub mod types_from;
pub use self::types_from::*;

View File

@ -1,94 +0,0 @@
//! A zokrates_program consists of nodes that keep track of position and wrap zokrates_program types.
//!
//! @file zokrates_program.rs
//! @author Collin Chin <collin@aleo.org>
//! @date 2020
use crate::zokrates_program::{Expression, ExpressionList, Statement, StatementNode, Variable};
use pest::Span;
use std::fmt;
use std::fmt::Formatter;
/// Position in input file
#[derive(Clone, Copy)]
pub struct Position {
pub line: usize,
pub column: usize,
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", self.line, self.column)
}
}
impl fmt::Debug for Position {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}:{}", self.line, self.column)
}
}
/// Building blocks for a zokrates_program
#[derive(Debug, Clone)]
pub struct Node<T: fmt::Display> {
start: Position,
end: Position,
value: T,
}
impl<T: fmt::Display> Node<T> {
pub fn new(start: Position, end: Position, value: T) -> Node<T> {
Self { start, end, value }
}
}
impl<T: fmt::Display> fmt::Display for Node<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.value)
}
}
impl<T: NodeValue> std::cmp::PartialEq for Node<T> {
fn eq(&self, other: &Node<T>) -> bool {
self.value.eq(&other.value)
}
}
pub trait NodeValue: fmt::Display + Sized + PartialEq {
fn span(self, span: Span) -> Node<Self> {
let start = span.start_pos().line_col();
let end = span.end_pos().line_col();
let start = Position {
line: start.0,
column: start.1,
};
let end = Position {
line: end.0,
column: end.1,
};
Node::new(start, end, self)
}
}
impl<V: NodeValue> From<V> for Node<V> {
fn from(v: V) -> Self {
let mock_position = Position { line: 1, column: 1 };
Self::new(mock_position, mock_position, v)
}
}
impl<'ast> NodeValue for Expression<'ast> {}
impl<'ast> NodeValue for ExpressionList<'ast> {}
impl<'ast> NodeValue for Statement<'ast> {}
impl<'ast> NodeValue for Variable<'ast> {}
/// A collection of nodes created from an abstract syntax tree.
#[derive(Debug)]
pub struct Program<'ast> {
pub nodes: Vec<StatementNode<'ast>>,
}

View File

@ -1,62 +0,0 @@
//! A zokrates_program type constrains data that an expression might take.
//!
//! @file zokrates_program.rs
//! @author Collin Chin <collin@aleo.org>
//! @date 2020
use crate::zokrates_program::Node;
pub type ExpressionNode<'ast> = Node<Expression<'ast>>;
pub type ExpressionListNode<'ast> = Node<ExpressionList<'ast>>;
pub type StatementNode<'ast> = Node<Statement<'ast>>;
pub type VariableNode<'ast> = Node<Variable<'ast>>;
/// Identifier string
pub type Identifier<'ast> = &'ast str;
/// Program variable
#[derive(Debug, Clone, PartialEq)]
pub struct Variable<'ast> {
pub id: Identifier<'ast>,
}
/// Program expression that evaluates to a value
#[derive(Debug, Clone, PartialEq)]
pub enum Expression<'ast> {
// Expression identifier
Identifier(Identifier<'ast>),
// Values
Boolean(bool),
Field(Identifier<'ast>),
// Variable
Variable(VariableNode<'ast>),
// Not expression
Not(Box<ExpressionNode<'ast>>),
// Binary expression
Or(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
And(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
Eq(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
Neq(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
Geq(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
Gt(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
Leq(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
Lt(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
Add(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
Sub(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
Mul(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
Div(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
Pow(Box<ExpressionNode<'ast>>, Box<ExpressionNode<'ast>>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct ExpressionList<'ast> {
pub expressions: Vec<ExpressionNode<'ast>>,
}
/// Program statement that defines some action (or expression) to be carried out
#[derive(Debug, PartialEq, Clone)]
pub enum Statement<'ast> {
Declaration(VariableNode<'ast>),
Definition(VariableNode<'ast>, ExpressionNode<'ast>),
Return(ExpressionListNode<'ast>),
}

View File

@ -1,64 +0,0 @@
//! Format display functions for zokrates_program types.
//!
//! @file zokrates_program.rs
//! @author Collin Chin <collin@aleo.org>
//! @date 2020
use crate::zokrates_program::{Expression, ExpressionList, Statement, Variable};
use std::fmt;
impl<'ast> fmt::Display for Variable<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.id)
}
}
impl<'ast> fmt::Display for Expression<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expression::Identifier(ref s) => write!(f, "Identifier({:?})", s),
Expression::Boolean(ref b) => write!(f, "Boolean({:?})", b),
Expression::Field(ref s) => write!(f, "Field({:?})", s),
Expression::Variable(ref v) => write!(f, "{}", v),
Expression::Not(ref e) => write!(f, "{}", e),
Expression::Or(ref lhs, ref rhs) => write!(f, "{} || {}", lhs, rhs),
Expression::And(ref lhs, ref rhs) => write!(f, "{} && {}", lhs, rhs),
Expression::Eq(ref lhs, ref rhs) => write!(f, "{} == {}", lhs, rhs),
Expression::Neq(ref lhs, ref rhs) => write!(f, "{} != {}", lhs, rhs),
Expression::Geq(ref lhs, ref rhs) => write!(f, "{} >= {}", lhs, rhs),
Expression::Gt(ref lhs, ref rhs) => write!(f, "{} > {}", lhs, rhs),
Expression::Leq(ref lhs, ref rhs) => write!(f, "{} <= {}", lhs, rhs),
Expression::Lt(ref lhs, ref rhs) => write!(f, "{} < {}", lhs, rhs),
Expression::Add(ref lhs, ref rhs) => write!(f, "{} + {}", lhs, rhs),
Expression::Sub(ref lhs, ref rhs) => write!(f, "{} - {}", lhs, rhs),
Expression::Mul(ref lhs, ref rhs) => write!(f, "{} * {}", lhs, rhs),
Expression::Div(ref lhs, ref rhs) => write!(f, "{} / {}", lhs, rhs),
Expression::Pow(ref lhs, ref rhs) => write!(f, "{} ** {}", lhs, rhs),
}
}
}
impl<'ast> fmt::Display for ExpressionList<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, expression) in self.expressions.iter().enumerate() {
write!(f, "{}", expression)?;
if i < self.expressions.len() - 1 {
write!(f, ", ")?;
}
}
write!(f, "")
}
}
impl<'ast> fmt::Display for Statement<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Statement::Return(ref expressions) => write!(f, "return {}", expressions),
Statement::Declaration(ref variable) => write!(f, "{}", variable),
Statement::Definition(ref variable, ref expression) => {
write!(f, "{} = {}", variable, expression)
}
}
}
}

View File

@ -1,179 +0,0 @@
//! Logic to convert from an abstract syntax tree (ast) representation to a typed zokrates_program.
//!
//! @file zokrates_program.rs
//! @author Collin Chin <collin@aleo.org>
//! @date 2020
use crate::{
ast,
zokrates_program::{program, types, NodeValue},
};
impl<'ast> From<ast::Boolean<'ast>> for types::ExpressionNode<'ast> {
fn from(boolean: ast::Boolean<'ast>) -> Self {
types::Expression::Boolean(
boolean
.value
.parse::<bool>()
.expect("unable to parse boolean"),
)
.span(boolean.span)
}
}
impl<'ast> From<ast::Field<'ast>> for types::ExpressionNode<'ast> {
fn from(field: ast::Field<'ast>) -> Self {
types::Expression::Field(field.span.as_str()).span(field.span)
}
}
impl<'ast> From<ast::Value<'ast>> for types::ExpressionNode<'ast> {
fn from(value: ast::Value<'ast>) -> Self {
match value {
ast::Value::Boolean(boolean) => types::ExpressionNode::from(boolean),
ast::Value::Field(field) => types::ExpressionNode::from(field),
}
}
}
impl<'ast> From<ast::Variable<'ast>> for types::VariableNode<'ast> {
fn from(variable: ast::Variable<'ast>) -> Self {
types::Variable {
id: variable.span.as_str(),
}
.span(variable.span)
}
}
impl<'ast> From<ast::Variable<'ast>> for types::ExpressionNode<'ast> {
fn from(variable: ast::Variable<'ast>) -> Self {
types::Expression::Variable(types::VariableNode::from(variable.clone())).span(variable.span)
}
}
impl<'ast> From<ast::NotExpression<'ast>> for types::ExpressionNode<'ast> {
fn from(expression: ast::NotExpression<'ast>) -> Self {
types::Expression::Not(Box::new(types::ExpressionNode::from(
*expression.expression,
)))
.span(expression.span)
}
}
impl<'ast> From<ast::BinaryExpression<'ast>> for types::ExpressionNode<'ast> {
fn from(expression: ast::BinaryExpression<'ast>) -> Self {
match expression.operation {
ast::BinaryOperator::Or => types::Expression::Or(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
ast::BinaryOperator::And => types::Expression::And(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
ast::BinaryOperator::Eq => types::Expression::Eq(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
ast::BinaryOperator::Neq => types::Expression::Neq(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
ast::BinaryOperator::Geq => types::Expression::Geq(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
ast::BinaryOperator::Gt => types::Expression::Gt(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
ast::BinaryOperator::Leq => types::Expression::Leq(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
ast::BinaryOperator::Lt => types::Expression::Lt(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
ast::BinaryOperator::Add => types::Expression::Add(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
ast::BinaryOperator::Sub => types::Expression::Sub(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
ast::BinaryOperator::Mul => types::Expression::Mul(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
ast::BinaryOperator::Div => types::Expression::Div(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
ast::BinaryOperator::Pow => types::Expression::Pow(
Box::new(types::ExpressionNode::from(*expression.left)),
Box::new(types::ExpressionNode::from(*expression.right)),
),
}
.span(expression.span)
}
}
impl<'ast> From<ast::Expression<'ast>> for types::ExpressionNode<'ast> {
fn from(expression: ast::Expression<'ast>) -> Self {
match expression {
ast::Expression::Value(expression) => types::ExpressionNode::from(expression),
ast::Expression::Variable(expression) => types::ExpressionNode::from(expression),
ast::Expression::Not(expression) => types::ExpressionNode::from(expression),
ast::Expression::Binary(expression) => types::ExpressionNode::from(expression),
}
}
}
impl<'ast> From<ast::AssignStatement<'ast>> for types::StatementNode<'ast> {
fn from(statement: ast::AssignStatement<'ast>) -> Self {
types::Statement::Definition(
types::VariableNode::from(statement.variable),
types::ExpressionNode::from(statement.expression),
)
.span(statement.span)
}
}
impl<'ast> From<ast::ReturnStatement<'ast>> for types::StatementNode<'ast> {
fn from(statement: ast::ReturnStatement<'ast>) -> Self {
types::Statement::Return(
types::ExpressionList {
expressions: statement
.expressions
.into_iter()
.map(|expression| types::ExpressionNode::from(expression))
.collect(),
}
.span(statement.span.clone()),
)
.span(statement.span)
}
}
impl<'ast> From<ast::Statement<'ast>> for types::StatementNode<'ast> {
fn from(statement: ast::Statement<'ast>) -> Self {
match statement {
ast::Statement::Assign(statement) => types::StatementNode::from(statement),
ast::Statement::Return(statement) => types::StatementNode::from(statement),
}
}
}
impl<'ast> From<ast::File<'ast>> for program::Program<'ast> {
fn from(file: ast::File<'ast>) -> Self {
program::Program {
nodes: file
.statements
.iter()
.map(|statement| types::StatementNode::from(statement.clone()))
.collect(),
}
}
}