Skip to content

Commit

Permalink
Use global list[str] type for argv,
Browse files Browse the repository at this point in the history
- Rename argv to _argv
- Initialize the argv values in C backend using `_lpython_set_argv` function call
  • Loading branch information
Thirumalai-Shaktivel committed Apr 23, 2023
1 parent 5fe474b commit e611337
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ R"(
}
src = contains
+ "int main(int argc, char* argv[])\n{\n"
+ indent1 + "_lpython_set_argv(argc, argv);\n"
+ decl + body
+ indent1 + "return 0;\n}\n";
indentation_level -= 2;
Expand Down
14 changes: 7 additions & 7 deletions src/libasr/runtime/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1393,23 +1393,23 @@ LFORTRAN_API int32_t _lfortran_all(bool *mask, int32_t n) {
}

// Command line arguments
int32_t argc;
char **argv;
int32_t _argc;
char **_argv;

LFORTRAN_API void _lpython_set_argv(int32_t argc_1, char *argv_1[]) {
argv = malloc(argc_1 * sizeof(char *));
_argv = malloc(argc_1 * sizeof(char *));
for (size_t i = 0; i < argc_1; i++) {
argv[i] = strdup(argv_1[i]);
_argv[i] = strdup(argv_1[i]);
}
argc = argc_1;
_argc = argc_1;
}

LFORTRAN_API int32_t _lpython_get_argc() {
return argc;
return _argc;
}

LFORTRAN_API char *_lpython_get_argv(int32_t index) {
return argv[index];
return _argv[index];
}

// << Command line arguments << ------------------------------------------------
Expand Down
20 changes: 12 additions & 8 deletions src/runtime/sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ def exit(error_code: i32):

quit(error_code)

# >----------------------------------- argv ----------------------------------->
@ccall
def _lpython_get_argc() -> i32:
pass

@ccall
def _lpython_get_argv(index: i32) -> str:
pass

def _lpython_argv() -> list[str]:
# TODO: use argv as a global `list[str]` variable
"""
Gets the list of command line arguments
"""
argc: i32 = _lpython_get_argc()
argv: list[str]
argv: list[str] = []
i: i32
for i in range(argc):
argv.append(_lpython_get_argv(i))
return argv

@ccall
def _lpython_get_argc() -> i32:
pass
argv: list[str] = _lpython_argv()

@ccall
def _lpython_get_argv(index: i32) -> str:
pass
# <----------------------------------- argv -----------------------------------<

0 comments on commit e611337

Please sign in to comment.