add shebang support ()

* add shebang support
This commit is contained in:
Ozan Hacıbekiroğlu 2020-05-08 20:09:44 +03:00 committed by GitHub
parent 3d7269c2c8
commit 50379940e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions
cmd/tengo
docs

View file

@ -67,6 +67,10 @@ func main() {
os.Exit(1)
}
} else if filepath.Ext(inputFile) == sourceFileExt {
if len(inputData) > 1 &&
bytes.Compare(inputData[:2], []byte("#!")) == 0 {
copy(inputData, "//")
}
err := CompileAndRun(modules, inputData, inputFile)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err.Error())

View file

@ -30,6 +30,28 @@ tengo -o myapp myapp.tengo # compile 'myapp.tengo' into binary file 'myapp'
tengo myapp # execute the compiled binary `myapp`
```
Or, you can make tengo source file executable
```bash
# copy tengo executable to a dir where PATH environment variable includes
cp tengo /usr/local/bin/
# add shebang line to source file
cat > myapp.tengo << EOF
#!/usr/local/bin/tengo
fmt := import("fmt")
fmt.println("Hello World!")
EOF
# make myapp.tengo file executable
chmod +x myapp.tengo
# run your script
./myapp.tengo
```
**Note: Your source file must have `.tengo` extension.**
## Tengo REPL
You can run Tengo [REPL](https://en.wikipedia.org/wiki/Readevalprint_loop)