Example #1
0
        private static void TryCollectFunctionDef(Context ctx, diagn.Reporter reporter, mod.Unit unit, syn.Node.FunctionDef node)
        {
            if (node == null)
            {
                return;
            }

            var hDef = ctx.defs.Reserve();

            ctx[hDef]                  = new sema.Def.Function();
            ctx[hDef].spanDef          = node.span;
            unit.semanticMap.def[node] = hDef;

            if (node.name != null)
            {
                var identifierToken = (node.name as syn.Node.Identifier).token;
                ctx[hDef].spanDefName = identifierToken.span;

                var namespaceNode = ctx.names.FindOrReserve(identifierToken.excerpt);
                if (namespaceNode.item != null)
                {
                    ReportDuplicate(ctx, reporter, identifierToken.span, namespaceNode);
                }

                else
                {
                    namespaceNode.item = new sema.Namespace.Item.Def {
                        def = hDef
                    };
                    ctx[hDef].namespaceNode                = namespaceNode;
                    unit.semanticMap.references[node]      = namespaceNode;
                    unit.semanticMap.references[node.name] = namespaceNode;
                }
            }
        }
        private static void TryCollectFunctionDef(Context ctx, diagn.Reporter reporter, mod.Unit unit, syn.Node.FunctionDef node)
        {
            if (node == null)
            {
                return;
            }

            var hDef = unit.semanticMap.def[node];

            if (hDef == null)
            {
                reporter.InternalError("def not found for node", new diagn.Caret(node.span));
            }

            else
            {
                var functionDef = ctx[hDef] as sema.Def.Function;
                functionDef.body = new sema.Code.Body();

                if (node.returnType == null)
                {
                    functionDef.body.registers.Add(new sema.Code.Register
                    {
                        type = new sema.Type.Tuple()
                    });
                }
                else
                {
                    functionDef.body.registers.Add(new sema.Code.Register
                    {
                        spanDef = node.returnType.span,
                        type    = TypeResolver.Resolve(ctx, reporter, node.returnType)
                    });
                }

                foreach (var child in node.parameters)
                {
                    var nodeParameter  = child as syn.Node.FunctionDefParameter;
                    var nodeIdentifier = nodeParameter.identifier as syn.Node.Identifier;

                    var parameterName = nodeIdentifier.token.excerpt;
                    var parameterType = TypeResolver.Resolve(ctx, reporter, nodeParameter.type);

                    functionDef.body.parameterCount++;
                    functionDef.body.registers.Add(new sema.Code.Register
                    {
                        spanDef     = nodeParameter.span,
                        spanDefName = nodeIdentifier.span,
                        name        = parameterName,
                        type        = parameterType
                    });
                }
            }
        }
Example #3
0
        private static void TryCollectFunctionDef(Context ctx, diagn.Reporter reporter, mod.Unit unit, syn.Node.FunctionDef node)
        {
            if (node == null)
            {
                return;
            }

            var hDef = unit.semanticMap.def[node];

            if (hDef == null)
            {
                reporter.InternalError("def not found for node", new diagn.Caret(node.span));
            }

            else
            {
                var functionDef = ctx[hDef] as sema.Def.Function;
                CodeResolver.ResolveFunctionBody(ctx, reporter, functionDef.body, node.body);
            }
        }