<commit_before>#include <stdio.h>
#include <string.h> /* strcmp */

#include "forms.h"
#include "value.h"
#include "plot.h"
#include "funcs.h"
#include "eval.h"
#include "hash.h"
#include "env.h"

#define DEBUG 0

/* ignore unused parameter warnings */
#pragma GCC diagnostic ignored "-Wunused-parameter"

/* (define-library (library name) body...) -core -syntax
 * define a library
 *
 * library definitions are of the form
 * (define-library <library-name>
 *      <library-declaration> ...)
 *
 * where a <library-declaration> is any of:
 *      (export <export spec> ...)
 *      (import <import set> ...)
 *      (begin <command or defintion> ...)
 *      (include <filename1> <filename2> ...)
 *      (include-ci <filename1> <filename2> ...)
 *      (include-library-declaration <filename1> <filename2> ...)
 *      (cond-expand <ce-clause1> <ce-clause2> ...)
 */
struct plot_value * plot_form_define_library(struct plot_env *env, struct plot_value *sexpr){
    /* library name */
    plot_value *name;
    /* body to iterate through */
    plot_value *body;
    /* current cursor for iteration*/
    plot_value *cur;
    /* current item we are processing in iteration */
    plot_value *item;
    /* library we generate */
    plot_value *lib;

    /* temporary returned when using other functions
     * to assist
     */
    plot_value *ret;

    /* temporary hash value of symbol being inspected
     * comparing against symbol hash is UGLY
     * FIXME TODO
     */
    unsigned long long hash = 0;

    /* FIXME ignore unused variable warnigns */
    #pragma GCC diagnostic ignored "-Wunused-variable"

    /* two new envs, internal and external/exported */
    plot_env *in, *ex;


    /* A library has 3 'parts': */

    /* IMPORTS
     * are processed immediately and modify the internal env.
     */

    /* DEFINITIONS
     * (inside begin exprs)
     * are processed after all imports are performed.
     */
    plot_value *definitions = null;
    /* cursor into definitions */
    plot_value **defcur = &definitions;

    /* EXPORTS
     * are processed after all DEFINITIONS have been processed.
     */
    plot_value *exports = null;
    /* cursor into exports */
    plot_value **expcur = &exports;


    if( !sexpr || sexpr->type != plot_type_pair ){
        return plot_runtime_error(plot_error_bad_args, "expected at least 2 args", "plot_form_define_library");
    }

    name = car(sexpr);
    body = cdr(sexpr);

    /* no parents to either as they are both 'clean' (or emtpy) envs */
    ex = plot_alloc_env(0);
    in = plot_alloc_env(0);
    /* construct out library object */
    lib = plot_new_library(in, ex);

    switch( name->type ){
        case plot_type_pair:
            break;
        case plot_type_symbol:
            break;
        default:
            return plot_runtime_error(plot_error_bad_args, "library name was of incorrect type", "plot_form_define_library");
    }

    /* go through body
     * examine each part
     *  if import -> process immediately
     *  if definition -> add to `defintions`
     *  if export -> add to `exports
     */
    for( cur = body; cur->type == plot_type_pair; cur = cdr(cur) ){
        item = car(cur);

        if( item->type != plot_type_pair ){
            return plot_runtime_error(plot_error_bad_args, "library body was malformed, expected sub-sexpr but found expr", "plot_form_define_library");
        }

        if( car(item)->type != plot_type_symbol ){
            return plot_runtime_error(plot_error_bad_args, "library body was malformed, expected symbol", "plot_form_define_library");
        }
<commit_msg>Documenting define-library cursor and item to avoid potential confusion, probably want to document in patterns<commit_after>#include <stdio.h>
#include <string.h> /* strcmp */

#include "forms.h"
#include "value.h"
#include "plot.h"
#include "funcs.h"
#include "eval.h"
#include "hash.h"
#include "env.h"

#define DEBUG 0

/* ignore unused parameter warnings */
#pragma GCC diagnostic ignored "-Wunused-parameter"

/* (define-library (library name) body...) -core -syntax
 * define a library
 *
 * library definitions are of the form
 * (define-library <library-name>
 *      <library-declaration> ...)
 *
 * where a <library-declaration> is any of:
 *      (export <export spec> ...)
 *      (import <import set> ...)
 *      (begin <command or defintion> ...)
 *      (include <filename1> <filename2> ...)
 *      (include-ci <filename1> <filename2> ...)
 *      (include-library-declaration <filename1> <filename2> ...)
 *      (cond-expand <ce-clause1> <ce-clause2> ...)
 */
struct plot_value * plot_form_define_library(struct plot_env *env, struct plot_value *sexpr){
    /* library name */
    plot_value *name;
    /* body to iterate through */
    plot_value *body;
    /* current cursor for iteration*/
    plot_value *cur;
    /* current item we are processing in iteration */
    plot_value *item;
    /* library we generate */
    plot_value *lib;

    /* temporary returned when using other functions
     * to assist
     */
    plot_value *ret;

    /* temporary hash value of symbol being inspected
     * comparing against symbol hash is UGLY
     * FIXME TODO
     */
    unsigned long long hash = 0;

    /* FIXME ignore unused variable warnigns */
    #pragma GCC diagnostic ignored "-Wunused-variable"

    /* two new envs, internal and external/exported */
    plot_env *in, *ex;


    /* A library has 3 'parts': */

    /* IMPORTS
     * are processed immediately and modify the internal env.
     */

    /* DEFINITIONS
     * (inside begin exprs)
     * are processed after all imports are performed.
     */
    plot_value *definitions = null;
    /* cursor into definitions */
    plot_value **defcur = &definitions;

    /* EXPORTS
     * are processed after all DEFINITIONS have been processed.
     */
    plot_value *exports = null;
    /* cursor into exports */
    plot_value **expcur = &exports;


    if( !sexpr || sexpr->type != plot_type_pair ){
        return plot_runtime_error(plot_error_bad_args, "expected at least 2 args", "plot_form_define_library");
    }

    name = car(sexpr);
    body = cdr(sexpr);

    /* no parents to either as they are both 'clean' (or emtpy) envs */
    ex = plot_alloc_env(0);
    in = plot_alloc_env(0);
    /* construct out library object */
    lib = plot_new_library(in, ex);

    switch( name->type ){
        case plot_type_pair:
            break;
        case plot_type_symbol:
            break;
        default:
            return plot_runtime_error(plot_error_bad_args, "library name was of incorrect type", "plot_form_define_library");
    }

    /* go through body
     * examine each part
     *  if import -> process immediately
     *  if definition -> add to `defintions`
     *  if export -> add to `exports
     */
    for( cur = body; cur->type == plot_type_pair; cur = cdr(cur) ){
        /* `cur` records our progress through the list (`body`)
         * `item` is the current element we are inspecting inside the list (`body`)
         *
         *      here our `body` is a list
         *          (foo bar)
         *          (fooo barr)
         *          (baz barf)
         *          null
         *
         *      at each step `cur` is pointing to our position through `body` (the list)
         *          first step: cur = (foo bar) (fooo barr) (baz barf) null
         *          second step: cur = (fooo barr) (baz barf) null
         *          third step: cur = (baz barf) null
         *          fourth step: cur = null, null->type != plot_type_pair so for loop terminates
         *
         *      at each step we then set `item` to point to the actual element in the `body` (the list)
         *          first step: item = (foo bar)
         *          second step: cur = (fooo barr)
         *          third step: cur = (baz barf)
         *
         *      we then inspect the car(item) which should be a symbol
         *          first step: car(item) = foo
         *          second step: car(item) = fooo
         *          third step: car(item) = baz
         *
         */
        item = car(cur);

        if( item->type != plot_type_pair ){
            return plot_runtime_error(plot_error_bad_args, "library body was malformed, expected sub-sexpr but found expr", "plot_form_define_library");
        }

        if( car(item)->type != plot_type_symbol ){
            return plot_runtime_error(plot_error_bad_args, "library body was malformed, expected symbol", "plot_form_define_library");
        }

{'fraction_non_alphanumeric': 0.11368692190609998, 'fraction_numerical': 0.003278304648167662, 'mean_word_length': 2.3185703185703184, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 52, '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': '6131555', 'n_tokens_mistral': 2566, 'n_tokens_neox': 2496, 'n_words': 807}