Module: Jade::Frontend::Desugaring

Extended by:
Desugaring
Included in:
Desugaring
Defined in:
lib/jade/frontend/desugaring.rb,
lib/jade/frontend/desugaring/resolved.rb,
lib/jade/frontend/desugaring/placeholder.rb

Defined Under Namespace

Modules: Placeholder

Instance Method Summary collapse

Instance Method Details

#desugar(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/jade/frontend/desugaring.rb', line 13

def desugar(node)
  case node
  in AST::Module(body:)
    node.with(body: desugar(body))

  in AST::Body(expressions:)
    node.with(expressions: desugar_expressions(expressions))

  in AST::InfixApplication(left:, right:, operator:)
    case operator
    in AST::InfixOperator(value: '|>')
      case right
      in AST::FunctionCall(args:)
        right
          .with(args: [left] + args)
          .then { desugar(it) }

      else
        AST::FunctionCall.new(
          callee: right,
          args: [left],
          infix: operator,
          range: node.range,
        )

      end
        .then { desugar(it) }

    else
      AST::FunctionCall.new(
        callee: AST::VariableReference["(#{operator.value})", operator.range],
        args: [desugar(left), desugar(right)],
        infix: operator,
        range: node.range,
      )
    end

  in AST::FunctionCall(callee:, args:)
    node
      .with(callee: desugar(callee))
      .with(args: args.map { desugar(it) })
      .then { Placeholder.lift(it) }

  in AST::KeyedCall(callee:, fields:)
    node
      .with(callee: desugar(callee))
      .with(fields: fields.map { desugar(it) })

  in AST::FunctionDeclaration(body:)
    node
      .with(body: desugar(body))

  in AST::MemberAccess(target:)
    node
      .with(target: desugar(target))

  in AST::CaseOf(expression:, branches:)
    node
      .with(expression: desugar(expression))
      .with(branches: branches.map { desugar(it) })

  in AST::CaseOfBranch(pattern:, body:)
    node
      .with(pattern: desugar(pattern))
      .with(body: desugar(body))

  in AST::IfThenElse(condition:, if_branch:, else_branch:)
    node
      .with(condition: desugar(condition))
      .with(if_branch: desugar(if_branch))
      .with(else_branch: desugar(else_branch))

  in AST::Assign(pattern:, expression:)
    node.with(pattern: desugar(pattern), expression: desugar(expression))

  in AST::Grouping(expression:)
    node.with(expression: desugar(expression))

  in AST::Lambda(params:, body:)
    node
      .with(params: params.map { desugar(it) })
      .with(body: desugar(body))

  in AST::List(items:)
    node
      .with(items: items.map { desugar(it) })

  in AST::RecordLiteral(fields:)
    fields.map { desugar(it) }.then { node.with(fields: it) }

  in AST::RecordUpdate(fields:)
    fields.map { desugar(it) }.then { node.with(fields: it) }

  in AST::RecordField(value:)
    desugar(value).then { node.with(value: it) }

  in AST::RecordAccessSugar(field_key:, range:)
    AST::VariableReference['x', nil]
      .then { AST::MemberAccess[it, AST::VariableReference[field_key, nil], range] }
      .then { AST::Body[[it], nil] }
      .then { AST::Lambda[[AST::Pattern::Binding['x', nil]], it, range] }

  in AST::RecordUpdateSugar(field_key:, range:)
    value_reference = AST::VariableReference['value', nil]

    AST::VariableReference['x', nil]
      .then { AST::RecordUpdate[it, [AST::RecordField[field_key, value_reference, nil]], range] }
      .then { AST::Body[[it], nil] }
      .then do |body|
        AST::Lambda[
          [
            AST::Pattern::Binding['x', nil],
            AST::Pattern::Binding[value_reference.name, nil],
          ],
          body,
          range,
        ]
      end

  in AST::Tuple(items:)
    AST::FunctionCall.new(
      callee: AST::ConstructorReference["Tuple.Tuple#{items.size}", node.range],
      args: items.map { desugar(it) },
      infix: false,
      range: node.range,
    )

  in AST::Pattern::Tuple(patterns:)
    AST::ConstructorReference[
      "Tuple.Tuple#{patterns.size}",
      node.range,
    ]
      .then do
        AST::Pattern::Constructor.new(
          constructor: it,
          patterns: patterns.map { desugar(it) },
          range: node.range,
        )
      end

  in AST::Pattern::Constructor(patterns:)
    node.with(patterns: patterns.map { desugar(it) })

  in AST::Pattern::Record(fields:)
    fields
      .map { it.with(pattern: desugar(it.pattern)) }
      .then { node.with(fields: it) }

  in AST::Pattern::List(patterns:, rest:)
    node
      .with(patterns: patterns.map { desugar(it) })
      .with(rest: rest && desugar(rest))

  in AST::Implementation(functions:)
    functions
      .map { desugar(it) }
      .then { node.with(functions: it) }

  in AST::ImplementationFunction(fn:)
    node.with(fn: desugar(fn))

  in AST::Literal | AST::CharLiteral | AST::VariableReference | AST::ConstructorReference |
    AST::TypeDeclaration | AST::ImportDeclaration |
    AST::Pattern::Literal | AST::Pattern::Binding | AST::Pattern::Wildcard |
    AST::Pattern::Record | AST::InteropImportDeclaration | AST::StructDeclaration |
    AST::QualifiedAccess | AST::Placeholder | AST::InterfaceDeclaration

    node
  end
end

#desugar_entry(entry) ⇒ Object



8
9
10
11
# File 'lib/jade/frontend/desugaring.rb', line 8

def desugar_entry(entry)
  desugar(entry.ast)
    .then { entry.with(ast: it) }
end

#desugar_resolved(node, registry) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/jade/frontend/desugaring/resolved.rb', line 14

def desugar_resolved(node, registry)
  case node
  in AST::VariableReference | AST::QualifiedAccess
    zero_arg_fn?(node.symbol, registry) ? wrap_call(node) : node

  else
    map_children(node) { desugar_resolved(it, registry) }
  end
end

#desugar_resolved_entry(entry, registry) ⇒ Object

Runs after SemanticAnalysis (which resolves names and attaches symbols). Add new post-resolution rewrites as ‘in` branches below; `map_children` handles transparent recursion.



9
10
11
12
# File 'lib/jade/frontend/desugaring/resolved.rb', line 9

def desugar_resolved_entry(entry, registry)
  desugar_resolved(entry.ast, registry)
    .then { entry.with(ast: it) }
end