aboutsummaryrefslogtreecommitdiff
path: root/module/c/compiler.scm
diff options
context:
space:
mode:
Diffstat (limited to 'module/c/compiler.scm')
-rw-r--r--module/c/compiler.scm84
1 files changed, 28 insertions, 56 deletions
diff --git a/module/c/compiler.scm b/module/c/compiler.scm
index 09d49578..c1563a0b 100644
--- a/module/c/compiler.scm
+++ b/module/c/compiler.scm
@@ -1,64 +1,36 @@
(define-module (c compiler)
- :use-module ((c lex2) :select (lex))
- :use-module ((c trigraph) :select (replace-trigraphs))
- :use-module ((c line-fold) :select (fold-lines))
- :use-module (c cpp-environment object-like-macro)
- :use-module ((c cpp-environment)
- :select (make-environment
- extend-environment
- enter-file))
- :use-module (hnh util)
- ;; TODO importort
- ;; handle-preprocessing-tokens
- ;; load-and-tokenize-file
- :export (run-compiler))
+ :use-module ((srfi srfi-1) :select (remove))
+ :use-module (srfi srfi-71)
+ :use-module ((c cpp-environment) :select (enter-file))
+ :use-module ((hnh util) :select (-> ->>))
+ :use-module ((hnh util io) :select (read-file))
+ :use-module ((c preprocessor2)
+ :select (preprocess-string
+ make-default-environment))
+ :use-module ((hnh util values) :select (abort* on-fst))
+ :use-module ((c ast) :select (build-ast))
+ :export (run-compiler
+ compile-string
+ compile-string*
+ the-environment
+ ))
-"
-#define __STDC__ 1
-#define __STDC_HOSTED__ 1
-#define __STDC_VERSION__ 201112L
-"
-
-(define now (localtime (current-time)))
-(define default-macros
- (list
- ;; 6.10.8
- (object-like-macro
- identifier: "__STDC__"
- body: (lex "1"))
- (object-like-macro
- identifier: "__STDC_HOSTED__"
- body: (lex "1"))
- (object-like-macro
- identifier: "__STDC_VERSION__"
- body: (lex "201112L"))
- (object-like-macro
- identifier: "__DATE__"
- ;; TODO format should always be in
- ;; english, and not tranlated
- body: (lex (strftime "\"%b %_d %Y\"" now)))
- (object-like-macro
- identifier: "__TIME__"
- body: (lex (strftime "\"%H:%M:%S\"" now)))))
-
-(define environment
- (-> (make-environment)
- (extend-environment default-macros)))
+;;; 5.1.11.2 Translation phases
+(define* (run-compiler path key: (environment (make-default-environment)))
+ (-> path read-file (compile-string (enter-file environment path))))
-;;; 5.1.11.2 Translation phases
+(define* (compile-string str optional: (environment (make-default-environment)))
+ (on-fst (build-ast (abort* (preprocess-string str environment)))))
+
+(define the-environment (make-parameter (make-default-environment)))
-(define (run-compiler path)
- (define environment (enter-file (make-environment) path))
- (-> (load-and-tokenize-file path)
- (handle-preprocessing-tokens environment))
-;;; 5. (something with character sets)
-;;; 6. concatenation of string literals
-;;; 7. Whitespace tokens are discarded, each preprocessing token is converted into a token
- ;; 6.4 paragraph 2
- ;; Each preprocessing toket thas is converted to a token shall have the lexcal form of a keyword, an identifier, a constant, a string literal, or a puncturtor
-;;; 8. All external objects and functions are resolved
- )
+(define* (compile-string* str)
+ (let ((result cpp-env (compile-string str)))
+ (if (null? result)
+ (compile-string (string-append str ";")
+ (the-environment))
+ result)))