-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcodeDog
More file actions
executable file
·272 lines (245 loc) · 10.4 KB
/
Copy pathcodeDog
File metadata and controls
executable file
·272 lines (245 loc) · 10.4 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env python3
# CodeDog Program Maker
import subprocess
import sys; sys.dont_write_bytecode = True
def _is_required_pyparsing_version(version):
try:
major, minor = version.split(".")[:2]
return (int(major), int(minor)) >= (3, 3)
except (ValueError, TypeError, AttributeError):
return False
try:
import pyparsing
if not _is_required_pyparsing_version(getattr(pyparsing, "__version__", None)):
raise ModuleNotFoundError
except ModuleNotFoundError:
import depsResolve
if subprocess.call(["which", "pip3"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0:
depsResolve.installPyparsing()
else:
depsResolve.installPipPackage()
depsResolve.installPyparsing()
try:
import pyparsing
except Exception:
print("Error attempting to reimport pyparsing")
import progSpec
import codeDogParser
import buildDog
import TestDog
########## Library Shells
import libraryMngr
from codeGenerator import CodeGenerator
#import xlator_JavaScript
from xlator_Swift import Xlator_Swift
from xlator_Java import Xlator_Java
from xlator_CPP import Xlator_CPP
from xlator_Kotlin import Xlator_Kotlin
import xlator
import re
import os
import errno
import platform
import copy
import atexit
import threading
from progSpec import cdlog, cdErr
from os.path import abspath
if sys.version_info.major < 3 or (sys.version_info.major == 3 and sys.version_info.minor < 5):
print("\n\nERROR: CodeDog must be used with Python 3.5 or newer\n")
exit(1)
codeDogVersion = '2.0'
runArgs = sys.argv[1:]
if(len(runArgs) < 1):
print("\n usage: codeDog [-v] filename\n")
exit(1)
arg1 = runArgs[0]
if arg1=="-v" or arg1=='--version':
print("\n CodeDog application compiler version "+ codeDogVersion+"\n")
exit(1)
if arg1=="-b" or arg1=='--build':
name = ''
lastArg=1
if(len(runArgs) >1 and runArgs[1][0]!='-'): name = runArgs[1]; lastArg=2
buildDog.buildWithScons(name, runArgs[lastArg:])
exit(1)
if arg1[0]=='-':
print("Unsupported argument:", arg1)
exit(1)
atexit.register(progSpec.whenExit)
codeGenerator = CodeGenerator()
def GenerateProgram(classes, buildTags, tagsList, libsToUse):
result='No Language Generator Found for '+buildTags['Lang']
langGenTag = buildTags['Lang']
if(langGenTag == 'CPP'):
langName='C + +'
xlator = Xlator_CPP()
elif(langGenTag == 'Java'):
langName='J A V A'
xlator = Xlator_Java()
elif(langGenTag == 'Swift'):
langName='S W I F T'
xlator = Xlator_Swift()
elif(langGenTag == 'Kotlin'):
langName='K O T L I N'
xlator = Xlator_Kotlin()
else:
cdErr( "ERROR: No language generator found for ".format( langGenTag))
exit(1)
xlator.codeGen = codeGenerator
codeGenerator.xlator = xlator
result=codeGenerator.generate(classes, tagsList, libsToUse, langName)
return result
from timeit import default_timer as timer
def localCPU():
machine = platform.machine().lower()
if machine in ['x86_64', 'amd64']:
return 'amd64'
if machine in ['aarch64', 'arm64']:
return 'arm64'
return machine
def localPlatformTags():
platformID = platform.system()
if platformID == 'Linux':
return {'Platform': 'Linux', 'CPU': localCPU(), 'Lang': 'CPP', 'LangVersion': 'GNU'}
if platformID == 'Windows':
return {'Platform': 'Windows', 'CPU': localCPU(), 'Lang': 'CPP', 'LangVersion': 'MSVC'}
if platformID == 'Darwin':
return {'Platform': 'MacOS', 'CPU': localCPU(), 'Lang': 'Swift'}
cdErr("ERROR: Local platform '{}' is not supported.".format(platformID))
def localPlatformBuildName():
platformID = platform.system()
if platformID == 'Linux':
return 'LinuxBuild'
if platformID == 'Windows':
return 'WindowsBuild'
if platformID == 'Darwin':
return 'MacBuild'
cdErr("ERROR: Local platform '{}' is not supported.".format(platformID))
WEB_PLATFORM_DEFAULTS = {
'Platform': 'Web',
'CPU': 'wasm32',
'Lang': 'CPP',
'LangVersion': 'Emscripten',
}
def applyBuildSpecificDefaults(buildSpec):
buildName = buildSpec[0]
buildTags = buildSpec[1]
platformTag = progSpec.fetchTagValue([buildTags], 'Platform')
if isinstance(platformTag, str) and platformTag.lower() == 'web':
for tagName, tagValue in WEB_PLATFORM_DEFAULTS.items():
if not (tagName in buildTags):
buildTags[tagName] = tagValue
buildTags['Platform'] = 'Web'
return [buildName, buildTags]
localTags = localPlatformTags()
if isinstance(platformTag, str) and platformTag.lower() == 'local':
buildTags['Platform'] = localTags['Platform']
if not ('Platform' in buildTags):
buildTags['Platform'] = localTags['Platform']
if not ('CPU' in buildTags):
buildTags['CPU'] = localTags['CPU']
if not ('Lang' in buildTags):
buildTags['Lang'] = localTags['Lang']
if buildTags['Lang'] == localTags['Lang'] and 'LangVersion' in localTags and not ('LangVersion' in buildTags):
buildTags['LangVersion'] = localTags['LangVersion']
return [buildName, buildTags]
def normalizeBuildSpecs(buildSpecs):
if len(buildSpecs) == 0:
buildSpecs = [[localPlatformBuildName(), localPlatformTags()]]
return [applyBuildSpecificDefaults(buildSpec) for buildSpec in buildSpecs]
def tagListItems(tagValue):
if isinstance(tagValue, list):
return tagValue
extracted = progSpec.extractListFromTagList(tagValue)
if extracted:
return extracted
return []
def tagPlatformMap(tagValue):
if isinstance(tagValue, dict):
return tagValue
return progSpec.extractMapFromTagMap(tagValue)
def interfaceItemsForPlatform(interfaceTags, tagName, platform):
values = []
if tagName in interfaceTags:
values += tagListItems(interfaceTags[tagName])
byPlatformName = tagName + 'ByPlatform'
if byPlatformName in interfaceTags:
platformMap = tagPlatformMap(interfaceTags[byPlatformName])
if platform in platformMap:
values += tagListItems(platformMap[platform])
return values
def GenerateSystem(classes, buildSpecs, tags, macroDefs):
cdlog(0, "\n###################### G E N E R A T I N G P R O G R A M S P E C I F I C A T I O N")
count=0
for buildSpec in buildSpecs:
count+=1
buildName=buildSpec[0]
buildTags=buildSpec[1]
buildTags['buildName']= buildName
testMode=progSpec.fetchTagValue([tags, buildTags], 'testMode')
progSpec.MarkItems=True
if testMode=='makeTests' or testMode=='runTests':
cdlog(1, "GENERATING: Test Program")
testTagStore=TestDog.generateTestCode(classes, buildTags, tags, macroDefs)
cdlog(1, "Test Program Finished")
tagsList=[tags, buildTags, testTagStore]
else:
tagsList=[tags, buildTags]
#print("BUILDTAGS:", buildTags)
cdlog(0, "\n###################### G E N E R A T I N G S O U R C E C O D E S Y S T E M {} o f {}... ({})".format(count, len(buildSpecs), buildName))
startTime = timer()
buildDog.buildStatus("Choosing libraries for {} ({})".format(buildName, buildTags.get('Platform', 'unknown platform')))
libsToUse=libraryMngr.ChooseLibs(classes, buildTags, tags)
chooseLibsTime = timer()-startTime
buildDog.buildStatus("Selected {} library layer(s) in {:.1f}s".format(len(libsToUse), chooseLibsTime))
print("Time Elapsed:", chooseLibsTime)
codeGenStartTime = timer()
buildDog.buildStatus("Generating source for {}".format(buildName))
fileSpecs = GenerateProgram(classes, buildTags, tagsList, libsToUse)
buildDog.buildStatus("Generated {} source file(s) in {:.1f}s".format(len(fileSpecs), timer() - codeGenStartTime))
print("Marker: Code Gen Successful")
#GenerateBuildSystem()###################################################
fileName = progSpec.fetchTagValue(tagsList, 'FileName')
labelName = progSpec.fetchTagValue(tagsList, 'Title')
launchIconName = progSpec.fetchTagValue(tagsList, 'LaunchIcon')
programOrLibrary = progSpec.fetchTagValue(tagsList, 'ProgramOrLibrary')
langGenTag = buildTags['Lang']
libFiles=[]
tagsFromLibFiles = libraryMngr.getTagsFromLibFiles()
packageData = []
tools = []
buildDog.buildStatus("Collecting link libraries and package requirements")
for lib in libsToUse:
if 'interface' in tagsFromLibFiles[lib]:
interfaceTags = tagsFromLibFiles[lib]['interface']
libFiles += interfaceItemsForPlatform(interfaceTags, 'libFiles', buildTags['Platform'])
packageData += interfaceItemsForPlatform(interfaceTags, 'packages', buildTags['Platform'])
tools += interfaceItemsForPlatform(interfaceTags, 'tools', buildTags['Platform'])
#TODO: need debug mode and minimum version
platform=progSpec.fetchTagValue([tags, buildTags], 'Platform')
buildDog.buildStatus("Build inputs: {} link item(s), {} package(s), {} tool requirement(s)".format(len(libFiles), len(packageData), len(tools)))
#cdlog(1, "\nWRITING {} FILE(S) AND COMPILING...".format(len(fileSpecs)))
buildDog.build("-g", '14', fileName, labelName, launchIconName, libFiles, buildName, platform, fileSpecs, programOrLibrary, packageData, tools, buildTags)
print("Marker: Build Successful")
progSpec.rollBack(classes, tags)
# GenerateDocuments()
############################################# L o a d / P a r s e P r o g r a m S p e c
def processMainProgram():
cdlog(0, "\n###################### P R O C E S S I N G M A I N P R O G R A M ######################")
libraryMngr.findLibraryFiles()
filename = abspath(os.getcwd()) + "/" + arg1
[ProgSpec, objNames, macroDefs] = [ {}, [], {} ]
[tagStore, buildSpecs, classes, newClasses] = codeGenerator.loadProgSpecFromDogFile(filename, ProgSpec, objNames, {}, macroDefs)
print("Marker: Parse Successful")
tagStore['dogFilename']=filename
buildSpecs = normalizeBuildSpecs(buildSpecs)
macroDefs= {}
GenerateSystem(classes, buildSpecs, tagStore, macroDefs)
cdlog(0, "\n###################### D O N E")
progSpec.noError=True
threading.stack_size(67108864) # 64MB stack
sys.setrecursionlimit(5000)
thread = threading.Thread(target=processMainProgram)
thread.start()
thread.join()