Skip to content
← Back

src/syntax/trees/expressions/default.ghul

1
namespace Syntax.Trees.Expressions is
2
3
use Source
4
5
// `type_expression` is the optional explicit type argument: `_[T]`.
6
// Null for a bare `_`, whose type is supplied by the context.
7
class DEFAULT(location: LOCATION, type_expression: TypeExpressions.TypeExpression?): Expression is
8
// Constraint pushed in by the parent context (assignment RHS,
9
// return position, typed `let` initializer, call argument).
10
// For a bare `_` this is what gives it its type.
11
12
super(location)
13
14
// An untyped `_` can stand in for a discard formal in a
15
// lambda's parameter group (`(_, b) => ...`, `_ => ...`): the
16
// group is parsed as a tuple expression first and reinterpreted
17
// as a pattern when the `=>` arrives, so `_` must round-trip
18
// back into a discard binding leaf here. declare-members renames
19
// the `_` name to a unique slot the way it does any other
20
// discard. `_[T]` has no reading as a formal — a formal's type
21
// comes from the `: T` syntax, not from `[T]`.
22
awaits_context_type: bool => !type_expression?
23
24
could_be_formal_argument: bool => !type_expression?
25
26
try_copy_as_variable_left() -> Variables.VariableLeft? =>
27
if !could_be_formal_argument then
28
null
29
else
30
Variables.SIMPLE_VARIABLE_LEFT(location, Identifiers.Identifier(location, "_"))
31
fi
32
33
try_copy_as_variable() -> VARIABLE? =>
34
if !could_be_formal_argument then
35
null
36
else
37
VARIABLE(location, Identifiers.Identifier(location, "_"), TypeExpressions.INFER(location), null)
38
fi
39
40
set_expected_type(expected_type: Semantic.Types.Type?, error_message: string?) is
41
compile_expressions_state.set_expected_type(expected_type, error_message)
42
si
43
44
accept(visitor: Visitor) is
45
visitor.visit(self)
46
si
47
48
walk(visitor: Visitor) is
49
if !visitor.pre(self) then
50
if type_expression? then
51
type_expression.walk(visitor)
52
fi
53
fi
54
55
accept(visitor)
56
si
57
si
58
si