Пример #1
0
        public static Program Parse(string script)
        {
            var instructions = new Dictionary <string, Instruction>();
            var tokens       = new TokenQueue(Tokenizer.Tokenize(script));

            var token = tokens.GetNext();

            while (!token.IsEndOfScript())
            {
                var instruction = ToInstruction(token, tokens);

                token = tokens.GetNext();
                var wireName = string.Empty;
                while (!token.IsEndOfLine())
                {
                    switch (token.TokenType)
                    {
                    case TokenType.Assign:
                        instruction = AssignInstruction(tokens, instruction);
                        wireName    = tokens.GetCurrent().Value;

                        break;

                    case TokenType.And:
                        instruction = AndInstruction(tokens, instruction);
                        break;

                    case TokenType.Or:
                        instruction = OrInstruction(tokens, instruction);
                        break;

                    case TokenType.Not:
                        instruction = NotInstruction(tokens);
                        break;

                    case TokenType.LShift:
                        instruction = LShiftInstruction(tokens, instruction);
                        break;

                    case TokenType.RShift:
                        instruction = RShiftInstruction(tokens, instruction);
                        break;
                    }

                    token = tokens.GetNext();
                }

                instructions.Add(wireName, instruction);

                token = tokens.GetNext();
            }

            return(new Program(instructions));
        }
Пример #2
0
        private static Instruction ToInstruction(Token token, TokenQueue tokens)
        {
            Func <Instruction> func = token.TokenType switch
            {
                TokenType.Number => () => new NumberInstruction(ushort.Parse(token.Value)),
                TokenType.WireName => () => new GetWireInstruction(token.Value),
                TokenType.Not => () =>
                {
                    var rightToken = tokens.GetNext();
                    return(new NotInstruction(new GetWireInstruction(rightToken.Value)));
                },
                _ => () => new NullInstruction()
            };

            return(func());
        }
    }
Пример #3
0
        private static Instruction AssignInstruction(TokenQueue tokens, Instruction instruction)
        {
            var rightToken = tokens.GetNext();

            return(new AssignInstruction(instruction, new GetWireInstruction(rightToken.Value)));
        }
Пример #4
0
        private static Instruction AndInstruction(TokenQueue tokens, Instruction instruction)
        {
            var rightToken = tokens.GetNext();

            return(new AndInstruction(instruction, ToInstruction(rightToken, tokens)));
        }
Пример #5
0
        private static Instruction NotInstruction(TokenQueue tokens)
        {
            var rightToken = tokens.GetNext();

            return(new NotInstruction(ToInstruction(rightToken, tokens)));
        }
Пример #6
0
        private static Instruction LShiftInstruction(TokenQueue tokens, Instruction instruction)
        {
            var rightToken = tokens.GetNext();

            return(new LeftShiftInstruction(instruction, ToInstruction(rightToken, tokens)));;
        }