Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
chyanju committed Mar 29, 2024
1 parent 8a504ac commit 402ad2f
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions vanguard/aleo/grammar/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
from .misc import *

class AleoEnvironment(AleoNode):
"""An AleoEnvironment is for holding all the programs in a project, and provides management of execution, memory, etc.."""

@staticmethod
def from_project(build_path: Union[str, Path], main_file: str="main.aleo"):
"""Create an environment from a project build path"""
"""Creates an environment from a project build path.
Args:
- build_path (str or Path): the path to the Aleo build folder
- main_file (str): the file name of the main aleo program in the project (default: main.aleo)
Rets:
- (AleoEnvironment): an AleoEnvironment object
"""
path = build_path if isinstance(build_path, Path) else Path(build_path)
_programs = {}

Expand All @@ -38,7 +45,12 @@ def from_project(build_path: Union[str, Path], main_file: str="main.aleo"):

@staticmethod
def from_program(main_path: Union[str, Path]):
"""Create an environment from a single program, ignoring all dependencies (usually for debugging)"""
"""Creates an environment from a single program, ignoring all dependencies (usually for debugging).
Args:
- main_path (str or Path): the path of Aleo file to load
Rets:
- (AleoEnvironment): an AleoEnvironment object
"""
_programs = {}

# load and deploy
Expand All @@ -51,10 +63,22 @@ def from_program(main_path: Union[str, Path]):
return AleoEnvironment(_programs, _main)

def __init__(self, programs, main):
"""Initializes an AleoEnvironment object.
Args:
- programs (list): a list of AleoPrograms
- main (AleoProgram): the main AleoProgram
"""
self.programs = programs
self.main = main

def resolve_function(self, pid, callee):
"""Locates a function.
Args:
- pid (str): program id
- callee (Aleoidentifier or AleoLocator): target location to call
Rets:
- (tuple of strs): a tuple of the target location (program id and function id) in str
"""
_pid = None
_fid = None
match callee:
Expand All @@ -72,7 +96,15 @@ def resolve_function(self, pid, callee):
else:
raise NotImplementedError(f"Cannot locate function/closure, got: {_fid}")

def mget(self, pid: str, id: Union[AleoRegister, AleoRegisterAccess], ctx: dict=None):
def mget(self, pid: str, id: AleoNode, ctx: dict=None):
"""Reads memory location.
Args:
- pid (str): program id
- id (AleoNode): target location to read
- ctx (dict): additional local context (value environment) to consider
Rets:
- (Any): value stored in target location of memory
"""
match id:

case AleoRegister():
Expand Down Expand Up @@ -110,7 +142,14 @@ def mget(self, pid: str, id: Union[AleoRegister, AleoRegisterAccess], ctx: dict=
raise NotImplementedError(f"Unsupported type of id, got: {id} of type {type(id)}")


def mset(self, pid: str, id: Union[AleoRegister, AleoRegisterAccess], val: AleoLiteral, ctx: dict=None):
def mset(self, pid: str, id: AleoNode, val: AleoLiteral, ctx: dict=None):
"""Updates memory location.
Args:
- pid (str): program id
- id (AleoNode): target location to write to
- val (aleoLiteral): value to be written to memory
- ctx (dict): additional local context (value environment) to consider
"""
match id:

case AleoRegister():
Expand Down Expand Up @@ -186,6 +225,16 @@ def from_json(node):
return AleoProgram(_id, _imports, _structs, _records, _mappings, _functions, _closures)

def __init__(self, id, imports, structs, records, mappings, functions, closures, **kwargs):
"""Initializes an AleoProgram object.
Args:
- id (AleoProgramId): program id
- imports (list of AleoImport): a list of AleoImport
- structs (list of AleoStruct): a list of AleoStruct
- records (list of AleoRecord): a list of AleoRecord
- mappings (list of AleoMapping): a list of AleoMapping
- functions (list of AleoFunction): a list of AleoFunction
- closures (list of AleoClosure): a list of AleoClosure
"""
super().__init__(**kwargs)
self.id = id
self.imports = imports
Expand All @@ -202,6 +251,7 @@ def __init__(self, id, imports, structs, records, mappings, functions, closures,
self.mem[key] = {}

def reset_memory(self):
"""Resets the memory of the program."""
self.mem = {}

def __str__(self):
Expand Down

0 comments on commit 402ad2f

Please sign in to comment.