Skip to content
← Back

src/syntax/trees/expressions/tuple_element.ghul

1
namespace Syntax.Trees.Expressions is
2
3
use Source
4
5
class TUPLE_ELEMENT: Expression is
6
name: Identifiers.Identifier
7
initializer: Expression?
8
type_expression: TypeExpressions.TypeExpression
9
is_tuple_element: bool => true
10
11
// Attribute pragmas carried over from the Expressions.VARIABLE
12
// this element was rewritten from — see Expressions.VARIABLE.
13
pragmas: Collections.LIST[Pragmas.PRAGMA]? public
14
15
init(
16
location: LOCATION,
17
name: Identifiers.Identifier,
18
type_expression: TypeExpressions.TypeExpression,
19
initializer: Expression?
20
)
21
is
22
super.init(location)
23
24
self.name = name
25
self.type_expression = type_expression
26
self.initializer = initializer
27
si
28
29
// The element's value is what the type is wanted of: the name
30
// only says which element of the tuple it is.
31
set_expected_type(expected_type: Semantic.Types.Type?, error_message: string?) is
32
if let value = initializer then
33
value.set_expected_type(expected_type, error_message)
34
fi
35
si
36
37
clear_expected_type() is
38
super.clear_expected_type()
39
40
if let value = initializer then
41
value.clear_expected_type()
42
fi
43
si
44
45
set_pragmas(pragmas: Collections.LIST[Pragmas.PRAGMA]) is
46
self.pragmas = pragmas
47
si
48
49
// A `name: T` element keeps that shape when the enclosing
50
// group turns out to be a destructure pattern rather than a
51
// tuple literal, where it is a leaf with a per-element type
52
// ascription exactly as in a `let`.
53
try_copy_as_variable_left() -> Variables.VariableLeft? is
54
let result = Variables.SIMPLE_VARIABLE_LEFT(location, name.copy())
55
56
if !isa TypeExpressions.INFER(type_expression) then
57
result.set_type_expression(type_expression.copy())
58
fi
59
60
return result
61
si
62
63
try_copy_as_variable() -> VARIABLE is
64
let result = VARIABLE(location, name, type_expression, initializer)
65
66
if let self_pragmas = pragmas then
67
result.set_pragmas(self_pragmas)
68
fi
69
70
return result
71
si
72
73
rewrite_as_expression() -> Expression is
74
IoC.CONTAINER.instance.logger.error(name.location, "name not allowed here")
75
76
IoC.CONTAINER.instance.logger.error(type_expression.location, "type not allowed here")
77
78
if pragmas? then
79
for pragma in pragmas do
80
IoC.CONTAINER.instance.logger.error(pragma.location, "attribute is not allowed here")
81
od
82
fi
83
84
return initializer!
85
si
86
87
accept(visitor: Visitor) is
88
visitor.visit(self)
89
si
90
91
walk(visitor: Visitor) is
92
if !visitor.pre(self) then
93
if pragmas? then
94
for pragma in pragmas do
95
pragma.walk(visitor)
96
od
97
fi
98
99
name.walk(visitor)
100
type_expression.walk(visitor)
101
102
if initializer? then
103
initializer.walk(visitor)
104
fi
105
fi
106
107
accept(visitor)
108
si
109
si
110
si