-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluvune.luau
More file actions
75 lines (60 loc) · 1.98 KB
/
luvune.luau
File metadata and controls
75 lines (60 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
local fs = require("@lune/fs")
local process = require("@lune/process")
-- 1. Initialize the table FIRST
local Luvune = {}
local KEYWORD_MAP = {
["def"] = "function",
["elif"] = "elseif",
["if"] = "if",
["else"] = "else",
["while"] = "while",
["for"] = "for",
["None"] = "nil",
["True"] = "true",
["False"] = "false",
["import"] = "local",
["pass"] = "-- pass",
["print"] = "print",
}
-- 2. Define the conversion logic
function Luvune.convertLine(line: string): string
-- Handle Python comments first so they don't break Luau
local converted = line:gsub("#", "--")
-- Replace keywords
for pyKey, luauKey in pairs(KEYWORD_MAP) do
converted = converted:gsub("%f[%a]" .. pyKey .. "%f[%A]", luauKey)
end
-- Handle Block Headers (Python ":" to Luau "then/do")
if converted:match("^%s*if") or converted:match("^%s*elseif") then
converted = converted:gsub(":", " then")
elseif converted:match("^%s*while") or converted:match("^%s*for") then
converted = converted:gsub(":", " do")
elseif converted:match("^%s*function") then
converted = converted:gsub(":", "")
end
return converted
end
-- 3. Execution Logic
local function main()
local args = process.args
local inputPath = args[1]
if not inputPath then
print("Usage: lune run luvune.luau <filename.py>")
return
end
if not fs.isFile(inputPath) then
print("Error: File not found -> " .. inputPath)
return
end
local sourceCode = fs.readFile(inputPath)
local outputLines = {}
print("Luvune is processing: " .. inputPath)
for line in sourceCode:gmatch("[^\r\n]+") do
-- Now Luvune.convertLine is guaranteed to exist
table.insert(outputLines, Luvune.convertLine(line))
end
local outputPath = inputPath:gsub("%.py$", "") .. ".luau"
fs.writeFile(outputPath, table.concat(outputLines, "\n"))
print("Successfully created: " .. outputPath)
end
main()