From c67490b382422edded3fcc640f20cff8677e3c6f Mon Sep 17 00:00:00 2001 From: Sarujan1999 Date: Tue, 30 Jan 2024 12:23:33 +0530 Subject: [PATCH 1/6] Examples : refactroing --- examples/reference/assert_statements.jac | 1 - .../atomic_pipe_back_expressions.jac | 2 +- examples/reference/connect_expressions.py | 55 +++++++++++++++++++ examples/reference/data_spatial_calls.jac | 11 ++-- examples/reference/data_spatial_calls.py | 20 ++++--- .../data_spatial_spawn_expressions.jac | 7 --- .../data_spatial_spawn_expressions.py | 10 ++-- .../data_spatial_walker_statements.jac | 48 ++++++---------- .../data_spatial_walker_statements.py | 54 ++++++------------ examples/reference/delete_statements.jac | 5 +- examples/reference/delete_statements.py | 6 +- examples/reference/f_string_tokens.py | 17 ++---- examples/reference/if_statements.jac | 16 +++--- examples/reference/if_statements.py | 8 +-- examples/reference/walrus_assignments.jac | 10 ++-- examples/reference/walrus_assignments.py | 7 +-- 16 files changed, 143 insertions(+), 134 deletions(-) diff --git a/examples/reference/assert_statements.jac b/examples/reference/assert_statements.jac index 826a1f107..c9c92c1c0 100644 --- a/examples/reference/assert_statements.jac +++ b/examples/reference/assert_statements.jac @@ -1,6 +1,5 @@ can foo(value : int){ assert value > 0, "Value must be positive"; - } with entry{ diff --git a/examples/reference/atomic_pipe_back_expressions.jac b/examples/reference/atomic_pipe_back_expressions.jac index c08407660..77f5deb7f 100644 --- a/examples/reference/atomic_pipe_back_expressions.jac +++ b/examples/reference/atomic_pipe_back_expressions.jac @@ -2,7 +2,7 @@ with entry { print <: "Hello world!" ; a = [2,4,5,7,8]; b = [4,8,9,13,20]; - c = len<:a + len<:b ; + c = len<:a + b:>len; print(c); } \ No newline at end of file diff --git a/examples/reference/connect_expressions.py b/examples/reference/connect_expressions.py index e69de29bb..cd1f68bcf 100644 --- a/examples/reference/connect_expressions.py +++ b/examples/reference/connect_expressions.py @@ -0,0 +1,55 @@ +from jaclang.plugin.feature import JacFeature as jac + + +@jac.make_architype("node", on_entry=[], on_exit=[]) +class node_a: + value: int + + +@jac.make_architype( + "walker", + on_entry=[ + jac.DSFunc("create", jac.RootType), + jac.DSFunc("travel", jac.RootType | node_a), + ], + on_exit=[], +) +class Creator: + def create(self, creator_here: jac.RootType) -> None: + end = creator_here + i = 0 + while i < 7: + if i % 2 == 0: + jac.connect( + end, + (end := node_a(value=i)), + jac.build_edge(jac.EdgeDir.OUT, None, None), + ) + else: + jac.connect( + end, + (end := node_a(value=i + 10)), + jac.build_edge(jac.EdgeDir.OUT, MyEdge, (("val",), (i,))), + ) + i += 1 + + def travel(self, creator_here: jac.RootType | node_a) -> None: + for i in jac.edge_ref( + creator_here, + jac.EdgeDir.OUT, + MyEdge, + lambda x: [i for i in x if i.val <= 6], + ): + print(i.value) + if jac.visit_node( + self, jac.edge_ref(creator_here, jac.EdgeDir.OUT, None, None) + ): + pass + + +@jac.make_architype("edge", on_entry=[], on_exit=[]) +class MyEdge: + val: int = jac.has_instance_default(gen_func=lambda: 5) + + +jac.spawn_call(jac.get_root(), Creator()) diff --git a/examples/reference/data_spatial_calls.jac b/examples/reference/data_spatial_calls.jac index 26aaa0d7c..04fbab336 100644 --- a/examples/reference/data_spatial_calls.jac +++ b/examples/reference/data_spatial_calls.jac @@ -1,11 +1,10 @@ -walker walker_1 { +walker Creator { can func2 with ` entry; } node node_1 { has val: int; - - can func_1 with walker_1 entry; + can func_1 with Creator entry; } :node:node_1:can:func_1 { @@ -13,7 +12,7 @@ node node_1 { visit -->; } -:walker:walker_1:can:func2 { +:walker:Creator:can:func2 { end = ; for i=0 to i<5 by i+=1 { end ++> (end := node_1(val=i + 1)); @@ -22,6 +21,6 @@ node node_1 { } with entry { - spawn |> walker_1; - spawn :> walker_1; + spawn :>Creator; + spawn |>Creator; } diff --git a/examples/reference/data_spatial_calls.py b/examples/reference/data_spatial_calls.py index 349522628..ee1b701d3 100644 --- a/examples/reference/data_spatial_calls.py +++ b/examples/reference/data_spatial_calls.py @@ -3,9 +3,9 @@ @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 +class Creator: + def func2(self, creator_here: jac.RootType) -> None: + end = creator_here i = 0 while i < 5: jac.connect( @@ -14,19 +14,21 @@ def func2(self, walker_here: jac.RootType) -> None: 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(creator_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", Creator)], on_exit=[]) class node_1: val: int - def func_1(self, node_here: walker_1) -> None: + def func_1(self, node_here: Creator) -> None: print("visiting ", self) - if jac.visit_node(node_here, jac.edge_ref(self, jac.EdgeDir.OUT, None)): + if jac.visit_node(node_here, jac.edge_ref(self, jac.EdgeDir.OUT, None, None)): pass -jac.spawn_call(jac.get_root(), walker_1()) -jac.spawn_call(jac.get_root(), walker_1()) +jac.spawn_call(jac.get_root(), Creator()) +jac.spawn_call(jac.get_root(), Creator()) diff --git a/examples/reference/data_spatial_spawn_expressions.jac b/examples/reference/data_spatial_spawn_expressions.jac index b8f978411..fabc4966b 100644 --- a/examples/reference/data_spatial_spawn_expressions.jac +++ b/examples/reference/data_spatial_spawn_expressions.jac @@ -1,27 +1,20 @@ walker Adder{ can do with ` entry; } - node node_a{ has x:int = 0; has y:int = 0; can add with Adder entry; } - :walker:Adder:can:do{ ++> node_a(); - visit -->; - } - :node:node_a:can:add{ .x= 550 ; .y= 450 ; print(int(.x) + int(.y)); - } - with entry{ Adder() spawn ; # spawn will iniiate the walker Adder from root node } \ No newline at end of file diff --git a/examples/reference/data_spatial_spawn_expressions.py b/examples/reference/data_spatial_spawn_expressions.py index a8d741731..1db9b4275 100644 --- a/examples/reference/data_spatial_spawn_expressions.py +++ b/examples/reference/data_spatial_spawn_expressions.py @@ -3,16 +3,16 @@ @jac.make_architype("walker", on_entry=[jac.DSFunc("do", jac.RootType)], on_exit=[]) class Adder: - def do(self, walker_here: jac.RootType) -> None: - jac.connect(walker_here, node_a(), jac.build_edge(jac.EdgeDir.OUT, None, None)) - if jac.visit_node(self, jac.edge_ref(walker_here, jac.EdgeDir.OUT, None)): + def do(self, Adder_here: jac.RootType) -> None: + jac.connect(Adder_here, node_a(), jac.build_edge(jac.EdgeDir.OUT, None, None)) + if jac.visit_node(self, jac.edge_ref(Adder_here, jac.EdgeDir.OUT, None, None)): pass @jac.make_architype("node", on_entry=[jac.DSFunc("add", Adder)], on_exit=[]) class node_a: - x: int = 0 - y: int = 0 + x: int = jac.has_instance_default(gen_func=lambda: 0) + y: int = jac.has_instance_default(gen_func=lambda: 0) def add(self, node_here: Adder) -> None: self.x = 550 diff --git a/examples/reference/data_spatial_walker_statements.jac b/examples/reference/data_spatial_walker_statements.jac index 08f5fce3d..4ffc04582 100644 --- a/examples/reference/data_spatial_walker_statements.jac +++ b/examples/reference/data_spatial_walker_statements.jac @@ -1,36 +1,24 @@ -walker walker_1 { +walker Creator{ can create with ` entry; } - -node node_a { - has val: int; - - can print_something with walker_1 entry; -} - -walker walker_2 { - can skip_root with ` entry { - visit -->; +node leaf{ + has val:int; + can print with Creator entry{ + print(f"This is {} walk along {}"); + if .val == 4{ + print("walking stopped!!!"); + disengage; + } + visit-->; } - - can do_something with node_a entry {} } -#creating five nodes - -:walker:walker_1:can:create { +:walker:Creator:can:create{ end = ; - for i=0 to i<5 by i+=1 { - end ++> (end := node_a(val=i + 1)); - } - visit -->; -} - -: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 + for i = 0 to i<=4 by i+=1{ + end ++> (end := leaf(val=i + 1)); + } + visit -->; } +with entry{ + spawn Creator(); +} \ No newline at end of file diff --git a/examples/reference/data_spatial_walker_statements.py b/examples/reference/data_spatial_walker_statements.py index b0c7e2b55..0dbdc3c57 100644 --- a/examples/reference/data_spatial_walker_statements.py +++ b/examples/reference/data_spatial_walker_statements.py @@ -2,55 +2,35 @@ @jac.make_architype("walker", on_entry=[jac.DSFunc("create", jac.RootType)], on_exit=[]) -class walker_1: - def create(self, walker_here: jac.RootType) -> None: - end = walker_here +class Creator: + def create(self, Creator_here: jac.RootType) -> None: + end = Creator_here i = 0 - while i < 5: + while i <= 4: jac.connect( end, - (end := node_a(val=i + 1)), + (end := leaf(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(Creator_here, jac.EdgeDir.OUT, None, None) + ): pass -@jac.make_architype( - "node", on_entry=[jac.DSFunc("print_something", walker_1)], on_exit=[] -) -class node_a: +@jac.make_architype("node", on_entry=[jac.DSFunc("print", Creator)], on_exit=[]) +class leaf: val: int - def print_something(self, walker_here: walker_1) -> None: - print(f"walker_1 entered to {self}") - if jac.visit_node(walker_here, jac.edge_ref(self, jac.EdgeDir.OUT, None)): - pass - - -@jac.make_architype( - "walker", - on_entry=[ - jac.DSFunc("skip_root", jac.RootType), - jac.DSFunc("do_something", node_a), - ], - on_exit=[], -) -class walker_2: - def skip_root(self, walker_here: jac.RootType) -> None: - if jac.visit_node(self, jac.edge_ref(walker_here, jac.EdgeDir.OUT, None)): - pass - - def do_something(self, walker_here: node_a) -> None: - print(f"walker_2 reached to {walker_here}") - if walker_here.val == 4: - jac.disengage(self) + def print(self, leaf_here: Creator) -> None: + print(f"This is {leaf_here} walk along {self}") + if self.val == 4: + print("walking stopped!!!") + jac.disengage(leaf_here) return - if jac.visit_node(self, jac.edge_ref(walker_here, jac.EdgeDir.OUT, None)): + if jac.visit_node(leaf_here, jac.edge_ref(self, jac.EdgeDir.OUT, None, None)): pass - jac.ignore(self, [node_a(val=2)]) -jac.spawn_call(walker_1(), jac.get_root()) -jac.spawn_call(jac.get_root(), walker_2()) +jac.spawn_call(jac.get_root(), Creator()) diff --git a/examples/reference/delete_statements.jac b/examples/reference/delete_statements.jac index 39152524f..a144f6bd5 100644 --- a/examples/reference/delete_statements.jac +++ b/examples/reference/delete_statements.jac @@ -1,7 +1,8 @@ with entry{ - x = 42; + x = [2,4,5,7,9]; print("Before Delete:", x); - del x; + del x[3]; + 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 c6aea216d..7f784beb7 100644 --- a/examples/reference/delete_statements.py +++ b/examples/reference/delete_statements.py @@ -1,4 +1,4 @@ -x = 42 +x = [2, 4, 5, 7, 9] print("Before Delete:", x) - -del x +del x[3] +print("After Delete:", x) diff --git a/examples/reference/f_string_tokens.py b/examples/reference/f_string_tokens.py index ae77df11f..b4baa55eb 100644 --- a/examples/reference/f_string_tokens.py +++ b/examples/reference/f_string_tokens.py @@ -1,19 +1,14 @@ x = "a" y = 25 - -tokens = f"Hello {x} {y} {{This is an escaped curly brace}}" -print(tokens) - +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.") - -# Whitespace comment still did not write yet -# This is comment in multiline - 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") - +print("Line 1\x0cLine 2") words = ["Hello", "World!", "I", "am", "a", "Jactastic!"] -print(f"{'\n'.join(words)}") +print( + f'''{""" +""".join(words)}''' +) diff --git a/examples/reference/if_statements.jac b/examples/reference/if_statements.jac index bb955aa20..aeb9e7f50 100644 --- a/examples/reference/if_statements.jac +++ b/examples/reference/if_statements.jac @@ -1,11 +1,11 @@ with entry { - x = 3; - if 0 <= x <= 5 { - print('ok'); - } elif 6 <= x <= 10 { - print('ook'); - } else { - print('not ok'); - } + x = 15; + + if 0 <= x <= 5{ + print("Not Bad");} + elif 6 <= x <= 10{ + print("Average");} + else{ + print("Good Enough");} } diff --git a/examples/reference/if_statements.py b/examples/reference/if_statements.py index 7e332474f..ca8f5e62d 100644 --- a/examples/reference/if_statements.py +++ b/examples/reference/if_statements.py @@ -1,7 +1,7 @@ -x = 3 +x = 15 if 0 <= x <= 5: - print("ok") + print("Not Bad") elif 6 <= x <= 10: - print("ook") + print("Average") else: - print("not ok") + print("Good Enough") diff --git a/examples/reference/walrus_assignments.jac b/examples/reference/walrus_assignments.jac index 44a6c0512..c028f3025 100644 --- a/examples/reference/walrus_assignments.jac +++ b/examples/reference/walrus_assignments.jac @@ -1,8 +1,6 @@ -with entry { - numbers = [1, 2, 3, 4, 5]; - for i in numbers { - if (j := i + i // 2) > 3 { - print(j); - } +with entry{ + a = 5; + if (b := a+a//2) > 5{ + print("b is grater than 5"); } } diff --git a/examples/reference/walrus_assignments.py b/examples/reference/walrus_assignments.py index bd2fb9694..f8cc12afb 100644 --- a/examples/reference/walrus_assignments.py +++ b/examples/reference/walrus_assignments.py @@ -1,4 +1,3 @@ -numbers = [1, 2, 3, 4, 5] -for i in numbers: - if (j := (i + (i // 2))) > 3: - print(j) +a = 5 +if (b := (a + a // 2)) > 5: + print("b is grater than 5") From 78ea5746911e01b740cf19e037ce887cedfb459f Mon Sep 17 00:00:00 2001 From: Sarujan1999 Date: Tue, 30 Jan 2024 12:50:40 +0530 Subject: [PATCH 2/6] Examples: refactoring --- examples/reference/special_comprehensions.jac | 6 +--- examples/reference/special_comprehensions.py | 28 +++++++------------ 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/examples/reference/special_comprehensions.jac b/examples/reference/special_comprehensions.jac index 5a5c746dc..2c107dfc3 100644 --- a/examples/reference/special_comprehensions.jac +++ b/examples/reference/special_comprehensions.jac @@ -5,10 +5,6 @@ obj TestObj { has x: int = random.randint(0, 15), y: int = random.randint(0, 15), z: int = random.randint(0, 15); - - can { - print(.x, .y, .z); - } } with entry { @@ -17,7 +13,7 @@ with entry { for i=0 to i<10 by i+=1 { apple.append(TestObj()); } - #print(apple); + #To get the instances with the y value less than 7 print(apple(=y <= 7)); } #assign comprehension diff --git a/examples/reference/special_comprehensions.py b/examples/reference/special_comprehensions.py index cfd752094..d0dec1120 100644 --- a/examples/reference/special_comprehensions.py +++ b/examples/reference/special_comprehensions.py @@ -1,23 +1,15 @@ -from __future__ import annotations -from jaclang.plugin.feature import JacFeature as _Jac +from jaclang.plugin.feature import JacFeature as jac import random -random.seed(42) - - -@_Jac.make_architype("obj", on_entry=[], on_exit=[]) +@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) + x: int = jac.has_instance_default(gen_func=lambda: random.randint(0, 15)) + y: int = jac.has_instance_default(gen_func=lambda: random.randint(0, 15)) + z: int = jac.has_instance_default(gen_func=lambda: random.randint(0, 15)) +random.seed(42) apple = [] i = 0 while i < 10: @@ -26,13 +18,13 @@ def __init__(self) -> None: 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 + apple: int = jac.has_instance_default(gen_func=lambda: 0) + banana: int = jac.has_instance_default(gen_func=lambda: 0) x = MyObj() y = MyObj() -mvar = _Jac.assign_compr([x, y], (("apple", "banana"), (5, 7))) +mvar = jac.assign_compr([x, y], (("apple", "banana"), (5, 7))) print(mvar) From bc83deb4cbae34fdc1b022f630bb54e88d48166e Mon Sep 17 00:00:00 2001 From: Sarujan1999 Date: Wed, 31 Jan 2024 21:01:01 +0530 Subject: [PATCH 3/6] examples:refactored --- examples/reference/abilities.jac | 77 ++++--------------- .../data_spatial_spawn_expressions.jac | 9 ++- examples/reference/free_code.jac | 4 +- examples/reference/free_code.py | 2 +- examples/reference/lambda_expressions.jac | 4 +- 5 files changed, 22 insertions(+), 74 deletions(-) diff --git a/examples/reference/abilities.jac b/examples/reference/abilities.jac index 59387ba06..6b85f90fb 100644 --- a/examples/reference/abilities.jac +++ b/examples/reference/abilities.jac @@ -1,76 +1,25 @@ -#Ability with decorators, access_modifiers -"""Calculator object with static method""" obj Calculator{ - #static ability(function) - static can :priv multiply(a :float, b :float) -> float{ return a*b; } -} -with entry{ - print(Calculator.multiply(9,-2)); -} -# Animal Archetype with an abstract ability -obj Animal { - """Abstract ability for making a sound.""" - can make_sound -> str abs; -} - -# Concrete obj representing a Dog -obj Dog:Animal: { - """Overridden method to make a dog sound.""" - can make_sound -> str { - return "Woof! Woof!"; + can substract -> float abs; + can add(number:float,*a :tuple) -> str; } -} -# Concrete obj representing a Cat -obj Cat:Animal: { - """Overridden method to make a cat sound.""" - can make_sound -> str { - return "Meow!"; +obj substractor:Calculator:{ + can substract(x:float,y:float)->float{ + return(x-y); } } -# Ability to simulate interactions with animals -can interact_with_animal(animal: Animal) -> None { - sound = animal.make_sound(); - print(f"The animal says: {sound}"); -} +:obj:Calculator:can:add(number:float,*a :tuple) -> str { + return(number*sum(a)); + } -# Usage with entry{ - # Creating instances of concrete architypes - Milo = Dog(); - Leo = Cat(); - - # Interacting with animals - interact_with_animal(Milo); - interact_with_animal(Leo); -} -# Declaration -can greet(name: str) -> str; - -# Definition after greet has been declared -:can:greet -(name: str) -> str { - print(f"Hey, {name} Welcome to Jaseci!"); - -} -with entry{ - #ability calling - greet('Coder'); - -} -#Simple Ability -"""Ability(Function) to calculate the numbers""" -can add(*a :tuple) -> int { - return sum(a); -} - -with entry{ - #ability calling - print(add(9,-3,4)); - + cal = Calculator(); + sub = substractor(); + print(Calculator.multiply(9,-2)); + print(cal.add(5,20,34,56)); + print(sub.substract(9,-2)); } - diff --git a/examples/reference/data_spatial_spawn_expressions.jac b/examples/reference/data_spatial_spawn_expressions.jac index fabc4966b..fecbdc0b2 100644 --- a/examples/reference/data_spatial_spawn_expressions.jac +++ b/examples/reference/data_spatial_spawn_expressions.jac @@ -2,8 +2,8 @@ walker Adder{ can do with ` entry; } node node_a{ - has x:int = 0; - has y:int = 0; + has x:int = 0, + y:int = 0; can add with Adder entry; } :walker:Adder:can:do{ @@ -13,8 +13,9 @@ node node_a{ :node:node_a:can:add{ .x= 550 ; .y= 450 ; - print(int(.x) + int(.y)); + print(int(.x) + int(.y)); } with entry{ - Adder() spawn ; # spawn will iniiate the walker Adder from root node + # spawn will iniiate the walker Adder from root node + Adder() spawn ; } \ No newline at end of file diff --git a/examples/reference/free_code.jac b/examples/reference/free_code.jac index fb2b5b06a..027ddb55b 100644 --- a/examples/reference/free_code.jac +++ b/examples/reference/free_code.jac @@ -14,9 +14,9 @@ can foo(n_1 : float) { } with entry{ - "Hello World! " |> print; + print("Hello World! ") ; print(foo(7)); - print(circle(7).area()); + print(int(circle(10).area())); #code block } diff --git a/examples/reference/free_code.py b/examples/reference/free_code.py index 880239c2e..a5526e3b1 100644 --- a/examples/reference/free_code.py +++ b/examples/reference/free_code.py @@ -15,4 +15,4 @@ def foo(n_1: float): print("Hello World!") print(foo(7)) -print(Circle(7).area()) +print(int(Circle(10).area())) diff --git a/examples/reference/lambda_expressions.jac b/examples/reference/lambda_expressions.jac index f175d9e9a..ae3814988 100644 --- a/examples/reference/lambda_expressions.jac +++ b/examples/reference/lambda_expressions.jac @@ -1,8 +1,6 @@ with entry{ - - x = with a: int , c:int can c+c; + x = with a: int , b:int can b+a; print(x(5,4)); - } From afdea2ae9fad139bcda5e4380216115596f9ce3c Mon Sep 17 00:00:00 2001 From: Sarujan1999 Date: Wed, 31 Jan 2024 23:54:40 +0530 Subject: [PATCH 4/6] examples:refactored --- examples/reference/visit_statements.jac | 104 ++++-------------------- 1 file changed, 18 insertions(+), 86 deletions(-) diff --git a/examples/reference/visit_statements.jac b/examples/reference/visit_statements.jac index d14c659bb..3107cb9c1 100644 --- a/examples/reference/visit_statements.jac +++ b/examples/reference/visit_statements.jac @@ -1,99 +1,31 @@ -#version1 -walker creator { +walker Creator { can create with ` entry; -} - -walker Travellor { - can func_3 with ` entry; -} + can stop with ` entry; + } -node item { - has val: int; +node item{ + can print_something with Creator entry{ + print("Hey There!!!"); - 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"); +:walker:Creator:can:create{ + for i=0 to i < 5 by i+=1{ + ++> item(); + } - visit --> else { - print("finished visitng all nodes ....\n"); + visit-->else{ + visit ; } - ; } -:walker:creator:can:create { - end = ; - for i=0 to i<5 by i+=1 { - end ++> (end := item(val=i + 1)); +:walker:Creator:can:stop{ + print("walking stopped"); + disengage; } - visit -->; -} -:walker:Travellor:can:func_3 { - visit(item(val=2)); +with entry{ + spawn Creator(); } -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; -# } - -# 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 373875a333b298bd2f66ffb141eb8c21ed889690 Mon Sep 17 00:00:00 2001 From: Sarujan1999 Date: Thu, 1 Feb 2024 00:16:32 +0530 Subject: [PATCH 5/6] examples:refactored --- examples/reference/abilities.py | 72 +++++-------------- .../data_spatial_typed_context_blocks.jac | 27 +++++-- .../data_spatial_typed_context_blocks.py | 37 +++++++++- 3 files changed, 76 insertions(+), 60 deletions(-) diff --git a/examples/reference/abilities.py b/examples/reference/abilities.py index 4e5f2cec0..553f93fa5 100644 --- a/examples/reference/abilities.py +++ b/examples/reference/abilities.py @@ -1,65 +1,27 @@ -# Function with decorators, access_modifiers -class Calculator: - """Calculator object with static method""" +from abc import ABC, abstractmethod + - # static function +class Calculator: @staticmethod - def multiply(a, b): + def multiply(a: float, b: float) -> float: return a * b + @abstractmethod + def substract(self, x: float, y: float) -> float: + pass -print(Calculator.multiply(9, -2)) - - -# Animal Archetype with an abstract ability -class Animal: - """Abstract class for making a sound.""" - - def make_sound(self): - raise NotImplementedError("Abstract method 'make_sound' must be overridden") - - -# Concrete class representing a Dog -class Dog(Animal): - def make_sound(self): - return "Woof! Woof!" - + def add(self, number: float, *a: tuple) -> str: + return str(number * sum(a)) -# Concrete class representing a Cat -class Cat(Animal): - def make_sound(self): - return "Meow!" +class Substractor(Calculator): + def substract(self, x: float, y: float) -> float: + return x - y -# Ability to simulate interactions with animals -def interact_with_animal(animal): - sound = animal.make_sound() - print(f"The animal says: {sound}") +cal = Calculator() +sub = Substractor() -# Creating instances of concrete archetypes -Milo = Dog() -Leo = Cat() - -# Interacting with animals -interact_with_animal(Milo) -interact_with_animal(Leo) - - -# Declaration & Definition in same block -def greet(name): - print(f"Hey, {name} Welcome to Jaseci!") - - -# fun calling -greet("Coder") - - -# Simple Function -def add(*a): - """Ability(Function) to calculate the numbers""" - return sum(a) - - -# function calling -print(add(9, -3, 4)) +print(Calculator.multiply(9, -2)) +print(cal.add(5, 20, 34, 56)) +print(sub.substract(9, -2)) diff --git a/examples/reference/data_spatial_typed_context_blocks.jac b/examples/reference/data_spatial_typed_context_blocks.jac index 21224a247..694c60529 100644 --- a/examples/reference/data_spatial_typed_context_blocks.jac +++ b/examples/reference/data_spatial_typed_context_blocks.jac @@ -1,6 +1,25 @@ +walker Producer{ + can produce with ` entry; +} + +node Product{ + has number:int; + can make with Producer entry; +} + +:walker:Producer:can:produce{ + end = ; + for i = 0 to i<=2 by i+=1{ + end ++> (end := Product(number=i + 1)); + } + visit -->; +} + +:node:Product:can:make -> str{ + print(f"Hi, I am {} returning a String"); + visit -->; +} + with entry{ - can foo(x: int) -> int{ - return x*2; - } - print(foo(3)); + spawn Producer(); } \ No newline at end of file diff --git a/examples/reference/data_spatial_typed_context_blocks.py b/examples/reference/data_spatial_typed_context_blocks.py index a07c786f4..8f72330d2 100644 --- a/examples/reference/data_spatial_typed_context_blocks.py +++ b/examples/reference/data_spatial_typed_context_blocks.py @@ -1 +1,36 @@ -print("Jactastic : Jac only feature") +from jaclang.plugin.feature import JacFeature as jac + + +@jac.make_architype( + "walker", on_entry=[jac.DSFunc("produce", jac.RootType)], on_exit=[] +) +class Producer: + def produce(self, Producer_here: jac.RootType) -> None: + end = Producer_here + i = 0 + while i <= 2: + jac.connect( + end, + (end := Product(number=i + 1)), + jac.build_edge(jac.EdgeDir.OUT, None, None), + ) + i += 1 + if jac.visit_node( + self, jac.edge_ref(Producer_here, jac.EdgeDir.OUT, None, None) + ): + pass + + +@jac.make_architype("node", on_entry=[jac.DSFunc("make", Producer)], on_exit=[]) +class Product: + number: int + + def make(self, Product_here: Producer) -> None: + print(f"Hi, I am {self} returning a String") + if jac.visit_node( + Product_here, jac.edge_ref(self, jac.EdgeDir.OUT, None, None) + ): + pass + + +jac.spawn_call(jac.get_root(), Producer()) From 0c61eb3d5087cda65a5ce687abfafe2739a4d2de Mon Sep 17 00:00:00 2001 From: marsninja Date: Wed, 31 Jan 2024 14:42:28 -0500 Subject: [PATCH 6/6] refactor: tweaked --- examples/reference/free_code.jac | 25 ++++++++++++----------- examples/reference/lambda_expressions.jac | 8 +++----- jaclang/tests/test_reference.py | 15 +++++++++----- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/examples/reference/free_code.jac b/examples/reference/free_code.jac index 027ddb55b..19da179dc 100644 --- a/examples/reference/free_code.jac +++ b/examples/reference/free_code.jac @@ -1,22 +1,23 @@ import:py math; -obj circle{ - can (radius : float) { - self.radius=radius; - } - can area-> float{ - return math.pi * self.radius * self.radius; - } +obj circle { + can (radius: float) { + self.radius = radius; + } + can area -> float { + return math.pi * self.radius * self.radius; + } } -can foo(n_1 : float) { - return n_1**2; + +can foo(n_1: float) { + return n_1 ** 2; } -with entry{ - print("Hello World! ") ; +with entry { + print("Hello World!"); print(foo(7)); print(int(circle(10).area())); + #code block } - diff --git a/examples/reference/lambda_expressions.jac b/examples/reference/lambda_expressions.jac index ae3814988..41a7df348 100644 --- a/examples/reference/lambda_expressions.jac +++ b/examples/reference/lambda_expressions.jac @@ -1,6 +1,4 @@ -with entry{ - x = with a: int , b:int can b+a; - print(x(5,4)); +with entry { + x = with a: int, b: int can b + a; + print(x(5, 4)); } - - diff --git a/jaclang/tests/test_reference.py b/jaclang/tests/test_reference.py index 520ae8e42..7cbcc30c9 100644 --- a/jaclang/tests/test_reference.py +++ b/jaclang/tests/test_reference.py @@ -49,20 +49,25 @@ def test_ref_jac_files_fully_tested(self: TestCase) -> None: # noqa: ANN001 def micro_suite_test(self, filename: str) -> None: """Test file.""" - def execute_and_capture_output(code: str) -> str: + def execute_and_capture_output(code: str | bytes, filename: str = "") -> str: f = io.StringIO() with redirect_stdout(f): - exec(code, {}) + exec(code, {"__file__": filename}) return f.getvalue() try: - code_content = jac_file_to_pass(filename).ir.gen.py - output_jac = execute_and_capture_output(code_content) + jacast = jac_file_to_pass(filename).ir + code_content = compile( + source=jacast.gen.py_ast, + filename=jacast.loc.mod_path, + mode="exec", + ) + output_jac = execute_and_capture_output(code_content, filename=filename) filename = filename.replace(".jac", ".py") with open(filename, "r") as file: code_content = file.read() - output_py = execute_and_capture_output(code_content) + output_py = execute_and_capture_output(code_content, filename=filename) # print(f"\nJAC Output:\n{output_jac}") # print(f"\nPython Output:\n{output_py}")