diff options
Diffstat (limited to 'src/gscc')
-rwxr-xr-x | src/gscc | 50 |
1 files changed, 27 insertions, 23 deletions
@@ -30,13 +30,10 @@ Options: (let ((extension (string-drop file (- (string-length file) 2)))) (string=? extension ".c"))) -(define (preprocess file) - "Returns an input port containing FILE processed with gcc." - (open-input-pipe (string-append "gcc -E -P " file))) - -(define (process port parse? generate? write?) +(define (process file parse? generate? write? dest) "Driver for lexing, parsing, and assembly generation." - (let* ((tokens (begin (set-current-input-port port) + (let* ((port (preprocess file)) + (tokens (begin (set-current-input-port port) (read-tokens)))) (close-input-port port) (when parse? @@ -44,7 +41,19 @@ Options: (when generate? (let ((assembly-ast (g-program c-ast))) (when write? - (e-program assembly-ast)))))))) + (let ((program (e-program assembly-ast)) + (a-file (string-append dest ".s"))) + (when (file-exists? a-file) + (delete-file a-file)) + (let ((port (open-output-file a-file))) + (display program port) + (close-port port)) + (when (postprocess a-file dest) + (display (string-concatenate `("Postprocess reported success (wrote " ,dest ").\n")))))))))))) + +(define (preprocess file) + "Returns an input port containing FILE processed with gcc." + (open-input-pipe (string-append "gcc -E -P " file))) (define (postprocess src dest) "Assembles and links SRC, producing executable DEST. @@ -52,6 +61,7 @@ Returns #f on a failure, #t on a success." (zero? (system (string-concatenate `("gcc " ,src " -o " ,dest))))) (define (main args) + "Entry point for the gscc. Handles user args and performs initial validity check." (let* ((option-spec '((version (single-char #\v) (value #f)) (debug (single-char #\d) (value #f)) @@ -60,9 +70,7 @@ Returns #f on a failure, #t on a success." (codegen (single-char #\c) (value #f)))) (options (getopt-long args option-spec)) (rest (option-ref options '() #f)) - (file (if (null? rest) #f (car rest))) - (executable (string-drop-right file 2)) - (assembly (string-append executable ".s"))) + (file (if (null? rest) #f (car rest)))) (cond ((option-ref options 'version #f) (display (string-concatenate `("gscc (the 'Guile Scheme C Compiler', " ,version ")\n")))) @@ -72,19 +80,15 @@ Returns #f on a failure, #t on a success." (not (equal? 'regular (stat:type (stat file)))) (not (c-extension? file))) (error "The file could not be read, or it is not a C source code file.")) (#t - (let* ((port (preprocess file)) - (parse? (not (option-ref options 'lex #f))) - (generate? (not (option-ref options 'parse #f))) - (write? (not (option-ref options 'codegen #f))) - (program (process port parse? generate? write?))) - (when write? - (when (file-exists? assembly) - (delete-file assembly)) - (let ((port (open-output-file assembly))) - (display program port) - (close-port port)) - (when (postprocess assembly executable) - (display (string-concatenate `("Postprocess reported success (wrote " ,executable ").\n")))))))))) + (let ((parse? (not (option-ref options 'lex #f))) + (generate? (not (option-ref options 'parse #f))) + (write? (not (option-ref options 'codegen #f)))) + (process file + parse? + generate? + write? + (string-drop-right file 2))))))) + ;; Local Variables: ;; mode: scheme |