Skip to content

Commit 84dfe95

Browse files
Variable Value in Python (#70)
* Delete not necessary dummy test file * Add python -u test_nucleoid.py * Remove unnecessary files * Add and prepare first testcase file * Add nucleoid file * Parse file to return abstract syntax tree * Process file to check which handler to use * Folder for Handlers and Operations * Handles operation for Expression statements * Stores the states of variables * Makes the graph * Fix EoL in Python --------- Co-authored-by: Invademars <[email protected]>
1 parent d8b79bf commit 84dfe95

14 files changed

+138
-134
lines changed

python/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
python version of nucleoid
3+
"""
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from ...nucleoid.state import variable_state
2+
from ...nucleoid.graph import maingraph
3+
import ast
4+
5+
def assignment_handler(node):
6+
# Extract the variable name from the target
7+
target = node.targets[0]
8+
if isinstance(target, ast.Name):
9+
var_name = target.id
10+
# Extract the value
11+
if isinstance(node.value, ast.Constant):
12+
var_value = node.value.value
13+
#THIS NEXT LINE IS FOR DEPENDENCY HANDLING
14+
#elif isinstance(node.value, ast.Name):
15+
# var_value = variable_state.get(node.value.id, None)
16+
else:
17+
var_value = None # Handle other types if necessary
18+
# Store the variable and its value in the dictionary
19+
variable_state[var_name] = var_value
20+
# Add the variable as a node in the graph
21+
maingraph.add_node(var_name)
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import ast
2+
from ...nucleoid.state import variable_state
3+
4+
def expression_handler(node):
5+
"""
6+
Evaluates an AST node and returns its value based on the variable_state dictionary.
7+
8+
Args:
9+
node (ast.Node): The AST node to evaluate.
10+
variable_state (dict): A dictionary containing variable names and their values.
11+
12+
Returns:
13+
The evaluated value of the node.
14+
15+
Raises:
16+
NameError: If a variable is not defined in variable_state.
17+
NotImplementedError: If the node type or operation is not supported.
18+
"""
19+
if isinstance(node, ast.Name):
20+
if node.id in variable_state:
21+
return variable_state[node.id]
22+
else:
23+
raise NameError(f"Variable {node.id} is not defined")
24+
elif isinstance(node, ast.Constant):
25+
return node.value
26+
elif isinstance(node, ast.BinOp):
27+
left = expression_handler(node.left)
28+
right = expression_handler(node.right)
29+
if isinstance(node.op, ast.Add):
30+
return left + right
31+
elif isinstance(node.op, ast.Sub):
32+
return left - right
33+
elif isinstance(node.op, ast.Mult):
34+
return left * right
35+
elif isinstance(node.op, ast.Div):
36+
return left / right
37+
# Add more operators as needed
38+
else:
39+
raise NotImplementedError(f"Operator {node.op} not supported")
40+
elif isinstance(node, ast.Compare):
41+
left = expression_handler(node.left)
42+
right = expression_handler(node.comparators[0])
43+
if isinstance(node.ops[0], ast.Eq):
44+
return left == right
45+
elif isinstance(node.ops[0], ast.NotEq):
46+
return left != right
47+
elif isinstance(node.ops[0], ast.Lt):
48+
return left < right
49+
elif isinstance(node.ops[0], ast.LtE):
50+
return left <= right
51+
elif isinstance(node.ops[0], ast.Gt):
52+
return left > right
53+
elif isinstance(node.ops[0], ast.GtE):
54+
return left >= right
55+
# Add more comparison operators as needed
56+
else:
57+
raise NotImplementedError(f"Comparison operator {node.ops[0]} not supported")
58+
else:
59+
raise NotImplementedError(f"Node type {type(node)} not supported")

python/nucleoid/Calculator.py

-35
This file was deleted.

python/nucleoid/graph.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import networkx as nx
2+
3+
maingraph = nx.MultiDiGraph()

python/nucleoid/logger.py

-28
This file was deleted.

python/nucleoid/main.py

-6
This file was deleted.

python/nucleoid/nucleoid.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from .parse import parse
2+
from .process import process
3+
4+
class Nucleoid:
5+
def __init__(self):
6+
print("Nucleoid object created")
7+
8+
#clear all the graph and state etc(for later)
9+
10+
11+
def run(self, statement):
12+
if isinstance(statement, str):
13+
print("Running statement: ", statement)
14+
parsed_tree = parse(statement)
15+
out = process(parsed_tree)
16+
return out

python/nucleoid/parse.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import ast
2+
3+
def parse(statement):
4+
print("Parsing statement: ", statement)
5+
tree = ast.parse(statement)
6+
return tree

python/nucleoid/process.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import ast
2+
from ..lang.handlers.assignment_handler import assignment_handler
3+
from ..lang.handlers.expression_handler import expression_handler
4+
5+
def process(parsed_tree):
6+
7+
"""ast.walk(node)
8+
Recursively yield all descendant nodes in the tree starting at node (including node itself), in no specified order.
9+
This is useful if you only want to modify nodes in place and don’t care about the context."""
10+
if isinstance(parsed_tree.body[0], ast.Expr):
11+
return expression_handler(parsed_tree.body[0].value)
12+
13+
14+
if isinstance(parsed_tree.body[0], ast.Assign):
15+
assignment_handler(parsed_tree.body[0])

python/nucleoid/state.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
variable_state = {}

python/tests/test_calculator.py

-65
This file was deleted.

python/tests/test_nucleoid.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pytest
2+
from ..nucleoid.nucleoid import Nucleoid # Import the Nucleoid class from the nucleoid module
3+
4+
@pytest.fixture
5+
def setup():
6+
# Initialize the Nucleoid class
7+
return Nucleoid()
8+
9+
def test_give_variable_value(setup):
10+
# Use the initialized Nucleoid instance
11+
nucleoid = setup
12+
nucleoid.run("i = 1")
13+
assert nucleoid.run("i == 1") is True

src/lang/$nuc/$INSTANCE.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function build(cls, object, name, args = []) {
1212
statement.obj = object;
1313
statement.nme = name;
1414
statement.args = args;
15+
1516
return statement;
1617
}
1718

0 commit comments

Comments
 (0)