From 266b9644b5f25cd59bfc4867fafa9fe79f2b04ea Mon Sep 17 00:00:00 2001 From: Invademars <86682731+Invademars@users.noreply.github.com> Date: Wed, 8 Jan 2025 06:54:59 +0530 Subject: [PATCH 01/12] Deleting not necessary dummy test file --- python/tests/test_calculator.py | 65 --------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 python/tests/test_calculator.py diff --git a/python/tests/test_calculator.py b/python/tests/test_calculator.py deleted file mode 100644 index e06df01..0000000 --- a/python/tests/test_calculator.py +++ /dev/null @@ -1,65 +0,0 @@ -import pytest -from centaur.Calculator import Calculator - - -@pytest.fixture -def calc(): - """Fixture to initialize a Calculator instance for testing.""" - return Calculator() - - -def test_add(calc): - """Tests addition functionality of the Calculator.""" - assert calc.add(2, 3) == 5 - assert calc.add(-1, 1) == 0 - assert calc.add(0, 0) == 0 - - -def test_subtract(calc): - """Tests subtraction functionality of the Calculator.""" - assert calc.subtract(5, 3) == 2 - assert calc.subtract(0, 5) == -5 - assert calc.subtract(-5, -5) == 0 - - -def test_multiply(calc): - """Tests multiplication functionality of the Calculator.""" - assert calc.multiply(2, 3) == 6 - assert calc.multiply(-1, 3) == -3 - assert calc.multiply(0, 5) == 0 - - -def test_divide(calc): - """ - Tests division functionality of the Calculator, - including handling division by zero. - """ - assert calc.divide(10, 2) == 5 - assert calc.divide(-9, 3) == -3 - assert calc.divide(5, 2) == 2.5 - - with pytest.raises(ValueError, match="Division by zero is not allowed"): - calc.divide(5, 0) - - -def test_logger_initialization(calc): - """Verifies that the logger is properly initialized in the Calculator.""" - assert calc.logger is not None - assert calc.logger.name == "centaur.Calculator" - - -def test_result_property(calc): - """ - Ensures the `result` attribute updates correctly after each operation. - """ - calc.add(2, 3) - assert calc.result == 5 - - calc.subtract(5, 3) - assert calc.result == 2 - - calc.multiply(2, 3) - assert calc.result == 6 - - calc.divide(6, 3) - assert calc.result == 2.0 From c83258f82058510144eff4acbfdebc51c3fd014f Mon Sep 17 00:00:00 2001 From: Invademars <86682731+Invademars@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:19:59 +0530 Subject: [PATCH 02/12] Addedpython -u c:\Users\Gitansh\Downloads\Nucleoid-main\python\tests\test_nucleoid1.py --- python/tests/test_nucleoid.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 python/tests/test_nucleoid.py diff --git a/python/tests/test_nucleoid.py b/python/tests/test_nucleoid.py new file mode 100644 index 0000000..5f6c26f --- /dev/null +++ b/python/tests/test_nucleoid.py @@ -0,0 +1,13 @@ +import pytest +from 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 From cbf01d6276e0af1d18660fcf08b6f68ffda8e459 Mon Sep 17 00:00:00 2001 From: Invademars <86682731+Invademars@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:30:23 +0530 Subject: [PATCH 03/12] Removing unnecessary files --- python/nucleoid/Calculator.py | 35 ----------------------------------- python/nucleoid/logger.py | 28 ---------------------------- python/nucleoid/main.py | 6 ------ 3 files changed, 69 deletions(-) delete mode 100644 python/nucleoid/Calculator.py delete mode 100644 python/nucleoid/logger.py delete mode 100644 python/nucleoid/main.py diff --git a/python/nucleoid/Calculator.py b/python/nucleoid/Calculator.py deleted file mode 100644 index 0d354ee..0000000 --- a/python/nucleoid/Calculator.py +++ /dev/null @@ -1,35 +0,0 @@ -from .logger import setup_logger - - -class Calculator: - def __init__(self): - self.result = 0 - self.logger = setup_logger(__name__) - self.logger.info("Calculator initialized") - - def add(self, x, y): - self.logger.debug(f"Adding numbers: {x} + {y}") - self.result = x + y - self.logger.info(f"Addition result: {self.result}") - return self.result - - def subtract(self, x, y): - self.logger.debug(f"Subtracting numbers: {x} - {y}") - self.result = x - y - self.logger.info(f"Subtraction result: {self.result}") - return self.result - - def multiply(self, x, y): - self.logger.debug(f"Multiplying numbers: {x} * {y}") - self.result = x * y - self.logger.info(f"Multiplication result: {self.result}") - return self.result - - def divide(self, x, y): - self.logger.debug(f"Dividing numbers: {x} / {y}") - if y == 0: - self.logger.error("Division by zero attempted") - raise ValueError("Division by zero is not allowed") - self.result = x / y - self.logger.info(f"Division result: {self.result}") - return self.result diff --git a/python/nucleoid/logger.py b/python/nucleoid/logger.py deleted file mode 100644 index 93b59a0..0000000 --- a/python/nucleoid/logger.py +++ /dev/null @@ -1,28 +0,0 @@ -import logging - - -def setup_logger(name: str) -> logging.Logger: - """ - Set up a logger with console handler only - - Args: - name: The name of the logger - - Returns: - logging.Logger: Configured logger instance - """ - logger = logging.getLogger(name) - logger.setLevel(logging.DEBUG) - - if logger.handlers: - return logger - - console_formatter = logging.Formatter("%(levelname)s - %(message)s") - - console_handler = logging.StreamHandler() - console_handler.setLevel(logging.INFO) - console_handler.setFormatter(console_formatter) - - logger.addHandler(console_handler) - - return logger diff --git a/python/nucleoid/main.py b/python/nucleoid/main.py deleted file mode 100644 index 754c194..0000000 --- a/python/nucleoid/main.py +++ /dev/null @@ -1,6 +0,0 @@ -def main(): - print("Welcome to Nucleoid!") - - -if __name__ == "__main__": - main() From 69805664cb3a90a2681da103e3be919ced864487 Mon Sep 17 00:00:00 2001 From: Invademars <86682731+Invademars@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:31:53 +0530 Subject: [PATCH 04/12] Adding and preparing first testcase file --- python/__init__.py | 3 +++ python/tests/test_nucleoid.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 python/__init__.py diff --git a/python/__init__.py b/python/__init__.py new file mode 100644 index 0000000..c4cb16c --- /dev/null +++ b/python/__init__.py @@ -0,0 +1,3 @@ +""" +python version of nucleoid +""" \ No newline at end of file diff --git a/python/tests/test_nucleoid.py b/python/tests/test_nucleoid.py index 5f6c26f..df901e0 100644 --- a/python/tests/test_nucleoid.py +++ b/python/tests/test_nucleoid.py @@ -1,5 +1,5 @@ import pytest -from nucleoid import Nucleoid # Import the Nucleoid class from the nucleoid module +from ..nucleoid.nucleoid import Nucleoid # Import the Nucleoid class from the nucleoid module @pytest.fixture def setup(): From 033d969041d6f701e33b9eff13e48f69e255d69e Mon Sep 17 00:00:00 2001 From: Invademars <86682731+Invademars@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:33:48 +0530 Subject: [PATCH 05/12] Adding nucleoid file --- python/nucleoid/nucleoid.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 python/nucleoid/nucleoid.py diff --git a/python/nucleoid/nucleoid.py b/python/nucleoid/nucleoid.py new file mode 100644 index 0000000..7134b18 --- /dev/null +++ b/python/nucleoid/nucleoid.py @@ -0,0 +1,18 @@ +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 + + From d35ec828a62f2e7ebf9a75b7f5d56eee3db2c9ca Mon Sep 17 00:00:00 2001 From: Invademars <86682731+Invademars@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:35:27 +0530 Subject: [PATCH 06/12] Parse file to return abstract syntax tree --- python/nucleoid/parse.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 python/nucleoid/parse.py diff --git a/python/nucleoid/parse.py b/python/nucleoid/parse.py new file mode 100644 index 0000000..356c2a2 --- /dev/null +++ b/python/nucleoid/parse.py @@ -0,0 +1,6 @@ +import ast + +def parse(statement): + print("Parsing statement: ", statement) + tree = ast.parse(statement) + return tree \ No newline at end of file From af6ba94b718ffb19c77cd3d195da9dace8d8a2cb Mon Sep 17 00:00:00 2001 From: Invademars <86682731+Invademars@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:36:55 +0530 Subject: [PATCH 07/12] process file to check which handler to use --- python/nucleoid/process.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 python/nucleoid/process.py diff --git a/python/nucleoid/process.py b/python/nucleoid/process.py new file mode 100644 index 0000000..6146813 --- /dev/null +++ b/python/nucleoid/process.py @@ -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]) \ No newline at end of file From d0c946984b84a3f9f76bdc035b3b098e07956c82 Mon Sep 17 00:00:00 2001 From: Invademars <86682731+Invademars@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:40:25 +0530 Subject: [PATCH 08/12] folder for Handlers for operations --- python/lang/handlers/assignment_handler.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 python/lang/handlers/assignment_handler.py diff --git a/python/lang/handlers/assignment_handler.py b/python/lang/handlers/assignment_handler.py new file mode 100644 index 0000000..2965c1c --- /dev/null +++ b/python/lang/handlers/assignment_handler.py @@ -0,0 +1,22 @@ +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) + From 2d6c8ae3efef3d47dacf4b95ac29018095c406a6 Mon Sep 17 00:00:00 2001 From: Invademars <86682731+Invademars@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:42:29 +0530 Subject: [PATCH 09/12] Handles operation for Expression statements --- python/lang/handlers/expression_handler.py | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 python/lang/handlers/expression_handler.py diff --git a/python/lang/handlers/expression_handler.py b/python/lang/handlers/expression_handler.py new file mode 100644 index 0000000..caba0a9 --- /dev/null +++ b/python/lang/handlers/expression_handler.py @@ -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") \ No newline at end of file From b5934412bd2d7bdaa9070b3a62772599c1b7af79 Mon Sep 17 00:00:00 2001 From: Invademars <86682731+Invademars@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:43:31 +0530 Subject: [PATCH 10/12] Stores the states of variables --- python/nucleoid/state.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 python/nucleoid/state.py diff --git a/python/nucleoid/state.py b/python/nucleoid/state.py new file mode 100644 index 0000000..75ad601 --- /dev/null +++ b/python/nucleoid/state.py @@ -0,0 +1 @@ +variable_state = {} \ No newline at end of file From 5f44d11a671d9ebd027052e54554f27cd59f05a5 Mon Sep 17 00:00:00 2001 From: Invademars <86682731+Invademars@users.noreply.github.com> Date: Wed, 8 Jan 2025 07:44:36 +0530 Subject: [PATCH 11/12] Makes the graph --- python/nucleoid/graph.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 python/nucleoid/graph.py diff --git a/python/nucleoid/graph.py b/python/nucleoid/graph.py new file mode 100644 index 0000000..a3d7720 --- /dev/null +++ b/python/nucleoid/graph.py @@ -0,0 +1,3 @@ +import networkx as nx + +maingraph = nx.MultiDiGraph() \ No newline at end of file From d8982d009afc17b7afa98512967c57f0b3ad0220 Mon Sep 17 00:00:00 2001 From: Can Mingir Date: Thu, 9 Jan 2025 02:24:08 -0500 Subject: [PATCH 12/12] Fix EoL in Python --- python/__init__.py | 2 +- python/lang/handlers/assignment_handler.py | 1 - python/lang/handlers/expression_handler.py | 4 ++-- python/nucleoid/graph.py | 2 +- python/nucleoid/nucleoid.py | 4 +--- python/nucleoid/parse.py | 2 +- python/nucleoid/process.py | 6 +++--- python/nucleoid/state.py | 2 +- src/lang/$nuc/$INSTANCE.js | 1 + 9 files changed, 11 insertions(+), 13 deletions(-) diff --git a/python/__init__.py b/python/__init__.py index c4cb16c..296f660 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -1,3 +1,3 @@ """ python version of nucleoid -""" \ No newline at end of file +""" diff --git a/python/lang/handlers/assignment_handler.py b/python/lang/handlers/assignment_handler.py index 2965c1c..194d48a 100644 --- a/python/lang/handlers/assignment_handler.py +++ b/python/lang/handlers/assignment_handler.py @@ -19,4 +19,3 @@ def assignment_handler(node): variable_state[var_name] = var_value # Add the variable as a node in the graph maingraph.add_node(var_name) - diff --git a/python/lang/handlers/expression_handler.py b/python/lang/handlers/expression_handler.py index caba0a9..3345654 100644 --- a/python/lang/handlers/expression_handler.py +++ b/python/lang/handlers/expression_handler.py @@ -1,4 +1,4 @@ -import ast +import ast from ...nucleoid.state import variable_state def expression_handler(node): @@ -56,4 +56,4 @@ def expression_handler(node): else: raise NotImplementedError(f"Comparison operator {node.ops[0]} not supported") else: - raise NotImplementedError(f"Node type {type(node)} not supported") \ No newline at end of file + raise NotImplementedError(f"Node type {type(node)} not supported") diff --git a/python/nucleoid/graph.py b/python/nucleoid/graph.py index a3d7720..222f456 100644 --- a/python/nucleoid/graph.py +++ b/python/nucleoid/graph.py @@ -1,3 +1,3 @@ import networkx as nx -maingraph = nx.MultiDiGraph() \ No newline at end of file +maingraph = nx.MultiDiGraph() diff --git a/python/nucleoid/nucleoid.py b/python/nucleoid/nucleoid.py index 7134b18..9a9dffe 100644 --- a/python/nucleoid/nucleoid.py +++ b/python/nucleoid/nucleoid.py @@ -4,7 +4,7 @@ class Nucleoid: def __init__(self): print("Nucleoid object created") - + #clear all the graph and state etc(for later) @@ -14,5 +14,3 @@ def run(self, statement): parsed_tree = parse(statement) out = process(parsed_tree) return out - - diff --git a/python/nucleoid/parse.py b/python/nucleoid/parse.py index 356c2a2..c762495 100644 --- a/python/nucleoid/parse.py +++ b/python/nucleoid/parse.py @@ -3,4 +3,4 @@ def parse(statement): print("Parsing statement: ", statement) tree = ast.parse(statement) - return tree \ No newline at end of file + return tree diff --git a/python/nucleoid/process.py b/python/nucleoid/process.py index 6146813..475c86c 100644 --- a/python/nucleoid/process.py +++ b/python/nucleoid/process.py @@ -1,4 +1,4 @@ -import ast +import ast from ..lang.handlers.assignment_handler import assignment_handler from ..lang.handlers.expression_handler import expression_handler @@ -10,6 +10,6 @@ def process(parsed_tree): 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]) \ No newline at end of file + assignment_handler(parsed_tree.body[0]) diff --git a/python/nucleoid/state.py b/python/nucleoid/state.py index 75ad601..78ec839 100644 --- a/python/nucleoid/state.py +++ b/python/nucleoid/state.py @@ -1 +1 @@ -variable_state = {} \ No newline at end of file +variable_state = {} diff --git a/src/lang/$nuc/$INSTANCE.js b/src/lang/$nuc/$INSTANCE.js index 0608ba2..8c494ac 100644 --- a/src/lang/$nuc/$INSTANCE.js +++ b/src/lang/$nuc/$INSTANCE.js @@ -12,6 +12,7 @@ function build(cls, object, name, args = []) { statement.obj = object; statement.nme = name; statement.args = args; + return statement; }