public Operand Visit(ILGenerator generator, PrimitiveNode literal)
        {
            // Take the raw value
            var value = literal.Literal.Value;

            // Return an immediate operand
            return(new ImmediateOperand(OperandType.FromToken(literal.Literal), value));
        }
Example #2
0
        protected Operand VarDefinitionNode(ILGenerator generator, VariableDefinitionNode vardecl)
        {
            foreach (var definition in vardecl.Definitions)
            {
                // Get the identifier name
                var variableName = definition.Left.Value;

                // Check if the symbol is already defined
                if (generator.SymbolTable.SymbolIsDefinedInBlock(variableName))
                {
                    throw new SymbolException($"Symbol {variableName} is already defined.");
                }

                // If it is a variable definition, emmit the code to resolve the right-hand side
                var rhs = definition.Right?.Visit(generator);

                // Get the variable type from the declaration
                var type = OperandType.FromToken(vardecl.Information.Type);

                if (type == OperandType.Auto)
                {
                    // If variable is implicitly typed, the right-hand side expression is a must
                    if (rhs == null)
                    {
                        throw new AstWalkerException($"Implicitly-typed variable must be initialized");
                    }

                    // Take the type from the right hand side expression
                    type = rhs.Type;
                }

                // Create the new symbol for the variable
                var symbol = generator.SymbolTable.NewSymbol(variableName, type);

                generator.Emmit(new VarInstruction(symbol, type, rhs));
            }
            return(null);
        }