/// <summary> /// Applies a <see cref="Negative" /> operation to the <paramref name="node" />. /// </summary> /// <param name="node">The <paramref name="node" />.</param> /// <returns><see cref="object" />.</returns> /// <exception cref="Exception"></exception> internal static object Negative(Nodes.Monadic node) { try { try { if (node.LHS.EvaluatedType == typeof(string)) { var chars = ((string)node.LHS.EvaluatedValue).ToCharArray(); Array.Reverse(chars); return(chars.ToString()); } return(Convert.ToDouble(node.LHS.EvaluatedValue) * -1); } catch { return(Convert.ToDouble(node.LHS.EvaluatedValue) * -1); } } catch (Exception e) { throw new Exception("Negative operation exception: " + e); } }
/// <summary> /// Applies a <see cref="BitwiseNot" /> operation to the <paramref name="node" />. /// </summary> /// <param name="node">The <paramref name="node" />.</param> /// <returns><see cref="object" />.</returns> /// <exception cref="Exception"></exception> internal static object BitwiseNot(Nodes.Monadic node) { try { return(~Convert.ToInt64(node.LHS.EvaluatedValue)); } catch (Exception e) { throw new Exception("Bitwise NOT operation exception: " + e); } }
/// <summary> /// Applies a <see cref="Not" /> operation to the <paramref name="node" />. /// </summary> /// <param name="node">The <paramref name="node" />.</param> /// <returns> /// <c> /// <see langword="true" /> /// </c> /// if the operation is successful, /// <c> /// <see langword="false" /> /// </c> /// otherwise. /// </returns> /// <exception cref="Exception"> /// </exception> internal static bool Not(Nodes.Monadic node) { try { if (node.LHS.EvaluatedType == typeof(bool)) { return(!(bool)node.LHS.EvaluatedValue); } throw new Exception(); } catch (Exception e) { throw new Exception("NOT operation exception: " + e); } }
/// <summary> /// <see cref="UnaryExprn" />:= {"(" <see cref="Exprn" /> ")" | /// <see cref="BaseExprn" /> | "!" <see cref="UnaryExprn" /> | "+" /// <see cref="UnaryExprn" /> | "-" <see cref="UnaryExprn" /> | "~" /// <see cref="UnaryExprn" /> }; NOTE: This function is purposefully /// different from the layout of the other production functions. /// </summary> /// <returns> /// <see cref="Nodes" />.<see cref="Nodes.Node" />. Monadic /// </returns> private static Nodes.Node UnaryExprn() { if (CurrentToken.TokenValue == '(') { //Consume the left bracket ConsumeToken(); //Spins off everything inside the brackets as a new expression to evaluate var lhs = Exprn(); //Consume the right bracket if (CurrentToken.TokenValue != ')') { return(AddNode(new Nodes.Error("Matching closing bracket not found"))); } ConsumeToken(); return(lhs); //Throw error if right bracket not found } if ((CurrentToken != null) && (CurrentToken.TokenType == Token.TokenTypes.Unary)) { //Create a new monadic node for the Unary operation and consume the token var monadic = new Nodes.Monadic(null, CurrentToken); ConsumeToken(); //Set left hand side var mlhs = BaseExprn(); if (mlhs == null) { return(new Nodes.Error("Value LHS could not be evaluated")); } //If analysis successful, add to the monadic and return the monadic (will be added later) monadic.LHS = mlhs; return(monadic); } //Create a new node, attempt to find a last-resort base expression var node = BaseExprn(); return(node == null ? new Nodes.Error("Could not find base expression") : AddNode(node)); }
/// <summary> /// <see cref="AsmExprn" /> := {"*"}[":"Instruction] {[<see cref="Exprn" /> /// |<see cref="AsmExprn" />]|"?"}; /// </summary> /// <returns><see cref="Nodes" />.<see cref="Nodes.Node" />.</returns> private static Nodes.Node AsmExprn() { //Stringbuilder for storing our ASM expression whilst we parse it var sb = new StringBuilder(); //Consumes the token sb.Append(CurrentToken.TokenValue); ConsumeToken(); //Get all idents if (CurrentToken.TokenType == Token.TokenTypes.Instruction) { //If ident found, consume it sb.Append(CurrentToken.TokenValue); ConsumeToken(); } //Check for a GET command if (CurrentToken.TokenValue.ToString() == "?") { //Consume GET command token sb.Append(CurrentToken.TokenValue); ConsumeToken(); //Return error if EOL not found if (CurrentToken.TokenType != Token.TokenTypes.Eol) { return(Error("Could not find end of line token")); } //Consume EOL token if found sb.Append(CurrentToken.TokenValue); ConsumeToken(); //Create our node and return return(AddNode(new Nodes.Node(new Token { TokenValue = sb.ToString() }) { Value = sb.ToString(), Token = { TokenValue = sb.ToString() } })); } //Try to analyse the left hand side var lhs = Exprn(); if (lhs == null) { return(new Nodes.Error("LHS could not be evaluated")); } //Add left hand side to a new monadic - WHY?!?! var monadic = new Nodes.Monadic(lhs, new Token { TokenType = Token.TokenTypes.None, TokenValue = sb.ToString() }); //Check for EOL Token and consume if ((CurrentToken == null) || (CurrentToken?.TokenType != Token.TokenTypes.Eol)) { return(Error("Could not find end of line token")); } ConsumeToken(); return(AddNode(monadic)); }