<commit_before>// for use with the PEG grammar (compiled using  http://canopy.jcoglan.com/references.html)

'use strict';

var AST = {};
module.exports = AST;

var grammar = require('./grammar');

var make_bin_op = function(name, ev) {
    AST[name] = function(left, right) {
        this.left = left; this.right = right;
    };

    AST[name].prototype.Eval = ev;
};


make_bin_op('AddOp', function(s) { return this.left.Eval() + this.right.Eval(); });
make_bin_op('SubOp', function(s) { return this.left.Eval() - this.right.Eval(); });
make_bin_op('DivideOp', function(s) { return this.left.Eval() / this.right.Eval(); });
make_bin_op('MultOp', function(s) { return this.left.Eval() * this.right.Eval(); });

AST.Literal = function(val) { this.val = val; };
AST.Literal.prototype.Eval = function(s) { return this.val; };

AST.UnaryMinus = function(val) { this.val = val; };
AST.UnaryMinus.prototype.Eval = function(s) { return -this.val; };

AST.Ref = function(name) { this.name = name; };
AST.Ref.prototype.Eval = function(scope) { return scope.get(this.name); }


AST.fromSource = function(source) {
    grammar.Parser.NumberLiteral = {
        isa: 'NumberLiteral',
        transform: function() { return new AST.Literal(Number(this.textValue));},
    };
    grammar.Parser.StringLiteral = {
        isa: 'StringLiteral',
        transform: function() { return new AST.Literal(this.textValue); },
    };
    grammar.Parser.SumNode = {
        isa: 'SumNode',
        transform: function() {
            return new AST.AddOp(this.ls.transform(), this.rs.transform());
        },
    };
    grammar.Parser.SubNode = {
        isa: 'SumNode',
        transform: function() {
            return new AST.SubOp(this.ls.transform(), this.rs.transform());
        },
    };
    grammar.Parser.ProductNode = {
        isa: 'ProductNode',
        transform: function() {
            return new AST.MultOp(this.ls.transform(), this.rs.transform());
        },
    };
    grammar.Parser.ValueNode = {
        isa: 'ValueNode',
        transform: function() { return this.elements[0].transform(); },
    }

    return grammar.parse(source);
};<commit_msg>simplify ast building<commit_after>// for use with the PEG grammar (compiled using  http://canopy.jcoglan.com/references.html)

'use strict';

var AST = {};
module.exports = AST;

var grammar = require('./grammar');

var make_bin_op = function(name, ev) {
    AST[name] = function(left, right) {
        this.left = left; this.right = right;
    };

    AST[name].prototype.Eval = ev;
};


make_bin_op('AddOp', function(s) { return this.left.Eval() + this.right.Eval(); });
make_bin_op('SubOp', function(s) { return this.left.Eval() - this.right.Eval(); });
make_bin_op('DivideOp', function(s) { return this.left.Eval() / this.right.Eval(); });
make_bin_op('MultOp', function(s) { return this.left.Eval() * this.right.Eval(); });

AST.Literal = function(val) { this.val = val; };
AST.Literal.prototype.Eval = function(s) { return this.val; };

AST.UnaryMinus = function(val) { this.val = val; };
AST.UnaryMinus.prototype.Eval = function(s) { return -this.val; };

AST.Ref = function(name) { this.name = name; };
AST.Ref.prototype.Eval = function(scope) { return scope.get(this.name); }


AST.fromSource = function(source) {
    grammar.Parser.NumberLiteral = {
        isa: 'NumberLiteral',
        transform: function() { return new AST.Literal(Number(this.textValue));},
    };
    grammar.Parser.StringLiteral = {
        isa: 'StringLiteral',
        transform: function() { return new AST.Literal(this.textValue); },
    };
    function make_bin_op_parser(name, ast) {
        grammar.Parser[name] = {
            isa: name,
            transform: function() {
                return new ast(this.ls.transform(), this.rs.transform());
            }
        }
    };

    make_bin_op_parser('SumNode', AST.AddOp);
    make_bin_op_parser('SubNode', AST.SubOp);
    make_bin_op_parser('ProductNode', AST.MultOp);
    make_bin_op_parser('QuotientNode', AST.DivideOp);

    grammar.Parser.ValueNode = {
        isa: 'ValueNode',
        transform: function() { return this.elements[0].transform(); },
    };

    return grammar.parse(source);
};
{'fraction_non_alphanumeric': 0.1700858778625954, 'fraction_numerical': 0.00047709923664122136, 'mean_word_length': 3.1432806324110674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1123455', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1337, 'n_words': 255}