FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/examples/tutorial/tutorial07.c at master · jacereda/daScript · GitHub
jacereda
/
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
/
tutorial07.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
119 lines (110 loc) · 4.24 KB
Breadcrumbs
daScript
/
examples
/
tutorial
/
tutorial07.c
Copy path
File metadata and controls
119 lines (110 loc) · 4.24 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
#include
<stdio.h>
#include
<string.h>
#include
<stddef.h>
#include
<assert.h>
#include
"daScript/daScriptC.h"
#define
TUTORIAL_NAME
"/examples/tutorial/tutorial07.das"
void
tutorial
() {
das_text_writer
*
tout
=
das_text_make_printer
();
// output stream for all compiler messages (stdout. for stringstream use TextWriter)
das_module_group
*
dummyLibGroup
=
das_modulegroup_make
();
// module group for compiled program
das_file_access
*
fAccess
=
das_fileaccess_make_default
();
// default file access
das_context
*
ctx
=
NULL
;
// get path to tutorial
char
fileName
[
256
];
das_get_root
(
fileName
,
sizeof
(
fileName
));
int
charsLeft
=
((
int
)
sizeof
(
fileName
))
-
((
int
)
strlen
(
fileName
))
-
1
;
assert
(
charsLeft
>=
0
&&
"fileName buffer is too small "
);
strncat
(
fileName
,
TUTORIAL_NAME
, (
size_t
)
charsLeft
);
// compile program
das_program
*
program
=
das_program_compile
(
fileName
,
fAccess
,
tout
,
dummyLibGroup
);
int
err_count
=
das_program_err_count
(
program
);
if
(
err_count
) {
// if compilation failed, report errors
das_text_output
(
tout
,
"failed to compile\n"
);
for
(
int
ei
=
0
;
ei
!=
err_count
;
++
ei
) {
das_error
*
error
=
das_program_get_error
(
program
,
ei
);
das_error_output
(
error
,
tout
);
}
goto
shutdown
;
}
// create daScript context
ctx
=
das_context_make
(
das_program_context_stack_size
(
program
));
if
( !
das_program_simulate
(
program
,
ctx
,
tout
) ) {
// if interpretation failed, report errors
das_text_output
(
tout
,
"failed to simulate\n"
);
err_count
=
das_program_err_count
(
program
);
for
(
int
ei
=
0
;
ei
!=
err_count
;
++
ei
) {
das_error
*
error
=
das_program_get_error
(
program
,
ei
);
das_error_output
(
error
,
tout
);
}
goto
shutdown
;
}
// find function 'test' in the context
das_function
*
fnTest
=
das_context_find_function
(
ctx
,
"test"
);
if
( !
fnTest
) {
das_text_output
(
tout
,
"function 'test' not found\n"
);
goto
shutdown
;
}
// evaluate 'test' function in the context
das_context_eval_with_catch
(
ctx
,
fnTest
,
NULL
);
char
*
ex
=
das_context_get_exception
(
ctx
);
if
(
ex
!=
NULL
) {
das_text_output
(
tout
,
"exception: "
);
das_text_output
(
tout
,
ex
);
das_text_output
(
tout
,
"\n"
);
goto
shutdown
;
}
shutdown
:;
das_program_release
(
program
);
das_fileaccess_release
(
fAccess
);
das_modulegroup_release
(
dummyLibGroup
);
das_text_release
(
tout
);
}
// this is test function
vec4f
das_c_func
(
das_context
*
ctx
,
das_node
*
node
,
vec4f
*
args
) {
ctx
;
node
;
printf
(
"from das_c_func(%i)\n"
,
das_argument_int
(
args
[
0
]));
// access argument
return
das_result_void
();
// return result
}
typedef
struct
{
int
foo
;
float
bar
;
}
FooBar
;
typedef
enum
{
OneTwo_one
=
1
,
OneTwo_two
=
2
}
OneTwo
;
das_module
*
register_module_tutorial_07
() {
// create module and library
das_module
*
mod
=
das_module_create
(
"tutorial_07"
);
das_module_group
*
lib
=
das_modulegroup_make
();
// alias
das_module_bind_alias
(
mod
,
lib
,
"intarray"
,
"1<i>A"
);
// enumeration
das_enumeration
*
en
=
das_enumeration_make
(
"OneTwo"
,
"OneTwo"
,
1
);
das_enumeration_add_value
(
en
,
"one"
,
"OneTwo_one"
,
OneTwo_one
);
das_enumeration_add_value
(
en
,
"two"
,
"OneTwo_two"
,
OneTwo_two
);
das_module_bind_enumeration
(
mod
,
en
);
// handled structure
das_structure
*
st
=
das_structure_make
(
lib
,
"FooBar"
,
"FooBar"
,
sizeof
(
FooBar
), _Alignof(
FooBar
));
das_structure_add_field
(
st
,
mod
,
lib
,
"foo"
,
"foo"
, offsetof(
FooBar
,
foo
),
"i"
);
das_structure_add_field
(
st
,
mod
,
lib
,
"bar"
,
"bar"
, offsetof(
FooBar
,
bar
),
"f"
);
das_module_bind_structure
(
mod
,
st
);
// bind das_c_func
das_module_bind_interop_function
(
mod
,
lib
,
&
das_c_func
,
"das_c_func"
,
"das_c_func"
,
SIDEEFFECTS_modifyExternal
,
"v i"
);
// cleanup
das_modulegroup_release
(
lib
);
return
mod
;
}
int
main
(
int
argc
,
char
*
*
argv
) {
argc
;
argv
;
// Initialize all default modules
das_initialize
();
// register modules
register_module_tutorial_07
();
// run the tutorial
tutorial
();
// shut-down daScript, free all memory
das_shutdown
();
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL