-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbvm.pl
executable file
·74 lines (70 loc) · 1.49 KB
/
bvm.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
71
72
73
74
#!/usr/bin/perl -w
use POSIX qw(ceil floor);
open("il", "$ARGV[0]") or die "can't open file";
my @cmds = <il>;
my @s;
my $sp = 0;
my $counter = 0;
sub getVal($){
$val = shift;
return $val =~ /s\[(.*)\]/ ? $s[$1] : $val;
}
while($sp < @cmds && $counter++ < 10000){
#print "sp:$sp\n";
#print "s:@s\n";
$cmd = $cmds[$sp];
if($cmd =~ /s\[(.*)\]=([-])(.*)/)
{
$s[$1] = -getVal($3);
}
elsif($cmd =~ /s\[(.*)\]=(.*)([-+*\/])(.*)/)
{
$v1 = getVal($2);
$v2 = getVal($4);
if($3 eq "-"){$s[$1] = $v1 - $v2;}
if($3 eq "+"){$s[$1] = $v1 + $v2;}
if($3 eq "*"){$s[$1] = $v1 * $v2;}
if($3 eq "/"){$s[$1] = floor($v1 / $v2);}
}
elsif($cmd =~ /if (.*)(==|!=|[<>])(.*) goto (\d+)/)
{
$v1 = getVal($1);
$v2 = getVal($3);
if($2 eq "==" && $v1 == $v2){$sp = $4; next;}
if($2 eq "!=" && $v1 != $v2){$sp = $4; next;}
if($2 eq "<" && $v1 < $v2){$sp = $4; next;}
if($2 eq ">" && $v1 > $v2){$sp = $4; next;}
}
elsif($cmd =~ /s\[(.*)\]=(.*)/)
{
#print "assign $1 $2\n";
$s[$1] = getVal($2);
}
elsif($cmd =~ /input s\[(\d+)\]/)
{
$s[$1] = <STDIN>;
exit 0 if $s[$1] !~ /\d+/;
}
elsif($cmd =~ /print \"(.*)\"/)
{
print "$1\n";
}
elsif($cmd =~ /printD s\[(.*)\]/)
{
print "$s[$1]\n";
}
elsif($cmd =~ /printB s\[(.*)\]/)
{
printf("%b\n", $s[$1]);
}
elsif($cmd =~ /printH s\[(.*)\]/)
{
printf("0x%X\n", $s[$1]);
}
elsif($cmd =~ /^goto (\d+)/)
{
$sp = $1;
next;
}
$sp++;
}