Skip to content

Commit

Permalink
Added map function for vector
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-balades committed Dec 12, 2023
1 parent 416412f commit 2793ebe
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/builder/llvm/buildFunc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ void LLVMBuilder::visit(ir::Func* func) {
}

auto bodyGep = builder->CreateStructGEP(getLambdaContextType(), alloca, 1, ".func.use.gep");
if (copy)
builder->CreateMemCpy(bodyGep, llvm::MaybeAlign(), body, llvm::MaybeAlign(), builder->getInt64(layout.getTypeAllocSize(builder->getPtrTy())));
else builder->CreateStore(body, bodyGep);
//if (copy)
// builder->CreateMemCpy(bodyGep, llvm::MaybeAlign(), body, llvm::MaybeAlign(), builder->getInt64(layout.getTypeAllocSize(builder->getPtrTy())));
builder->CreateStore(body, bodyGep);
this->value = alloca;
}
return;
Expand Down
2 changes: 1 addition & 1 deletion src/builder/llvm/varFetchImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
arg = ctx->getCurrentFunction()->getArg(argumentIndex);\
} else {\
closure = ctx->closures.at(x->getId());\
arg = closure.closure; \
arg = builder->CreateLoad(builder->getPtrTy(), closure.closure); \
}\
auto index = std::distance(\
closure.variables.begin(),\
Expand Down
47 changes: 41 additions & 6 deletions src/visitors/transform/utils/deduceFunction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,26 @@ bool genericMatch(Expression::Param* generic, Expression::TypeRef* arg) {
}

auto exists = arg->getName() == generic->getName();
if (arg->getGenerics().size() > 0 && !exists) {
if (!exists && arg->getGenerics().size() > 0) {
auto generics = arg->getGenerics();
exists = std::any_of(generics.begin(), generics.end(), [&](auto& g) {
return genericMatch(generic, g);
});
}

// ----- FOR A LONG DISTANCED FUTURE: AUTO DEDUCE FUNCTION TYPES
// if (!exists && utils::is<Expression::FuncType>(arg)) {
// auto funcType = utils::cast<Expression::FuncType>(arg);
// auto args = funcType->getArgs();
// exists = std::any_of(args.begin(), args.end(), [&](auto& a) {
// return genericMatch(generic, a);
// });
// if (!exists) {
// exists = genericMatch(generic, funcType->getReturnValue());
// }
// }
/// -----

return exists;
}

Expand Down Expand Up @@ -59,11 +72,37 @@ std::pair<types::Type*, int> matchedGeneric(Expression::Param* generic, types::T
break;
}
}
if (indexExists != -1)
if (indexExists != -1) {
return {matchedGeneric(generic, generics[indexExists], genericParam).first, 3};
}
}
}

// ----- FOR A LONG DISTANCED FUTURE: AUTO DEDUCE FUNCTION TYPES
// if (auto genericRefType = utils::cast<types::FunctionType>(arg)) {
// bool match = false;
// auto args = genericRefType->getArgs();
// DUMP_S(genericRefType->getPrettyName().c_str())
// auto fn = utils::cast<Expression::FuncType>(generic->getType());
// if (!fn) return {arg, 1};
// auto fnArgs = fn->getArgs();
// for (int i = 0; i < args.size(); i++) {
// if (genericMatch(genericParam, args[i]->toRef())) {
// match = true;
// auto [deducedType, imp] = matchedGeneric(generic, fnArgs[i], genericParam);
// if (deducedType != nullptr) {
// return {matchedGeneric(generic, fnArgs[i], genericParam).first, 3};
// }
// }
// }
// if (!match) {
// if (genericMatch(genericParam, fn->getReturnValue())) {
// return {matchedGeneric(generic, genericRefType->getRetType(), genericParam).first, 3};
// }
// }
// }
/// -----

// If the generic is neither a reference nor a pointer, return the argument as is
return {arg, 1};
}
Expand All @@ -88,10 +127,6 @@ std::pair<std::optional<types::Type*>, int> Transformer::deduceFunctionType(
return {generics[index], 1};
}

if (generic->getName() == "XD") {
DUMP_S("yeh")
}

// Check if the generic is used inside the variables
const auto it = std::find_if(fnArgs.begin(), fnArgs.end(), [&](const auto& arg) {
return genericMatch(generic, arg->getType());
Expand Down
2 changes: 1 addition & 1 deletion src/visitors/transform/utils/getFunction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ std::shared_ptr<ir::Value> Transformer::getFunction(
}
E<TYPE_ERROR>(
dbgInfo,
FMT("Call parameters to '%s' does not match it's "
FMT("Call parameters to '%s' does not match its "
"function type ('%s')!",
name.c_str(),
v->getType()->getPrettyName().c_str())
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Core.sn
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public class Vector<T: Sized, Allocator: Sized = Ptr::Allocator<T>>
func map<Output>(fn: Function<func (T) => Output>) Vector<Output> {
let mut result = new Vector<Output>();
for let mut i = 0; i < self.length; i = i+1 {
result.push(fn(self[i]));
result.push(fn(*self[i]));
}
return result;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/lambdas.sn
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,15 @@ func ret_with_parent_with_arg() i32 {
return x(25);
}

@test(expect = 10)
func usage_after_lambda() i32 {
let x = 5;
let y = func() i32 {
return x;
};
let z = y();
return x + z;
}

}

26 changes: 13 additions & 13 deletions tests/vector.sn
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ func with_struct() i32 {
return vec[0] == "hello";
}

@test(expect = 20, skip)
@test(expect = 5, skip)
func map() i32 {
//let mut v = new Vector<i32>();
//v.push(1);
//v.push(2);
//v.push(3);
//v.push(4);
//v.push(5);
//let mut i = 0;
//v.map(func(x: i32) i32 {
// i = i + 1;
// return x * 2;
//});
//return i;
let mut v = new Vector<i32>();
v.push(1);
v.push(2);
v.push(3);
v.push(4);
v.push(5);
let mut i = 0;
v.map<?i32>(func(x: i32) i32 {
i = i + 1;
return x * 2;
});
return i;
}

}

0 comments on commit 2793ebe

Please sign in to comment.