Skip to content

Commit

Permalink
Close #304 - Parse assignment operators / 'if' function as expression…
Browse files Browse the repository at this point in the history
… instead of statement
  • Loading branch information
sys27 committed Sep 21, 2020
1 parent 0789644 commit fa90f75
Show file tree
Hide file tree
Showing 23 changed files with 531 additions and 303 deletions.
24 changes: 12 additions & 12 deletions xFunc Grammar.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// It's just a reference grammar for xFunc (The implementaion is not completely equal to grammar).

statement = unaryAssign
/ binaryAssign
/ assign
/ def
/ undef
/ if
/ for
statement = for
/ while
/ exp

if = 'if' '(' conditional ',' exp (',' exp)* ')'
for = 'for' '(' statement ',' exp ',' conditional ',' exp ')'
while = 'while' '(' exp ',' conditional ')'

unaryAssign = variable ('++' / '--')
binaryAssign = variable ('+=' / '-=' / '*=' / '/=' / '<<=' / '>>=') exp

Expand All @@ -18,11 +16,13 @@ assign = assignmentKey ':=' exp
def = ('def' / 'define') '(' assignmentKey ',' exp ')'
undef = ('undef' / 'undefine') '(' assignmentKey ')'

if = 'if' '(' conditional ',' exp (',' exp)* ')'
for = 'for' '(' statement ',' exp ',' conditional ',' statement ')'
while = 'while' '(' exp ',' conditional ')'

exp = ternary
exp = unaryAssign
/ binaryAssign
/ assign
/ def
/ undef
/ if
/ ternary

ternary = conditional ('?' exp ':' exp)*

Expand Down
4 changes: 2 additions & 2 deletions xFunc.Maths/Analyzers/TypeAnalyzers/TypeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public virtual ResultTypes Analyze(Ceil exp)
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
public virtual ResultTypes Analyze(Define exp)
=> CheckArgument(exp, ResultTypes.String);
=> CheckArgument(exp, ResultTypes.Undefined);

/// <summary>
/// Analyzes the specified expression.
Expand Down Expand Up @@ -1001,7 +1001,7 @@ public virtual ResultTypes Analyze(UnaryMinus exp)
/// <param name="exp">The expression.</param>
/// <returns>The result of analysis.</returns>
public virtual ResultTypes Analyze(Undefine exp)
=> CheckArgument(exp, ResultTypes.String);
=> CheckArgument(exp, ResultTypes.Undefined);

/// <summary>
/// Analyzes the specified expression.
Expand Down
11 changes: 9 additions & 2 deletions xFunc.Maths/Expressions/Collections/FunctionCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,17 @@ public new void Add(UserFunction key, IExpression value)
/// Removes the specified key.
/// </summary>
/// <param name="key">The key.</param>
public new void Remove(UserFunction key)
/// <returns>The function associated with <paramref name="key"/>.</returns>
public new IExpression Remove(UserFunction key)
{
if (base.Remove(key))
if (Remove(key, out var func))
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, key));

return func;
}

throw new KeyNotFoundException(string.Format(CultureInfo.InvariantCulture, Resource.FunctionNotFoundExceptionError, key));
}

/// <summary>
Expand Down
18 changes: 13 additions & 5 deletions xFunc.Maths/Expressions/Collections/ParameterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,31 +211,39 @@ public void Add(string key, object value)
/// Removes the specified element from this object.
/// </summary>
/// <param name="param">The element.</param>
/// <returns>The value of deleted parameter.</returns>
/// <exception cref="ArgumentNullException"><paramref name="param"/> is null.</exception>
/// <exception cref="ParameterIsReadOnlyException">The variable is read only.</exception>
public void Remove(Parameter param)
public object Remove(Parameter param)
{
if (param == null)
throw new ArgumentNullException(nameof(param));
if (param.Type == ParameterType.Constant)
throw new ArgumentException(Resource.ConstError);

collection.Remove(param.Key);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, param));
if (collection.Remove(param.Key, out var value))
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, param));

return value.Value;
}

throw new KeyNotFoundException(string.Format(CultureInfo.InvariantCulture, Resource.VariableNotFoundExceptionError, param.Key));
}

/// <summary>
/// Removes the specified element from this object.
/// </summary>
/// <param name="key">The name of variable.</param>
/// <returns>The value of deleted parameter.</returns>
/// <exception cref="ArgumentNullException"><paramref name="key" /> is null.</exception>
/// <exception cref="ParameterIsReadOnlyException">The variable is read only.</exception>
public void Remove(string key)
public object Remove(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));

Remove(GetParameterByKey(key));
return Remove(GetParameterByKey(key));
}

/// <summary>
Expand Down
10 changes: 2 additions & 8 deletions xFunc.Maths/Expressions/Define.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,9 @@ public object Execute(ExpressionParameters? parameters)
throw new ArgumentNullException(nameof(parameters));

if (key is Variable variable)
{
parameters.Variables[variable.Name] = value.Execute(parameters);

return string.Format(CultureInfo.InvariantCulture, Resource.AssignVariable, key, value);
}

parameters.Functions[(UserFunction)key] = value;
return parameters.Variables[variable.Name] = value.Execute(parameters);

return string.Format(CultureInfo.InvariantCulture, Resource.AssignFunction, key, value);
return parameters.Functions[(UserFunction)key] = value;
}

/// <summary>
Expand Down
7 changes: 1 addition & 6 deletions xFunc.Maths/Expressions/Programming/AddAssign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public override object Execute(ExpressionParameters? parameters)
{
var rightResult = Value.Execute(parameters);
if (rightResult is double rightValue)
{
var newValue = value + rightValue;
parameters.Variables[Variable.Name] = newValue;

return newValue;
}
return parameters.Variables[Variable.Name] = value + rightValue;

throw new ResultIsNotSupportedException(this, rightResult);
}
Expand Down
13 changes: 5 additions & 8 deletions xFunc.Maths/Expressions/Programming/Dec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ public override object Execute(ExpressionParameters? parameters)
throw new ArgumentNullException(nameof(parameters));

var result = Variable.Execute(parameters);
if (result is double value)
{
var newValue = value - 1;
parameters.Variables[Variable.Name] = newValue;

return newValue;
}

throw new ResultIsNotSupportedException(this, result);
return result switch
{
double value => parameters.Variables[Variable.Name] = --value,
_ => throw new ResultIsNotSupportedException(this, result),
};
}

/// <summary>
Expand Down
7 changes: 1 addition & 6 deletions xFunc.Maths/Expressions/Programming/DivAssign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public override object Execute(ExpressionParameters? parameters)
{
var rightResult = Value.Execute(parameters);
if (rightResult is double rightValue)
{
var newValue = value / rightValue;
parameters.Variables[Variable.Name] = newValue;

return newValue;
}
return parameters.Variables[Variable.Name] = value / rightValue;

throw new ResultIsNotSupportedException(this, rightResult);
}
Expand Down
13 changes: 5 additions & 8 deletions xFunc.Maths/Expressions/Programming/Inc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ public override object Execute(ExpressionParameters? parameters)
throw new ArgumentNullException(nameof(parameters));

var result = Variable.Execute(parameters);
if (result is double value)
{
var newValue = value + 1;
parameters.Variables[Variable.Name] = newValue;

return newValue;
}

throw new ResultIsNotSupportedException(this, result);
return result switch
{
double value => parameters.Variables[Variable.Name] = ++value,
_ => throw new ResultIsNotSupportedException(this, result),
};
}

/// <summary>
Expand Down
7 changes: 1 addition & 6 deletions xFunc.Maths/Expressions/Programming/LeftShiftAssign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public override object Execute(ExpressionParameters? parameters)
{
var rightResult = Value.Execute(parameters);
if (rightResult is double rightValue)
{
var newValue = value.LeftShift(rightValue);
parameters.Variables[Variable.Name] = newValue;

return newValue;
}
return parameters.Variables[Variable.Name] = value.LeftShift(rightValue);

throw new ResultIsNotSupportedException(this, rightResult);
}
Expand Down
7 changes: 1 addition & 6 deletions xFunc.Maths/Expressions/Programming/MulAssign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public override object Execute(ExpressionParameters? parameters)
{
var rightResult = Value.Execute(parameters);
if (rightResult is double rightValue)
{
var newValue = value * rightValue;
parameters.Variables[Variable.Name] = newValue;

return newValue;
}
return parameters.Variables[Variable.Name] = value * rightValue;

throw new ResultIsNotSupportedException(this, rightResult);
}
Expand Down
7 changes: 1 addition & 6 deletions xFunc.Maths/Expressions/Programming/RightShiftAssign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public override object Execute(ExpressionParameters? parameters)
{
var rightResult = Value.Execute(parameters);
if (rightResult is double rightValue)
{
var newValue = value.RightShift(rightValue);
parameters.Variables[Variable.Name] = newValue;

return newValue;
}
return parameters.Variables[Variable.Name] = value.RightShift(rightValue);

throw new ResultIsNotSupportedException(this, rightResult);
}
Expand Down
7 changes: 1 addition & 6 deletions xFunc.Maths/Expressions/Programming/SubAssign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public override object Execute(ExpressionParameters? parameters)
{
var rightResult = Value.Execute(parameters);
if (rightResult is double rightValue)
{
var newValue = value - rightValue;
parameters.Variables[Variable.Name] = newValue;

return newValue;
}
return parameters.Variables[Variable.Name] = value - rightValue;

throw new ResultIsNotSupportedException(this, rightResult);
}
Expand Down
10 changes: 2 additions & 8 deletions xFunc.Maths/Expressions/Undefine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,9 @@ public object Execute(ExpressionParameters? parameters)
throw new ArgumentNullException(nameof(parameters));

if (key is Variable variable)
{
parameters.Variables.Remove(variable.Name);

return string.Format(CultureInfo.InvariantCulture, Resource.UndefineVariable, key);
}

parameters.Functions.Remove((UserFunction)key);
return parameters.Variables.Remove(variable.Name);

return string.Format(CultureInfo.InvariantCulture, Resource.UndefineFunction, key);
return parameters.Functions.Remove((UserFunction)key);
}

/// <summary>
Expand Down

0 comments on commit fa90f75

Please sign in to comment.