This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
/
hll-compiler.pir
126 lines (106 loc) · 2.95 KB
/
hll-compiler.pir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# we have to overload PCT::HLLCompiler's parse method to support P6Regex grammars
.namespace ['HLL';'Compiler']
.sub 'parse' :method
.param pmc source
.param pmc options :slurpy :named
.local string tcode
tcode = options['transcode']
unless tcode goto transcode_done
.local pmc tcode_it
$P0 = split ' ', tcode
tcode_it = iter $P0
tcode_loop:
unless tcode_it goto transcode_done
tcode = shift tcode_it
push_eh tcode_enc
$I0 = find_charset tcode
$S0 = source
$S0 = trans_charset $S0, $I0
assign source, $S0
pop_eh
goto transcode_done
tcode_enc:
pop_eh
push_eh tcode_fail
$I0 = find_encoding tcode
$S0 = source
$S0 = trans_encoding $S0, $I0
assign source, $S0
pop_eh
goto transcode_done
tcode_fail:
pop_eh
goto tcode_loop
transcode_done:
.local pmc parsegrammar, parseactions, match
parsegrammar = self.'parsegrammar'()
null parseactions
$S0 = options['target']
if $S0 == 'parse' goto have_parseactions
parseactions = self.'parseactions'()
have_parseactions:
.local int rxtrace
rxtrace = options['parsetrace']
match = parsegrammar.'parse'(source, 'p'=>0, 'actions'=>parseactions, 'rxtrace'=>rxtrace)
unless match goto err_parsefail
.return (match)
err_parsefail:
self.'panic'('Unable to parse source')
.return (match)
.end
.sub 'pir' :method
.param pmc source
.param pmc adverbs :slurpy :named
$P0 = compreg 'POST'
$S0 = $P0.'to_pir'(source, adverbs :flat :named)
.return ($S0)
.end
.sub 'parse_name' :method
.param string name
# split name on ::
.local pmc ns
ns = split '::', name
# move any leading sigil to the last item
.local string sigil
$S0 = ns[0]
sigil = substr $S0, 0, 1
$I0 = index '$@%&', sigil
if $I0 < 0 goto sigil_done
$S0 = replace $S0, 0, 1, ''
ns[0] = $S0
$S0 = ns[-1]
$S0 = concat sigil, $S0
ns[-1] = $S0
sigil_done:
# remove any empty items from the list
.local pmc ns_it
ns_it = iter ns
ns = new ['ResizablePMCArray']
ns_loop:
unless ns_it goto ns_done
$S0 = shift ns_it
unless $S0 > '' goto ns_loop
push ns, $S0
goto ns_loop
ns_done:
# return the result
.return (ns)
.end
# This sub serves as a cumulative "outer context" for code
# executed in HLL::Compiler's interactive REPL. It's invoked
# exactly once upon load/init to obtain a context, and its
# default LexPad is replaced with a Hash that we can use to
# cumulatively store outer context information. Both the
# context and hash are then made available via package
# variables.
.namespace []
.sub '&interactive_outer' :lex :init :load
.local pmc ctx, pad
$P0 = getinterp
ctx = $P0['context']
set_global ['HLL';'Compiler'], '$interactive_ctx', ctx
pad = getattribute ctx, 'lex_pad'
$P1 = new ['Hash']
copy pad, $P1
set_global ['HLL';'Compiler'], '%interactive_pad', pad
.end