restructure parsing

This commit is contained in:
jane 2021-06-04 21:36:40 -04:00
parent e13e57a66b
commit 7a03bc121b
2 changed files with 90 additions and 33 deletions

View file

@ -37,24 +37,60 @@ export function set_pattern(pat) {
log.debug("pattern set"); log.debug("pattern set");
} }
export const functions = { export class Function {
random: (index, args, prev) => { return Math.floor(Math.random() * 256) }, func;
constant: (index, args, prev) => { return args; }, options;
modulo: (index, args, prev) => { return prev % args; },
move: (index, args, prev) => { targets[args] = prev; return prev; }, constructor(func, options) {
swap: (index, args, prev) => { let temp = targets[args]; targets[args] = prev; return temp; }, this.func = func;
add: (index, args, prev) => { return prev + args; }, this.options = options;
subtract: (index, args, prev) => { return prev - args; } }
} }
export const reqs = { export const functions = {
random: false, random: new Function(
constant: true, (index, arg1, arg2) => { return Math.floor(Math.random() * 256) },
modulo: true, {
move: true, requires_arg1: true,
swap: true, requires_arg2: false
add: true, }
subtract: true ),
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() { function tick_pattern() {
@ -64,23 +100,11 @@ function tick_pattern() {
if (Object.keys(pattern).length > 0) { if (Object.keys(pattern).length > 0) {
for (let i = 0; i < cfg.leds; i++) { for (let i = 0; i < cfg.leds; i++) {
for (let command of pattern) { for (let command of pattern) {
if (command.valid) { let name = command.command;
let instruction = command.instruction;
let name = instruction.name.toLowerCase();
if (functions[name]) { if (functions[name]) {
for (let target of instruction.args) { targets[command.arg1] = functions[name].func(i, targets[command.arg1], target.arg);
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]);
} }
} }
}
}
log.debug(`next: ${targets["r"]}, ${targets["g"]}, ${targets["b"]}`); log.debug(`next: ${targets["r"]}, ${targets["g"]}, ${targets["b"]}`);
next_pattern[i] = rgb_to_int(targets["r"] || 0, targets["g"] || 0, targets["b"] || 0); next_pattern[i] = rgb_to_int(targets["r"] || 0, targets["g"] || 0, targets["b"] || 0);
targets["r"] = 0; targets["g"] = 0; targets["b"] = 0; targets["r"] = 0; targets["g"] = 0; targets["b"] = 0;

View file

@ -1,14 +1,47 @@
import * as fs from 'fs'; import * as fs from 'fs';
import Logger, { levels } from './logger.js'; import Logger, { levels } from './logger.js';
import { functions } from './lights.js';
const cfg = JSON.parse(fs.readFileSync('./config.json')); const cfg = JSON.parse(fs.readFileSync('./config.json'));
const log = new Logger("server", cfg.log_level ? levels[cfg.log_level] : levels.INFO); const log = new Logger("server", cfg.log_level ? levels[cfg.log_level] : levels.INFO);
export default function parse(data) { 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 { return {
parsed: parsed
data: data, data: data,
errors: ["not yet implemented"] errors: errors
} }
} }