nanopass / nanopass-framework-scheme

The new nanopass framework; an embedded DSL for writing compilers in Scheme
nanopass.org
MIT License
487 stars 55 forks source link

Exception: meta not found #18

Closed gwatt closed 6 years ago

gwatt commented 6 years ago

I've defined two simple languages:

(define-language N
  (terminals (number (n)))
  (Num ()
    n))

(define-language M
  (terminals (number (n)))
  (Math (e)
    n
    (+ e0 e1)
    (* e0 e1)
    (- e0 e1)
    (/ e0 e1)))

the pass between the two is similarly simple:

(define-pass M->N : M (e) -> N ()
  (Eval : Math (e) -> Num ()
    ((+ ,(e0) ,(e1)) (+ e0 e1))
    ((* ,(e0) ,(e1)) (* e0 e1))
    ((- ,(e0) ,(e1)) (- e0 e1))
    ((/ ,(e0) ,(e1)) (/ e0 e1))
    (,n n)))

But, I get this error:

Exception in N: meta not found e0 in N

I can easily fix this by adding e to the meta-variable list for the nonterminal Num, but I don't think I should have to. Is this behavior intentional?

akeep commented 6 years ago

The issue here is your use of e0 and e1 in the catamorphisms in the Eval processor of the M->N. The names you use here are actually significant because it is used to determine the type of the processor that should be called (it is not simply recursion to the current processor).

,[e0] is effectively shorthand for ,[e -> e0] (where the first e is determined from looking at the production and determining that an e is what is produced there, so it is looking for there to be an e metavariable in the base. However you are not converting from Math in M to some variant on Math in N, you converting from Math in M to Num in N so you need to use an appropriately named output variable like n0 and n1.

If you were just matching (e.g. not doing a catamorphism) than ((+ ,e0 ,e1) ---) would be correct, since there it would be looking to match an incomingMath` production.

akeep commented 6 years ago

Ah, sorry you actually also need to give the Num production some sort of metavariable so that you can use it in expressions. Or you could just change your transform to be:

(define-pass M->N : M (e) -> N ()
    (Eval : Math (e) -> number ()
      ((+ ,(n0) ,(n1)) (+ n0 n1))
      ((* ,(n0) ,(n1)) (* n0 n1))
      ((- ,(n0) ,(n1)) (- n0 n1))
      ((/ ,(n0) ,(n1)) (/ n0 n1))
      (,n n)))

All of this is really type driven, and since you've made the type of Eval :: Math -> Num, but Num doesn't have an associated metavariable, you cannot express the type of the processor in metavariables for the catamorphism.

If you did want to keep something that looked more like the original, you could choose a different metavariable for Num, like num to avoid conflicting with other names:

(define-language N
  (terminals (number (n)))
  (Num (num)
    n))
(define-pass M->N : M (e) -> N ()
  (Eval : Math (e) -> Num ()
    ((+ ,(num0) ,(num1)) (+ num0 num1))
    ((* ,(num0) ,(num1)) (* num0 num1))
    ((- ,(num0) ,(num1)) (- num0 num1))
    ((/ ,(num0) ,(num1)) (/ num0 num1))
    (,n n)))

I did notice that it is not possible to have a nanopass language without a nonterminal, which is probably a bug I should fix.

gwatt commented 6 years ago

Ah, perfect. Replacing eX with nX in the cataform worked. Thank you!