コード例 #1
0
        public void start(List <Token> postfixNotation)
        {
            while (postfixNotation[pointer].getLexemType().getType() != terminalTypeEnum.types["END"].getType())
            {
                Lexem  temp  = postfixNotation[pointer].getLexemType();
                double temp1 = 0;
                double temp2 = 0;
                switch (temp.getType())
                {
                case "VAR":
                    calcStack.Push(postfixNotation[pointer].getValue());
                    break;

                case "DIGIT":
                    calcStack.Push(Convert.ToDouble(postfixNotation[pointer].getValue()));
                    break;

                case "PRINT":
                    if (variables.ContainsKey((string)calcStack.Peek()))
                    {
                        Console.WriteLine(variables[(string)calcStack.Peek()]);
                    }
                    else
                    {
                        throw new NoSuchVariableException((string)calcStack.Peek());
                    }
                    break;

                case "LIST":
                    lists.Add((string)calcStack.Pop(), new DoublyLinkedList());
                    break;

                case "HASHTABLE":
                    hashtables.Add((string)calcStack.Pop(), new types.hashtable.Hashtable());
                    break;

                case "INSERT":
                    int    pos   = (int)(double)calcStack.Pop();
                    string name  = (string)calcStack.Pop();
                    double value = (double)calcStack.Pop();
                    if (lists.ContainsKey(name))
                    {
                        lists[name].InsertAfter(value, pos);
                    }
                    else if (hashtables.ContainsKey(name))
                    {
                        hashtables[name].Insert(value, pos);
                    }
                    break;

                case "DELETE":
                    name = (string)calcStack.Pop();
                    pos  = (int)(double)calcStack.Pop();
                    if (lists.ContainsKey(name))
                    {
                        lists[name].Delete(pos);
                    }
                    else if (hashtables.ContainsKey(name))
                    {
                        hashtables[name].Delete(pos);
                    }
                    break;

                case "COUNTOF":
                    name = (string)calcStack.Pop();
                    calcStack.Push((int)lists[name].Count());
                    break;

                case "GET":
                    name = (string)calcStack.Pop();
                    pos  = (int)(double)calcStack.Pop();
                    if (lists.ContainsKey(name))
                    {
                        calcStack.Push((double)lists[name].Return(pos));
                    }
                    else if (hashtables.ContainsKey(name))
                    {
                        calcStack.Push((double)hashtables[name].Search(pos));
                    }
                    break;

                case "OP":
                    temp1 = get();
                    temp2 = get();
                    double aResult = 0;
                    switch (postfixNotation[pointer].getValue())
                    {
                    case "+":
                        aResult = temp2 + temp1;
                        break;

                    case "-":
                        aResult = temp2 - temp1;
                        break;

                    case "*":
                        aResult = temp2 * temp1;
                        break;

                    case "/":
                        aResult = temp2 / temp1;
                        break;

                    default:
                        break;
                    }
                    calcStack.Push(aResult);
                    break;

                case "ASSIGN_OP":
                    object var = calcStack.Pop();
                    try
                    {
                        temp1 = (double)var;
                        variables[(string)calcStack.Pop()] = temp1;
                    }
                    catch (Exception e)
                    {
                        int temp3 = (int)var;
                        variables[(string)calcStack.Pop()] = temp3;
                    }

                    break;

                case "COMPARISON_OP":
                    temp1 = get();
                    temp2 = get();
                    bool lResult = false;
                    switch (postfixNotation[pointer].getValue())
                    {
                    case "==":
                        lResult = temp2 == temp1;
                        break;

                    case "!=":
                        lResult = temp2 != temp1;
                        break;

                    case ">":
                        lResult = temp2 > temp1;
                        break;

                    case "<":
                        lResult = temp2 < temp1;
                        break;

                    case ">=":
                        lResult = temp2 >= temp1;
                        break;

                    case "<=":
                        lResult = temp2 <= temp1;
                        break;

                    default:
                        break;
                    }
                    calcStack.Push(lResult);
                    break;

                case "GOTO_POINT":
                    calcStack.Push(Convert.ToInt32(postfixNotation[pointer].getValue()));
                    break;

                case "GOTO_ON_FALSE":
                    temp1 = (int)calcStack.Pop();
                    bool condition = (bool)calcStack.Pop();
                    if (!condition)
                    {
                        pointer = (int)temp1;
                        pointer--;
                    }
                    break;

                case "GOTO":
                    temp1   = (int)calcStack.Pop();
                    pointer = (int)temp1;
                    pointer--;
                    break;

                default:
                    break;
                }
                pointer++;
            }
        }
コード例 #2
0
        private bool expression()
        {
            Lexem temp = tokens[pointer].getLexemType();

            switch (temp.getType())
            {
            case "DIGIT":
            case "VAR":
                outString.Add(tokens[pointer]);
                pointer++;
                break;

            case "OP":
            case "ASSIGN_OP":
            case "COMPARISON_OP":
                while (tokenStack.Count != 0 && getPriority(tokens[pointer], tokenStack.Peek()))
                {
                    outString.Add(tokenStack.Pop());
                }
                tokenStack.Push(tokens[pointer]);
                pointer++;
                break;

            case "LP":
                tokenStack.Push(tokens[pointer]);
                pointer++;
                break;

            case "RP":
                while (tokenStack.Count != 0 && tokenStack.Peek().getLexemType().getType() != lexemTypeEnum.types["LP"].getType())
                {
                    outString.Add(tokenStack.Pop());
                }
                tokenStack.Pop();
                pointer++;
                break;

            case "IF":
                conditionalExpression();
                int tempIfPosition = outString.Count;
                outString.Add(new Token("", terminalTypeEnum.types["GOTO_POINT"]));
                outString.Add(new Token("!F", terminalTypeEnum.types["GOTO_ON_FALSE"]));
                body();
                if (tokens[pointer].getLexemType().getType() == lexemTypeEnum.types["ELSE"].getType())
                {
                    pointer++;
                    int tempElsePosition = outString.Count;
                    outString.Add(new Token("", terminalTypeEnum.types["GOTO_POINT"]));
                    outString.Add(new Token("!", terminalTypeEnum.types["GOTO"]));
                    outString[tempIfPosition].setValue(outString.Count.ToString());
                    body();
                    outString[tempElsePosition].setValue(outString.Count.ToString());
                }
                else
                {
                    outString[tempIfPosition].setValue(outString.Count.ToString());
                }
                break;

            case "WHILE":
                int tempWhilePosition = outString.Count;
                conditionalExpression();
                int tempEndPosition = outString.Count;
                outString.Add(new Token("", terminalTypeEnum.types["GOTO_POINT"]));
                outString.Add(new Token("!F", terminalTypeEnum.types["GOTO_ON_FALSE"]));
                body();
                outString.Add(new Token(tempWhilePosition.ToString(), terminalTypeEnum.types["GOTO_POINT"]));
                outString.Add(new Token("!", terminalTypeEnum.types["GOTO"]));
                outString[tempEndPosition].setValue(outString.Count.ToString());
                break;

            case "PRINT":
                pointer++;
                tokenStack.Push(tokens[pointer]);
                pointer++;
                while (tokens[pointer].getLexemType().getType() != lexemTypeEnum.types["EOE"].getType())
                {
                    expression();
                }
                outString.Add(new Token("print", lexemTypeEnum.types["PRINT"]));
                pointer++;
                break;

            case "LIST":
                pointer++;
                while (tokens[pointer].getLexemType().getType() != lexemTypeEnum.types["EOE"].getType())
                {
                    expression();
                }
                pointer++;
                outString.Add(new Token("list", lexemTypeEnum.types["LIST"]));
                break;

            case "HASHTABLE":
                pointer++;
                while (tokens[pointer].getLexemType().getType() != lexemTypeEnum.types["EOE"].getType())
                {
                    expression();
                }
                pointer++;
                outString.Add(new Token("hashtable", lexemTypeEnum.types["HASHTABLE"]));
                break;

            case "INSERT":
                pointer++;
                while (tokens[pointer].getLexemType().getType() != lexemTypeEnum.types["INTO"].getType())
                {
                    expression();
                }
                pointer++;
                while (tokens[pointer].getLexemType().getType() != lexemTypeEnum.types["ON"].getType())
                {
                    expression();
                }
                pointer++;
                while (tokens[pointer].getLexemType().getType() != lexemTypeEnum.types["EOE"].getType())
                {
                    expression();
                }
                pointer++;
                outString.Add(new Token("insert", lexemTypeEnum.types["INSERT"]));
                break;

            case "DELETE":
                pointer++;
                while (tokens[pointer].getLexemType().getType() != lexemTypeEnum.types["FROM"].getType())
                {
                    expression();
                }
                pointer++;
                while (tokens[pointer].getLexemType().getType() != lexemTypeEnum.types["EOE"].getType())
                {
                    expression();
                }
                pointer++;
                outString.Add(new Token("delete", lexemTypeEnum.types["DELETE"]));
                break;

            case "GET":
                pointer++;
                while (tokens[pointer].getLexemType().getType() != lexemTypeEnum.types["FROM"].getType())
                {
                    expression();
                }
                pointer++;
                expression();
                pointer++;
                outString.Add(new Token("get", lexemTypeEnum.types["GET"]));
                while (tokenStack.Count != 0 && getPriority(tokens[pointer], tokenStack.Peek()))
                {
                    outString.Add(tokenStack.Pop());
                }
                break;

            case "COUNTOF":
                pointer++;
                expression();
                pointer++;
                outString.Add(new Token("countof", lexemTypeEnum.types["COUNTOF"]));
                while (tokenStack.Count != 0 && getPriority(tokens[pointer], tokenStack.Peek()))
                {
                    outString.Add(tokenStack.Pop());
                }
                break;

            case "EOE":
                while (tokenStack.Count != 0)
                {
                    outString.Add(tokenStack.Pop());
                }
                pointer++;
                break;

            case "END":
                while (tokenStack.Count != 0)
                {
                    outString.Add(tokenStack.Pop());
                }
                return(false);

            default:
                return(false);
            }
            return(true);
        }
コード例 #3
0
        private IFormat ParseExpression(string str)
        {
            List <IFormat> operands = new List <IFormat>();
            LatexLexems    ll       = LatexLexems.getInstance();

            foreach (var def in ll.getDefinitionsLexem())
            {
                if (matches(str, def.getRegex()))
                {
                    IFormat name  = ParseExpression(getTokenValue(str, def.getRegex(), "name"));
                    IFormat value = ParseExpression(getTokenValue(str, def.getRegex(), "expr"));
                    operands.Add(name);
                    operands.Add(value);
                    return(new Latex(def.getType(), operands, null));
                }
            }

            if ((operands = ParseMultiOperandsExpression(str, '+')) != null)
            {
                return(new Latex(Types.Addition, operands, null));
            }
            if ((operands = ParseMultiOperandsExpression(str, '-')) != null)
            {
                return(new Latex(Types.Substraction, operands, null));
            }
            if ((operands = ParseMultiOperandsExpression(str, '*')) != null)
            {
                return(new Latex(Types.Multiplication, operands, null));
            }

            operands = new List <IFormat>();
            string[] strings;
            Lexem    tmp    = new Lexem(Types.Variable, "");
            int      length = 0;

            foreach (var def in ll.getOperandsLexem())
            {
                Regex rgx   = new Regex(def.getRegex());
                Match match = rgx.Match(str);
                if (match.Length > length)
                {
                    tmp    = def;
                    length = match.Length;
                }
            }
            if (tmp.getRegex() == "")
            {
                throw new Exception("Bad input, really! Follow the syntax");
            }

            switch (tmp.getType())
            {
            case Types.Variable:
                return(new Latex(str));

            case Types.Number:
                return(new Latex(Convert.ToDouble(str)));

            case Types.Interval:
            case Types.List:
            {
                str     = str.Substring(1, str.Length - 2);
                strings = str.Split(';');
                foreach (var s in strings)
                {
                    operands.Add(ParseExpression(s));
                }
                return(new Latex(tmp.getType(), operands, null));
            }

            case Types.FuncExpression:
                IFormat funcName  = ParseExpression(getTokenValue(str, tmp.getRegex(), "fr"));
                string  parametrs = getTokenValue(str, tmp.getRegex(), "sec");

                parametrs = parametrs.Substring(1, parametrs.Length - 2);
                strings   = parametrs.Split(',');
                foreach (var s in strings)
                {
                    operands.Add(ParseExpression(s));
                }
                return(new Latex(tmp.getType(), operands, funcName));
            }
            IFormat first  = ParseExpression(getTokenValue(str, tmp.getRegex(), "fr"));
            IFormat second = ParseExpression(getTokenValue(str, tmp.getRegex(), "sec"));

            operands.Add(first);
            operands.Add(second);
            return(new Latex(tmp.getType(), operands, null));
        }
コード例 #4
0
 private bool match(Lexem lexem)
 {
     return(iter >= tokens.Count ? false : tokens[iter].getLexemType().getType() == lexem.getType() ? iter++ != -1 && true : this.error.add(iter, tokens[iter], lexem) && false);
 }