mirror of
https://github.com/AleoHQ/leo.git
synced 2024-09-22 12:39:52 +03:00
add println, debug, error to leo-types
This commit is contained in:
parent
82a98e1867
commit
364e7684ac
@ -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
|
||||
|
@ -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>,
|
||||
}
|
||||
|
@ -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::*;
|
||||
|
@ -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)?;
|
||||
|
@ -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
19
types/src/macros/debug.rs
Normal 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")
|
||||
}
|
||||
}
|
24
types/src/macros/formatted_container.rs
Normal file
24
types/src/macros/formatted_container.rs
Normal 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, "{{}}")
|
||||
}
|
||||
}
|
19
types/src/macros/formatted_error.rs
Normal file
19
types/src/macros/formatted_error.rs
Normal 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")
|
||||
}
|
||||
}
|
33
types/src/macros/formatted_macro.rs
Normal file
33
types/src/macros/formatted_macro.rs
Normal 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()),
|
||||
)
|
||||
}
|
||||
}
|
26
types/src/macros/formatted_parameter.rs
Normal file
26
types/src/macros/formatted_parameter.rs
Normal 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)
|
||||
}
|
||||
}
|
43
types/src/macros/formatted_string.rs
Normal file
43
types/src/macros/formatted_string.rs
Normal 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)
|
||||
}
|
||||
}
|
32
types/src/macros/macro_name.rs
Normal file
32
types/src/macros/macro_name.rs
Normal 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
23
types/src/macros/mod.rs
Normal 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::*;
|
19
types/src/macros/print_line.rs
Normal file
19
types/src/macros/print_line.rs
Normal 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")
|
||||
}
|
||||
}
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user