353: Add support argv parameter of SPy main function#449
353: Add support argv parameter of SPy main function#449kanin-kearpimy wants to merge 4 commits intospylang:mainfrom
argv parameter of SPy main function#449Conversation
cd8d6be to
c83e239
Compare
antocuni
left a comment
There was a problem hiding this comment.
thanks @kanin-kearpimy .
Congrats to find a way to solve the list[str] bootstrapping problem, I didn't think about it and indeed it's annoying.
I think we can solve it in a much easier way by just emitting the code directly in the generated C instead of trying to have it in a separate file.
See me comment below
spy/backend/c/cmodwriter.py
Outdated
| returns_i32 = w_main.w_functype.w_restype == B.w_i32 | ||
|
|
||
| if needs_argv: | ||
| self.tbh_includes.wl('#include "spy/list.h"') |
There was a problem hiding this comment.
here, instead of including spy/list.h, I'd just copy&paste the code for wrapping argv.
Something like this:
self.tbc.wb("""
#define spy_list_str spy__list$list__builtins$str$_ListImpl
#define spy_list_str_new spy__list$list__builtins$str$_ListImpl$__new__
#define spy_list_str_push spy__list$list__builtins$str$_ListImpl$_push
spy_list_str
spy_wrap_argv(int argc, const char *argv[]) {
...
}
""")
it's not ideal, but it's just a very short amount of code and it saves lots of complications
There was a problem hiding this comment.
Yeah,
At first I thought of this as well. Let me resolve it this way.
There was a problem hiding this comment.
Okay, refactored it.
542f549 to
09f3c14
Compare
antocuni
left a comment
There was a problem hiding this comment.
there seem to be several leftovers from the previous attempt, but overall looks good 👍
| }} | ||
|
|
||
| needs_argv = len(w_main.w_functype.params) == 1 | ||
| returns_i32 = w_main.w_functype.w_restype == B.w_i32 |
There was a problem hiding this comment.
why do you need this instead of calling vm.typecheck_main as it was before?
| returns_i32 = w_main.w_functype.w_restype == B.w_i32 | ||
|
|
||
| if needs_argv: | ||
| self.tbh.wb(""" |
There was a problem hiding this comment.
I think this should go to the tbc, not tbh
| size_t size_str = strlen(argv[i]); | ||
| spy_Str *allo = spy_str_alloc(size_str); |
There was a problem hiding this comment.
| size_t size_str = strlen(argv[i]); | |
| spy_Str *allo = spy_str_alloc(size_str); | |
| size_t length = strlen(argv[i]); | |
| spy_Str *s = spy_str_alloc(length); |
I think it's better to call it length for consistency with the field in spy_Str.
Also, allo was a very werid name, better to use s (you need to fix it also below)
| """) | ||
|
|
||
| if needs_argv and returns_i32: | ||
| execution_code = f""" |
There was a problem hiding this comment.
| execution_code = f""" | |
| main_src = f""" |
a bit of nitpick, but I think this is clearer. After all, it's the "source of the main function".
| @@ -0,0 +1,17 @@ | |||
| #ifndef SPY_LIST_H | |||
There was a problem hiding this comment.
I think this file is no longer needed, is it?
| @@ -0,0 +1,20 @@ | |||
| #include "spy/list.h" | |||
| main_c = self.tmpdir.join("src", "return_exit_code_src.c") | ||
| assert main_c.exists() | ||
| csrc = main_c.read() | ||
| assert "return 99;" in csrc |
There was a problem hiding this comment.
I think I have already deleted/refactored these tests during the review of #419, see e.g. this commit :)
3ebdaa8
There are few code style problems:
- there is no need for a "cwrite" test for every case. The existing
test_cwritechecks that the option work, but we shouldn't test cwrite for every single combination of options.
Also, the rest of the file follows different naming convention, e.g. it always use src = """...""" for the source to compile. Please follow them for consistency.
| csrc = main_c.read() | ||
| assert "return 99;" in csrc | ||
|
|
||
| def test_nested_functions_exit_code_cwrite(self): |
There was a problem hiding this comment.
this was also deleted by 3ebdaa8 . It seems to test that you can call a function, but that's already tested elsewhere.
Please look at that commit and refactor/redelete all the tests which I have already fixed :)
|
|
||
| import py.path | ||
|
|
||
| import spy.libspy |
|
@kanin-kearpimy also, I just merged #445 which (rightly) adds the executable name as the first argv. |
Issue: #353
Hi everyone,
this is implementation of backend SPy argv parameter for main function.
I separated argument wrapper to
list.candlist.hthen calling it incmodwriter.However, there is a tricky part happening
if any spy files need argument in main like below
It will include
list.hto such compiled C file to usespy_wrap_argv. However, such implementation is inlist.cwhich requires to compile before such current working file got compile.Rough correct pipeline.
Our current pipeline does compile all standard libraries (e.g. _list.spy) at once. It's challenging to modify current pipeline to receive customized on-the-fly
.cto compile as above pipeline. Also, to compilelist.cwith alllibspyis challenging becauselist.cneed_list.cto compiled first.So, now I juggled by copy content of
list.cand compile it with [any current working file withmain(argv: list[str])] instead.Well, I'm not sure I explained it clearly. Please let me know if you need elaboration.