1
0

jtalkc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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] [-m class] [-M file] [-i] [-I file] file1 [file2 ...] [Program]
  9. Will compile Jtalk files in a variety of ways.
  10. Files listed will be handled using these rules:
  11. *.js files are concatenated as is. If not found we look in $JTALK/js
  12. *.st files are compiled into .js files. If not found we look in $JTALK/st.
  13. Each file is currently considered to be a fileout of a single class
  14. category of the same name as the file.
  15. If no Program is specified only each given .st file will be compiled into a .js file.
  16. Otherwise a Program.js file is linked together based on the given options described below:
  17. -N or -D
  18. Compile for Node.js or D8 (V8 shell). Defaults to "-Kim Main" so boot.js and Kernel.js
  19. are added first and init.js is added last with a call to Main class>>main.
  20. -K
  21. Add libraries to get minimal JTalk Kernel running.
  22. -C
  23. Add libraries to get minimal JTalk Compiler running.
  24. -i
  25. Add library standard initializer $JTALK/js/init.js
  26. -I file
  27. Add library initializer <file>.
  28. -m class
  29. Add call to #main in class <class>.
  30. -M file
  31. Add javascript file <file> at the end acting as main.
  32. Example invocations:
  33. Compile Kernel.st to Kernel.js:
  34. jtalkc Kernel.st
  35. Compile Hello.st to Hello.js and create complete program called
  36. Program.js for Node.js including boot.js, Kernel.js, init.js and
  37. adding a call to class method #main in class Hello:
  38. jtalkc -N -m Hello Hello.st Program
  39. Compile two .st files into corresponding .js files,
  40. and manually link with specific myboot.js, myKernel.js, myinit.js and main.js
  41. and create complete program called Program.js:
  42. jtalkc -M main.js -I myinit.js myboot.js myKernel.js Cat1.st Cat2.st Program
  43. ENDOFHELP
  44. exit 1;
  45. }
  46. # Check we at least got one argument
  47. if [ -z $1 ] ; then
  48. usage
  49. fi
  50. # Define our predefined library combinations
  51. BOOT="$JTALK/js/boot.js"
  52. KERNEL="$BOOT $JTALK/js/Kernel.js"
  53. COMPILER="$KERNEL $JTALK/js/Parser.js $JTALK/js/Compiler.js"
  54. # Predefined initializer
  55. INITIALIZER="$JTALK/js/init.js"
  56. # Default values
  57. ENV=
  58. INIT=
  59. MAIN=
  60. MAINFILE=
  61. BASE=$KERNEL
  62. OURCOMPILER="$COMPILER $JTALK/js/init.js $JTALK/nodejs/nodecompile.js"
  63. # Read options and shift them away
  64. while getopts "NDKCiI:M:m:h?" o; do
  65. case "$o" in
  66. N) ENV=NODE
  67. BASE=$KERNEL
  68. INIT=$INITIALIZER
  69. MAIN=Main;;
  70. D) ENV=D8
  71. BASE=$KERNEL
  72. INIT=$INITIALIZER
  73. MAIN=Main;;
  74. K) BASE=$KERNEL;;
  75. C) BASE=$COMPILER;;
  76. I) INIT=$INITIALIZER;;
  77. i) INIT=$OPTARG;;
  78. M) MAINFILE=$OPTARG;;
  79. m) MAIN=$OPTARG;;
  80. h) usage;;
  81. [?]) usage;;
  82. esac
  83. done
  84. shift $(($OPTIND - 1))
  85. # Combine supplied libraries
  86. LIBS="$BASE $ADDONS"
  87. # Get a unique tempdir and make it get auto removed on exit
  88. TMPDIR=`mktemp -d`
  89. trap "rm -rf $TMPDIR" EXIT
  90. # --------------------------------------------------
  91. # Collect libraries and Smalltalk files looking
  92. # both locally and in $JTALK/js and $JTALK/st
  93. # --------------------------------------------------
  94. PROGRAM=
  95. until [ "$*" = "" ]
  96. do
  97. case $1 in
  98. *.st)
  99. CATEGORY=`basename $1 .st`
  100. if [ -f "$1" ]; then
  101. COMPILE="$COMPILE $1 $CATEGORY"
  102. COMPILED="$COMPILED $CATEGORY.js"
  103. else
  104. if [ -f $JTALK/st/$1 ]; then
  105. COMPILE="$COMPILE $JTALK/st/$1 $CATEGORY"
  106. COMPILED="$COMPILED $CATEGORY.js"
  107. else
  108. echo "JTalk file not found: $1"
  109. exit 1
  110. fi
  111. fi
  112. ;;
  113. *.js)
  114. if [ -f "$1" ]; then
  115. LIBS="$LIBS $1"
  116. else
  117. if [ -f $JTALK/js/$1 ]; then
  118. LIBS="$LIBS $JTALK/js/$1"
  119. else
  120. echo "Javascript file not found: $1"
  121. exit 1
  122. fi
  123. fi
  124. ;;
  125. *)
  126. # Will end up being the last non js/st argument
  127. PROGRAM=$1
  128. ;;
  129. esac
  130. shift
  131. done
  132. # --------------------------------------------------
  133. # Actual compilation phase of collected .st files
  134. # --------------------------------------------------
  135. # Create compiler dynamically
  136. cat $OURCOMPILER > $TMPDIR/compiler.js
  137. # Compile all collected .st files to .js
  138. echo "Compiling ..."
  139. node $TMPDIR/compiler.js $COMPILE
  140. # Verify all .js files corresponding to .st files were created, otherwise exit
  141. IFS=" "
  142. for FILE in $COMPILED
  143. do
  144. if [ ! -f "$FILE" ]; then
  145. echo "Failed compilation of $FILE, exiting."
  146. exit 1
  147. fi
  148. done
  149. if [ -z $PROGRAM ]; then
  150. echo "Done."
  151. exit 0
  152. fi
  153. # --------------------------------------------------
  154. # Now we start composing resulting javascript file.
  155. # --------------------------------------------------
  156. # Add collected libraries to libs.js file.
  157. if [ ! -z "$LIBS" ]; then
  158. echo "Adding libraries $LIBS ..."
  159. cat $LIBS > $TMPDIR/libs.js
  160. LIBS=$TMPDIR/libs.js
  161. fi
  162. echo "Adding Jtalk code$COMPILED ..."
  163. # Check for init file
  164. if [ ! -z "$INIT" ]; then
  165. if [ -f "$INIT" ]; then
  166. echo "Adding initializer $INIT ..."
  167. else
  168. echo "Can not find init file $INIT, exiting."
  169. exit 1
  170. fi
  171. fi
  172. # Check for adding main
  173. if [ ! -z "$MAIN" ]; then
  174. echo "Adding call to $MAIN class >> main ..."
  175. echo "smalltalk.$MAIN._main()" > $TMPDIR/main.js
  176. MAIN=$TMPDIR/main.js
  177. fi
  178. # Check for adding main file
  179. if [ ! -z "$MAINFILE" ]; then
  180. if [ -f "$MAINFILE" ]; then
  181. echo "Adding main as $MAINFILE ..."
  182. else
  183. echo "Can not find main file $MAINFILE, exiting."
  184. exit 1
  185. fi
  186. MAIN=$MAINFILE
  187. fi
  188. # And finally concatenate Program.js
  189. echo "Writing $PROGRAM.js ..."
  190. cat $LIBS $COMPILED $INIT $MAIN > $PROGRAM.js
  191. echo "Done."
  192. # Optionally run Program and give all args left to it
  193. if [ -n "$RUN" ]; then
  194. echo "Running program"
  195. echo "---------------"
  196. node $PROGRAM.js $@
  197. fi