예제 #1
0
파일: ValueParser.cs 프로젝트: ionlang/ir
        public Value Parse(ParserContext context)
        {
            // Invoke the kind parser.
            Kind kind = new KindParser().Parse(context);

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

            // Abstract the token's value.
            string tokenValue = token.Value;

            // Ensure current token is recognized as a valid value.
            if (!Recognition.IsLiteral(token))
            {
                throw new Exception($"Invalid token provided as value: {token.Type}");
            }
            // Strip character/string delimiters if applicable.
            else if (token.Type == TokenType.LiteralString || token.Type == TokenType.LiteralCharacter)
            {
                tokenValue = Util.ExtractStringLiteralValue(tokenValue);
            }

            // Create the value construct.
            Value value = new Value(kind, tokenValue);

            context.Stream.Skip();

            // Return the construct.
            return(value);
        }
예제 #2
0
파일: LlvmVisitor.cs 프로젝트: ionlang/ir
        public Construct VisitValue(Value node)
        {
            // Create the value buffer.
            LlvmValue value;

            // Ensure value is identified as a literal.
            if (!Recognition.IsLiteral(node.Content))
            {
                throw new Exception("Content could not be identified as a valid literal");
            }
            // Integer literal.
            else if (Recognition.IsInteger(node.Content))
            {
                // Visit the kind.
                this.VisitKind(node.Kind);

                // Pop the resulting type off the stack.
                LlvmType type = this.typeStack.Pop();

                // Create the type and assign the value buffer.
                value = LlvmFactory.Int(type, int.Parse(node.Content));
            }
            // String literal.
            else if (Recognition.IsStringLiteral(node.Content))
            {
                value = LlvmFactory.String(node.Content);
            }
            // Unrecognized literal.
            else
            {
                throw new Exception($"Unrecognized literal: {node.Content}");
            }

            // Append the value onto the stack.
            this.valueStack.Push(value);

            // Return the node.
            return(node);
        }