commit 798876f385e18b1b0a750092319bd98ac0819179
parent 1dc1462c2eec670c798bcf760b9b84e81e2421b7
Author: Ed van Bruggen <edvb@uw.edu>
Date: Mon, 19 Mar 2018 23:29:46 -0700
Evaluate file passed as argument
Also update README and man page
Diffstat:
README.md | | | 33 | ++++++++++++++++++++++++++++----- |
tisp.1 | | | 45 | ++++++++++++++++++++++++++++++++++++++++----- |
tisp.c | | | 19 | ++++++++++++++----- |
3 files changed, 82 insertions(+), 15 deletions(-)
diff --git a/README.md b/README.md
@@ -15,19 +15,42 @@ Print version info and exit
## USAGE
-Run the program from the command line to launch the REPL, type command and
-press enter to see result. Alternatively you can pass a single command as the
-first argument to the command line program and see a result instantly.
+Run the program from the command line to launch the REPL, type a command and
+press enter to see the result.
+
+```
+$ tisp
+> (+ (+ 1 2) 3)
+6
+> (+ 1 2 3)
+6
+```
+
+Alternatively you can pass a file name which will be opened and run, outputting
+the result before exiting.
+
+```
+$ echo '(+ 1 2 3)' > add.lisp
+$ tisp add.lisp
+6
+```
+
+Commands can also be piped directing into tisp through the command line.
+
+```
+$ echo '(+ 1 2 3)' | tisp
+6
+```
## AUTHOR
-Ed van Bruggen <ed@edryd.org>
+Ed van Bruggen <edvb@uw.edu>
## SEE ALSO
See project page at <https://edryd.org/projects/tisp.html>
-View source code at <https://github.com/edvb/tisp>
+View source code at <https://git.edryd.org/tisp>
## LICENSE
diff --git a/tisp.1 b/tisp.1
@@ -1,12 +1,12 @@
.\" x-roff document
.do mso man.tmac
-.TH tisp 1 "February 2018" "tisp 0.0.0"
+.TH tisp 1 "March 2018" "tisp 0.0.0"
.PP
.SH NAME
tisp - tiny lisp
.PP
.SH SYNOPSIS
-\fBtisp\fP [\fB-hv\fP] [\fICODE\fP]
+\fBtisp\fP [\fB-hv\fP] [\fIFILENAME\fP]
.PP
.SH DESCRIPTION
Tiny lisp implementation, still in heavy development, barely even "useable." Designed to be scheme-like, ie very minimalist.
@@ -21,15 +21,50 @@ Tiny lisp implementation, still in heavy development, barely even "useable." Des
\fRPrint version info and exit
.PP
.SH USAGE
-Run the program from the command line to launch the REPL, type command and press enter to see result. Alternatively you can pass a single command as the first argument to the command line program and see a result instantly.
+Run the program from the command line to launch the REPL, type a command and press enter to see the result.
+.PP
+.RS 4
+.EX
+
+$ tisp
+> (+ (+ 1 2) 3)
+6
+> (+ 1 2 3)
+6
+
+.EE
+.RE
+.PP
+Alternatively you can pass a file name which will be opened and run, outputting the result before exiting.
+.PP
+.RS 4
+.EX
+
+$ echo '(+ 1 2 3)' > add.lisp
+$ tisp add.lisp
+6
+
+.EE
+.RE
+.PP
+Commands can also be piped directing into tisp through the command line.
+.PP
+.RS 4
+.EX
+
+$ echo '(+ 1 2 3)' | tisp
+6
+
+.EE
+.RE
.PP
.SH AUTHOR
-Ed van Bruggen <ed@edryd.org>
+Ed van Bruggen <edvb@uw.edu>
.PP
.SH SEE ALSO
See project page at <https://edryd.org/projects/tisp.html>
.PP
-View source code at <https://github.com/edvb/tisp>
+View source code at <https://git.edryd.org/tisp>
.PP
.SH LICENSE
zlib License
diff --git a/tisp.c b/tisp.c
@@ -14,6 +14,8 @@
#include "util.h"
/* defines */
+#define BUF_SIZE 2048
+
#define car(P) ((P)->v.p.car)
#define cdr(P) ((P)->v.p.cdr)
#define nilp(P) ((P)->t == NIL)
@@ -114,7 +116,8 @@ issymbol(char c)
/* TODO skip comments */
static void
-skip_spaces(Str str) {
+skip_spaces(Str str)
+{
for (; *str->d && isspace(*str->d); str->d++);
}
@@ -513,7 +516,7 @@ val_free(Val v)
static void
usage(const int eval)
{
- die(eval, "usage: %s [-hv] [CODE]", argv0);
+ die(eval, "usage: %s [-hv] [FILENAME]", argv0);
}
int
@@ -529,14 +532,20 @@ main(int argc, char *argv[])
usage(1);
} ARGEND;
- char *str = NULL;
+ size_t nread;
+ char *str = NULL, buf[BUF_SIZE];
+ FILE *fp;
Val v;
Hash env = init_env();
nil.t = NIL;
- if (argc > 0 && argv[0][0] == '(')
- str = argv[0];
+ if (argc > 0) {
+ if (!(fp = fopen(*argv, "r")))
+ die(1, "%s: %s:", argv[0], *argv);
+ while ((nread = fread(buf, 1, sizeof(buf), fp)) > 0) ;
+ str = buf;
+ }
while ((v = tisp_read(str))) {
v = tisp_eval(env, v);