Example #1
0
        static void declare(AST_Node node)
        {
            if (node.result?.dType != null)
            {
                skipSymbol(node);
                return;
            }

            var      enclosureNode = (AST_Scope)enclosure.node;
            var      declaration   = (AST_Declaration)node;
            DataType allocType     = declaration.typeFrom.result.dType;

            // Has to be instantiable
            if ((allocType.flags & DataType.Flags.INSTANTIABLE) == 0)
            {
                throw Jolly.addError(node.location, "The type {0} is not instantiable.".fill(allocType));
            }

            switch (enclosure.type)
            {
            case NT.FUNCTION:
            case NT.GLOBAL: {
                var alloc = new IR_Allocate {
                    dType = allocType
                };
                declaration.symbol.declaration = alloc;
                declaration.result             = instructions.Add(alloc);

                // Variable is an argument
                if (context.kind == Context.Kind.FUNCTION_DECLARATION)
                {
                    var function     = (AST_Function)enclosure.node;
                    var functionType = (DataType_Function)function.result.dType;
                    functionType.arguments[function.finishedArguments] = allocType;
                    function.finishedArguments += 1;
                    cursor = enclosure.end;
                }
            } break;

            case NT.STRUCT: {
                ((DataType_Struct)enclosureNode.result.dType).finishDefinition(declaration.text, allocType);
            } break;

            default: throw Jolly.addError(declaration.location, "Cannot define a variable here");
            }
            if (allocType == Lookup.AUTO)
            {
                declaration.infer = inferAutoVariable;
                contextStack.Push(new Context(declaration.memberCount + cursor, Context.Kind.DECLARATION)
                {
                    target = declaration
                });
            }
        }
Example #2
0
        static IR packTuple(AST_Tuple tuple, DataType_Tuple tupleType)
        {
            IR_Allocate alloc = new IR_Allocate {
                dType = tupleType
            };

            tuple.values.forEach((val, i) => {
                IR member = instructions.Add(IR.getMember(alloc, tupleType.members[i], i));
                instructions.Add(IR.operation <IR_Assign>(member, val.result, null));
            });
            return(alloc);
        }