Skip to content

Instantly share code, notes, and snippets.

@masak
Created February 17, 2010 14:10
Show Gist options
  • Save masak/306640 to your computer and use it in GitHub Desktop.
Save masak/306640 to your computer and use it in GitHub Desktop.
$ cat src/glue/enum.pir
=head1 NAME
src/glue/enum.pir -- internal handling of enums
=head2 Subs
=over 4
=item !create_anon_enum(value_list)
Constructs a EnumMap, based upon the values list.
=cut
.sub '!create_anon_enum'
.param pmc values
# Put the values into list context, so case of a single valued enum works.
values = values.'list'()
# For now, we assume integer type, unless we have a first pair that says
# otherwise.
.local pmc cur_val
cur_val = box 0
# Iterate over values and make mapping.
.local pmc result, values_it, cur_item
result = new ['EnumMap']
values_it = iter values
values_loop:
unless values_it goto values_loop_end
cur_item = shift values_it
$I0 = isa cur_item, 'Perl6Pair'
if $I0 goto pair
nonpair:
$P0 = '&postfix:<++>'(cur_val)
result[cur_item] = $P0
goto values_loop
pair:
cur_val = cur_item.'value'()
$P0 = cur_item.'key'()
result[$P0] = cur_val
cur_val = clone cur_val
'postfix:++'(cur_val)
goto values_loop
values_loop_end:
.return (result)
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir:
$ ./perl6 -e 'enum <a b c>'
Cannot assign to readonly value
current instr.: '&die' pc 16101 (src/builtins/Junction.pir:307)
called from Sub '&infix:<=>' pc 15925 (src/builtins/Junction.pir:207)
called from Sub '&postfix:<++>' pc 338485 (src/gen/core.pir:47341)
called from Sub '!create_anon_enum' pc 742 (src/glue/enum.pir:15)
called from Sub '_block14' pc 29 (EVAL_1:0)
called from Sub '!UNIT_START' pc 1109 (src/glue/run.pir:17)
called from Sub 'perl6;PCT;HLLCompiler;eval' pc -1 ((unknown file):-1)
called from Sub 'perl6;PCT;HLLCompiler;command_line' pc 1489 (compilers/pct/src/PCT/HLLCompiler.pir:794)
called from Sub 'perl6;Perl6;Compiler;main' pc -1 ((unknown file):-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment