FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/examples/tutorial/tutorial06.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
/
tutorial06.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
117 lines (110 loc) · 3.99 KB
Breadcrumbs
daScript
/
examples
/
tutorial
/
tutorial06.cpp
Copy path
File metadata and controls
117 lines (110 loc) · 3.99 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
#
include
"
daScript/daScript.h
"
using
namespace
das
;
#
define
TUTORIAL_NAME
"
/examples/tutorial/tutorial01.das
"
typedef
das_map<string,
float
> ExprVars;
//
variable lookup
class
ExprCalc
{
public:
ExprCalc
(
const
string & expr,
const
ExprVars & vars );
float
compute
(ExprVars & vars);
protected:
ContextPtr ctx;
//
context
SimFunction * fni =
nullptr
;
//
'test' function
string text;
//
program text (for reference purposes)
das_map<string,
float
*> variables;
//
pointers to variables in context global data
};
float
ExprCalc::compute
(ExprVars & vars) {
if
( fni==
nullptr
) {
//
if no function - no result
return
0
.
0f
;
}
for
(
auto
& va : vars ) {
//
set variables
auto
it = variables.
find
(va.
first
);
if
( it != variables.
end
() ) {
*(it->
second
) = va.
second
;
}
}
auto
res = ctx->
evalWithCatch
(fni,
nullptr
);
//
eval the expression
if
(
auto
ex = ctx->
getException
() ) {
//
if exception - report it
TextPrinter tout;
tout <<
"
with exception
"
<< ex <<
"
\n
"
;
return
0
.
0f
;
}
return
cast<
float
>::
to
(res);
//
cast result to float
}
ExprCalc::ExprCalc
(
const
string & expr,
const
ExprVars & vars ) {
//
build expression program
TextWriter ss;
ss <<
"
require math
\n
"
;
for
(
auto
& va : vars ) {
ss <<
"
var
"
<< va.
first
<<
"
= 0.0f
\n
"
;
//
make each variable float, set to 0.0f by default
}
ss <<
"
[export]
\n
"
<<
"
def test
\n
"
<<
"
\t
return
"
<< expr <<
"
\n
"
<<
"
\n\n
"
;
text = ss.
str
();
//
make file access, introduce string as if it was a file
TextPrinter tout;
auto
fAccess
= make_smart<FsFileAccess>();
auto
fileInfo = make_unique<TextFileInfo>(text.
c_str
(),
uint32_t
(text.
length
()),
false
);
fAccess
->
setFileInfo
(
"
dummy.das
"
,
das::move
(fileInfo));
//
compile script
ModuleGroup dummyLibGroup;
auto
program =
compileDaScript
(
"
dummy.das
"
,
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
;
}
//
create context
ctx = make_shared<Context>(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
;
}
//
call context function
fni = ctx->
findFunction
(
"
test
"
);
//
variables
for
(
auto
& va : vars ) {
auto
idx = ctx->
findVariable
(va.
first
.
c_str
());
//
find index of the variable
assert
(idx != -
1
);
variables[va.
first
] = (
float
*) ctx->
getVariable
(idx);
//
get its pointer in context
}
}
int
main
(
int
,
char
* [] ) {
//
request all da-script built in modules
NEED_ALL_DEFAULT_MODULES
;
//
Initialize modules
Module::Initialize
();
//
make calculator 'program'
ExprVars variables;
variables[
"
a
"
] =
1
.
0f
;
variables[
"
b
"
] =
2
.
0f
;
variables[
"
c
"
] =
3
.
0f
;
ExprCalc
calc
(
"
a*b+c
"
, variables);
//
verify initial values
auto
res = calc.
compute
(variables);
assert
(res==
5
.
0f
);
//
verify for the range of values
for
(
float
a=
1
.
0f
; a<
4
.
0f
; a++ ) {
variables[
"
a
"
] = a;
for
(
float
b=
1
.
0f
; b<
5
.
0f
; b++ ) {
variables[
"
b
"
] = b;
for
(
float
c=
1
.
0f
; c<
6
.
0f
; c++ ) {
variables[
"
c
"
] = c;
float
rref = a * b + c;
float
reval = calc.
compute
(variables);
assert
(rref==reval);
}
}
}
//
shut-down daScript, free all memory
Module::Shutdown
();
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL