Skip to content

Cons pair support - #6

Merged
8dcc merged 6 commits into
mainfrom
cons-pairs
Jan 25, 2025
Merged

Cons pair support#6
8dcc merged 6 commits into
mainfrom
cons-pairs

Conversation

@8dcc

@8dcc 8dcc commented Jan 25, 2025

Copy link
Copy Markdown
Owner

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:

  • 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.
  • Add more tests for cons pairs.

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.

8dcc added 6 commits January 24, 2025 22:01
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.
@8dcc
8dcc merged commit 62b1de9 into main Jan 25, 2025
@8dcc
8dcc deleted the cons-pairs branch January 25, 2025 13:05
@8dcc

8dcc commented Jan 25, 2025

Copy link
Copy Markdown
Owner Author

Later commits that should have been part of this PR:

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant