Exemplo n.º 1
0
        public Construct VisitEndInst(EndInst node)
        {
            // Visit the value.
            this.VisitValue(node.Value);

            // Pop off the resulting value.
            LlvmValue value = this.valueStack.Pop();

            // Create the return instruction.
            LlvmValue returnInst = this.builder.CreateReturn(value);

            // Append the return instruction onto the stack.
            this.valueStack.Push(returnInst);

            // Return the node.
            return(node);
        }
Exemplo n.º 2
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);
        }