bcc.bmx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. ' Copyright (c) 2013-2024 Bruce A Henderson
  2. '
  3. ' Based on the public domain Monkey "trans" by Mark Sibly
  4. '
  5. ' This software is provided 'as-is', without any express or implied
  6. ' warranty. In no event will the authors be held liable for any damages
  7. ' arising from the use of this software.
  8. '
  9. ' Permission is granted to anyone to use this software for any purpose,
  10. ' including commercial applications, and to alter it and redistribute it
  11. ' freely, subject to the following restrictions:
  12. '
  13. ' 1. The origin of this software must not be misrepresented; you must not
  14. ' claim that you wrote the original software. If you use this software
  15. ' in a product, an acknowledgment in the product documentation would be
  16. ' appreciated but is not required.
  17. '
  18. ' 2. Altered source versions must be plainly marked as such, and must not be
  19. ' misrepresented as being the original software.
  20. '
  21. ' 3. This notice may not be removed or altered from any source
  22. ' distribution.
  23. '
  24. SuperStrict
  25. Framework brl.StandardIO
  26. Import "ctranslator.bmx"
  27. Local args:String[] = ParseArgs(AppArgs[1..])
  28. If args.length = 0 Then
  29. Print "bcc[ng] Release Version " + BCC_VERSION
  30. End
  31. End If
  32. If args.length <> 1 Then
  33. CmdError("Command line error")
  34. End If
  35. opt_filepath = args[0]
  36. If opt_buildtype = BUILDTYPE_MODULE Then
  37. If opt_filepath.ToLower() = ModuleSource(opt_modulename).ToLower() Then
  38. opt_ismain = True
  39. End If
  40. End If
  41. TGenProcessor.processor = New TParser
  42. Local app:TAppDecl
  43. Local trans:TCTranslator
  44. Try
  45. If opt_verbose Then
  46. Print "Parsing..."
  47. End If
  48. app = ParseApp(opt_filepath)
  49. If opt_verbose Then
  50. Print "Semanting..."
  51. End If
  52. app.Semant()
  53. trans = New TCTranslator
  54. trans.TransApp(app)
  55. Catch error:String
  56. WriteStderr error
  57. exit_(-1) ' return a non-zero exit code
  58. End Try
  59. Local makeApp:Int = False
  60. If opt_apptype Then
  61. makeApp = True
  62. End If
  63. Local mung:String = FileMung(makeApp)
  64. SaveInterface(opt_filepath, trans, mung)
  65. SaveHeader(opt_filepath, trans, mung)
  66. SaveSource(opt_filepath, trans, mung)
  67. SaveIncBinHeader(opt_filepath, trans, FileMung(False), app)
  68. SaveDef(opt_filepath, trans, mung, app)
  69. Function SaveInterface(file:String, trans:TCTranslator, mung:String)
  70. If opt_verbose Then
  71. Print "Generating interface..."
  72. End If
  73. Local path:String
  74. If opt_buildtype = BUILDTYPE_MODULE Then
  75. If opt_ismain Then
  76. ' module interface
  77. path = ModuleInterface(opt_modulename, mung)
  78. Else
  79. ' file interface
  80. path = OutputFilePath(file, mung, "i")
  81. End If
  82. Else
  83. ' file interface
  84. path = OutputFilePath(file, mung, "i")
  85. End If
  86. SaveText(trans.JoinLines("interface"), path)
  87. End Function
  88. Function SaveHeader(file:String, trans:TCTranslator, mung:String)
  89. If opt_verbose Then
  90. Print "Generating header..."
  91. End If
  92. Local path:String = OutputFilePath(file, mung, "h")
  93. Local header:String = BuildHeaderName(path).ToUpper()
  94. Local text:String = HeaderComment()
  95. text :+ "#ifndef " + header + "~n"
  96. text :+ "#define " + header + "~n~n"
  97. If opt_buildtype = BUILDTYPE_MODULE And opt_modulename = "brl.blitz" Then
  98. text :+ "#include <brl.mod/blitz.mod/blitz.h>~n"
  99. End If
  100. text :+ trans.JoinLines("head")
  101. text :+ "~n~n#endif~n"
  102. SaveText(text, path)
  103. End Function
  104. Function SaveSource(file:String, trans:TCTranslator, mung:String)
  105. If opt_verbose Then
  106. Print "Generating source..."
  107. End If
  108. Local path:String = OutputFilePath(file, mung, "c")
  109. Local pre:String = trans.JoinLines("pre_source")
  110. Local def_data:String = trans.JoinLines("def_data")
  111. Local src:String = trans.JoinLines("source")
  112. Local txt:String = pre + "~n"
  113. If def_data Then
  114. txt :+ def_data + "~n"
  115. End If
  116. txt :+ src
  117. SaveText(txt, path)
  118. End Function
  119. Function SaveIncBinHeader(file:String, trans:TCTranslator, mung:String, app:TAppDecl)
  120. If app.genIncBinHeader Then
  121. Local ibFile:String = "incbin"
  122. If opt_legacy_incbin Then
  123. ibFile :+ ".c"
  124. Else
  125. ibFile :+ "2.c"
  126. End If
  127. Local path:String = OutputFilePath(file, mung, ibFile)
  128. SaveText(trans.JoinLines("incbin"), path)
  129. End If
  130. End Function
  131. Function SaveDef(file:String, trans:TCTranslator, mung:String, app:TAppDecl)
  132. If opt_def And opt_apptype And opt_platform = "win32" Then
  133. If opt_verbose Then
  134. Print "Generating def..."
  135. End If
  136. Local path:String = ExtractDir(opt_filepath) + "/" + StripExt(StripDir(opt_filepath)) + ".def"
  137. SaveText(trans.JoinLines("def"), path)
  138. End If
  139. End Function