on master help sign in
Implement shebang handling

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

commitfbc01c4d4c3d0fe1fabaf025351117427bde1e49 authorNiklas Larsson <niklas@mm.st> dateTue 25 Jul 2017, 19:58 (9 years ago) parentcc45cdd
10 files changed+82−1
scripts/runidris+18 scripts/runidris-node+18 src/Idris/Parser.hs+5−1 test/interactive017/TestMod.idr+5 test/interactive017/expected+7 test/interactive017/run+9 test/interactive017/shebang-args.idr+6 test/interactive017/shebang-import.idr+6 test/interactive017/shebang-node.idr+4 test/interactive017/shebang.idr+4
scripts/runidrisnew file+18
@@ -0,0 +1,18 @@
1+#!/bin/sh
2+
3+# This is a script that runs an idris program, suitable for use
4+# in a shebang line. We cache the executables by naming them after
5+# the SHA hash of the idris source and looking for it before
6+# compiling
7+RUNIDRIS_DIR="${TEMP:-/tmp}/runidris"
8+if [ ! -d $RUNIDRIS_DIR ]; then
9+ mkdir $RUNIDRIS_DIR
10+ chmod 700 $RUNIDRIS_DIR
11+fi
12+TEMP_NAME="runidris-`sha1sum $1 | cut -c1-40`"
13+FP="$RUNIDRIS_DIR/$TEMP_NAME"
14+if [ ! -e $FP ]; then
15+ "${IDRIS:-idris}" $1 --ibcsubdir "$RUNIDRIS_DIR" -i "." -o $FP
16+fi
17+shift
18+"$FP" "$@"
scripts/runidris-nodenew file+18
@@ -0,0 +1,18 @@
1+#!/bin/sh
2+
3+# This is a script that runs an idris program, suitable for use
4+# in a shebang line. We cache the executables by naming them after
5+# the SHA hash of the idris source and looking for it before
6+# compiling. This is the node version.
7+RUNIDRIS_DIR="${TEMP:-/tmp}/runidris-node"
8+if [ ! -d $RUNIDRIS_DIR ]; then
9+ mkdir $RUNIDRIS_DIR
10+ chmod 700 $RUNIDRIS_DIR
11+fi
12+TEMP_NAME="runidris-`sha1sum $1 | cut -c1-40`.js"
13+FP="$RUNIDRIS_DIR/$TEMP_NAME"
14+if [ ! -e $FP ]; then
15+ "${IDRIS:-idris}" --codegen node --ibcsubdir "$RUNIDRIS_DIR" -i "." $1 -o $FP
16+fi
17+shift
18+node "$FP" "$@"
src/Idris/Parser.hs+5−1
@@ -1569,7 +1569,8 @@ parseImports fname input
15691569 [ImportInfo],
15701570 Maybe Delta),
15711571 [(FC, OutputAnnotation)], IState)
1572− imports = do whiteSpace
1572+ imports = do optional shebang
1573+ whiteSpace
15731574 (mdoc, mname, annots) <- moduleHeader
15741575 ps_exp <- many import_
15751576 mrk <- mark
@@ -1594,6 +1595,9 @@ parseImports fname input
15941595 (fc, AnnNamespace ns (Just path)) : addPath annots path
15951596 addPath (annot:annots) path = annot : addPath annots path
15961597
1598+ shebang :: IdrisParser ()
1599+ shebang = string "#!" *> many (satisfy $ not . isEol) *> eol *> pure ()
1600+
15971601-- | There should be a better way of doing this...
15981602findFC :: Doc -> (FC, String)
15991603findFC x = let s = show (plain x) in findFC' s
test/interactive017/TestMod.idrnew file+5
@@ -0,0 +1,5 @@
1+module TestMod
2+
3+export
4+test : IO ()
5+test = putStrLn "Hello from a module"
test/interactive017/expectednew file+7
@@ -0,0 +1,7 @@
1+hello from C
2+hello from C
3+hello from node
4+hello from node
5+Hello ["aaa"]
6+Hello ["bbb", "aaa"]
7+Hello from a module
test/interactive017/runnew file+9
@@ -0,0 +1,9 @@
1+#!/bin/sh
2+PATH="../../scripts:$PATH"
3+./shebang.idr
4+./shebang.idr
5+./shebang-node.idr
6+./shebang-node.idr
7+./shebang-args.idr aaa
8+./shebang-args.idr bbb aaa
9+./shebang-import.idr
test/interactive017/shebang-args.idrnew file+6
@@ -0,0 +1,6 @@
1+#!/usr/bin/env runidris
2+
3+main : IO ()
4+main = do
5+ args <- getArgs
6+ putStrLn $ "Hello " ++ (show $ drop 1 args)
test/interactive017/shebang-import.idrnew file+6
@@ -0,0 +1,6 @@
1+#!/usr/bin/env runidris
2+
3+import TestMod
4+
5+main : IO ()
6+main = TestMod.test
test/interactive017/shebang-node.idrnew file+4
@@ -0,0 +1,4 @@
1+#!/usr/bin/env runidris-node
2+
3+main : IO ()
4+main = putStrLn "hello from node"
test/interactive017/shebang.idrnew file+4
@@ -0,0 +1,4 @@
1+#!/usr/bin/env runidris
2+
3+main : IO ()
4+main = putStrLn "hello from C"