du060334 2018-03-30 02:03
浏览 153

Golang将AST表示为结构并将其序列化

I want to encode/decode an AST in Golang but am running up against a roadblock with the lack of sumtypes.

A very trivial AST to demonstrate the problem:

data BoolAST = Or BoolAST BoolAST | And BoolAST BoolAST | Lit Bool

Approach 1

 type BoolStatement interface {
      ComputeBool() bool
      Serialize() []byte
 }
 type Or struct {
      Left BoolStatement
      Right BoolStatement
 }
 type And struct {
      Left BoolStatement
      Right BoolStatement
 }
 type BoolFunc struct {
      Value bool
 }

Now this structure serializes very well, But what I am having trouble with is deserializing (msgpack, json, etc). So You would have to wrap the interface in a struct so you can add a deserializer method. This to me also seems sloppy as I dont want to have to hand craft a serializer

Approach 2

To solve this I changed my struct a little bit. Everything is the same struct, but only one field is populated at a time. This code makes deserialization a breeze.

 type BoolAST struct {
      Or []BoolAST
      And []BoolAST
      Lit bool
 }

This works well to deserialize because empty fields are left nil, but then I have these empty fields. When interpreting the AST I have to use switch statements to find what field is null and that adds a tiny bit of overhead.

This also makes the json nice:

 {
    "and": [
       {"lit": true},
       {"or": [
           {"lit": false},
           {"lit": true}}
       ]
    ]
 }

But this can get crazy when the ast has many types of branching paths. You might end up with 10 nil fields on the struct.

It seems very hard with no sum types to solve this problem. Is this the right approach?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度