<commit_before>package hdm.pk070.jscheme.obj.builtin.syntax;

import hdm.pk070.jscheme.error.SchemeError;
import hdm.pk070.jscheme.eval.SchemeEval;
import hdm.pk070.jscheme.obj.SchemeObject;
import hdm.pk070.jscheme.obj.builtin.simple.SchemeCons;
import hdm.pk070.jscheme.obj.builtin.simple.SchemeNil;
import hdm.pk070.jscheme.obj.builtin.simple.SchemeSymbol;
import hdm.pk070.jscheme.obj.builtin.simple.SchemeVoid;
import hdm.pk070.jscheme.obj.custom.SchemeCustomUserFunction;
import hdm.pk070.jscheme.table.environment.Environment;
import hdm.pk070.jscheme.table.environment.entry.EnvironmentEntry;

import java.util.Objects;

/**
 *
 *
 * @author patrick.kleindienst
 */
public final class SchemeBuiltinDefine extends SchemeBuiltinSyntax {

    public static SchemeBuiltinDefine create() {
        return new SchemeBuiltinDefine();
    }

    private SchemeBuiltinDefine() {
        super("define");
    }

    @Override
    public SchemeObject apply(SchemeObject argumentList, Environment<SchemeSymbol, EnvironmentEntry> environment)
            throws SchemeError {

        Objects.requireNonNull(argumentList);
        Objects.requireNonNull(environment);

        // (define abc 123)
        // (define abc (+ 42 1))
        // (define (add1 x) (+ 1 x))
        // Argument list must be a cons
        if (!argumentList.typeOf(SchemeCons.class) || !((SchemeCons) argumentList).getCdr().typeOf(SchemeCons.class)) {
            throw new SchemeError("(define): bad syntax (requires exactly 2 arguments)");
        }<commit_msg>Added and improved Javadoc.<commit_after>package hdm.pk070.jscheme.obj.builtin.syntax;

import hdm.pk070.jscheme.error.SchemeError;
import hdm.pk070.jscheme.eval.SchemeEval;
import hdm.pk070.jscheme.obj.SchemeObject;
import hdm.pk070.jscheme.obj.builtin.simple.SchemeCons;
import hdm.pk070.jscheme.obj.builtin.simple.SchemeNil;
import hdm.pk070.jscheme.obj.builtin.simple.SchemeSymbol;
import hdm.pk070.jscheme.obj.builtin.simple.SchemeVoid;
import hdm.pk070.jscheme.obj.custom.SchemeCustomUserFunction;
import hdm.pk070.jscheme.table.environment.Environment;
import hdm.pk070.jscheme.table.environment.entry.EnvironmentEntry;

import java.util.Objects;

/**
 * Create a binding between a {@link SchemeSymbol} and a variable or rather a function
 * in a local or global environment.
 * <br/><br/>
 * Usage examples:
 * <br/><br/>
 * (define abc 123)<br/>
 * (define abc (+ 42 1))<br/>
 * (define (add1 x) (+ 1 x))<br/>
 *
 * @author patrick.kleindienst
 */
public final class SchemeBuiltinDefine extends SchemeBuiltinSyntax {

    public static SchemeBuiltinDefine create() {
        return new SchemeBuiltinDefine();
    }

    private SchemeBuiltinDefine() {
        super("define");
    }

    @Override
    public SchemeObject apply(SchemeObject argumentList, Environment<SchemeSymbol, EnvironmentEntry> environment)
            throws SchemeError {

        Objects.requireNonNull(argumentList);
        Objects.requireNonNull(environment);

        // Argument list must be a cons
        if (!argumentList.typeOf(SchemeCons.class) || !((SchemeCons) argumentList).getCdr().typeOf(SchemeCons.class)) {
            throw new SchemeError("(define): bad syntax (requires exactly 2 arguments)");
        }
{'fraction_non_alphanumeric': 0.1148502624266749, 'fraction_numerical': 0.025933930225378203, 'mean_word_length': 3.909090909090909, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '5775767', 'n_tokens_mistral': 1070, 'n_tokens_neox': 968, 'n_words': 197}