jtalkc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #!/bin/bash
  2. #
  3. # This is a "compiler" for JTalk code. Run without arguments for help.
  4. # Get JTalk root directory from the location of this script.
  5. JTALK=`dirname ${0}`/..
  6. function usage {
  7. cat <<ENDOFHELP
  8. Usage: $0 [-N|D] [-K|C] [-o] [-O] [-m class] [-M file] [-i] [-I file] file1 [file2 ...] [Program]
  9. Will compile Jtalk files - either separately or into a runnable complete program.
  10. Files listed will be handled using these rules:
  11. *.js
  12. Files are concatenated in listed order. If not found we look in $JTALK/js
  13. *.st
  14. Files are compiled into .js files before concatenated. If not found we look
  15. in $JTALK/st.
  16. NOTE: Each file is currently considered to be a fileout of a single class
  17. category of the same name as the file!
  18. If no Program is specified each given .st file will be compiled into a .js file.
  19. Otherwise a <Program>.js file is linked together based on the options:
  20. -N or -D
  21. Compile for Node.js or D8 (V8 shell). Implies "-K -i -m Main" so boot.js and Kernel.js
  22. are added first and init.js is added last with a call to Main class>>main.
  23. -K
  24. Add libraries to get minimal JTalk Kernel running.
  25. -C
  26. Add libraries to get minimal JTalk Compiler running.
  27. -o
  28. Optimize each js file using the Google closure compiler. Using ~/compiler.jar
  29. -O
  30. Optimize <Program>.js using the Google closure compiler. Using ~/compiler.jar
  31. -l libray1,library2
  32. Load listed libraries (no spaces) in Compiler before compiling.
  33. -L
  34. Loads all libraries in directory js in Compiler before compiling.
  35. -i
  36. Add library standard initializer $JTALK/js/init.js
  37. -I file
  38. Add library initializer <file>.
  39. -m class
  40. Add call to #main in class <class>.
  41. -M file
  42. Add javascript file <file> at the end acting as main.
  43. Example invocations:
  44. Compile Kernel.st to Kernel.js:
  45. jtalkc Kernel.st
  46. Compile Hello.st to Hello.js and create complete program called
  47. Program.js for Node.js including boot.js, Kernel.js, init.js and
  48. adding a call to class method #main in class Hello:
  49. jtalkc -N -m Hello Hello.st Program
  50. Compile two .st files into corresponding .js files,
  51. and manually link with specific myboot.js, myKernel.js, myinit.js and main.js
  52. and create complete program called Program.js:
  53. jtalkc -M main.js -I myinit.js myboot.js myKernel.js Cat1.st Cat2.st Program
  54. ENDOFHELP
  55. exit 1;
  56. }
  57. # Check we at least got one argument
  58. if [ -z $1 ] ; then
  59. usage
  60. fi
  61. # Define our predefined library combinations
  62. BOOT="$JTALK/js/boot.js"
  63. KERNEL="$BOOT $JTALK/js/Kernel.js"
  64. COMPILER="$KERNEL $JTALK/js/Parser.js $JTALK/js/Compiler.js"
  65. CANVAS="$COMPILER $JTALK/js/Canvas.js"
  66. # Predefined initializer
  67. INITIALIZER="$JTALK/js/init.js"
  68. # Default values
  69. ENV=
  70. INIT=
  71. MAIN=
  72. MAINFILE=
  73. BASE=$KERNEL
  74. LOAD=
  75. # Read options and shift them away
  76. while getopts "NDKCoOl:LiI:M:m:h?" o; do
  77. case "$o" in
  78. N) ENV=NODE
  79. BASE=$KERNEL
  80. INIT=$INITIALIZER
  81. MAIN=Main;;
  82. D) ENV=D8
  83. BASE=$KERNEL
  84. INIT=$INITIALIZER
  85. MAIN=Main;;
  86. K) BASE=$KERNEL;;
  87. C) BASE=$COMPILER;;
  88. o) CLOSURE=true;;
  89. O) CLOSUREFULL=true;;
  90. l) LOAD=$OPTARG;;
  91. L) LOAD="$JTALK/*.js";;
  92. I) INIT=$INITIALIZER;;
  93. i) INIT=$OPTARG;;
  94. M) MAINFILE=$OPTARG;;
  95. m) MAIN=$OPTARG;;
  96. h) usage;;
  97. [?]) usage;;
  98. esac
  99. done
  100. shift $(($OPTIND - 1))
  101. # Define our Compiler
  102. # We need to add extra listed libraries in $LOAD not already in $CANVAS
  103. OURCOMPILER="$CANVAS $JTALK/js/init.js $JTALK/nodejs/nodecompile.js"
  104. # Combine supplied libraries
  105. LIBS="$BASE $ADDONS"
  106. # Get a unique tempdir and make it get auto removed on exit
  107. TMPDIR=`mktemp -d`
  108. trap "rm -rf $TMPDIR" EXIT
  109. # --------------------------------------------------
  110. # Collect libraries and Smalltalk files looking
  111. # both locally and in $JTALK/js and $JTALK/st
  112. # --------------------------------------------------
  113. PROGRAM=
  114. until [ "$*" = "" ]
  115. do
  116. case $1 in
  117. *.st)
  118. CATEGORY=`basename $1 .st`
  119. if [ -f "$1" ]; then
  120. COMPILE="$COMPILE $1 $CATEGORY"
  121. COMPILED="$COMPILED $CATEGORY.js"
  122. else
  123. if [ -f $JTALK/st/$1 ]; then
  124. COMPILE="$COMPILE $JTALK/st/$1 $CATEGORY"
  125. COMPILED="$COMPILED $CATEGORY.js"
  126. else
  127. echo "JTalk file not found: $1"
  128. exit 1
  129. fi
  130. fi
  131. ;;
  132. *.js)
  133. if [ -f "$1" ]; then
  134. LIBS="$LIBS $1"
  135. else
  136. if [ -f $JTALK/js/$1 ]; then
  137. LIBS="$LIBS $JTALK/js/$1"
  138. else
  139. echo "Javascript file not found: $1"
  140. exit 1
  141. fi
  142. fi
  143. ;;
  144. *)
  145. # Will end up being the last non js/st argument
  146. PROGRAM=$1
  147. ;;
  148. esac
  149. shift
  150. done
  151. # --------------------------------------------------
  152. # Actual compilation phase of collected .st files
  153. # --------------------------------------------------
  154. # Create compiler dynamically
  155. cat $OURCOMPILER > $TMPDIR/compiler.js
  156. # Compile all collected .st files to .js
  157. echo "Compiling ..."
  158. node $TMPDIR/compiler.js $COMPILE
  159. # Verify all .js files corresponding to .st files were created, otherwise exit
  160. IFS=" "
  161. for FILE in $COMPILED
  162. do
  163. if [ ! -f "$FILE" ]; then
  164. echo "Failed compilation of $FILE, exiting."
  165. exit 1
  166. fi
  167. done
  168. if [ ! -z $CLOSURE ]; then
  169. echo "Compiling all js files using Google closure compiler."
  170. ALLJSFILES="$COMPILED $LIBS"
  171. for FILE in $ALLJSFILES
  172. do
  173. mv $FILE $FILE.original
  174. java -jar ~/compiler.jar --js $FILE.original --js_output_file $FILE
  175. rm $FILE.original
  176. done
  177. fi
  178. if [ -z $PROGRAM ]; then
  179. echo "Done."
  180. exit 0
  181. fi
  182. # --------------------------------------------------
  183. # Now we start composing resulting javascript file.
  184. # --------------------------------------------------
  185. # Add collected libraries to libs.js file.
  186. if [ ! -z "$LIBS" ]; then
  187. echo "Adding libraries $LIBS ..."
  188. cat $LIBS > $TMPDIR/libs.js
  189. LIBS=$TMPDIR/libs.js
  190. fi
  191. echo "Adding Jtalk code$COMPILED ..."
  192. # Check for init file
  193. if [ ! -z "$INIT" ]; then
  194. if [ -f "$INIT" ]; then
  195. echo "Adding initializer $INIT ..."
  196. else
  197. echo "Can not find init file $INIT, exiting."
  198. exit 1
  199. fi
  200. fi
  201. # Check for adding main
  202. if [ ! -z "$MAIN" ]; then
  203. echo "Adding call to $MAIN class >> main ..."
  204. echo "smalltalk.$MAIN._main()" > $TMPDIR/main.js
  205. MAIN=$TMPDIR/main.js
  206. fi
  207. # Check for adding main file
  208. if [ ! -z "$MAINFILE" ]; then
  209. if [ -f "$MAINFILE" ]; then
  210. echo "Adding main as $MAINFILE ..."
  211. else
  212. echo "Can not find main file $MAINFILE, exiting."
  213. exit 1
  214. fi
  215. MAIN=$MAINFILE
  216. fi
  217. # And finally concatenate Program.js
  218. echo "Writing $PROGRAM.js ..."
  219. cat $LIBS $COMPILED $INIT $MAIN > $PROGRAM.js
  220. echo "Done."
  221. if [ ! -z $CLOSUREFULL ]; then
  222. echo "Compiling $PROGRAM.js file using Google closure compiler."
  223. mv $PROGRAM.js $PROGRAM.js.original
  224. java -jar ~/compiler.jar --js $PROGRAM.js.original --js_output_file $PROGRAM.js
  225. rm $PROGRAM.js.original
  226. echo "Done."
  227. fi