--- unlambda.scm	Wed Nov  3 22:00:52 1999
+++ unlambda-bigloo.scm	Wed Nov  3 21:52:15 1999
@@ -1,6 +1,6 @@
 ;;; This is an implementation of the Unlambda programming language.
 ;;; Version 1.92.1 of 1999/10/30
-;;; $Id: unlambda.scm,v 1.10 1999/11/03 21:01:03 madore Exp $
+;;; Modified to work with bigloo (tested with bigloo-2.1a)
 
 ;;; Copyright (C) 1999 by David A. Madore <david.madore@ens.fr>
 
@@ -30,13 +30,9 @@
 ;; I would be very grateful if somebody could explain to me how all
 ;; this works.  -- The author
 
-;; Bail out with an error.
-(define (error str)
-  (display str)
-  (newline)
-; (quit) ; There is no standard quit operation in Scheme!!!
-  (disappear-in-the-pit-of-hell) ; Force an error
-  )
+;; Bigloo module declaration
+(module unlambda
+  (main main))
 
 ;; Parse the input file and return the representation of it.
 (define (parse input-port)
@@ -47,7 +43,7 @@
 	(gobble-comment)))
   (case (let ((ch (read-char input-port)))
 	  (if (eof-object? ch)
-	      (error "Unexpected end of file")
+	      (error "PARSE" "Unexpected end of file" input-port)
 	      ch))
     ((#\`) (let* ((op (parse input-port))
 		  (arg (parse input-port)))
@@ -67,15 +63,15 @@
     ((#\r #\R) '(pr #\newline)) ; print newline
     ((#\.) `(pr ,(let ((ch (read-char input-port)))
 		   (if (eof-object? ch)
-		       (error "Unexpected end of file")
+		       (error "PARSE" "Unexpected end of file" input-port)
 		       ch)))) ; print given char
     ((#\@) '(rd)) ; read next input char
     ((#\?) `(rc ,(let ((ch (read-char input-port)))
 		   (if (eof-object? ch)
-		       (error "Unexpected end of file")
+		       (error "PARSE" "Unexpected end of file" input-port)
 		       ch)))) ; compare character under reading head
     ((#\|) '(pc)) ; call arg with dot function for current char
-    (else (error "Character not understood"))))
+    (else (error "PARSE" "Character not understood" input-port))))
 
 ;; Unparse (display) an object.
 (define (unparse exp)
@@ -101,7 +97,9 @@
 	   ((rd) (write-char #\@))
 	   ((rc) (begin (write-char #\?) (write-char (cadr exp))))
 	   ((pc) (write-char #\|))
-	   (else (error "Internal error: unexpected type to unparse!"))))))
+	   (else
+	    (error "UNPARSE" "Internal error: unexpected type to unparse!"
+		   exp))))))
 
 ;; The eval function
 (define (ev exp)
@@ -115,7 +113,7 @@
 
 ;; The exit continuation (makes the e function work).
 (define (exit-cnt v)
-  (error "Please start with entry-ev and not ev"))
+  (error "EXIT-CNT" "Please start with entry-ev and not ev" v))
 ;; The first eval function (begins by capturing a continuation so that
 ;; the exit function works).
 (define (entry-ev exp)
@@ -157,8 +155,51 @@
 				 (not (eof-object? current-char)))
 			    `(pr ,current-char)
 			    '(v)))))
-    (else (error "Internal error: unexpected type to apply!"))))
+    (else (error "AP" "Internal error: unexpected type to apply!" exp))))
 
-;; ``Main'' function
-(define (main . junk)
-  (entry-ev (parse (current-input-port))))
+ 
+;; Main program function
+(define (main argv)
+  (let parse-args
+      ((args (cdr argv))
+       (arg-fileport #f)
+       (opt-unparse #f)
+       (options-done #f))
+    (if (null? args)
+	((if opt-unparse
+	     (lambda (exp) (unparse exp) (newline))
+	     (lambda (exp) #t))
+	 (entry-ev (parse (if arg-fileport
+			      arg-fileport
+			      (current-input-port)))))
+	(if (and (not options-done)
+		 (eqv? (string-ref (car args) 0) #\-))
+	    (cond ((equal? (car args) "-")
+		   (parse-args (cdr args) (current-input-port) opt-unparse #t))
+		  ((equal? (car args) "--")
+		   (parse-args (cdr args) arg-fileport opt-unparse #t))
+		  ((equal? (car args) "-e")
+		   (if (null? (cdr args))
+		       (error "MAIN" "Argument to -e missing" (cdr args))
+		       (parse-args (cddr args) (open-input-string (cadr args))
+				   opt-unparse options-done)))
+		  ((equal? (car args) "-u")
+		   (parse-args (cdr args) arg-fileport #t options-done))
+		  ((equal? (car args) "-h")
+		   (display "Syntax is unlambda [opts] [filename]") (newline)
+		   (display "Options include:") (newline)
+		   (display "  -h  this help") (newline)
+		   (display "  -v  display version number") (newline)
+		   (display "  -e  evaluate following unlambda expr") (newline)
+		   (display "  -u  print resulting expression after eval") (newline)
+		   (display "  --  terminate option line") (newline)
+		   (display "A filename of - or none at all means read stdin")(newline))
+		  ((equal? (car args) "-v")
+		   (display "Unlambda Scheme Interpreter") (newline)
+		   (display "Bigloo-compiled version 1.92.1") (newline))
+		  (else
+		   (error "MAIN" "Option not recognized (try -h)" (car args))))
+	    (if arg-fileport
+		(error "MAIN" "Multiple filenames specified" (car args))
+		(parse-args (cdr args) (open-input-file (car args))
+			    opt-unparse #t))))))
