From bb7edeb3863775fcd82d3d71dc8671ca4f68ae78 Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Wed, 24 Jan 2024 16:19:03 +0530 Subject: [PATCH 01/10] fixed Raw and Byte string --- jaclang/compiler/absyntree.py | 8 ++++ jaclang/compiler/jac.lark | 2 +- .../compiler/passes/main/pyast_gen_pass.py | 9 ++-- jaclang/tests/fixtures/raw_byte_string.jac | 10 ++++ jaclang/tests/fixtures/semstr.jac | 48 +++++++++---------- jaclang/tests/test_language.py | 10 ++++ 6 files changed, 57 insertions(+), 30 deletions(-) create mode 100644 jaclang/tests/fixtures/raw_byte_string.jac diff --git a/jaclang/compiler/absyntree.py b/jaclang/compiler/absyntree.py index 6c0cc11b9..28099a228 100644 --- a/jaclang/compiler/absyntree.py +++ b/jaclang/compiler/absyntree.py @@ -2168,6 +2168,14 @@ def lit_value(self) -> str: self.value.startswith('"') and self.value.endswith('"') ): ret_str = self.value[1:-1] + elif (self.value.startswith("r'") and self.value.endswith("'")) or ( + self.value.startswith('r"') and self.value.endswith('"') + ): + return self.value[2:-1] + elif (self.value.startswith("b'") and self.value.endswith("'")) or ( + self.value.startswith('b"') and self.value.endswith('"') + ): + return eval(self.value) else: ret_str = self.value ret_str = ret_str.encode().decode("unicode_escape") diff --git a/jaclang/compiler/jac.lark b/jaclang/compiler/jac.lark index a56c31d3c..8be35fe90 100644 --- a/jaclang/compiler/jac.lark +++ b/jaclang/compiler/jac.lark @@ -460,7 +460,7 @@ builtin_type: TYP_TYPE FLOAT: /(\d+(\.\d*)|\.\d+)([eE][+-]?\d+)?/ DOC_STRING: /"""(.|\n|\r)*?"""|'''(.|\n|\r)*?'''/ PYNLINE: /::py::(.|\n|\r)*?::py::/ -STRING: /"[^"\r\n]*"|'[^'\r\n]*'/ +STRING: /[rb]?("[^"\r\n]*"|'[^'\r\n]*')/ BOOL.1: /True|False/ KW_NIN.1: /\bnot\s+in\b/ KW_ISN.1: /\bis\s+not\b/ diff --git a/jaclang/compiler/passes/main/pyast_gen_pass.py b/jaclang/compiler/passes/main/pyast_gen_pass.py index a9a85da22..f6da4de45 100644 --- a/jaclang/compiler/passes/main/pyast_gen_pass.py +++ b/jaclang/compiler/passes/main/pyast_gen_pass.py @@ -1646,16 +1646,15 @@ def get_pieces(str_seq: Sequence) -> list[str | ast3.AST]: combined_multi: list[str | ast3.AST] = [] for item in get_pieces(node.strings): - if ( - combined_multi - and isinstance(item, str) - and isinstance(combined_multi[-1], str) + if combined_multi and ( + (isinstance(item, str) and isinstance(combined_multi[-1], str)) + or (isinstance(item, bytes) and isinstance(combined_multi[-1], bytes)) ): combined_multi[-1] += item else: combined_multi.append(item) for i in range(len(combined_multi)): - if isinstance(combined_multi[i], str): + if isinstance(combined_multi[i], (str, bytes)): combined_multi[i] = self.sync(ast3.Constant(value=combined_multi[i])) if len(combined_multi) > 1 or not isinstance(combined_multi[0], ast3.Constant): node.gen.py_ast = self.sync( diff --git a/jaclang/tests/fixtures/raw_byte_string.jac b/jaclang/tests/fixtures/raw_byte_string.jac new file mode 100644 index 000000000..7fac9581f --- /dev/null +++ b/jaclang/tests/fixtures/raw_byte_string.jac @@ -0,0 +1,10 @@ +with entry{ + + hello_world_bytes = b"\x48\x65\x6C\x6C\x6F\x20\x57\x6F\x72\x6C\x64"; + print(hello_world_bytes); + print(type(hello_world_bytes)); + ret_str = r"Hello\\nWorld"; + print(ret_str); + + +} \ No newline at end of file diff --git a/jaclang/tests/fixtures/semstr.jac b/jaclang/tests/fixtures/semstr.jac index 182e46849..d0f594087 100644 --- a/jaclang/tests/fixtures/semstr.jac +++ b/jaclang/tests/fixtures/semstr.jac @@ -1,29 +1,29 @@ -# enum 'enum' emotion { -# Happy, -# Sad, -# Calm, -# Angry -# } +enum 'enum' emotion { + Happy, + Sad, + Calm, + Angry +} -# node 'personality' -# personality{ -# has -# 'how nice the user is ' -# niceness:int, -# 'what kind of things they like' -# likes : list[str], -# 'current mood of the user' -# currentMood: mood; +node 'personality' +personality{ + has + 'how nice the user is ' + niceness:int, + 'what kind of things they like' + likes : list[str], + 'current mood of the user' + currentMood: mood; -# } +} -# walker 'users personal journal ' -# journal { -# has -# 'Daily Entry' -# count: int; -# } +walker 'users personal journal ' +journal { + has + 'Daily Entry' + count: int; +} can add() -> 'no of'int {} -# can 'update user mood' -# updateMood(* 'jll'mood: str,'fd'gfgf:str) ->'mood selection'str {} \ No newline at end of file +can 'update user mood' +updateMood(* 'jll' mood: str,'semstrinngfd' gfgf:str) ->'mood selection'str {} \ No newline at end of file diff --git a/jaclang/tests/test_language.py b/jaclang/tests/test_language.py index 7f631eb30..5b3e06cfb 100644 --- a/jaclang/tests/test_language.py +++ b/jaclang/tests/test_language.py @@ -171,3 +171,13 @@ def test_semstr(self) -> None: sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertNotIn("Error", stdout_value) + + def test_raw_bytestr(self) -> None: + """Test semstring.""" + captured_output = io.StringIO() + sys.stdout = captured_output + jac_import("raw_byte_string", self.fixture_abs_path("./")) + sys.stdout = sys.__stdout__ + stdout_value = captured_output.getvalue() + self.assertIn(r"Hello\\nWorld", stdout_value) + self.assertIn(r"", stdout_value) From 15cd9a7038959439b8d77cde5452f543d555f62f Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Wed, 24 Jan 2024 20:04:59 +0530 Subject: [PATCH 02/10] added for rb and br also and some refactored in exisiting code --- jaclang/compiler/absyntree.py | 24 ++++++------------------ jaclang/compiler/jac.lark | 2 +- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/jaclang/compiler/absyntree.py b/jaclang/compiler/absyntree.py index 28099a228..53e0777a7 100644 --- a/jaclang/compiler/absyntree.py +++ b/jaclang/compiler/absyntree.py @@ -2150,8 +2150,7 @@ class Int(Literal): def lit_value(self) -> int: """Return literal value in its python type.""" return int(self.value) - - + class String(Literal): """String node type for Jac Ast.""" @@ -2160,28 +2159,17 @@ class String(Literal): @property def lit_value(self) -> str: """Return literal value in its python type.""" - if (self.value.startswith("'''") and self.value.endswith("'''")) or ( - self.value.startswith('"""') and self.value.endswith('"""') - ): - ret_str = self.value[3:-3] - elif (self.value.startswith("'") and self.value.endswith("'")) or ( - self.value.startswith('"') and self.value.endswith('"') - ): - ret_str = self.value[1:-1] - elif (self.value.startswith("r'") and self.value.endswith("'")) or ( - self.value.startswith('r"') and self.value.endswith('"') - ): - return self.value[2:-1] - elif (self.value.startswith("b'") and self.value.endswith("'")) or ( - self.value.startswith('b"') and self.value.endswith('"') - ): + prefix_len = 3 if self.value.startswith("'''") or self.value.startswith('"""') else 1 + + if self.value.startswith(("r", "br")): return eval(self.value) + elif self.value.startswith(("'", '"')): + ret_str = self.value[prefix_len:-prefix_len] else: ret_str = self.value ret_str = ret_str.encode().decode("unicode_escape") return ret_str - class Bool(Literal): """Bool node type for Jac Ast.""" diff --git a/jaclang/compiler/jac.lark b/jaclang/compiler/jac.lark index 68c04e7a4..f78869cbe 100644 --- a/jaclang/compiler/jac.lark +++ b/jaclang/compiler/jac.lark @@ -460,7 +460,7 @@ builtin_type: TYP_TYPE FLOAT: /(\d+(\.\d*)|\.\d+)([eE][+-]?\d+)?/ DOC_STRING: /"""(.|\n|\r)*?"""|'''(.|\n|\r)*?'''/ PYNLINE: /::py::(.|\n|\r)*?::py::/ -STRING: /[rb]?("[^"\r\n]*"|'[^'\r\n]*')/ +STRING: /(r?b?|b?r?)"[^"\r\n]*"|'[^'\r\n]*'/ BOOL.1: /True|False/ KW_NIN.1: /\bnot\s+in\b/ KW_ISN.1: /\bis\s+not\b/ From df742bc19ae77e40045b51bc2545d942436d6b88 Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Wed, 24 Jan 2024 20:40:29 +0530 Subject: [PATCH 03/10] refactored raw and byte string --- jaclang/compiler/absyntree.py | 8 +++++--- jaclang/tests/fixtures/raw_byte_string.jac | 8 ++++++++ jaclang/tests/test_language.py | 6 +++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/jaclang/compiler/absyntree.py b/jaclang/compiler/absyntree.py index 53e0777a7..ebfa442b1 100644 --- a/jaclang/compiler/absyntree.py +++ b/jaclang/compiler/absyntree.py @@ -2150,7 +2150,7 @@ class Int(Literal): def lit_value(self) -> int: """Return literal value in its python type.""" return int(self.value) - + class String(Literal): """String node type for Jac Ast.""" @@ -2159,9 +2159,11 @@ class String(Literal): @property def lit_value(self) -> str: """Return literal value in its python type.""" - prefix_len = 3 if self.value.startswith("'''") or self.value.startswith('"""') else 1 + prefix_len = ( + 3 if self.value.startswith("'''") or self.value.startswith('"""') else 1 + ) - if self.value.startswith(("r", "br")): + if self.value.startswith(("r", "b", "br")): return eval(self.value) elif self.value.startswith(("'", '"')): ret_str = self.value[prefix_len:-prefix_len] diff --git a/jaclang/tests/fixtures/raw_byte_string.jac b/jaclang/tests/fixtures/raw_byte_string.jac index 7fac9581f..942b1cc9f 100644 --- a/jaclang/tests/fixtures/raw_byte_string.jac +++ b/jaclang/tests/fixtures/raw_byte_string.jac @@ -3,8 +3,16 @@ with entry{ hello_world_bytes = b"\x48\x65\x6C\x6C\x6F\x20\x57\x6F\x72\x6C\x64"; print(hello_world_bytes); print(type(hello_world_bytes)); + ret_str = r"Hello\\nWorld"; print(ret_str); + br = br"Hello\\nWorld"; + print(br); + print(type(br)); + + rb = rb"Hello\\nWorld"; + print(rb); + print(type(rb)); } \ No newline at end of file diff --git a/jaclang/tests/test_language.py b/jaclang/tests/test_language.py index 5b3e06cfb..eb42a7f47 100644 --- a/jaclang/tests/test_language.py +++ b/jaclang/tests/test_language.py @@ -173,11 +173,11 @@ def test_semstr(self) -> None: self.assertNotIn("Error", stdout_value) def test_raw_bytestr(self) -> None: - """Test semstring.""" + """Test raw string and byte string.""" captured_output = io.StringIO() sys.stdout = captured_output jac_import("raw_byte_string", self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() - self.assertIn(r"Hello\\nWorld", stdout_value) - self.assertIn(r"", stdout_value) + self.assertEqual(stdout_value.count(r"\\\\"), 2) + self.assertEqual(stdout_value.count(""), 3) From 9412a0f0c3194e99abf09858bddd386157ba92ec Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Wed, 24 Jan 2024 23:01:21 +0530 Subject: [PATCH 04/10] refactored for rb and br --- jaclang/compiler/absyntree.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/jaclang/compiler/absyntree.py b/jaclang/compiler/absyntree.py index ebfa442b1..0bed00a96 100644 --- a/jaclang/compiler/absyntree.py +++ b/jaclang/compiler/absyntree.py @@ -2160,18 +2160,22 @@ class String(Literal): def lit_value(self) -> str: """Return literal value in its python type.""" prefix_len = ( - 3 if self.value.startswith("'''") or self.value.startswith('"""') else 1 + 3 if self.value.startswith(("'''", '"""')) else 1 ) - - if self.value.startswith(("r", "b", "br")): + if ( + (self.value.startswith(("r", "b")) and self.value[1:].startswith(("'", '"'))) or + (self.value.startswith(("br","rb")) and self.value[2:].startswith(("'", '"'))) + ): return eval(self.value) elif self.value.startswith(("'", '"')): ret_str = self.value[prefix_len:-prefix_len] else: ret_str = self.value - ret_str = ret_str.encode().decode("unicode_escape") + # ret_str = ret_str.encode().decode("unicode_escape") + ret_str = ret_str return ret_str + class Bool(Literal): """Bool node type for Jac Ast.""" From 4592063ae2fd0cd91bd27e2f96d9964ab8661185 Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Wed, 24 Jan 2024 23:41:45 +0530 Subject: [PATCH 05/10] linting fixed --- jaclang/compiler/absyntree.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/jaclang/compiler/absyntree.py b/jaclang/compiler/absyntree.py index 0bed00a96..21a9a6382 100644 --- a/jaclang/compiler/absyntree.py +++ b/jaclang/compiler/absyntree.py @@ -2151,6 +2151,7 @@ def lit_value(self) -> int: """Return literal value in its python type.""" return int(self.value) + class String(Literal): """String node type for Jac Ast.""" @@ -2159,20 +2160,19 @@ class String(Literal): @property def lit_value(self) -> str: """Return literal value in its python type.""" - prefix_len = ( - 3 if self.value.startswith(("'''", '"""')) else 1 - ) - if ( - (self.value.startswith(("r", "b")) and self.value[1:].startswith(("'", '"'))) or - (self.value.startswith(("br","rb")) and self.value[2:].startswith(("'", '"'))) + prefix_len = 3 if self.value.startswith(("'''", '"""')) else 1 + if any( + self.value.startswith(prefix) + and self.value[len(prefix) :].startswith(("'", '"')) + for prefix in ["r", "b", "br", "rb"] ): return eval(self.value) + elif self.value.startswith(("'", '"')): ret_str = self.value[prefix_len:-prefix_len] else: ret_str = self.value - # ret_str = ret_str.encode().decode("unicode_escape") - ret_str = ret_str + ret_str = ret_str.encode().decode("unicode_escape") return ret_str From 06423d2201e64b59eb692bea6f28196ce3aa6372 Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Thu, 25 Jan 2024 01:38:41 +0530 Subject: [PATCH 06/10] Examples modified --- examples/reference/tests.jac | 12 ++------ examples/reference/tests.py | 37 +++++------------------ examples/reference/try_statements.jac | 30 +++++------------- examples/reference/try_statements.py | 24 ++------------- examples/reference/walrus_assignments.jac | 16 +++------- examples/reference/walrus_assignments.py | 14 +++------ examples/reference/while_statements.jac | 12 +++----- examples/reference/while_statements.py | 13 +++----- examples/reference/yield_statements.jac | 24 +++++++-------- examples/reference/yield_statements.py | 17 +++++------ 10 files changed, 57 insertions(+), 142 deletions(-) diff --git a/examples/reference/tests.jac b/examples/reference/tests.jac index d340076f6..8e91e510c 100644 --- a/examples/reference/tests.jac +++ b/examples/reference/tests.jac @@ -1,12 +1,6 @@ -glob r=5, x =10, y=5, z=5; - -can foo(a: float) -> float { - return a *2; -} - -test test1 { check.AlmostEqual(foo(r), x); } -test test2 { check.Equal(y, z); } -test test3 { check.Greater(38,4); } +test test1 { check.AlmostEqual(4.99999, 4.99999); } +test test2 { check.Equal(5, 5); } +test test3 { check.In("e","qwerty");} with entry{ print(test1()); diff --git a/examples/reference/tests.py b/examples/reference/tests.py index 4fcb6a081..f902f1afb 100644 --- a/examples/reference/tests.py +++ b/examples/reference/tests.py @@ -1,35 +1,14 @@ import unittest -# Global variables -r, x, y, z = 5, 10, 5, 5 +class TestCases(unittest.TestCase): + def test_test1(self): + self.assertAlmostEqual(4.99999, 4.99999) + def test_test2(self): + self.assertEqual(5, 5) -def foo(a: float) -> float: - return a * 2 + def test_test3(self): + self.assertIn("e", "qwerty") - -# Unit Tests -class TestShapesFunctions(unittest.TestCase): - def test_1(self) -> None: - result = foo(r) - self.assertAlmostEqual( - result, x, places=2, msg=f"foo({r}) should be {x}, but got {result}" - ) - - def test_2(self) -> None: - self.assertEqual(y, z, msg=f"Expected y to be {z}, but got {y}") - - def test_3(self) -> None: - self.assertEqual( - 35, - 4, - msg="This test is intentionally designed to fail for demonstration purposes.", - ) - - -def run_tests(): +if __name__ == '__main__': unittest.main() - - -if __name__ == "__main__": - run_tests() diff --git a/examples/reference/try_statements.jac b/examples/reference/try_statements.jac index c1a894d9e..490ce46e5 100644 --- a/examples/reference/try_statements.jac +++ b/examples/reference/try_statements.jac @@ -1,25 +1,9 @@ -can divide_numbers(a : int,b : int){ - try{ - result = a / b; - print("Result of division:", result);} - except ZeroDivisionError{ - print("Error: Division by zero is not allowed.");} - except TypeError as e{ - print(f"Error: {e}");} - except Exception as e{ - print(f"An unexpected error occurred: {e}");} - else{ - print("Division operation completed successfully.");} - finally{ - print("This block always executes, regardless of success or failure.");} -} - -# Example usage with entry{ -try{ - divide_numbers(10, 2); # Normal division - divide_numbers(10, 0); # Division by zero (ZeroDivisionError) - divide_numbers(10, '2');} # Type error (TypeError) -except Exception as e{ - print(f"Exception caught in the outer try-except block: {e}");} + try{ + print("Result", 5/ 0); + } + except Exception as e{ + print(e); + } + } \ No newline at end of file diff --git a/examples/reference/try_statements.py b/examples/reference/try_statements.py index 2a2b8df47..67b44d10b 100644 --- a/examples/reference/try_statements.py +++ b/examples/reference/try_statements.py @@ -1,23 +1,5 @@ -def divide_numbers(a, b): - try: - result = a / b - print("Result of division:", result) - except ZeroDivisionError: - print("Error: Division by zero is not allowed.") - except TypeError as e: - print(f"Error: {e}") - except Exception as e: - print(f"An unexpected error occurred: {e}") - else: - print("Division operation completed successfully.") - finally: - print("This block always executes, regardless of success or failure.") - - -# Example usage try: - divide_numbers(10, 2) # Normal division - divide_numbers(10, 0) # Division by zero (ZeroDivisionError) - divide_numbers(10, "2") # Type error (TypeError) + print("Result", 5/ 0) except Exception as e: - print(f"Exception caught in the outer try-except block: {e}") + print(e) + \ No newline at end of file diff --git a/examples/reference/walrus_assignments.jac b/examples/reference/walrus_assignments.jac index 42ebf07a8..9f0a8f105 100644 --- a/examples/reference/walrus_assignments.jac +++ b/examples/reference/walrus_assignments.jac @@ -1,14 +1,8 @@ with entry{ - d=[2,3,4,5,6,7,8]; - - can count_odds(data : list) -> int { - odds=[o for o in data if o%2 ==1]; - return len(odds); + numbers = [1, 2, 3, 4, 5]; + for i in numbers{ + if (j := i + i//2) > 3{ + print(j); + } } - - if (n := count_odds(d))>1{ - print(f"{n} odds"); - } - - } \ No newline at end of file diff --git a/examples/reference/walrus_assignments.py b/examples/reference/walrus_assignments.py index b411211f6..9b87bf70b 100644 --- a/examples/reference/walrus_assignments.py +++ b/examples/reference/walrus_assignments.py @@ -1,10 +1,4 @@ -d = [2, 3, 4, 5, 6, 7, 8] - - -def count_odds(data): - odds = [o for o in data if o % 2 == 1] - return len(odds) - - -if (n := count_odds(d)) > 1: - print(f"{n} odds") +numbers = [1, 2, 3, 4, 5] +for i in numbers: + if (j := i + i//2) > 3: + print(j) \ No newline at end of file diff --git a/examples/reference/while_statements.jac b/examples/reference/while_statements.jac index 235f8a362..b60f8c42c 100644 --- a/examples/reference/while_statements.jac +++ b/examples/reference/while_statements.jac @@ -1,11 +1,7 @@ with entry{ - count = 5; - - while count > 0{ - print(f"Countdown: {count}"); - count -= 1; + i = 1; + while i < 6{ + print(i); + i += 1; } - - print("Blastoff!"); - } \ No newline at end of file diff --git a/examples/reference/while_statements.py b/examples/reference/while_statements.py index 3cb465f4d..f5876bc1b 100644 --- a/examples/reference/while_statements.py +++ b/examples/reference/while_statements.py @@ -1,9 +1,4 @@ -# Example: Counting down from 5 to 1 using a while loop -count = 5 - -while count > 0: - print(f"Countdown: {count}") - count -= 1 - # Decrement the count - -print("Blastoff!") +i = 1 +while i < 6: + print(i) + i += 1 \ No newline at end of file diff --git a/examples/reference/yield_statements.jac b/examples/reference/yield_statements.jac index e3567e9bf..d76555468 100644 --- a/examples/reference/yield_statements.jac +++ b/examples/reference/yield_statements.jac @@ -1,16 +1,16 @@ -can fibonacci(n:int){ - (a, b ) = (0, 1) ; - count = 0 ; +can myFunc() { + + yield "Hello"; + yield 91; + yield "Good Bye"; - while count < n{ - yield a ; - (a, b) = (b, a + b) ; - count += 1 ; - } } + with entry{ - number = 15; - for num in fibonacci(number){ - print(num); - } + + x = myFunc(); + + for z in x { + print(z); + } } \ No newline at end of file diff --git a/examples/reference/yield_statements.py b/examples/reference/yield_statements.py index 05468bbbd..78eb44f77 100644 --- a/examples/reference/yield_statements.py +++ b/examples/reference/yield_statements.py @@ -1,12 +1,9 @@ -def fibonacci(n: int) -> None: - a, b = (0, 1) - count = 0 - while count < n: - yield a - a, b = (b, a + b) - count += 1 +def myFunc() -> None: + yield 'Hello' + yield 91 + yield 'Good Bye' +x = myFunc() -number = 15 -for num in fibonacci(number): - print(num) +for z in x: + print(z) \ No newline at end of file From 70d8aee6a59e147b8dc6ee56d952ba25ea947494 Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Thu, 25 Jan 2024 02:53:59 +0530 Subject: [PATCH 07/10] added examples --- examples/reference/lambda_expressions.jac | 10 +++++----- examples/reference/lambda_expressions.py | 11 ++--------- examples/reference/tests.py | 4 +++- examples/reference/try_statements.jac | 4 ++-- examples/reference/try_statements.py | 3 +-- examples/reference/walrus_assignments.py | 4 ++-- examples/reference/while_statements.py | 4 ++-- examples/reference/yield_statements.jac | 2 +- examples/reference/yield_statements.py | 7 ++++--- 9 files changed, 22 insertions(+), 27 deletions(-) diff --git a/examples/reference/lambda_expressions.jac b/examples/reference/lambda_expressions.jac index 916c4d47b..32011f67c 100644 --- a/examples/reference/lambda_expressions.jac +++ b/examples/reference/lambda_expressions.jac @@ -1,8 +1,8 @@ with entry{ -# Example usage of the lambda function -calculate_rectangle_area = with length: float, width: float can length * width; -rectangle_area = calculate_rectangle_area(5.0, 8.0); -print(f"The area of the rectangle is: {rectangle_area}"); + x = with a: int , b:int can a+b; + print(x(5,4)); + +} + -} \ No newline at end of file diff --git a/examples/reference/lambda_expressions.py b/examples/reference/lambda_expressions.py index eee7ad83d..8bd1f451e 100644 --- a/examples/reference/lambda_expressions.py +++ b/examples/reference/lambda_expressions.py @@ -1,9 +1,2 @@ -calculate_rectangle_area = lambda length, width: length * width - -# Example usage -length = 5.0 -width = 8.0 - -area = calculate_rectangle_area(length, width) - -print(f"The area of the rectangle is: {area}") +x = lambda a, b: a + b +print(x(5, 4)) diff --git a/examples/reference/tests.py b/examples/reference/tests.py index f902f1afb..236f9f1df 100644 --- a/examples/reference/tests.py +++ b/examples/reference/tests.py @@ -1,5 +1,6 @@ import unittest + class TestCases(unittest.TestCase): def test_test1(self): self.assertAlmostEqual(4.99999, 4.99999) @@ -10,5 +11,6 @@ def test_test2(self): def test_test3(self): self.assertIn("e", "qwerty") -if __name__ == '__main__': + +if __name__ == "__main__": unittest.main() diff --git a/examples/reference/try_statements.jac b/examples/reference/try_statements.jac index 490ce46e5..316b5716f 100644 --- a/examples/reference/try_statements.jac +++ b/examples/reference/try_statements.jac @@ -4,6 +4,6 @@ with entry{ } except Exception as e{ print(e); - } - + } + } \ No newline at end of file diff --git a/examples/reference/try_statements.py b/examples/reference/try_statements.py index 67b44d10b..ab8948977 100644 --- a/examples/reference/try_statements.py +++ b/examples/reference/try_statements.py @@ -1,5 +1,4 @@ try: - print("Result", 5/ 0) + print("Result", 5 / 0) except Exception as e: print(e) - \ No newline at end of file diff --git a/examples/reference/walrus_assignments.py b/examples/reference/walrus_assignments.py index 9b87bf70b..8269f1a6d 100644 --- a/examples/reference/walrus_assignments.py +++ b/examples/reference/walrus_assignments.py @@ -1,4 +1,4 @@ numbers = [1, 2, 3, 4, 5] for i in numbers: - if (j := i + i//2) > 3: - print(j) \ No newline at end of file + if (j := i + i // 2) > 3: + print(j) diff --git a/examples/reference/while_statements.py b/examples/reference/while_statements.py index f5876bc1b..bc1bd35da 100644 --- a/examples/reference/while_statements.py +++ b/examples/reference/while_statements.py @@ -1,4 +1,4 @@ i = 1 while i < 6: - print(i) - i += 1 \ No newline at end of file + print(i) + i += 1 diff --git a/examples/reference/yield_statements.jac b/examples/reference/yield_statements.jac index d76555468..0dae60c7b 100644 --- a/examples/reference/yield_statements.jac +++ b/examples/reference/yield_statements.jac @@ -9,7 +9,7 @@ can myFunc() { with entry{ x = myFunc(); - + for z in x { print(z); } diff --git a/examples/reference/yield_statements.py b/examples/reference/yield_statements.py index 78eb44f77..965e42b83 100644 --- a/examples/reference/yield_statements.py +++ b/examples/reference/yield_statements.py @@ -1,9 +1,10 @@ def myFunc() -> None: - yield 'Hello' + yield "Hello" yield 91 - yield 'Good Bye' + yield "Good Bye" + x = myFunc() for z in x: - print(z) \ No newline at end of file + print(z) From 148958bc485bfe6dbc1d8f36070d11be8882ac11 Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Thu, 25 Jan 2024 05:44:31 +0530 Subject: [PATCH 08/10] exampls modified --- .../atomic_pipe_back_expressions.jac | 4 +- examples/reference/for_statements.jac | 12 ++-- examples/reference/for_statements.py | 12 +--- .../logical_and_compare_expressions.jac | 71 ++++--------------- .../logical_and_compare_expressions.py | 53 +++----------- examples/reference/match_capture_patterns.jac | 13 ++-- examples/reference/match_capture_patterns.py | 14 ++-- examples/reference/match_class_patterns.jac | 8 ++- examples/reference/match_class_patterns.py | 2 + .../reference/match_litteral_patterns.jac | 3 - examples/reference/match_litteral_patterns.py | 4 +- examples/reference/match_mapping_patterns.jac | 1 - examples/reference/match_mapping_patterns.py | 1 - .../reference/match_sequence_patterns.jac | 5 +- examples/reference/match_sequence_patterns.py | 6 +- examples/reference/match_statements.jac | 8 +-- examples/reference/match_statements.py | 7 +- examples/reference/raise_statements.jac | 1 - 18 files changed, 65 insertions(+), 160 deletions(-) diff --git a/examples/reference/atomic_pipe_back_expressions.jac b/examples/reference/atomic_pipe_back_expressions.jac index 18a1749e7..c08407660 100644 --- a/examples/reference/atomic_pipe_back_expressions.jac +++ b/examples/reference/atomic_pipe_back_expressions.jac @@ -1,8 +1,8 @@ with entry { - print <: "Hello world!" ; # print("Hello world!") + print <: "Hello world!" ; a = [2,4,5,7,8]; b = [4,8,9,13,20]; - c = len<:a + len<:b ; #len(a) + len(b) + c = len<:a + len<:b ; print(c); } \ No newline at end of file diff --git a/examples/reference/for_statements.jac b/examples/reference/for_statements.jac index d8a81eda4..bb8c4b856 100644 --- a/examples/reference/for_statements.jac +++ b/examples/reference/for_statements.jac @@ -1,13 +1,9 @@ with entry{ - start_range = 1; - end_range = 5; - - for i in range(start_range, end_range + 1){ - for number = 1 to number<=10 by number+=1{ - product = i * number; - print(f"{i} x {number} = {product}"); + + for i in range(1, 4 ){ + for j = 1 to j<3 by j+=1{ + print(i,j); } - print("\n", end=""); } } diff --git a/examples/reference/for_statements.py b/examples/reference/for_statements.py index a281ca68b..72f2b9cdf 100644 --- a/examples/reference/for_statements.py +++ b/examples/reference/for_statements.py @@ -1,9 +1,3 @@ -start_range = 1 -end_range = 5 -for i in range(start_range, end_range + 1): - number = 1 - while number <= 10: - product = i * number - print(f"{i} x {number} = {product}") - number += 1 - print("\n", end="") +for i in "banana": + for j in range(1, 3): + print(i, j) diff --git a/examples/reference/logical_and_compare_expressions.jac b/examples/reference/logical_and_compare_expressions.jac index a2ecdf646..4a8d10265 100644 --- a/examples/reference/logical_and_compare_expressions.jac +++ b/examples/reference/logical_and_compare_expressions.jac @@ -1,64 +1,17 @@ - -with entry{ - apples = 5; - oranges = 8; - - # Check if there are more oranges than apples - if oranges > apples{ - print("There are more oranges than apples."); - } - else{ - print("There are fewer oranges than apples."); - } - } - -with entry{ - city1 = "New York"; - city2 = "Los Angeles"; - - # Check if the cities are the same - if city1 == city2{ - print("Both cities are the same."); - } - else{ - print("The cities are different."); - } - -} - -with entry{ - # Using is for identity comparison - list1 = [1, 2, 3]; - list2 = [1, 2, 3]; - - if list1 is list2{ - print("list1 and list2 refer to the same object."); - } - else{ - print("list1 and list2 are different objects."); - } - -} -with entry{ - # Example with a list - my_list = [1, 2, 3, 4, 5]; - result = 3 in my_list; - print(result); # Output: True - -} - -#logical OR,AND .jac with entry{ - is_star_player = True; # Player is a star player - has_high_score = False; # Player has a high score in the game - is_team_captain = True; # Player is the team captain - # Logical OR: Star Player or High Score - access_allowed_or = is_star_player or has_high_score; - print(f"Access allowed (OR): {access_allowed_or}"); # Output: True + if 5 < 4{ + print("True");} + elif "a" != "b"{ + print("'a' is 'a' ");} + else{ + print("No");} - # Logical AND: Team Captain and Star Player - access_allowed_and = is_team_captain and is_star_player; - print(f"Access allowed (AND): {access_allowed_and}"); # Output: True + a = [1, 2, 3]; + b = [1, 2, 3]; + print(a is b); + print( 3 in a); + print(True or False); + print(False and False); } \ No newline at end of file diff --git a/examples/reference/logical_and_compare_expressions.py b/examples/reference/logical_and_compare_expressions.py index 816f22f71..b4d755343 100644 --- a/examples/reference/logical_and_compare_expressions.py +++ b/examples/reference/logical_and_compare_expressions.py @@ -1,47 +1,16 @@ -apples = 5 -oranges = 8 - -if oranges > apples: - print("There are more oranges than apples.") -else: - print("There are fewer oranges than apples.") - - -city1 = "New York" -city2 = "Los Angeles" - -# Check if the cities are the same -if city1 == city2: - print("Both cities are the same.") +if 5 > 4: + print("True") +elif "a" == "a": + print("'a' is 'a' ") else: - print("The cities are different.") - - -list1 = [1, 2, 3] -list2 = [1, 2, 3] - -if list1 is list2: - print("list1 and list2 refer to the same object.") -else: - print("list1 and list2 are different objects.") - + print("False") +a = [1, 2, 3] +b = [1, 2, 3] +print(a is b) +print( 3 in a) -# in -my_list = [1, 2, 3, 4, 5] -result = 3 in my_list -print(result) # Output: True +print(True or False) +print(False and False) -is_star_player = True -# Player is a star player -has_high_score = False -# Player has a high score in the game -is_team_captain = True -# Player is the team captain -# Logical OR: Star Player or High Score -access_allowed_or = is_star_player or has_high_score -print(f"Access allowed (OR): {access_allowed_or}") # Output: True -# Logical AND: Team Captain and Star Player -access_allowed_and = is_team_captain and is_star_player -print(f"Access allowed (AND): {access_allowed_and}") # Output: True diff --git a/examples/reference/match_capture_patterns.jac b/examples/reference/match_capture_patterns.jac index 27d98681a..5f09d418d 100644 --- a/examples/reference/match_capture_patterns.jac +++ b/examples/reference/match_capture_patterns.jac @@ -1,13 +1,10 @@ with entry{ - data=" snoopy"; + day=" sunday"; - match data{ - case "Ironman": - print("You are Marvel fan"); - case "Batman": - print("You are DC fan"); + match day{ + case "monday": + print("confirmed"); case _: - print("You are not a Marvel or DC fan"); - + print("other"); } } \ No newline at end of file diff --git a/examples/reference/match_capture_patterns.py b/examples/reference/match_capture_patterns.py index f85298117..1385c5491 100644 --- a/examples/reference/match_capture_patterns.py +++ b/examples/reference/match_capture_patterns.py @@ -1,9 +1,7 @@ -data = "snoopy" -match data: - case "Ironman": - print("You are Marvel fan") - - case "Batman": - print("You are DC fan") +day=" sunday" +match day: + case "monday": + print("confirmed") case _: - print("You are not a Marvel or DC fan") + print("other") + \ No newline at end of file diff --git a/examples/reference/match_class_patterns.jac b/examples/reference/match_class_patterns.jac index ba8159a68..502d1a06a 100644 --- a/examples/reference/match_class_patterns.jac +++ b/examples/reference/match_class_patterns.jac @@ -6,8 +6,12 @@ with entry{ match data { - # MatchClass case Point(int(a), y=0): print(f"Point with x={a} and y=0"); + case _ : + print("Not on the x-axis"); } - } \ No newline at end of file + } + + + diff --git a/examples/reference/match_class_patterns.py b/examples/reference/match_class_patterns.py index 8e0ed9cf5..f63cd48b5 100644 --- a/examples/reference/match_class_patterns.py +++ b/examples/reference/match_class_patterns.py @@ -11,3 +11,5 @@ class Point: match data: case Point(int(a), y=0): print(f"Point with x={a} and y=0") + case _: + print("Not on the x-axis") diff --git a/examples/reference/match_litteral_patterns.jac b/examples/reference/match_litteral_patterns.jac index 6fa734f33..b10330a88 100644 --- a/examples/reference/match_litteral_patterns.jac +++ b/examples/reference/match_litteral_patterns.jac @@ -1,12 +1,9 @@ with entry{ num=89; match num{ - # MatchSequence case 89: print("Correct"); case 8: print("Nope"); - case 9: - print("Nope"); } } \ No newline at end of file diff --git a/examples/reference/match_litteral_patterns.py b/examples/reference/match_litteral_patterns.py index 817160efa..596e9c21e 100644 --- a/examples/reference/match_litteral_patterns.py +++ b/examples/reference/match_litteral_patterns.py @@ -1,9 +1,7 @@ num = 89 match num: - # MatchSequence case 89: print("Correct") case 8: print("Nope") - case 9: - print("Nope") + diff --git a/examples/reference/match_mapping_patterns.jac b/examples/reference/match_mapping_patterns.jac index 623a9de48..d58a72f51 100644 --- a/examples/reference/match_mapping_patterns.jac +++ b/examples/reference/match_mapping_patterns.jac @@ -2,7 +2,6 @@ data={"key1":1,"key2":2,"232":3453}; - # MatchMapping match data{ case {"key1": 1, "key2": 2, **rest}: print(f"Matched a mapping with key1 and key2. Rest: {rest}"); diff --git a/examples/reference/match_mapping_patterns.py b/examples/reference/match_mapping_patterns.py index 8cf0361e4..9c4b3e93c 100644 --- a/examples/reference/match_mapping_patterns.py +++ b/examples/reference/match_mapping_patterns.py @@ -1,6 +1,5 @@ data = {"key1": 1, "key2": 2, "232": 3453} -# MatchMapping match data: case {"key1": 1, "key2": 2, **rest}: print(f"Matched a mapping with key1 and key2. Rest: {rest}") diff --git a/examples/reference/match_sequence_patterns.jac b/examples/reference/match_sequence_patterns.jac index 4b2e986c1..ffdead425 100644 --- a/examples/reference/match_sequence_patterns.jac +++ b/examples/reference/match_sequence_patterns.jac @@ -1,8 +1,9 @@ with entry{ data=[1,2,3]; match data{ - # MatchSequence case [1, 2, 3]: - print("Matched a specific sequence [1, 2, 3]."); + print("Matched"); + case _: + print("Not Found") } } \ No newline at end of file diff --git a/examples/reference/match_sequence_patterns.py b/examples/reference/match_sequence_patterns.py index a4ba43afc..b5aa2d07a 100644 --- a/examples/reference/match_sequence_patterns.py +++ b/examples/reference/match_sequence_patterns.py @@ -1,5 +1,7 @@ -# MatchSequence data = [1, 2, 3] match data: case [1, 2, 3]: - print("Matched a specific sequence [1, 2, 3].") + print("Matched") + case _: + print("Not Found") + diff --git a/examples/reference/match_statements.jac b/examples/reference/match_statements.jac index 3f83f3f26..004ed2795 100644 --- a/examples/reference/match_statements.jac +++ b/examples/reference/match_statements.jac @@ -2,10 +2,8 @@ with entry { a=8; match a { case 7: - print("Nope"); - case 8: - print("Yep"); + print("doable"); case _: - print("Not possible"); + print("Undoable"); } -} +} \ No newline at end of file diff --git a/examples/reference/match_statements.py b/examples/reference/match_statements.py index 288c467d7..b4d5991b0 100644 --- a/examples/reference/match_statements.py +++ b/examples/reference/match_statements.py @@ -1,8 +1,7 @@ a = 8 match a: case 7: - print("Nope") - case 8: - print("Yep") + print("Doable") case _: - print("Not possible") + print("Undoable") + diff --git a/examples/reference/raise_statements.jac b/examples/reference/raise_statements.jac index 9c6dd738d..517f2968b 100644 --- a/examples/reference/raise_statements.jac +++ b/examples/reference/raise_statements.jac @@ -4,7 +4,6 @@ can foo(value : int){ } } -#have to use KW_FROM with entry{ try{ foo(-1); From 8a4b501decdc07aaa72de2b611b6682efff7dc82 Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Thu, 25 Jan 2024 06:33:31 +0530 Subject: [PATCH 09/10] refactored --- examples/reference/delete_statements.jac | 2 -- examples/reference/delete_statements.py | 3 --- examples/reference/f_string_tokens.jac | 16 ++++++++-------- examples/reference/for_statements.jac | 2 +- .../logical_and_compare_expressions.jac | 2 +- .../reference/logical_and_compare_expressions.py | 5 +---- examples/reference/match_capture_patterns.py | 3 +-- examples/reference/match_class_patterns.jac | 4 ++-- examples/reference/match_litteral_patterns.py | 1 - examples/reference/match_sequence_patterns.py | 3 +-- examples/reference/match_statements.py | 1 - 11 files changed, 15 insertions(+), 27 deletions(-) diff --git a/examples/reference/delete_statements.jac b/examples/reference/delete_statements.jac index 433040133..39152524f 100644 --- a/examples/reference/delete_statements.jac +++ b/examples/reference/delete_statements.jac @@ -4,6 +4,4 @@ with entry{ del x; - # Raises NameError because x is deleted - #print("After Delete:", x); } \ No newline at end of file diff --git a/examples/reference/delete_statements.py b/examples/reference/delete_statements.py index 668b7c0ce..c6aea216d 100644 --- a/examples/reference/delete_statements.py +++ b/examples/reference/delete_statements.py @@ -2,6 +2,3 @@ print("Before Delete:", x) del x - -# Raises NameError because x is deleted -# print("After Delete:", x) diff --git a/examples/reference/f_string_tokens.jac b/examples/reference/f_string_tokens.jac index ee235ad89..06b773654 100644 --- a/examples/reference/f_string_tokens.jac +++ b/examples/reference/f_string_tokens.jac @@ -2,16 +2,16 @@ with entry{ x = "a"; y = 25; - # tokens =f"Hello {x} {y} {{This is an escaped curly brace}}"; - # print(tokens); - person = {"name": "Jane", "age": 25}; - print(f"Hello, {person['name']}! You're {person['age']} years old."); + print(f"Hello {x} {y} {{This is an escaped curly brace}} mm""jhjvhvjh"); - print("This is the first line.\n This is the second line."); - print("This willnot print.\r This will be printed"); - print("This is \t tabbed."); - print("Line 1\fLine 2"); + # person = {"name": "Jane", "age": 25}; + # print(f"Hello, {person['name']}! You're {person['age']} years old."); + + # print("This is the first line.\n This is the second line."); + # print("This willnot print.\r This will be printed"); + # print("This is \t tabbed."); + # print("Line 1\fLine 2"); # words = ["Hello", "World!", "I", "am", "a", "‘Jactastic’ !"]; # print(f"{'\n'.join(words)}"); diff --git a/examples/reference/for_statements.jac b/examples/reference/for_statements.jac index bb8c4b856..e2a4306f3 100644 --- a/examples/reference/for_statements.jac +++ b/examples/reference/for_statements.jac @@ -1,5 +1,5 @@ with entry{ - + for i in range(1, 4 ){ for j = 1 to j<3 by j+=1{ print(i,j); diff --git a/examples/reference/logical_and_compare_expressions.jac b/examples/reference/logical_and_compare_expressions.jac index 4a8d10265..b6ef4288c 100644 --- a/examples/reference/logical_and_compare_expressions.jac +++ b/examples/reference/logical_and_compare_expressions.jac @@ -13,5 +13,5 @@ with entry{ print( 3 in a); print(True or False); - print(False and False); + print(False and False); } \ No newline at end of file diff --git a/examples/reference/logical_and_compare_expressions.py b/examples/reference/logical_and_compare_expressions.py index b4d755343..73fa02ef1 100644 --- a/examples/reference/logical_and_compare_expressions.py +++ b/examples/reference/logical_and_compare_expressions.py @@ -7,10 +7,7 @@ a = [1, 2, 3] b = [1, 2, 3] print(a is b) -print( 3 in a) +print(3 in a) print(True or False) print(False and False) - - - diff --git a/examples/reference/match_capture_patterns.py b/examples/reference/match_capture_patterns.py index 1385c5491..b1963dfb0 100644 --- a/examples/reference/match_capture_patterns.py +++ b/examples/reference/match_capture_patterns.py @@ -1,7 +1,6 @@ -day=" sunday" +day = " sunday" match day: case "monday": print("confirmed") case _: print("other") - \ No newline at end of file diff --git a/examples/reference/match_class_patterns.jac b/examples/reference/match_class_patterns.jac index 502d1a06a..952dc7832 100644 --- a/examples/reference/match_class_patterns.jac +++ b/examples/reference/match_class_patterns.jac @@ -13,5 +13,5 @@ with entry{ } } - - + + diff --git a/examples/reference/match_litteral_patterns.py b/examples/reference/match_litteral_patterns.py index 596e9c21e..aceda07d1 100644 --- a/examples/reference/match_litteral_patterns.py +++ b/examples/reference/match_litteral_patterns.py @@ -4,4 +4,3 @@ print("Correct") case 8: print("Nope") - diff --git a/examples/reference/match_sequence_patterns.py b/examples/reference/match_sequence_patterns.py index b5aa2d07a..d0c0e36b0 100644 --- a/examples/reference/match_sequence_patterns.py +++ b/examples/reference/match_sequence_patterns.py @@ -3,5 +3,4 @@ case [1, 2, 3]: print("Matched") case _: - print("Not Found") - + print("Not Found") diff --git a/examples/reference/match_statements.py b/examples/reference/match_statements.py index b4d5991b0..40257c78d 100644 --- a/examples/reference/match_statements.py +++ b/examples/reference/match_statements.py @@ -4,4 +4,3 @@ print("Doable") case _: print("Undoable") - From 143284c5d498d19f799d4ce239c37563f1ca3332 Mon Sep 17 00:00:00 2001 From: marsninja Date: Thu, 25 Jan 2024 14:03:50 -0500 Subject: [PATCH 10/10] test: tweak --- examples/reference/match_sequence_patterns.jac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/reference/match_sequence_patterns.jac b/examples/reference/match_sequence_patterns.jac index ffdead425..6427f7cac 100644 --- a/examples/reference/match_sequence_patterns.jac +++ b/examples/reference/match_sequence_patterns.jac @@ -4,6 +4,6 @@ with entry{ case [1, 2, 3]: print("Matched"); case _: - print("Not Found") + print("Not Found"); } } \ No newline at end of file