-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcitrine.c
170 lines (162 loc) · 5.4 KB
/
citrine.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "citrine.h"
int ctr_argc;
char** ctr_argv;
char* ctr_mode_input_file;
char* ctr_mode_dict_file;
char* ctr_mode_hfile1;
char* ctr_mode_hfile2;
char ctr_flag_sandbox;
ctr_size ctr_clex_keyword_eol_len;
ctr_size ctr_clex_keyword_num_sep_dec_len;
ctr_size ctr_clex_keyword_num_sep_tho_len;
ctr_size ctr_clex_keyword_assignment_len;
ctr_size ctr_clex_keyword_return_len;
ctr_size ctr_clex_keyword_chain_len;
uint16_t ctr_sandbox_steps = 0;
/**
* CommandLine Display Welcome Message
* Displays a Welcome message, copyright information,
* version information and usage.
*/
void ctr_cli_welcome() {
printf( CTR_MSG_WELCOME );
printf( CTR_MSG_COPYRIGHT );
printf( CTR_VERSION );
printf("\n");
}
/**
* CommandLine Read Arguments
* Parses command line arguments and sets global settings accordingly.
*/
int ctr_cli_read_args(int argc, char* argv[]) {
int mode = 0;
if (argc == 1) {
ctr_cli_welcome();
exit(0);
}
if (strncmp(argv[1],"-x", 2)==0) {
mode = 3;
ctr_mode_input_file = (char*) ctr_heap_allocate_tracked( sizeof( char ) * 255 );
strncpy(ctr_mode_input_file, argv[2], 254);
return mode;
}
if (strncmp(argv[1],"-g", 2)==0) {
if (argc < 4) {
ctr_print_error( CTR_MSG_USAGE_G, 1 );
}
ctr_mode_hfile1 = (char*) ctr_heap_allocate_tracked( sizeof( char ) * 255 );
strncpy(ctr_mode_hfile1, argv[2], 254);
ctr_mode_hfile2 = (char*) ctr_heap_allocate_tracked( sizeof( char ) * 255 );
strncpy(ctr_mode_hfile2, argv[3], 254);
mode = 2;
}
if (strncmp(argv[1],"-t", 2)==0) {
if (argc < 4) {
ctr_print_error( CTR_MSG_USAGE_T, 1 );
}
ctr_mode_dict_file = (char*) ctr_heap_allocate_tracked( sizeof( char ) * 255 );
strncpy(ctr_mode_dict_file, argv[2], 254);
ctr_mode_input_file = (char*) ctr_heap_allocate_tracked( sizeof( char ) * 255 );
strncpy(ctr_mode_input_file, argv[3], 254);
mode = 1;
} else {
if (strncmp(argv[1],"-s", 2)==0) {
ctr_flag_sandbox = 1;
ctr_mode_input_file = (char*) ctr_heap_allocate_tracked( sizeof( char ) * 255 );
strncpy(ctr_mode_input_file, argv[2], 254);
} else {
ctr_mode_input_file = (char*) ctr_heap_allocate_tracked( sizeof( char ) * 255 );
strncpy(ctr_mode_input_file, argv[1], 254);
}
}
return mode;
}
/**
* Inits the Citrine environment.
*/
int ctr_init() {
ctr_in_message = 0;
ctr_callstack_index = 0;
ctr_sandbox_steps = 0;
ctr_source_map_head = NULL;
ctr_source_mapping = 0;
CtrStdFlow = NULL;
ctr_source_mapping = 1;
ctr_deserialize_mode = 0;
ctr_clex_keyword_me_icon = CTR_DICT_ME_ICON;
ctr_clex_keyword_my_icon = CTR_DICT_MY_ICON;
ctr_clex_keyword_var_icon = CTR_DICT_VAR_ICON;
ctr_clex_keyword_my_icon_len = strlen( ctr_clex_keyword_my_icon );
ctr_clex_keyword_var_icon_len = strlen( ctr_clex_keyword_var_icon );
ctr_clex_keyword_eol_len = strlen( CTR_DICT_END_OF_LINE );
ctr_clex_keyword_chain_len = strlen( CTR_DICT_MESSAGE_CHAIN );
ctr_clex_keyword_num_sep_dec_len = strlen( CTR_DICT_NUM_DEC_SEP );
ctr_clex_keyword_num_sep_tho_len = strlen( CTR_DICT_NUM_THO_SEP );
ctr_clex_keyword_qo_len = strlen( CTR_DICT_QUOT_OPEN );
ctr_clex_keyword_qc_len = strlen( CTR_DICT_QUOT_CLOSE );
ctr_clex_keyword_assignment_len = strlen( CTR_DICT_ASSIGN );
ctr_clex_keyword_return_len = strlen( CTR_DICT_RETURN );
ctr_clex_param_prefix_char = CTR_DICT_PARAMETER_PREFIX[0];
ctr_gc_memlimit = 10 * 1000000; /* Default memory limit: 10MB */
ctr_gc_mode = 1; /* Default GC mode: regular GC, no pool. */
return 0;
}
/**
* Citrine Application Main Start
* Bootstraps the Citrine Application.
*
*/
int main(int argc, char* argv[]) {
char* prg;
char ctr_pool_share;
ctr_tnode* program;
uint64_t program_text_size = 0;
ctr_argc = argc;
ctr_argv = argv;
ctr_init();
ctr_pool_share = 2; /* Default: pool ratio = 50% */
//Command line options
int mode = ctr_cli_read_args(argc, argv);
if (mode == 1) {
prg = ctr_internal_readf(ctr_mode_input_file, &program_text_size);
ctr_translate_program(prg, ctr_mode_input_file);
exit(0);
}
if (mode == 2) {
ctr_translate_generate_dicts(ctr_mode_hfile1, ctr_mode_hfile2);
exit(0);
}
prg = ctr_internal_readf(ctr_mode_input_file, &program_text_size);
//Advanced parameters - environment
char* env_param_citrine_memory_limit_mb = getenv("CITRINE_MEMORY_LIMIT_MB"); // - memory limit in MB
char* env_param_citrine_memory_mode = getenv("CITRINE_MEMORY_MODE"); // - GC mode
char* env_param_citrine_memory_pool_share = getenv("CITRINE_MEMORY_POOL_SHARE"); // - how much % for the pool
if (env_param_citrine_memory_limit_mb) ctr_gc_memlimit = atoi(env_param_citrine_memory_limit_mb) * 1000000;
if (env_param_citrine_memory_mode) ctr_gc_mode = atoi(env_param_citrine_memory_mode);
if (env_param_citrine_memory_pool_share) ctr_pool_share = atoi(env_param_citrine_memory_pool_share);
if (ctr_gc_mode & 8) ctr_pool_init(ctr_gc_memlimit/ctr_pool_share);
program = ctr_cparse_parse(prg, ctr_mode_input_file);
if (program == NULL) {
fwrite(CtrStdFlow->value.svalue->value, CtrStdFlow->value.svalue->vlen, 1, stderr);
exit(1);
}
if (mode == 3) {
ctr_internal_export_tree(program);
exit(0);
}
ctr_initialize_world();
ctr_cwlk_run(program);
ctr_gc_sweep(1);
ctr_heap_free( prg );
ctr_heap_free_rest();
//For memory profiling
if ( ctr_gc_alloc != 0 ) {
fprintf( stderr, "[WARNING] Citrine has detected an internal memory leak of: %" PRIu64 " bytes.\n", ctr_gc_alloc );
exit(1);
}
if (CtrStdFlow && CtrStdFlow != CtrStdExit) {
exit(1);
}
exit(0);
return 0;
}