tiancaiamao / shen-go

A Go implementation of the Shen programming language
Other
55 stars 4 forks source link

cora: change the runtime environment representation for 'closure as codegen' #34

Closed tiancaiamao closed 2 years ago

tiancaiamao commented 2 years ago

Continue with https://github.com/tiancaiamao/shen-go/pull/32/

The old runtime environment representation is a variant of SECD machine. Although there is an optimization that the local variable is optimized to not allocate in Env (heap), the env object itself is still there.

After this commit, the runtime representation is just like the C language -- no Environment! I use closure convert to collect the free variable in the Closure struct. In the calling protocol, args[0] is the closure object itself, so the free variable is accessible through args[0]->parent->parent->freeVars[n] The Closure struct will record the it closed free variable, and also the parent closure within which it's created.

This runtime representation is much efficient than before, it avoid the allocating of Environment object in every call. Run shen-go test:

Before 160s
After 35s