-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytecode.pl
71 lines (51 loc) · 1.23 KB
/
bytecode.pl
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
$f = join("", <>);
$f =~ s/\s+/ /smg;
@strings= ();
$snum= 0;
$f =~ s/("([^"]|\.)*")/($strings[$snum++]=$1, "\"$snum\"")/eg;
$self = "[!\\\"#\$%&\'\(\)\*\+,\-./0123456789:;<=>?@\[\\\]\^_\`abcdefghijklmnopqrstuvwxyz\{\|\}~]";
%words = (
"if", "?(",
"exit", "]",
"unloop", "]",
"drop", "\\",
"parloc", "l#",
"setlocals", "l,",
"local@", "l@",
"local!", "l!",
":=", "`",
"==", "=",
"endif", ")",
# import functions
# TODO: add par count in compilation
"putchar", "14 x",
"printf", "16 x",
);
#$wnum= 128+65; # 'A'...
$wnum= 65; # 'A'...
sub w {
my ($w)= @_;
$w =~ s/^_(\S+)$/$1/;
my $r= $words{$w};
return $r if $r;
return $w if $w =~ /^[^a-zA-Z]*$/;
if ($wnum>=255) {
die "problem- too many wnum!";
}
$r= chr($wnum++);
$words{$w} = $r;
$words{$r} = $w; # yeah... lol
# TODO: local vars
print STDERR "\nDEFINED $r = $w\n";
return $r;
}
$f =~ s/(\S+)/w($1)/smeg;
$f =~ s/^\s*//smg;
$f =~ s/\s*$//smg;
# remove most spaces
while ($f =~ s/([^\d])\s+(.)/$1$2/smg) {};
while ($f =~ s/([^\d])\s+(\d)/$1$2/smg) {};
while ($f =~ s/(\d)\s+([^\d])/$1$2/smg) {};
# restore strings
$f =~ s/"(\d+)"/$strings[$1-1]/smeg;
print "$f\n";