visitEnumDecl method

  1. @override
Uint8List visitEnumDecl(
  1. EnumDecl stmt
)
override

Implementation

@override
Uint8List visitEnumDecl(EnumDecl stmt) {
  final bytesBuilder = BytesBuilder();
  if (!stmt.isExternal) {
    bytesBuilder.addByte(HTOpCode.classDecl);
    bytesBuilder.add(_parseIdentifier(stmt.id.id));
    bytesBuilder.addByte(stmt.isExternal ? 1 : 0);
    bytesBuilder.addByte(0); // bool: is abstract
    bytesBuilder.addByte(stmt.isTopLevel ? 1 : 0);
    bytesBuilder.addByte(1); // bool: has user defined constructor
    bytesBuilder.addByte(0); // bool: has super class
    bytesBuilder.addByte(1); // bool: is enum

    final valueId = '${_lexicon.privatePrefix}${Semantic.name}';
    final value = VarDecl(IdentifierExpr(valueId), classId: stmt.id.id);
    final valueBytes = visitVarDecl(value);
    bytesBuilder.add(valueBytes);

    final ctorParam = ParamDecl(IdentifierExpr(Semantic.name));
    final ctorDef = AssignExpr(IdentifierExpr(valueId), _lexicon.assign,
        IdentifierExpr(Semantic.name));
    final constructor = FuncDecl(
        '${InternalIdentifier.namedConstructorPrefix}${_lexicon.privatePrefix}',
        id: IdentifierExpr(_lexicon.privatePrefix),
        classId: stmt.id.id,
        paramDecls: [ctorParam],
        minArity: 1,
        maxArity: 1,
        definition: ctorDef,
        category: FunctionCategory.constructor);
    final ctorBytes = visitFuncDecl(constructor);
    bytesBuilder.add(ctorBytes);

    final toStringDef = ASTStringInterpolation(
        '${stmt.id.id}${_lexicon.memberGet}${_lexicon.stringInterpolationStart}0${_lexicon.stringInterpolationEnd}',
        _lexicon.stringStart1,
        _lexicon.stringEnd1,
        [IdentifierExpr(valueId)]);
    final toStringFunc = FuncDecl(_lexicon.idToString,
        id: IdentifierExpr(_lexicon.idToString),
        classId: stmt.id.id,
        hasParamDecls: true,
        paramDecls: [],
        definition: toStringDef);
    final toStringBytes = visitFuncDecl(toStringFunc);
    bytesBuilder.add(toStringBytes);

    final itemList = <ASTNode>[];
    for (final item in stmt.enumerations) {
      itemList.add(item);
      final itemInit = CallExpr(
          MemberExpr(stmt.id,
              IdentifierExpr(_lexicon.privatePrefix, isLocal: false)),
          positionalArgs: [
            ASTLiteralString(
                item.id, _lexicon.stringStart1, _lexicon.stringEnd1)
          ]);
      final itemDecl = VarDecl(item,
          classId: stmt.classId,
          initializer: itemInit,
          isStatic: true,
          lateInitialize: true);
      final itemBytes = visitVarDecl(itemDecl);
      bytesBuilder.add(itemBytes);
    }

    final valuesInit = ListExpr(itemList);
    final valuesDecl = VarDecl(IdentifierExpr(_lexicon.idCollectionValues),
        classId: stmt.classId,
        initializer: valuesInit,
        isStatic: true,
        lateInitialize: true);
    final valuesBytes = visitVarDecl(valuesDecl);
    bytesBuilder.add(valuesBytes);

    bytesBuilder.addByte(HTOpCode.classDeclEnd);
  } else {
    bytesBuilder.addByte(HTOpCode.externalEnumDecl);
    bytesBuilder.add(_parseIdentifier(stmt.id.id));
    bytesBuilder.addByte(stmt.isTopLevel ? 1 : 0);
  }

  return bytesBuilder.toBytes();
}