t.c options processing in (skint-main)

This commit is contained in:
ESL 2024-07-18 21:34:54 -04:00
parent 020c0613b5
commit 4d40a0e303
4 changed files with 104 additions and 42 deletions

20
s.c
View file

@ -1178,16 +1178,16 @@ char *s_code[] = {
"(i10)2",
"P", "print-command-line-options",
"%!1,,,,#0#1#2#3.4p?{.0^a}{P12}.!0&0{%1.0ddda,.1dda,.2da,,,,#0#1#2.3?{."
"3S3}{'0}.!0.4?{.4S3}{'0}.!1.5?{.5S3}{'0}.!2'0,.2^,.2^+=?{${.8,'(s22:in"
"valid options record),@(y5:error)[02}}'0,.3^=?{'0}{'1,.3^+},'0,.2^>?{'"
"0,.3^>}{f}?{'2,.3^,.3^++}{.2^,.2^+}+]7}.!1${.7,.4^,@(y5:%25map1)[02}.!"
"2${.4^,'0c,@(y3:max),@(y13:apply-to-list)[02}.!3${.4^,.8,,#0.0,.6,.(i1"
"0),&3{%2.0u~?{.1a,.1a,:1^,'(s2: )W4.0dddda,.1ddda,.2dda,.3da,.0?{.1}{"
"f}?{:1^,.1W4:1^,'(s2:, )W4:1^,.2W4.2?{:1^,'(s1:=)W4:1^,.3W4}}{.0?{:1^,"
".1W4.2?{:1^,'(s1: )W4:1^,.3W4}}{.1?{:1^,.2W4.2?{:1^,'(s1:=)W4:1^,.3W4}"
"}{f}}}:1^,'(c ),.7,:0^-S2W4:1^,'(s4: )W4:1^,.4W4:1^W6.7d,.7d,:2^[82"
"}]2}.!0.0^_1[02}'4,.4^,'2++]6",
"%!1,,,,#0#1#2#3.4p?{.4a}{P11}.!0&0{%1.0ddda,.1dda,.2da,,,,#0#1#2.3?{.3"
"S3}{'0}.!0.4?{.4S3}{'0}.!1.5?{.5S3}{'0}.!2'0,.2^,.2^+=?{${.8,'(s22:inv"
"alid options record),@(y5:error)[02}}'0,.3^=?{'0}{'1,.3^+},'0,.2^>?{'0"
",.3^>}{f}?{'2,.3^,.3^++}{.2^,.2^+}+]7}.!1${.7,.4^,@(y5:%25map1)[02}.!2"
"${.4^,'0c,@(y3:max),@(y13:apply-to-list)[02}.!3${.4^,.8,,#0.0,.6,.(i10"
"),&3{%2.0u~?{.1a,.1a,:1^,'(s2: )W4.0dddda,.1ddda,.2dda,.3da,.0?{.1}{f"
"}?{:1^,.1W4:1^,'(s2:, )W4:1^,.2W4.2?{:1^,'(s1:=)W4:1^,.3W4}}{.0?{:1^,."
"1W4.2?{:1^,'(s1: )W4:1^,.3W4}}{.1?{:1^,.2W4.2?{:1^,'(s1:=)W4:1^,.3W4}}"
"{f}}}:1^,'(c ),.7,:0^-S2W4:1^,'(s4: )W4:1^,.4W4:1^W6.7d,.7d,:2^[82}"
"]2}.!0.0^_1[02}'4,.4^,'2++]6",
0, 0, 0
};

View file

@ -2171,7 +2171,7 @@
; printer for optmap options used for --help; returns offset of help lines
(define (print-command-line-options optmap . ?port)
(define port (if (pair? ?port) (car port) (standard-error-port)))
(define port (if (pair? ?port) (car ?port) (standard-output-port)))
(define (optlen i)
(let ([co (cadr i)] [lo (caddr i)] [oa (cadddr i)])
(define colen (if co (string-length co) 0))

View file

@ -2300,7 +2300,7 @@
(define (run-script filename args)
(define env (interaction-environment))
(define ci? #f) ; normal load-like behavior is the default
(define callmain #f)
(define callmain #f) ; got changed via first #! line
(define main-args (cons filename args))
(let* ([filepath (and (string? filename) (file-resolve-relative-to-current filename))]
[fileok? (and (string? filepath) (file-exists? filepath))])
@ -2312,7 +2312,8 @@
(let ([x0 (read-code-sexp port)])
(when (shebang? x0)
(let ([shs (symbol->string (shebang->symbol x0))])
(cond [(string-position "r7rs" shs)] ; ok, repl env will do
(cond [(string-position "r7rs" shs)
(command-line main-args)]
[(string-position "r5rs" shs)
(set! env (scheme-report-environment 5))
(set! ci? #t)]
@ -2460,21 +2461,54 @@
;--------------------------------------------------------------------------------------------------
(define *skint-options*
'([verbose "-v" "--verbose" #f "Increase output verbosity"]
[quiet "-q" "--quiet" #f "Suppress nonessential messages"]
[append-libdir "-A" "--append-libdir" "<DIR>" "Append a library search directory"]
[prepend-libdir "-I" "--prepend-libdir" "<DIR>" "Prepend a library search directory"]
[eval "-e" "--eval" "<SEXP>" "Evaluate and print an expression"]
[script "-s" "--script" "<FILE>" "Run file as a Scheme script"]
[program "-p" "--program" "<FILE>" "Run file as a Scheme program"]
[version "-V" "--version" #f "Display version info"]
[help "-h" "--help" #f "Display this help"]
'([verbose "-v" "--verbose" #f "Increase output verbosity"]
[quiet "-q" "--quiet" #f "Suppress nonessential messages"]
[append-libdir "-A" "--append-libdir" "DIR" "Append a library search directory"]
[prepend-libdir "-I" "--prepend-libdir" "DIR" "Prepend a library search directory"]
[eval "-e" "--eval" "SEXP" "Evaluate and print an expression"]
[script "-s" "--script" "FILE" "Run file as a Scheme script"]
[program "-p" "--program" "FILE" "Run file as a Scheme program"]
[version "-V" "--version" #f "Display version info"]
[help "-h" "--help" #f "Display this help"]
))
(define *skint-version* "0.1.9")
(define (skint-main)
; todo: here we can process command line by ourselves!
; see if command line asks for special processing
(define (eval! str print?)
(let ([obj (read-from-string str)])
(for-each (lambda (val) (when print? (write val) (newline)))
(call-with-values (lambda () (eval obj)) list))))
(define (print-version!)
(format #t "(version ~s)~%(scheme.id skint)~%" *skint-version*))
(define (print-help!)
(format #t "SKINT Scheme Interpreter v~a~%" *skint-version*)
(format #t "usage: skint [OPTION]... [FILE] [ARG]...~%")
(format #t "~%")
(format #t "Options:~%")
(print-command-line-options *skint-options* (current-output-port))
(format #t "~%")
(format #t "If no FILE is given, skint enters REPL~%")
(format #t "~%"))
(define cl (command-line))
(let loop ([args (cdr cl)] [repl? #t])
(get-next-command-line-option args *skint-options* ;=>
(lambda (keysym optarg restargs)
(sexp-case (if optarg (list keysym optarg) (list keysym))
[(verbose) (set! *verbose* #t) (loop restargs #t)]
[(quiet) (set! *quiet* #t) (loop restargs #t)]
[(append-libdir *) (append-library-path! optarg) (loop restargs #t)]
[(prepend-libdir *) (prepend-library-path! optarg) (loop restargs #t)]
[(eval *) (eval! optarg #t) (loop restargs #f)]
[(script *) (run-script optarg restargs)] ; will exit if a script
[(version) (print-version!) (loop '() #f)]
[(help) (print-help!) (loop '() #f)]
[(#f) (cond [(pair? restargs) (run-script (car restargs) (cdr restargs))]
[(not repl?) (exit #t)])])))) ; all done -- no need to continue
; falling through to interactive mode
(when (and (tty-port? (current-input-port)) (tty-port? (current-output-port)))
; quick check for non-interactive use failed, greet
(display "SKINT Scheme Interpreter v0.0.9\n")
(display "Copyright (c) 2024 False Schemers\n"))
(format #t "SKINT Scheme Interpreter v~a~%" *skint-version*)
(format #t "Copyright (c) 2024 False Schemers~%"))
#t) ; exited normally

62
t.c
View file

@ -1448,13 +1448,14 @@ char *t_code[] = {
".4S0?{${.6,@(y32:file-resolve-relative-to-current)[01}}{f},.0S0?{.0F0}"
"{f},.0~?{${.3,.9,'(s17:cannot run script),@(y5:error)[03}}${.6,.8,.6,."
"8,.7,&5{%0:1,:2,:3,:4,&4{%1${.2,@(y14:read-code-sexp)[01},#0.0^Y5?{.0^"
"Y7X4,.0,'(s4:r7rs)S8,.0?{.0}{.1,'(s4:r5rs)S8?{${'5,@(y25:scheme-report"
"-environment)[01}:!2t:!3}{${.3,'(s41:only scheme-r[57]rs scripts are s"
"upported),@(y5:error)[02}}}_1:3^?{t,.3P79}t:!0${.4,@(y14:read-code-sex"
"p)[01}.!1_1}${.2^,,#0:2,.6,.2,&3{%1.0R8~?{${:2^,.3,@(y4:eval)[02}${:1,"
"@(y14:read-code-sexp)[01},:0^[11}]1}.!0.0^_1[01}:0^?{${:2^,n,n,:1^c,'("
"y5:quote)cc,'(y4:main)c,@(y4:eval)[02},@(y4:exit)[21}]2},:0,@(y20:call"
"-with-input-file)[02},.4,@(y17:with-current-file)[02}Y9]8",
"Y7X4,.0,'(s4:r7rs)S8?{${:1^,@(y12:command-line)[01}}{.0,'(s4:r5rs)S8?{"
"${'5,@(y25:scheme-report-environment)[01}:!2t:!3}{${.2,'(s41:only sche"
"me-r[57]rs scripts are supported),@(y5:error)[02}}}:3^?{t,.3P79}t:!0${"
".4,@(y14:read-code-sexp)[01}.!1_1}${.2^,,#0:2,.6,.2,&3{%1.0R8~?{${:2^,"
".3,@(y4:eval)[02}${:1,@(y14:read-code-sexp)[01},:0^[11}]1}.!0.0^_1[01}"
":0^?{${:2^,n,n,:1^c,'(y5:quote)cc,'(y4:main)c,@(y4:eval)[02},@(y4:exit"
")[21}]2},:0,@(y20:call-with-input-file)[02},.4,@(y17:with-current-file"
")[02}Y9]8",
"P", "repl-evaluate-top-form",
"%3,,#0#1.!0${.2,&1{%!0.0:!0]1},.6,.6,&2{%0:1,:0,@(y17:evaluate-top-for"
@ -1560,18 +1561,45 @@ char *t_code[] = {
"C", 0,
"'(l9:l5:y7:verbose;s2:-v;s9:--verbose;f;s25:Increase output verbosity;"
";l5:y5:quiet;s2:-q;s7:--quiet;f;s30:Suppress nonessential messages;;l5"
":y13:append-libdir;s2:-A;s15:--append-libdir;s5:<DIR>;s33:Append a lib"
"rary search directory;;l5:y14:prepend-libdir;s2:-I;s16:--prepend-libdi"
"r;s5:<DIR>;s34:Prepend a library search directory;;l5:y4:eval;s2:-e;s6"
":--eval;s6:<SEXP>;s32:Evaluate and print an expression;;l5:y6:script;s"
"2:-s;s8:--script;s6:<FILE>;s27:Run file as a Scheme script;;l5:y7:prog"
"ram;s2:-p;s9:--program;s6:<FILE>;s28:Run file as a Scheme program;;l5:"
"y7:version;s2:-V;s9:--version;f;s20:Display version info;;l5:y4:help;s"
"2:-h;s6:--help;f;s17:Display this help;;)@!(y15:*skint-options*)",
":y13:append-libdir;s2:-A;s15:--append-libdir;s3:DIR;s33:Append a libra"
"ry search directory;;l5:y14:prepend-libdir;s2:-I;s16:--prepend-libdir;"
"s3:DIR;s34:Prepend a library search directory;;l5:y4:eval;s2:-e;s6:--e"
"val;s4:SEXP;s32:Evaluate and print an expression;;l5:y6:script;s2:-s;s"
"8:--script;s4:FILE;s27:Run file as a Scheme script;;l5:y7:program;s2:-"
"p;s9:--program;s4:FILE;s28:Run file as a Scheme program;;l5:y7:version"
";s2:-V;s9:--version;f;s20:Display version info;;l5:y4:help;s2:-h;s6:--"
"help;f;s17:Display this help;;)@!(y15:*skint-options*)",
"C", 0,
"'(s5:0.1.9)@!(y15:*skint-version*)",
"P", "skint-main",
"%0PiP09?{PoP09}{f}?{Po,'(s32:SKINT Scheme Interpreter v0.0.9%0a)W4Po,'"
"(s34:Copyright (c) 2024 False Schemers%0a)W4}t]0",
"%0,,,,#0#1#2#3&0{%2${.2,@(y16:read-from-string)[01},${@(y4:list),.3,&1"
"{%0:0,@(y4:eval)[01},@(y16:call-with-values)[02},.3,&1{%1:0?{Po,.1W5Po"
"W6]1}]1},@(y10:%25for-each1)[32}.!0&0{%0@(y15:*skint-version*),'(s33:("
"version ~s)~%25(scheme.id skint)~%25),t,@(y6:format)[03}.!1&0{%0${@(y1"
"5:*skint-version*),'(s30:SKINT Scheme Interpreter v~a~%25),t,@(y6:form"
"at)[03}${'(s42:usage: skint [OPTION]... [FILE] [ARG]...~%25),t,@(y6:fo"
"rmat)[02}${'(s2:~%25),t,@(y6:format)[02}${'(s10:Options:~%25),t,@(y6:f"
"ormat)[02}${Po,@(y15:*skint-options*),@(y26:print-command-line-options"
")[02}${'(s2:~%25),t,@(y6:format)[02}${'(s40:If no FILE is given, skint"
" enters REPL~%25),t,@(y6:format)[02}'(s2:~%25),t,@(y6:format)[02}.!2${"
"@(y12:command-line)[00}.!3${t,.6^d,,#0.7,.1,.8,.8,&4{%2:0,:1,:2,:3,.5,"
"&5{%3.1?{.1,.1,l2}{.0,l1},${.2,'(l1:y7:verbose;),@(y11:sexp-match?)[02"
"}?{t@!(y9:*verbose*)t,.4,:2^[42}${.2,'(l1:y5:quiet;),@(y11:sexp-match?"
")[02}?{t@!(y7:*quiet*)t,.4,:2^[42}${.2,'(l2:y13:append-libdir;y1:*;),@"
"(y11:sexp-match?)[02}?{${.4,@(y20:append-library-path!)[01}t,.4,:2^[42"
"}${.2,'(l2:y14:prepend-libdir;y1:*;),@(y11:sexp-match?)[02}?{${.4,@(y2"
"1:prepend-library-path!)[01}t,.4,:2^[42}${.2,'(l2:y4:eval;y1:*;),@(y11"
":sexp-match?)[02}?{${t,.5,:4^[02}f,.4,:2^[42}${.2,'(l2:y6:script;y1:*;"
"),@(y11:sexp-match?)[02}?{.3,.3,@(y10:run-script)[42}${.2,'(l1:y7:vers"
"ion;),@(y11:sexp-match?)[02}?{${:3^[00}f,n,:2^[42}${.2,'(l1:y4:help;),"
"@(y11:sexp-match?)[02}?{${:1^[00}f,n,:2^[42}${.2,'(l1:f;),@(y11:sexp-m"
"atch?)[02}?{.3p?{.3d,.4a,@(y10:run-script)[42}:0~?{t,@(y4:exit)[41}f]4"
"}]4},@(y15:*skint-options*),.2,@(y28:get-next-command-line-option)[23}"
".!0.0^_1[02}PiP09?{PoP09}{f}?{${@(y15:*skint-version*),'(s30:SKINT Sch"
"eme Interpreter v~a~%25),t,@(y6:format)[03}${'(s35:Copyright (c) 2024 "
"False Schemers~%25),t,@(y6:format)[02}}t]4",
0, 0, 0
};