dannypsnl / typed-nanopass

rebuild nanopass with typed supports
Apache License 2.0
11 stars 0 forks source link

or `define-pass` is too complex, we just need a `recur-match*`? #5

Closed dannypsnl closed 1 year ago

dannypsnl commented 2 years ago

The idea of recur-match* is to write a code like

(struct Expr ())
(struct Int Expr
  ([val : Integer]))
(struct Add Expr
  ([left : Expr] [right : Expr]))
(define-language L0
  (terminals (Integer (n)))
  (Expr (e)
    ,n
    (Add ,e ,e)))

(define-language L1
  (Prog () ,@i)
  (Instruction (i) ...))

(: compile-expr : L0:Expr -> L1:Prog)
(define (compile-expr e)
  (recur-match* e
    #:lang L0
    #:on compile-expr
    [,n (list (mov 'x0 n))]
    [(Add ,[l] ,[r])
     `(,@l
       ,(mov 'x1 'x0)
       ,@r
       ,(add 'x0 'x1))]))
  1. #:lang spec the language, so it can get meta information to generate some code automatically.
  2. #:on keyword spec the recursive function for ,[x] pattern.
  3. match* stands for matching several terms at once, it would be convenient for type checking.

Problems here

  1. It's easy to see that, the current pattern didn't strictly require we write Add ,[e0] ,[e1], would this make problems in the future? I have no idea.
  2. Maybe redundant code to get information.