<commit_before>sys = require 'sys'
fs = require 'fs' 
util = require 'util'

{ALFunction} = require './function'
{ALMacro} = require './macro'
{desexpify} = require './desexpify'

aLispParser = require('./a-lisp-parser').parser

{Stack, StackFrame} = require './stack'  

parse = (str) ->
  ret = null
  aLispParser.yy.record = (sexp) ->
    ret = sexp
    
  aLispParser.parse(str)
  
  ret

log = (msg, obj)->
  tabs = ""
  tabs += "  " for i in [0..stack.depth()]
  console.log tabs + msg + util.inspect(obj, false, 3)


env =
  vars:
    T: 'T'
    NIL: 'NIL'
    PI: Math.PI,
    E: Math.E
  funcs:
    
    '+': new ALFunction( '+', ['&rest','values'], (args...)->
      sum = 0
      sum += arg for arg in args when typeof arg == 'number'
      sum
    )
    '-': new ALFunction( '-', ['&rest','values'], (args...)->
      sum = args[0]
      sum -= arg for arg in args[1..] when typeof arg == 'number'
      sum
    )
    '*': new ALFunction( '*', ['&rest','values'], (args...)->
      sum = 1
      sum *= arg for arg in args when typeof arg == 'number'
      sum
    )
    '<': new ALFunction( '<', ['&rest','values'], (args...)->
        
      prev = args[0]
      for arg in args[1..]
        if prev >= arg
          return 'NIL'
        else
          prev = arg 

      return 'T'
    )
    # TODO: implement lispy division, reduce fractions but don't return floating point values
    CONS: new ALFunction('CONS', ['first','&rest','values'], (first, rest)->
      throw {message: "wrong number of arguments to CONS"} if arguments.length != 2
    
      if rest instanceof Array
        rest.unshift(first)
      else
        rest = [first, rest]<commit_msg>partially implemented division operator '/'<commit_after>sys = require 'sys'
fs = require 'fs' 
util = require 'util'

{ALFunction} = require './function'
{ALMacro} = require './macro'
{desexpify} = require './desexpify'

aLispParser = require('./a-lisp-parser').parser

{Stack, StackFrame} = require './stack'  

parse = (str) ->
  ret = null
  aLispParser.yy.record = (sexp) ->
    ret = sexp
    
  aLispParser.parse(str)
  
  ret

log = (msg, obj)->
  tabs = ""
  tabs += "  " for i in [0..stack.depth()]
  console.log tabs + msg + util.inspect(obj, false, 3)


env =
  vars:
    T: 'T'
    NIL: 'NIL'
    PI: Math.PI,
    E: Math.E
  funcs:
    
    '+': new ALFunction( '+', ['&rest','values'], (args...)->
      sum = 0
      sum += arg for arg in args when typeof arg == 'number'
      sum
    )
    '-': new ALFunction( '-', ['&rest','values'], (args...)->
      sum = args[0]
      sum -= arg for arg in args[1..] when typeof arg == 'number'
      sum
    )
    '*': new ALFunction( '*', ['&rest','values'], (args...)->
      sum = 1
      sum *= arg for arg in args when typeof arg == 'number'
      sum
    )
    '<': new ALFunction( '<', ['&rest','values'], (args...)->
        
      prev = args[0]
      for arg in args[1..]
        if prev >= arg
          return 'NIL'
        else
          prev = arg 

      return 'T'
    )
    '/': new ALFunction('/', ['numerator','denominator'], (numerator, denominator)->
      if parseInt(numerator)==numerator && parseInt(denominator)==denominator
        gcd = _eval.call(this, ['GCD', numerator, denominator])
      
        numerator /= gcd
        denominator /= gcd
      
        if denominator > 1
          return [numerator, denominator]
        else
          return numerator
      else
        return numerator/denominator
    )
    # TODO: implement lispy division, reduce fractions but don't return floating point values
    CONS: new ALFunction('CONS', ['first','&rest','values'], (first, rest)->
      throw {message: "wrong number of arguments to CONS"} if arguments.length != 2
    
      if rest instanceof Array
        rest.unshift(first)
      else
        rest = [first, rest]
{'fraction_non_alphanumeric': 0.16910229645093947, 'fraction_numerical': 0.004958246346555324, 'mean_word_length': 2.1651527663088355, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '7338948', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1252, 'n_words': 370}