From bf222f4186a5915f64e83edf1b1c6436765b8979 Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Mon, 29 Jan 2024 03:57:51 +0530 Subject: [PATCH 1/9] refactored examples --- examples/reference/arithmetic_expressions.jac | 41 ++------- examples/reference/arithmetic_expressions.py | 38 ++------ examples/reference/assignments.jac | 24 ++--- examples/reference/assignments.py | 27 ++---- examples/reference/atom.jac | 12 +-- examples/reference/bitwise_expressions.jac | 33 ++----- examples/reference/bitwise_expressions.py | 34 ++----- examples/reference/disengage_statements.jac | 4 +- examples/reference/disengage_statements.py | 40 +++----- examples/reference/elvis_expressions.jac | 15 +-- examples/reference/elvis_expressions.py | 5 +- .../global_and_nonlocal_statements.jac | 19 ++-- .../global_and_nonlocal_statements.py | 22 ++--- examples/reference/ignore_statements.jac | 20 +--- examples/reference/ignore_statements.py | 47 ++-------- examples/reference/visit_statements.jac | 91 +++++++++++++++---- 16 files changed, 180 insertions(+), 292 deletions(-) diff --git a/examples/reference/arithmetic_expressions.jac b/examples/reference/arithmetic_expressions.jac index 9a5115cec..745d1d293 100644 --- a/examples/reference/arithmetic_expressions.jac +++ b/examples/reference/arithmetic_expressions.jac @@ -1,34 +1,11 @@ -with entry{ - # Addition -result_addition = 5 + 3; -print("Addition:", result_addition); # Output: 8 +with entry{ + p = print; -# Subtraction -result_subtraction = 10 - 4; -print("Subtraction:", result_subtraction); # Output: 6 + p("Multiply:", 7 * 2); + p("Division:", 15 / 3); + p("Floor:", 15 // 3); + p("Modulo:", 17 % 5); + p("Expon:", 2 ** 3); + p("combo:", (9+ 2) *9 - 2); -# Multiplication -result_multiplication = 7 * 2; -print("Multiplication:", result_multiplication); # Output: 14 - -# Division -result_division = 15 / 3; -print("Division:", result_division); # Output: 5.0 (result is a float) - -# Integer Division (Floor Division) -result_floor_division = 15 // 3; -print("Floor Division:", result_floor_division); # Output: 5 (result is an integer) - -# Modulo (Remainder) -result_modulo = 17 % 5; -print("Modulo (Remainder):", result_modulo); # Output: 2 - -# Exponentiation -result_exponentiation = 2 ** 3; -print("Exponentiation:", result_exponentiation); # Output: 8 - -# Combined Operations -combined_result = (4 + 7) * 2 - 3; -print("Combined Operations:", combined_result); # Output: 15 - -} \ No newline at end of file +} diff --git a/examples/reference/arithmetic_expressions.py b/examples/reference/arithmetic_expressions.py index 69132af22..4d77c7d48 100644 --- a/examples/reference/arithmetic_expressions.py +++ b/examples/reference/arithmetic_expressions.py @@ -1,30 +1,8 @@ -result_addition = 5 + 3 -print("Addition:", result_addition) # Output: 8 - -# Subtraction -result_subtraction = 10 - 4 -print("Subtraction:", result_subtraction) # Output: 6 - -# Multiplication -result_multiplication = 7 * 2 -print("Multiplication:", result_multiplication) # Output: 14 - -# Division -result_division = 15 / 3 -print("Division:", result_division) # Output: 5.0 (result is a float) - -# Integer Division (Floor Division) -result_floor_division = 15 // 3 -print("Floor Division:", result_floor_division) # Output: 5 (result is an integer) - -# Modulo (Remainder) -result_modulo = 17 % 5 -print("Modulo (Remainder):", result_modulo) # Output: 2 - -# Exponentiation -result_exponentiation = 2**3 -print("Exponentiation:", result_exponentiation) # Output: 8 - -# Combined Operations -combined_result = (4 + 7) * 2 - 3 -print("Combined Operations:", combined_result) # Output: 15 +p = print + +p("Multiply:", 7 * 2) +p("Division:", 15 / 3) +p("Floor:", 15 // 3) +p("Modulo:", 17 % 5) +p("Expon:", 2 ** 3) +p("combo:", (9+ 2) * 9 - 2) \ No newline at end of file diff --git a/examples/reference/assignments.jac b/examples/reference/assignments.jac index 0807cf26f..ff24469f0 100644 --- a/examples/reference/assignments.jac +++ b/examples/reference/assignments.jac @@ -1,22 +1,14 @@ with entry{ - a=b=5; - let c = 10; + a=b=16; + let c = 18; print(a,b,c); - - # Right Shift Equal (>>=) - x=16; - x>>=2; - print(x); - -#Left shift Equal (<<=) - x<<=2; - print(x); - - #floor Division - x= 18; - x//=4; - print(x); + a>>=2; + print(a); + a<<=2; + print(a); + c//=4; + print(c); } diff --git a/examples/reference/assignments.py b/examples/reference/assignments.py index 4669792af..a534a6772 100644 --- a/examples/reference/assignments.py +++ b/examples/reference/assignments.py @@ -1,20 +1,9 @@ -a = b = 5 -c = 10 +a = b = 16 +c = 18 print(a, b, c) - - -# Right Shift Equal (>>=) -x = 16 # Binary: 0b10000 -x >>= 2 # Right shift x by 2 positions and assign the result back to x -print(x) # After the operation, x will be 4 (Binary: 0b100) - - -# Left shift Equal (<<=) -x <<= 2 # Left shift x by 2 positions and assign the result back to x -print(x) # After the operation, x will be 64 (Binary: 0b1000000) - - -# floor division equal -x = 18 -x //= 4 -print(x) # output gives 4 +a >>= 2 +print(a) +a <<= 2 +print(a) +c //= 4 +print(c) \ No newline at end of file diff --git a/examples/reference/atom.jac b/examples/reference/atom.jac index 5bbc2b4dc..8b4cab609 100644 --- a/examples/reference/atom.jac +++ b/examples/reference/atom.jac @@ -1,15 +1,15 @@ :enum:x { - a_b=67, - y="aaa" +f"b{a_b}bbcc" + aa=67, + y="aaa" +f"b{aa}bbcc" } - glob c=(3,4,5) , l_1=[2,3,4,5]; +glob c=(3,4,5) , list1=[2,3,4,5]; with entry{ - #do we need to give example for what are atoms ? a="abcde...."; b=True; c=bin(12); d=hex(78); - # e=0x4e; ???? - print(l_1,a,b,c,d); + print(list1,a,b,c,d); + # pp=0x4e; + # print(0b1100); enum x; print(x.y.value); } \ No newline at end of file diff --git a/examples/reference/bitwise_expressions.jac b/examples/reference/bitwise_expressions.jac index 8c945b0d5..d285b7b2c 100644 --- a/examples/reference/bitwise_expressions.jac +++ b/examples/reference/bitwise_expressions.jac @@ -1,29 +1,10 @@ -#Bitwise_operators.jac with entry{ - # Bitwise AND - a = 5; # binary: 0101 - b = 3; # binary: 0011 - result_and = a & b; - print(f"Bitwise AND: {result_and}"); # Output: 1 (binary: 0001) - - # Bitwise OR - result_or = a | b; - print(f"Bitwise OR: {result_or}") ; # Output: 7 (binary: 0111) - - # Bitwise XOR - result_xor = a ^ b; - print(f"Bitwise XOR: {result_xor}"); # Output: 6 (binary: 0110) - - # Bitwise NOT - result_not_a = ~a; - print(f"Bitwise NOT of a: {result_not_a}"); # Output: -6 (binary: 11111010) - - # Left shift - result_left_shift = a << 1; - print(f"Left shift of a: {result_left_shift}"); # Output: 10 (binary: 1010) - - # Right shift - result_right_shift = a >> 1; - print(f"Right shift of a: {result_right_shift}"); # Output: 2 (binary: 0010) + p=print; + p("&:", 5 & 3); + p("|:", 5 | 3); + p("^:", 5 ^ 3); + p("~:", ~5); + p("<<:", 5 << 1); + p(">>:", 5 >> 1); } diff --git a/examples/reference/bitwise_expressions.py b/examples/reference/bitwise_expressions.py index 97c2cee0e..50242e521 100644 --- a/examples/reference/bitwise_expressions.py +++ b/examples/reference/bitwise_expressions.py @@ -1,27 +1,7 @@ -# Bitwise AND -a = 5 -# binary: 0101 -b = 3 -# binary: 0011 -result_and = a & b -print(f"Bitwise AND: {result_and}") # Output: 1 (binary: 0001) - -# Bitwise OR -result_or = a | b -print(f"Bitwise OR: {result_or}") # Output: 7 (binary: 0111) - -# Bitwise XOR -result_xor = a ^ b -print(f"Bitwise XOR: {result_xor}") # Output: 6 (binary: 0110) - -# Bitwise NOT -result_not_a = ~a -print(f"Bitwise NOT of a: {result_not_a}") # Output: -6 (binary: 11111010) - -# Left shift -result_left_shift = a << 1 -print(f"Left shift of a: {result_left_shift}") # Output: 10 (binary: 1010) - -# Right shift -result_right_shift = a >> 1 -print(f"Right shift of a: {result_right_shift}") # Output: 2 (binary: 0010) +p=print +p("&:", 5 & 3) +p("|:", 5 | 3) +p("^:", 5 ^ 3) +p("~:", ~5) +p("<<:", 5 << 1) +p(">>:", 5 >> 1) diff --git a/examples/reference/disengage_statements.jac b/examples/reference/disengage_statements.jac index d98b56bff..0f6212f97 100644 --- a/examples/reference/disengage_statements.jac +++ b/examples/reference/disengage_statements.jac @@ -22,9 +22,7 @@ node node_1{ } visit -->; } -node node2{ - has val2:int; -} + with entry{ spawn walker_1(); } diff --git a/examples/reference/disengage_statements.py b/examples/reference/disengage_statements.py index 8e556323f..c0631edf9 100644 --- a/examples/reference/disengage_statements.py +++ b/examples/reference/disengage_statements.py @@ -1,42 +1,30 @@ from __future__ import annotations from jaclang.plugin.feature import JacFeature as jac - -@jac.make_architype("walker", on_entry=[jac.DSFunc("func2", jac.RootType)], on_exit=[]) +@jac.make_architype('walker', on_entry=[jac.DSFunc('func2', jac.RootType)], on_exit=[]) class walker_1: - def func2(self, walker_node: jac.RootType) -> None: - end = walker_node + + def func2(self, _jac_here_: jac.RootType) -> None: + end = _jac_here_ i = 0 while i < 5: - jac.connect( - end, - (end := node_1(val=i + 1)), - jac.build_edge(jac.EdgeDir.OUT, None, None), - ) + jac.connect(end, (end := node_1(val=i + 1)), jac.build_edge(jac.EdgeDir.OUT, None, None)) i += 1 - if jac.visit_node(self, jac.edge_ref(walker_node, jac.EdgeDir.OUT, None)): + if jac.visit_node(self, jac.edge_ref(_jac_here_, jac.EdgeDir.OUT, None, None)): pass - -@jac.make_architype("node", on_entry=[jac.DSFunc("func_1", walker_1)], on_exit=[]) +@jac.make_architype('node', on_entry=[jac.DSFunc('func_1', walker_1)], on_exit=[]) class node_1: val: int - def func_1(self, node_here: walker_1) -> None: - print("visiting ", self) + def func_1(self, _jac_here_: walker_1) -> None: + print('visiting ', self) if self.val == 3: - print("Disengaging traversal in node with value 3.") - jac.disengage(node_here) + print('Disengaging traversal in node with value 3.') + jac.disengage(_jac_here_) return - if jac.visit_node(node_here, jac.edge_ref(self, jac.EdgeDir.OUT, None)): + if jac.visit_node(_jac_here_, jac.edge_ref(self, jac.EdgeDir.OUT, None, None)): pass else: - print("finished visitng all nodes ....\n") - - -@jac.make_architype("node", on_entry=[], on_exit=[]) -class node2: - val2: int - - -jac.spawn_call(jac.get_root(), walker_1()) + print('finished visitng all nodes ....\n') +jac.spawn_call(jac.get_root(), walker_1()) \ No newline at end of file diff --git a/examples/reference/elvis_expressions.jac b/examples/reference/elvis_expressions.jac index 78500e0fc..870fad714 100644 --- a/examples/reference/elvis_expressions.jac +++ b/examples/reference/elvis_expressions.jac @@ -1,12 +1,7 @@ with entry{ - - data = { - 'username': 'john_doe', - 'email': 'john.doe@example.com', - 'timezone': 'UTC'}; - - # Using the Elvis expression to get the 'language' configuration, with a default value of 'english' if not present - user_language = data.get('language') ?: 'english'; - - print(f"User's preferred language: {user_language}"); + + user_language = None; + preferred_language = user_language ?: 'english'; + + print(preferred_language); } \ No newline at end of file diff --git a/examples/reference/elvis_expressions.py b/examples/reference/elvis_expressions.py index fd5e4b2a7..5f6576ea6 100644 --- a/examples/reference/elvis_expressions.py +++ b/examples/reference/elvis_expressions.py @@ -1 +1,4 @@ -print("User's preferred language: english") +from jaclang.plugin.feature import JacFeature as jac +user_language = None +preferred_language = jac.elvis(user_language, 'english') +print(preferred_language) \ No newline at end of file diff --git a/examples/reference/global_and_nonlocal_statements.jac b/examples/reference/global_and_nonlocal_statements.jac index df90edaf2..3b2d0b6fd 100644 --- a/examples/reference/global_and_nonlocal_statements.jac +++ b/examples/reference/global_and_nonlocal_statements.jac @@ -1,19 +1,18 @@ -glob x=5,y=10,z=1; +glob x="Jaclang "; -can foo() -> none { - :g: x,y,z; - y = 30; - print(x,y); - - can foo2() ->int { +can foo() -> None { + :g: x; + x='Jaclang is '; + y='Awesome'; + can foo2() ->tuple[str,str]{ :nl: y; - y=9; + y="Fantastic"; return (x,y); } + print(x,y); print(foo2()); - } with entry{ - print(foo()); + foo(); } diff --git a/examples/reference/global_and_nonlocal_statements.py b/examples/reference/global_and_nonlocal_statements.py index d6b898709..d8cf6078f 100644 --- a/examples/reference/global_and_nonlocal_statements.py +++ b/examples/reference/global_and_nonlocal_statements.py @@ -1,19 +1,15 @@ -x = 5 -y = 10 -z = 1 +from __future__ import annotations +x = 'Jaclang ' - -def foo(): +def foo() -> None: global x - y = 30 - print(x, y) + x = 'Jaclang is ' + y = 'Awesome' - def foo2() -> int: + def foo2() -> tuple[str, str]: nonlocal y - y = 9 + y = 'Fantastic' return (x, y) - + print(x, y) print(foo2()) - - -print(foo()) +foo() \ No newline at end of file diff --git a/examples/reference/ignore_statements.jac b/examples/reference/ignore_statements.jac index c98f7368d..dc5ecc1e6 100644 --- a/examples/reference/ignore_statements.jac +++ b/examples/reference/ignore_statements.jac @@ -1,24 +1,15 @@ -"""Testing ignore.""" walker GuessGame { can start_game with ` entry; } -walker GuessGame2 { - can start_game with ` entry; -} node turn { - can check with GuessGame|GuessGame2 entry; + can check with GuessGame entry; } + :walker:GuessGame:can:start_game { for i=0 to i<10 by i+=1 { ++> turn(); } - visit -->; -} -:walker:GuessGame2:can:start_game { - for i=0 to i<10 by i+=1 { - ++> turn(); - } - for i=0 to i<15 by i+=1 { + for i=0 to i<5 by i+=1 { ignore -->[i]; } visit -->; @@ -26,11 +17,6 @@ node turn { :node:turn:can:check { print("here", end=", "); } - -# # Run the game with entry { spawn GuessGame(); - print(''); - spawn GuessGame2(); - print(''); } diff --git a/examples/reference/ignore_statements.py b/examples/reference/ignore_statements.py index 241651af7..6ef5ae4cb 100644 --- a/examples/reference/ignore_statements.py +++ b/examples/reference/ignore_statements.py @@ -1,51 +1,24 @@ -"""Testing ignore.""" from __future__ import annotations from jaclang.plugin.feature import JacFeature as jac - -@jac.make_architype( - "walker", on_entry=[jac.DSFunc("start_game", jac.RootType)], on_exit=[] -) +@jac.make_architype('walker', on_entry=[jac.DSFunc('start_game', jac.RootType)], on_exit=[]) class GuessGame: - def start_game(self, walker_here: jac.RootType) -> None: - i = 0 - while i < 10: - jac.connect( - walker_here, turn(), jac.build_edge(jac.EdgeDir.OUT, None, None) - ) - i += 1 - if jac.visit_node(self, jac.edge_ref(walker_here, jac.EdgeDir.OUT, None)): - pass - -@jac.make_architype( - "walker", on_entry=[jac.DSFunc("start_game", jac.RootType)], on_exit=[] -) -class GuessGame2: - def start_game(self, walker_here: jac.RootType) -> None: + def start_game(self, _jac_here_: jac.RootType) -> None: i = 0 while i < 10: - jac.connect( - walker_here, turn(), jac.build_edge(jac.EdgeDir.OUT, None, None) - ) + jac.connect(_jac_here_, turn(), jac.build_edge(jac.EdgeDir.OUT, None, None)) i += 1 i = 0 - while i < 15: - jac.ignore(self, jac.edge_ref(walker_here, jac.EdgeDir.OUT, None)[i]) + while i < 5: + jac.ignore(self, jac.edge_ref(_jac_here_, jac.EdgeDir.OUT, None, None)[i]) i += 1 - if jac.visit_node(self, jac.edge_ref(walker_here, jac.EdgeDir.OUT, None)): + if jac.visit_node(self, jac.edge_ref(_jac_here_, jac.EdgeDir.OUT, None, None)): pass - -@jac.make_architype( - "node", on_entry=[jac.DSFunc("check", GuessGame | GuessGame2)], on_exit=[] -) +@jac.make_architype('node', on_entry=[jac.DSFunc('check', GuessGame)], on_exit=[]) class turn: - def check(self, node_here: GuessGame | GuessGame2) -> None: - print("here", end=", ") - -jac.spawn_call(jac.get_root(), GuessGame()) -print("") -jac.spawn_call(jac.get_root(), GuessGame2()) -print("") + def check(self, _jac_here_: GuessGame) -> None: + print('here', end=', ') +jac.spawn_call(jac.get_root(), GuessGame()) \ No newline at end of file diff --git a/examples/reference/visit_statements.jac b/examples/reference/visit_statements.jac index a527f0f86..c6c355800 100644 --- a/examples/reference/visit_statements.jac +++ b/examples/reference/visit_statements.jac @@ -1,34 +1,87 @@ -walker walker_1{ - can func2 with ` entry; +#version1 +walker creator{ + can create with ` entry; } -node node_1{ - has val :int; - can func_1 with walker_1 entry; -} -walker walker_2{ +walker Travellor{ can func_3 with ` entry; } -:node:node_1:can:func_1{ +node item{ + has val :int; + can func_1 with creator|Travellor + entry; +} +:node:item:can:func_1{ print("visiting ",); + if .val==2 and str()== 'Travellor()'{ + print("visiting again a specific node by w2\n"); + } visit-->else{ print("finished visitng all nodes ....\n"); }; } -:walker:walker_1:can:func2{ - end = ; +:walker:creator:can:create{ + end = ; for i=0 to i<5 by i+=1 { - end ++> end := node_1(val=i+1); + end ++> end := item(val=i+1); } visit -->; } -:walker:walker_2:can:func_3{ - print("visiting specific node by w3\n"); - visit(node_1(val=2)); -} -node node2{ - has val2:int; +:walker:Travellor:can:func_3{ + visit(item(val=2)); } + with entry{ - spawn walker_1(); - spawn walker_2(); + spawn creator(); + spawn Travellor(); } + +# #version2 +# #1 +# walker creator{ +# can create with ` entry; +# } +# node item{ +# has val :int; +# can func_1 with creator entry; +# } +# :walker:creator:can:create{ +# end = ; +# for i=0 to i<5 by i+=1 { +# end ++> end := item(val=i+1); +# } +# visit(item(val=2)); +# } +# :node:item:can:func_1{ +# print("visiting ",); +# } +# with entry{ +# spawn creator(); +# } +# #2 +# walker creator{ +# can create with ` entry; +# } + +# node item{ +# has val :int; +# can func_1 with creator entry; +# } +# :node:item:can:func_1{ +# print("visiting ",); + +# visit-->else{ +# print("finished visitng all nodes ....\n"); +# }; +# } +# :walker:creator:can:create{ +# end = ; +# for i=0 to i<5 by i+=1 { +# end ++> end := item(val=i+1); +# } +# visit -->; +# } + + +# with entry{ +# spawn creator(); +# } From ab5299613105b2b0514c322f7a3cd7f4af169ec3 Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Mon, 29 Jan 2024 04:17:05 +0530 Subject: [PATCH 2/9] linting fixed --- examples/reference/arithmetic_expressions.jac | 2 +- examples/reference/arithmetic_expressions.py | 4 ++-- examples/reference/assignments.py | 2 +- examples/reference/bitwise_expressions.py | 2 +- examples/reference/disengage_statements.py | 23 ++++++++++++------- examples/reference/elvis_expressions.jac | 4 ++-- examples/reference/elvis_expressions.py | 5 ++-- .../global_and_nonlocal_statements.py | 15 ++++++++---- examples/reference/ignore_statements.py | 16 ++++++++----- examples/reference/visit_statements.jac | 12 +++++----- 10 files changed, 51 insertions(+), 34 deletions(-) diff --git a/examples/reference/arithmetic_expressions.jac b/examples/reference/arithmetic_expressions.jac index 745d1d293..ddf222840 100644 --- a/examples/reference/arithmetic_expressions.jac +++ b/examples/reference/arithmetic_expressions.jac @@ -1,4 +1,4 @@ -with entry{ +with entry{ p = print; p("Multiply:", 7 * 2); diff --git a/examples/reference/arithmetic_expressions.py b/examples/reference/arithmetic_expressions.py index 4d77c7d48..a6382fde7 100644 --- a/examples/reference/arithmetic_expressions.py +++ b/examples/reference/arithmetic_expressions.py @@ -4,5 +4,5 @@ p("Division:", 15 / 3) p("Floor:", 15 // 3) p("Modulo:", 17 % 5) -p("Expon:", 2 ** 3) -p("combo:", (9+ 2) * 9 - 2) \ No newline at end of file +p("Expon:", 2**3) +p("combo:", (9 + 2) * 9 - 2) diff --git a/examples/reference/assignments.py b/examples/reference/assignments.py index a534a6772..4da67aa00 100644 --- a/examples/reference/assignments.py +++ b/examples/reference/assignments.py @@ -6,4 +6,4 @@ a <<= 2 print(a) c //= 4 -print(c) \ No newline at end of file +print(c) diff --git a/examples/reference/bitwise_expressions.py b/examples/reference/bitwise_expressions.py index 50242e521..51c924380 100644 --- a/examples/reference/bitwise_expressions.py +++ b/examples/reference/bitwise_expressions.py @@ -1,4 +1,4 @@ -p=print +p = print p("&:", 5 & 3) p("|:", 5 | 3) p("^:", 5 ^ 3) diff --git a/examples/reference/disengage_statements.py b/examples/reference/disengage_statements.py index c0631edf9..f56d4a5e7 100644 --- a/examples/reference/disengage_statements.py +++ b/examples/reference/disengage_statements.py @@ -1,30 +1,37 @@ from __future__ import annotations from jaclang.plugin.feature import JacFeature as jac -@jac.make_architype('walker', on_entry=[jac.DSFunc('func2', jac.RootType)], on_exit=[]) -class walker_1: +@jac.make_architype("walker", on_entry=[jac.DSFunc("func2", jac.RootType)], on_exit=[]) +class walker_1: def func2(self, _jac_here_: jac.RootType) -> None: end = _jac_here_ i = 0 while i < 5: - jac.connect(end, (end := node_1(val=i + 1)), jac.build_edge(jac.EdgeDir.OUT, None, None)) + jac.connect( + end, + (end := node_1(val=i + 1)), + jac.build_edge(jac.EdgeDir.OUT, None, None), + ) i += 1 if jac.visit_node(self, jac.edge_ref(_jac_here_, jac.EdgeDir.OUT, None, None)): pass -@jac.make_architype('node', on_entry=[jac.DSFunc('func_1', walker_1)], on_exit=[]) + +@jac.make_architype("node", on_entry=[jac.DSFunc("func_1", walker_1)], on_exit=[]) class node_1: val: int def func_1(self, _jac_here_: walker_1) -> None: - print('visiting ', self) + print("visiting ", self) if self.val == 3: - print('Disengaging traversal in node with value 3.') + print("Disengaging traversal in node with value 3.") jac.disengage(_jac_here_) return if jac.visit_node(_jac_here_, jac.edge_ref(self, jac.EdgeDir.OUT, None, None)): pass else: - print('finished visitng all nodes ....\n') -jac.spawn_call(jac.get_root(), walker_1()) \ No newline at end of file + print("finished visitng all nodes ....\n") + + +jac.spawn_call(jac.get_root(), walker_1()) diff --git a/examples/reference/elvis_expressions.jac b/examples/reference/elvis_expressions.jac index 870fad714..baae488f5 100644 --- a/examples/reference/elvis_expressions.jac +++ b/examples/reference/elvis_expressions.jac @@ -1,7 +1,7 @@ with entry{ - + user_language = None; preferred_language = user_language ?: 'english'; - + print(preferred_language); } \ No newline at end of file diff --git a/examples/reference/elvis_expressions.py b/examples/reference/elvis_expressions.py index 5f6576ea6..6f1e99ea1 100644 --- a/examples/reference/elvis_expressions.py +++ b/examples/reference/elvis_expressions.py @@ -1,4 +1,5 @@ from jaclang.plugin.feature import JacFeature as jac + user_language = None -preferred_language = jac.elvis(user_language, 'english') -print(preferred_language) \ No newline at end of file +preferred_language = jac.elvis(user_language, "english") +print(preferred_language) diff --git a/examples/reference/global_and_nonlocal_statements.py b/examples/reference/global_and_nonlocal_statements.py index d8cf6078f..0a1007f14 100644 --- a/examples/reference/global_and_nonlocal_statements.py +++ b/examples/reference/global_and_nonlocal_statements.py @@ -1,15 +1,20 @@ from __future__ import annotations -x = 'Jaclang ' + +x = "Jaclang " + def foo() -> None: global x - x = 'Jaclang is ' - y = 'Awesome' + x = "Jaclang is " + y = "Awesome" def foo2() -> tuple[str, str]: nonlocal y - y = 'Fantastic' + y = "Fantastic" return (x, y) + print(x, y) print(foo2()) -foo() \ No newline at end of file + + +foo() diff --git a/examples/reference/ignore_statements.py b/examples/reference/ignore_statements.py index 6ef5ae4cb..c34c554df 100644 --- a/examples/reference/ignore_statements.py +++ b/examples/reference/ignore_statements.py @@ -1,9 +1,11 @@ from __future__ import annotations from jaclang.plugin.feature import JacFeature as jac -@jac.make_architype('walker', on_entry=[jac.DSFunc('start_game', jac.RootType)], on_exit=[]) -class GuessGame: +@jac.make_architype( + "walker", on_entry=[jac.DSFunc("start_game", jac.RootType)], on_exit=[] +) +class GuessGame: def start_game(self, _jac_here_: jac.RootType) -> None: i = 0 while i < 10: @@ -16,9 +18,11 @@ def start_game(self, _jac_here_: jac.RootType) -> None: if jac.visit_node(self, jac.edge_ref(_jac_here_, jac.EdgeDir.OUT, None, None)): pass -@jac.make_architype('node', on_entry=[jac.DSFunc('check', GuessGame)], on_exit=[]) -class turn: +@jac.make_architype("node", on_entry=[jac.DSFunc("check", GuessGame)], on_exit=[]) +class turn: def check(self, _jac_here_: GuessGame) -> None: - print('here', end=', ') -jac.spawn_call(jac.get_root(), GuessGame()) \ No newline at end of file + print("here", end=", ") + + +jac.spawn_call(jac.get_root(), GuessGame()) diff --git a/examples/reference/visit_statements.jac b/examples/reference/visit_statements.jac index c6c355800..a4bd7e0d3 100644 --- a/examples/reference/visit_statements.jac +++ b/examples/reference/visit_statements.jac @@ -1,4 +1,4 @@ -#version1 +#version1 walker creator{ can create with ` entry; } @@ -20,7 +20,7 @@ node item{ }; } :walker:creator:can:create{ - end = ; + end = ; for i=0 to i<5 by i+=1 { end ++> end := item(val=i+1); } @@ -34,7 +34,7 @@ with entry{ spawn creator(); spawn Travellor(); } - + # #version2 # #1 # walker creator{ @@ -45,7 +45,7 @@ with entry{ # can func_1 with creator entry; # } # :walker:creator:can:create{ -# end = ; +# end = ; # for i=0 to i<5 by i+=1 { # end ++> end := item(val=i+1); # } @@ -68,13 +68,13 @@ with entry{ # } # :node:item:can:func_1{ # print("visiting ",); - + # visit-->else{ # print("finished visitng all nodes ....\n"); # }; # } # :walker:creator:can:create{ -# end = ; +# end = ; # for i=0 to i<5 by i+=1 { # end ++> end := item(val=i+1); # } From c12bbd17061555a6de3d888bd00337ad3df15ab8 Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Mon, 29 Jan 2024 16:48:38 +0530 Subject: [PATCH 3/9] refactored examples --- .../data_spatial_walker_statements.jac | 9 +-- examples/reference/enumerations.jac | 5 +- examples/reference/f_string_tokens.jac | 18 +++--- examples/reference/f_string_tokens.py | 8 +-- examples/reference/for_statements.jac | 10 ++-- examples/reference/for_statements.py | 5 +- .../reference/import_include_statements.py | 9 ++- examples/reference/lambda_expressions.jac | 2 +- .../logical_and_compare_expressions.jac | 2 +- .../logical_and_compare_expressions.py | 2 +- examples/reference/match_patterns.jac | 7 ++- examples/reference/match_patterns.py | 6 +- examples/reference/special_comprehensions.jac | 1 + examples/reference/special_comprehensions.py | 20 +++++-- examples/reference/visit_statements.py | 57 ++++++++----------- examples/reference/walrus_assignments.jac | 2 +- examples/reference/walrus_assignments.py | 2 +- 17 files changed, 86 insertions(+), 79 deletions(-) diff --git a/examples/reference/data_spatial_walker_statements.jac b/examples/reference/data_spatial_walker_statements.jac index 52e5dd231..effdbb601 100644 --- a/examples/reference/data_spatial_walker_statements.jac +++ b/examples/reference/data_spatial_walker_statements.jac @@ -19,14 +19,7 @@ walker walker_2{ } visit -->; } -:walker:walker_2:can:do_something{ - print(f"walker_2 reached to {}"); - if .val == 4{ - # walker_2 will terminate visiting nodes. - disengage; - } - visit -->; -} + :node:node_a:can:print_something -> str{ print(f"walker_1 entered to {}"); visit -->; diff --git a/examples/reference/enumerations.jac b/examples/reference/enumerations.jac index dc7c8d2ce..cb0ceb0fa 100644 --- a/examples/reference/enumerations.jac +++ b/examples/reference/enumerations.jac @@ -3,4 +3,7 @@ enum Color{ RED = 1, GREEN = 2, BLUE = 3 -} \ No newline at end of file +} + + + diff --git a/examples/reference/f_string_tokens.jac b/examples/reference/f_string_tokens.jac index 06b773654..a7130f648 100644 --- a/examples/reference/f_string_tokens.jac +++ b/examples/reference/f_string_tokens.jac @@ -3,17 +3,17 @@ with entry{ y = 25; - print(f"Hello {x} {y} {{This is an escaped curly brace}} mm""jhjvhvjh"); + print(f"Hello {x} {y} {{This is an escaped curly brace}}"); - # person = {"name": "Jane", "age": 25}; - # print(f"Hello, {person['name']}! You're {person['age']} years old."); + 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"); + print("This is the first line.\n This is the second line."); + print("This will not 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)}"); + words = ["Hello", "World!", "I", "am", "a", "Jactastic!"]; + print(f"{'\n'.join(words)}"); } \ No newline at end of file diff --git a/examples/reference/f_string_tokens.py b/examples/reference/f_string_tokens.py index 48c8480b8..ae77df11f 100644 --- a/examples/reference/f_string_tokens.py +++ b/examples/reference/f_string_tokens.py @@ -1,8 +1,8 @@ x = "a" y = 25 -# tokens = f"Hello {x} {y} {{This is an escaped curly brace}}" -# print(tokens) +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.") @@ -15,5 +15,5 @@ print("This is \t tabbed.") print("Line 1\fLine 2") -# words = ["Hello", "World!", "I", "am", "a", "‘Jactastic’ !"] -# print(f"{'\n'.join(words)}") +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 e2a4306f3..f82224265 100644 --- a/examples/reference/for_statements.jac +++ b/examples/reference/for_statements.jac @@ -1,9 +1,11 @@ with entry{ - for i in range(1, 4 ){ - for j = 1 to j<3 by j+=1{ - print(i,j); - } + for i in "ban"{ + for j in range(1, 3 ){ + for k = 1 to k<3 by k+=1{ + print(i,j,k); + } + } } } diff --git a/examples/reference/for_statements.py b/examples/reference/for_statements.py index 72f2b9cdf..2ba1e0f12 100644 --- a/examples/reference/for_statements.py +++ b/examples/reference/for_statements.py @@ -1,3 +1,4 @@ -for i in "banana": +for i in "ban": for j in range(1, 3): - print(i, j) + for k in range(1, 3, 1): + print(i, j, k) diff --git a/examples/reference/import_include_statements.py b/examples/reference/import_include_statements.py index a21f0aec0..521105d57 100644 --- a/examples/reference/import_include_statements.py +++ b/examples/reference/import_include_statements.py @@ -1,10 +1,9 @@ -from jaclang import jac_import +from __future__ import annotations +from jaclang import jac_import as __jac_import__ import os from math import sqrt as square_root import datetime as dt - -jac_import(target="base_module_structure", base_path=__file__) +__jac_import__(target='base_module_structure', base_path=__file__) from base_module_structure import add, subtract - for i in range(int(square_root(dt.datetime.now().year))): - print(os.getcwd(), add(i, subtract(i, 1))) + print(os.getcwd(), add(i, subtract(i, 1))) \ No newline at end of file diff --git a/examples/reference/lambda_expressions.jac b/examples/reference/lambda_expressions.jac index 32011f67c..f175d9e9a 100644 --- a/examples/reference/lambda_expressions.jac +++ b/examples/reference/lambda_expressions.jac @@ -1,6 +1,6 @@ with entry{ - x = with a: int , b:int can a+b; + x = with a: int , c:int can c+c; print(x(5,4)); } diff --git a/examples/reference/logical_and_compare_expressions.jac b/examples/reference/logical_and_compare_expressions.jac index b6ef4288c..b6406d29e 100644 --- a/examples/reference/logical_and_compare_expressions.jac +++ b/examples/reference/logical_and_compare_expressions.jac @@ -1,6 +1,6 @@ with entry{ - if 5 < 4{ + if 5 > 4{ print("True");} elif "a" != "b"{ print("'a' is 'a' ");} diff --git a/examples/reference/logical_and_compare_expressions.py b/examples/reference/logical_and_compare_expressions.py index 73fa02ef1..5ea0a9290 100644 --- a/examples/reference/logical_and_compare_expressions.py +++ b/examples/reference/logical_and_compare_expressions.py @@ -1,6 +1,6 @@ if 5 > 4: print("True") -elif "a" == "a": +elif "a" != "a": print("'a' is 'a' ") else: print("False") diff --git a/examples/reference/match_patterns.jac b/examples/reference/match_patterns.jac index 27556b2b1..6eb9f1e5c 100644 --- a/examples/reference/match_patterns.jac +++ b/examples/reference/match_patterns.jac @@ -2,7 +2,7 @@ obj Point { has x: flaot, y: float; } -can match_example(data: dict) { +can match_example(data :any) { match data { # MatchValue @@ -43,3 +43,8 @@ can match_example(data: dict) { print("No match found."); } } + +with entry{ + match_example(Point(x=9,y=0)); + +} \ No newline at end of file diff --git a/examples/reference/match_patterns.py b/examples/reference/match_patterns.py index d04cfb0a3..216b6a1a9 100644 --- a/examples/reference/match_patterns.py +++ b/examples/reference/match_patterns.py @@ -4,7 +4,7 @@ def __init__(self, x: float, y: float): self.y = y -def match_example(data: dict): +def match_example(data: any): match data: # MatchValue case 42: @@ -31,7 +31,7 @@ def match_example(data: dict): print(f"Matched a mapping with key1 and key2. Rest: {rest}") # MatchClass - case Point(int(a), y=0): + case Point(x=int(a), y=0): print(f"Point with x={a} and y=0") # MatchAs @@ -44,3 +44,5 @@ def match_example(data: dict): case _: print("No match found.") + +match_example( Point(x=9, y=0)) \ No newline at end of file diff --git a/examples/reference/special_comprehensions.jac b/examples/reference/special_comprehensions.jac index 137752d27..cc0780e48 100644 --- a/examples/reference/special_comprehensions.jac +++ b/examples/reference/special_comprehensions.jac @@ -13,6 +13,7 @@ obj TestObj { } with entry{ + random.seed(42); apple = []; for i=0 to i<10 by i+=1{ apple.append(TestObj()); diff --git a/examples/reference/special_comprehensions.py b/examples/reference/special_comprehensions.py index e1b8ca756..be41995d4 100644 --- a/examples/reference/special_comprehensions.py +++ b/examples/reference/special_comprehensions.py @@ -1,20 +1,32 @@ +from __future__ import annotations +from jaclang.plugin.feature import JacFeature as _Jac import random +random.seed(42) + +@_Jac.make_architype('obj', on_entry=[], on_exit=[]) class TestObj: + def __init__(self) -> None: self.x = random.randint(0, 15) self.y = random.randint(0, 15) self.z = random.randint(0, 15) - x: int = random.randint(0, 15) y: int = random.randint(0, 20) z: int = random.randint(0, 50) - - apple = [] i = 0 while i < 10: apple.append(TestObj()) i += 1 -print((lambda x: [i for i in x if i.y <= 10])(apple)) +print((lambda x: [i for i in x if i.y <= 7])(apple)) + +@_Jac.make_architype('obj', on_entry=[], on_exit=[]) +class MyObj: + apple: int = 0 + banana: int = 0 +x = MyObj() +y = MyObj() +mvar = _Jac.assign_compr([x, y], (('apple', 'banana'), (5, 7))) +print(mvar) \ No newline at end of file diff --git a/examples/reference/visit_statements.py b/examples/reference/visit_statements.py index 8ae26cb81..bfa577656 100644 --- a/examples/reference/visit_statements.py +++ b/examples/reference/visit_statements.py @@ -1,47 +1,36 @@ from __future__ import annotations -from jaclang.plugin.feature import JacFeature as jac +from jaclang.plugin.feature import JacFeature as _Jac +@_Jac.make_architype('walker', on_entry=[_Jac.DSFunc('create', _Jac.RootType)], on_exit=[]) +class creator: -@jac.make_architype("walker", on_entry=[jac.DSFunc("func2", jac.RootType)], on_exit=[]) -class walker_1: - def func2(self, walker_here: jac.RootType) -> None: - end = walker_here + def create(self, _jac_here_: _Jac.RootType) -> None: + end = _jac_here_ i = 0 while i < 5: - jac.connect( - end, - (end := node_1(val=i + 1)), - jac.build_edge(jac.EdgeDir.OUT, None, None), - ) + _Jac.connect(end, (end := item(val=i + 1)), _Jac.build_edge(_Jac.EdgeDir.OUT, None, None)) i += 1 - if jac.visit_node(self, jac.edge_ref(walker_here, jac.EdgeDir.OUT, None)): + if _Jac.visit_node(self, _Jac.edge_ref(_jac_here_, _Jac.EdgeDir.OUT, None, None)): pass +@_Jac.make_architype('walker', on_entry=[_Jac.DSFunc('func_3', _Jac.RootType)], on_exit=[]) +class Travellor: -@jac.make_architype("node", on_entry=[jac.DSFunc("func_1", walker_1)], on_exit=[]) -class node_1: - val: int - - def func_1(self, node_here: walker_1) -> None: - print("visiting ", self) - if jac.visit_node(node_here, jac.edge_ref(self, jac.EdgeDir.OUT, None)): + def func_3(self, _jac_here_: _Jac.RootType) -> None: + if _Jac.visit_node(self, item(val=2)): pass - else: - print("finished visitng all nodes ....\n") +@_Jac.make_architype('node', on_entry=[_Jac.DSFunc('func_1', creator | Travellor)], on_exit=[]) +class item: + val: int -@jac.make_architype("walker", on_entry=[jac.DSFunc("func_3", jac.RootType)], on_exit=[]) -class walker_2: - def func_3(self, walker_here: jac.RootType) -> None: - print("visiting specific node by w3\n") - if jac.visit_node(self, node_1(val=2)): + def func_1(self, _jac_here_: creator | Travellor) -> None: + print('visiting ', self) + if self.val == 2 and str(_jac_here_) == 'Travellor()': + print('visiting again a specific node by w2\n') + if _Jac.visit_node(_jac_here_, _Jac.edge_ref(self, _Jac.EdgeDir.OUT, None, None)): pass - - -@jac.make_architype("node", on_entry=[], on_exit=[]) -class node2: - val2: int - - -jac.spawn_call(jac.get_root(), walker_1()) -jac.spawn_call(jac.get_root(), walker_2()) + else: + print('finished visitng all nodes ....\n') +_Jac.spawn_call(_Jac.get_root(), creator()) +_Jac.spawn_call(_Jac.get_root(), Travellor()) \ No newline at end of file diff --git a/examples/reference/walrus_assignments.jac b/examples/reference/walrus_assignments.jac index 9f0a8f105..6c0718fb5 100644 --- a/examples/reference/walrus_assignments.jac +++ b/examples/reference/walrus_assignments.jac @@ -1,7 +1,7 @@ with entry{ numbers = [1, 2, 3, 4, 5]; for i in numbers{ - if (j := i + i//2) > 3{ + if (j := (i + (i//2))) > 3{ print(j); } } diff --git a/examples/reference/walrus_assignments.py b/examples/reference/walrus_assignments.py index 8269f1a6d..bd2fb9694 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: + if (j := (i + (i // 2))) > 3: print(j) From fdb1590cd69febb3790fd498bd9c232165ab3cf5 Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Mon, 29 Jan 2024 16:57:54 +0530 Subject: [PATCH 4/9] linting fixed (example) --- examples/reference/for_statements.jac | 2 +- .../reference/import_include_statements.py | 6 ++- examples/reference/match_patterns.py | 3 +- examples/reference/special_comprehensions.py | 16 ++++--- examples/reference/visit_statements.py | 43 +++++++++++++------ 5 files changed, 48 insertions(+), 22 deletions(-) diff --git a/examples/reference/for_statements.jac b/examples/reference/for_statements.jac index f82224265..41f64427e 100644 --- a/examples/reference/for_statements.jac +++ b/examples/reference/for_statements.jac @@ -5,7 +5,7 @@ with entry{ for k = 1 to k<3 by k+=1{ print(i,j,k); } - } + } } } diff --git a/examples/reference/import_include_statements.py b/examples/reference/import_include_statements.py index 521105d57..715e64b71 100644 --- a/examples/reference/import_include_statements.py +++ b/examples/reference/import_include_statements.py @@ -3,7 +3,9 @@ import os from math import sqrt as square_root import datetime as dt -__jac_import__(target='base_module_structure', base_path=__file__) + +__jac_import__(target="base_module_structure", base_path=__file__) from base_module_structure import add, subtract + for i in range(int(square_root(dt.datetime.now().year))): - print(os.getcwd(), add(i, subtract(i, 1))) \ No newline at end of file + print(os.getcwd(), add(i, subtract(i, 1))) diff --git a/examples/reference/match_patterns.py b/examples/reference/match_patterns.py index 216b6a1a9..51c7d237a 100644 --- a/examples/reference/match_patterns.py +++ b/examples/reference/match_patterns.py @@ -45,4 +45,5 @@ def match_example(data: any): case _: print("No match found.") -match_example( Point(x=9, y=0)) \ No newline at end of file + +match_example(Point(x=9, y=0)) diff --git a/examples/reference/special_comprehensions.py b/examples/reference/special_comprehensions.py index be41995d4..cfd752094 100644 --- a/examples/reference/special_comprehensions.py +++ b/examples/reference/special_comprehensions.py @@ -5,16 +5,19 @@ random.seed(42) -@_Jac.make_architype('obj', on_entry=[], on_exit=[]) -class TestObj: +@_Jac.make_architype("obj", on_entry=[], on_exit=[]) +class TestObj: def __init__(self) -> None: self.x = random.randint(0, 15) self.y = random.randint(0, 15) self.z = random.randint(0, 15) + x: int = random.randint(0, 15) y: int = random.randint(0, 20) z: int = random.randint(0, 50) + + apple = [] i = 0 while i < 10: @@ -22,11 +25,14 @@ def __init__(self) -> None: i += 1 print((lambda x: [i for i in x if i.y <= 7])(apple)) -@_Jac.make_architype('obj', on_entry=[], on_exit=[]) + +@_Jac.make_architype("obj", on_entry=[], on_exit=[]) class MyObj: apple: int = 0 banana: int = 0 + + x = MyObj() y = MyObj() -mvar = _Jac.assign_compr([x, y], (('apple', 'banana'), (5, 7))) -print(mvar) \ No newline at end of file +mvar = _Jac.assign_compr([x, y], (("apple", "banana"), (5, 7))) +print(mvar) diff --git a/examples/reference/visit_statements.py b/examples/reference/visit_statements.py index bfa577656..1e24a0e2d 100644 --- a/examples/reference/visit_statements.py +++ b/examples/reference/visit_statements.py @@ -1,36 +1,53 @@ from __future__ import annotations from jaclang.plugin.feature import JacFeature as _Jac -@_Jac.make_architype('walker', on_entry=[_Jac.DSFunc('create', _Jac.RootType)], on_exit=[]) -class creator: +@_Jac.make_architype( + "walker", on_entry=[_Jac.DSFunc("create", _Jac.RootType)], on_exit=[] +) +class creator: def create(self, _jac_here_: _Jac.RootType) -> None: end = _jac_here_ i = 0 while i < 5: - _Jac.connect(end, (end := item(val=i + 1)), _Jac.build_edge(_Jac.EdgeDir.OUT, None, None)) + _Jac.connect( + end, + (end := item(val=i + 1)), + _Jac.build_edge(_Jac.EdgeDir.OUT, None, None), + ) i += 1 - if _Jac.visit_node(self, _Jac.edge_ref(_jac_here_, _Jac.EdgeDir.OUT, None, None)): + if _Jac.visit_node( + self, _Jac.edge_ref(_jac_here_, _Jac.EdgeDir.OUT, None, None) + ): pass -@_Jac.make_architype('walker', on_entry=[_Jac.DSFunc('func_3', _Jac.RootType)], on_exit=[]) -class Travellor: +@_Jac.make_architype( + "walker", on_entry=[_Jac.DSFunc("func_3", _Jac.RootType)], on_exit=[] +) +class Travellor: def func_3(self, _jac_here_: _Jac.RootType) -> None: if _Jac.visit_node(self, item(val=2)): pass -@_Jac.make_architype('node', on_entry=[_Jac.DSFunc('func_1', creator | Travellor)], on_exit=[]) + +@_Jac.make_architype( + "node", on_entry=[_Jac.DSFunc("func_1", creator | Travellor)], on_exit=[] +) class item: val: int def func_1(self, _jac_here_: creator | Travellor) -> None: - print('visiting ', self) - if self.val == 2 and str(_jac_here_) == 'Travellor()': - print('visiting again a specific node by w2\n') - if _Jac.visit_node(_jac_here_, _Jac.edge_ref(self, _Jac.EdgeDir.OUT, None, None)): + print("visiting ", self) + if self.val == 2 and str(_jac_here_) == "Travellor()": + print("visiting again a specific node by w2\n") + if _Jac.visit_node( + _jac_here_, _Jac.edge_ref(self, _Jac.EdgeDir.OUT, None, None) + ): pass else: - print('finished visitng all nodes ....\n') + print("finished visitng all nodes ....\n") + + _Jac.spawn_call(_Jac.get_root(), creator()) -_Jac.spawn_call(_Jac.get_root(), Travellor()) \ No newline at end of file +_Jac.spawn_call(_Jac.get_root(), Travellor()) From 8b22aeecb1a633670059e5e0353f4b93ac8f07fc Mon Sep 17 00:00:00 2001 From: marsninja Date: Mon, 29 Jan 2024 09:30:11 -0500 Subject: [PATCH 5/9] feat: all has variables are object unless explicitly static --- examples/reference/special_comprehensions.jac | 34 +++++++++---------- .../compiler/passes/main/pyast_gen_pass.py | 25 ++++++++++---- jaclang/plugin/default.py | 4 +-- jaclang/plugin/feature.py | 4 +-- jaclang/plugin/spec.py | 2 +- support/jac-lang.org/docs/learn/jac_ref.md | 34 +++++++++---------- 6 files changed, 57 insertions(+), 46 deletions(-) diff --git a/examples/reference/special_comprehensions.jac b/examples/reference/special_comprehensions.jac index cc0780e48..5a5c746dc 100644 --- a/examples/reference/special_comprehensions.jac +++ b/examples/reference/special_comprehensions.jac @@ -2,36 +2,34 @@ import:py random; obj TestObj { - can (){ - self.x = random.randint(0,15); - self.y =random.randint(0,15); - self.z = random.randint(0,15); + has x: int = random.randint(0, 15), + y: int = random.randint(0, 15), + z: int = random.randint(0, 15); + + can { + print(.x, .y, .z); } - has x:int = random.randint(0,15), - y:int = random.randint(0,20), - z:int = random.randint(0,50); } -with entry{ +with entry { random.seed(42); apple = []; - for i=0 to i<10 by i+=1{ + for i=0 to i<10 by i+=1 { apple.append(TestObj()); } #print(apple); - print (apple(= y <= 7)); - + print(apple(=y <= 7)); } - #assign comprehension + obj MyObj { - has apple:int=0, banana:int=0; + has apple: int = 0, + banana: int = 0; } with entry { - x=MyObj(); - y=MyObj(); - - mvar = [x, y](* apple=5, banana=7); - print(mvar); + x = MyObj(); + y = MyObj(); + mvar = [x, y](*apple=5, banana=7); + print(mvar); } diff --git a/jaclang/compiler/passes/main/pyast_gen_pass.py b/jaclang/compiler/passes/main/pyast_gen_pass.py index 6fac1990f..55a47369b 100644 --- a/jaclang/compiler/passes/main/pyast_gen_pass.py +++ b/jaclang/compiler/passes/main/pyast_gen_pass.py @@ -905,7 +905,7 @@ def exit_has_var(self, node: ast.HasVar) -> None: value=self.sync( ast3.Name(id=Con.JAC_FEATURE.value, ctx=ast3.Load()) ), - attr="has_container_default", + attr="has_instance_default", ctx=ast3.Load(), ) ), @@ -913,16 +913,29 @@ def exit_has_var(self, node: ast.HasVar) -> None: keywords=[ self.sync( ast3.keyword( - arg="container", - value=node.value.gen.py_ast, + arg="gen_func", + value=self.sync( + ast3.Lambda( + args=self.sync( + ast3.arguments( + posonlyargs=[], + args=[], + kwonlyargs=[], + vararg=None, + kwargs=None, + kw_defaults=[], + defaults=[], + ) + ), + body=node.value.gen.py_ast, + ) + ), ) ) ], ) ) - if node.value - and not is_class_var - and isinstance(node.value.gen.py_ast, (ast3.List, ast3.Dict)) + if node.value and not is_class_var else node.value.gen.py_ast if node.value else None, diff --git a/jaclang/plugin/default.py b/jaclang/plugin/default.py index 9c6f2e51d..fa9ebe368 100644 --- a/jaclang/plugin/default.py +++ b/jaclang/plugin/default.py @@ -77,9 +77,9 @@ def elvis(op1: Optional[T], op2: T) -> T: @staticmethod @hookimpl - def has_container_default(container: list | dict) -> list[Any] | dict[Any, Any]: + def has_instance_default(gen_func: Callable) -> list[Any] | dict[Any, Any]: """Jac's has container default feature.""" - return field(default_factory=lambda: container) + return field(default_factory=lambda: gen_func()) @staticmethod @hookimpl diff --git a/jaclang/plugin/feature.py b/jaclang/plugin/feature.py index 927afca9b..42ed6f51c 100644 --- a/jaclang/plugin/feature.py +++ b/jaclang/plugin/feature.py @@ -46,9 +46,9 @@ def elvis(op1: Optional[T], op2: T) -> T: return JacFeature.pm.hook.elvis(op1=op1, op2=op2) @staticmethod - def has_container_default(container: list | dict) -> list[Any] | dict[Any, Any]: + def has_instance_default(gen_func: Callable) -> list[Any] | dict[Any, Any]: """Jac's has container default feature.""" - return JacFeature.pm.hook.has_container_default(container=container) + return JacFeature.pm.hook.has_instance_default(gen_func=gen_func) @staticmethod def spawn_call(op1: Architype, op2: Architype) -> Architype: diff --git a/jaclang/plugin/spec.py b/jaclang/plugin/spec.py index 0cab0b3bd..605b4b909 100644 --- a/jaclang/plugin/spec.py +++ b/jaclang/plugin/spec.py @@ -64,7 +64,7 @@ def elvis(op1: Optional[T], op2: T) -> T: @staticmethod @hookspec(firstresult=True) - def has_container_default(container: list | dict) -> list[Any] | dict[Any, Any]: + def has_instance_default(gen_func: Callable) -> list[Any] | dict[Any, Any]: """Jac's has container default feature.""" raise NotImplementedError diff --git a/support/jac-lang.org/docs/learn/jac_ref.md b/support/jac-lang.org/docs/learn/jac_ref.md index 8597516fb..f6d461330 100644 --- a/support/jac-lang.org/docs/learn/jac_ref.md +++ b/support/jac-lang.org/docs/learn/jac_ref.md @@ -358,55 +358,55 @@ ## Atom ```yaml linenums="344" ---8<-- "jaclang/compiler/jac.lark:344:362" +--8<-- "jaclang/compiler/jac.lark:344:365" ``` --8<-- "examples/reference/atom.md" ## Collection values -```yaml linenums="365" ---8<-- "jaclang/compiler/jac.lark:365:386" +```yaml linenums="368" +--8<-- "jaclang/compiler/jac.lark:368:389" ``` --8<-- "examples/reference/collection_values.md" ## Tuples and Jac Tuples -```yaml linenums="389" ---8<-- "jaclang/compiler/jac.lark:389:396" +```yaml linenums="392" +--8<-- "jaclang/compiler/jac.lark:392:399" ``` --8<-- "examples/reference/tuples_and_jac_tuples.md" ## Data Spatial References -```yaml linenums="399" ---8<-- "jaclang/compiler/jac.lark:399:409" +```yaml linenums="402" +--8<-- "jaclang/compiler/jac.lark:402:412" ``` --8<-- "examples/reference/data_spatial_references.md" ## Special Comprehensions -```yaml linenums="412" ---8<-- "jaclang/compiler/jac.lark:412:415" +```yaml linenums="415" +--8<-- "jaclang/compiler/jac.lark:415:418" ``` --8<-- "examples/reference/special_comprehensions.md" ## Names and references -```yaml linenums="418" ---8<-- "jaclang/compiler/jac.lark:418:444" +```yaml linenums="421" +--8<-- "jaclang/compiler/jac.lark:421:447" ``` --8<-- "examples/reference/names_and_references.md" ## Builtin types -```yaml linenums="447" ---8<-- "jaclang/compiler/jac.lark:447:457" +```yaml linenums="450" +--8<-- "jaclang/compiler/jac.lark:450:460" ``` --8<-- "examples/reference/builtin_types.md" ## Lexer Tokens -```yaml linenums="460" ---8<-- "jaclang/compiler/jac.lark:460:628" +```yaml linenums="463" +--8<-- "jaclang/compiler/jac.lark:463:631" ``` --8<-- "examples/reference/lexer_tokens.md" ## f-string tokens -```yaml linenums="631" ---8<-- "jaclang/compiler/jac.lark:631:639" +```yaml linenums="634" +--8<-- "jaclang/compiler/jac.lark:634:644" ``` --8<-- "examples/reference/f_string_tokens.md" From 41580aef1c329789c54caba7a65ed4d903623000 Mon Sep 17 00:00:00 2001 From: marsninja Date: Mon, 29 Jan 2024 09:33:45 -0500 Subject: [PATCH 6/9] test: All passing --- .../data_spatial_walker_statements.jac | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/examples/reference/data_spatial_walker_statements.jac b/examples/reference/data_spatial_walker_statements.jac index effdbb601..508b945cb 100644 --- a/examples/reference/data_spatial_walker_statements.jac +++ b/examples/reference/data_spatial_walker_statements.jac @@ -1,31 +1,36 @@ -walker walker_1{ +walker walker_1 { can create with ` entry; } -node node_a{ - has val:int; + +node node_a { + has val: int; + can print_something with walker_1 entry; } -walker walker_2{ - can skip_root with ` entry { - visit-->; + +walker walker_2 { + can skip_root with ` entry { + visit -->; } - can do_something with node_a entry; + + can do_something with node_a entry {} } #creating five nodes -:walker:walker_1:can:create{ + +:walker:walker_1:can:create { end = ; - for i=0 to i<5 by i+=1 { - end ++> end := node_a(val=i+1); + for i=0 to i<5 by i+=1 { + end ++> end := node_a(val=i + 1); } visit -->; } -:node:node_a:can:print_something -> str{ +:node:node_a:can:print_something -> str { print(f"walker_1 entered to {}"); visit -->; } -with entry{ - walker_1() spawn ; #walker_1 statrt walking from root node - spawn walker_2(); #after walker_1 finish walking walker_2 start walking to nodes -} \ No newline at end of file +with entry { + walker_1() spawn ; #walker_1 statrt walking from root node + spawn walker_2(); #after walker_1 finish walking walker_2 start walking to nodes +} From 09a52cc5f2517e8ea42c0f7aa88f608caa6fd147 Mon Sep 17 00:00:00 2001 From: marsninja Date: Mon, 29 Jan 2024 09:59:20 -0500 Subject: [PATCH 7/9] fix: walrus precedence --- jaclang/compiler/jac.lark | 13 +++++++------ jaclang/compiler/parser.py | 21 ++++++++++----------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/jaclang/compiler/jac.lark b/jaclang/compiler/jac.lark index 8de696a71..592285e5d 100644 --- a/jaclang/compiler/jac.lark +++ b/jaclang/compiler/jac.lark @@ -258,7 +258,11 @@ aug_op: RSHIFT_EQ | STAR_POW_EQ // Expressions -expression: pipe (KW_IF expression KW_ELSE expression)? | lambda_expr +expression: walrus_assign (KW_IF expression KW_ELSE expression)? + | lambda_expr + +// Walrus assignments +walrus_assign: (walrus_assign WALRUS_EQ)? pipe // Lambda expressions lambda_expr: KW_WITH func_decl_params? (RETURN_HINT expression)? KW_CAN expression @@ -317,10 +321,7 @@ ds_spawn: (ds_spawn KW_SPAWN)? unpack unpack: (STAR_MUL | STAR_POW) unpack | ref // References (unused) -ref: BW_AND? walrus_assign - -// Walrus assignments -walrus_assign: (walrus_assign WALRUS_EQ)? pipe_call +ref: BW_AND? pipe_call // Data spatial calls pipe_call: (PIPE_FWD | A_PIPE_FWD | KW_SPAWN | KW_AWAIT)? atomic_chain @@ -378,7 +379,7 @@ list_compr: LSQUARE expression inner_compr+ RSQUARE gen_compr: LPAREN expression inner_compr+ RPAREN set_compr: LBRACE expression inner_compr+ RBRACE dict_compr: LBRACE kv_pair inner_compr+ RBRACE -inner_compr: KW_ASYNC? KW_FOR atomic_chain KW_IN walrus_assign (KW_IF expression)? +inner_compr: KW_ASYNC? KW_FOR atomic_chain KW_IN pipe_call (KW_IF expression)? dict_val: LBRACE ((kv_pair COMMA)* kv_pair)? RBRACE list_val: LSQUARE expr_list? RSQUARE diff --git a/jaclang/compiler/parser.py b/jaclang/compiler/parser.py index 38893a3e2..170081892 100644 --- a/jaclang/compiler/parser.py +++ b/jaclang/compiler/parser.py @@ -1722,9 +1722,9 @@ def assignment(self, kid: list[ast.AstNode]) -> ast.Assignment: def expression(self, kid: list[ast.AstNode]) -> ast.Expr: """Grammar rule. - expression: pipe KW_IF expression KW_ELSE expression - | pipe - | lamda_expr + expression: walrus_assign + | pipe (KW_IF expression KW_ELSE expression)? + | lambda_expr """ if len(kid) > 1: if ( @@ -1747,6 +1747,13 @@ def expression(self, kid: list[ast.AstNode]) -> ast.Expr: else: raise self.ice() + def walrus_assign(self, kid: list[ast.AstNode]) -> ast.Expr: + """Grammar rule. + + walrus_assign: (walrus_assign WALRUS_EQ)? pipe + """ + return self.binary_expr_unwind(kid) + def binary_expr_unwind(self, kid: list[ast.AstNode]) -> ast.Expr: """Binary expression helper.""" if len(kid) > 1: @@ -2064,14 +2071,6 @@ def ref(self, kid: list[ast.AstNode]) -> ast.Expr: raise self.ice() return self.binary_expr_unwind(kid) - def walrus_assign(self, kid: list[ast.AstNode]) -> ast.Expr: - """Grammar rule. - - walrus_assign: pipe_call walrus_op walrus_assign - | pipe_call - """ - return self.binary_expr_unwind(kid) - def pipe_call(self, kid: list[ast.AstNode]) -> ast.Expr: """Grammar rule. From a136281fd0deea0f91e239dc185091fe53db0f3a Mon Sep 17 00:00:00 2001 From: marsninja Date: Mon, 29 Jan 2024 10:22:44 -0500 Subject: [PATCH 8/9] fix: walrus is now pythonic --- examples/guess_game/guess_game5.jac | 11 +++++++---- examples/reference/walrus_assignments.jac | 8 ++++---- jaclang/compiler/jac.lark | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/guess_game/guess_game5.jac b/examples/guess_game/guess_game5.jac index eb1c09f07..0b04f60f9 100644 --- a/examples/guess_game/guess_game5.jac +++ b/examples/guess_game/guess_game5.jac @@ -1,8 +1,10 @@ """A Number Guessing Game""" + import:py random; walker GuessGame { has correct_number: int = (1, 100) |> random.randint; + can start_game with ` entry; can process_guess(guess: int); } @@ -23,13 +25,14 @@ node turn { :walker:GuessGame:can:start_game { end = ; - for i=0 to i<10 by i+=1 { - end ++> end := :> turn; + for i=0 to i<10 by i+=1 { + end ++> (end := turn()); } visit -->; } -:walker:GuessGame:can:process_guess(guess: int) { +:walker:GuessGame:can:process_guess +(guess: int) { if guess > .correct_number { "Too high!" |> print; } elif guess < .correct_number { @@ -39,8 +42,8 @@ node turn { disengage; } } - # # Run the game + with entry { :> GuessGame spawn ; } diff --git a/examples/reference/walrus_assignments.jac b/examples/reference/walrus_assignments.jac index 6c0718fb5..44a6c0512 100644 --- a/examples/reference/walrus_assignments.jac +++ b/examples/reference/walrus_assignments.jac @@ -1,8 +1,8 @@ -with entry{ +with entry { numbers = [1, 2, 3, 4, 5]; - for i in numbers{ - if (j := (i + (i//2))) > 3{ + for i in numbers { + if (j := i + i // 2) > 3 { print(j); } } -} \ No newline at end of file +} diff --git a/jaclang/compiler/jac.lark b/jaclang/compiler/jac.lark index 592285e5d..5d97f02c8 100644 --- a/jaclang/compiler/jac.lark +++ b/jaclang/compiler/jac.lark @@ -262,7 +262,7 @@ expression: walrus_assign (KW_IF expression KW_ELSE expression)? | lambda_expr // Walrus assignments -walrus_assign: (walrus_assign WALRUS_EQ)? pipe +walrus_assign: (any_ref WALRUS_EQ)? pipe // Lambda expressions lambda_expr: KW_WITH func_decl_params? (RETURN_HINT expression)? KW_CAN expression From 97b90489d129f5df02958021100344ba8f0139e1 Mon Sep 17 00:00:00 2001 From: marsninja Date: Mon, 29 Jan 2024 10:30:10 -0500 Subject: [PATCH 9/9] test: all passing --- examples/reference/connect_expressions.jac | 41 ++++--- examples/reference/data_spatial_calls.jac | 35 +++--- .../data_spatial_walker_statements.jac | 2 +- examples/reference/disengage_statements.jac | 45 ++++---- examples/reference/visit_statements.jac | 66 ++++++----- jaclang/tests/fixtures/edge_ops.jac | 44 +++---- jaclang/tests/fixtures/gendot_bubble_sort.jac | 108 ++++++++++-------- jaclang/tests/fixtures/guess_game.jac | 11 +- 8 files changed, 199 insertions(+), 153 deletions(-) diff --git a/examples/reference/connect_expressions.jac b/examples/reference/connect_expressions.jac index 38c32679e..c040e3ae4 100644 --- a/examples/reference/connect_expressions.jac +++ b/examples/reference/connect_expressions.jac @@ -1,31 +1,36 @@ -node node_a{ - has value:int; +node node_a { + has value: int; } + walker Creator { can create with ` entry; - can travel with `|node_a entry; + can travel with ` | node_a entry; } + edge MyEdge { - has val:int =5; + has val: int = 5; } -:walker:Creator:can:create{ + +:walker:Creator:can:create { end = ; - for i = 0 to i<7 by i+=1{ - if i % 2==0{ - end ++> end := node_a(value=i); - } - else{ - end +:MyEdge:val=i:+> end := node_a(value=i+10); + for i=0 to i<7 by i+=1 { + if i % 2 == 0 { + end ++> (end := node_a(value=i)); + } else { + end +:MyEdge:val=i:+> (end := node_a(value=i + 10)); } } } -:walker:Creator:can:travel{ - for i in -:MyEdge:val<=6 :->{ - print(i.value); - } - visit-->; + +:walker:Creator:can:travel { + for i in -:MyEdge:val <= 6:-> { + print(i.value); + } + visit -->; } -with entry{ + +with entry { spawn Creator(); + # print(._jac_.gen_dot()); -} \ No newline at end of file +} diff --git a/examples/reference/data_spatial_calls.jac b/examples/reference/data_spatial_calls.jac index ed73d4dba..26aaa0d7c 100644 --- a/examples/reference/data_spatial_calls.jac +++ b/examples/reference/data_spatial_calls.jac @@ -1,24 +1,27 @@ -walker walker_1{ - can func2 with ` entry; +walker walker_1 { + can func2 with ` entry; } -node node_1{ - has val :int; - can func_1 with walker_1 entry; + +node node_1 { + has val: int; + + can func_1 with walker_1 entry; } -:node:node_1:can:func_1{ - print("visiting ",); - visit-->; - } -:walker:walker_1:can:func2{ + +:node:node_1:can:func_1 { + print("visiting ", ); + visit -->; +} + +:walker:walker_1:can:func2 { end = ; - for i=0 to i<5 by i+=1 { - end ++> end := node_1(val=i+1); + for i=0 to i<5 by i+=1 { + end ++> (end := node_1(val=i + 1)); } visit -->; } -with entry{ - spawn |> walker_1 ; - spawn :> walker_1 ; - +with entry { + spawn |> walker_1; + spawn :> walker_1; } diff --git a/examples/reference/data_spatial_walker_statements.jac b/examples/reference/data_spatial_walker_statements.jac index 508b945cb..08f5fce3d 100644 --- a/examples/reference/data_spatial_walker_statements.jac +++ b/examples/reference/data_spatial_walker_statements.jac @@ -20,7 +20,7 @@ walker walker_2 { :walker:walker_1:can:create { end = ; for i=0 to i<5 by i+=1 { - end ++> end := node_a(val=i + 1); + end ++> (end := node_a(val=i + 1)); } visit -->; } diff --git a/examples/reference/disengage_statements.jac b/examples/reference/disengage_statements.jac index 0f6212f97..4a39e78a7 100644 --- a/examples/reference/disengage_statements.jac +++ b/examples/reference/disengage_statements.jac @@ -1,28 +1,33 @@ -walker walker_1{ - can func2 with ` entry; +walker walker_1 { + can func2 with ` entry; } -node node_1{ - has val :int; - can func_1 with walker_1 entry; + +node node_1 { + has val: int; + + can func_1 with walker_1 entry; } -:node:node_1:can:func_1{ - print("visiting ",); - if .val==3{ - print("Disengaging traversal in node with value 3."); - disengage; - } - visit-->else{ - print("finished visitng all nodes ....\n"); - }; - } -:walker:walker_1:can:func2{ + +:node:node_1:can:func_1 { + print("visiting ", ); + if .val == 3 { + print("Disengaging traversal in node with value 3."); + disengage; + } + visit --> else { + print("finished visitng all nodes ....\n"); + } + ; +} + +:walker:walker_1:can:func2 { end = ; - for i=0 to i<5 by i+=1 { - end ++> end := node_1(val=i+1); + for i=0 to i<5 by i+=1 { + end ++> (end := node_1(val=i + 1)); } visit -->; } -with entry{ - spawn walker_1(); +with entry { + spawn walker_1(); } diff --git a/examples/reference/visit_statements.jac b/examples/reference/visit_statements.jac index a4bd7e0d3..d14c659bb 100644 --- a/examples/reference/visit_statements.jac +++ b/examples/reference/visit_statements.jac @@ -1,62 +1,73 @@ #version1 -walker creator{ - can create with ` entry; +walker creator { + can create with ` entry; } -walker Travellor{ + +walker Travellor { can func_3 with ` entry; } -node item{ - has val :int; - can func_1 with creator|Travellor - entry; + +node item { + has val: int; + + can func_1 with creator | Travellor entry; } -:node:item:can:func_1{ - print("visiting ",); - if .val==2 and str()== 'Travellor()'{ - print("visiting again a specific node by w2\n"); - } - visit-->else{ - print("finished visitng all nodes ....\n"); - }; - } -:walker:creator:can:create{ + +:node:item:can:func_1 { + print("visiting ", ); + if .val == 2 and str() == 'Travellor()' { + print("visiting again a specific node by w2\n"); + } + visit --> else { + print("finished visitng all nodes ....\n"); + } + ; +} + +:walker:creator:can:create { end = ; - for i=0 to i<5 by i+=1 { - end ++> end := item(val=i+1); + for i=0 to i<5 by i+=1 { + end ++> (end := item(val=i + 1)); } visit -->; } -:walker:Travellor:can:func_3{ - visit(item(val=2)); -} -with entry{ - spawn creator(); - spawn Travellor(); +:walker:Travellor:can:func_3 { + visit(item(val=2)); } +with entry { + spawn creator(); + spawn Travellor(); +} # #version2 # #1 # walker creator{ # can create with ` entry; # } + # node item{ # has val :int; # can func_1 with creator entry; # } + # :walker:creator:can:create{ # end = ; # for i=0 to i<5 by i+=1 { # end ++> end := item(val=i+1); # } + # visit(item(val=2)); # } + # :node:item:can:func_1{ # print("visiting ",); # } + # with entry{ # spawn creator(); # } + # #2 # walker creator{ # can create with ` entry; @@ -66,22 +77,23 @@ with entry{ # has val :int; # can func_1 with creator entry; # } + # :node:item:can:func_1{ # print("visiting ",); - # visit-->else{ # print("finished visitng all nodes ....\n"); # }; # } + # :walker:creator:can:create{ # end = ; # for i=0 to i<5 by i+=1 { # end ++> end := item(val=i+1); # } + # visit -->; # } - # with entry{ # spawn creator(); # } diff --git a/jaclang/tests/fixtures/edge_ops.jac b/jaclang/tests/fixtures/edge_ops.jac index 5eea5633f..1f1f7b1b7 100644 --- a/jaclang/tests/fixtures/edge_ops.jac +++ b/jaclang/tests/fixtures/edge_ops.jac @@ -1,43 +1,45 @@ import:py random; -node node_a{ - has value:int; +node node_a { + has value: int; } walker Creator { can create with ` entry; - can travel with `|node_a entry; + can travel with ` | node_a entry; } edge MyEdge { - has val:int =5, - val2:int = 10; + has val: int = 5, + val2: int = 10; } -:walker:Creator:can:create{ +:walker:Creator:can:create { end = ; - for i = 0 to i<5 by i+=1{ - end ++> end := node_a(value=i+1); - if i == 2{ - for j=0 to j<3 by j+=1 { - end +:MyEdge:val=random.randint(1,15), val2=random.randint(1,5):+> node_a(value=j+10); + for i=0 to i<5 by i+=1 { + end ++> (end := node_a(value=i + 1)); + if i == 2 { + for j=0 to j<3 by j+=1 { + end +:MyEdge:val=random.randint(1, 15), val2=random.randint(1, 5):+> node_a(value=j + 10); } - print([(i.val, i.val2) for i in list(end._jac_.edges.values())[1]]); + print([(i.val, i.val2) for i in list(end._jac_.edges.values())[1]]); } } - for i=0 to i<3 by i+=1 { - end +:MyEdge:val=random.randint(1,20):+> node_a(value=i+5); + for i=0 to i<3 by i+=1 { + end +:MyEdge:val=random.randint(1, 20):+> node_a(value=i + 5); } } -:walker:Creator:can:travel{ - for i in -:MyEdge:val<=5:-> { - print(i.value); - } - visit-->; + +:walker:Creator:can:travel { + for i in -:MyEdge:val <= 5:-> { + print(i.value); + } + visit -->; } -with entry{ +with entry { random.seed(1); spawn Creator(); + #print(._jac_.gen_dot()); -} \ No newline at end of file +} diff --git a/jaclang/tests/fixtures/gendot_bubble_sort.jac b/jaclang/tests/fixtures/gendot_bubble_sort.jac index 68f93f177..37f5508c4 100644 --- a/jaclang/tests/fixtures/gendot_bubble_sort.jac +++ b/jaclang/tests/fixtures/gendot_bubble_sort.jac @@ -1,61 +1,77 @@ """Bubble sort using DS Features of Jac (somparision and swapping happens in inner nodes).""" -glob list1=[80,79,60,59,40,35,19,1]; -node main_node{ - has no: int =0; -} -node inner_node{ - has main:int=0,sub:int=0; - } -walker walker1{ - can create_main_node with `entry ; + +glob list1 = [80, 79, 60, 59, 40, 35, 19, 1]; + +node main_node { + has no: int = 0; +} + +node inner_node { + has main: int = 0, + sub: int = 0; +} + +walker walker1 { + can create_main_node with ` entry; +} + +:walker:walker1:can:create_main_node { + end = ; + for i=0 to i (end := main_node(no=i + 1)); + visit -->; } - :walker:walker1:can:create_main_node{ - end=; - for i=0 to i end:=main_node(no=i+1); +} + +walker walker2 { + can skip_root with ` entry { visit -->; - } - } - walker walker2{ - can skip_root with ` entry { visit -->; } - can process with main_node entry ; - } -:walker:walker2:can:process{ - :g: list1; - for j in range(0, len(list1)-(.no)){ - ++> xx:=inner_node(main=.no,sub=j+1); - } - visit-->; -} - -walker walker3{ - can skiproot with ` entry { visit -->; } - can adjust with main_node entry ; -} -:walker:walker3:can:adjust{ + } + can process with main_node entry; +} + +:walker:walker2:can:process { + :g: list1 ; + + for j in range(0, len(list1) - (.no)) { + ++> inner_node(main=.no, sub=j + 1); + } + visit -->; +} + +walker walker3 { + can skiproot with ` entry { + visit -->; + } + can adjust with main_node entry; +} + +:walker:walker3:can:adjust { spawn walker4(); } -walker walker4{ + +walker walker4 { can skipmain with main_node entry { - visit-->; - # print(f"iteration {.no} started {list1}"); + visit -->;# print(f"iteration {.no} started {list1}"); + } - } can skipin with inner_node entry { - :g: list1; - j=.sub-1; - if list1[j] > list1[j + 1]{ - x= list1[j]; - list1[j]=list1[j + 1]; list1[j + 1] =x; - } - #uncomment below to see the swap in each inner nodes - # print(list1); + :g: list1 ; + + j = .sub - 1; + if list1[j] > list1[j + 1] { + x = list1[j]; + list1[j] = list1[j + 1]; + list1[j + 1] = x; } + #uncomment below to see the swap in each inner nodes + # print(list1); + } } -with entry{ +with entry { spawn walker1(); spawn walker2(); spawn walker3(); print(._jac_.gen_dot()); -} \ No newline at end of file +} diff --git a/jaclang/tests/fixtures/guess_game.jac b/jaclang/tests/fixtures/guess_game.jac index aa0e7728a..b0677f735 100644 --- a/jaclang/tests/fixtures/guess_game.jac +++ b/jaclang/tests/fixtures/guess_game.jac @@ -1,10 +1,12 @@ """A Number Guessing Game""" + import:py random; glob guesses = [50, 25, 35, 30]; walker GuessGame { has correct_number: int = 30; + can start_game with ` entry; can process_guess(guess: int); } @@ -21,13 +23,14 @@ node turn { :walker:GuessGame:can:start_game { end = ; - for i=0 to i<10 by i+=1 { - end ++> end := :> turn; + for i=0 to i<10 by i+=1 { + end ++> (end := :> turn); } visit -->; } -:walker:GuessGame:can:process_guess(guess: int) { +:walker:GuessGame:can:process_guess +(guess: int) { if guess > .correct_number { "Too high!" |> print; } elif guess < .correct_number { @@ -37,8 +40,8 @@ node turn { disengage; } } - # # Run the game + with entry { spawn GuessGame(); }