diff --git a/lights/light_parser.js b/lights/light_parser.js index f0e94bd..543a333 100644 --- a/lights/light_parser.js +++ b/lights/light_parser.js @@ -1,7 +1,7 @@ import Logger, { levels } from '../logger.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 { constructor(channel) { @@ -16,6 +16,8 @@ const available_targets = { g: new Target('g'), b: new Target('b'), stack: new Target('stack'), + tick: new Target('tick'), + index: new Target('index'), }; class Instruction { @@ -41,8 +43,11 @@ class Instruction { export const instructions = [ { n: 'CONSTANT', a: true }, { n: 'ADD', a: true }, - { n: 'SUB', a: true }, - { n: 'RAND', a: false }, + { n: 'SUBTRACT', a: true }, + { n: 'MULTIPLY', a: true }, + { n: 'DIVIDE', a: true }, + { n: 'MODULO', a: true }, + { n: 'RANDOM', a: false }, ]; export function initialize(ctx) { @@ -57,7 +62,9 @@ function parseSingleInstruction(str = '') { }; let split = str.split(' '); let cmd = split[0]; + log.debug(cmd); let match = instructions.filter((i) => i.n == cmd.toUpperCase()); + log.debug(match); split = split.slice(1); if (match.length) { let inst_targets = []; @@ -102,12 +109,12 @@ function parseSingleInstruction(str = '') { item.errors = 'Not enough arguments.'; 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) { item.errors = 'No valid args.'; } inst_args.forEach((arg) => { - if (!arg) { + if (arg === NaN) { item.errors = 'An argument is null or undefined.'; } }); @@ -127,7 +134,7 @@ function parseSingleInstruction(str = '') { export default function parse(str) { let parsed = []; - let split = str.split('\n'); + let split = str.split('\n').filter((s) => s.length > 0); split.forEach((item) => { let parsedItem = parseSingleInstruction(item); parsed.push(parsedItem); diff --git a/lights/request.js b/lights/request.js index 29132e2..dd47d90 100644 --- a/lights/request.js +++ b/lights/request.js @@ -7,16 +7,22 @@ function getBinarySize(string) { 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) { let string_data = JSON.stringify(data); let size = getBinarySize(string_data); let client = new net.Socket(); client.connect(port, hostname, () => { - let res = client.write(size); + let res = client.write(padLeft(4, `${size}`)); if (res) { client.write(string_data); } + client.destroy(); }); }