Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions CSharp/Interpreter/ArcscriptExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class Expression : ArcscriptExpressionBase, IComparable
{
public object Value { get; set; }
public Expression() { Value = null; }

Check warning on line 10 in CSharp/Interpreter/ArcscriptExpression.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable property 'Value' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 10 in CSharp/Interpreter/ArcscriptExpression.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Cannot convert null literal to non-nullable reference type.
public Expression(object value) { Value = value; }
public Expression(object value, Type type) { Value = Convert.ChangeType(value, type); }
public Expression(Expression expression) { Value = expression.Value; }
Expand Down Expand Up @@ -93,6 +93,28 @@
}
return new Expression(result);
}

public static Expression operator %(Expression first, Expression second)
{
if (first.Type() == typeof(string) || second.Type() == typeof(string))
{
throw new InvalidOperationException("Modulo is not supported for string types.");
}
var doubleValues = GetDoubleValues(first.Value, second.Value);
if (doubleValues.Value2 == 0)
{
throw new DivideByZeroException("Modulo by zero.");
}
if (!doubleValues.HasDouble)
{
var intValue = (int)(doubleValues.Value1 % doubleValues.Value2);
return new Expression(intValue);
}
else
{
return new Expression(doubleValues.Value1 % doubleValues.Value2);
}
}

public static bool operator ==(Expression first, Expression second)
{
Expand All @@ -113,8 +135,12 @@
return false;
}

public int CompareTo(object other)
public int CompareTo(object? other)
{
if (other == null || !(other is Expression))
{
throw new ArgumentException("Object is not an Expression");
}
Expression o = (Expression)other;
DoubleValues fValues = GetDoubleValues(this.Value, o.Value);
double result = fValues.Value1 - fValues.Value2;
Expand All @@ -128,7 +154,7 @@
return 0;
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
if (obj == null || !(obj is Expression))
{
Expand Down
11 changes: 9 additions & 2 deletions CSharp/Interpreter/ArcscriptVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public override object VisitStatement_assignment([NotNull] ArcscriptParser.State
variableValue *= compound_condition_or;
} else if ( context.ASSIGNDIV() != null ) {
variableValue /= compound_condition_or;
} else if (context.ASSIGNMOD() != null) {
variableValue %= compound_condition_or;
}

this.state.SetVarValue(variableName, variableValue.Value);
Expand Down Expand Up @@ -281,8 +283,13 @@ public override object VisitMultiplicative_numeric_expression([NotNull] Arcscrip
if ( context.MUL() != null ) {
return result * signed_unary_num_expr;
}
// Else DIV
return result / signed_unary_num_expr;

if (context.DIV() != null)
{
return result / signed_unary_num_expr;
}
// Else MOD
return result % signed_unary_num_expr;
}

return (Expression)this.VisitSigned_unary_numeric_expression(context.signed_unary_numeric_expression());
Expand Down
12 changes: 12 additions & 0 deletions CSharp/__tests__/runtimeErrors.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
{
"code": "<pre><code>w -= _a</code></pre>",
"error": "runtime"
},
{
"code": "<pre><code>z = 10 % 0</code></pre>",
"error": "runtime"
},
{
"code": "<pre><code>x %= 0</code></pre>",
"error": "runtime"
},
{
"code": "<pre><code>y = 5 % \"test\"</code></pre>",
"error": "runtime"
}
]
}
36 changes: 36 additions & 0 deletions CSharp/__tests__/valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,42 @@
"var1": 8
}
},
{
"code": "<pre><code>x = 10 % 3</code></pre>",
"changes": {
"var1": 1
}
},
{
"code": "<pre><code>x = 10 % 3 * 2</code></pre>",
"changes": {
"var1": 2
}
},
{
"code": "<pre><code>x = 10 % (3 * 2)</code></pre>",
"changes": {
"var1": 4
}
},
{
"code": "<pre><code>x = 5 + 6 % 3</code></pre>",
"changes": {
"var1": 5
}
},
{
"code": "<pre><code>x %= 6</code></pre>",
"changes": {
"var1": 2
}
},
{
"code": "<pre><code>x = 10 % 7 % 2</code></pre>",
"changes": {
"var1": 1
}
},
{
"code": "<pre><code>x = 3 / 4 * 5</code></pre>",
"changes": {
Expand Down
12 changes: 12 additions & 0 deletions Cpp/demo/tests/runtimeErrors.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
{
"code": "<pre><code>w -= _a</code></pre>",
"error": "runtime"
},
{
"code": "<pre><code>z = 10 % 0</code></pre>",
"error": "runtime"
},
{
"code": "<pre><code>x %= 0</code></pre>",
"error": "runtime"
},
{
"code": "<pre><code>y = 5 % \"test\"</code></pre>",
"error": "runtime"
}
]
}
36 changes: 36 additions & 0 deletions Cpp/demo/tests/valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,42 @@
"var1": 8
}
},
{
"code": "<pre><code>x = 10 % 3</code></pre>",
"changes": {
"var1": 1
}
},
{
"code": "<pre><code>x = 10 % 3 * 2</code></pre>",
"changes": {
"var1": 2
}
},
{
"code": "<pre><code>x = 10 % (3 * 2)</code></pre>",
"changes": {
"var1": 4
}
},
{
"code": "<pre><code>x = 5 + 6 % 3</code></pre>",
"changes": {
"var1": 5
}
},
{
"code": "<pre><code>x %= 6</code></pre>",
"changes": {
"var1": 2
}
},
{
"code": "<pre><code>x = 10 % 7 % 2</code></pre>",
"changes": {
"var1": 1
}
},
{
"code": "<pre><code>x = 3 / 4 * 5</code></pre>",
"changes": {
Expand Down
Loading
Loading