visitIf method

  1. @override
Uint8List visitIf(
  1. IfStmt stmt
)
override

Implementation

@override
Uint8List visitIf(IfStmt stmt) {
  final bytesBuilder = BytesBuilder();
  bytesBuilder.add(_lineInfo(stmt.line, stmt.column));
  final condition = compileAST(stmt.condition);
  // The else branch of if stmt is optional, so just check stmt.value is not safe,
  bytesBuilder.add(condition);
  bytesBuilder.addByte(HTOpCode.ifStmt);
  final thenBranch = compileAST(stmt.thenBranch);
  Uint8List? elseBranch;
  if (stmt.elseBranch != null) {
    elseBranch = compileAST(stmt.elseBranch!);
  }
  final thenBranchLength = thenBranch.length + 3;
  final elseBranchLength = elseBranch?.length ?? 0;
  bytesBuilder.add(_uint16(thenBranchLength));
  bytesBuilder.add(thenBranch);
  bytesBuilder.addByte(HTOpCode.skip); // 执行完 then 之后,直接跳过 else block
  bytesBuilder.add(_int16(elseBranchLength));
  if (elseBranch != null) {
    bytesBuilder.add(elseBranch);
  }
  return bytesBuilder.toBytes();
}