Skip to content

Commit

Permalink
Add missed ord(), Fix small bug in reversing loop (lcompilers#767)
Browse files Browse the repository at this point in the history
* Make `str()` work for non-constant values
  • Loading branch information
AbdelrahmanKhaledd committed Jul 18, 2022
1 parent 7aedd1a commit 2677ef1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
12 changes: 12 additions & 0 deletions integration_tests/test_builtin_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,16 @@ def test_str_int():
assert str(False) == "False"
assert str(True) == "True"

def str_conv_for_variables():
x: i32
x = 123
assert "123" == str(x)
x = 12345
assert "12345" == str(x)
x = -12
assert "-12" == str(x)
x = -121212
assert "-121212" == str(x)

test_str_int()
str_conv_for_variables()
4 changes: 2 additions & 2 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ def str(x: i32) -> str:
rev_result_len: i32
rev_result_len = 0
while x > 0:
rev_result += chr(x - _lpython_floordiv(x, 10)*10)
rev_result += chr(48 + (x - _lpython_floordiv(x, 10)*10))
rev_result_len += 1
x = _lpython_floordiv(x, 10)
pos: i32
for pos in range(rev_result_len - 1, -1, -1):
for pos in range(rev_result_len - 1, 1, -1):
result += rev_result[pos]
return result

Expand Down
4 changes: 2 additions & 2 deletions tests/reference/asr-test_builtin_str-580e920.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-test_builtin_str-580e920",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/test_builtin_str.py",
"infile_hash": "82ac5ac239edda2c3078f337a380c4c70a472fb3cdf891b0d877f54a",
"infile_hash": "9b5d5eaaa6856df8ac20b9f34b434749709c46cdcb3dbd313fd9ee9a",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-test_builtin_str-580e920.stdout",
"stdout_hash": "f56760e251553af16e6ac7e6aaa83bf9b3d68562497e60d65debc698",
"stdout_hash": "409450a916b0725c49a3da948ac3642649ec6856c9e495706f84ad5a",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-test_builtin_str-580e920.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Subroutine (SymbolTable 80 {}) _lpython_main_program [] [(SubroutineCall 1 test_str_int () [] ())] Source Public Implementation () .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 79 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())]), str@__lpython_overloaded_0__str: (ExternalSymbol 1 str@__lpython_overloaded_0__str 4 __lpython_overloaded_0__str lpython_builtin [] __lpython_overloaded_0__str Public), str@__lpython_overloaded_1__str: (ExternalSymbol 1 str@__lpython_overloaded_1__str 4 __lpython_overloaded_1__str lpython_builtin [] __lpython_overloaded_1__str Public), str@__lpython_overloaded_2__str: (ExternalSymbol 1 str@__lpython_overloaded_2__str 4 __lpython_overloaded_2__str lpython_builtin [] __lpython_overloaded_2__str Public), str@__lpython_overloaded_3__str: (ExternalSymbol 1 str@__lpython_overloaded_3__str 4 __lpython_overloaded_3__str lpython_builtin [] __lpython_overloaded_3__str Public), test_str_int: (Subroutine (SymbolTable 2 {s: (Variable 2 s Local () () Default (Character 1 -2 () []) Source Public Required .false.), str: (ExternalSymbol 2 str 4 str lpython_builtin [] str Private)}) test_str_int [] [(= (Var 2 s) (FunctionCall 1 str@__lpython_overloaded_3__str 2 str [((IntegerConstant 356 (Integer 4 [])))] (Character 1 -2 () []) (StringConstant "356" (Character 1 1 () [])) ()) ()) (Assert (StringCompare (Var 2 s) Eq (StringConstant "356" (Character 1 3 () [])) (Logical 4 []) ()) ()) (= (Var 2 s) (FunctionCall 1 str@__lpython_overloaded_3__str 2 str [((IntegerUnaryMinus (IntegerConstant 567 (Integer 4 [])) (Integer 4 []) (IntegerConstant -567 (Integer 4 []))))] (Character 1 -2 () []) (StringConstant "-567" (Character 1 1 () [])) ()) ()) (Assert (StringCompare (Var 2 s) Eq (StringConstant "-567" (Character 1 4 () [])) (Logical 4 []) ()) ()) (Assert (StringCompare (FunctionCall 1 str@__lpython_overloaded_3__str 2 str [((IntegerConstant 4 (Integer 4 [])))] (Character 1 -2 () []) (StringConstant "4" (Character 1 1 () [])) ()) Eq (StringConstant "4" (Character 1 1 () [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (Assert (StringCompare (FunctionCall 1 str@__lpython_overloaded_3__str 2 str [((IntegerUnaryMinus (IntegerConstant 5 (Integer 4 [])) (Integer 4 []) (IntegerConstant -5 (Integer 4 []))))] (Character 1 -2 () []) (StringConstant "-5" (Character 1 1 () [])) ()) Eq (StringConstant "-5" (Character 1 2 () [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (Assert (StringCompare (FunctionCall 1 str@__lpython_overloaded_0__str 2 str [] (Character 1 -2 () []) (StringConstant "" (Character 1 1 () [])) ()) Eq (StringConstant "" (Character 1 0 () [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (Assert (StringCompare (FunctionCall 1 str@__lpython_overloaded_1__str 2 str [((StringConstant "1234" (Character 1 4 () [])))] (Character 1 -2 () []) (StringConstant "1234" (Character 1 1 () [])) ()) Eq (StringConstant "1234" (Character 1 4 () [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (Assert (StringCompare (FunctionCall 1 str@__lpython_overloaded_2__str 2 str [((LogicalConstant .false. (Logical 4 [])))] (Character 1 -2 () []) (StringConstant "False" (Character 1 1 () [])) ()) Eq (StringConstant "False" (Character 1 5 () [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (Assert (StringCompare (FunctionCall 1 str@__lpython_overloaded_2__str 2 str [((LogicalConstant .true. (Logical 4 [])))] (Character 1 -2 () []) (StringConstant "True" (Character 1 1 () [])) ()) Eq (StringConstant "True" (Character 1 4 () [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ())] Source Public Implementation () .false. .false.)}) [])
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Subroutine (SymbolTable 81 {}) _lpython_main_program [] [(SubroutineCall 1 test_str_int () [] ()) (SubroutineCall 1 str_conv_for_variables () [] ())] Source Public Implementation () .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 80 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())]), str@__lpython_overloaded_0__str: (ExternalSymbol 1 str@__lpython_overloaded_0__str 5 __lpython_overloaded_0__str lpython_builtin [] __lpython_overloaded_0__str Public), str@__lpython_overloaded_1__str: (ExternalSymbol 1 str@__lpython_overloaded_1__str 5 __lpython_overloaded_1__str lpython_builtin [] __lpython_overloaded_1__str Public), str@__lpython_overloaded_2__str: (ExternalSymbol 1 str@__lpython_overloaded_2__str 5 __lpython_overloaded_2__str lpython_builtin [] __lpython_overloaded_2__str Public), str@__lpython_overloaded_3__str: (ExternalSymbol 1 str@__lpython_overloaded_3__str 5 __lpython_overloaded_3__str lpython_builtin [] __lpython_overloaded_3__str Public), str_conv_for_variables: (Subroutine (SymbolTable 3 {str: (ExternalSymbol 3 str 5 str lpython_builtin [] str Private), x: (Variable 3 x Local () () Default (Integer 4 []) Source Public Required .false.)}) str_conv_for_variables [] [(= (Var 3 x) (IntegerConstant 123 (Integer 4 [])) ()) (Assert (StringCompare (StringConstant "123" (Character 1 3 () [])) Eq (FunctionCall 1 str@__lpython_overloaded_3__str 3 str [((Var 3 x))] (Character 1 -2 () []) () ()) (Logical 4 []) ()) ()) (= (Var 3 x) (IntegerConstant 12345 (Integer 4 [])) ()) (Assert (StringCompare (StringConstant "12345" (Character 1 5 () [])) Eq (FunctionCall 1 str@__lpython_overloaded_3__str 3 str [((Var 3 x))] (Character 1 -2 () []) () ()) (Logical 4 []) ()) ()) (= (Var 3 x) (IntegerUnaryMinus (IntegerConstant 12 (Integer 4 [])) (Integer 4 []) (IntegerConstant -12 (Integer 4 []))) ()) (Assert (StringCompare (StringConstant "-12" (Character 1 3 () [])) Eq (FunctionCall 1 str@__lpython_overloaded_3__str 3 str [((Var 3 x))] (Character 1 -2 () []) () ()) (Logical 4 []) ()) ()) (= (Var 3 x) (IntegerUnaryMinus (IntegerConstant 121212 (Integer 4 [])) (Integer 4 []) (IntegerConstant -121212 (Integer 4 []))) ()) (Assert (StringCompare (StringConstant "-121212" (Character 1 7 () [])) Eq (FunctionCall 1 str@__lpython_overloaded_3__str 3 str [((Var 3 x))] (Character 1 -2 () []) () ()) (Logical 4 []) ()) ())] Source Public Implementation () .false. .false.), test_str_int: (Subroutine (SymbolTable 2 {s: (Variable 2 s Local () () Default (Character 1 -2 () []) Source Public Required .false.), str: (ExternalSymbol 2 str 5 str lpython_builtin [] str Private)}) test_str_int [] [(= (Var 2 s) (FunctionCall 1 str@__lpython_overloaded_3__str 2 str [((IntegerConstant 356 (Integer 4 [])))] (Character 1 -2 () []) (StringConstant "356" (Character 1 1 () [])) ()) ()) (Assert (StringCompare (Var 2 s) Eq (StringConstant "356" (Character 1 3 () [])) (Logical 4 []) ()) ()) (= (Var 2 s) (FunctionCall 1 str@__lpython_overloaded_3__str 2 str [((IntegerUnaryMinus (IntegerConstant 567 (Integer 4 [])) (Integer 4 []) (IntegerConstant -567 (Integer 4 []))))] (Character 1 -2 () []) (StringConstant "-567" (Character 1 1 () [])) ()) ()) (Assert (StringCompare (Var 2 s) Eq (StringConstant "-567" (Character 1 4 () [])) (Logical 4 []) ()) ()) (Assert (StringCompare (FunctionCall 1 str@__lpython_overloaded_3__str 2 str [((IntegerConstant 4 (Integer 4 [])))] (Character 1 -2 () []) (StringConstant "4" (Character 1 1 () [])) ()) Eq (StringConstant "4" (Character 1 1 () [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (Assert (StringCompare (FunctionCall 1 str@__lpython_overloaded_3__str 2 str [((IntegerUnaryMinus (IntegerConstant 5 (Integer 4 [])) (Integer 4 []) (IntegerConstant -5 (Integer 4 []))))] (Character 1 -2 () []) (StringConstant "-5" (Character 1 1 () [])) ()) Eq (StringConstant "-5" (Character 1 2 () [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (Assert (StringCompare (FunctionCall 1 str@__lpython_overloaded_0__str 2 str [] (Character 1 -2 () []) (StringConstant "" (Character 1 1 () [])) ()) Eq (StringConstant "" (Character 1 0 () [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (Assert (StringCompare (FunctionCall 1 str@__lpython_overloaded_1__str 2 str [((StringConstant "1234" (Character 1 4 () [])))] (Character 1 -2 () []) (StringConstant "1234" (Character 1 1 () [])) ()) Eq (StringConstant "1234" (Character 1 4 () [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (Assert (StringCompare (FunctionCall 1 str@__lpython_overloaded_2__str 2 str [((LogicalConstant .false. (Logical 4 [])))] (Character 1 -2 () []) (StringConstant "False" (Character 1 1 () [])) ()) Eq (StringConstant "False" (Character 1 5 () [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ()) (Assert (StringCompare (FunctionCall 1 str@__lpython_overloaded_2__str 2 str [((LogicalConstant .true. (Logical 4 [])))] (Character 1 -2 () []) (StringConstant "True" (Character 1 1 () [])) ()) Eq (StringConstant "True" (Character 1 4 () [])) (Logical 4 []) (LogicalConstant .true. (Logical 4 []))) ())] Source Public Implementation () .false. .false.)}) [])

0 comments on commit 2677ef1

Please sign in to comment.