Skip to content

Commit c0afa3b

Browse files
committed
Merge pull request #85 from afernandez90/develop
C codegen extended support for recursive lambda expressions
2 parents 1c59caa + 5fc16de commit c0afa3b

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/common/Functions.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,13 @@ trait CGenFunctions extends CGenEffect with BaseGenFunctions {
349349
// Case for functions with a single argument (therefore, not tupled)
350350
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
351351
case e@Lambda(fun, x, y) =>
352-
stream.println("auto "+quote(sym)+" = [&]("+remap(x.tp)+" "+quote(x)+") {")
352+
val retType = remap(getBlockResult(y).tp)
353+
stream.println("function<"+retType+"("+
354+
remap(x.tp)+")> "+quote(sym)+
355+
" = [&]("+remap(x.tp)+" "+quote(x)+") {")
353356
emitBlock(y)
354357
val z = getBlockResult(y)
355-
if (remap(z.tp) != "void")
358+
if (retType != "void")
356359
stream.println("return " + quote(z) + ";")
357360
stream.println("};")
358361
case Apply(fun, arg) =>
@@ -374,10 +377,13 @@ trait CGenTupledFunctions extends CGenFunctions with GenericGenUnboxedTupleAcces
374377

375378
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
376379
case Lambda(fun, UnboxedTuple(xs), y) =>
377-
stream.println("auto "+quote(sym)+" = [&]("+xs.map(s=>remap(s.tp)+" "+quote(s)).mkString(",")+") {")
380+
val retType = remap(getBlockResult(y).tp)
381+
stream.println("function<"+retType+"("+
382+
xs.map(s=>remap(s.tp)).mkString(",")+")> "+quote(sym)+
383+
" = [&]("+xs.map(s=>remap(s.tp)+" "+quote(s)).mkString(",")+") {")
378384
emitBlock(y)
379385
val z = getBlockResult(y)
380-
if (remap(z.tp) != "void")
386+
if (retType != "void")
381387
stream.println("return " + quote(z) + ";")
382388
stream.println("};")
383389
case Apply(fun, UnboxedTuple(args)) =>

test-out/epfl/test14-cgen2.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ int32_t main(int32_t);
88
#include <string.h>
99
#include <stdbool.h>
1010
int32_t main(int32_t x0) {
11-
auto x1 = [&](int32_t x2,int32_t x3) {
11+
function<int32_t(int32_t,int32_t)> x1 = [&](int32_t x2,int32_t x3) {
1212
int32_t x4 = x2;
1313
bool x6 = x4 == 0;
1414
int32_t x12;

0 commit comments

Comments
 (0)