Browse Source

Improved jtalk compiler, now named jtalkc in nodejs directory. Not specific to Node anymore.

Göran Krampe 13 years ago
parent
commit
2a20d14e2f

+ 3 - 25
nodejs/README

@@ -2,35 +2,13 @@ JTalk + Node.js = Awesomeness
 =============================
 These are some experiments of using JTalk together with Node.js:
 
-ntalkc          - a bash script for compiling JTalk programs for Node.js.
-nodecompile.js  - trivial Node.js javascript code for compiling .st files, used by ntalkc.
+jtalkc          - a bash script for compiling JTalk programs.
+nodecompile.js  - Node.js javascript code for compiling .st files, used by jtalkc.
 
-hello           - Hello world example with a simple Makefile
+hello           - Hello world example with a simple Makefile.
 trivialserver   - A slightly larger example.
 trivialserver2  - A variation on trivialserver that tries to do more in JTalk.
 
 You will need "node" in your path to try examples.
 
 
-ntalkc
-======
-
-The "compiler" is simply a bash script that uses Nicolas' Exporter/Importer to compile .st files into .js files. The source code should be easy to follow but basically it does the following:
-
-1. Collect all listed (as arguments) .st and .js files. We also check in the JTalk js and st directories if not found.
-
-2. Create a "compiler" by concatenating these files (this means we always use freshest from git):
-        boot.js Kernel.js Parser.js Compiler.js init.js nodejs/nodecompile.js 
-
-3. Run this "compiler" using node and give .st files as command line arguments. This produces corresponding .js files. If not all .js files exist afterwards we abort.
-
-4. Build final program.js by concatenating:
-
-        boot.js
-        Kernel.js
-        ...all .js libraries collected in step 1.
-        ...all .js files produced from the .st files in step 3.
-        init.js (either locally or from JTalk js directory)
-        main.js (if found locally)
-
-5. Run program.js using node if "-r" was specified.

+ 5 - 0
nodejs/hello/Hello.st

@@ -2,6 +2,11 @@ Object subclass: #Hello
         instanceVariableNames: ''
         category: 'Hello'!
 
+!Hello class methodsFor: 'main'!
+
+main
+	{'console.log(smalltalk.Hello._new()._hello());'}
+! ! 
 
 !Hello methodsFor: 'printing'!
 

+ 1 - 1
nodejs/hello/Makefile

@@ -1,5 +1,5 @@
 Program.js: Hello.st
-	../ntalkc Hello.st Program
+	../jtalkc -N -m Hello Hello.st Program
 
 run: Program.js
 	./hello

+ 0 - 2
nodejs/hello/main.js

@@ -1,2 +0,0 @@
-sys = require('sys');
-sys.puts(smalltalk.Hello._new()._hello());

+ 225 - 0
nodejs/jtalkc

@@ -0,0 +1,225 @@
+#!/bin/bash
+#
+# This is a "compiler" for JTalk code. Run without arguments for help.
+
+# Get JTalk root directory from the location of this script.
+JTALK=`dirname ${0}`/..
+
+function usage {
+	cat <<ENDOFHELP
+Usage: $0 [-N|D] [-K|C] [-m class] [-M file] [-i file] [-I] [-r] file1 file2 ... file3 Program
+
+   Will compile Jtalk programs in a variety of ways.
+   Files listed will be handled using these rules:
+
+     *.js files are concatenated as is. If not found we look in $JTALK/js
+
+     *.st files are compiled into .js files. If not found we look in $JTALK/st.
+     Each file is currently considered to be a fileout of a single class
+     category of the same name as the file.
+
+  -N or -D
+     Compile for Node.js or D8 (V8 shell). Defaults to "-KIm Main" so boot.js and Kernel.js
+     are added first and init.js is added last with a call to Main class>>main.
+
+  -K
+     Add libraries to get minimal JTalk Kernel running. Implies -i.
+
+  -C
+     Add libraries to get minimal JTalk Compiler running. Implies -i.
+
+  -I
+     Add library standard initializer $JTALK/js/init.js
+
+  -i file
+     Add library initializer <file>.
+
+  -M file
+     Add javascript file <file> last acting as main.
+
+  -m class
+     Add call to #main in class <class>. 
+
+
+     Example invocations:
+
+     Compile Kernel.st to Kernel.js:
+
+        jtalkc Kernel.st
+
+     Compile Hello.st to Hello.js and create complete program called
+     Hello-all.js for Node.js including boot.js, Kernel.js, init.js and
+     adding a call to class method #main in class Hello:
+
+        jtalkc -N Hello.st
+
+     Compile two different class categories into corresponding .js files,
+     and manually link with specific boot.js, Kernel.js, init.js and main.js
+     and create complete program called Program.js:
+
+        jtalkc boot.js Kernel.js CategoryOne.st CategoryTwo.st init.js main.js Program
+ENDOFHELP
+	exit 1;
+}
+
+
+# Define our predefined library combinations
+BOOT="$JTALK/js/boot.js"
+KERNEL="$BOOT $JTALK/js/Kernel.js"
+COMPILER="$KERNEL $JTALK/js/Parser.js $JTALK/js/Compiler.js"
+
+# Predefined initializer
+INITIALIZER="$JTALK/js/init.js"
+
+# Default values
+ENV=
+INIT=
+MAIN=
+MAINFILE=
+BASE=$KERNEL
+OURCOMPILER="$COMPILER $JTALK/js/init.js $JTALK/nodejs/nodecompile.js"
+
+# Read options and shift them away
+while getopts "NDKCIi:M:m:r" o; do
+case "$o" in
+   N) ENV=NODE
+      BASE=$KERNEL
+      INIT=$INITIALIZER
+      ADDMAIN=Main;;
+   D) ENV=D8
+      BASE=$KERNEL
+      INIT=$INITIALIZER
+      ADDMAIN=Main;;
+   K) BASE=$KERNEL;;
+   C) BASE=$COMPILER;;
+   I) INIT=$INITIALIZER;;
+   i) INIT=$OPTARG;;
+   M) MAINFILE=$OPTARG;;
+   m) MAIN=$OPTARG;;
+   h) usage;;
+   [?])  usage;;
+   esac
+done
+shift $(($OPTIND - 1))
+
+# Combine supplied libraries
+LIBS="$BASE $ADDONS"
+
+# Get a unique tempdir and make it get auto removed on exit
+TMPDIR=`mktemp -d`
+trap "rm -rf $TMPDIR" EXIT
+
+# --------------------------------------------------
+# Collect libraries and Smalltalk files looking
+# both locally and in $JTALK/js and $JTALK/st 
+# --------------------------------------------------
+until [ "$*" = "" ]
+do
+  case $1 in
+     *.st)
+        CATEGORY=`basename $1 .st`
+        if [ -f "$1" ]; then
+           COMPILE="$COMPILE $1 $CATEGORY"
+           COMPILED="$COMPILED $CATEGORY.js"
+        else
+           if [ -f $JTALK/st/$1 ]; then
+             COMPILE="$COMPILE $JTALK/st/$1 $CATEGORY"
+             COMPILED="$COMPILED $CATEGORY.js"
+           else
+             echo "JTalk file not found: $1"
+             exit 1
+           fi
+        fi
+        ;;
+
+     *.js)
+        if [ -f "$1" ]; then
+           LIBS="$LIBS $1" 
+        else
+           if [ -f $JTALK/js/$1 ]; then
+             LIBS="$LIBS $JTALK/js/$1"
+           else
+             echo "Javascript file not found: $1"
+             exit 1
+           fi
+        fi
+        ;;
+  esac
+  # Will end up being the last argument
+  PROGRAM=$1
+  shift
+done
+
+# --------------------------------------------------
+# Actual compilation phase of collected .st files
+# --------------------------------------------------
+
+# Create compiler dynamically
+cat $OURCOMPILER > $TMPDIR/compiler.js
+
+# Compile all collected .st files to .js
+echo "Compiling ..."
+node $TMPDIR/compiler.js $COMPILE
+
+# Verify all .js files corresponding to .st files were created, otherwise exit
+IFS=" "
+for FILE in $COMPILED
+do
+  if [ ! -f "$FILE" ]; then
+    echo "Failed compilation of $FILE, exiting."
+    exit 1
+  fi 
+done
+
+# --------------------------------------------------
+# Now we start composing resulting javascript file.
+# --------------------------------------------------
+
+# Add collected libraries to libs.js file.
+if [ ! -z "$LIBS" ]; then
+  echo "Adding libraries $LIBS ..."
+  cat $LIBS > $TMPDIR/libs.js
+  LIBS=$TMPDIR/libs.js
+fi
+
+echo "Adding Jtalk code$COMPILED ..."
+
+# Check for init file
+if [ ! -z "$INIT" ]; then
+   if [ -f "$INIT" ]; then
+      echo "Adding initializer $INIT ..."
+   else
+      echo "Can not find init file $INIT, exiting."
+      exit 1
+   fi 
+fi
+
+# Check for adding main
+if [ ! -z "$MAIN" ]; then
+  echo "Adding call to $MAIN class >> main ..."
+  echo "smalltalk.$MAIN._main()" > $TMPDIR/main.js
+  MAIN=$TMPDIR/main.js
+fi
+
+# Check for adding main file
+if [ ! -z "$MAINFILE" ]; then
+   if [ -f "$MAINFILE" ]; then
+      echo "Adding main as $MAINFILE ..."
+   else
+      echo "Can not find main file $MAINFILE, exiting."
+      exit 1
+   fi 
+   MAIN=$MAINFILE
+fi
+
+# And finally concatenate Program.js
+echo "Writing $PROGRAM.js ..."
+cat $LIBS $COMPILED $INIT $MAIN > $PROGRAM.js
+echo "Done."
+
+# Optionally run Program and give all args left to it
+if [ -n "$RUN" ]; then
+  echo "Running program"
+  echo "---------------"
+  node $PROGRAM.js $@
+fi

+ 0 - 142
nodejs/ntalkc

@@ -1,142 +0,0 @@
-#!/bin/bash
-#
-# This is a trivial "compiler" for JTalk code. Run without arguments for help.
-
-# Get JTalk root directory from the location of this script.
-JTALK=`dirname ${0}`/..
-
-USAGE="Usage: $0 [-r] file1 file2 ... file3 Program
-
-   Will compile a JTalk program into a single javascript file called
-   <Program>.js by concatenating listed files using these rules:
-
-     *.js files are concatenated as is. If not found we look in $JTALK/js
-
-     *.st files are compiled into .js files. If not found we look in $JTALK/st.
-     Each file is currently considered to be a fileout of a single class
-     category of the same name as the file.
-
-     NOTE: boot.js and Kernel.js is always added first and init.js
-     is always added last. Finally main.js is added if found.
-
-     If -r is used we also run node with the resulting Program.js.
-
-     Example invocations:
-
-     Compile Hello.st to Hello.js and create
-     complete program called Program.js:
-
-        ntalkc Hello.st Program
-
-     Compile two different class categories into corresponding .js files,
-     link with two existing javascript libraries and create complete
-     program called Program.js:
-
-        ntalkc lib1.js lib2.js CategoryOne.st CategoryTwo.st Program
-"
-
-if [ "$#" == "0" ]; then
-	echo "$USAGE"
-	exit 1
-fi
-
-if [ $1 == "-r" ]; then
-  RUN=true
-  shift
-fi
-
-# Get a unique tempdir and make sure it gets nuked later
-TMPDIR=`mktemp -d`
-trap "rm -rf $TMPDIR" EXIT
-
-# Collect libraries and Smalltalk files
-until [ "$*" = "" ]
-do
-  case $1 in
-     *.st)
-        CATEGORY=`basename $1 .st`
-        if [ -f "$1" ]; then
-           COMPILE="$COMPILE $1 $CATEGORY"
-           COMPILED="$COMPILED $CATEGORY.js"
-        else
-           if [ -f $JTALK/st/$1 ]; then
-             COMPILE="$COMPILE $JTALK/st/$1 $CATEGORY"
-             COMPILED="$COMPILED $CATEGORY.js"
-           else
-             echo "JTalk file not found: $1"
-           fi
-        fi
-        shift
-        ;;
-
-     *.js)
-        if [ -f "$1" ]; then
-           LIBS="$LIBS $1" 
-        else
-           if [ -f $JTALK/js/$1 ]; then
-             LIBS="$LIBS $JTALK/js/$1"
-           else
-             echo "Javascript file not found: $1"
-           fi
-        fi
-        ;;
-  esac
-  # Will end up being the last argument
-  PROGRAM=$1
-  shift
-done
-
-# Create compiler dynamically
-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
-
-# Compile all collected .st files to .js
-echo "Compiling$COMPILED ..."
-node $TMPDIR/compiler-all.js $COMPILE
-
-# Verify all .js files corresponding .st files were created, otherwise exit
-IFS=" "
-for FILE in $COMPILED
-do
-  if [ ! -f "$FILE" ]; then
-    echo "Failed compilation of $FILE, exiting."
-    exit 1
-  fi 
-done
-
-# We must have these first...
-echo "Adding boot.js and Kernel.js ..."
-BOOT="$JTALK/js/boot.js $JTALK/js/Kernel.js"
-
-# Compose the complete libs.js file from collected .js filenames.
-if [ -n "$LIBS" ]; then
-  echo "Adding libraries $LIBS ..."
-  cat $LIBS > $TMPDIR/libs.js
-  LIBS=$TMPDIR/libs.js
-fi
-
-# Check for init.js
-if [ -f "init.js" ]; then
-  echo "Adding init.js ..."
-  INIT="init.js"
-else
-  echo "Adding $JTALK/js/init.js ..."
-  INIT=$JTALK/js/init.js
-fi
-
-# Check for main.js
-if [ -f "main.js" ]; then
-  echo "Adding main.js ..."
-  MAIN="main.js"
-fi
-
-# And finally concatenate Program.js
-echo "Writing $PROGRAM.js ..."
-cat $BOOT $LIBS $COMPILED $INIT $MAIN > $PROGRAM.js
-echo "Done."
-
-# Optionally run Program and give all args left to it
-if [ -n "$RUN" ]; then
-  echo "Running program"
-  echo "---------------"
-  node $PROGRAM.js $@
-fi

+ 1 - 1
nodejs/trivialserver/Makefile

@@ -1,5 +1,5 @@
 Program.js: TrivialServer.st main.js
-	../ntalkc TrivialServer.st Program
+	../jtalkc -N -M main.js TrivialServer.st Program
 
 run: Program.js
 	./trivial

+ 2 - 2
nodejs/trivialserver2/Makefile

@@ -1,5 +1,5 @@
-Program.js: TrivialServer.st main.js
-	../ntalkc TrivialServer.st Program
+Program.js: TrivialServer.st
+	../jtalkc -N -m TrivialServer TrivialServer.st Program
 
 run: Program.js
 	./trivial

+ 4 - 1
nodejs/trivialserver2/TrivialServer.st

@@ -7,7 +7,6 @@ initialize
         counter := 0
 ! !
 
-
 !TrivialServer methodsFor: 'processing'!
 process: aRequest
         | hostname httpVersion stream |
@@ -43,4 +42,8 @@ initialize
         "We require these Node modules."
 
 	{'os = require(''os''), http = require(''http'');'}
+!
+
+main
+	self new start
 ! !

+ 0 - 1
nodejs/trivialserver2/main.js

@@ -1 +0,0 @@
-smalltalk.TrivialServer._new()._start()