From 2327bca6179abc6a95033c67802470963e8f0ec7 Mon Sep 17 00:00:00 2001 From: collin <16715212+collinc97@users.noreply.github.com> Date: Thu, 26 May 2022 16:43:18 -0400 Subject: [PATCH] implement static string type --- compiler/ast/src/common/mod.rs | 3 ++ compiler/ast/src/common/static_string.rs | 33 +++++++++++++++++++ compiler/ast/src/expression/value.rs | 10 ++---- .../ast/src/passes/reconstructing_director.rs | 2 +- .../ast/src/passes/reconstructing_reducer.rs | 4 +-- .../src/statements/console/console_args.rs | 6 ++-- compiler/ast/src/types/type_.rs | 6 +++- compiler/parser/src/parser/statement.rs | 17 +++++----- compiler/parser/src/tokenizer/lexer.rs | 3 +- examples/hello-world/src/main.leo | 4 +-- .../compiler/compiler/console/error.leo.out | 4 +-- .../compiler/compiler/console/log.leo.out | 4 +-- .../compiler/console/log_conditional.leo.out | 4 +-- .../compiler/console/log_input.leo.out | 4 +-- .../compiler/console/log_parameter.leo.out | 4 +-- .../console/log_parameter_many.leo.out | 4 +-- .../program_input/main_group.leo.out | 4 +-- .../main_group.leo.out | 4 +-- 18 files changed, 78 insertions(+), 42 deletions(-) diff --git a/compiler/ast/src/common/mod.rs b/compiler/ast/src/common/mod.rs index ad45846431..ac948824b1 100644 --- a/compiler/ast/src/common/mod.rs +++ b/compiler/ast/src/common/mod.rs @@ -24,3 +24,6 @@ pub use imported_modules::*; pub mod positive_number; pub use positive_number::*; + +pub mod static_string; +pub use static_string::*; diff --git a/compiler/ast/src/common/static_string.rs b/compiler/ast/src/common/static_string.rs index e69de29bb2..bfee1a854e 100644 --- a/compiler/ast/src/common/static_string.rs +++ b/compiler/ast/src/common/static_string.rs @@ -0,0 +1,33 @@ +// Copyright (C) 2019-2022 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 . + +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct StaticString(String); + +impl StaticString { + pub fn new(string: String) -> Self { + Self(string) + } +} + +impl fmt::Display for StaticString { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} \ No newline at end of file diff --git a/compiler/ast/src/expression/value.rs b/compiler/ast/src/expression/value.rs index 8e10e1bf06..c64e6391da 100644 --- a/compiler/ast/src/expression/value.rs +++ b/compiler/ast/src/expression/value.rs @@ -15,7 +15,6 @@ // along with the Leo library. If not, see . use super::*; -use crate::Char; /// A literal expression. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -37,7 +36,7 @@ pub enum ValueExpression { /// An unsigned number followed by the keyword `scalar`. Scalar(String, #[serde(with = "leo_span::span_json")] Span), /// A string literal, e.g., `"foobar"`. - String(Vec, #[serde(with = "leo_span::span_json")] Span), + String(String, #[serde(with = "leo_span::span_json")] Span), } impl fmt::Display for ValueExpression { @@ -50,12 +49,7 @@ impl fmt::Display for ValueExpression { Group(group) => write!(f, "{}", group), Integer(type_, value, _) => write!(f, "{}{}", value, type_), Scalar(scalar, _) => write!(f, "{}", scalar), - String(string, _) => { - for character in string.iter() { - write!(f, "{}", character)?; - } - Ok(()) - } + String(string, _) => write!(f, "{}", string), } } } diff --git a/compiler/ast/src/passes/reconstructing_director.rs b/compiler/ast/src/passes/reconstructing_director.rs index 334fcb394e..aae08f6661 100644 --- a/compiler/ast/src/passes/reconstructing_director.rs +++ b/compiler/ast/src/passes/reconstructing_director.rs @@ -69,7 +69,7 @@ impl ReconstructingDirector { self.reducer.reduce_group_value(group_value, new) } - pub fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result { + pub fn reduce_string(&mut self, string: &String, span: &Span) -> Result { self.reducer.reduce_string(string, span) } diff --git a/compiler/ast/src/passes/reconstructing_reducer.rs b/compiler/ast/src/passes/reconstructing_reducer.rs index 8b09f97477..926336b01f 100644 --- a/compiler/ast/src/passes/reconstructing_reducer.rs +++ b/compiler/ast/src/passes/reconstructing_reducer.rs @@ -59,8 +59,8 @@ pub trait ReconstructingReducer { Ok(new) } - fn reduce_string(&mut self, string: &[Char], span: &Span) -> Result { - Ok(Expression::Value(ValueExpression::String(string.to_vec(), *span))) + fn reduce_string(&mut self, string: &String, span: &Span) -> Result { + Ok(Expression::Value(ValueExpression::String(string.clone(), *span))) } fn reduce_value(&mut self, _value: &ValueExpression, new: Expression) -> Result { diff --git a/compiler/ast/src/statements/console/console_args.rs b/compiler/ast/src/statements/console/console_args.rs index b6a4d4ec50..3e902f9a0f 100644 --- a/compiler/ast/src/statements/console/console_args.rs +++ b/compiler/ast/src/statements/console/console_args.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Char, Expression, Node}; +use crate::{Expression, Node, StaticString}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -24,7 +24,7 @@ use std::fmt; #[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] pub struct ConsoleArgs { /// The formatting string with `parameters` interpolated into it. - pub string: Vec, + pub string: StaticString, /// Parameters to interpolate in `string`. pub parameters: Vec, /// The span from `(` to `)`. @@ -36,7 +36,7 @@ impl fmt::Display for ConsoleArgs { write!( f, "\"{}\", {}", - self.string.iter().map(|x| x.to_string()).collect::>().join(""), + self.string, self.parameters .iter() .map(|p| p.to_string()) diff --git a/compiler/ast/src/types/type_.rs b/compiler/ast/src/types/type_.rs index ffc0209124..41f92b1f75 100644 --- a/compiler/ast/src/types/type_.rs +++ b/compiler/ast/src/types/type_.rs @@ -33,6 +33,8 @@ pub enum Type { Group, /// The `scalar` type. Scalar, + /// The `string` type. + String, /// An integer type. IntegerType(IntegerType), @@ -53,7 +55,8 @@ impl Type { | (Type::Boolean, Type::Boolean) | (Type::Field, Type::Field) | (Type::Group, Type::Group) - | (Type::Scalar, Type::Scalar) => true, + | (Type::Scalar, Type::Scalar) + | (Type::String, Type::String) => true, (Type::IntegerType(left), Type::IntegerType(right)) => left.eq(right), _ => false, } @@ -68,6 +71,7 @@ impl fmt::Display for Type { Type::Field => write!(f, "field"), Type::Group => write!(f, "group"), Type::Scalar => write!(f, "scalar"), + Type::String => write!(f, "string"), Type::IntegerType(ref integer_type) => write!(f, "{}", integer_type), Type::Err => write!(f, "error"), } diff --git a/compiler/parser/src/parser/statement.rs b/compiler/parser/src/parser/statement.rs index 738eac9da9..0efae6483c 100644 --- a/compiler/parser/src/parser/statement.rs +++ b/compiler/parser/src/parser/statement.rs @@ -148,18 +148,19 @@ impl ParserContext<'_> { /// Returns a [`ConsoleArgs`] AST node if the next tokens represent a formatted string. fn parse_console_args(&mut self) -> Result { - let mut string = None; + let mut static_string = None; let (parameters, _, span) = self.parse_paren_comma_list(|p| { - if string.is_none() { + if static_string.is_none() { p.bump(); let SpannedToken { token, span } = p.prev_token.clone(); - string = Some(match token { - Token::StaticString(chars) => chars, + match token { + Token::StaticString(string) => { + static_string = Some(StaticString::new(string)); + }, _ => { - p.emit_err(ParserError::unexpected_str(token, "formatted string", span)); - Vec::new() + p.emit_err(ParserError::unexpected_str(token, "formatted static_string", span)); } - }); + }; Ok(None) } else { p.parse_expression().map(Some) @@ -167,7 +168,7 @@ impl ParserContext<'_> { })?; Ok(ConsoleArgs { - string: string.unwrap_or_default(), + string: static_string.unwrap_or_default(), span, parameters, }) diff --git a/compiler/parser/src/tokenizer/lexer.rs b/compiler/parser/src/tokenizer/lexer.rs index e3c1ec0380..430e36ae4d 100644 --- a/compiler/parser/src/tokenizer/lexer.rs +++ b/compiler/parser/src/tokenizer/lexer.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::tokenizer::{Char, Token}; +use crate::tokenizer::Token; use leo_errors::{ParserError, Result}; use leo_span::{Span, Symbol}; use snarkvm_dpc::{prelude::*, testnet2::Testnet2}; @@ -185,6 +185,7 @@ impl Token { } Some('"') => { let mut string = String::from("\""); + input.next(); let mut ended = false; while let Some(c) = input.next() { diff --git a/examples/hello-world/src/main.leo b/examples/hello-world/src/main.leo index 97c29841d6..16244c18ff 100644 --- a/examples/hello-world/src/main.leo +++ b/examples/hello-world/src/main.leo @@ -1,3 +1,3 @@ -function main() { - console.log("Hello World!"); +function main(a: u8, b: u8) -> u8 { + return a + b; } diff --git a/tests/expectations/compiler/compiler/console/error.leo.out b/tests/expectations/compiler/compiler/console/error.leo.out index 2ce54190cf..4bfd57813a 100644 --- a/tests/expectations/compiler/compiler/console/error.leo.out +++ b/tests/expectations/compiler/compiler/console/error.leo.out @@ -4,5 +4,5 @@ expectation: Pass outputs: - output: - initial_input_ast: 6b9e5227fdce9f916cd2398ea85c2d7e7b2f7d706bfa730b8cd1acdeb3f168cd - initial_ast: e3b69d0b4355afd331edf8c572b64746bc763a714626043a9edc8eba42b08ec8 - symbol_table: d46f6eb98259f34d32a60788aa178efa34166bcc6ba1058e2ff5f8327a129b9c + initial_ast: 0ae1ebc0e62265d17e6b569f2f8ac5b6ce6514b9403c68ab581c44f1cfbe9c31 + symbol_table: 96b1b61a449645b155fdf9499ec0dfe7281795ae5bde4a57d081bdf4021bb72b diff --git a/tests/expectations/compiler/compiler/console/log.leo.out b/tests/expectations/compiler/compiler/console/log.leo.out index 7fa5f4bdca..7e06766ca6 100644 --- a/tests/expectations/compiler/compiler/console/log.leo.out +++ b/tests/expectations/compiler/compiler/console/log.leo.out @@ -4,5 +4,5 @@ expectation: Pass outputs: - output: - initial_input_ast: 89959164cbf734ac0d261c7459b9c1214eb2f4b3ab9ec57a0b22db487d6537e4 - initial_ast: cb79b9db64a7e92b85706e517b586b4fe9cd591850e7130cc7bfad6dd92b4d3f - symbol_table: 559484bc163178bf54b169f5dd573167771566aa993055b6a28f0c1a759339bc + initial_ast: f3a9cc35481db214372f311df86bdeff108ced2951f36c35bc5e3f4cf874b9fd + symbol_table: f09a4b65028c90ac4d6b84d217b187e692ac65ff713943b27f6ad44c2d154b45 diff --git a/tests/expectations/compiler/compiler/console/log_conditional.leo.out b/tests/expectations/compiler/compiler/console/log_conditional.leo.out index 479df480d2..ca4d41d6e3 100644 --- a/tests/expectations/compiler/compiler/console/log_conditional.leo.out +++ b/tests/expectations/compiler/compiler/console/log_conditional.leo.out @@ -5,5 +5,5 @@ outputs: - output: - initial_input_ast: 4132cf36ac66f6b23e249f81b5c2aafa58e4e5e945920cc29752edc5d6d8057f - initial_input_ast: 586ed72429932a1aafcd0f8eed983a4babff8eada9c028b88bbeef24dab1cbc0 - initial_ast: 0360f7fba87784c290f73693b81ca1b71e3259794f0fb3d3cbe39cc143e92d82 - symbol_table: 560afbb790df70bfc770d5c2f966428a37baf94a5c0f5312ad445456d33a2cd9 + initial_ast: 3b169c5b325765b378d7ea1a131c22d6581a5678c7612dd89d5b65dc9ea741c5 + symbol_table: 1d675fa8a7bd7ce83ba28eb5421a5455d633b4a79ab0332df2c6e2f51ed8da4e diff --git a/tests/expectations/compiler/compiler/console/log_input.leo.out b/tests/expectations/compiler/compiler/console/log_input.leo.out index ec01248f24..21b988a693 100644 --- a/tests/expectations/compiler/compiler/console/log_input.leo.out +++ b/tests/expectations/compiler/compiler/console/log_input.leo.out @@ -4,5 +4,5 @@ expectation: Pass outputs: - output: - initial_input_ast: 5411bd17943bb0aa7b0bb27e8b38f57fd27f06f2080b13a32eee50c53de66f6c - initial_ast: ab02de15b37b07d52d385468d72b03f8f9ecff3c9f130b8a3985be326f9f6edf - symbol_table: 720c2aafae77c261ed1640d4080f9a73657638baa30e54a5e10e2323b6f6eca0 + initial_ast: 6ac72cc3fb0f49ffb8b5e19ad7199fbefc43b4fda14bc81e72b9a8158da4c488 + symbol_table: f4cde37b60d4f9dd3f9c6d68adcd775a4a658aa0230638ae719c530e73a50aa1 diff --git a/tests/expectations/compiler/compiler/console/log_parameter.leo.out b/tests/expectations/compiler/compiler/console/log_parameter.leo.out index 2985e69ae9..9cfa62961b 100644 --- a/tests/expectations/compiler/compiler/console/log_parameter.leo.out +++ b/tests/expectations/compiler/compiler/console/log_parameter.leo.out @@ -4,5 +4,5 @@ expectation: Pass outputs: - output: - initial_input_ast: 18e8a4118829470d3150268bbf8d298954e1f566ea2d42ed9f3660dc25c23fcc - initial_ast: 4fa505d3542f2df2abcdbbf7d487ff65e1e311366fc2eaee286f8ba253876883 - symbol_table: e5159343ab03573032873783b28058a482dd401d534a0d3af03790a5286ba470 + initial_ast: d2b0bcc028f1faca5015149527326b012acae44c7f4eccedd2bb4e6ddd60d458 + symbol_table: b95e9b9a3c056c2b908593687e5e196733d23ff345a18ec0c71db9be3f79fed6 diff --git a/tests/expectations/compiler/compiler/console/log_parameter_many.leo.out b/tests/expectations/compiler/compiler/console/log_parameter_many.leo.out index 7f8a52b671..df5bbc6c1f 100644 --- a/tests/expectations/compiler/compiler/console/log_parameter_many.leo.out +++ b/tests/expectations/compiler/compiler/console/log_parameter_many.leo.out @@ -4,5 +4,5 @@ expectation: Pass outputs: - output: - initial_input_ast: caa45de3b412d749048b9be732a4593f3094c671701093103f580817a741acbb - initial_ast: 8dc13a9cb4d48b8a5e33d4e16a3071869a6ac7034962af5baf8f5f19a2b218dd - symbol_table: 757bb967973b87466c01be1a9dc78d30701964e0d234e0e65d1bbcbd3072370f + initial_ast: 85fd5d2885d1f52b613acce71636df9549406c4b65e716cabf207a84123ebc7e + symbol_table: 030ff41534ded0e7779554395f177c9a10bc1775954a48b97c410fbb0271eaf9 diff --git a/tests/expectations/compiler/compiler/input_files/program_input/main_group.leo.out b/tests/expectations/compiler/compiler/input_files/program_input/main_group.leo.out index 4344961255..0022a19c9d 100644 --- a/tests/expectations/compiler/compiler/input_files/program_input/main_group.leo.out +++ b/tests/expectations/compiler/compiler/input_files/program_input/main_group.leo.out @@ -4,5 +4,5 @@ expectation: Pass outputs: - output: - initial_input_ast: a610c3b16aefb8d1dcf55aa316fadd3da191a4045279d74de5667b795a079ba4 - initial_ast: 14b47cd09e5cca03ce44fe3f038294840fb2259aef8c912ed8e9f41494d62338 - symbol_table: 3b68eff86b45e4f940b0a1031e19a64178b0bf802acb59f3b743e664f3010876 + initial_ast: b3b74914a19f82eb0c7dd69a9bfa2323f65c0c1de2dfc8788e166c46af7e1b3b + symbol_table: 24bb3b7964ed9b1a081d18e792ff976523146390accc79275998448ac3c9a510 diff --git a/tests/expectations/compiler/compiler/input_files/program_input_constants/main_group.leo.out b/tests/expectations/compiler/compiler/input_files/program_input_constants/main_group.leo.out index 3e97178ef9..76ae8dd90b 100644 --- a/tests/expectations/compiler/compiler/input_files/program_input_constants/main_group.leo.out +++ b/tests/expectations/compiler/compiler/input_files/program_input_constants/main_group.leo.out @@ -4,5 +4,5 @@ expectation: Pass outputs: - output: - initial_input_ast: ab788992b0e08b3ba20bde1db2c473a4e006f06adefcddca936b148efff76b89 - initial_ast: 3558d89ef38d1a8f0f2b458b79c0b50aa9736d9c3d1af04435ac5f2253df2f44 - symbol_table: 14140f05d5fb8a85352940a67860fd36ed597f93ac882fdb76ef3d1ed89b5031 + initial_ast: 27eed50a20003ec963a52eb683c3795ee289b3cb71626aab64a9279a98261966 + symbol_table: e76eaaeb156f8c53fdb8f478f7ca9705c62075adc8b1bba7583da100bad2b6c6