Beispiel #1
0
        protected CheckedType VarDestructuringNode(TypeCheckerVisitor checker, VariableDestructuringNode vardestnode)
        {
            var initType = vardestnode.Right.Visit(checker);

            for (int i = 0; i < vardestnode.Left.Count; i++)
            {
                var declaration = vardestnode.Left[i];

                if (declaration == null)
                {
                    continue;
                }

                // Get the variable type from the declaration
                var lhsType = checker.SymbolTable.GetVariableSymbol(declaration.Value).TypeSymbol;
                var rhsType = (initType.TypeSymbol as Tuple).Elements[i];

                // When lhs is "var", take the type from the right hand side expression, or throw if it is not available

                /*if (!lhsType.Type.IsAssignableFrom(rhsType))
                 *  throw new SymbolException($"Cannot assign type {rhsType} to variable of type {lhsType}");*/
            }

            return(null);
        }
        protected void VarDestructuringNode(SymbolResolverVisitor visitor, VariableDestructuringNode destrnode)
        {
            var types = destrnode.Right.Visit(visitor);

            foreach (var declaration in destrnode.Left)
            {
                if (declaration == null)
                {
                    continue;
                }

                // Get the identifier name
                var variableName = declaration.Value;

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

                // If the type anotation is not specific (uses 'var'), we need to create an anonymous type
                // for every variable. If not, we just get the type information from the token
                var varType = destrnode.Information.Type.Type == Syntax.TokenType.Variable
                    ? visitor.Inferrer.NewAnonymousType()
                    : SymbolHelper.GetTypeSymbol(visitor.SymbolTable, visitor.Inferrer, destrnode.Information.Type);

                // Create the new symbol for the variable
                var boundSymbol = visitor.SymbolTable.AddNewVariableSymbol(variableName, varType, Access.Public, SymbolHelper.GetStorage(destrnode.Information.Mutability));

                if (varType is Anonymous asym)
                {
                    visitor.Inferrer.TrackSymbol(asym, boundSymbol);
                }
            }
        }
Beispiel #3
0
        protected IType VarDestructuringNode(TypeInferrerVisitor visitor, VariableDestructuringNode destructuringNode)
        {
            var rhsTupleType = destructuringNode.Right.Visit(visitor) as Tuple;

            for (int i = 0; i < destructuringNode.Left.Count; i++)
            {
                var declaration = destructuringNode.Left[i];

                if (declaration == null)
                {
                    continue;
                }

                // Symbol should be already resolved here
                var lhs = visitor.SymbolTable.GetVariableSymbol(declaration.Value);

                // If it is a variable definition, get the right-hand side type info
                var rhsType = rhsTupleType.Elements[i];

                // Check types to see if we can unify them
                var generalType = visitor.Inferrer.FindMostGeneralType(lhs.TypeSymbol, rhsType is IType rts ? rts : (rhsType as IVariable).TypeSymbol);

                visitor.Inferrer.Unify(visitor.SymbolTable, generalType, lhs as IVariable);
            }

            return(rhsTupleType);
        }
Beispiel #4
0
        protected Operand VarDestructuringNode(ILGenerator generator, VariableDestructuringNode vardestnode)
        {
            // Get the variable type
            //TypeResolver typeresolver = TypeResolver.GetTypeResolverFromToken(vardestnode.VarType.TypeToken);

            /*vardestnode.DestructInit.Exec()
             *
             * foreach (var declaration in vardestnode.VarDefinitions)
             * {
             *  var identifierToken = declaration.Item1;
             *  var initializerInstr = declaration.Item2?.Exec(generator);
             *
             *  generator.Emmit(new LocalVarInstruction(dataType, identifierToken.Value.ToString(), initializerInstr?.TargetName));
             * }*/
            return(null);
        }
        protected MutabilityCheckResult VarDestructuringNode(MutabilityCheckerVisitor checker, VariableDestructuringNode vardestnode)
        {
            vardestnode.Right.Visit(checker);

            return(null);
        }