Objects Initial
This commit is contained in:
parent
00bc875846
commit
803ce23169
76 changed files with 7895 additions and 17 deletions
90
Objects/tests/TestEntry.cpp
Normal file
90
Objects/tests/TestEntry.cpp
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
|
||||
|
||||
#include "MethodObject/methodobject.h"
|
||||
#include "primitives/primitives.h"
|
||||
|
||||
#include "Interpreter/Interpreter.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include "ByteCodeGen/Function.h"
|
||||
|
||||
using namespace osc;
|
||||
using namespace tp;
|
||||
using namespace obj;
|
||||
|
||||
void TestCompile(ByteCode& bytecode) {
|
||||
using namespace BCgen;
|
||||
|
||||
Genereate(bytecode,
|
||||
StmScope({
|
||||
|
||||
StmDefFunc("main", {}, {
|
||||
StmDefLocal("i1", ExprConst(1)),
|
||||
StmDefLocal("i2", ExprConst(2)),
|
||||
|
||||
StmDefFunc("add", {"first", "second"}, {
|
||||
StmReturn(ExprAdd(ExprLocal("first"), ExprLocal("second")))
|
||||
}),
|
||||
|
||||
StmPrint(ExprLocal("i1")),
|
||||
StmPrint(ExprConst(" + ")),
|
||||
StmPrint(ExprLocal("i2")),
|
||||
StmPrint(ExprConst(" = ")),
|
||||
|
||||
StmPrint(ExprFunc("add")->ExprCall({ ExprLocal("i1"), ExprLocal("i2") })),
|
||||
|
||||
StmPrint(ExprConst("\n")),
|
||||
}),
|
||||
|
||||
StmPrint(ExprBoolNot(ExprConst(0))),
|
||||
|
||||
StmWhile(ExprConst(false), StmScope({
|
||||
StmIgnore(ExprFunc("main")->ExprCall({}))
|
||||
})
|
||||
),
|
||||
|
||||
StmIf(ExprConst(true),
|
||||
StmScope({
|
||||
//StmIgnore(ExprFunc("main")->ExprCall({})),
|
||||
//StmPrint(ExprConst("true")),
|
||||
}),
|
||||
StmScope({
|
||||
//StmPrint(ExprConst("false")),
|
||||
})
|
||||
)
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
tp::alloc_init();
|
||||
string::Initialize();
|
||||
Logger::init();
|
||||
|
||||
objects_init();
|
||||
primitives_define_types();
|
||||
MethodObject::Initialize();
|
||||
|
||||
{
|
||||
Interpreter interp;
|
||||
|
||||
ByteCode bytecode;
|
||||
TestCompile(bytecode);
|
||||
|
||||
GLog->write(" >> Exec: start \n");
|
||||
auto start = get_time();
|
||||
interp.exec(&bytecode);
|
||||
auto end = get_time();
|
||||
GLog->write(sfmt("\n >> Exec time %i ms", end - start));
|
||||
}
|
||||
|
||||
MethodObject::UnInitialize();
|
||||
objects_finalize();
|
||||
primitives_uninitialize();
|
||||
|
||||
Logger::deinit();
|
||||
string::UnInitialize();
|
||||
tp::alloc_uninit();
|
||||
}
|
||||
75
Objects/tests/test.cpp
Normal file
75
Objects/tests/test.cpp
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
|
||||
#include "primitives/primitives.h"
|
||||
|
||||
#include "compiler/function.h"
|
||||
#include "interpreter/interpreter.h"
|
||||
|
||||
#include "CmdArgParser.h"
|
||||
|
||||
void compile(char argc, const char* argv[]) {
|
||||
tp::CmdArgParser args = {
|
||||
{ "script", tp::CmdArgParser::Arg::FILE_IN },
|
||||
{ "out", "out.mo" }
|
||||
};
|
||||
|
||||
if (!args.parse(argc, argv, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto& script_file = args.getFile("script");
|
||||
auto outpath = args.getString("out");
|
||||
|
||||
tp::string script;
|
||||
script.loadFile(&script_file);
|
||||
|
||||
auto method = obj::MethodObject::create(script);
|
||||
|
||||
obj::NDO->save(method, outpath);
|
||||
}
|
||||
|
||||
void execute(char argc, const char* argv[]) {
|
||||
tp::CmdArgParser args = {
|
||||
{ "object_path", tp::CmdArgParser::Arg::STR },
|
||||
{ "debug", false }
|
||||
};
|
||||
|
||||
if (!args.parse(argc, argv, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto object_path = args.getString("object_path");
|
||||
auto debug = args.getBool("debug");
|
||||
|
||||
auto obj = obj::NDO->load(object_path);
|
||||
if (!obj) {
|
||||
printf("Invalid Object File\n");
|
||||
return;
|
||||
}
|
||||
|
||||
NDO_CASTV(obj::MethodObject, obj, method);
|
||||
if (!method) {
|
||||
printf("Object Is Not A Method\n");
|
||||
return;
|
||||
}
|
||||
|
||||
obj::Interpreter interpreter;
|
||||
interpreter.execAll(method);
|
||||
}
|
||||
|
||||
|
||||
int main(char argc, const char* argv[]) {
|
||||
|
||||
tp::ModuleManifest* ModuleDependencies[] = { &obj::gModuleObjects, NULL };
|
||||
tp::ModuleManifest TestModule("Test", NULL, NULL, ModuleDependencies);
|
||||
if (!TestModule.initialize()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef OBC
|
||||
compile(argc, argv);
|
||||
#elif OBVM
|
||||
execute(argc, argv);
|
||||
#endif // OBC
|
||||
|
||||
TestModule.deinitialize();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue