<commit_before>// # module: runtime
//
// Provides the base runtime.

// -- Dependencies -----------------------------------------------------
var c = require('core.check');
var show = require('core.inspect');
var equal = require('deep-equal');
var { curry } = require('core.lambda');
var { Base } = require('boo');
var { List, Value:{ Applicative, Symbol, Lambda, Tagged } } = require('./data');
var { eval } = require('./eval');


// -- Helpers ----------------------------------------------------------

// ### function: assert
// @private
// @type: Validation[Violation, α] → α :: throws
function assert(val) {
  val.cata({
    Failure: λ(a) -> { throw new Error('Expected ' + show(a)) },
    Success: λ[#]
  })
}

// ### function: raise
// @private
// @type: Error -> Void :: throws
function raise(e) {
  throw e;
}

// ### function: unbox
// @private
// @type: String -> Tagged -> Any
unbox = curry(2, unbox);
function unbox(tag, val) {
  assert(tag(val));
  return val
}

var str = unbox(c.String);
var num = unbox(c.Number);
var bool = unbox(c.Boolean);


// -- Core environment -------------------------------------------------
var Env = module.exports = Base.derive({

  // --- Boolean operations --------------------------------------------
  not:
  Applicative(['value'], function(data) {
    return match data.value {
      false => true,
      []    => true,
      *     => false
    }
  }),

  'boolean?':
  Applicative(['value'], function(data) {
    return data.value === true || data.value === false
  }),

  // --- Numeric operations --------------------------------------------
  '+':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) + num(data.right)
  }),

  '-':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) + num(data.right)
  }),

  '*':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) * num(data.right)
  }),

  '/':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) / num(data.right)
  }),

  paragraph:
  Applicative(['value'], function(data) {
    return data.value
  }),

  text:
  Applicative(['value'], function(data) {
    return data.value
  }),
  
  // --- Comparison operations -----------------------------------------
  '=':
  Applicative(['left', 'right'], function(data) {
    return equal(data.left, data.right)
  }),
  
  '<':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) < num(data.right)
  }),

  '<=':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) <= num(data.right)
  }),

  '>':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) > num(data.right)
  }),

  '>=':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) >= num(data.right)
  }),

  
  // --- Symbol operations ---------------------------------------------
  name:
  Applicative(['value'], function(data) {
    return match data.value {
      Symbol(a) => a,
      a         => raise(new TypeError('Not a symbol: ' + show(a)))
    }
  }),

  
  // -- Vector operations ----------------------------------------------
  first:
  Applicative(['value'], function(data) {
    assert(c.Array(data.value));
    return data.value.length > 0?  data.value[0]
    :      /* otherwise */         []
  }),

  last:
  Applicative(['value'], function(data) {
    assert(c.Array(data.value));
    return data.value.length > 0?  data.value[data.value.length - 1]
    :      /* otherwise */         []
  }),

  nth:
  Applicative(['value', 'index'], function(data) {
    assert(c.Array(data.value));
    var i = num(data.index);
    if (i > data.value.length - 1) {
      throw new RangeError('Index out of bounds: ' + i);
    } else {
      return data.value[i]
    }
  }),


});
<commit_msg>Implementation for declaration/section<commit_after>// # module: runtime
//
// Provides the base runtime.

// -- Dependencies -----------------------------------------------------
var c = require('core.check');
var show = require('core.inspect');
var Maybe = require('data.maybe');
var equal = require('deep-equal');
var { curry } = require('core.lambda');
var { Base } = require('boo');
var { List, Value:{ Applicative, Symbol, Lambda, Tagged } } = require('./data');
var { eval } = require('./eval');


// -- Helpers ----------------------------------------------------------

// ### function: assert
// @private
// @type: Validation[Violation, α] → α :: throws
function assert(val) {
  val.cata({
    Failure: λ(a) -> { throw new Error('Expected ' + show(a)) },
    Success: λ[#]
  })
}

// ### function: raise
// @private
// @type: Error -> Void :: throws
function raise(e) {
  throw e;
}

// ### function: unbox
// @private
// @type: String -> Tagged -> Any :: throws
unbox = curry(2, unbox);
function unbox(tag, val) {
  assert(tag(val));
  return val
}

var str = unbox(c.String);
var num = unbox(c.Number);
var bool = unbox(c.Boolean);


// -- Core environment -------------------------------------------------
var Env = module.exports = Base.derive({

  // -- Core operations ------------------------------------------------
  tag:
  Applicative(['tag', 'value'], function(data) {
    Tagged(data.tag, data.value)
  }),

  // --- Boolean operations --------------------------------------------
  not:
  Applicative(['value'], function(data) {
    return match data.value {
      false => true,
      []    => true,
      *     => false
    }
  }),

  'boolean?':
  Applicative(['value'], function(data) {
    return data.value === true || data.value === false
  }),

  // --- Numeric operations --------------------------------------------
  '+':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) + num(data.right)
  }),

  '-':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) + num(data.right)
  }),

  '*':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) * num(data.right)
  }),

  '/':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) / num(data.right)
  }),

  // --- Comparison operations -----------------------------------------
  '=':
  Applicative(['left', 'right'], function(data) {
    return equal(data.left, data.right)
  }),
  
  '<':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) < num(data.right)
  }),

  '<=':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) <= num(data.right)
  }),

  '>':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) > num(data.right)
  }),

  '>=':
  Applicative(['left', 'right'], function(data) {
    return num(data.left) >= num(data.right)
  }),

  
  // --- Symbol operations ---------------------------------------------
  name:
  Applicative(['value'], function(data) {
    return match data.value {
      Symbol(a) => a,
      a         => raise(new TypeError('Not a symbol: ' + show(a)))
    }
  }),

  
  // -- Vector operations ----------------------------------------------
  first:
  Applicative(['value'], function(data) {
    assert(c.Array(data.value));
    return data.value.length > 0?  data.value[0]
    :      /* otherwise */         []
  }),

  last:
  Applicative(['value'], function(data) {
    assert(c.Array(data.value));
    return data.value.length > 0?  data.value[data.value.length - 1]
    :      /* otherwise */         []
  }),

  nth:
  Applicative(['value', 'index'], function(data) {
    assert(c.Array(data.value));
    var i = num(data.index);
    if (i > data.value.length - 1) {
      throw new RangeError('Index out of bounds: ' + i);
    } else {
      return data.value[i]
    }
  }),

  // -- Text -----------------------------------------------------------
  paragraph:
  Applicative(['value'], function(data) {
    return Tagged(Symbol('paragraph'), data.value)
  }),

  text:
  Applicative(['value'], function(data) {
    return Tagged(Symbol('text'), data.value)
  }),

  bold:
  Applicative(['value'], function(data) {
    return Tagged(Symbol('bold'), data.value)
  }),

  italic:
  Applicative(['value'], function(data) {
    return Tagged(Symbol('italic'), data.value)
  }),

  'soft-break':
  Applicative(['value'], function(data) {
    return Tagged(Symbol('soft-break'), data.value)
  }),

  line:
  Applicative(['value'], function(data) {
    return Tagged(Symbol('line'), data.value)
  }),
  
  declaration:
  Applicative(['kind', 'name', 'children'], function(data) {
    return Tagged(Symbol('declaration'), data)
  }),

  section:
  Applicative(['title', 'children'], function(data) {
    return Tagged(Symbol('section'), data)
  })

});

{'fraction_non_alphanumeric': 0.28568123987971317, 'fraction_numerical': 0.0013879250520471894, 'mean_word_length': 2.03829936753338, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 35, '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': '7342627', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2640, 'n_words': 544}