/
mp_tutorial.cpp
63 lines (53 loc) · 1.51 KB
/
mp_tutorial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// [MPSL-Test]
// MathPresso's Shading Language with JIT Engine for C++.
//
// [License]
// Zlib - See LICENSE.md file in the package.
// [Dependencies]
#include "./mpsl.h"
#include "./mp_utils.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
struct Args {
double a, b;
float c;
double result;
};
int main(int argc, char* argv[]) {
mpsl::Context ctx = mpsl::Context::create();
mpsl::LayoutTmp<> args;
args.addMember("a" , mpsl::kTypeDouble | mpsl::kTypeRO, MPSL_OFFSET_OF(Args, a));
args.addMember("b" , mpsl::kTypeDouble | mpsl::kTypeRO, MPSL_OFFSET_OF(Args, b));
args.addMember("c" , mpsl::kTypeFloat | mpsl::kTypeRO, MPSL_OFFSET_OF(Args, c));
args.addMember("@ret", mpsl::kTypeDouble | mpsl::kTypeWO, MPSL_OFFSET_OF(Args, result));
const char body[] =
"double main() {\n"
" double x = sqrt(a * b) * c;\n"
" return ++x;\n"
"}\n";
printf("[Program]\n%s\n", body);
uint32_t options =
mpsl::kOptionVerbose |
mpsl::kOptionDebugAst |
mpsl::kOptionDebugIR |
mpsl::kOptionDebugASM ;
TestLog log;
mpsl::Program1<> program;
mpsl::Error err = program.compile(ctx, body, options, args, &log);
if (err) {
printf("Compilation failed: ERROR 0x%08X\n", err);
}
else {
Args args;
args.a = 4.0;
args.b = 16.0;
args.c = 0.5f;
err = program.run(&args);
if (err == mpsl::kErrorOk)
printf("Return=%.17g\n", args.result);
else
printf("Execution failed: ERROR %08X\n", static_cast<unsigned int>(err));
}
return 0;
}