Skip to content

Commit

Permalink
Implemented remaining math functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Nov 16, 2023
1 parent f88cfdb commit 4d30179
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
3 changes: 3 additions & 0 deletions StepLang/Expressions/Results/NumberResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public NumberResult(double value) : base(ResultType.Number, value)
public static implicit operator NumberResult(string value) => double.Parse(value, NumberStyles.Any, CultureInfo.InvariantCulture);

public static implicit operator int(NumberResult result) => (int)Math.Round(result.Value);
public static implicit operator uint(NumberResult result) => (uint)Math.Round(result.Value);

public static implicit operator double(NumberResult result) => result.Value;

Expand All @@ -74,6 +75,8 @@ public NumberResult(double value) : base(ResultType.Number, value)

public int ToInt32() => this;

public uint ToUInt32() => this;

public double ToDouble() => this;

public static NumberResult Add(NumberResult left, NumberResult right) => left + right;
Expand Down
2 changes: 1 addition & 1 deletion StepLang/Framework/Conversion/ToRadixFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected override ExpressionResult Invoke(TokenLocation callLocation, Interpret
{
return radix.Value switch
{
2 or 8 or 10 or 16 => (StringResult)Convert.ToString((long)number, radix).ToUpperInvariant(),
2 or 8 or 10 or 16 => (StringResult)Convert.ToString(number.ToUInt32(), radix).ToUpperInvariant(),
_ => NullResult.Instance,
};
}
Expand Down
57 changes: 50 additions & 7 deletions StepLang/Interpreting/Interpreter.Math.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using StepLang.Expressions.Results;
using StepLang.Parsing;
using StepLang.Tokenizing;
Expand Down Expand Up @@ -217,37 +218,79 @@ public ExpressionResult Evaluate(LogicalOrExpressionNode expressionNode)

public ExpressionResult Evaluate(BitwiseXorExpressionNode expressionNode)
{
throw new NotImplementedException();
var (left, right, location) = EvaluateBinary(expressionNode);

return left switch
{
NumberResult aNumber when right is NumberResult bNumber => (NumberResult)(aNumber ^ bNumber),
_ => throw new IncompatibleExpressionOperandsException(location, left, right, "bitwise xor"),
};
}

public ExpressionResult Evaluate(BitwiseAndExpressionNode expressionNode)
{
throw new NotImplementedException();
var (left, right, location) = EvaluateBinary(expressionNode);

return left switch
{
NumberResult aNumber when right is NumberResult bNumber => (NumberResult)(aNumber & bNumber),
_ => throw new IncompatibleExpressionOperandsException(location, left, right, "bitwise and"),
};
}

public ExpressionResult Evaluate(BitwiseOrExpressionNode expressionNode)
{
throw new NotImplementedException();
var (left, right, location) = EvaluateBinary(expressionNode);

return left switch
{
NumberResult aNumber when right is NumberResult bNumber => (NumberResult)(aNumber | bNumber),
_ => throw new IncompatibleExpressionOperandsException(location, left, right, "bitwise or"),
};
}

public ExpressionResult Evaluate(BitwiseShiftLeftExpressionNode expressionNode)
{
throw new NotImplementedException();
var (left, right, location) = EvaluateBinary(expressionNode);

return left switch
{
NumberResult aNumber when right is NumberResult bNumber => (NumberResult)(aNumber << bNumber),
_ => throw new IncompatibleExpressionOperandsException(location, left, right, "bitwise shift left"),
};
}

public ExpressionResult Evaluate(BitwiseShiftRightExpressionNode expressionNode)
{
throw new NotImplementedException();
var (left, right, location) = EvaluateBinary(expressionNode);

return left switch
{
NumberResult aNumber when right is NumberResult bNumber => (NumberResult)(aNumber >> bNumber),
_ => throw new IncompatibleExpressionOperandsException(location, left, right, "bitwise shift right"),
};
}

public ExpressionResult Evaluate(BitwiseRotateLeftExpressionNode expressionNode)
{
throw new NotImplementedException();
var (left, right, location) = EvaluateBinary(expressionNode);

return left switch
{
NumberResult aNumber when right is NumberResult bNumber => (NumberResult)BitOperations.RotateLeft(aNumber, bNumber),
_ => throw new IncompatibleExpressionOperandsException(location, left, right, "bitwise rotate left"),
};
}

public ExpressionResult Evaluate(BitwiseRotateRightExpressionNode expressionNode)
{
throw new NotImplementedException();
var (left, right, location) = EvaluateBinary(expressionNode);

return left switch
{
NumberResult aNumber when right is NumberResult bNumber => (NumberResult)BitOperations.RotateRight(aNumber, bNumber),
_ => throw new IncompatibleExpressionOperandsException(location, left, right, "bitwise rotate right"),
};
}

public ExpressionResult Evaluate(NotExpressionNode expressionNode)
Expand Down

0 comments on commit 4d30179

Please sign in to comment.