Ejemplo n.º 1
0
        public override void Codegen(CIntermediateLang cil, IndentingStringBuilder sb)
        {
            var argTmps = new List <string>();

            foreach (var arg in Args)
            {
                arg.Codegen(cil, sb);
                argTmps.Add(cil.LastUsedVar);
            }
            Callee.Codegen(cil, sb);

            // This doesn't account for:
            //      calling a returned function pointer
            //      calling from an array subscript
            //      calling a member
            var callee      = cil.SymTable.LookupFunction(cil.LastUsedVar);
            var callRetType = callee.TryInferType(cil);

            sb.LineDecl(SourceInfo);
            if (callRetType == "void")
            {
                sb.Append(string.Format("{0}(", callee.Name));
                cil.LastUsedVar = null;
            }
            else
            {
                var tmp = NameGenerator.NewTemp();
                sb.Append(string.Format(
                              "{0} {1} = {2}(",
                              callee.TryInferType(cil),
                              tmp,
                              callee.Name));
                cil.LastUsedVar = tmp;
            }
            for (int i = 0; i < argTmps.Count; ++i)
            {
                sb.AppendNoIndent(argTmps[i]);
                if (i + 1 < argTmps.Count)
                {
                    sb.AppendNoIndent(", ");
                }
            }
            sb.AppendLineNoIndent(");");
        }