3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-05 03:29:33 +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
#
# Purpose: Interfaces with the PBot virtual machine server hosted at
# PeerAddr/PeerPort defined below. This allows us to host instances of
# virtual machines on remote servers.
# Purpose: Interfaces with the PBot virtual machine server hosted by
# `vm-server` at PeerAddr/PeerPort defined below. This allows us to
# host instances of virtual machines on remote servers.
#
# This script is intended to be installed to PBot's applets directory
# and attached to a PBot command such as `cc`.
# This script is intended to be attached to a PBot command such as `cc`.
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
@ -18,7 +17,6 @@ use warnings;
use strict;
use IO::Socket;
use JSON::XS;
use constant {
SERVER_PORT => $ENV{PBOTVM_SERVER} // 9000,
@ -31,22 +29,10 @@ my $sock = IO::Socket::INET->new(
);
if (not defined $sock) {
print "Fatal error compiling: $!; try again later\n";
print "Fatal error: $!; try again later\n";
die $!;
}
my $json = join ' ', @ARGV;
my $h = decode_json $json;
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"; }
print $sock "@ARGV\n";
while (my $line = <$sock>) { print $line; }
close $sock;

View File

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