<commit_before>'use strict';

var _ = require('underscore');
var CodeGenerator = require('./code-generator');
var filters = require('./filters');
var FilterSimplifier = require('./filters/filter-simplifier');
var StaticFilterChecker = filters.StaticFilterChecker;
var DynamicFilterChecker = filters.DynamicFilterChecker;
var FilterJSCompiler = filters.FilterJSCompiler;
var errors = require('../errors');

var UNARY_OPS_TO_FUNCTIONS = {
    '+':   'pos',
    '-':   'neg',
    '~':   'bnot',
    'NOT': 'lnot'
};

var BINARY_OPS_TO_FUNCTIONS = {
    '+':   'add',
    '-':   'sub',
    '*':   'mul',
    '/':   'div',
    '%':   'mod',
    '&':   'band',
    '|':   'bor',
    '^':   'bxor',
    '<<':  'shl',
    '>>':  'shr',
    '>>>': 'shrz',
    '==':  'eql',
    '!=':  'neq',
    '=~':  'match',
    '!~':  'nmatch',
    '<':   'lt',
    '>':   'gt',
    '<=':  'lte',
    '>=':  'gte',
    'in':  'in',
    'AND': 'land',
    'OR':  'lor',
    '??':  'coal'
};

var SYMBOL_TYPE_NAMES = {
    'function': 'function',
    'reducer': 'reducer',
    'sub': 'subgraph',
    'import': 'module'
};

//
// take a dataflow graph representation and generate a javascript program
// that runs against the Juttle javascript runtime
//
class GraphCompiler extends CodeGenerator {
    constructor() {
        super();
        // XXX/sm we can easily get rid of emit and just recursively return
        // the code strings, except we rely on returning the uname for the
        // proc when building the graph contruction code
        this.code = '';
    }
    gen(graph) {
        var code, body, entry;
        this.now = graph.now;
        this.gen_now(graph.now);
        this.gen_functionDefs(graph.functions);
        this.gen_reducerDefs(graph.reducers);
        entry = this.gen_procs(graph.nodes);

        body = this.code;
        //
        // wrap main in a closure so it does not pollute the name space.
        // call main and return its value, which is the flowgraph head object.
        // this should be the only object reference into the entire
        // compiled world and associated runtime.
        //
        code = '(function(juttle) {\n';
        code += 'var views=[];\n';
        code += 'var program = {};\n';
        code += body;
        code += 'return { program: program, now: program.now, views: views,';
        code += 'graph: ' + entry + ' };\n';
        code += '})';
        return code;
    }
    emit(s) {
        this.code += s;
    }
    gen_now(now) {
        this.emit('program.now = ' + 'new juttle.types.JuttleMoment("' + now + '");');
        this.emit('program.channels = 0;');
    }
    gen_functionDefs(fns) {
        var k;
        for (k = 0; k < fns.length; ++k) {<commit_msg>graph-compiler.js - remove unused this.now<commit_after>'use strict';

var _ = require('underscore');
var CodeGenerator = require('./code-generator');
var filters = require('./filters');
var FilterSimplifier = require('./filters/filter-simplifier');
var StaticFilterChecker = filters.StaticFilterChecker;
var DynamicFilterChecker = filters.DynamicFilterChecker;
var FilterJSCompiler = filters.FilterJSCompiler;
var errors = require('../errors');

var UNARY_OPS_TO_FUNCTIONS = {
    '+':   'pos',
    '-':   'neg',
    '~':   'bnot',
    'NOT': 'lnot'
};

var BINARY_OPS_TO_FUNCTIONS = {
    '+':   'add',
    '-':   'sub',
    '*':   'mul',
    '/':   'div',
    '%':   'mod',
    '&':   'band',
    '|':   'bor',
    '^':   'bxor',
    '<<':  'shl',
    '>>':  'shr',
    '>>>': 'shrz',
    '==':  'eql',
    '!=':  'neq',
    '=~':  'match',
    '!~':  'nmatch',
    '<':   'lt',
    '>':   'gt',
    '<=':  'lte',
    '>=':  'gte',
    'in':  'in',
    'AND': 'land',
    'OR':  'lor',
    '??':  'coal'
};

var SYMBOL_TYPE_NAMES = {
    'function': 'function',
    'reducer': 'reducer',
    'sub': 'subgraph',
    'import': 'module'
};

//
// take a dataflow graph representation and generate a javascript program
// that runs against the Juttle javascript runtime
//
class GraphCompiler extends CodeGenerator {
    constructor() {
        super();
        // XXX/sm we can easily get rid of emit and just recursively return
        // the code strings, except we rely on returning the uname for the
        // proc when building the graph contruction code
        this.code = '';
    }
    gen(graph) {
        var code, body, entry;
        this.gen_now(graph.now);
        this.gen_functionDefs(graph.functions);
        this.gen_reducerDefs(graph.reducers);
        entry = this.gen_procs(graph.nodes);

        body = this.code;
        //
        // wrap main in a closure so it does not pollute the name space.
        // call main and return its value, which is the flowgraph head object.
        // this should be the only object reference into the entire
        // compiled world and associated runtime.
        //
        code = '(function(juttle) {\n';
        code += 'var views=[];\n';
        code += 'var program = {};\n';
        code += body;
        code += 'return { program: program, now: program.now, views: views,';
        code += 'graph: ' + entry + ' };\n';
        code += '})';
        return code;
    }
    emit(s) {
        this.code += s;
    }
    gen_now(now) {
        this.emit('program.now = ' + 'new juttle.types.JuttleMoment("' + now + '");');
        this.emit('program.channels = 0;');
    }
    gen_functionDefs(fns) {
        var k;
        for (k = 0; k < fns.length; ++k) {
{'fraction_non_alphanumeric': 0.17954837525243253, 'fraction_numerical': 0.0007343491830365339, 'mean_word_length': 2.242857142857143, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3688298', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1782, 'n_words': 451}