From 64f4abb8d6fe7bd708d8e0574fe452f2a9b61f10 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 1 Oct 2019 14:35:28 -0700 Subject: [PATCH] Start writing up the implementation --- content/blog/06_compiler_semantics.md | 36 +++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/content/blog/06_compiler_semantics.md b/content/blog/06_compiler_semantics.md index 814d884..ee90da4 100644 --- a/content/blog/06_compiler_semantics.md +++ b/content/blog/06_compiler_semantics.md @@ -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 -instructions into the vector. +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& 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.