Writing a compiler for a DSL in python

前端 未结 8 1785
囚心锁ツ
囚心锁ツ 2021-02-02 03:51

I am writing a game in python and have decided to create a DSL for the map data files. I know I could write my own parser with regex, but I am wondering if there are existing py

8条回答
  •  被撕碎了的回忆
    2021-02-02 04:24

    Here's an approach that works really well.

    abc= ONETHING( ... )
    xyz= ANOTHERTHING( ... )
    pqr= SOMETHING( this=abc, that=123, more=(xyz,123) )
    

    Declarative. Easy-to-parse.

    And...

    It's actually Python. A few class declarations and the work is done. The DSL is actually class declarations.

    What's important is that a DSL merely creates objects. When you define a DSL, first you have to start with an object model. Later, you put some syntax around that object model. You don't start with syntax, you start with the model.

提交回复
热议问题