add println, debug, error to leo-types

This commit is contained in:
collin 2020-07-09 01:39:13 -07:00
parent 82a98e1867
commit 364e7684ac
16 changed files with 259 additions and 14 deletions

View File

@ -362,7 +362,7 @@ formatted_string = {
// Declared in macros/formatted_container.rs
formatted_container = { "{" ~ "}"}
// Declared in marcros/formatted_parameter.rs
// Declared in macros/formatted_parameter.rs
formatted_parameter = { expression }
// Declared in macros/macro_symbol.rs

View File

@ -7,7 +7,7 @@ use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq)]
#[pest_ast(rule(Rule::formatted_parameter))]
pub struct FormattedParameter<'ast> {
expression: Expression<'ast>,
pub expression: Expression<'ast>,
#[pest_ast(outer())]
pub span: Span<'ast>,
}

View File

@ -1,5 +1,5 @@
pub mod assert_statement;
pub use assert_statement::*;
pub mod macro_statement;
pub use macro_statement::*;
pub mod assign_statement;
pub use assign_statement::*;

View File

@ -78,6 +78,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
self.enforce_assert_eq_statement(cs, indicator, &resolved_left, &resolved_right, span)?;
}
Statement::Macro(macro_) => {
println!("{}", macro_);
}
Statement::Expression(expression, span) => {
let expression_string = expression.to_string();
let value = self.enforce_expression(cs, file_scope, function_scope, &vec![], expression)?;

View File

@ -19,6 +19,9 @@ pub use self::imports::*;
pub mod inputs;
pub use self::inputs::*;
pub mod macros;
pub use self::macros::*;
pub mod program;
pub use self::program::*;

19
types/src/macros/debug.rs Normal file
View File

@ -0,0 +1,19 @@
use leo_ast::macros::Debug as AstDebug;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Debug {}
impl<'ast> From<AstDebug<'ast>> for Debug {
fn from(_debug: AstDebug<'ast>) -> Self {
Self {}
}
}
impl fmt::Display for Debug {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "debug")
}
}

View File

@ -0,0 +1,24 @@
use crate::Span;
use leo_ast::macros::FormattedContainer as AstFormattedContainer;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FormattedContainer {
pub span: Span,
}
impl<'ast> From<AstFormattedContainer<'ast>> for FormattedContainer {
fn from(container: AstFormattedContainer<'ast>) -> Self {
Self {
span: Span::from(container.span),
}
}
}
impl fmt::Display for FormattedContainer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{{}}")
}
}

View File

@ -0,0 +1,19 @@
use leo_ast::macros::Error as AstError;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FormattedError {}
impl<'ast> From<AstError<'ast>> for FormattedError {
fn from(_error: AstError<'ast>) -> Self {
Self {}
}
}
impl fmt::Display for FormattedError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "error")
}
}

View File

@ -0,0 +1,33 @@
use crate::{FormattedString, MacroName, Span};
use leo_ast::macros::FormattedMacro as AstFormattedMacro;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FormattedMacro {
pub name: MacroName,
pub string: Option<FormattedString>,
pub span: Span,
}
impl<'ast> From<AstFormattedMacro<'ast>> for FormattedMacro {
fn from(formatted: AstFormattedMacro<'ast>) -> Self {
Self {
name: MacroName::from(formatted.name),
string: formatted.string.map(|string| FormattedString::from(string)),
span: Span::from(formatted.span),
}
}
}
impl fmt::Display for FormattedMacro {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}!({});",
self.name,
self.string.as_ref().map(|s| s.to_string()).unwrap_or("".to_string()),
)
}
}

View File

@ -0,0 +1,26 @@
use crate::{Expression, Span};
use leo_ast::macros::FormattedParameter as AstFormattedParameter;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FormattedParameter {
pub expression: Expression,
pub span: Span,
}
impl<'ast> From<AstFormattedParameter<'ast>> for FormattedParameter {
fn from(parameter: AstFormattedParameter<'ast>) -> Self {
Self {
expression: Expression::from(parameter.expression),
span: Span::from(parameter.span),
}
}
}
impl fmt::Display for FormattedParameter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.expression)
}
}

View File

@ -0,0 +1,43 @@
use crate::{FormattedContainer, FormattedParameter, Span};
use leo_ast::macros::FormattedString as AstFormattedString;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FormattedString {
pub string: String,
pub containers: Vec<FormattedContainer>,
pub parameters: Vec<FormattedParameter>,
pub span: Span,
}
impl<'ast> From<AstFormattedString<'ast>> for FormattedString {
fn from(formatted: AstFormattedString<'ast>) -> Self {
let string = formatted.string;
let span = Span::from(formatted.span);
let containers = formatted
.containers
.into_iter()
.map(|container| FormattedContainer::from(container))
.collect();
let parameters = formatted
.parameters
.into_iter()
.map(|parameter| FormattedParameter::from(parameter))
.collect();
Self {
string,
containers,
parameters,
span,
}
}
}
impl fmt::Display for FormattedString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.string)
}
}

View File

@ -0,0 +1,32 @@
use crate::{Debug, FormattedError, PrintLine};
use leo_ast::macros::MacroName as AstMacroName;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MacroName {
Debug(Debug),
Error(FormattedError),
PrintLine(PrintLine),
}
impl<'ast> From<AstMacroName<'ast>> for MacroName {
fn from(name: AstMacroName<'ast>) -> Self {
match name {
AstMacroName::Debug(debug) => MacroName::Debug(Debug::from(debug)),
AstMacroName::Error(error) => MacroName::Error(FormattedError::from(error)),
AstMacroName::PrintLine(print_line) => MacroName::PrintLine(PrintLine::from(print_line)),
}
}
}
impl fmt::Display for MacroName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
MacroName::Debug(ref debug) => write!(f, "{}", debug),
MacroName::Error(ref error) => write!(f, "{}", error),
MacroName::PrintLine(ref print_line) => write!(f, "{}", print_line),
}
}
}

23
types/src/macros/mod.rs Normal file
View File

@ -0,0 +1,23 @@
pub mod debug;
pub use debug::*;
pub mod formatted_container;
pub use formatted_container::*;
pub mod formatted_error;
pub use formatted_error::*;
pub mod formatted_macro;
pub use formatted_macro::*;
pub mod formatted_string;
pub use formatted_string::*;
pub mod formatted_parameter;
pub use formatted_parameter::*;
pub mod macro_name;
pub use macro_name::*;
pub mod print_line;
pub use print_line::*;

View File

@ -0,0 +1,19 @@
use leo_ast::macros::PrintLine as AstPrintLine;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PrintLine {}
impl<'ast> From<AstPrintLine<'ast>> for PrintLine {
fn from(_print_line: AstPrintLine<'ast>) -> Self {
Self {}
}
}
impl fmt::Display for PrintLine {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "println")
}
}

View File

@ -1,4 +1,4 @@
use crate::{Assignee, ConditionalStatement, Declare, Expression, Identifier, Span, Variable};
use crate::{Assignee, ConditionalStatement, Declare, Expression, FormattedMacro, Identifier, Span, Variable};
use leo_ast::{
common::Return,
operations::AssignOperation,
@ -27,6 +27,7 @@ pub enum Statement {
Conditional(ConditionalStatement, Span),
Iteration(Identifier, Expression, Expression, Vec<Statement>, Span),
AssertEq(Expression, Expression, Span),
Macro(FormattedMacro),
Expression(Expression, Span),
}
@ -170,15 +171,14 @@ impl<'ast> From<ForStatement<'ast>> for Statement {
impl<'ast> From<MacroStatement<'ast>> for Statement {
fn from(statement: MacroStatement<'ast>) -> Self {
println!("{}", statement);
// match statement {
// MacroStatement::AssertEq(assert_eq) => Statement::AssertEq(
// Expression::from(assert_eq.left),
// Expression::from(assert_eq.right),
// Span::from(assert_eq.span),
// ),
// }
unimplemented!()
match statement {
MacroStatement::AssertEq(assert_eq) => Statement::AssertEq(
Expression::from(assert_eq.left),
Expression::from(assert_eq.right),
Span::from(assert_eq.span),
),
MacroStatement::Formatted(formatted) => Statement::Macro(FormattedMacro::from(formatted)),
}
}
}
@ -247,6 +247,7 @@ impl fmt::Display for Statement {
write!(f, "\t}}")
}
Statement::AssertEq(ref left, ref right, ref _span) => write!(f, "assert_eq({}, {});", left, right),
Statement::Macro(ref macro_) => write!(f, "{}", macro_),
Statement::Expression(ref expression, ref _span) => write!(f, "{};", expression),
}
}