<commit_before>-- | Define operations to build javascript expressions
--   use precedence table from
--   https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence
module Javascript.Formatted.Expression where

import Data.Char (ord)
import Data.List (intersperse, intercalate)
import Control.Monad (when)

import Javascript.Language hiding (null)
import Javascript.Formatted.Base

leftBinOp :: String -> Precedence -> Expression Formatted -> Expression Formatted -> Expression Formatted
leftBinOp op p a b = mkOperation p $
  do tellWithPrecedenceConstraint a p
     tell op
     tellWithPrecedenceConstraint b (p - 1)

rightBinOp :: String -> Precedence -> Expression Formatted -> Expression Formatted -> Expression Formatted
rightBinOp op p a b = mkOperation p $
  do tellWithPrecedenceConstraint a (p - 1)
     tell op
     tellWithPrecedenceConstraint b p

leftUnaryOp :: String -> Precedence -> Expression Formatted -> Expression Formatted
leftUnaryOp op p a = mkOperation p $
  do tell op
     tellWithPrecedenceConstraint a p

-- Avoid the risk of getting "a--1" when we want "a- -1"
showJsInt :: (Integral a) => a -> String
showJsInt n
    | n < 0     = " -" ++ showJsInt (negate n)
    | otherwise = show . toInteger $ n

showJsFrac :: (RealFrac a) => a -> String
showJsFrac f
    | f < 0     =  "-" ++ showJsFrac (negate f)
    | otherwise =  show df -- hopefully javascript always understands the output of this
    where
      df :: Double
      df = realToFrac f

instance JavascriptExpression Formatted
  where var = mkOperation 0 . tell
	int = mkOperation 0 . tell . showJsInt
	float = mkOperation 0 . tell . showJsFrac
	string = mkOperation 0 . tell . showJsString
	list xs = mkOperation 0 $
          do tell "["
             sequence_ . intersperse (tell ",") . map tellUnconstraint $ xs<commit_msg>Fix issue where "x--1.0" when we really want "x- -1.0"<commit_after>-- | Define operations to build javascript expressions
--   use precedence table from
--   https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence
module Javascript.Formatted.Expression where

import Data.Char (ord)
import Data.List (intersperse, intercalate)
import Control.Monad (when)

import Javascript.Language hiding (null)
import Javascript.Formatted.Base

leftBinOp :: String -> Precedence -> Expression Formatted -> Expression Formatted -> Expression Formatted
leftBinOp op p a b = mkOperation p $
  do tellWithPrecedenceConstraint a p
     tell op
     tellWithPrecedenceConstraint b (p - 1)

rightBinOp :: String -> Precedence -> Expression Formatted -> Expression Formatted -> Expression Formatted
rightBinOp op p a b = mkOperation p $
  do tellWithPrecedenceConstraint a (p - 1)
     tell op
     tellWithPrecedenceConstraint b p

leftUnaryOp :: String -> Precedence -> Expression Formatted -> Expression Formatted
leftUnaryOp op p a = mkOperation p $
  do tell op
     tellWithPrecedenceConstraint a p

-- Avoid the risk of getting "a--1" when we want "a- -1"
showJsInt :: (Integral a) => a -> String
showJsInt n
    | n < 0     = " -" ++ showJsInt (negate n)
    | otherwise = show . toInteger $ n

-- Avoid the risk of getting "a--1" when we want "a- -1"
showJsFrac :: (RealFrac a) => a -> String
showJsFrac f
    | f < 0     =  " -" ++ showJsFrac (negate f)
    | otherwise =  show df -- hopefully javascript always understands the output of this
    where
      df :: Double
      df = realToFrac f

instance JavascriptExpression Formatted
  where var = mkOperation 0 . tell
	int = mkOperation 0 . tell . showJsInt
	float = mkOperation 0 . tell . showJsFrac
	string = mkOperation 0 . tell . showJsString
	list xs = mkOperation 0 $
          do tell "["
             sequence_ . intersperse (tell ",") . map tellUnconstraint $ xs
{'fraction_non_alphanumeric': 0.08877146631439894, 'fraction_numerical': 0.007397622192866579, 'mean_word_length': 3.1926910299003324, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 2, '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': '3938561', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1093, 'n_words': 405}