From 4fe91f0f391d938232f8ad89f293b89d5ab4f789 Mon Sep 17 00:00:00 2001 From: gluax Date: Thu, 20 May 2021 12:39:36 -0400 Subject: [PATCH] string canonicalization to char array --- ast/src/reducer/canonicalization.rs | 14 ++++++++++++++ ast/src/reducer/reconstructing_director.rs | 13 +++++++++---- ast/src/reducer/reconstructing_reducer.rs | 13 ++++++++----- compiler/src/phases/reducing_director.rs | 8 +++----- parser/src/tokenizer/lexer.rs | 2 +- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/ast/src/reducer/canonicalization.rs b/ast/src/reducer/canonicalization.rs index 7237d88d69..674c55ac5f 100644 --- a/ast/src/reducer/canonicalization.rs +++ b/ast/src/reducer/canonicalization.rs @@ -485,6 +485,20 @@ impl ReconstructingReducer for Canonicalizer { } } + fn reduce_string(&mut self, string: &[char], span: &Span) -> Result { + let mut elements = Vec::new(); + for character in string { + elements.push(SpreadOrExpression::Expression(Expression::Value( + ValueExpression::Char(*character, span.clone()), + ))); + } + + Ok(Expression::ArrayInline(ArrayInlineExpression { + elements, + span: span.clone(), + })) + } + fn reduce_array_init( &mut self, array_init: &ArrayInitExpression, diff --git a/ast/src/reducer/reconstructing_director.rs b/ast/src/reducer/reconstructing_director.rs index 942eaec3aa..bb727b4186 100644 --- a/ast/src/reducer/reconstructing_director.rs +++ b/ast/src/reducer/reconstructing_director.rs @@ -51,7 +51,7 @@ impl ReconstructingDirector { pub fn reduce_expression(&mut self, expression: &Expression) -> Result { let new = match expression { Expression::Identifier(identifier) => Expression::Identifier(self.reduce_identifier(&identifier)?), - Expression::Value(value) => Expression::Value(self.reduce_value(&value)?), + Expression::Value(value) => self.reduce_value(&value)?, Expression::Binary(binary) => Expression::Binary(self.reduce_binary(&binary)?), Expression::Unary(unary) => Expression::Unary(self.reduce_unary(&unary)?), Expression::Ternary(ternary) => Expression::Ternary(self.reduce_ternary(&ternary)?), @@ -100,12 +100,17 @@ impl ReconstructingDirector { self.reducer.reduce_group_value(group_value, new) } - pub fn reduce_value(&mut self, value: &ValueExpression) -> Result { + pub fn reduce_string(&mut self, string: &[char], span: &Span) -> Result { + self.reducer.reduce_string(string, span) + } + + pub fn reduce_value(&mut self, value: &ValueExpression) -> Result { let new = match value { ValueExpression::Group(group_value) => { - ValueExpression::Group(Box::new(self.reduce_group_value(&group_value)?)) + Expression::Value(ValueExpression::Group(Box::new(self.reduce_group_value(&group_value)?))) } - _ => value.clone(), + ValueExpression::String(string, span) => self.reduce_string(&string, &span)?, + _ => Expression::Value(value.clone()), }; self.reducer.reduce_value(value, new) diff --git a/ast/src/reducer/reconstructing_reducer.rs b/ast/src/reducer/reconstructing_reducer.rs index 36c8972728..0af238d2b8 100644 --- a/ast/src/reducer/reconstructing_reducer.rs +++ b/ast/src/reducer/reconstructing_reducer.rs @@ -51,11 +51,14 @@ pub trait ReconstructingReducer { Ok(new) } - fn reduce_value( - &mut self, - _value: &ValueExpression, - new: ValueExpression, - ) -> Result { + fn reduce_string(&mut self, string: &[char], span: &Span) -> Result { + Ok(Expression::Value(ValueExpression::String( + string.to_vec(), + span.clone(), + ))) + } + + fn reduce_value(&mut self, _value: &ValueExpression, new: Expression) -> Result { Ok(new) } diff --git a/compiler/src/phases/reducing_director.rs b/compiler/src/phases/reducing_director.rs index 0bec948eb3..0138ca6d67 100644 --- a/compiler/src/phases/reducing_director.rs +++ b/compiler/src/phases/reducing_director.rs @@ -152,9 +152,7 @@ impl CombineAstAsgDirector { asg: &AsgExpression, ) -> Result { let new = match (ast, asg) { - (AstExpression::Value(value), AsgExpression::Constant(const_)) => { - AstExpression::Value(self.reduce_value(&value, &const_)?) - } + (AstExpression::Value(value), AsgExpression::Constant(const_)) => self.reduce_value(&value, &const_)?, (AstExpression::Binary(ast), AsgExpression::Binary(asg)) => { AstExpression::Binary(self.reduce_binary(&ast, &asg)?) } @@ -404,7 +402,7 @@ impl CombineAstAsgDirector { self.ast_reducer.reduce_unary(ast, inner, ast.op.clone()) } - pub fn reduce_value(&mut self, ast: &ValueExpression, asg: &AsgConstant) -> Result { + pub fn reduce_value(&mut self, ast: &ValueExpression, asg: &AsgConstant) -> Result { let mut new = ast.clone(); if self.options.type_inference_enabled() { @@ -444,7 +442,7 @@ impl CombineAstAsgDirector { } } - self.ast_reducer.reduce_value(ast, new) + self.ast_reducer.reduce_value(ast, AstExpression::Value(new)) } pub fn reduce_variable_ref( diff --git a/parser/src/tokenizer/lexer.rs b/parser/src/tokenizer/lexer.rs index d7d3512946..5f77eefb72 100644 --- a/parser/src/tokenizer/lexer.rs +++ b/parser/src/tokenizer/lexer.rs @@ -154,7 +154,7 @@ impl Token { } continue; } - + return (0, None); }