Skip to content

Commit

Permalink
CLI, staticmethod, string, list, dict, way more
Browse files Browse the repository at this point in the history
  • Loading branch information
FIRST_NAME LAST_NAME authored and FIRST_NAME LAST_NAME committed Jul 8, 2023
1 parent 3048078 commit 156eab7
Show file tree
Hide file tree
Showing 30 changed files with 253 additions and 35 deletions.
121 changes: 88 additions & 33 deletions __main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,91 @@ def flake(self, message):
app = Flask(__name__)
translator = Translator()

@app.route('/', methods=["GET", "POST"])
def base_page():
code = (request.data).decode()
script_name = os.path.realpath(__file__)
folder = os.path.dirname(script_name)
luainit_path = os.path.join(folder, "header.lua")
header = ""
with open(luainit_path) as file:
header = file.read()

try:
lua_code = header+translator.translate(code)
except Exception as e:
return "CompileError!:"+str(e)

return lua_code

@app.route('/err', methods=["GET", "POST"])
def debug():
code = (request.data).decode()
rep = Reporter()
num = str(api.check(code, "roblox.py", rep))
print(num)
return rep.diagnostics

@app.route("/lib", methods=["GET"])
def library():
return translator.getluainit()
app.run(
host='0.0.0.0',
port=5555
)

def backwordreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)

# Mode is either "cli" or "web". Get it from sys.argv if not then ask using input()
mode = "plugin"
if len(sys.argv) > 1:
mode = sys.argv[1]
else:
mode = input("Mode (cli/plugin): ")

if mode == "plugin":
@app.route('/', methods=["GET", "POST"])
def base_page():
code = (request.data).decode()
script_name = os.path.realpath(__file__)
folder = os.path.dirname(script_name)
luainit_path = os.path.join(folder, "src/header.lua")
header = ""
with open(luainit_path) as file:
header = file.read()

try:
lua_code = header+translator.translate(code)
except Exception as e:
return "CompileError!:"+str(e)

return lua_code

@app.route('/err', methods=["GET", "POST"])
def debug():
code = (request.data).decode()
rep = Reporter()
num = str(api.check(code, "roblox.py", rep))
print(num)
return rep.diagnostics

@app.route("/lib", methods=["GET"])
def library():
return translator.getluainit()
app.run(
host='0.0.0.0',
port=5555
)
elif mode == "cli":
print("WARNING: AT THE MOMENT, THIS ONLY WORKS WITH THE TEST FOLDER.")
print("roblox-py: Ready to compile ", os.path.join(os.path.dirname(os.path.realpath(__file__)), "test"), "...\n Type 'exit' to exit, Press enter to compile.")
def cli():
# NOTE: Since this isnt packaged yet, using this will only check files inside of the test folder

# Get all the files inside of the path, look for all of them which are .py and even check inside of folders. If this is happening in the same directory as the script, do it in the sub directory test
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test")

for r, d, f in os.walk(path):
for file in f:
if '.py' in file:
# compile the file to a file with the same name and path but .lua
contents = ""
script_name = os.path.realpath(__file__)
folder = os.path.dirname(script_name)
luainit_path = os.path.join(folder, "src/header.lua")
header = ""
with open(luainit_path) as hfile:
header = hfile.read()
with open(os.path.join(r, file)) as rf:
contents = rf.read()
try:
lua_code = header+translator.translate(contents)
print("roblox-py: Compiled "+os.path.join(r, file))
# get the relative path of the file and replace .py with .lua
relative_path = backwordreplace(os.path.join(r, file),".py", ".lua", 1)
if not os.path.exists(relative_path):
open(relative_path, "x").close()
with open(relative_path, "w") as f:
f.write(lua_code)
except Exception as e:
print("CompileError"+str(e))


action = input("")
if action == "exit":
exit(0)
else:
cli()
cli()
else:
print("Invalid mode! (cli/plugin)")
exit(1)
Binary file added src/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added src/__pycache__/binopdesc.cpython-311.pyc
Binary file not shown.
Binary file added src/__pycache__/boolopdesc.cpython-311.pyc
Binary file not shown.
Binary file added src/__pycache__/cmpopdesc.cpython-311.pyc
Binary file not shown.
Binary file added src/__pycache__/config.cpython-311.pyc
Binary file not shown.
Binary file added src/__pycache__/context.cpython-311.pyc
Binary file not shown.
Binary file added src/__pycache__/loopcounter.cpython-311.pyc
Binary file not shown.
Binary file added src/__pycache__/nameconstdesc.cpython-311.pyc
Binary file not shown.
Binary file added src/__pycache__/nodevisitor.cpython-311.pyc
Binary file not shown.
Binary file added src/__pycache__/symbolsstack.cpython-311.pyc
Binary file not shown.
Binary file added src/__pycache__/tokenendmode.cpython-311.pyc
Binary file not shown.
Binary file added src/__pycache__/translator.cpython-311.pyc
Binary file not shown.
Binary file added src/__pycache__/unaryopdesc.cpython-311.pyc
Binary file not shown.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/nodevisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


class NodeVisitor(ast.NodeVisitor):
LUACODE = "[[luacode]]"
LUACODE = "[[lua]]"

"""Node visitor"""
def __init__(self, context=None, config=None):
Expand Down Expand Up @@ -491,7 +491,7 @@ def visit_Str(self, node):
elif self.context.last()["docstring"]:
self.emit('--[[ {} ]]'.format(node.s))
else:
self.emit('"{}"'.format(node.s))
self.emit('stringmeta "{}"'.format(node.s))

def visit_Subscript(self, node):
"""Visit subscript"""
Expand Down
31 changes: 31 additions & 0 deletions test/class.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--// Compiled using Roblox.py \\--


------------------------------------ BUILT IN -------------------------------
local stringmeta, list, dict, staticmethod, class, range, __name__, len, abs, str, int, sum, max, min, reversed, split, round, all, any, ord, char, callable, zip, float, format, hex, id, map = unpack(require(game.ReplicatedStorage["Roblox.py"])(script))
-----------------------------------------------------------------------------
local Example = class(function(Example)
function Example.__init__(self, name)
self.name = name
end
function Example.print_name(self)
print(self.name)
end
function Example.sethobby(self, hobby)
self.hobby = hobby
end
function Example.printhobby(self)
print(self.hobby)
end
function Example.setage(self, age)
self.age = age
end
function Example.printage(self)
print(self.age)
end
return Example
end, {})
local new = Example(stringmeta "John")
new.print_name()
new.sethobby(stringmeta "Roblox Game Development")
new.printhobby()
24 changes: 24 additions & 0 deletions test/class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Example:
def __init__(self, name):
self.name = name

def print_name(self):
print(self.name)

def sethobby(self, hobby):
self.hobby = hobby

def printhobby(self):
print(self.hobby)

def setage(self, age):
self.age = age

def printage(self):
print(self.age)


new = Example("John")
new.print_name()
new.sethobby("Roblox Game Development")
new.printhobby()
14 changes: 14 additions & 0 deletions test/dict.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--// Compiled using Roblox.py \\--


------------------------------------ BUILT IN -------------------------------
local stringmeta, list, dict, staticmethod, class, range, __name__, len, abs, str, int, sum, max, min, reversed, split, round, all, any, ord, char, callable, zip, float, format, hex, id, map = unpack(require(game.ReplicatedStorage["Roblox.py"])(script))
-----------------------------------------------------------------------------
local newdict = dict {}
local newdict[stringmeta "one"] = 1
local newdict[stringmeta "two"] = 2
local newdict[stringmeta "three"] = 3
local newdict[stringmeta "four"] = 4
for key in newdict do
print(key, newdict[key])
end
8 changes: 8 additions & 0 deletions test/dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
newdict = {}
newdict['one'] = 1
newdict['two'] = 2
newdict['three'] = 3
newdict['four'] = 4

for key in newdict:
print(key, newdict[key])
7 changes: 7 additions & 0 deletions test/helloworld.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--// Compiled using Roblox.py \\--


------------------------------------ BUILT IN -------------------------------
local stringmeta, list, dict, staticmethod, class, range, __name__, len, abs, str, int, sum, max, min, reversed, split, round, all, any, ord, char, callable, zip, float, format, hex, id, map = unpack(require(game.ReplicatedStorage["Roblox.py"])(script))
-----------------------------------------------------------------------------
print(stringmeta "Hello World!")
1 change: 1 addition & 0 deletions test/helloworld.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello World!")
17 changes: 17 additions & 0 deletions test/list.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--// Compiled using Roblox.py \\--


------------------------------------ BUILT IN -------------------------------
local stringmeta, list, dict, staticmethod, class, range, __name__, len, abs, str, int, sum, max, min, reversed, split, round, all, any, ord, char, callable, zip, float, format, hex, id, map = unpack(require(game.ReplicatedStorage["Roblox.py"])(script))
-----------------------------------------------------------------------------
local newlist = list {1, 2, 3, 4, 5}
newlist.append(6)
newlist.append(7)
newlist.append(8)
for item in newlist do
print(item)
end
print(newlist[0])
print(newlist[1])
newlist.sort()
newlist.reverse()
13 changes: 13 additions & 0 deletions test/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
newlist = [1, 2, 3, 4, 5]
newlist.append(6)
newlist.append(7)
newlist.append(8)

for item in newlist:
print(item)

print(newlist[0])
print(newlist[1])

newlist.sort()
newlist.reverse()
9 changes: 9 additions & 0 deletions test/memoryaddress.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--// Compiled using Roblox.py \\--


------------------------------------ BUILT IN -------------------------------
local stringmeta, list, dict, staticmethod, class, range, __name__, len, abs, str, int, sum, max, min, reversed, split, round, all, any, ord, char, callable, zip, float, format, hex, id, map = unpack(require(game.ReplicatedStorage["Roblox.py"])(script))
-----------------------------------------------------------------------------
local item = stringmeta "Hello!"
local location = id(item)
print(location)
4 changes: 4 additions & 0 deletions test/memoryaddress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
item = "Hello!"
location = id(item)

print(location)
16 changes: 16 additions & 0 deletions test/staticmethod.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--// Compiled using Roblox.py \\--


------------------------------------ BUILT IN -------------------------------
local stringmeta, list, dict, staticmethod, class, range, __name__, len, abs, str, int, sum, max, min, reversed, split, round, all, any, ord, char, callable, zip, float, format, hex, id, map = unpack(require(game.ReplicatedStorage["Roblox.py"])(script))
-----------------------------------------------------------------------------
local function foo(x)
print((math.fmod(stringmeta "executing foo(%s)", x)))
end
foo = staticmethod(foo)
local A = class(function(A)
A.foo = foo
return A
end, {})
local a = A()
a.foo(stringmeta "hi")
9 changes: 9 additions & 0 deletions test/staticmethod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@staticmethod
def foo(x):
print("executing foo(%s)" % (x))

class A:
foo = foo

a = A()
a.foo('hi')
8 changes: 8 additions & 0 deletions test/string.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--// Compiled using Roblox.py \\--


------------------------------------ BUILT IN -------------------------------
local stringmeta, list, dict, staticmethod, class, range, __name__, len, abs, str, int, sum, max, min, reversed, split, round, all, any, ord, char, callable, zip, float, format, hex, id, map = unpack(require(game.ReplicatedStorage["Roblox.py"])(script))
-----------------------------------------------------------------------------
local newstring = stringmeta "Hello World"
print(newstring)
2 changes: 2 additions & 0 deletions test/string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
newstring = 'Hello World'
print(newstring)

0 comments on commit 156eab7

Please sign in to comment.