Cons pair support - #6
Merged
Merged
Conversation
Replace linked lists approach for expression lists with proper cons-pairs. One of my biggest/ugliest commits, but most of the files had to be changed. General changes: * The 'expr_is_nil' function now only admits symbols with a value of "nil", since there are no expressions of type "List". * Make 'expr_list_print' static, add 'print_func' argument. New functions and callable macros: * Add 'expr_is_proper_list' function. * Add 'expr_list_nth' function. * Add 'expr_list_has_only_lists' function, used in 'prim_append'. * Add 'SL_EXPECT_PROPER_LIST' callable macro. * Add 'CAR', 'CDR', 'CADR' and 'CDDR' callable macros. Renamed functions and macros: * Replace old 'EXPR_PARENT' macro with 'EXPR_PAIR'. * Replace 'EXPR_LST_P' callable macro with 'EXPR_PAIR_P'. * Rename 'expr_list_is_member' function to 'expr_is_member'. * Replace 'prim_is_list' C function (that used to check for 'EXPR_PARENT') and 'list?' Lisp function with 'prim_is_pair' and 'pair?', respectively. Removed functions: * Remove 'expr_list_clone' function, since we can now clone the a whole tree if needed with 'expr_clone_recur'. * Remove 'expr_list_equal' function. * Remove 'expr_list_write' function. * Remove 'mark_lambdactx' static function from 'garbage_collector.c'. TODO comments: * About adding a 'mapcar' C function, in 'eval_list'. * About adding a 'SL_EXPECT_LEN' callable macro, in 'SL_EXPECT_ARG_NUM'. * About renaming the argument names in C primitives, in 'SL_EXPECT_ARG_NUM'. * About adding 'TOKEN_DOT' to lexer, in 'parse_recur'. * About allowing dot inside lists to indicate the CDR, in 'parse_recur'. * About 'list?' Lisp function. Things left to do: * Allow dots when parsing lists for specifying the CDR.
Add 'TOKEN_DOT' to lexer.
8dcc
added a commit
that referenced
this pull request
Feb 16, 2025
Work with references, instead of always returning copies. This change was possible thanks to #5 (which needed #4), #6 and #7. Some changes were merged into this 'copy-references' branch in #7. Major changes: * Remove most calls to 'expr_clone', for working with references instead of copies. * Return or store references in 'car', 'cdr', 'cons', 'nth', 'quote' and 'backquote' primitives. * Add proper environment closures, setting the parent environments whenever a lambda is created, not when it's called. See #7. * Change how GC handles lambdas, add 'is_used' member to 'Env'. Mostly included in #7, but not completely (see 5758c96). * Don't mark globals ('g_nil', 'g_tru' and 'g_debug_trace_list') for garbage collection, since their references are being stored in the environment. Minor changes: * Add 'gc_mark_env' and 'gc_mark_env_and_parents' static functions to 'garbage_collector.c'. * Add an 'Env*' parameter to 'lambdactx_init', remove it from 'lambdactx_eval_body'. * Don't make the 'Expr*' parameter of 'env_bind' and 'env_bind_global' constant. * Add closure test to 'test/lambdas.lisp'. See #7.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace linked lists approach for expression lists with proper cons-pairs. Most of the files had to be changed. See my blog article for more information.
General changes:
expr_is_nilfunction now only admits symbols with a value of "nil",since there are no expressions of type "List".
expr_list_printstatic, addprint_funcargument.New functions and callable macros:
expr_is_proper_listfunction.expr_list_nthfunction.expr_list_has_only_listsfunction, used inprim_append.SL_EXPECT_PROPER_LISTcallable macro.CAR,CDR,CADRandCDDRcallable macros.Renamed functions and macros:
EXPR_PARENTmacro withEXPR_PAIR.EXPR_LST_Pcallable macro withEXPR_PAIR_P.expr_list_is_memberfunction toexpr_is_member.prim_is_listC function (that used to check forEXPR_PARENT) andlist?Lisp function withprim_is_pairandpair?, respectively.Removed functions:
expr_list_clonefunction, since we can now clone the a whole tree ifneeded with
expr_clone_recur.expr_list_equalfunction.expr_list_writefunction.mark_lambdactxstatic function fromgarbage_collector.c.TODO comments:
mapcarC function, ineval_list.SL_EXPECT_LENcallable macro, inSL_EXPECT_ARG_NUM.SL_EXPECT_ARG_NUM.TOKEN_DOTto lexer, inparse_recur.parse_recur.list?Lisp function.