diff options
author | bd <bdunaisky@umass.edu> | 2024-11-04 20:13:50 -0500 |
---|---|---|
committer | bd <bdunaisky@umass.edu> | 2024-11-04 20:13:50 -0500 |
commit | bad998157c95851eb29d3e4f6824a276d7032b12 (patch) | |
tree | c5d39f90f5048faa4422c8c347d7cd4d497e0565 | |
parent | 64de3b65fec63268fb17cf7b76dba69c68ea7d1d (diff) |
Add modules, open port for reading
-rw-r--r-- | .dir-locals.el | 4 | ||||
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | manifest.scm | 5 | ||||
-rwxr-xr-x | src/gscc | 23 | ||||
-rw-r--r-- | src/modules/lexer/lexer.scm | 31 |
5 files changed, 53 insertions, 11 deletions
diff --git a/.dir-locals.el b/.dir-locals.el new file mode 100644 index 0000000..cb836da --- /dev/null +++ b/.dir-locals.el @@ -0,0 +1,4 @@ +;;; Directory Local Variables -*- no-byte-compile: t -*- +;;; For more information see (info "(emacs) Directory Variables") + +((scheme-mode . ((compile-command . "guix shell -m manifest.scm -- ./writing-a-c-compiler-tests/test_compiler ./src/gscc --chapter 1 --stage lex")))) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a231d4b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/writing-a-c-compiler-tests/ diff --git a/manifest.scm b/manifest.scm new file mode 100644 index 0000000..be6c84e --- /dev/null +++ b/manifest.scm @@ -0,0 +1,5 @@ +(specifications->manifest + '( + "gcc-toolchain" + "python" + )) @@ -1,9 +1,10 @@ #!/run/current-system/profile/bin/guile \ --e main -s +-L ./src -e main -s !# -(use-modules (ice-9 getopt-long)) +(use-modules (ice-9 getopt-long) + (modules lexer lexer)) (define version "v0.1") @@ -30,8 +31,11 @@ file on a success." (zero? (system (string-concatenate `("gcc -E -P " ,file " -o " ,preprocessed-file-name)))) preprocessed-file-name))) -(define (process file lex? parse? codegen?) - "") +(define (process file parse? assemble?) + "Driver for lexing, parsing, and assembly generation." + (let ((port (open-input-file file))) + (read-tokens-from-port port) + (close-input-port port))) (define (postprocess file dest) "Assembles and links file, producing an executable. @@ -50,7 +54,7 @@ Returns #f on a failure, #t on a success." (file (if (null? rest) #f (car rest)))) (cond ((option-ref options 'version #f) - (display (string-concatenate `("gscc (the 'Guile Scheme C Compiler', " ,version ")")))) + (display (string-concatenate `("gscc (the 'Guile Scheme C Compiler', " ,version ")\n")))) ((not (equal? 1 (length rest))) (error "Wrong number of arguments.")) ((or (not file) (not (access? file R_OK)) @@ -58,13 +62,10 @@ Returns #f on a failure, #t on a success." (#t (let ((source (preprocess file))) (when source - (display (string-concatenate `("Preprocess reported success (wrote " ,source ")."))) + (display (string-concatenate `("Preprocess reported success (wrote " ,source ").\n"))) (process source - (option-ref options 'lex #f) - (option-ref options 'parse #f) - (option-ref options 'codegen #f)) - (newline) - (display "Done!"))))))) + (not (option-ref options 'lex #f)) + (not (option-ref options 'parse #f))))))))) ;; Local Variables: diff --git a/src/modules/lexer/lexer.scm b/src/modules/lexer/lexer.scm new file mode 100644 index 0000000..0661fa6 --- /dev/null +++ b/src/modules/lexer/lexer.scm @@ -0,0 +1,31 @@ +(define-module (modules lexer lexer) + #:export (read-tokens-from-port)) + +(define (read-tokens-from-port port) + "" + (display (read-char port))) + +(define (read-token) + + (define (read-token-loop first-char) + (cond + + ((or (char-whitespace? first-char)) + (read-token) + + ((eq? first-char #\() + (cons 'left-paren #\()) + ((eq? first-char #\)) + (cons 'right-paren #\))) + ((eq? first-char #\{) + (cons 'right-bracket #\{)) + ((eq? first-char #\}) + (cons 'right-bracket #\})) + + (#t + (error "illegal lexical syntax"))))) + + (let ((first-char (read-char))) + (if (eof-object? first-char) + '() + (cons (read-token-loop first-char) '())))) |