Skip to content

Commit ab32228

Browse files
committed
added --analyze option
1 parent e8a5284 commit ab32228

File tree

4 files changed

+127
-37
lines changed

4 files changed

+127
-37
lines changed

cli/main.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ typedef struct
1616
bool optHelp;
1717
bool optVersion;
1818
bool optEval;
19+
bool optAnalyze;
1920
bool optDump;
2021
bool optCompile;
2122
bool optRun;
@@ -58,6 +59,7 @@ static inline void parse_args(ParsedArgs *parsedArgs, int argc, const char **arg
5859
parsedArgs->optHelp = false;
5960
parsedArgs->optVersion = false;
6061
parsedArgs->optEval = false;
62+
parsedArgs->optAnalyze = false;
6163
parsedArgs->optDump = false;
6264
parsedArgs->optCompile = false;
6365
parsedArgs->optRun = false;
@@ -100,6 +102,11 @@ static inline void parse_option(ParsedArgs *parsedArgs, const char *arg)
100102
parsedArgs->optEval = true;
101103
return;
102104
}
105+
if (option(arg, "-a") || option(arg, "--analyze"))
106+
{
107+
parsedArgs->optAnalyze = true;
108+
return;
109+
}
103110
if (option(arg, "-d") || option(arg, "--dump"))
104111
{
105112
parsedArgs->optDump = true;
@@ -157,6 +164,7 @@ static inline void print_help(const char *cmd)
157164
" -h, --help prints this message\n"
158165
" -v, --version shows version information\n"
159166
" -e, --eval evaluates a string from the terminal\n"
167+
" -a, --analyze analyzes source code\n"
160168
" -d, --dump shows the bytecode\n"
161169
" -c, --compile compiles source code\n"
162170
" -r, --run runs directly from bytecode\n"
@@ -269,7 +277,7 @@ int main(int argc, const char **argv)
269277
fatal_error("no input string");
270278
HkString *file = hk_string_from_chars(-1, "<terminal>");
271279
HkString *source = hk_string_from_chars(-1, input);
272-
HkClosure *cl = hk_compile(file, source);
280+
HkClosure *cl = hk_compile(file, source, HK_COMPILER_FLAG_NONE);
273281
return run_bytecode(cl, &parsedArgs);
274282
}
275283
if (parsedArgs.optRun)
@@ -286,7 +294,8 @@ int main(int argc, const char **argv)
286294
}
287295
HkString *file = hk_string_from_chars(-1, input ? input : "<stdin>");
288296
HkString *source = input ? load_source_from_file(input) : hk_string_from_stream(stdin, '\0');
289-
HkClosure *cl = hk_compile(file, source);
297+
int flags = parsedArgs.optAnalyze ? HK_COMPILER_FLAG_ANALYZE : HK_COMPILER_FLAG_NONE;
298+
HkClosure *cl = hk_compile(file, source, flags);
290299
const char *output = parsedArgs.output;
291300
if (parsedArgs.optDump)
292301
{
@@ -306,5 +315,10 @@ int main(int argc, const char **argv)
306315
hk_closure_free(cl);
307316
return EXIT_SUCCESS;
308317
}
318+
if (parsedArgs.optAnalyze)
319+
{
320+
hk_closure_free(cl);
321+
return EXIT_SUCCESS;
322+
}
309323
return run_bytecode(cl, &parsedArgs);
310324
}

include/hook/compiler.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
#include "callable.h"
1010

11-
HkClosure *hk_compile(HkString *file, HkString *source);
11+
#define HK_COMPILER_FLAG_NONE 0x00
12+
#define HK_COMPILER_FLAG_ANALYZE 0x01
13+
14+
HkClosure *hk_compile(HkString *file, HkString *source, int flags);
1215

1316
#endif // HK_COMPILER_H

0 commit comments

Comments
 (0)