lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 163 forks source link

`stmt Assign` printed as `=` #1466

Open rebcabin opened 1 year ago

rebcabin commented 1 year ago

I use ASR.adsl to generate ASR processor functions. I read ASR.adsl and find a stmt node named Assign.

stmt
    = ...
    | Assign(int label, identifier variable)
    | ...

Then I print lpython --show-asr --no-color examples/expr2.py and find, in the body slot of a function, (edited)

       [(=                        ;  stmt * body, head, term: ? ... stmt.Assign ? 
         (Var 2 x)                ;
         (IntegerBinOp
          (IntegerBinOp
           (IntegerConstant 2 (Integer 4 []))
           Add
...
          (IntegerConstant 25 (Integer 4 [])))
         ())
        (Print () [(Var 2 x)] () ())]

That messes me up as I must now write a spec for = by hand instead of automatically reading it out of ASR.adsl.

It also undermines my confidece ... how many other ASR terms have undocumented aliases?

certik commented 1 year ago

I think we should get rid of aliases. I started with that in AST, being inspired by LISP (!!), to have nodes like (+ 1 2) instead of (BinOp 1 Add 2). But after quite a lot of experience with AST and ASR printouts, I think it is not worth it. It is much better to be 100% consistent and always use the full names. This is done here and we should get rid of it: https://github.com/lcompilers/lpython/blob/2c5034a299c7ec3edcd6bb13daab1434b8e6d0cc/src/libasr/asdl_cpp.py#L1239

Note that the node you want is called Assignment (such as x = 3), not Assign (which is a Fortran Assign statement), see https://github.com/lcompilers/lpython/issues/1467.

rebcabin commented 1 year ago

Thanks for the answer. I'm leaving this open as a tracking issue for getting rid of the subs. The goal of (at least our new Clojure work) is for ASR to be machine-checkable, not just human-readable, and subs introduces "another layer of mapping."

rebcabin commented 1 year ago

my ad-hoc "mapping layer":

(def expr2-lpy "examples/expr2.py")
(def expr2-clj
  (walk/prewalk
   (fn [node]
     ;; see https://github.com/lcompilers/lpython/issues/1466
     (cond (= node '=) 
           'Assignment
           :else
           node))
   (lpython/get-sample-clj expr2-lpy)))