-
Notifications
You must be signed in to change notification settings - Fork 0
/
Emit.pm
203 lines (132 loc) · 2.93 KB
/
Emit.pm
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/perl
# --- * --- * ---
# EMIT
# Base class for code emitters
#
# LIBRE SOFTWARE
# Licensed under GNU GPL3
# be a bro and inherit
#
# CONTRIBUTORS
# lyeb,
# --- * --- * ---
# deps
package Emit;
use v5.36.0;
use strict;
use warnings;
use Readonly;
use English qw(-no_match_vars);
use lib $ENV{'ARPATH'}.'/lib/sys/';
use Style;
use Chk;
use Arstd::Array;
use Arstd::Path;
use Arstd::Re;
use Arstd::IO;
use Arstd::PM;
# --- * --- * ---
# info
our $VERSION=v0.00.5;#b
our $AUTHOR='IBN-3DILA';
# --- * --- * ---
# ROM
Readonly our $ON_NO_TITLE => 'SCRATCH';
Readonly our $ON_NO_VERSION => 'v0.00.1b';
Readonly our $ON_NO_AUTHOR => 'ANON';
# --- * --- * ---
# find emitter subclass matching name
# (c-)load it in if found
sub get_class($class,$name) {
my $out=$NULLSTR;
# nocase Emit::$name lookup
my $fname=find_subpkg('Emit',$name);
# ^make classname from filename
if($fname) {
$out=fname_to_pkg($fname);
cload($out);
};
return $out;
};
# --- * --- * ---
# transforms type to peso equivalent
sub typetrim($class,$typeref) {
$$typeref=~ s[^\s*|\s*$][]sg;
# temporal patch ;>
$$typeref=~ s[inline][];
};
sub get_typetab($class) {
no strict 'refs';
my $out=${"$class\::TYPETAB"};
use strict 'refs';
return $out;
};
sub typecon($class,$type) {
my $tab=$class->get_typetab();
$class->typetrim(\$type);
if(exists $tab->{$type}) {
$type=$tab->{$type};
};
return $type;
};
# --- * --- * ---
# dummies, derived class implements if needed
sub boiler_open($class,$fname,%O) {return q{}};
sub boiler_close($class,$fname,%O) {return q{}};
sub xltab($class,%table) {return %table};
sub fnwrap($class,$name,$code,%O) {
return $name.q{ }.$code;
};
sub datasec($class,$name,$type,@items) {
return $type.q{ }.$name.q{ }.(join ',',@items);
};
sub tidy($class,$sref) {
return $$sref;
};
# --- * --- * ---
# puts code in-between two pieces of boiler
sub codewrap($class,$fname,%O) {
# defaults
$O{guards} //= 0;
$O{ldo} //= [];
$O{lib} //= [];
$O{inc} //= [];
$O{def} //= [];
$O{body} //= [$NULLSTR=>[]];
$O{author} //= $ON_NO_AUTHOR;
$O{version} //= $ON_NO_VERSION;
$O{tidy} //= 0;
# run code generation
my $s=$NULLSTR;
$s.=$class->boiler_open($fname,%O);
is_arrayref($O{body})
or throw_genbody();
my @code=array_keys($O{body});
my @args=array_values($O{body});
map {
my $args=shift @args;
$s.=(is_coderef($ARG))
? $ARG->(@$args)
: $ARG
;
} @code;
$s.=$class->boiler_close($fname,%O);
$s=($O{tidy})
? $class->tidy(\$s)
: $s
;
return $s;
};
# --- * --- * ---
# ^errme
sub throw_genbody() {
errcaller();
errout(
q[Body of [ctl]:%s must be an ]
. q[array of [F=>ARGS]],
lvl => $AR_FATAL,
args => ['codewrap'],
);
};
# --- * --- * ---
1; # ret