pic18/define-pic18-forth.ss
#lang scheme/base
;; PIC18 code to support the Forth syntax.

;; I'm moving away from Forth syntax as the basic abstraction for code
;; definitions.  The default should now be scheme and the `words' and
;; `variables' forms.  The main reason is that Forth syntax is
;; inherently flat and therefore mixes badly with scheme syntax trees.

;; However, even if the Forth syntax is a bit of a horribly complex
;; mess of hygienic and non-hygic macros written by trial and error,
;; it is still useful in the interactive console.

(require
 "../tools.ss"
 "../forth/forth-lex.ss"
 "../forth/forth-tx.ss"
 "forth.ss"                 ;; PIC18 specific forth words
 "../comp/postprocess.ss"
 "../macro.ss"
 "../coma/macro-forth.ss"
 (for-syntax scheme/base
             "../tools/grabbag.ss"))

(provide
 define-pic18-forth)

;; Let's just put everything behind a single instantiation macro.
;; Collecting all this in a unit can be done later.

(define-syntax (define-pic18-forth stx)
  (syntax-case stx ()
    ((_ 
      ;; provides
      (forth-parse
       pic18-module-begin
       forth-compile
       forth-load/compile
       )
      
      ;; requirements
      (compile!
       code-register-postponed!
       wrap-macro
       wrap-word
       wrap-variable))

     ;; The non-hygienic part is for `define-forth-parser' so it
     ;; places the identifiers in the correct context.
     (datum->syntax
      stx 
      `(begin

         ;; ---- FORTH PARSER

         ;; Link the parser to the compiler: create defining prefix
         ;; macros in terms of code wrapping functions and the code
         ;; registry.
         (define-forth-parser ,#'forth-parse ,#'(code-register-postponed!
                                                 wrap-macro
                                                 wrap-word
                                                 wrap-variable))

         ,#'(begin
              (provide forth-begin)

              ;; Toplevel begin used for interactive development.
              (define-syntax (forth-begin stx)
                (syntax-case stx ()
                  ((_ . code)
                   #`(begin
                       (forth-parse
                        path #,(build-path (home) "pic18") ;; library path
                        . code)
                       (compile!)))))

              ;; Module level begin used in pic18/lang.ss
              (define-syntax-rule (pic18-module-begin . words)
                (#%plain-module-begin
                 (forth-begin . words)))


              ;; Note: all compilation needs to be in macro from.  Interactive
              ;; compilation always needs "eval" to call a macro as a function, to
              ;; make sure it is at toplevel to introduce bindings in the module or
              ;; toplevel namespace.

              ;; String/file read-eval interface.
              (define-syntax-rule (forth-compile str)
                (forth-lex-string/cps forth-begin str))
              (define-syntax-rule (forth-load/compile str)
                (forth-begin load str))


              
              ))))))