visitVarDecl method
Implementation
@override
Uint8List visitVarDecl(VarDecl stmt) {
final bytesBuilder = BytesBuilder();
final docs = stmt.documentation;
// Only const declaration with a const expression as initializer
// can be compiled into a const declaration,
// otherwise will be compiled as a normal variable declaration,
// with a static warning output.
if (stmt.isConstValue) {
bytesBuilder.addByte(HTOpCode.constDecl);
if (docs.isNotEmpty && !config.removeDocumentation) {
bytesBuilder.addByte(1); // bool: has doc
bytesBuilder.add(_utf8String(docs));
} else {
bytesBuilder.addByte(0); // bool: has doc
}
bytesBuilder.add(_parseIdentifier(stmt.id.id));
if (stmt.classId != null) {
bytesBuilder.addByte(1); // bool: has class id
bytesBuilder.add(_parseIdentifier(stmt.classId!));
} else {
bytesBuilder.addByte(0); // bool: has class id
}
bytesBuilder.addByte(stmt.isTopLevel ? 1 : 0);
late int type, index;
if (stmt.value is bool) {
type = HTConstantType.boolean.index;
index = _currentConstantTable.addGlobalConstant<bool>(stmt.value);
} else if (stmt.value is int) {
type = HTConstantType.integer.index;
index = _currentConstantTable.addGlobalConstant<int>(stmt.value);
} else if (stmt.value is double) {
type = HTConstantType.float.index;
index = _currentConstantTable.addGlobalConstant<double>(stmt.value);
} else if (stmt.value is String) {
type = HTConstantType.string.index;
index = _currentConstantTable.addGlobalConstant<String>(stmt.value);
}
bytesBuilder.addByte(type);
bytesBuilder.add(_uint16(index));
} else {
bytesBuilder.addByte(HTOpCode.varDecl);
if (docs.isNotEmpty && !config.removeDocumentation) {
bytesBuilder.addByte(1); // bool: has doc
bytesBuilder.add(_utf8String(docs));
} else {
bytesBuilder.addByte(0); // bool: has doc
}
bytesBuilder.add(_parseIdentifier(stmt.id.id));
if (stmt.classId != null) {
bytesBuilder.addByte(1); // bool: has class id
bytesBuilder.add(_parseIdentifier(stmt.classId!));
} else {
bytesBuilder.addByte(0); // bool: has class id
}
bytesBuilder.addByte(stmt.isField ? 1 : 0);
bytesBuilder.addByte(stmt.isExternal ? 1 : 0);
bytesBuilder.addByte(stmt.isStatic ? 1 : 0);
bytesBuilder.addByte(stmt.isMutable ? 1 : 0);
bytesBuilder.addByte(stmt.isTopLevel ? 1 : 0);
bytesBuilder.addByte(stmt.lateFinalize ? 1 : 0);
bytesBuilder.addByte(stmt.lateInitialize ? 1 : 0);
if (stmt.declType != null) {
bytesBuilder.addByte(1); // bool: has type decl
final typeDecl = compileAST(stmt.declType!);
bytesBuilder.add(typeDecl);
} else {
bytesBuilder.addByte(0); // bool: has type decl
}
if (stmt.initializer != null) {
bytesBuilder.addByte(1); // bool: has initializer
final initializer = compileAST(stmt.initializer!, endOfExec: true);
if (stmt.lateInitialize) {
bytesBuilder.add(_uint16(stmt.initializer!.line));
bytesBuilder.add(_uint16(stmt.initializer!.column));
bytesBuilder.add(_uint16(initializer.length));
}
bytesBuilder.add(initializer);
} else {
bytesBuilder.addByte(0); // bool: has initializer
}
}
if (stmt.hasEndOfStmtMark) {
bytesBuilder.addByte(HTOpCode.endOfStmt);
}
return bytesBuilder.toBytes();
}