jtalkc 5.5 KB

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