Beispiel #1
0
        public Reference Parse(ParserContext context)
        {
            // Ensure current token is of type symbol dollar.
            context.Stream.EnsureCurrent(TokenType.SymbolDollar);

            // Skip symbol dollar token.
            context.Stream.Skip();

            // Invoke the identifier parser.
            string identifier = new IdentifierParser().Parse(context);

            // Create the reference construct.
            Reference reference = new Reference(identifier);

            // Return the resulting id construct.
            return(reference);
        }
Beispiel #2
0
        public Kind Parse(ParserContext context)
        {
            // Ensure current type is symbol parentheses start.
            context.Stream.EnsureCurrent(TokenType.SymbolColon);

            // Skip parentheses start.
            context.Stream.Skip();

            // Invoke identifier parser.
            string name = new IdentifierParser().Parse(context);

            // TODO: Pointer support.

            // Create the kind construct.
            Kind kind = new Kind(name);

            // Return the construct.
            return(kind);
        }
Beispiel #3
0
        public Section Parse(ParserContext context)
        {
            // Invoke identifier parser to capture section name.
            string name = new IdentifierParser().Parse(context);

            // Ensure current token is of type symbol colon.
            context.Stream.EnsureCurrent(TokenType.SymbolColon);

            // Skip symbol colon.
            context.Stream.Skip();

            // // Create the instruction buffer list.
            List <Instruction> instructions = new List <Instruction>();

            // Create the token buffer.
            Token buffer = context.Stream.Current;

            // Begin instruction parsing.
            while (buffer.Type != TokenType.SymbolTilde)
            {
                // Invoke instruction parser.
                Instruction instruction = new InstructionParser().Parse(context);

                // Append the instruction to the list.
                instructions.Add(instruction);

                // Update the token buffer.
                buffer = context.Stream.Current;
            }

            // Ensure current token is of type symbol tilde.
            context.Stream.EnsureCurrent(TokenType.SymbolTilde);

            // Skip symbol tilde.
            context.Stream.Skip();

            // Create the section construct.
            Section section = new Section(name, instructions.ToArray());

            // Return the construct.
            return(section);
        }
Beispiel #4
0
        public Metadata Parse(ParserContext context)
        {
            // Ensure current token type to be of symbol exclamation.
            context.Stream.EnsureCurrent(TokenType.SymbolExclamation);

            // Skip symbol exclamation token.
            context.Stream.Skip();

            // TODO: Key and value are delimitered-string literals, not identifiers.

            // Parse key.
            string key = new IdentifierParser().Parse(context);

            // Parse the value.
            string value = new IdentifierParser().Parse(context);

            // Create the construct.
            Metadata metadata = new Metadata(key, value);

            // Return the construct.
            return(metadata);
        }
Beispiel #5
0
        public Instruction Parse(ParserContext context)
        {
            string?resultIdentifier = null;

            if (context.Stream.Current.Type == TokenType.SymbolPercent)
            {
                context.Stream.Skip();

                resultIdentifier = new IdentifierParser().Parse(context);

                context.Stream.EnsureCurrent(TokenType.SymbolEqual);

                context.Stream.Skip();
            }

            string name = new IdentifierParser().Parse(context);

            // Create a buffer for the current token.
            Token token = context.Stream.Get();

            // Create the inputs buffer list.
            List <IConstruct> inputs = new List <IConstruct>();

            // Instruction contains arguments.
            while (token.Type != TokenType.SymbolSemiColon)
            {
                // Invoke the input parser.
                IConstruct input = new InputParser().Parse(context);

                // Append the input to the list.
                inputs.Add(input);

                // Update the buffer token.
                token = context.Stream.Get();
            }

            // Ensure current token is of type semi-colon.
            context.Stream.EnsureCurrent(TokenType.SymbolSemiColon);

            // Skip semi-colon token.
            context.Stream.Skip();

            Instruction inst;

            switch (name)
            {
            case InstructionName.End:
            {
                if (inputs.Count > 1)
                {
                    throw new Exception("Unexpected amount of inputs");
                }
                else if (!(inputs[0] is Value))
                {
                    throw new Exception("Expected input to be a value");
                }

                inst = new EndInst(inputs[0] as Value);

                break;
            }

            default:
            {
                throw new Exception($"Unrecognized instruction name: {name}");
            }
            }

            return(inst);
        }
Beispiel #6
0
        public Routine Parse(ParserContext context)
        {
            // Ensure current token is of type symbol at.
            context.Stream.EnsureCurrent(TokenType.SymbolAt);

            // Skip symbol at token.
            context.Stream.Skip();

            // Invoke the identifier parser.
            string identifier = new IdentifierParser().Parse(context);

            // Ensure current token is of type parentheses start.
            context.Stream.EnsureCurrent(TokenType.SymbolParenthesesL);

            // Skip parentheses start token.
            context.Stream.Skip();

            // Capture the current token as the buffer.
            Token buffer = context.Stream.Current;

            // Create the argument buffer list.
            List <(Kind, Reference)> args = new List <(Kind, Reference)>();

            // Begin argument parsing.
            while (buffer.Type != TokenType.SymbolParenthesesR)
            {
                // Invoke kind parser.
                Kind kind = new KindParser().Parse(context);

                // Invoke reference parser.
                Reference reference = new ReferenceParser().Parse(context);

                // Abstract current token.
                Token token = context.Stream.Current;

                // Current token must be symbol comma or parentheses end.
                if (token.Type != TokenType.SymbolParenthesesR && token.Type != TokenType.SymbolComma)
                {
                    throw new Exception($"Unexpected token in argument list: {token.Type}");
                }
                // Skip symbol comma token.
                else if (token.Type == TokenType.SymbolComma)
                {
                    context.Stream.Skip();
                }

                // Append the argument.
                args.Add((kind, reference));

                // Update the buffer.
                buffer = context.Stream.Current;
            }

            // Ensure current token is symbol parentheses end.
            context.Stream.EnsureCurrent(TokenType.SymbolParenthesesR);

            // Skip parentheses end.
            context.Stream.Skip();

            // Invoke kind parser to parse return kind.
            Kind returnKind = new KindParser().Parse(context);

            // Update the token buffer.
            buffer = context.Stream.Current;

            // Parse the body.
            Section body = new SectionParser().Parse(context);

            // Ensure current token is of type symbol tilde.
            context.Stream.EnsureCurrent(TokenType.SymbolTilde);

            // Skip symbol tilde.
            context.Stream.Skip();

            // Create the prototype.
            Prototype prototype = new Prototype(identifier, args.ToArray(), returnKind, false);

            // Create the routine construct.
            Routine routine = new Routine(prototype, body);

            // Return the resulting routine.
            return(routine);
        }