visitLetDeclaration method

  1. @override
Future<TypedElement> visitLetDeclaration(
  1. LetDeclaration node
)
override

Implementation

@override
Future<TypedElement> visitLetDeclaration(LetDeclaration node) async {
  if (_environment.getDefinition(node.identifier.lexeme) != null) {
    throw IdentifierAlreadyDefinedError(node.identifier);
  }

  final expressionElement = await node.body.accept(this) as ExpressionElement;
  final TypedElement declaration;

  if (node.parameter case final parameter?) {
    final functionType = FunctionType(returnType: expressionElement.type!);

    final element = LetFunctionDeclaration(
      name: node.identifier.lexeme,
      parameter: parameter,
      type: functionType,
      body: expressionElement,
    );

    declaration = element;
    functionType.element = element;
  } else {
    declaration = LetVariableDeclaration(
      name: node.identifier.lexeme,
      type: expressionElement.type,
      body: expressionElement,
    );
  }

  expressionElement.enclosingElement = declaration;

  _environment.defineSymbol(node.identifier.lexeme, declaration);
  _environment = _environment.fork();

  node.body.accept(this);

  _environment = _environment.enclosing!;

  return declaration;
}