public override AstNode Visit(VariableDeclaration node) { // Begin the node. builder.BeginNode(node); // Get the initial value. Expression initial = node.GetInitialValue(); if(initial == null) return builder.EndNode(); // Visit the initial value. initial.Accept(this); // Get the initial value type and the node type. object value = initial.GetNodeValue(); IChelaType valueType = initial.GetNodeType(); IChelaType varType = node.GetNodeType(); // Cast the initial value. if(valueType != varType) Cast(node, value, valueType, varType); // Store the value. PerformAssignment(node, node.GetVariable()); return builder.EndNode(); }
public override AstNode Visit(VariableDeclaration node) { // Get the variable name. string varName = node.GetName(); // Avoid redefinition. if(currentContainer.FindMember(varName) != null) Error(node, "attempting to redefine a variable in a same scope."); // Get the initial value. Expression initialValue = node.GetInitialValue(); // Visit the initial value. // Do this here to use the state previous to the variable declaration. if(initialValue != null) initialValue.Accept(this); // Create the variable. LocalVariable local = new LocalVariable(node.GetName(), (LexicalScope)currentContainer, node.GetNodeType()); // Store it in the node. node.SetVariable(local); // Check the compatibility with the initial value. if(initialValue == null) return node; // Get the variable type and the initial value type. IChelaType varType = node.GetNodeType(); IChelaType valueType = initialValue.GetNodeType(); // Class instances are held by reference. if(varType != valueType && Coerce(node, varType, valueType, initialValue.GetNodeValue()) != varType) Error(node, "cannot implicitly cast from {0} to {1}.", valueType.GetFullName(), varType.GetFullName()); return node; }