Skip to content
Closed
3 changes: 3 additions & 0 deletions python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
python version of nucleoid
"""
21 changes: 21 additions & 0 deletions python/lang/handlers/assignment_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from ...nucleoid.state import variable_state
from ...nucleoid.graph import maingraph
import ast

def assignment_handler(node):
# Extract the variable name from the target
target = node.targets[0]
if isinstance(target, ast.Name):
var_name = target.id
# Extract the value
if isinstance(node.value, ast.Constant):
var_value = node.value.value
#THIS NEXT LINE IS FOR DEPENDENCY HANDLING
#elif isinstance(node.value, ast.Name):
# var_value = variable_state.get(node.value.id, None)
else:
var_value = None # Handle other types if necessary
# Store the variable and its value in the dictionary
variable_state[var_name] = var_value
# Add the variable as a node in the graph
maingraph.add_node(var_name)
59 changes: 59 additions & 0 deletions python/lang/handlers/expression_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import ast
from ...nucleoid.state import variable_state

def expression_handler(node):
"""
Evaluates an AST node and returns its value based on the variable_state dictionary.

Args:
node (ast.Node): The AST node to evaluate.
variable_state (dict): A dictionary containing variable names and their values.

Returns:
The evaluated value of the node.

Raises:
NameError: If a variable is not defined in variable_state.
NotImplementedError: If the node type or operation is not supported.
"""
if isinstance(node, ast.Name):
if node.id in variable_state:
return variable_state[node.id]
else:
raise NameError(f"Variable {node.id} is not defined")
elif isinstance(node, ast.Constant):
return node.value
elif isinstance(node, ast.BinOp):
left = expression_handler(node.left)
right = expression_handler(node.right)
if isinstance(node.op, ast.Add):
return left + right
elif isinstance(node.op, ast.Sub):
return left - right
elif isinstance(node.op, ast.Mult):
return left * right
elif isinstance(node.op, ast.Div):
return left / right
# Add more operators as needed
else:
raise NotImplementedError(f"Operator {node.op} not supported")
elif isinstance(node, ast.Compare):
left = expression_handler(node.left)
right = expression_handler(node.comparators[0])
if isinstance(node.ops[0], ast.Eq):
return left == right
elif isinstance(node.ops[0], ast.NotEq):
return left != right
elif isinstance(node.ops[0], ast.Lt):
return left < right
elif isinstance(node.ops[0], ast.LtE):
return left <= right
elif isinstance(node.ops[0], ast.Gt):
return left > right
elif isinstance(node.ops[0], ast.GtE):
return left >= right
# Add more comparison operators as needed
else:
raise NotImplementedError(f"Comparison operator {node.ops[0]} not supported")
else:
raise NotImplementedError(f"Node type {type(node)} not supported")
35 changes: 0 additions & 35 deletions python/nucleoid/Calculator.py

This file was deleted.

3 changes: 3 additions & 0 deletions python/nucleoid/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import networkx as nx

maingraph = nx.MultiDiGraph()
28 changes: 0 additions & 28 deletions python/nucleoid/logger.py

This file was deleted.

6 changes: 0 additions & 6 deletions python/nucleoid/main.py

This file was deleted.

16 changes: 16 additions & 0 deletions python/nucleoid/nucleoid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .parse import parse
from .process import process

class Nucleoid:
def __init__(self):
print("Nucleoid object created")

#clear all the graph and state etc(for later)


def run(self, statement):
if isinstance(statement, str):
print("Running statement: ", statement)
parsed_tree = parse(statement)
out = process(parsed_tree)
return out
6 changes: 6 additions & 0 deletions python/nucleoid/parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ast

def parse(statement):
print("Parsing statement: ", statement)
tree = ast.parse(statement)
return tree
15 changes: 15 additions & 0 deletions python/nucleoid/process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import ast
from ..lang.handlers.assignment_handler import assignment_handler
from ..lang.handlers.expression_handler import expression_handler

def process(parsed_tree):

"""ast.walk(node)
Recursively yield all descendant nodes in the tree starting at node (including node itself), in no specified order.
This is useful if you only want to modify nodes in place and don’t care about the context."""
if isinstance(parsed_tree.body[0], ast.Expr):
return expression_handler(parsed_tree.body[0].value)


if isinstance(parsed_tree.body[0], ast.Assign):
assignment_handler(parsed_tree.body[0])
1 change: 1 addition & 0 deletions python/nucleoid/state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
variable_state = {}
65 changes: 0 additions & 65 deletions python/tests/test_calculator.py

This file was deleted.

13 changes: 13 additions & 0 deletions python/tests/test_nucleoid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest
from ..nucleoid.nucleoid import Nucleoid # Import the Nucleoid class from the nucleoid module

@pytest.fixture
def setup():
# Initialize the Nucleoid class
return Nucleoid()

def test_give_variable_value(setup):
# Use the initialized Nucleoid instance
nucleoid = setup
nucleoid.run("i = 1")
assert nucleoid.run("i == 1") is True
1 change: 1 addition & 0 deletions src/lang/$nuc/$INSTANCE.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function build(cls, object, name, args = []) {
statement.obj = object;
statement.nme = name;
statement.args = args;

return statement;
}

Expand Down
Loading