<commit_before>package xyz.javecs.tools.expr

import kotlin.collections.HashMap

class Calculator(expressions: Array<String> = emptyArray()) : EvalContext {
    private val evaluator = EvalVisitor(this)
    private val symbol: HashMap<String, Double> = HashMap()
    private val constant = constant()
    private val function = builtIn()
    private var expression = Expression()
    var value: Number = expression.value
        get() = expression.getValue()

    init {
        for (expr in expressions) {
            eval(expr)
        }
    }

    fun eval(expr: String = ""): Calculator {
        expression = when (expr.isEmpty()) {
            true -> expression
            else -> {
                val evaluated = evaluator.visit(parser(expr).start())
                evaluated.expr = expr
                evaluated
            }
        }
        return this
    }

    fun clear() {
        symbol.clear()
        expression = Expression()
    }

    override fun isConstant(name: String) = constant.contains(name.toLowerCase())
    override fun get(name: String) = constant.getOrDefault(name.toLowerCase(), symbol.getOrDefault(name, Double.NaN))
    override fun put(name:String, value: Double) = when (isConstant(name)) {
        true -> Double.NaN
        else -> {
            symbol[name] = value
            value
        }
    }
    override fun call(name: String, value: Double) = function.getOrDefault(name.toLowerCase(), { Double.NaN })(value)

    override fun toString(): String {
        return expression.toString()
    }
}<commit_msg>コードを見直しました。<commit_after>package xyz.javecs.tools.expr

import kotlin.collections.HashMap

class Calculator(expressions: Array<String> = emptyArray()) : EvalContext {
    private val evaluator = EvalVisitor(this)
    private val variables = HashMap<String, Double>()
    private val constants = constant()
    private val functions = builtIn()
    private var expression = Expression()
    var value: Number = expression.value
        get() = expression.getValue()

    init {
        for (expr in expressions) {
            eval(expr)
        }
    }

    fun eval(expr: String = ""): Calculator {
        expression = when (expr.isEmpty()) {
            true -> expression
            else -> {
                val evaluated = evaluator.visit(parser(expr).start())
                evaluated.expr = expr
                evaluated
            }
        }
        return this
    }

    fun clear() {
        variables.clear()
        expression = Expression()
    }

    override fun toString() = expression.toString()
    override fun isConstant(name: String) = constants.contains(name.toLowerCase())
    override fun call(name: String, value: Double) = functions.getOrDefault(name.toLowerCase(), { Double.NaN })(value)
    override fun get(name: String) = constants.getOrDefault(name.toLowerCase(), variables.getOrDefault(name, Double.NaN))
    override fun put(name:String, value: Double) = when (isConstant(name)) {
        true -> Double.NaN
        else -> {
            variables[name] = value
            value
        }
    }
}
{'fraction_non_alphanumeric': 0.1111111111111111, 'fraction_numerical': 0.0, 'mean_word_length': 2.3087606837606836, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '5930883', 'n_tokens_mistral': 822, 'n_tokens_neox': 816, 'n_words': 209}