-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support construct function by cpp user
- Loading branch information
Showing
6 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
|
||
#include <wamon/interpreter.h> | ||
#include <wamon/variable.h> | ||
#include <wamon/variable_void.h> | ||
|
||
#include <functional> | ||
|
||
#include "wamon/ast.h" | ||
|
||
namespace wamon { | ||
|
||
class ExecutableBlockStmt : public BlockStmt { | ||
public: | ||
using executable_type = std::function<std::shared_ptr<Variable>(Interpreter&)>; | ||
|
||
ExecutableBlockStmt() = default; | ||
|
||
std::string GetStmtName() override { return "executable_block_stmt"; } | ||
|
||
ExecuteResult Execute(Interpreter& ip) override { | ||
if (executable_) { | ||
return ExecuteResult::Return(executable_(ip)); | ||
} | ||
return ExecuteResult::Return(GetVoidVariable()); | ||
} | ||
|
||
void SetExecutable(executable_type&& e) { executable_ = std::move(e); } | ||
|
||
void SetExecutable(const executable_type& e) { executable_ = e; } | ||
|
||
private: | ||
executable_type executable_; | ||
}; | ||
|
||
} // namespace wamon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
#include "wamon/function_def.h" | ||
|
||
#include "wamon/ast.h" | ||
#include "wamon/executable_block_stmt.h" | ||
|
||
namespace wamon { | ||
|
||
void FunctionDef::SetBlockStmt(std::unique_ptr<BlockStmt>&& block_stmt) { block_stmt_ = std::move(block_stmt); } | ||
|
||
bool FunctionDef::IsExecutableBlockStmt() const { | ||
return dynamic_cast<ExecutableBlockStmt*>(block_stmt_.get()) != nullptr; | ||
} | ||
|
||
} // namespace wamon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "wamon/executable_block_stmt.h" | ||
|
||
#include <wamon/ast.h> | ||
#include <wamon/package_unit.h> | ||
#include <wamon/type_checker.h> | ||
#include <wamon/variable_int.h> | ||
#include <wamon/variable_string.h> | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "gtest/gtest.h" | ||
#include "wamon/function_def.h" | ||
#include "wamon/interpreter.h" | ||
#include "wamon/parser.h" | ||
#include "wamon/scanner.h" | ||
#include "wamon/type.h" | ||
#include "wamon/variable.h" | ||
|
||
using namespace wamon; | ||
|
||
std::shared_ptr<FunctionDef> generate_func_for_test() { | ||
std::shared_ptr<FunctionDef> fd(new FunctionDef("my_fn")); | ||
fd->AddParamList({ | ||
.name = "main$param_a", | ||
.type = TypeFactory<int>::Get(), | ||
.is_ref = false, | ||
}); | ||
|
||
fd->AddParamList({ | ||
.name = "main$param_ref", | ||
.type = TypeFactory<std::string>::Get(), | ||
.is_ref = true, | ||
}); | ||
|
||
fd->SetReturnType(TypeFactory<int>::Get()); | ||
|
||
auto executable_block = std::make_unique<ExecutableBlockStmt>(); | ||
executable_block->SetExecutable([](Interpreter& ip) -> std::shared_ptr<Variable> { | ||
auto va = ip.FindVariableById("main$param_a"); | ||
auto vref = ip.FindVariableById("main$param_ref"); | ||
va = ip.CallFunctionByName("main$update_value_from_script", {va}); | ||
int ret = AsIntVariable(va)->GetValue(); | ||
AsStringVariable(vref)->SetValue("changed"); | ||
return std::make_shared<IntVariable>(ret * 2, Variable::ValueCategory::RValue, ""); | ||
}); | ||
|
||
fd->SetBlockStmt(std::move(executable_block)); | ||
return fd; | ||
} | ||
|
||
TEST(executable_block_stmt, basic) { | ||
std::string script = R"( | ||
package main; | ||
func update_value_from_script(int a) -> int { | ||
return a + 10; | ||
} | ||
)"; | ||
|
||
Scanner scan; | ||
auto tokens = scan.Scan(script); | ||
auto pu = Parse(tokens); | ||
pu.AddFuncDef(generate_func_for_test()); | ||
pu = MergePackageUnits(std::move(pu)); | ||
auto va = std::make_shared<IntVariable>(1, Variable::ValueCategory::RValue, ""); | ||
auto vb = std::make_shared<StringVariable>("hello", Variable::ValueCategory::LValue, ""); | ||
TypeChecker tc(pu); | ||
std::string reason; | ||
bool succ = tc.CheckAll(reason); | ||
EXPECT_EQ(succ, true) << reason; | ||
|
||
Interpreter ip(pu); | ||
auto ret = ip.CallFunctionByName("main$my_fn", {va, vb}); | ||
EXPECT_EQ(AsIntVariable((ret))->GetValue(), 22); | ||
EXPECT_EQ(AsStringVariable(vb)->GetValue(), "changed"); | ||
} |