Closures (and garbage collection for environments, which is required by closures...
authorDavid Kerkeslager <kerkeslager@gmail.com>
Fri, 11 Aug 2017 15:50:11 +0000 (11:50 -0400)
committerGitHub <noreply@github.com>
Fri, 11 Aug 2017 15:50:11 +0000 (11:50 -0400)
commitd6af7d074bf65e782e42055623a197863b5f8000
tree8d9cbeb227d5786ef4c8cd219a77130658f9f62a
parentc9f47106ccf806533783a551b5c51d9280869635
Closures (and garbage collection for environments, which is required by closures) (#1)

* Add proper reference counting to environments

* It has become clear we need a garbage collector for environments before we can implement closures

* Allow line comments starting with #

* Allocate Environments from a garbage-collected EnvironmentPool

* Wrap closure functions in a struct in preparation for storing the defining environment with them

* Move the instantiation of closures into where they are created

* Fixed a bug in the mark/sweep algorithm:

* It would infinitely recurse in the case of cycles, because nothing was
  checking that Environments had already been marked.
* Simply exiting on self->mark == mark would fix this, but introduce a
  separate, worse bug where recursion wouldn't reach some live objects,
  since some objects might already be marked with the current mark due
  to previous cycles.
* To fix, I introduced a pass over the pool that marks everything false
  so we can reliably assume that self->mark == true means that the
  environment has been marked in the current GC round. It's slower than
  I wanted, but it's better to do the correct thing slowly than the
  wrong thing quickly.

* Store the defining environment on closures and GC it appropriately

* Test a simple case of closures and fix the discovered errors

* Add a test that explicitly demonstrates freeing a cycle

* Allow user-defined functions to take arguments

* Another closure example
16 files changed:
examples/18_comments.fur [new file with mode: 0644]
examples/18_comments.fur.output.txt [new file with mode: 0644]
examples/19_closures.fur [new file with mode: 0644]
examples/19_closures.fur.output.txt [new file with mode: 0644]
examples/20_cycles.fur [new file with mode: 0644]
examples/20_cycles.fur.output.txt [new file with mode: 0644]
examples/21_arguments.fur [new file with mode: 0644]
examples/21_arguments.fur.output.txt [new file with mode: 0644]
examples/22_close_arguments.fur [new file with mode: 0644]
examples/22_close_arguments.fur.output.txt [new file with mode: 0644]
generation.py
normalization.py
parsing.py
templates/program.c
tokenization.py
transformation.py