ntalkc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/bash
  2. #
  3. # This is a trivial "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. USAGE="Usage: $0 [-r] file1 file2 ... file3 Program
  7. Will compile a JTalk program into a single javascript file called
  8. Program.js (last argument) by concatenating listed files using these rules:
  9. *.js files are concatenated as is. If not found we look in $JTALK/js
  10. *.st files are compiled into .js files. If not found we look in $JTALK/st.
  11. Each file is considered to be a fileout of a single class category of
  12. the same name as the file.
  13. NOTE: boot.js and Kernel.js is always added first and init.js
  14. is always added last. Finally main.js is added if found.
  15. If -r is used we also run node with the resulting Program.js.
  16. "
  17. if [ "$#" == "0" ]; then
  18. echo "$USAGE"
  19. exit 1
  20. fi
  21. if [ $1 == "-r" ]; then
  22. RUN=true
  23. shift
  24. fi
  25. # Get a unique tempdir and make sure it gets nuked later
  26. TMPDIR=`mktemp -d`
  27. trap "rm -rf $TMPDIR" EXIT
  28. # Collect libraries and Smalltalk files
  29. until [ "$*" = "" ]
  30. do
  31. case $1 in
  32. *.st)
  33. CATEGORY=`basename $1 .st`
  34. if [ -f "$1" ]; then
  35. COMPILE="$COMPILE $1 $CATEGORY"
  36. COMPILED="$COMPILED $CATEGORY.js"
  37. else
  38. if [ -f $JTALK/st/$1 ]; then
  39. COMPILE="$COMPILE $JTALK/st/$1 $CATEGORY"
  40. COMPILED="$COMPILED $CATEGORY.js"
  41. else
  42. echo "JTalk file not found: $1"
  43. fi
  44. fi
  45. shift
  46. ;;
  47. *.js)
  48. if [ -f "$1" ]; then
  49. LIBS="$LIBS $1"
  50. else
  51. if [ -f $JTALK/js/$1 ]; then
  52. LIBS="$LIBS $JTALK/js/$1"
  53. else
  54. echo "Javascript file not found: $1"
  55. fi
  56. fi
  57. ;;
  58. esac
  59. # Will end up being the last argument
  60. PROGRAM=$1
  61. shift
  62. done
  63. # Create compiler dynamically
  64. cat $JTALK/js/boot.js $JTALK/js/Kernel.js $JTALK/js/Parser.js $JTALK/js/Compiler.js $JTALK/js/init.js $JTALK/nodejs/nodecompile.js > $TMPDIR/compiler-all.js
  65. # Compile all collected .st files to .js
  66. node $TMPDIR/compiler-all.js $COMPILE
  67. # Verify all .js files corresponding .st files were created, otherwise exit
  68. IFS=" "
  69. for FILE in $COMPILED
  70. do
  71. if [ ! -f "$FILE" ]; then
  72. echo "Failed compilation of $FILE, exiting."
  73. exit 1
  74. fi
  75. done
  76. # Compose the complete libs.js file from collected .js filenames.
  77. if [ -n "$LIBS" ]; then
  78. echo "LIBS $LIBS"
  79. cat $LIBS > $TMPDIR/libs.js
  80. LIBS=$TMPDIR/libs.js
  81. fi
  82. # Check for main.js
  83. if [ -f "main.js" ]; then
  84. MAIN="main.js"
  85. fi
  86. # And finally concatenate Program.js
  87. cat $JTALK/js/boot.js $JTALK/js/Kernel.js $LIBS $COMPILED $JTALK/js/init.js $MAIN > $PROGRAM.js
  88. # Optionally run Program and give all args left to it
  89. if [ -n "$RUN" ]; then
  90. echo "Running program"
  91. echo "---------------"
  92. node $PROGRAM.js $@
  93. fi