Implement shebang handling

Add two scripts for running idris programs with the C and node backends and adds a test.

Fixes #2247
This commit is contained in:
Niklas Larsson 2017-07-25 19:58:03 +02:00
parent cc45cddc86
commit fbc01c4d4c
10 changed files with 82 additions and 1 deletions

18
scripts/runidris Normal file
View File

@ -0,0 +1,18 @@
#!/bin/sh
# This is a script that runs an idris program, suitable for use
# in a shebang line. We cache the executables by naming them after
# the SHA hash of the idris source and looking for it before
# compiling
RUNIDRIS_DIR="${TEMP:-/tmp}/runidris"
if [ ! -d $RUNIDRIS_DIR ]; then
mkdir $RUNIDRIS_DIR
chmod 700 $RUNIDRIS_DIR
fi
TEMP_NAME="runidris-`sha1sum $1 | cut -c1-40`"
FP="$RUNIDRIS_DIR/$TEMP_NAME"
if [ ! -e $FP ]; then
"${IDRIS:-idris}" $1 --ibcsubdir "$RUNIDRIS_DIR" -i "." -o $FP
fi
shift
"$FP" "$@"

18
scripts/runidris-node Normal file
View File

@ -0,0 +1,18 @@
#!/bin/sh
# This is a script that runs an idris program, suitable for use
# in a shebang line. We cache the executables by naming them after
# the SHA hash of the idris source and looking for it before
# compiling. This is the node version.
RUNIDRIS_DIR="${TEMP:-/tmp}/runidris-node"
if [ ! -d $RUNIDRIS_DIR ]; then
mkdir $RUNIDRIS_DIR
chmod 700 $RUNIDRIS_DIR
fi
TEMP_NAME="runidris-`sha1sum $1 | cut -c1-40`.js"
FP="$RUNIDRIS_DIR/$TEMP_NAME"
if [ ! -e $FP ]; then
"${IDRIS:-idris}" --codegen node --ibcsubdir "$RUNIDRIS_DIR" -i "." $1 -o $FP
fi
shift
node "$FP" "$@"

View File

@ -1569,7 +1569,8 @@ parseImports fname input
[ImportInfo],
Maybe Delta),
[(FC, OutputAnnotation)], IState)
imports = do whiteSpace
imports = do optional shebang
whiteSpace
(mdoc, mname, annots) <- moduleHeader
ps_exp <- many import_
mrk <- mark
@ -1594,6 +1595,9 @@ parseImports fname input
(fc, AnnNamespace ns (Just path)) : addPath annots path
addPath (annot:annots) path = annot : addPath annots path
shebang :: IdrisParser ()
shebang = string "#!" *> many (satisfy $ not . isEol) *> eol *> pure ()
-- | There should be a better way of doing this...
findFC :: Doc -> (FC, String)
findFC x = let s = show (plain x) in findFC' s

View File

@ -0,0 +1,5 @@
module TestMod
export
test : IO ()
test = putStrLn "Hello from a module"

View File

@ -0,0 +1,7 @@
hello from C
hello from C
hello from node
hello from node
Hello ["aaa"]
Hello ["bbb", "aaa"]
Hello from a module

9
test/interactive017/run Normal file
View File

@ -0,0 +1,9 @@
#!/bin/sh
PATH="../../scripts:$PATH"
./shebang.idr
./shebang.idr
./shebang-node.idr
./shebang-node.idr
./shebang-args.idr aaa
./shebang-args.idr bbb aaa
./shebang-import.idr

View File

@ -0,0 +1,6 @@
#!/usr/bin/env runidris
main : IO ()
main = do
args <- getArgs
putStrLn $ "Hello " ++ (show $ drop 1 args)

View File

@ -0,0 +1,6 @@
#!/usr/bin/env runidris
import TestMod
main : IO ()
main = TestMod.test

View File

@ -0,0 +1,4 @@
#!/usr/bin/env runidris-node
main : IO ()
main = putStrLn "hello from node"

View File

@ -0,0 +1,4 @@
#!/usr/bin/env runidris
main : IO ()
main = putStrLn "hello from C"