add commands

This commit is contained in:
jane 2020-11-09 01:29:38 -05:00
parent 2a6f2d6f9b
commit 5966977a76
2 changed files with 20 additions and 7 deletions

View file

@ -1,7 +1,7 @@
import Logger, { levels } from '../logger.js'; import Logger, { levels } from '../logger.js';
import { filename } from '../utils.js'; import { filename } from '../utils.js';
const log = new Logger(filename(import.meta.url), levels.PANIC); const log = new Logger(filename(import.meta.url), levels.DEBUG);
class Target { class Target {
constructor(channel) { constructor(channel) {
@ -16,6 +16,8 @@ const available_targets = {
g: new Target('g'), g: new Target('g'),
b: new Target('b'), b: new Target('b'),
stack: new Target('stack'), stack: new Target('stack'),
tick: new Target('tick'),
index: new Target('index'),
}; };
class Instruction { class Instruction {
@ -41,8 +43,11 @@ class Instruction {
export const instructions = [ export const instructions = [
{ n: 'CONSTANT', a: true }, { n: 'CONSTANT', a: true },
{ n: 'ADD', a: true }, { n: 'ADD', a: true },
{ n: 'SUB', a: true }, { n: 'SUBTRACT', a: true },
{ n: 'RAND', a: false }, { n: 'MULTIPLY', a: true },
{ n: 'DIVIDE', a: true },
{ n: 'MODULO', a: true },
{ n: 'RANDOM', a: false },
]; ];
export function initialize(ctx) { export function initialize(ctx) {
@ -57,7 +62,9 @@ function parseSingleInstruction(str = '') {
}; };
let split = str.split(' '); let split = str.split(' ');
let cmd = split[0]; let cmd = split[0];
log.debug(cmd);
let match = instructions.filter((i) => i.n == cmd.toUpperCase()); let match = instructions.filter((i) => i.n == cmd.toUpperCase());
log.debug(match);
split = split.slice(1); split = split.slice(1);
if (match.length) { if (match.length) {
let inst_targets = []; let inst_targets = [];
@ -102,12 +109,12 @@ function parseSingleInstruction(str = '') {
item.errors = 'Not enough arguments.'; item.errors = 'Not enough arguments.';
return item; return item;
} }
inst_args = parsed_args[1].split(',').map((s) => (s == 'stack' ? 'stack' : parseInt(s.trim()))); inst_args = parsed_args[1].split(',').map((s) => (available_targets[s] ? s : parseInt(s.trim())));
if (!inst_args.length) { if (!inst_args.length) {
item.errors = 'No valid args.'; item.errors = 'No valid args.';
} }
inst_args.forEach((arg) => { inst_args.forEach((arg) => {
if (!arg) { if (arg === NaN) {
item.errors = 'An argument is null or undefined.'; item.errors = 'An argument is null or undefined.';
} }
}); });
@ -127,7 +134,7 @@ function parseSingleInstruction(str = '') {
export default function parse(str) { export default function parse(str) {
let parsed = []; let parsed = [];
let split = str.split('\n'); let split = str.split('\n').filter((s) => s.length > 0);
split.forEach((item) => { split.forEach((item) => {
let parsedItem = parseSingleInstruction(item); let parsedItem = parseSingleInstruction(item);
parsed.push(parsedItem); parsed.push(parsedItem);

View file

@ -7,16 +7,22 @@ function getBinarySize(string) {
return Buffer.byteLength(string, 'utf8'); return Buffer.byteLength(string, 'utf8');
} }
function padLeft(size = 4, str = '') {
let remaining = size - str.length;
return '0'.repeat(remaining) + str;
}
export default function req(data, callback, errorCallback) { export default function req(data, callback, errorCallback) {
let string_data = JSON.stringify(data); let string_data = JSON.stringify(data);
let size = getBinarySize(string_data); let size = getBinarySize(string_data);
let client = new net.Socket(); let client = new net.Socket();
client.connect(port, hostname, () => { client.connect(port, hostname, () => {
let res = client.write(size); let res = client.write(padLeft(4, `${size}`));
if (res) { if (res) {
client.write(string_data); client.write(string_data);
} }
client.destroy();
}); });
} }