FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/examples/tutorial/tutorial08.cpp at master · fiveakatto/daScript · GitHub
fiveakatto
/
daScript
Public
forked from
GaijinEntertainment/daScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
daScript
/
examples
/
tutorial
/
tutorial08.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
129 lines (121 loc) · 4.38 KB
Breadcrumbs
daScript
/
examples
/
tutorial
/
tutorial08.cpp
Copy path
File metadata and controls
129 lines (121 loc) · 4.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#
include
"
daScript/daScript.h
"
#
include
"
daScript/ast/ast_serializer.h
"
using
namespace
das
;
//
function, which we are going to expose to daScript
float
xmadd
(
float
a,
float
b,
float
c,
float
d ) {
return
a*b + c*d;
}
//
making custom builtin module
class
Module_Tutorial08
:
public
Module
{
public:
Module_Tutorial08
() : Module(
"
tutorial_08
"
) {
//
module name, when used from das file
ModuleLibrary lib;
lib.
addModule
(
this
);
lib.
addBuiltInModule
();
//
adding constant to the module
addConstant
(*
this
,
"
SQRT2
"
,
sqrtf
(
2.0
));
//
adding function to the module
addExtern<
DAS_BIND_FUN
(xmadd)>(*
this
, lib,
"
xmadd
"
,SideEffects::none,
"
xmadd
"
);
}
};
//
registering module, so that its available via 'NEED_MODULE' macro
REGISTER_MODULE
(Module_Tutorial08);
#
define
TUTORIAL_NAME
"
/examples/tutorial/tutorial08.das
"
//
#define TUTORIAL_NAME "/examples/test/unit_tests/abc.das"
//
#define TUTORIAL_NAME "/examples/test/unit_tests/access_private_from_lambda.das"
//
#define TUTORIAL_NAME "/examples/test/unit_tests/aka.das"
//
#define TUTORIAL_NAME "/examples/test/unit_tests/aonce.das"
void
tutorial
() {
TextPrinter tout;
//
output stream for all compiler messages (stdout. for stringstream use TextWriter)
ModuleGroup dummyLibGroup;
//
module group for compiled program
auto
fAccess
= make_smart<FsFileAccess>();
//
default file access
//
compile program
auto
program =
compileDaScript
(
getDasRoot
() +
TUTORIAL_NAME
,
fAccess
, tout, dummyLibGroup);
if
( program->
failed
() ) {
//
if compilation failed, report errors
tout <<
"
failed to compile
\n
"
;
for
(
auto
& err : program->
errors
) {
tout <<
reportError
(err.
at
, err.
what
, err.
extra
, err.
fixme
, err.
cerr
);
}
return
;
}
//
serialize
auto
writeTo = make_unique<SerializationStorageVector>();
AstSerializer
ser
( writeTo.
get
(),
true
);
program->
serialize
(ser);
ser.
moduleLibrary
=
nullptr
;
program.
reset
();
tout <<
"
serialized size:
"
<< writeTo->
buffer
.
size
() <<
"
\n
"
;
//
deserialize
auto
readFrom = make_unique<SerializationStorageVector>();
readFrom->
buffer
=
das::move
(writeTo->
buffer
);
AstSerializer
deser
( readFrom.
get
(),
false
);
program = make_smart<Program>();
program->
serialize
(deser);
deser.
moduleLibrary
=
nullptr
;
tout <<
"
deserialized
\n
"
;
//
create daScript context
Context
ctx
(program->
getContextStackSize
());
if
( !program->
simulate
(ctx, tout) ) {
//
if interpretation failed, report errors
tout <<
"
failed to simulate
\n
"
;
for
(
auto
& err : program->
errors
) {
tout <<
reportError
(err.
at
, err.
what
, err.
extra
, err.
fixme
, err.
cerr
);
}
return
;
}
//
find function 'test' in the context
auto
fnTest = ctx.
findFunction
(
"
test
"
);
if
( !fnTest ) {
tout <<
"
function 'test' not found
\n
"
;
return
;
}
//
verify if 'test' is a function, with the correct signature
//
note, this operation is slow, so don't do it every time for every call
if
( !verifyCall<
bool
>(fnTest->
debugInfo
, dummyLibGroup) ) {
tout <<
"
function 'test', call arguments do not match. expecting def test : bool
\n
"
;
return
;
}
ctx.
restart
();
bool
result = cast<
bool
>::
to
(ctx.
eval
(fnTest,
nullptr
));
if
(
auto
ex = ctx.
getException
() ) {
tout <<
"
exception:
"
<< ex <<
"
\n
"
;
return
;
}
if
( !result ) {
tout <<
"
failed
\n
"
;
return
;
}
tout <<
"
ok
\n
"
;
}
int
main
(
int
,
char
* [] ) {
//
request all da-script built in modules
NEED_ALL_DEFAULT_MODULES
;
//
request our custom module
NEED_MODULE
(Module_Tutorial08);
//
Initialize modules
Module::Initialize
();
//
run the tutorial
tutorial
();
//
shut-down daScript, free all memory
Module::Shutdown
();
#
if
DAS_SMART_PTR_TRACKER
if
( g_smart_ptr_total ) {
TextPrinter tp;
tp <<
"
smart pointers leaked:
"
<<
uint64_t
(g_smart_ptr_total) <<
"
\n
"
;
#
if
DAS_SMART_PTR_ID
tp <<
"
leaked ids:
"
;
vector<
uint64_t
> ids;
for
(
auto
it : ptr_ref_count::ref_count_ids ) {
ids.
push_back
(it);
}
std::sort
(ids.
begin
(), ids.
end
());
for
(
auto
it : ids ) {
tp <<
"
"
<<
HEX
<< it <<
DEC
;
}
tp <<
"
\n
"
;
#
endif
}
#
endif
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL