Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGrulich committed Mar 8, 2024
1 parent cc609d9 commit b77959d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
50 changes: 44 additions & 6 deletions nautilus-api/include/Interface/function.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <functional>
#include "DataTypes/Val.hpp"

#pragma once

namespace nautilus {



template<is_base_type R, is_base_type... FunctionArguments>
class CallableNautilusFunction {
public:
Expand Down Expand Up @@ -33,14 +34,51 @@ namespace nautilus {
}

template<is_integral... FunctionArgumentsRaw>
auto invoke(FunctionArgumentsRaw... args){
auto invoke(FunctionArgumentsRaw... args) {
return (*this)(args...);
}

private:
R (*fnptr)(FunctionArguments...);
};


template<typename R, typename... FunctionArguments>
class CallableRuntimeFunction {
public:
CallableRuntimeFunction(R (*fnptr)(FunctionArguments...)) : fnptr(fnptr) {

};

template<typename Arg>
auto transform(Arg argument) {
return details::getRawValue(argument);
}

template<typename... FunctionArgumentsRaw>
auto operator()(FunctionArgumentsRaw... args) {
// function is called from an external context.
auto result = fnptr(transform((args))...);
return make_value(result);
}

template<is_integral... FunctionArgumentsRaw>
auto invoke(FunctionArgumentsRaw... args) {
return (*this)(args...);
}

private:
R (*fnptr)(FunctionArguments...);
};


template<is_integral R, is_integral... FunctionArguments, typename... ValueArguments>
auto
invoke(R (*fnptr)(FunctionArguments...), ValueArguments... args) {
return CallableRuntimeFunction<R, FunctionArguments...>(fnptr)(args...);
}


template<class>
constexpr bool is_reference_wrapper_v = false;
template<class U>
Expand All @@ -53,6 +91,7 @@ namespace nautilus {
template<is_base_type R, is_base_type... FunctionArguments>
auto
Function(R (*fnptr)(FunctionArguments...)) {

return CallableNautilusFunction<R, FunctionArguments...>(fnptr);
}

Expand All @@ -62,16 +101,15 @@ namespace nautilus {
MemberFunction(TP _Class::* __pm) : __pm(__pm) {}

template<typename ... FunctionArgumentsRaw>
auto operator()(_Class* mem, FunctionArgumentsRaw... args) {
auto operator()(_Class *mem, FunctionArgumentsRaw... args) {
// we are in the nautilus context
// keep current tracing context and continue tracing
// TODO implement polymorphic inline cache
return std::invoke(__pm, mem, (args)...);
// return __pm(mem, (args)...);
return std::invoke(__pm, mem, (args)...);
// return __pm(mem, (args)...);
}



TP _Class::* __pm;
};

Expand Down
14 changes: 12 additions & 2 deletions nautilus-api/test/ExecutionTests/Executions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "engine/engine.hpp"
#include "ExpressionFunctions.hpp"
#include "ControlFlowFunctions.hpp"
#include "RunctimeCallFunctions.hpp"
#include "LoopFunctions.hpp"

namespace nautilus::engine {
Expand Down Expand Up @@ -31,8 +32,7 @@ namespace nautilus::engine {
REQUIRE(f((double) 7.0) == 14);
REQUIRE(f((double) -7) == 0);
REQUIRE(f((double) -14) == -7);
}
SECTION("castFloatToDoubleAddExpression") {
}SECTION("castFloatToDoubleAddExpression") {
auto f = engine.registerFunction(castFloatToDoubleAddExpression);
REQUIRE(f((float) 7.0) == 14);
REQUIRE(f((float) -7) == 0);
Expand Down Expand Up @@ -132,6 +132,14 @@ namespace nautilus::engine {
}
}

void functionCallExecutionTest(engine::NautilusEngine &engine) {
SECTION("sumLoop") {
auto f = engine.registerFunction(simpleDirectCall);
REQUIRE(f(10, 10) == 20);
REQUIRE(f(0, 1) == 1);
}
}

TEMPLATE_TEST_CASE("Engine Test", "[value][template]", int8_t) {
auto engine = engine::NautilusEngine();
SECTION("expressionTest") {
Expand All @@ -140,6 +148,8 @@ namespace nautilus::engine {
controlFlowTest(engine);
}SECTION("loopExecutionTest") {
loopExecutionTest(engine);
}SECTION("functionCallExecutionTest") {
functionCallExecutionTest(engine);
}
}

Expand Down
11 changes: 0 additions & 11 deletions nautilus-api/test/ExecutionTests/LoopFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ namespace nautilus::engine {
return agg;
}

/*
TEST_P(LoopCompilationTest, ifElseSumLoopTest) {
auto execution = Nautilus::Tracing::traceFunctionWithReturn([]() {
return ifElseSumLoop();
});
auto engine = prepare(execution);
auto function = engine->getInvocableMember<int32_t>("execute");
ASSERT_EQ(function(), 56);
}*/

val<int32_t> elseOnlySumLoop() {
val<int32_t> agg = val<int32_t>(1);
for (val<int32_t> start = 0; start < 10; start = start + 1) {
Expand Down
14 changes: 14 additions & 0 deletions nautilus-api/test/ExecutionTests/RunctimeCallFunctions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <engine/engine.hpp>
namespace nautilus::engine {

int32_t add(int32_t x, int32_t y) {
return x + y;
}

val<int32_t> simpleDirectCall(val<int32_t> x, val<int32_t> y) {
return invoke<>(add, x, y);
}


}

0 comments on commit b77959d

Please sign in to comment.