@@ -8,25 +8,25 @@ multi-staging programming. We can think of compile-time meta-programming as a
88two stage compilation process: one that we write the code in top-level splices,
99that will be used for code generation (macros) and one that will perform all
1010necessecary evaluations at compile-time and an object program that we will run
11- as usual. What if we could synthesize code at runtime and offer one extra stage
12- to the programmer? Then we can have a value of type ` Expr[T] ` at runtime that we
11+ as usual. What if we could synthesize code at run-time and offer one extra stage
12+ to the programmer? Then we can have a value of type ` Expr[T] ` at run-time that we
1313can essentially treat as a typed-syntax tree that we can either _ show_ as a
1414string (pretty-print) or compile and run. If the number of quotes exceeds the
15- number of splices more than one (effectively handling at run-time values of type
16- ` Expr[Expr[T]] ` , ` Expr[Expr[Expr[T]]] ` , ... we talk about Multi-Stage
17- Programming) .
15+ number of splices by more than one (effectively handling at run-time values of type
16+ ` Expr[Expr[T]] ` , ` Expr[Expr[Expr[T]]] ` , ...) then we talk about Multi-Stage
17+ Programming.
1818
1919The motivation behind this _ paradigm_ is to let runtime information affect or
2020guide code-generation.
2121
2222Intuition: The phase in which code is run is determined by the difference
2323between the number of splice scopes and quote scopes in which it is embedded.
2424
25- - If there are more splices than quotes, the code is run at " compile-time" i.e.
25+ - If there are more splices than quotes, the code is run at compile-time i.e.
2626 as a macro. In the general case, this means running an interpreter that
2727 evaluates the code, which is represented as a typed abstract syntax tree. The
2828 interpreter can fall back to reflective calls when evaluating an application
29- of a previously compiled method. If the splice excess is more than one, it
29+ of a previously compiled method. If the splice excess is more than one, it
3030 would mean that a macro’s implementation code (as opposed to the code it
3131 expands to) invokes other macros. If macros are realized by interpretation,
3232 this would lead to towers of interpreters, where the first interpreter would
@@ -61,7 +61,7 @@ to be executed at a later stage. To run that code, there is another method
6161in class ` Expr ` called ` run ` . Note that ` $ ` and ` run ` both map from ` Expr[T] `
6262to ` T ` but only ` $ ` is subject to the PCP, whereas ` run ` is just a normal method.
6363Run provides a ` QuoteContext ` that can be used to show the expression in the scope of ` run ` .
64- On the other hand ` withQuoteContext ` provides a ` QuoteContext ` without evauating the expression.
64+ On the other hand ` withQuoteContext ` provides a ` QuoteContext ` without evaluating the expression.
6565
6666``` scala
6767package scala .quoted .staging
@@ -81,12 +81,25 @@ From [lampepfl/dotty-staging.g8](https://github.com/lampepfl/dotty-staging.g8).
8181
8282It will create a project with the necessary dependencies and some examples.
8383
84+ In case you prefer to create the project on your own, make sure to define the following dependency in your build.sbt
85+
86+ ``` scala
87+ libraryDependencies += " ch.epfl.lamp" %% " dotty-staging" % scalaVersion.value
88+ ```
89+
90+ and in case you use ` dotc ` /` dotr ` directly, then use the ` -with-compiler ` flag for both:
91+
92+ ``` shell
93+ dotc -with-compiler -d out Test.scala
94+ dotr -with-compiler -classpath out Test
95+ ```
96+
8497## Example
8598
8699Now take exactly the same example as in [ Macros] ( ./macros.md ) . Assume that we
87- do not want to pass an array statically but generated code at run-time and pass
100+ do not want to pass an array statically but generate code at run-time and pass
88101the value, also at run-time. Note, how we make a future-stage function of type
89- ` Expr[Array[Int] => Int] ` in line 4 below. Using ` run { ... } ` we can evaluate an
102+ ` Expr[Array[Int] => Int] ` in line 6 below. Using ` run { ... } ` we can evaluate an
90103expression at runtime. Within the scope of ` run ` we can also invoke ` show ` on an expression
91104to get a source-like representation of the expression.
92105
@@ -104,24 +117,3 @@ val f: Array[Int] => Int = run {
104117
105118f.apply(Array (1 , 2 , 3 )) // Returns 6
106119```
107-
108- Note that if we need to run the main (in an object called ` Test ` ) after
109- compilation we need make available the compiler to the runtime:
110-
111- ``` shell
112- dotc -with-compiler -d out Test.scala
113- dotr -with-compiler -classpath out Test
114- ```
115-
116- Or, from SBT:
117-
118- ``` scala
119- libraryDependencies += " ch.epfl.lamp" %% " dotty-staging" % scalaVersion.value
120- ```
121-
122- ## Template project
123- Using sbt version ` 1.1.5+ ` , do:
124- ```
125- sbt new lampepfl/dotty-staging.g8
126- ```
127- in the folder where you want to clone the template.
0 commit comments