Ejemplo n.º 1
0
        /// <summary>
        /// Create a new instance of the <see cref="Capture"/> class.
        /// </summary>
        /// <param name="unit">Unit.</param>
        /// <param name="lex">Lex.</param>
        public static Capture Create(ParsingUnit unit, Lexeme <LoreToken> lex)
        {
            if (lex.Is(LoreToken.Identifier))
            {
                return(new Capture(lex.Value, capturesAll: false));
            }
            if (lex.Is(LoreToken.Operator) && lex.Is("="))
            {
                return(new Capture(lex.Value, capturesAll: true));
            }

            // Invalid capture
            throw LoreException.Create(unit.Location).Describe($"Not a capture: '{lex.Value}' ({lex.Token})");
        }
Ejemplo n.º 2
0
        private void ExecuteCommand(Lexeme lexeme, ref int i)
        {
            if (lexeme.Is(LexemeCodes.ID))
            {
                _stack.Push(lexeme.Body);
                return;
            }

            if (lexeme.Is(LexemeCodes.CONSTANT))
            {
                _stack.Push(lexeme.Body);
                return;
            }

            if (lexeme.Is(LexemeCodes.LABEL))
            {
                _stack.Push(lexeme.Body);
                return;
            }

            if (lexeme.Is(LexemeCodes.WRITE))
            {
                var variable = _stack.Pop();
                WriteConsole($"{variable}: {_scope[variable]}");
                return;
            }

            if (lexeme.Is(LexemeCodes.ASSIGN))
            {
                var value    = _stack.Pop();
                var variable = _stack.Pop();

                if (!int.TryParse(value, out var parsedValue))
                {
                    parsedValue = int.Parse(_scope[value]);
                }
                _scope[variable] = parsedValue.ToString();
                return;
            }

            if (lexeme.Is(LexemeCodes.NOTHING))
            {
                if (lexeme.Body.Equals("БП"))
                {
                    var label = _stack.Pop();
                    i = _poliz.IndexOf($"{label}:");
                }

                if (lexeme.Body.Equals("УПЛ"))
                {
                    var label         = _stack.Pop();
                    var compareResult = _stack.Pop();
                    if (compareResult.Equals(false.ToString()))
                    {
                        i = _poliz.IndexOf($"{label}:");
                    }
                }

                return;
            }

            if (lexeme.Is(LexemeCodes.READ))
            {
                var variable = _stack.Pop();
                var value    = new Prompt().ShowDialog(variable, "Enter value for variable");
                _scope[variable] = int.Parse(value).ToString();
                return;
            }

            switch ((LexemeCodes)lexeme.Code)
            {
            case LexemeCodes.PLUS: PopAndPush((x, y) => x + y); break;

            case LexemeCodes.MINUS: PopAndPush((x, y) => x - y); break;

            case LexemeCodes.DIVISION: PopAndPush((x, y) => x / y); break;

            case LexemeCodes.MULTIPLY: PopAndPush((x, y) => x * y); break;

            case LexemeCodes.AND: PopAndPush((bool x, bool y) => x & y); break;

            case LexemeCodes.OR: PopAndPush((bool x, bool y) => x | y); break;

            case LexemeCodes.NOT: PopAndPush(x => !x); break;

            case LexemeCodes.LESS_EQUAL: PopAndPush((x, y) => x <= y); break;

            case LexemeCodes.LESS_THAN: PopAndPush((x, y) => x < y); break;

            case LexemeCodes.MORE_EQUAL: PopAndPush((x, y) => x >= y); break;

            case LexemeCodes.MORE_THAN: PopAndPush((x, y) => x > y); break;

            case LexemeCodes.NOT_EQUAL: PopAndPush((int x, int y) => x != y); break;

            case LexemeCodes.EQUAL: PopAndPush((int x, int y) => x == y); break;

            default: throw new Exception(lexeme.Body);
            }
        }