string canonicalization to char array

This commit is contained in:
gluax 2021-05-20 12:39:36 -04:00
parent 86fc23942b
commit 4fe91f0f39
5 changed files with 35 additions and 15 deletions

View File

@ -485,6 +485,20 @@ impl ReconstructingReducer for Canonicalizer {
}
}
fn reduce_string(&mut self, string: &[char], span: &Span) -> Result<Expression, ReducerError> {
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,

View File

@ -51,7 +51,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_expression(&mut self, expression: &Expression) -> Result<Expression, ReducerError> {
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<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_group_value(group_value, new)
}
pub fn reduce_value(&mut self, value: &ValueExpression) -> Result<ValueExpression, ReducerError> {
pub fn reduce_string(&mut self, string: &[char], span: &Span) -> Result<Expression, ReducerError> {
self.reducer.reduce_string(string, span)
}
pub fn reduce_value(&mut self, value: &ValueExpression) -> Result<Expression, ReducerError> {
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)

View File

@ -51,11 +51,14 @@ pub trait ReconstructingReducer {
Ok(new)
}
fn reduce_value(
&mut self,
_value: &ValueExpression,
new: ValueExpression,
) -> Result<ValueExpression, ReducerError> {
fn reduce_string(&mut self, string: &[char], span: &Span) -> Result<Expression, ReducerError> {
Ok(Expression::Value(ValueExpression::String(
string.to_vec(),
span.clone(),
)))
}
fn reduce_value(&mut self, _value: &ValueExpression, new: Expression) -> Result<Expression, ReducerError> {
Ok(new)
}

View File

@ -152,9 +152,7 @@ impl<R: ReconstructingReducer, O: CombinerOptions> CombineAstAsgDirector<R, O> {
asg: &AsgExpression,
) -> Result<AstExpression, ReducerError> {
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<R: ReconstructingReducer, O: CombinerOptions> CombineAstAsgDirector<R, O> {
self.ast_reducer.reduce_unary(ast, inner, ast.op.clone())
}
pub fn reduce_value(&mut self, ast: &ValueExpression, asg: &AsgConstant) -> Result<ValueExpression, ReducerError> {
pub fn reduce_value(&mut self, ast: &ValueExpression, asg: &AsgConstant) -> Result<AstExpression, ReducerError> {
let mut new = ast.clone();
if self.options.type_inference_enabled() {
@ -444,7 +442,7 @@ impl<R: ReconstructingReducer, O: CombinerOptions> CombineAstAsgDirector<R, O> {
}
}
self.ast_reducer.reduce_value(ast, new)
self.ast_reducer.reduce_value(ast, AstExpression::Value(new))
}
pub fn reduce_variable_ref(

View File

@ -154,7 +154,7 @@ impl Token {
}
continue;
}
return (0, None);
}