-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcfunction.nim
213 lines (148 loc) · 4.64 KB
/
mcfunction.nim
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
## MCFunction assembler
## by Lucilla
import os, strutils, tables
type
Namespace = string
FilePath = string
FileName = string
MCFnName = string
MCFnCode = string
LineNumber = int
MCFnNames = seq[MCFnName]
MCFnLines = seq[MCFnCode]
NSFiles = Table[Namespace, MCFnNames]
Namespaces = Table[MCFnName, Namespace]
FilePaths = Table[MCFnName, FilePath]
FileNames = Table[MCFnName, FileName]
MCFunctions = Table[MCFnName, MCFnLines]
const
fWord = "function"
fileEnding = ".mcfunction"
prefix = "##"
pComment = '#'
pInclude = '+'
pNamespace = ':'
pFilePath = '/'
pStartCode = '{'
pEndCode = '}'
aliases = toTable {
"ex": "execute",
"fn": "function"
}
var
namespace {.global.}: Namespace
filePath {.global.}: FilePath
nsFiles {.global.}: NSFiles
namespaces {.global.}: Namespaces
filePaths {.global.}: FilePaths
fileNames {.global.}: FileNames
mcFunctions {.global.}: MCFunctions
proc generatePath(s: MCFnName): string =
return namespaces[s] & ":" & (filePaths[s] / fileNames[s]).replace(DirSep, AltSep)
proc replaceNames(l: MCFnCode, kw: string): MCFnCode =
var
rLeft, rRight, rStop: int
line, lLE, lLT, lEQ, lGE, lGT, lNew: MCFnCode
let
keyword = kw & " "
kwLen = keyword.len
line = l & " "
rLeft = line.find(keyword, rStop)
while rLeft >= 0:
rLeft = rLeft + kwLen
lLT = line[0 .. rLeft - 1]
lGE = line[rLeft .. ^1]
rRight = lGE.find(" "); if rRight < 0: break
lEQ = lGE[0 .. rRight - 1]
lGT = lGE[rRight .. ^1]
lNew = lEQ.generatePath
lLE = lLT & lNew
line = lLE & lGT
rStop = lLE.len
rLeft = line.find(keyword, rStop)
line.removeSuffix(" ")
return line
proc mcPrepareFile(lines: MCFnLines): MCFnCode =
var
line: MCFnCode
linesOut: MCFnLines = newSeqOfCap[MCFnCode](lines.len)
for i, l in pairs(lines):
line = l
if line.isEmptyOrWhitespace or line[0] == pComment:
continue
for r_i, r_o in pairs(aliases):
line = line.replaceWord(r_i, r_o)
line = line.strip
line = line.replaceNames(fWord)
linesOut.add(line)
return linesOut.join("\p")
proc mcRequestFiles(lines: MCFnLines) =
var
line: MCFnCode
linePair: MCFnLines
lineStart: LineNumber
lineEnd: LineNumber
fnName: MCFnName
fnPath: FilePath
fnParent: FilePath
fnFile: FileName
for i, l in pairs(lines):
line = l
if line.isEmptyOrWhitespace:
continue
if line.startsWith(prefix):
line.removePrefix(prefix)
if line.startsWith(pNamespace):
namespace = line[1 .. ^1].strip
if line.startsWith(pFilePath):
filePath = line[1 .. ^1].strip
if line.startsWith(pInclude):
let newFile = line[1 .. ^1].strip
let newCode = readFile(newFile & fileEnding).splitLines
mcRequestFiles(newCode)
if line.startsWith(pStartCode):
lineStart = i + 1
linePair = line[1 .. ^1].splitWhitespace
fnName = linePair[0]
fnPath = linePair[1]
fnParent = fnPath.parentDir
fnFile = fnPath.extractFilename
if fnParent == ".": fnParent = ""
if namespace in nsFiles:
nsFiles[namespace].add(fnName)
else:
nsFiles[namespace] = @[fnName]
namespaces[fnName] = namespace
filePaths[fnName] = filePath / fnParent
fileNames[fnName] = fnFile
if line.startsWith(pEndCode):
lineEnd = i - 1
mcFunctions[fnName] = lines[lineStart .. lineEnd]
proc mcMakeFiles(dir: FilePath) =
var
innerDir: FilePath
innerPath: FilePath
filePath: FilePath
fileName: FileName
fileCode: MCFnCode
let home = getCurrentDir() / dir / "data"
createDir(home)
setCurrentDir(home)
for namespace, fnNames in pairs(nsFiles):
innerDir = home / namespace / "functions"
createDir(innerDir)
setCurrentDir(innerDir)
for fnName in fnNames:
filePath = filePaths[fnName]
fileName = fileNames[fnName] & fileEnding
fileCode = mcPrepareFile(mcFunctions[fnName])
innerPath = innerDir / filePath
createDir(innerPath)
setCurrentDir(innerPath)
writeFile(fileName, fileCode)
setCurrentDir(innerDir)
setCurrentDir(home)
let file = paramStr(1)
let sourceCode = readFile(file).splitLines
mcRequestFiles(sourceCode)
mcMakeFiles(file.splitFile.name)