<commit_before>.HLL '_Tcl', ''
.namespace []

=head2 _Tcl::toList

Given a PMC, get a list from it. If the PMC is a TclList,
this is as simple as returning the list.

=cut

.sub toList :multi(TclList)
  .param pmc list
  .return(list)
.end

.sub toList :multi(_)
  .param pmc value

  $P0 = new 'TclString'
  $S0 = value

  push_eh convert_to_tcl_error
    $P0 = $P0.'get_list'($S0)
  pop_eh

  copy value, $P0

  .return(value)

  # The PMC method only throws a regular exception, we need to tcl-ify it.
  convert_to_tcl_error:
    get_results '0,0', $P0, $S0
    tcl_error $S0

.end

=head2 _Tcl::toDict

Given a PMC, get a TclDict from it, converting as needed.

=cut

.sub toDict :multi(TclDict)
  .param pmc dict
  .return(dict)
.end

.sub toDict :multi(TclList)
  .param pmc list

  $P0 = __listToDict(list)
  copy list, $P0

  .return(list)
.end

.sub toDict :multi(_)
  .param pmc value

  $P0 = __stringToDict(value)
  copy value, $P0

  .return(value)
.end


=head2 _Tcl::toNumber

Given a PMC, get a number from it.

=cut

.sub toNumber :multi(TclInt)
  .param pmc n
  .return(n)
.end

.sub toNumber :multi(TclFloat)
  .param pmc n
  .return(n)
.end

.sub toNumber :multi(_)
  .param pmc number

  .local string str
  .local int    len
  str = number
  len = length str
  .include 'cclass.pasm'

  .local pmc parse
  .local pmc match

  parse = get_root_global ['parrot'; 'TclExpr::Grammar'], 'number'
  $I0 = find_not_cclass .CCLASS_WHITESPACE, str, 0, len
  match = parse(str, 'pos'=>$I0, 'grammar'=>'TclExpr::Grammar')

  $I0 = match.'to'()
  $I1 = len - $I0
  $I0 = find_not_cclass .CCLASS_WHITESPACE, str, $I0, $I1
  if $I0 < len goto NaN

  unless match goto NaN

  .local pmc astgrammar, astbuilder, ast
  astgrammar = new 'TclExpr::PAST::Grammar'
  astbuilder = astgrammar.apply(match)
  ast = astbuilder.get('past')

  .local string className
  .local pmc    value

  className = ast['class']
  value     = ast['value']

  # XXX We probably shouldn't have to invoke the PIR compiler here.

  $P0 = new 'CodeString'
  $P0.emit(".sub 'anon' :anon")
  $P0.emit('$P0 = new "%0"', className)
  $P0.emit("$P0 = %0", value)
  $P0.emit(".return($P0)")
  $P0.emit(".end")

  $P1 = compreg 'PIR'
  $P2 = $P1($P0)
  number = $P2()

  .return(number)

NaN:
  $S1 = 'expected floating-point number but got "'
  $S0 = number
  $S1 .= $S0
  $S1 .= '"'
  tcl_error $S1
.end

=head2 _Tcl::toInteger

Given a PMC, get an integer from it.

=cut

.sub toInteger :multi(TclInt)
  .param pmc n
  .return(n)
.end

.sub toInteger :multi(_)
  .param pmc value
  .param pmc rawhex :named ('rawhex') :optional
  .param int has_rawhex               :opt_flag

  unless has_rawhex goto normal
  $S0 = value
  $S0 =  '0x' . $S0
  value = $S0

normal:
  .local pmc integer

  push_eh not_integer_eh
    integer = toNumber(value)
  pop_eh
  $S0 = typeof integer
  if $S0 != 'TclInt' goto not_integer

  copy value, integer

  .return(value)

not_integer:
  $S1 = value
  $S0 = 'expected integer but got "'
  $S0 .= $S1
  $S0 .= '"'
  tcl_error $S0

not_integer_eh:
  get_results '0,0', $P99, $S99
  $I0 = index $S99, 'expected integer'
  if $I0 == -1 goto not_integer # got some other exception, rewrap it.
  rethrow $P99 # preserves the invalid octal message.
.end

=head2 _Tcl::__index

=cut

.sub __index
  .param string idx
  .param pmc    list

  if idx == 'end' goto end

  $S0 = substr idx, 0, 4
  if $S0 == 'end-' goto before_end
  if $S0 == 'end+' goto after_end

  push_eh bad_index
    $I0 = toInteger(idx)
  pop_eh
  .return($I0)

before_end:
  $S0 = substr idx, 4
  push_eh bad_index
    $I0 = toInteger($S0)
  pop_eh

  $I1 = elements list
  dec $I1
  $I0 = $I1 - $I0
  .return($I0)
<commit_msg>[tcl] add some javadoc. Er, some function docs. Er, -one- function doc.<commit_after>.HLL '_Tcl', ''
.namespace []

=head2 _Tcl::toList

Given a PMC, get a list from it. If the PMC is a TclList,
this is as simple as returning the list.

=cut

.sub toList :multi(TclList)
  .param pmc list
  .return(list)
.end

.sub toList :multi(_)
  .param pmc value

  $P0 = new 'TclString'
  $S0 = value

  push_eh convert_to_tcl_error
    $P0 = $P0.'get_list'($S0)
  pop_eh

  copy value, $P0

  .return(value)

  # The PMC method only throws a regular exception, we need to tcl-ify it.
  convert_to_tcl_error:
    get_results '0,0', $P0, $S0
    tcl_error $S0

.end

=head2 _Tcl::toDict

Given a PMC, get a TclDict from it, converting as needed.

=cut

.sub toDict :multi(TclDict)
  .param pmc dict
  .return(dict)
.end

.sub toDict :multi(TclList)
  .param pmc list

  $P0 = __listToDict(list)
  copy list, $P0

  .return(list)
.end

.sub toDict :multi(_)
  .param pmc value

  $P0 = __stringToDict(value)
  copy value, $P0

  .return(value)
.end


=head2 _Tcl::toNumber

Given a PMC, get a number from it.

=cut

.sub toNumber :multi(TclInt)
  .param pmc n
  .return(n)
.end

.sub toNumber :multi(TclFloat)
  .param pmc n
  .return(n)
.end

.sub toNumber :multi(_)
  .param pmc number

  .local string str
  .local int    len
  str = number
  len = length str
  .include 'cclass.pasm'

  .local pmc parse
  .local pmc match

  parse = get_root_global ['parrot'; 'TclExpr::Grammar'], 'number'
  $I0 = find_not_cclass .CCLASS_WHITESPACE, str, 0, len
  match = parse(str, 'pos'=>$I0, 'grammar'=>'TclExpr::Grammar')

  $I0 = match.'to'()
  $I1 = len - $I0
  $I0 = find_not_cclass .CCLASS_WHITESPACE, str, $I0, $I1
  if $I0 < len goto NaN

  unless match goto NaN

  .local pmc astgrammar, astbuilder, ast
  astgrammar = new 'TclExpr::PAST::Grammar'
  astbuilder = astgrammar.apply(match)
  ast = astbuilder.get('past')

  .local string className
  .local pmc    value

  className = ast['class']
  value     = ast['value']

  # XXX We probably shouldn't have to invoke the PIR compiler here.

  $P0 = new 'CodeString'
  $P0.emit(".sub 'anon' :anon")
  $P0.emit('$P0 = new "%0"', className)
  $P0.emit("$P0 = %0", value)
  $P0.emit(".return($P0)")
  $P0.emit(".end")

  $P1 = compreg 'PIR'
  $P2 = $P1($P0)
  number = $P2()

  .return(number)

NaN:
  $S1 = 'expected floating-point number but got "'
  $S0 = number
  $S1 .= $S0
  $S1 .= '"'
  tcl_error $S1
.end

=head2 _Tcl::toInteger

Given a PMC, get an integer from it.

=cut

.sub toInteger :multi(TclInt)
  .param pmc n
  .return(n)
.end

.sub toInteger :multi(_)
  .param pmc value
  .param pmc rawhex :named ('rawhex') :optional
  .param int has_rawhex               :opt_flag

  unless has_rawhex goto normal
  $S0 = value
  $S0 =  '0x' . $S0
  value = $S0

normal:
  .local pmc integer

  push_eh not_integer_eh
    integer = toNumber(value)
  pop_eh
  $S0 = typeof integer
  if $S0 != 'TclInt' goto not_integer

  copy value, integer

  .return(value)

not_integer:
  $S1 = value
  $S0 = 'expected integer but got "'
  $S0 .= $S1
  $S0 .= '"'
  tcl_error $S0

not_integer_eh:
  get_results '0,0', $P99, $S99
  $I0 = index $S99, 'expected integer'
  if $I0 == -1 goto not_integer # got some other exception, rewrap it.
  rethrow $P99 # preserves the invalid octal message.
.end

=head2 _Tcl::__index

Given a tcl string index and an List pmc, return the corresponding numeric
index.

=cut

.sub __index
  .param string idx
  .param pmc    list

  if idx == 'end' goto end

  $S0 = substr idx, 0, 4
  if $S0 == 'end-' goto before_end
  if $S0 == 'end+' goto after_end

  push_eh bad_index
    $I0 = toInteger(idx)
  pop_eh
  .return($I0)

before_end:
  $S0 = substr idx, 4
  push_eh bad_index
    $I0 = toInteger($S0)
  pop_eh

  $I1 = elements list
  dec $I1
  $I0 = $I1 - $I0
  .return($I0)

{'fraction_non_alphanumeric': 0.1320504313205043, 'fraction_numerical': 0.025215660252156602, 'mean_word_length': 2.985193019566367, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2999899', 'n_tokens_mistral': 3224, 'n_tokens_neox': 3123, 'n_words': 921}