Exemplo n.º 1
0
        public void NotFail()
        {
            NumberToken command = new NumberToken();
            var         result  = command.Process(new InputStack());

            Assert.Null(result);
        }
Exemplo n.º 2
0
        public void EqualsNullTest()
        {
            var token = new NumberToken(1.1);

            Assert.False(token.Equals(null));
            Assert.NotEqual(null, token);
        }
Exemplo n.º 3
0
 public IToken Combine(IToken other)
 {
     if (other is NumberToken)
     {
         NumberToken otherNumber = other as NumberToken;
         if (HasDot && otherNumber.HasDot)
         {
             return(new ErrorToken(Text + other.Text, "number can not have more than 1 dot"));
         }
         else
         {
             return(new NumberToken(Text + otherNumber.Text, HasDot || otherNumber.HasDot));
         }
     }
     else if (other is DotToken)
     {
         if (HasDot)
         {
             return(new ErrorToken(Text + other.Text, "number can not have more than 1 dot"));
         }
         else
         {
             return(new NumberToken(Text + other.Text, true));
         }
     }
     else
     {
         return(null);
     }
 }
Exemplo n.º 4
0
 private static IToken Resolve(LexerContext context)
 {
     if (CommentToken.IsCommentBegin(context))
     {
         return(ResolveComment(context));
     }
     else if (NumberToken.IsNumberBegin(context))
     {
         return(ResolveNumber(context));
     }
     else if (StringToken.IsStringBegin(context))
     {
         return(ResolveString(context));
     }
     else if (SpecialSymbolToken.IsSpecial(context))
     {
         return(ResolveSpecial(context));
     }
     else if (IdentifierToken.IsIdentifierBegin(context))
     {
         return(ResolveIdentifier(context));
     }
     else
     {
         throw new SyntaxErrorException();
     }
 }
Exemplo n.º 5
0
        public int Write(Placeholder location, string token, long size)
        {
            NumberToken lt = Sym(token, location, 16, 0, 0);

            lt.SetValue(size);
            return(entryCount - 1);
        }
Exemplo n.º 6
0
 public void Visit(NumberToken numberToken)
 {
     mCurrent = new Constant {
         Value = numberToken.Value
     };
     HandleNonParenthesesAndOperation();
 }
Exemplo n.º 7
0
        public void EqualsDiffTypeTest()
        {
            var token = new NumberToken(1.1);

            Assert.False(token.Equals(1));
            Assert.NotEqual((object)1, token);
        }
Exemplo n.º 8
0
        /**
         * Evalutes all dice operators in the Token sequence.
         *
         * tokens - the token sequence that whose dice operators will be evaluated
         */
        private void EvaluateBinaryOperator <T>(List <Token> tokens)
        {
            for (int i = 0; i < tokens.Count;)
            {
                Token token = tokens[i];
                if (token.GetTokenType() == Token.Type.BINARY_OPERATOR)
                {
                    if (token is T)
                    {
                        BinaryOperatorToken op    = (BinaryOperatorToken)token;
                        int         operand1Index = i - 1;
                        int         operand2Index = i + 1;
                        NumberToken operand1      = (NumberToken)tokens[operand1Index];
                        NumberToken operand2      = (NumberToken)tokens[operand2Index];

                        tokens[operand1Index] = new NumberToken(op.Execute(operand1.Number, operand2.Number));
                        tokens.RemoveAt(operand2Index);
                        tokens.RemoveAt(i);
                    }
                    else
                    {
                        ++i;
                    }
                }
                else
                {
                    ++i;
                }
            }
        }
Exemplo n.º 9
0
        /**
         *  Evalutes the specified token sequence.
         *
         *  tokens - the token sequence that whose dice operators will be evaluated
         */
        public double Evaluate(List <Token> tokens)
        {
            RemoveEndOfEquationTokens(tokens);
            EvaluateBinaryOperator <DiceOperatorToken>(tokens);
            EvaluateBinaryOperator <MultiplicationOperatorToken>(tokens);
            EvaluateBinaryOperator <DivisonOperatorToken>(tokens);
            EvaluateBinaryOperator <AdditionOperatorToken>(tokens);
            EvaluateBinaryOperator <SubtractionOperatorToken>(tokens);

            if (tokens.Count == 1)
            {
                if (tokens[0].GetTokenType() == Token.Type.OPERAND)
                {
                    if (tokens[0] is NumberToken)
                    {
                        NumberToken number = (NumberToken)tokens[0];
                        return(number.Number);
                    }
                    else
                    {
                        throw new EquationParsingException("Final token is not of a number");
                    }
                }
                else
                {
                    throw new EquationParsingException("Final token is not an operand");
                }
            }
            else
            {
                throw new EquationParsingException("Equation had more then one token after parsing");
            }
        }
        public Boolean AllowLiteral(out LiteralToken literal)
        {
            var current = m_enumerator.Current;

            switch (current.Type)
            {
            case ParseEventType.Null:
                literal = new NullToken(m_fileId, current.Line, current.Column);
                m_enumerator.MoveNext();
                return(true);

            case ParseEventType.Boolean:
                literal = new BooleanToken(m_fileId, current.Line, current.Column, (Boolean)current.Value);
                m_enumerator.MoveNext();
                return(true);

            case ParseEventType.Number:
                literal = new NumberToken(m_fileId, current.Line, current.Column, (Double)current.Value);
                m_enumerator.MoveNext();
                return(true);

            case ParseEventType.String:
                literal = new StringToken(m_fileId, current.Line, current.Column, (String)current.Value);
                m_enumerator.MoveNext();
                return(true);
            }

            literal = null;
            return(false);
        }
Exemplo n.º 11
0
        private NumberToken Sym(string name, Placeholder location, byte info, byte other, short shndx)
        {
            output.WriteInt32(dynstr.Get(name));
            if (output.Is64Bit)
            {
                output.WriteByte(info);
                output.WriteByte(other);
                output.WriteInt16(shndx);
            }
            if (location.IsNull)
            {
                output.WriteNumber(0);
            }
            else
            {
                output.WritePlaceholder(location);
            }
            NumberToken result = output.InsertNumberToken();

            if (!output.Is64Bit)
            {
                output.WriteByte(info);
                output.WriteByte(other);
                output.WriteInt16(shndx);
            }

            hash[name] = entryCount;
            entryCount++;
            return(result);
        }
Exemplo n.º 12
0
        public void EqualsSameObjectTest()
        {
            var token = new NumberToken(1.1);

            Assert.True(token.Equals(token));
            Assert.Equal(token, token);
        }
Exemplo n.º 13
0
        public static bool IsLeadingZeroOmitted(NumberToken numberToken)
        {
            var numberText = numberToken.Text;

            return(numberToken.HasSign
                                ? numberToken.Text[1].ToString().Equals(numberToken.DecimalSeparator)
                                : numberText[0].ToString().Equals(numberToken.DecimalSeparator));
        }
Exemplo n.º 14
0
        public void SignPrecedence()
        {
            var eq       = new ImageFramework.Model.Equation.Equation("-1+2");
            var hlsl     = eq.GetHlslExpression();
            var expected = $"(({NumberToken.ToHlsl(-1.0f)}*{NumberToken.ToHlsl(1.0f)})+{NumberToken.ToHlsl(2.0f)})";

            Assert.AreEqual(expected, hlsl.Replace(" ", ""));
        }
Exemplo n.º 15
0
        public void EqualsDiffVarTest()
        {
            var token1 = new NumberToken(1.1);
            var token2 = new NumberToken(1.14);

            Assert.False(token1.Equals(token2));
            Assert.NotEqual(token1, token2);
        }
Exemplo n.º 16
0
        public void SignOperatorAndBrackets()
        {
            var eq       = new ImageFramework.Model.Equation.Equation("1+0.4*-2");
            var hlsl     = eq.GetHlslExpression();
            var expected =
                $"({NumberToken.ToHlsl(1.0f)}+({NumberToken.ToHlsl(0.4f)}*({NumberToken.ToHlsl(-1.0f)}*{NumberToken.ToHlsl(2.0f)})))";

            Assert.AreEqual(expected, hlsl.Replace(" ", ""));
        }
Exemplo n.º 17
0
        public void GivenTokenConverter_WhenHasRankOrHigherWithFiat_ThenFail()
        {
            var numberToken    = new NumberToken("4");
            var currencyToken  = new CurrencyToken("EUR", "EUR", CurrencyType.Fiat);
            var tokenConverter = CreateTokenConverter();

            var result = tokenConverter.HasRankOrHigher(numberToken, currencyToken);

            Assert.Fail();
        }
Exemplo n.º 18
0
        public NumberExpr(NumberToken token, IEnv env)
            : base(env)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            base.Token = token;
        }
Exemplo n.º 19
0
        public DynamicSymbols(Sections sections, StringTable dynstr)
        {
            this.dynstr = dynstr;
            dynsym      = sections.GetSection(".dynsym");
            output      = dynsym.AllocateRegion();
            hashRegion  = sections.GetSection(".hash").AllocateRegion();
            NumberToken s = Sym("", Placeholder.Null, 0, 0, 0);

            s.SetValue(0);
        }
Exemplo n.º 20
0
        static Token MakeNumber(Scanner scanner)
        {
            NumberToken token = scanner.NumberBuilder.GetToken();

            scanner.Position--;
            scanner.NumberBuilder = null;
            token.Line            = scanner.Line;
            token.Column          = scanner.Column - token.Word.Length;
            return(token);
        }
Exemplo n.º 21
0
        public void ReturnNumberWithDigitInput(string text, int expectedResultValue)
        {
            NumberToken command = new NumberToken();
            var         stack   = new InputStack(text);
            var         result  = command.Process(stack);

            Assert.NotNull(result);
            Assert.Equal(expectedResultValue, result.Value);
            Assert.Equal(0, result.Score);
        }
Exemplo n.º 22
0
        public void NumberTokenCtor()
        {
            string value      = "1";
            int    position   = 22;
            string tokenValue = $@"[number:{position}]";

            NumberToken token = new NumberToken(position, value);

            Assert.AreEqual(tokenValue, token.TokenValue);
            Assert.AreEqual(value, token.Value);
            Assert.AreEqual(position, token.Position);
        }
Exemplo n.º 23
0
        private bool numberReader(InputStream str, out Token outtok)
        {
            Match match = numberRegex.Match(str.InputString);

            if (match.Success)
            {
                outtok = new NumberToken(match.Value.Replace(" ", ""), str.Position);
                str.MoveForward(match.Length);
                return(true);
            }
            outtok = null;
            return(false);
        }
Exemplo n.º 24
0
        public static string Normalize(this NumberToken numberToken)
        {
            var normalized      = "";
            var beforeDecimal   = true;
            var previousWasSign = false;

            foreach (var part in numberToken.NumberParts)
            {
                switch (part.Type)
                {
                case NumberPart.NumberType.Number:
                    var number = part.Value;
                    if (beforeDecimal && numberToken.NumberParts.All(p => p.Type != NumberPart.NumberType.GroupSeparator))
                    {
                        number = GetNumberWithThousandSeparatorsAdded(part.Value);
                    }
                    normalized += number;
                    break;

                case NumberPart.NumberType.DecimalSeparator:
                    normalized   += "d";
                    beforeDecimal = false;
                    break;

                case NumberPart.NumberType.GroupSeparator:
                    if (!previousWasSign)
                    {
                        normalized += "t";
                    }
                    break;

                case NumberPart.NumberType.Sign:
                    normalized     += part.Value == "+" ? "p" : "n";
                    previousWasSign = true;
                    break;
                }
                if (part.Type != NumberPart.NumberType.Sign && previousWasSign)
                {
                    previousWasSign = false;
                }
            }

            if (IsLeadingZeroOmitted(numberToken))
            {
                normalized = normalized.AddLeadingZero(numberToken.HasSign);
            }

            return(normalized);
        }
Exemplo n.º 25
0
        protected override Core.Tokenization.Token CreateToken(string s,
                                                               System.Text.RegularExpressions.GroupCollection groups)
        {
            string decimalPart = groups["sdec"].Value;

            if (String.IsNullOrEmpty(decimalPart))
            {
                decimalPart = groups["gdec"].Value;
            }

            NumberToken value = new NumberToken(s,
                                                groups["sign"].Value, decimalPart, groups["frac"].Value, _NumberFormat);

            return(value);
        }
Exemplo n.º 26
0
        public Importer(Sections sections)
        {
            relapltRegion = sections.GetSection(".rela.plt").AllocateRegion();
            relocator     = new Relocator(relapltRegion);
            mainRegion    = sections.GetSection(".dynamic").AllocateRegion();
            dynstrRegion  = sections.GetSection(".dynstr").AllocateRegion();
            dynstr        = new StringTable();
            plt           = sections.GetSection(".plt").AllocateRegion();
            pltgot        = sections.GetSection(".got.plt").AllocateRegion();
            dynsym        = new DynamicSymbols(sections, dynstr);

            dynamicTokenSize = dynsym.Write(mainRegion.CurrentLocation, "_DYNAMIC");

            mainRegion.WriteNumber(3);  // dt_pltgot
            mainRegion.WritePlaceholder(pltgot.BaseLocation);
            mainRegion.WriteNumber(4);  // dt_hash
            mainRegion.WritePlaceholder(dynsym.HashRegion.BaseLocation);
            mainRegion.WriteNumber(5);  // dt_strtab
            mainRegion.WritePlaceholder(dynstrRegion.BaseLocation);
            mainRegion.WriteNumber(6);  // dt_symtab
            mainRegion.WritePlaceholder(dynsym.SymbolRegion.BaseLocation);
            mainRegion.WriteNumber(11); // dt_syment
            mainRegion.WriteNumber(dynsym.ElementSize);
            //            mainRegion.WriteNumber(15); // dt_rpath
            //            mainRegion.WriteNumber(dynstr.Get(".:/lib64:/lib"));

            plt0 = plt.CurrentLocation;
            pltgot.WriteNumber(0);
            if (plt.Is64Bit)
            {
                plt.Write(new byte[] { 0xff, 0x35 }); // push [rip+imm32]
                plt.WritePlaceholderDisplacement32(pltgot.CurrentLocation);
                pltgot.WriteNumber(0);
                plt.Write(new byte[] { 0xff, 0x25 }); // jmp [rip+imm32]
                plt.WritePlaceholderDisplacement32(pltgot.CurrentLocation);
                pltgot.WriteNumber(0);
            }
            else
            {
                plt.WriteByte(0x68); // push IMM32
                plt.WritePlaceholder(pltgot.CurrentLocation);
                pltgot.WriteNumber(0);
                plt.Write(new byte[] { 0xff, 0x25 }); // jmp [imm32]
                plt.WritePlaceholder(pltgot.CurrentLocation);
                pltgot.WriteNumber(0);
            }
            plt.Align(16, 0x90); // nop
        }
Exemplo n.º 27
0
 protected T Visit(IToken token, T state = default)
 {
     return(token switch
     {
         AddToken addToken => Visit(addToken, state),
         SubtractToken subtractToken => Visit(subtractToken, state),
         MultiplyToken multiplyToken => Visit(multiplyToken, state),
         DivideToken divideToken => Visit(divideToken, state),
         NegateToken negateToken => Visit(negateToken, state),
         NumberToken numberToken => Visit(numberToken, state),
         FactorialToken factorialToken => Visit(factorialToken, state),
         PowerToken powerToken => Visit(powerToken, state),
         VariableToken variableToken => Visit(variableToken, state),
         AssignmentToken assignmentToken => Visit(assignmentToken, state),
         _ => throw new ArgumentOutOfRangeException(nameof(token))
     });
Exemplo n.º 28
0
        /// <summary>
        /// Parse number value from <param name="text"/>
        /// Convention: [ws] [$] [sign][integral-digits,]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]
        /// </summary>
        /// <param name="text"></param>
        /// <param name="omitLeadingZero"></param>
        /// <returns>Returns a list of <see cref="NumberToken">NumberToken</see></returns>
        public NumberToken Parse(string text, bool?omitLeadingZero = null)
        {
            var originalText = text;

            if (string.IsNullOrEmpty(text))
            {
                return(null);
            }

            // parse out the currency symbol
            // [ws] [$]
            var currencyParts = GetCurrencyParts(text);

            if (currencyParts != null)
            {
                var value = currencyParts.Aggregate(string.Empty, (current, numberPart) => current + numberPart.Value);
                text = text.Substring(value.Length);
            }

            // parse out the sign
            // [sign]
            var signParts = GetSignParts(text);

            if (signParts != null)
            {
                var value = signParts.Aggregate(string.Empty, (current, numberPart) => current + numberPart.Value);
                text = text.Substring(value.Length);
            }

            // parse out the exponents
            //[E[sign]exponential-digits]
            var exponentialPart = GetExponentialPart(text);

            if (exponentialPart != null)
            {
                text = text.Substring(0, text.Length - exponentialPart.Value.Length);
            }

            // [integral-digits,]integral-digits[.[fractional-digits]]
            var integralAndFractionalParts = GetIntegralAndFractionalParts(text);

            // combine the individual number parts
            var numberParts = CombineParts(currencyParts, signParts, integralAndFractionalParts, exponentialPart);
            var numberToken = new NumberToken(originalText, numberParts, omitLeadingZero);

            return(numberToken);
        }
Exemplo n.º 29
0
        public void ReturnMultipleResultsNumberWithDigitInput(string text, params int[] expectedResultValues)
        {
            NumberToken command = new NumberToken();
            var         stack   = new InputStack(text);

            Assert.Equal(expectedResultValues.Length, stack.Count);
            for (int i = 0; i < stack.Count; i++)
            {
                var result = command.Process(stack);

                stack.Pop();

                Assert.NotNull(result);
                Assert.Equal(expectedResultValues[i], result.Value);
                Assert.Equal(0, result.Score);
            }
        }
Exemplo n.º 30
0
        public void OperatorPrecedence()
        {
            var eq       = new ImageFramework.Model.Equation.Equation("(3-1)*0.4+1");
            var hlsl     = eq.GetHlslExpression();
            var expected =
                $"((({NumberToken.ToHlsl(3.0f)}-{NumberToken.ToHlsl(1.0f)})*{NumberToken.ToHlsl(0.4f)})+{NumberToken.ToHlsl(1.0f)})";

            Assert.AreEqual(expected, hlsl.Replace(" ", ""));

            eq   = new ImageFramework.Model.Equation.Equation("1+0.4*(3-1)");
            hlsl = eq.GetHlslExpression();

            expected =
                $"({NumberToken.ToHlsl(1.0f)}+({NumberToken.ToHlsl(0.4f)}*({NumberToken.ToHlsl(3.0f)}-{NumberToken.ToHlsl(1.0f)})))";

            Assert.AreEqual(expected, hlsl.Replace(" ", ""));
        }
Exemplo n.º 31
0
                private static Token[] GetInfixTokens(string term)
                {
                    //Leerzeichen entfernen und in Kleinbuchstaben konvertieren
                    term = term.Replace(" ", string.Empty).ToLowerInvariant();

                    var tokens = new List<Token>();

                    //mit RegEx alle Zahlen aussortieren
                    var numbers = parserRegEx.Matches(term);
                    term = parserRegEx.Replace(term, "1");

                    var sb = new StringBuilder(term);

                    //Term in Tokens teilen
                    var numberIndex = 0;
                    while (sb.Length > 0)
                    {
                        var validToken = false;

                        //Zahlen prüfen
                        if (sb.StartsWith("1"))
                        {
                            var t = new NumberToken();
                            t.ParseFromString(numbers[numberIndex].Groups["number"].Value);
                            tokens.Add(t);

                            numberIndex++;

                            //term = ReduceString(term, 1);
                            sb.Remove(0, 1);

                            validToken = true;
                        }

                        //Operatoren prüfen
                        if (!validToken)
                            for (int i = 0; i < operators.Length; i++)
                            {
                                var token = operators[i];
                                if (sb.StartsWith(token))
                                {
                                    var t = new OperatorToken();

                                    if ((token == '+' || token == '-') && (tokens.Count == 0 || tokens.Last().GetName() == "(")) //Vorzeichen
                                    {
                                        if (token == '-')
                                            t.ParseFromString("!");
                                    }
                                    else
                                        t.ParseFromString(token.ToString());

                                    tokens.Add(t);

                                    //term = ReduceString(term, 1);
                                    sb.Remove(0, 1);

                                    validToken = true;
                                    break;
                                }
                            }

                        //Funktionen prüfen
                        if (!validToken)
                            for (int i = 0; i < functions.Length; i++)
                            {
                                var token = functions[i];
                                if (sb.StartsWith(token))
                                {
                                    var t = new FunctionToken();
                                    t.ParseFromString(token);
                                    tokens.Add(t);

                                    //term = ReduceString(term, token.Length);
                                    sb.Remove(0, token.Length);

                                    validToken = true;
                                    break;
                                }
                            }

                        //Rest prüfen
                        if (!validToken)
                        {
                            if (sb.StartsWith("pi")) //Pi
                            {
                                var t = new NumberToken();
                                t.ParseFromString(System.Math.PI.ToString());
                                tokens.Add(t);

                                //term = ReduceString(term, 2);
                                sb.Remove(0, 2);
                            }

                            else if (sb.StartsWith("e")) //e
                            {
                                var t = new NumberToken();
                                t.ParseFromString(System.Math.E.ToString());
                                tokens.Add(t);

                                //term = ReduceString(term, 1);
                                sb.Remove(0, 1);
                            }

                            else if (sb.StartsWith("(")) //öffnende Klammer
                            {
                                var t = new SpecialToken();
                                t.ParseFromString("(");
                                tokens.Add(t);

                                //term = ReduceString(term, 1);
                                sb.Remove(0, 1);
                            }

                            else if (sb.StartsWith(")")) //schließende Klammer
                            {
                                var t = new SpecialToken();
                                t.ParseFromString(")");
                                tokens.Add(t);

                                //term = ReduceString(term, 1);
                                sb.Remove(0, 1);
                            }

                            else if (sb.StartsWith(";")) //Argumenttrennzeichen
                            {
                                var t = new SpecialToken();
                                t.ParseFromString(";");
                                tokens.Add(t);

                                //term = ReduceString(term, 1);
                                sb.Remove(0, 1);
                            }

                            else //Token nicht bekannt
                                throw new ArgumentException("Dieser Term enthält einen ungültigen Token.");
                        }
                    }

                    return tokens.ToArray();
                }