Start writing up the implementation
This commit is contained in:
parent
bcaa67cc7a
commit
64f4abb8d6
|
@ -161,9 +161,35 @@ graphs when we have to instantiate those functions, we simply
|
|||
evaluate the arguments and perform the relevant arithmetic operation using BinOp.
|
||||
We will do a similar thing for constructors.
|
||||
|
||||
With that out of the way, we can get around to writing some code. We can envision
|
||||
a method on the `ast` struct that takes an environment (just like our compilation
|
||||
scheme takes the environment \\(\\rho\\\)). Rather than returning a vector
|
||||
### Implementation
|
||||
|
||||
With that out of the way, we can get around to writing some code. Let's
|
||||
first define C++ structs for the instructions of the G-machine:
|
||||
|
||||
{{< codeblock "C++" "compiler/06/instruction.hpp" >}}
|
||||
|
||||
We can now envision a method on the `ast` struct that takes an environment
|
||||
(just like our compilation scheme takes the environment \\(\\rho\\\)),
|
||||
and compiles the `ast`. Rather than returning a vector
|
||||
of instructions (which involves copying, unless we get some optimization kicking in),
|
||||
we'll pass to it a reference to a vector. The method will then place the generated
|
||||
we'll pass a reference to a vector to our method. The method will then place the generated
|
||||
instructions into the vector.
|
||||
|
||||
There's one more thing to be considered. How do we tell apart a "global"
|
||||
from a variable? A naive solution would be to take a list or map of
|
||||
global functions as a third parameter to our `compile` method.
|
||||
But there's an easier way! We know that the program passed type checking.
|
||||
This means that every referenced variable exists. From then, the situation is easy -
|
||||
if actual variable names are kept in the environment, \\(\\rho\\), then whenever
|
||||
we see a variable that __isn't__ in the current environment, it must be a function name.
|
||||
|
||||
Having finished contemplating out method, it's time to define a signature:
|
||||
```C++
|
||||
virtual void compile(const env_ptr env, std::vector<instruction>& into) const;
|
||||
```
|
||||
|
||||
Ah, but now we have to define "environment". Let's do that:
|
||||
|
||||
{{< codeblock "C++" "compiler/06/env.hpp" >}}
|
||||
|
||||
And now, we begin our implementation.
|
||||
|
|
Loading…
Reference in New Issue
Block a user