bcc.bmx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ' Copyright (c) 2013-2016 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 " + 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. Local app:TAppDecl
  42. Local trans:TCTranslator
  43. Try
  44. If opt_verbose Then
  45. Print "Parsing..."
  46. End If
  47. app = ParseApp(opt_filepath)
  48. If opt_verbose Then
  49. Print "Semanting..."
  50. End If
  51. app.Semant()
  52. trans = New TCTranslator
  53. trans.TransApp(app)
  54. Catch error:String
  55. WriteStderr error
  56. exit_(-1) ' return a non-zero exit code
  57. End Try
  58. Local makeApp:Int = False
  59. If opt_apptype Then
  60. makeApp = True
  61. End If
  62. Local mung:String = FileMung(makeApp)
  63. SaveInterface(opt_filepath, trans, mung)
  64. SaveHeader(opt_filepath, trans, mung)
  65. SaveSource(opt_filepath, trans, mung)
  66. SaveIncBinHeader(opt_filepath, trans, FileMung(False), app)
  67. Function SaveInterface(file:String, trans:TCTranslator, mung:String)
  68. If opt_verbose Then
  69. Print "Generating interface..."
  70. End If
  71. Local path:String
  72. If opt_buildtype = BUILDTYPE_MODULE Then
  73. If opt_ismain Then
  74. ' module interface
  75. path = ModuleInterface(opt_modulename, mung)
  76. Else
  77. ' file interface
  78. path = OutputFilePath(file, mung, "i")
  79. End If
  80. Else
  81. ' file interface
  82. path = OutputFilePath(file, mung, "i")
  83. End If
  84. SaveText(trans.JoinLines("interface"), path)
  85. End Function
  86. Function SaveHeader(file:String, trans:TCTranslator, mung:String)
  87. If opt_verbose Then
  88. Print "Generating header..."
  89. End If
  90. Local path:String = OutputFilePath(file, mung, "h")
  91. Local header:String = BuildHeaderName(path).ToUpper()
  92. Local text:String = HeaderComment()
  93. text :+ "#ifndef " + header + "~n"
  94. text :+ "#define " + header + "~n~n"
  95. If opt_buildtype = BUILDTYPE_MODULE And opt_modulename = "brl.blitz" Then
  96. text :+ "#include <brl.mod/blitz.mod/blitz.h>~n"
  97. End If
  98. text :+ trans.JoinLines("head")
  99. text :+ "~n~n#endif~n"
  100. SaveText(text, path)
  101. End Function
  102. Function SaveSource(file:String, trans:TCTranslator, mung:String)
  103. If opt_verbose Then
  104. Print "Generating source..."
  105. End If
  106. Local path:String = OutputFilePath(file, mung, "c")
  107. Local pre:String = trans.JoinLines("pre_source")
  108. Local src:String = trans.JoinLines("source")
  109. SaveText(pre + "~n" + src, path)
  110. End Function
  111. Function SaveIncBinHeader(file:String, trans:TCTranslator, mung:String, app:TAppDecl)
  112. If app.genIncBinHeader Then
  113. Local path:String = OutputFilePath(file, mung, "incbin.h")
  114. SaveText(trans.JoinLines("incbin"), path)
  115. End If
  116. End Function