Skip to content

Commit

Permalink
0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
eanorambuena committed Jan 9, 2022
1 parent 51e2b1e commit 941991e
Show file tree
Hide file tree
Showing 48 changed files with 3,352 additions and 0 deletions.
30 changes: 30 additions & 0 deletions build/lib/adam/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
__init__.py
Initialization file for adam package.
"""

author = "eanorambuena"
author_email = "eanorambuena@uc.cl"

# MIT License
#
# Copyright (c) 2021 Emmanuel Norambuena Sepulveda
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
36 changes: 36 additions & 0 deletions build/lib/adam/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
__main__.py
The entry point of the program. It drives the program, and allows to
use the program in console.
"""

author = "eanorambuena"
author_email = "eanorambuena@uc.cl"

# MIT License
#
# Copyright (c) 2021 Emmanuel Norambuena Sepulveda
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from adam.app import main

if __name__ == "__main__":
main()
132 changes: 132 additions & 0 deletions build/lib/adam/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
"""
app.py
The entry point of the program from Python Scripts. It drives the program, and allows to
use the program in console.
"""

author = "eanorambuena"
author_email = "eanorambuena@uc.cl"

# MIT License
#
# Copyright (c) 2021 Emmanuel Norambuena Sepulveda
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import sys

from adam.assembly import Assembler
from adam.error import ArgumentError, FileError, RuntimeError
from adam.run import execute_driver

def get_argument(position, plural = False):
"""
Get an argument from command line, with a specific position.
You can get more than one argument, if you set 'plural' to True.
It receives the following arguments:
position: The position of the argument in the command line.
plural: If you want to get more than one argument, set it to True.
"""

if plural:

try:
return sys.argv[position:]
except:
return ["none"]

try:
return sys.argv[position]
except:
return "none"

def main():
params = sys.argv[:]

if len(params) >= 2:

if len(params) >= 3:

try:
file = sys.argv[2]

arg = get_argument(3)
args = get_argument(3, True)

# Switch command

command = params[1]

if command == "set-time-unit":

if file == "ms":
miliseconds = True
elif file == "s":
miliseconds = False
else:
miliseconds = True

f = open("adam/nateve_vars", "w")
print(miliseconds, file = f)
f.close()

elif command == "build":
assembler = Assembler(file, [arg])
assembler.assemble()

elif command == "run":
assembler = Assembler(file, [arg])
assembler.run()

elif command == "compile":

if len(params) >= 4:

arg2 = get_argument(4)

assembler = Assembler(file, [arg2])
assembler.compile(arg)

else:
ArgumentError(None, "no executable file name specified")

elif command == "run-init-loop":

if len(params) >= 4:

arg2 = get_argument(4)
"""
file, modules = build(file, arg2)
file2, modules = build(arg, arg2, driver = "import " + file + "\nwhile True:\n\timport {}")
"""
execute_driver()

else:
ArgumentError(None, "no loop file specified")

except IndexError:
RuntimeError(None, "Error in compilation")

else:
FileError(None, "no file or argument specified")

else:
ArgumentError(None, "no arguments specified")
178 changes: 178 additions & 0 deletions build/lib/adam/assembly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
"""
assembly.py
This module contains the following classes:
Assembler
This class is used to assemble the Nateve source code into a final python file.
Module
This class is used to represent a module in the assembly process.
"""

author = "eanorambuena"
author_email = "eanorambuena@uc.cl"

# MIT License
#
# Copyright (c) 2021 Emmanuel Norambuena Sepulveda
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import math, os, subprocess, sys

from adam.build import build
from adam.run import execute_driver, driver_file

class Assembler():
"""
This class is used to assemble the Nateve source code into a final python file.
It has the following attributes:
root: The name of the root module.
modules: The modules to be assembled.
args: The arguments to be passed to the modules.
It has the following methods:
add_module: This method is used to add a module to the assembly process.
assemble: This method is used to assemble the modules into a final python file.
compile: This method is used to compile the root module into an executable.
run: This method is used to run the root module.
"""

def __init__(self, root_module, args = ["none"]):
"""
This method is used to initialize the Assembler class.
It takes the following arguments:
root_module: The name of the root module.
args: The arguments to be passed to the root module.
"""

self.root = root_module
self.modules = []
self.args = args
self.add_module(self.root)

def add_module(self, module_name):
"""
This method is used to add a module to the assembly process.
It takes the following arguments:
module_name: The name of the module to be added.
"""

self.modules.append(Module(module_name))

module = Module(module_name, self.args)
self.modules.append(module)

for module_name in module.modules_names:
self.add_module(module_name)

def __str__(self):
"""
This method returns a string representation of the Assembler class.
"""

result = ""

for module in self.modules:
result += module.__str__() + "\n"

return result

def assemble(self):
"""
This method is used to assemble the modules into a final python file.
"""

file = open(driver_file + ".py", "w")

text = ""

n = math.floor(len(self.modules) / 2)

for i in range(n):
temp = self.modules[i]
self.modules[i] = self.modules[-(i + 1)]
self.modules[-(i + 1)] = temp

for module in self.modules:
text += module.content + "\n"

n_text = math.floor(len(text) / 2) # Skip modules double-counting

file.write(text[:n_text])

file.close()

def compile(self, name):
"""
This method is used to compile the root module into an executable.
It takes the following arguments:
name: The name of the executable.
"""

build(name)

self.assemble()
subprocess.call([sys.executable, "-m", "PyInstaller", "--onefile", "--name", f'{name}', f'{self.file}.py'])

def run(self):
"""
This method is used to run the root module.
"""

self.assemble()
execute_driver()

if os.path.exists(driver_file + ".py"):
os.remove(driver_file + ".py")

class Module():
"""
This class is used to represent a module in the assembly process.
It has the following attributes:
name: The name of the module.
modules_names: The names of the modules imported by the module.
content: The content of the module.
"""
def __init__(self, name, args = ["none"]):
"""
This method is used to initialize the Module class.
It takes the following arguments:
name: The name of the module.
args: The arguments to be passed to the module.
"""

self.name = name

file_name, self.modules_names = build(name, args, driver = "import {}")
file = open(file_name + ".py", "r")

self.content = file.read() + "\n"

file.close()

def __str__(self):
"""
This method returns a string representation of the Module class.
"""

return f"Module {self.name}"

Loading

0 comments on commit 941991e

Please sign in to comment.