Skip to content
This repository was archived by the owner on Feb 3, 2021. It is now read-only.

Commit c00a30c

Browse files
committed
Switch HLL::Compiler, NQP::Compiler and Regex::P6Regex::Compiler to use class_6m. In the process, create .pm files for a couple of them (we keep the PIR around too, to combine the various produced outputs). Needs re-configure.
1 parent 1146ba7 commit c00a30c

File tree

7 files changed

+197
-174
lines changed

7 files changed

+197
-174
lines changed

build/Makefile.in

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ P6REGEX_SOURCES = \
7171
src/Regex/P6Regex.pir \
7272
src/Regex/P6Regex/Grammar.pm \
7373
src/Regex/P6Regex/Actions.pm \
74+
src/Regex/P6Regex/Compiler.pm \
7475

7576
HLLGRAMMAR_SOURCES = \
7677
src/HLL/Grammar.pm \
@@ -80,6 +81,7 @@ HLLGRAMMAR_SOURCES = \
8081
NQP_SOURCES = \
8182
src/NQP/Grammar.pm \
8283
src/NQP/Actions.pm \
84+
src/NQP/Compiler.pm \
8385
src/NQP/Compiler.pir \
8486

8587
SETTING_SOURCES = \
@@ -113,6 +115,7 @@ HLLCOMPILER_PIR = gen/hllcompiler.pir
113115
P6REGEX_PBC = P6Regex.pbc
114116
P6REGEX_G = gen/p6regex-grammar.pir
115117
P6REGEX_A = gen/p6regex-actions.pir
118+
P6REGEX_C = gen/p6regex-compiler.pir
116119

117120
P6GRAMMAR_PBC = P6Grammar.pbc
118121
P6GRAMMAR_G = gen/p6grammar-grammar.pir
@@ -122,6 +125,7 @@ NQP_PBC = nqp.pbc
122125
NQP_HOW = gen/nqp-how.pir
123126
NQP_G = gen/nqp-grammar.pir
124127
NQP_A = gen/nqp-actions.pir
128+
NQP_C = gen/nqp-compiler.pir
125129
NQP_EXE = nqp$(EXE)
126130

127131
STAGE0_PBCS = $(STAGE0)/$(HLL_PBC) $(STAGE0)/$(P6REGEX_PBC) $(STAGE0)/$(NQP_PBC) $(STAGE0)/$(REGEX_PBC)
@@ -247,6 +251,9 @@ $(STAGE1)/$(P6REGEX_PBC): $(STAGE0_PBCS) $(P6REGEX_SOURCES)
247251
$(PARROT) --library=$(STAGE0) $(STAGE0)/$(NQP_PBC) \
248252
--target=pir --output=$(STAGE1)/$(P6REGEX_A) \
249253
src/Regex/P6Regex/Actions.pm
254+
$(PARROT) --library=$(STAGE0) $(STAGE0)/$(NQP_PBC) \
255+
--target=pir --output=$(STAGE1)/$(P6REGEX_C) \
256+
src/Regex/P6Regex/Compiler.pm
250257
$(PARROT) --include=$(STAGE1) -o $(STAGE1)/$(P6REGEX_PBC) \
251258
src/Regex/P6Regex.pir
252259

@@ -264,6 +271,9 @@ $(STAGE1)/$(NQP_PBC): $(STAGE0_PBCS) $(NQP_SOURCES) $(HOW_COMBINED)
264271
$(PARROT) --library=$(STAGE0) $(STAGE0)/$(NQP_PBC) \
265272
--target=pir --output=$(STAGE1)/$(NQP_A) \
266273
src/NQP/Actions.pm
274+
$(PARROT) --library=$(STAGE0) $(STAGE0)/$(NQP_PBC) \
275+
--target=pir --output=$(STAGE1)/$(NQP_C) \
276+
src/NQP/Compiler.pm
267277
$(PARROT) --include=$(STAGE1) -o $(STAGE1)/$(NQP_PBC) \
268278
src/NQP/Compiler.pir
269279

@@ -291,6 +301,9 @@ $(STAGE2)/$(P6REGEX_PBC): $(STAGE1_PBCS) $(P6REGEX_SOURCES)
291301
$(PARROT) --library=$(STAGE1) $(STAGE1)/$(NQP_PBC) \
292302
--target=pir --output=$(STAGE2)/$(P6REGEX_A) \
293303
src/Regex/P6Regex/Actions.pm
304+
$(PARROT) --library=$(STAGE1) $(STAGE1)/$(NQP_PBC) \
305+
--target=pir --output=$(STAGE2)/$(P6REGEX_C) \
306+
src/Regex/P6Regex/Compiler.pm
294307
$(PARROT) --include=$(STAGE2) -o $(STAGE2)/$(P6REGEX_PBC) \
295308
src/Regex/P6Regex.pir
296309

@@ -305,6 +318,9 @@ $(STAGE2)/$(NQP_PBC): $(STAGE1_PBCS) $(NQP_SOURCES) $(HOW_COMBINED)
305318
$(PARROT) --library=$(STAGE1) $(STAGE1)/$(NQP_PBC) \
306319
--target=pir --output=$(STAGE2)/$(NQP_A) \
307320
src/NQP/Actions.pm
321+
$(PARROT) --library=$(STAGE1) $(STAGE1)/$(NQP_PBC) \
322+
--target=pir --output=$(STAGE2)/$(NQP_C) \
323+
src/NQP/Compiler.pm
308324
$(PARROT) --include=$(STAGE2) -o $(STAGE2)/$(NQP_PBC) \
309325
src/NQP/Compiler.pir
310326

src/HLL/Compiler.pm

Lines changed: 170 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ INIT {
66
# as various additional things that initially appeared in the nqp-rx HLL::Compiler.
77
# Conversion of it all the NQP is a work in progress; for now, many methods are
88
# simply NQP wrappers around inline PIR.
9-
class HLL::Compiler {
9+
class_6m HLL::Compiler {
1010
has @!stages;
1111
has $!parsegrammar;
1212
has $!parseactions;
@@ -19,6 +19,13 @@ class HLL::Compiler {
1919
has $!compiler_progname;
2020
has $!language;
2121

22+
# XXX HACK!!! Need a Mu. :-)
23+
method new() {
24+
my $obj := pir::repr_instance_of__PP(self);
25+
$obj.BUILD();
26+
$obj
27+
}
28+
2229
method BUILD() {
2330
# Default stages.
2431
@!stages := pir::split(' ', 'parse past post pir evalpmc');
@@ -266,6 +273,13 @@ class HLL::Compiler {
266273
}
267274
$!compiler_progname;
268275
}
276+
277+
method commandline_options(@value?) {
278+
if +@value {
279+
@!cmdoptions := @value;
280+
}
281+
@!cmdoptions;
282+
}
269283

270284
method command_line(@args, *%adverbs) {
271285
Q:PIR {
@@ -530,6 +544,55 @@ class HLL::Compiler {
530544
};
531545
}
532546

547+
method parse($source, *%adverbs) {
548+
Q:PIR {
549+
.local pmc source, options, self
550+
source = find_lex '$source'
551+
options = find_lex '%adverbs'
552+
self = find_lex 'self'
553+
554+
.local string tcode
555+
tcode = options['transcode']
556+
unless tcode goto transcode_done
557+
.local pmc tcode_it
558+
$P0 = split ' ', tcode
559+
tcode_it = iter $P0
560+
tcode_loop:
561+
unless tcode_it goto transcode_done
562+
tcode = shift tcode_it
563+
push_eh tcode_fail
564+
$I0 = find_encoding tcode
565+
$S0 = source
566+
$S0 = trans_encoding $S0, $I0
567+
assign source, $S0
568+
pop_eh
569+
goto transcode_done
570+
tcode_fail:
571+
pop_eh
572+
goto tcode_loop
573+
transcode_done:
574+
575+
.local pmc parsegrammar, parseactions, match
576+
parsegrammar = self.'parsegrammar'()
577+
578+
null parseactions
579+
$S0 = options['target']
580+
if $S0 == 'parse' goto have_parseactions
581+
parseactions = self.'parseactions'()
582+
have_parseactions:
583+
584+
.local int rxtrace
585+
rxtrace = options['parsetrace']
586+
match = parsegrammar.'parse'(source, 'p'=>0, 'actions'=>parseactions, 'rxtrace'=>rxtrace)
587+
unless match goto err_parsefail
588+
.return (match)
589+
590+
err_parsefail:
591+
self.'panic'('Unable to parse source')
592+
.return (match)
593+
};
594+
}
595+
533596
method past($source, *%adverbs) {
534597
Q:PIR {
535598
.local pmc source, adverbs, self
@@ -673,11 +736,112 @@ class HLL::Compiler {
673736
done:
674737
};
675738
}
739+
740+
method parse_name($name) {
741+
Q:PIR {
742+
.local string name
743+
$P0 = find_lex '$name'
744+
name = $P0
745+
746+
# split name on ::
747+
.local pmc ns
748+
ns = split '::', name
749+
750+
# move any leading sigil to the last item
751+
.local string sigil
752+
$S0 = ns[0]
753+
sigil = substr $S0, 0, 1
754+
$I0 = index '$@%&', sigil
755+
if $I0 < 0 goto sigil_done
756+
$S0 = replace $S0, 0, 1, ''
757+
ns[0] = $S0
758+
$S0 = ns[-1]
759+
$S0 = concat sigil, $S0
760+
ns[-1] = $S0
761+
sigil_done:
762+
763+
# remove any empty items from the list
764+
.local pmc ns_it
765+
ns_it = iter ns
766+
ns = new ['ResizablePMCArray']
767+
ns_loop:
768+
unless ns_it goto ns_done
769+
$S0 = shift ns_it
770+
unless $S0 > '' goto ns_loop
771+
push ns, $S0
772+
goto ns_loop
773+
ns_done:
774+
775+
# return the result
776+
.return (ns)
777+
};
778+
}
779+
780+
method lineof($target, $pos, :$cache) {
781+
Q:PIR {
782+
.local pmc target, linepos
783+
.local int pos, cache
784+
target = find_lex '$target'
785+
$P0 = find_lex '$pos'
786+
pos = $P0
787+
$P0 = find_lex '$cache'
788+
cache = $P0
789+
790+
.include 'cclass.pasm'
791+
792+
# If we've previously cached C<linepos> for target, we use it.
793+
unless cache goto linepos_build
794+
linepos = getprop '!linepos', target
795+
unless null linepos goto linepos_done
796+
797+
# calculate a new linepos array.
798+
linepos_build:
799+
linepos = new ['ResizableIntegerArray']
800+
unless cache goto linepos_build_1
801+
setprop target, '!linepos', linepos
802+
linepos_build_1:
803+
.local string s
804+
.local int jpos, eos
805+
s = target
806+
eos = length s
807+
jpos = 0
808+
# Search for all of the newline markers in C<target>. When we
809+
# find one, mark the ending offset of the line in C<linepos>.
810+
linepos_loop:
811+
jpos = find_cclass .CCLASS_NEWLINE, s, jpos, eos
812+
unless jpos < eos goto linepos_done
813+
$I0 = ord s, jpos
814+
inc jpos
815+
push linepos, jpos
816+
# Treat \r\n as a single logical newline.
817+
if $I0 != 13 goto linepos_loop
818+
$I0 = ord s, jpos
819+
if $I0 != 10 goto linepos_loop
820+
inc jpos
821+
goto linepos_loop
822+
linepos_done:
823+
824+
# We have C<linepos>, so now we search the array for the largest
825+
# element that is not greater than C<pos>. The index of that
826+
# element is the line number to be returned.
827+
# (Potential optimization: use a binary search.)
828+
.local int line, count
829+
count = elements linepos
830+
line = 0
831+
line_loop:
832+
if line >= count goto line_done
833+
$I0 = linepos[line]
834+
if $I0 > pos goto line_done
835+
inc line
836+
goto line_loop
837+
line_done:
838+
.return (line)
839+
};
840+
}
676841
}
677842

678843
# Set up compiler for "Parrot" language.
679-
INIT {
680-
my $pl := HLL::Compiler.new();
681-
$pl.BUILD();
682-
$pl.language('parrot');
683-
}
844+
#INIT {
845+
# my $pl := HLL::Compiler.new();
846+
# $pl.language('parrot');
847+
#}

src/NQP/Compiler.pir

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,21 @@ NQP::Compiler - NQP compiler
2222

2323
.include 'gen/nqp-grammar.pir'
2424
.include 'gen/nqp-actions.pir'
25+
.include 'gen/nqp-compiler.pir'
2526
.include 'src/cheats/nqp-builtins.pir'
2627

2728
.namespace ['NQP';'Compiler']
2829

2930
.sub '' :anon :load :init
30-
.local pmc p6meta, nqpproto, nqpcomp
31-
p6meta = get_hll_global 'P6metaclass'
32-
nqpproto = p6meta.'new_class'('NQP::Compiler', 'parent'=>'HLL::Compiler')
31+
.local pmc nqpproto, nqpcomp
32+
nqpproto = get_hll_global ['NQP'], 'Compiler'
3333
nqpcomp = nqpproto.'new'()
34-
nqpcomp.'BUILD'() # XXX
3534
nqpcomp.'language'('NQP-rx')
3635
$P0 = get_hll_global ['NQP'], 'Grammar'
3736
nqpcomp.'parsegrammar'($P0)
3837
$P0 = get_hll_global ['NQP'], 'Actions'
3938
nqpcomp.'parseactions'($P0)
40-
$P0 = getattribute nqpcomp, '@!cmdoptions'
39+
$P0 = nqpcomp.'commandline_options'()
4140
push $P0, 'parsetrace'
4241
.end
4342

src/NQP/Compiler.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class_6m NQP::Compiler is HLL::Compiler {
2+
}

src/Regex/P6Regex.pir

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ Regex::P6Regex - Parser/compiler for Perl 6 regexes
1515

1616
.include 'gen/p6regex-grammar.pir'
1717
.include 'gen/p6regex-actions.pir'
18+
.include 'gen/p6regex-compiler.pir'
1819

1920
.namespace ['Regex';'P6Regex';'Compiler']
2021

2122
.sub '' :anon :load :init
22-
.local pmc p6meta, p6regexproto, p6regex
23-
p6meta = get_hll_global 'P6metaclass'
24-
p6regexproto = p6meta.'new_class'('Regex::P6Regex::Compiler', 'parent'=>'HLL::Compiler')
23+
.local pmc p6regexproto, p6regex
24+
p6regexproto = get_hll_global ['Regex'; 'P6Regex'], 'Compiler'
2525
p6regex = p6regexproto.'new'()
26-
p6regex.'BUILD'()
2726
p6regex.'language'('Regex::P6Regex')
2827
$P0 = get_hll_global ['Regex';'P6Regex'], 'Grammar'
2928
p6regex.'parsegrammar'($P0)

src/Regex/P6Regex/Compiler.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class_6m Regex::P6Regex::Compiler is HLL::Compiler {
2+
}

0 commit comments

Comments
 (0)