Skip to content

Implement sys.exit() #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions integration_tests/exit_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from sys import exit

def main0():
print("Before")
exit(0)
print("After")

main0()
1 change: 1 addition & 0 deletions integration_tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys

tests = [
"exit_01.py",
"expr_01.py",
"expr_02.py",
"expr_03.py",
Expand Down
12 changes: 10 additions & 2 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1763,8 +1763,16 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
args.p, args.size());
return;

} else if (call_name == "exit" || call_name == "quit") {
ASR::expr_t *code = nullptr;
} else if (call_name == "quit") {
ASR::expr_t *code;
if (args.size() == 0) {
code = nullptr;
} else if (args.size() == 1) {
code = args[0];
} else {
throw SemanticError("The function quit() requires 0 or 1 arguments",
x.base.base.loc);
}
tmp = ASR::make_Stop_t(al, x.base.base.loc, code);
return;
}
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/sys.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from ltypes import i32

def exit(error_code: i32):
"""
Exits the program with an error code `error_code`.
"""

print("Not implemented yet.")
quit(error_code)
4 changes: 3 additions & 1 deletion tests/loop2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from sys import exit

def test_for():
i: i32
for i in range(0, 10):
Expand All @@ -7,4 +9,4 @@ def test_for():
break
if i == 3:
quit()
exit()
exit(0)
4 changes: 2 additions & 2 deletions tests/reference/asr-loop2-e874469.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-loop2-e874469",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/loop2.py",
"infile_hash": "8bc96fe7a628ac6571893b80a53dc45785f08e770c335319ad3e2cbc",
"infile_hash": "0f1af9370fbae5de0ed9e612e933d7745598fc9c74b036496f4ae8ca",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-loop2-e874469.stdout",
"stdout_hash": "5c7a6323cbd8137a7b82feb7e6d4f0dfb976c28229af3e2244a2597c",
"stdout_hash": "d1dc630006e0a7a21748d0a8bd26f3c99b277d5c208ed6d8077ac9e9",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-loop2-e874469.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) main_program [] []), test_for: (Subroutine (SymbolTable 2 {i: (Variable 2 i Local () () Default (Integer 4 []) Source Public Required .false.)}) test_for [] [(DoLoop ((Var 2 i) (ConstantInteger 0 (Integer 4 [])) (BinOp (ConstantInteger 10 (Integer 4 [])) Sub (ConstantInteger 1 (Integer 4 [])) (Integer 4 []) (ConstantInteger 9 (Integer 4 [])) ()) (ConstantInteger 1 (Integer 4 []))) [(If (Compare (Var 2 i) Eq (ConstantInteger 0 (Integer 4 [])) (Logical 4 []) () ()) [(Cycle)] []) (If (Compare (Var 2 i) Gt (ConstantInteger 5 (Integer 4 [])) (Logical 4 []) () ()) [(Exit)] []) (If (Compare (Var 2 i) Eq (ConstantInteger 3 (Integer 4 [])) (Logical 4 []) () ()) [(Stop ())] [])]) (Stop ())] Source Public Implementation () .false. .false.)}) [])
(TranslationUnit (SymbolTable 1 {exit: (ExternalSymbol 1 exit 3 exit sys [] exit Public), main_program: (Program (SymbolTable 6 {}) main_program [] []), sys: (Module (SymbolTable 3 {exit: (Subroutine (SymbolTable 4 {error_code: (Variable 4 error_code In () () Default (Integer 4 []) Source Public Required .false.)}) exit [(Var 4 error_code)] [(Stop (Var 4 error_code))] Source Public Implementation () .false. .false.)}) sys [] .false.), test_for: (Subroutine (SymbolTable 5 {i: (Variable 5 i Local () () Default (Integer 4 []) Source Public Required .false.)}) test_for [] [(DoLoop ((Var 5 i) (ConstantInteger 0 (Integer 4 [])) (BinOp (ConstantInteger 10 (Integer 4 [])) Sub (ConstantInteger 1 (Integer 4 [])) (Integer 4 []) (ConstantInteger 9 (Integer 4 [])) ()) (ConstantInteger 1 (Integer 4 []))) [(If (Compare (Var 5 i) Eq (ConstantInteger 0 (Integer 4 [])) (Logical 4 []) () ()) [(Cycle)] []) (If (Compare (Var 5 i) Gt (ConstantInteger 5 (Integer 4 [])) (Logical 4 []) () ()) [(Exit)] []) (If (Compare (Var 5 i) Eq (ConstantInteger 3 (Integer 4 [])) (Logical 4 []) () ()) [(Stop ())] [])]) (SubroutineCall 1 exit () [(ConstantInteger 0 (Integer 4 []))] ())] Source Public Implementation () .false. .false.)}) [])
4 changes: 2 additions & 2 deletions tests/reference/ast-loop2-63bf329.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "ast-loop2-63bf329",
"cmd": "lpython --show-ast --no-color {infile} -o {outfile}",
"infile": "tests/loop2.py",
"infile_hash": "8bc96fe7a628ac6571893b80a53dc45785f08e770c335319ad3e2cbc",
"infile_hash": "0f1af9370fbae5de0ed9e612e933d7745598fc9c74b036496f4ae8ca",
"outfile": null,
"outfile_hash": null,
"stdout": "ast-loop2-63bf329.stdout",
"stdout_hash": "087c70fedf810add24d5c170994e52aee61ff6e6e91f0dabfe4f49ce",
"stdout_hash": "a5cd7c1a9f0bdb94b4ae8a8ec509111aede5651ea109408828ce40c8",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/ast-loop2-63bf329.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(Module [(FunctionDef test_for ([] [] [] [] [] [] []) [(AnnAssign (Name i Store) (Name i32 Load) () 1) (For (Name i Store) (Call (Name range Load) [(ConstantInt 0 ()) (ConstantInt 10 ())] []) [(If (Compare (Name i Load) Eq [(ConstantInt 0 ())]) [(Continue)] []) (If (Compare (Name i Load) Gt [(ConstantInt 5 ())]) [(Break)] []) (If (Compare (Name i Load) Eq [(ConstantInt 3 ())]) [(Expr (Call (Name quit Load) [] []))] [])] [] ()) (Expr (Call (Name exit Load) [] []))] [] () ())] [])
(Module [(ImportFrom sys [(exit ())] 0) (FunctionDef test_for ([] [] [] [] [] [] []) [(AnnAssign (Name i Store) (Name i32 Load) () 1) (For (Name i Store) (Call (Name range Load) [(ConstantInt 0 ()) (ConstantInt 10 ())] []) [(If (Compare (Name i Load) Eq [(ConstantInt 0 ())]) [(Continue)] []) (If (Compare (Name i Load) Gt [(ConstantInt 5 ())]) [(Break)] []) (If (Compare (Name i Load) Eq [(ConstantInt 3 ())]) [(Expr (Call (Name quit Load) [] []))] [])] [] ()) (Expr (Call (Name exit Load) [(ConstantInt 0 ())] []))] [] () ())] [])
4 changes: 2 additions & 2 deletions tests/reference/cpp-loop2-0686fc4.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "cpp-loop2-0686fc4",
"cmd": "lpython --no-color --show-cpp {infile}",
"infile": "tests/loop2.py",
"infile_hash": "8bc96fe7a628ac6571893b80a53dc45785f08e770c335319ad3e2cbc",
"infile_hash": "0f1af9370fbae5de0ed9e612e933d7745598fc9c74b036496f4ae8ca",
"outfile": null,
"outfile_hash": null,
"stdout": "cpp-loop2-0686fc4.stdout",
"stdout_hash": "1b3ba76abf9c0f99b17c6b8293a0b73a0a6765c7716654832212aa9e",
"stdout_hash": "eef4f1ff5b5b8880a2e873cd82808099e1f3612117a10667599f5d62",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
5 changes: 5 additions & 0 deletions tests/reference/cpp-loop2-0686fc4.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ void test_for()
exit(0);
}

void exit(int error_code)
{
exit(0);
}

namespace {

void main2() {
Expand Down