Skip to content

Commit

Permalink
Add Factor BBHW with Linux run scripts and README
Browse files Browse the repository at this point in the history
  • Loading branch information
AndydeCleyre committed May 23, 2024
1 parent cdf3c0b commit 124daf1
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
2 changes: 2 additions & 0 deletions code/factor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bbhw/bbhw
factor
27 changes: 27 additions & 0 deletions code/factor/README.md
Original file line number Diff line number Diff line change
@@ -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/
34 changes: 34 additions & 0 deletions code/factor/bbhw/bbhw.factor
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions code/factor/run
Original file line number Diff line number Diff line change
@@ -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 [email protected]: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" "$@"
36 changes: 36 additions & 0 deletions code/factor/run-optimized
Original file line number Diff line number Diff line change
@@ -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 [email protected]: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 "$@"

0 comments on commit 124daf1

Please sign in to comment.