Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #180 from Jaseci-Labs/examples_01
Browse files Browse the repository at this point in the history
refactored examples
  • Loading branch information
marsninja committed Jan 29, 2024
2 parents bb9c91d + 97b9048 commit 587d13e
Show file tree
Hide file tree
Showing 46 changed files with 548 additions and 545 deletions.
11 changes: 7 additions & 4 deletions examples/guess_game/guess_game5.jac
Original file line number Diff line number Diff line change
@@ -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 `<root> entry;
can process_guess(guess: int);
}
Expand All @@ -23,13 +25,14 @@ node turn {

:walker:GuessGame:can:start_game {
end = <here>;
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 > <self>.correct_number {
"Too high!" |> print;
} elif guess < <self>.correct_number {
Expand All @@ -39,8 +42,8 @@ node turn {
disengage;
}
}

# # Run the game

with entry {
:> GuessGame spawn <root>;
}
39 changes: 8 additions & 31 deletions examples/reference/arithmetic_expressions.jac
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
with entry{
# Addition
result_addition = 5 + 3;
print("Addition:", result_addition); # Output: 8
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

}
}
38 changes: 8 additions & 30 deletions examples/reference/arithmetic_expressions.py
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 8 additions & 16 deletions examples/reference/assignments.jac
Original file line number Diff line number Diff line change
@@ -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);
}


Expand Down
27 changes: 8 additions & 19 deletions examples/reference/assignments.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 6 additions & 6 deletions examples/reference/atom.jac
Original file line number Diff line number Diff line change
@@ -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);
}
33 changes: 7 additions & 26 deletions examples/reference/bitwise_expressions.jac
Original file line number Diff line number Diff line change
@@ -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);

}
34 changes: 7 additions & 27 deletions examples/reference/bitwise_expressions.py
Original file line number Diff line number Diff line change
@@ -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)
41 changes: 23 additions & 18 deletions examples/reference/connect_expressions.jac
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
node node_a{
has value:int;
node node_a {
has value: int;
}

walker Creator {
can create with `<root> entry;
can travel with `<root>|node_a entry;
can travel with `<root> | node_a entry;
}

edge MyEdge {
has val:int =5;
has val: int = 5;
}
:walker:Creator:can:create{

:walker:Creator:can:create {
end = <here>;
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 {
<root> spawn Creator();

# print(<r>._jac_.gen_dot());
}
}
35 changes: 19 additions & 16 deletions examples/reference/data_spatial_calls.jac
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
walker walker_1{
can func2 with `<root> entry;
walker walker_1 {
can func2 with `<root> 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 ",<s>);
visit-->;
}
:walker:walker_1:can:func2{

:node:node_1:can:func_1 {
print("visiting ", <s>);
visit -->;
}

:walker:walker_1:can:func2 {
end = <here>;
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{
<r> spawn |> walker_1 ;
<r> spawn :> walker_1 ;

with entry {
<r> spawn |> walker_1;
<r> spawn :> walker_1;
}
Loading

0 comments on commit 587d13e

Please sign in to comment.