diff --git a/code/factor/.gitignore b/code/factor/.gitignore new file mode 100644 index 0000000..0d9689f --- /dev/null +++ b/code/factor/.gitignore @@ -0,0 +1,2 @@ +bbhw/bbhw +factor diff --git a/code/factor/README.md b/code/factor/README.md new file mode 100644 index 0000000..024c2ac --- /dev/null +++ b/code/factor/README.md @@ -0,0 +1,27 @@ +# Factor + +From [Factor's homepage]: + +> The Factor programming language is a concatenative, +> stack-based programming language with high-level features including dynamic types, +> extensible syntax, macros, and garbage collection. +> On a practical side, Factor has a full-featured library, +> supports many different platforms, and has been extensively documented. +> +> The implementation is fully compiled for performance, +> while still supporting interactive development. +> Factor applications are portable between all common platforms. +> Factor can deploy stand-alone applications on all platforms. +> Full source code for the Factor project is available under a BSD license. + +--- + +If Factor is already installed and its `factor` in your path, +`run` will use the fast, non-optimizing compiler to run `bbhw.factor`, +and `run-optimized` will use the slow, optimizing compiler to build `bbhw` +from `bbhw.factor` and run that. + +If Factor is not found, +either of those scripts will first download and build Factor itself. + +[Factor's homepage]: https://factorcode.org/ diff --git a/code/factor/bbhw/bbhw.factor b/code/factor/bbhw/bbhw.factor new file mode 100644 index 0000000..5dd592d --- /dev/null +++ b/code/factor/bbhw/bbhw.factor @@ -0,0 +1,34 @@ +USING: calendar combinators.short-circuit.smart command-line +interpolate io kernel math math.parser namespaces ranges +sequences threads ; +IN: bbhw + +: ask-n ( -- str ) + "countdown: " write flush + readln ; + +: initial-n ( -- str ) + command-line get + [ ask-n ] [ first ] if-empty ; + +: valid-n? ( str -- ? ) + string>number + { [ ] [ integer? ] [ 0 > ] } && ; + +: say-bye-bye ( n -- ) + "World, Hello..." write + 1 [a..b] [ + I"${}..." write flush + 1 seconds sleep + ] each + "Bye Bye." print ; + +: main ( -- ) + initial-n + [ dup valid-n? ] [ + I"Invalid countdown ${}, try again..." print + ask-n + ] until + string>number say-bye-bye ; + +MAIN: main diff --git a/code/factor/run b/code/factor/run new file mode 100755 index 0000000..4b1d2b8 --- /dev/null +++ b/code/factor/run @@ -0,0 +1,29 @@ +#!/bin/sh -e +PATH_HERE=$(pwd)/$(dirname "$0") + +FACTOR=$(which factor) +# This is usually an irrelevant prime factors tool that we ignore: +if [ -x "$FACTOR" ]; then + if $FACTOR --help 2>&1 | grep -q 'prime factors'; then + FACTOR="" + fi +fi + +# If not found, build Factor from source: +if [ "$FACTOR" = "" ]; then + PATH_REPO="$PATH_HERE/factor" + FACTOR="$PATH_REPO/factor" + + if [ ! -x "$FACTOR" ]; then + if [ ! -d "$PATH_REPO" ]; then + git clone -v --depth=1 git@github.com:factor/factor + fi + cd "$PATH_REPO" + git checkout master + git pull + ./build.sh net-bootstrap + fi +fi + +# Use the fast, non-optimizing compiler to run bbhw: +$FACTOR "$PATH_HERE/bbhw/bbhw.factor" "$@" diff --git a/code/factor/run-optimized b/code/factor/run-optimized new file mode 100755 index 0000000..a1adf9d --- /dev/null +++ b/code/factor/run-optimized @@ -0,0 +1,36 @@ +#!/bin/sh -e +PATH_HERE=$(pwd)/$(dirname "$0") + +FACTOR=$(which factor) +# This is usually an irrelevant prime factors tool that we ignore: +if [ -x "$FACTOR" ]; then + if $FACTOR --help 2>&1 | grep -q 'prime factors'; then + FACTOR="" + fi +fi + +# If not found, build Factor from source: +if [ "$FACTOR" = "" ]; then + PATH_REPO="$PATH_HERE/factor" + FACTOR="$PATH_REPO/factor" + + if [ ! -x "$FACTOR" ]; then + if [ ! -d "$PATH_REPO" ]; then + git clone -v --depth=1 git@github.com:factor/factor + fi + cd "$PATH_REPO" + git checkout master + git pull + ./build.sh net-bootstrap + fi +fi + +# Use the slow, optimizing compiler to build bbhw: +BIN="$PATH_HERE"/bbhw/bbhw +if [ ! -x "$BIN" ]; then + $FACTOR -roots="$PATH_HERE" -e='"bbhw" deploy' + mv "$($FACTOR -e='deploy-directory get absolute-path print')/bbhw/bbhw" "$BIN" +fi + +# Run it: +$BIN "$@"