nibi-lang / nibi

An interpreted list processing language inspired by Lisp
GNU Affero General Public License v3.0
3 stars 2 forks source link

Function definition vs Implementation #143

Open bosley opened 1 year ago

bosley commented 1 year ago

Right now the following are equivalent:

(:= x (fn [] (<- 0))

and

(fn x [] (<- 0))

The former was added as an after thought to allow anonymous functions, but the making of the threads module brought to light the issue that functions are living entities. When you define a function its also implementing it. This means that multiple threads calling the same function causes a trampling, especially with loops. Thread A starts a loop a moment before thread B, and when thread B modifies the counter, thread A loses its place.

This task is to distinguish the above statements into two separate entities. The latter will be solely a definition that when called, creates a unique copy of itself to use for execution.

I picture the latter as having its own alternative to function_info_s that has a thread safe "queue" of copies. When called, it will grab a copy off the queue to be used for execution.The function should keep track of how often it is called so it can decide if it should queue more objects if its a hot function.

Since the queue will be thread safe once it hits a certain number and needs to re-queue copies, it can spawn a thread to generate the copies that inserts them once copies are completed. We want to spin this off because it could cause substantial overhead on the active thread.

This will lay the foundation for bytecode-constructed functions that work within the interpreter

bosley commented 1 year ago

To consider this complete, it needs to be able to fix the trample demo in /programs and not hinder the runtime

bosley commented 1 year ago

perhaps even anonymous functions should work this way. just generate a random name for it and when the target assigned item is cloned or called, the same thing happens.

this is de way

bosley commented 1 year ago

Just to note: this won't resolve the issue of accessing global variables at the same time and editing them, but it is required regardless