From 7a03bc121b65eaf1b544087168c1407eca7d7029 Mon Sep 17 00:00:00 2001 From: Jane Petrovna Date: Fri, 4 Jun 2021 21:36:40 -0400 Subject: [PATCH] restructure parsing --- lights.js | 86 +++++++++++++++++++++++++++++++++++-------------------- parse.js | 37 ++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 33 deletions(-) diff --git a/lights.js b/lights.js index 50bf9a2..9b9ebf6 100644 --- a/lights.js +++ b/lights.js @@ -37,24 +37,60 @@ export function set_pattern(pat) { log.debug("pattern set"); } -export const functions = { - random: (index, args, prev) => { return Math.floor(Math.random() * 256) }, - constant: (index, args, prev) => { return args; }, - modulo: (index, args, prev) => { return prev % args; }, - move: (index, args, prev) => { targets[args] = prev; return prev; }, - swap: (index, args, prev) => { let temp = targets[args]; targets[args] = prev; return temp; }, - add: (index, args, prev) => { return prev + args; }, - subtract: (index, args, prev) => { return prev - args; } +export class Function { + func; + options; + + constructor(func, options) { + this.func = func; + this.options = options; + } } -export const reqs = { - random: false, - constant: true, - modulo: true, - move: true, - swap: true, - add: true, - subtract: true +export const functions = { + random: new Function( + (index, arg1, arg2) => { return Math.floor(Math.random() * 256) }, + { + requires_arg1: true, + requires_arg2: false + } + ), + constant: new Function( + (index, arg1, arg2) => { return args; }, + { + requires_arg1: true, + requires_arg2: true + }), + modulo: new Function( + (index, arg1, arg2) => { return arg1 % args; }, + { + requires_arg1: true, + requires_arg2: true + }), + move: new Function( + (index, arg1, arg2) => { targets[args] = arg1; return arg1; }, + { + requires_arg1: true, + requires_arg2: true + }), + swap: new Function( + (index, arg1, arg2) => { let temp = targets[args]; targets[args] = arg1; return temp; }, + { + requires_arg1: true, + requires_arg2: true + }), + add: new Function( + (index, arg1, arg2) => { return arg1 + args; }, + { + requires_arg1: true, + requires_arg2: true + }), + subtract: new Function( + (index, arg1, arg2) => { return arg1 - args; }, + { + requires_arg1: true, + requires_arg2: true + }) } function tick_pattern() { @@ -64,23 +100,11 @@ function tick_pattern() { if (Object.keys(pattern).length > 0) { for (let i = 0; i < cfg.leds; i++) { for (let command of pattern) { - if (command.valid) { - let instruction = command.instruction; - let name = instruction.name.toLowerCase(); - if (functions[name]) { - for (let target of instruction.args) { - let channel = target.channel; - if (!targets[channel]) { - targets[channel] = 0; - } - log.debug("targets " + name + " " + channel + " " + target.arg); - targets[channel] = - functions[name](i, target.arg, targets[channel]); - } - } + let name = command.command; + if (functions[name]) { + targets[command.arg1] = functions[name].func(i, targets[command.arg1], target.arg); } } - log.debug(`next: ${targets["r"]}, ${targets["g"]}, ${targets["b"]}`); next_pattern[i] = rgb_to_int(targets["r"] || 0, targets["g"] || 0, targets["b"] || 0); targets["r"] = 0; targets["g"] = 0; targets["b"] = 0; diff --git a/parse.js b/parse.js index 0dd81d1..3ad298c 100644 --- a/parse.js +++ b/parse.js @@ -1,14 +1,47 @@ import * as fs from 'fs'; import Logger, { levels } from './logger.js'; +import { functions } from './lights.js'; const cfg = JSON.parse(fs.readFileSync('./config.json')); const log = new Logger("server", cfg.log_level ? levels[cfg.log_level] : levels.INFO); export default function parse(data) { - log.info(data) + let parsed = [] + let errors = [] + log.info(data); + // errors.push("not yet implemented"); + data = JSON.parse(data); + let line = data.split("\n"); + for (let lineNumber in line) { + let currentLine = line[lineNumber]; + let split = currentLine.split(" "); + let command = split[0]; + let arg1 = split[1]; + let arg2 = split[2]; + + let match = functions[command]; + if (match != undefined) { + if ( + !(match.options["requires_arg1"] && arg1 != undefined) && + !(match.options["requires_arg2"] && arg2 != undefined) + ) { + parsed.push( + { + command: command, + arg1: arg1 || undefined, + arg2: arg2 || undefined + } + ); + } + else { + errors.push(`error parsing line ${lineNumber}, invalid number of args`); + } + } + } return { + parsed: parsed data: data, - errors: ["not yet implemented"] + errors: errors } } \ No newline at end of file