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
could_be_formal_argument: bool => !type_expression?;
23
24
try_copy_as_variable_left() -> Variables.VariableLeft? =>
25
if !could_be_formal_argument then
26
null
27
else
28
Variables.SIMPLE_VARIABLE_LEFT(location, Identifiers.Identifier(location, "_"))
29
fi;
30
31
try_copy_as_variable() -> VARIABLE? =>
32
if !could_be_formal_argument then
33
null
34
else
35
VARIABLE(location, Identifiers.Identifier(location, "_"), TypeExpressions.INFER(location), null)
36
fi;
37
38
set_expected_type(expected_type: Semantic.Types.Type?, error_message: string?) is
39
compile_expressions_state.set_expected_type(expected_type, error_message);
40
si
41
42
accept(visitor: Visitor) is
43
visitor.visit(self);
44
si
45
46
walk(visitor: Visitor) is
47
if !visitor.pre(self) then
48
if type_expression? then
49
type_expression.walk(visitor);
50
fi
51
fi
52
53
accept(visitor);
54
si
55
si
56
si