3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 13:59:47 +01:00

compiler_vm: simplify vm-client; move -lang option to vm-exec

This commit is contained in:
Pragmatic Software 2022-02-08 09:55:00 -08:00
parent 85b9aaddf8
commit 31aa1d5869
2 changed files with 26 additions and 36 deletions

View File

@ -2,12 +2,11 @@
# File: vm-client # File: vm-client
# #
# Purpose: Interfaces with the PBot virtual machine server hosted at # Purpose: Interfaces with the PBot virtual machine server hosted by
# PeerAddr/PeerPort defined below. This allows us to host instances of # `vm-server` at PeerAddr/PeerPort defined below. This allows us to
# virtual machines on remote servers. # host instances of virtual machines on remote servers.
# #
# This script is intended to be installed to PBot's applets directory # This script is intended to be attached to a PBot command such as `cc`.
# and attached to a PBot command such as `cc`.
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com> # SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
@ -18,7 +17,6 @@ use warnings;
use strict; use strict;
use IO::Socket; use IO::Socket;
use JSON::XS;
use constant { use constant {
SERVER_PORT => $ENV{PBOTVM_SERVER} // 9000, SERVER_PORT => $ENV{PBOTVM_SERVER} // 9000,
@ -31,22 +29,10 @@ my $sock = IO::Socket::INET->new(
); );
if (not defined $sock) { if (not defined $sock) {
print "Fatal error compiling: $!; try again later\n"; print "Fatal error: $!; try again later\n";
die $!; die $!;
} }
my $json = join ' ', @ARGV; print $sock "@ARGV\n";
my $h = decode_json $json; while (my $line = <$sock>) { print $line; }
my $lang = $h->{lang} // "c11";
if ($h->{code} =~ s/-lang=([^ ]+)//) { $lang = lc $1; }
$h->{lang} = $lang;
$json = encode_json $h;
print $sock "$json\n";
while (my $line = <$sock>) { print "$line"; }
close $sock; close $sock;

View File

@ -25,29 +25,33 @@ use constant {
}; };
my $json = join ' ', @ARGV; my $json = join ' ', @ARGV;
my $data = eval { decode_json $json };
my $args = eval { decode_json $json };
if ($@) { if ($@) {
# wasn't JSON; make structure manually # wasn't JSON; make structure manually
if ($json =~ s/^-lang=([^ ]+)//) { if ($json =~ s/^-lang=([^ ]+)\s+//) {
$args = { lang => $1, code => $json }; $data = { lang => $1, code => $json };
} else { } else {
$args = { code => $json }; $data = { code => $json };
} }
} }
if (not exists $args->{code}) { if (not exists $data->{code}) {
die "Usage: $0 <code>\n"; die "Usage: $0 <code>\n";
} }
# set any missing fields to default values # set any missing fields to default values
$args->{nick} //= 'vm'; $data->{nick} //= 'vm';
$args->{channel} //= 'vm'; $data->{channel} //= 'vm';
$args->{lang} //= 'c11'; $data->{lang} //= 'c11';
$args->{'vm-port'} //= SERIAL_PORT; $data->{'vm-port'} //= SERIAL_PORT;
my $language = lc $args->{lang}; # parse -lang option
if ($data->{code} =~ s/^-lang=([^ ]+)\s+//) {
$data->{lang} = $1;
}
my $language = lc $data->{lang};
eval { eval {
require "Languages/$language.pm"; require "Languages/$language.pm";
@ -80,16 +84,16 @@ eval {
} }
}; };
if (not length $args->{code}) { if (not length $data->{code}) {
if (exists $args->{usage}) { if (exists $data->{usage}) {
print "$args->{usage}\n"; print "$data->{usage}\n";
} else { } else {
print "Usage: cc [-lang=<language>] [-info] [-paste] [-args \"command-line arguments\"] [compiler/language options] <code> [-stdin <stdin input>]\n"; print "Usage: cc [-lang=<language>] [-info] [-paste] [-args \"command-line arguments\"] [compiler/language options] <code> [-stdin <stdin input>]\n";
} }
exit; exit;
} }
my $lang = "Languages::$language"->new(%{$args}); my $lang = "Languages::$language"->new(%$data);
$lang->{local} = $ENV{CC_LOCAL}; $lang->{local} = $ENV{CC_LOCAL};