mirror of
https://github.com/AleoHQ/leo.git
synced 2024-11-29 03:35:10 +03:00
impl console functions in typed
This commit is contained in:
parent
34d09df92e
commit
f3ee68be2b
@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
ast::Rule,
|
||||
console::{ConsoleAssert, ConsoleDebug, ConsoleError, ConsolePrint},
|
||||
console::{ConsoleAssert, ConsoleDebug, ConsoleError, ConsoleLog},
|
||||
};
|
||||
|
||||
use pest_ast::FromPest;
|
||||
@ -13,7 +13,7 @@ pub enum ConsoleFunction<'ast> {
|
||||
Assert(ConsoleAssert<'ast>),
|
||||
Debug(ConsoleDebug<'ast>),
|
||||
Error(ConsoleError<'ast>),
|
||||
Print(ConsolePrint<'ast>),
|
||||
Log(ConsoleLog<'ast>),
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for ConsoleFunction<'ast> {
|
||||
@ -22,7 +22,7 @@ impl<'ast> fmt::Display for ConsoleFunction<'ast> {
|
||||
ConsoleFunction::Assert(assert) => write!(f, "{}", assert),
|
||||
ConsoleFunction::Debug(debug) => write!(f, "{}", debug),
|
||||
ConsoleFunction::Error(error) => write!(f, "{}", error),
|
||||
ConsoleFunction::Print(print) => write!(f, "{}", print),
|
||||
ConsoleFunction::Log(log) => write!(f, "{}", log),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,16 +6,16 @@ use serde::Serialize;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
|
||||
#[pest_ast(rule(Rule::console_print))]
|
||||
pub struct ConsolePrint<'ast> {
|
||||
#[pest_ast(rule(Rule::console_log))]
|
||||
pub struct ConsoleLog<'ast> {
|
||||
pub string: FormattedString<'ast>,
|
||||
#[pest_ast(outer())]
|
||||
#[serde(with = "SpanDef")]
|
||||
pub span: Span<'ast>,
|
||||
}
|
||||
|
||||
impl<'ast> fmt::Display for ConsolePrint<'ast> {
|
||||
impl<'ast> fmt::Display for ConsoleLog<'ast> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "print({})", self.string)
|
||||
write!(f, "log({})", self.string)
|
||||
}
|
||||
}
|
@ -16,8 +16,8 @@ pub use console_function_call::*;
|
||||
pub mod console_keyword;
|
||||
pub use console_keyword::*;
|
||||
|
||||
pub mod console_print;
|
||||
pub use console_print::*;
|
||||
pub mod console_log;
|
||||
pub use console_log::*;
|
||||
|
||||
pub mod formatted_container;
|
||||
pub use formatted_container::*;
|
||||
|
@ -424,7 +424,7 @@ console_function = {
|
||||
console_assert
|
||||
| console_debug
|
||||
| console_error
|
||||
| console_print
|
||||
| console_log
|
||||
}
|
||||
|
||||
// Declared in console/console_assert.rs
|
||||
@ -436,8 +436,8 @@ console_debug = !{"debug(" ~ formatted_string? ~ ")"}
|
||||
// Declared in console/console_error.rs
|
||||
console_error = !{"error(" ~ formatted_string? ~ ")"}
|
||||
|
||||
// Declared in console/console_print.rs
|
||||
console_print = !{"print(" ~ formatted_string? ~ ")"}
|
||||
// Declared in console/console_log.rs
|
||||
console_log = !{"log(" ~ formatted_string? ~ ")"}
|
||||
|
||||
// Declared in console/formatted_string.rs
|
||||
formatted_string = {
|
||||
|
@ -77,13 +77,13 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
|
||||
results.append(&mut result);
|
||||
}
|
||||
Statement::AssertEq(left, right, span) => {
|
||||
Statement::Assert(left, right, span) => {
|
||||
let (resolved_left, resolved_right) =
|
||||
self.enforce_binary_expression(cs, file_scope, function_scope, None, left, right, span.clone())?;
|
||||
|
||||
self.enforce_assert_eq_statement(cs, indicator, &resolved_left, &resolved_right, span)?;
|
||||
}
|
||||
Statement::Macro(macro_) => {
|
||||
Statement::Console(macro_) => {
|
||||
self.evaluate_macro(cs, file_scope, function_scope, macro_)?;
|
||||
}
|
||||
Statement::Expression(expression, span) => {
|
||||
|
65
typed/src/console/console_function.rs
Normal file
65
typed/src/console/console_function.rs
Normal file
@ -0,0 +1,65 @@
|
||||
use crate::{Expression, FormattedString};
|
||||
use leo_ast::console::{
|
||||
ConsoleAssert as AstConsoleAssert,
|
||||
ConsoleDebug as AstConsoleDebug,
|
||||
ConsoleError as AstConsoleError,
|
||||
ConsoleFunction as AstConsoleFunction,
|
||||
ConsoleLog as AstConsoleLog,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum ConsoleFunction {
|
||||
Assert(Expression),
|
||||
Debug(FormattedString),
|
||||
Error(FormattedString),
|
||||
Log(FormattedString),
|
||||
}
|
||||
|
||||
impl<'ast> From<AstConsoleFunction<'ast>> for ConsoleFunction {
|
||||
fn from(console_function: AstConsoleFunction<'ast>) -> Self {
|
||||
match console_function {
|
||||
AstConsoleFunction::Assert(assert) => ConsoleFunction::from(assert),
|
||||
AstConsoleFunction::Debug(debug) => ConsoleFunction::from(debug),
|
||||
AstConsoleFunction::Error(error) => ConsoleFunction::from(error),
|
||||
AstConsoleFunction::Log(log) => ConsoleFunction::from(log),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<AstConsoleAssert<'ast>> for ConsoleFunction {
|
||||
fn from(assert: AstConsoleAssert<'ast>) -> Self {
|
||||
ConsoleFunction::Assert(Expression::from(assert.expression))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<AstConsoleDebug<'ast>> for ConsoleFunction {
|
||||
fn from(debug: AstConsoleDebug<'ast>) -> Self {
|
||||
ConsoleFunction::Debug(FormattedString::from(debug.string))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<AstConsoleError<'ast>> for ConsoleFunction {
|
||||
fn from(error: AstConsoleError<'ast>) -> Self {
|
||||
ConsoleFunction::Error(FormattedString::from(error.string))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<AstConsoleLog<'ast>> for ConsoleFunction {
|
||||
fn from(log: AstConsoleLog<'ast>) -> Self {
|
||||
ConsoleFunction::Log(FormattedString::from(log.string))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ConsoleFunction {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
ConsoleFunction::Assert(assert) => write!(f, "assert({})", assert),
|
||||
ConsoleFunction::Debug(debug) => write!(f, "debug({})", debug),
|
||||
ConsoleFunction::Error(error) => write!(f, "error{})", error),
|
||||
ConsoleFunction::Log(log) => write!(f, "log({})", log),
|
||||
}
|
||||
}
|
||||
}
|
26
typed/src/console/console_function_call.rs
Normal file
26
typed/src/console/console_function_call.rs
Normal file
@ -0,0 +1,26 @@
|
||||
use crate::{ConsoleFunction, Span};
|
||||
use leo_ast::console::ConsoleFunctionCall as AstConsoleFunctionCall;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ConsoleFunctionCall {
|
||||
pub function: ConsoleFunction,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
impl<'ast> From<AstConsoleFunctionCall<'ast>> for ConsoleFunctionCall {
|
||||
fn from(console: AstConsoleFunctionCall<'ast>) -> Self {
|
||||
ConsoleFunctionCall {
|
||||
function: ConsoleFunction::from(console.function),
|
||||
span: Span::from(console.span),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ConsoleFunctionCall {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "console.{};", self.function)
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
use crate::Span;
|
||||
use leo_ast::macros::FormattedContainer as AstFormattedContainer;
|
||||
use leo_ast::console::FormattedContainer as AstFormattedContainer;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
@ -1,5 +1,5 @@
|
||||
use crate::{Expression, Span};
|
||||
use leo_ast::macros::FormattedParameter as AstFormattedParameter;
|
||||
use leo_ast::console::FormattedParameter as AstFormattedParameter;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
@ -1,5 +1,5 @@
|
||||
use crate::{FormattedContainer, FormattedParameter, Span};
|
||||
use leo_ast::macros::FormattedString as AstFormattedString;
|
||||
use leo_ast::console::FormattedString as AstFormattedString;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
14
typed/src/console/mod.rs
Normal file
14
typed/src/console/mod.rs
Normal file
@ -0,0 +1,14 @@
|
||||
pub mod console_function;
|
||||
pub use console_function::*;
|
||||
|
||||
pub mod console_function_call;
|
||||
pub use console_function_call::*;
|
||||
|
||||
pub mod formatted_container;
|
||||
pub use formatted_container::*;
|
||||
|
||||
pub mod formatted_parameter;
|
||||
pub use formatted_parameter::*;
|
||||
|
||||
pub mod formatted_string;
|
||||
pub use formatted_string::*;
|
@ -7,6 +7,9 @@ pub use self::circuits::*;
|
||||
pub mod common;
|
||||
pub use self::common::*;
|
||||
|
||||
pub mod console;
|
||||
pub use self::console::*;
|
||||
|
||||
pub mod errors;
|
||||
pub use self::errors::*;
|
||||
|
||||
@ -22,9 +25,6 @@ pub use self::imports::*;
|
||||
pub mod input;
|
||||
pub use self::input::*;
|
||||
|
||||
pub mod macros;
|
||||
pub use self::macros::*;
|
||||
|
||||
pub mod program;
|
||||
pub use self::program::*;
|
||||
|
||||
|
@ -1,19 +0,0 @@
|
||||
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")
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
use leo_ast::macros::Error as AstError;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ErrorMacro {}
|
||||
|
||||
impl<'ast> From<AstError<'ast>> for ErrorMacro {
|
||||
fn from(_error: AstError<'ast>) -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ErrorMacro {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "error")
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
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()),
|
||||
)
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
use crate::{Debug, ErrorMacro, Print};
|
||||
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(ErrorMacro),
|
||||
Print(Print),
|
||||
}
|
||||
|
||||
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(ErrorMacro::from(error)),
|
||||
AstMacroName::Print(print_line) => MacroName::Print(Print::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::Print(ref print_line) => write!(f, "{}", print_line),
|
||||
}
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
pub mod debug;
|
||||
pub use debug::*;
|
||||
|
||||
pub mod formatted_container;
|
||||
pub use formatted_container::*;
|
||||
|
||||
pub mod error_macro;
|
||||
pub use error_macro::*;
|
||||
|
||||
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;
|
||||
pub use print::*;
|
@ -1,19 +0,0 @@
|
||||
use leo_ast::macros::Print as AstPrint;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Print {}
|
||||
|
||||
impl<'ast> From<AstPrint<'ast>> for Print {
|
||||
fn from(_print: AstPrint<'ast>) -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Print {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "print")
|
||||
}
|
||||
}
|
@ -1,12 +1,12 @@
|
||||
use crate::{Assignee, ConditionalStatement, Declare, Expression, FormattedMacro, Identifier, Span, Variables};
|
||||
use crate::{Assignee, ConditionalStatement, ConsoleFunctionCall, Declare, Expression, Identifier, Span, Variables};
|
||||
use leo_ast::{
|
||||
console::ConsoleFunctionCall as AstConsoleFunctionCall,
|
||||
operations::AssignOperation,
|
||||
statements::{
|
||||
AssignStatement,
|
||||
DefinitionStatement,
|
||||
ExpressionStatement,
|
||||
ForStatement,
|
||||
MacroStatement,
|
||||
ReturnStatement,
|
||||
Statement as AstStatement,
|
||||
},
|
||||
@ -23,8 +23,7 @@ pub enum Statement {
|
||||
Assign(Assignee, Expression, Span),
|
||||
Conditional(ConditionalStatement, Span),
|
||||
Iteration(Identifier, Expression, Expression, Vec<Statement>, Span),
|
||||
AssertEq(Expression, Expression, Span),
|
||||
Macro(FormattedMacro),
|
||||
Console(ConsoleFunctionCall),
|
||||
Expression(Expression, Span),
|
||||
}
|
||||
|
||||
@ -139,16 +138,9 @@ impl<'ast> From<ForStatement<'ast>> for Statement {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast> From<MacroStatement<'ast>> for Statement {
|
||||
fn from(statement: MacroStatement<'ast>) -> Self {
|
||||
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)),
|
||||
}
|
||||
impl<'ast> From<AstConsoleFunctionCall<'ast>> for Statement {
|
||||
fn from(function_call: AstConsoleFunctionCall<'ast>) -> Self {
|
||||
Statement::Console(ConsoleFunctionCall::from(function_call))
|
||||
}
|
||||
}
|
||||
|
||||
@ -174,7 +166,7 @@ impl<'ast> From<AstStatement<'ast>> for Statement {
|
||||
Statement::Conditional(ConditionalStatement::from(statement), span)
|
||||
}
|
||||
AstStatement::Iteration(statement) => Statement::from(statement),
|
||||
AstStatement::Assert(statement) => Statement::from(statement),
|
||||
AstStatement::Console(console) => Statement::from(console),
|
||||
AstStatement::Expression(statement) => Statement::from(statement),
|
||||
}
|
||||
}
|
||||
@ -202,8 +194,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::Console(ref console) => write!(f, "{}", console),
|
||||
Statement::Expression(ref expression, ref _span) => write!(f, "{};", expression),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user