@@ -8,25 +8,25 @@ multi-staging programming. We can think of compile-time meta-programming as a
8
8
two stage compilation process: one that we write the code in top-level splices,
9
9
that will be used for code generation (macros) and one that will perform all
10
10
necessecary 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
13
13
can essentially treat as a typed-syntax tree that we can either _ show_ as a
14
14
string (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.
18
18
19
19
The motivation behind this _ paradigm_ is to let runtime information affect or
20
20
guide code-generation.
21
21
22
22
Intuition: The phase in which code is run is determined by the difference
23
23
between the number of splice scopes and quote scopes in which it is embedded.
24
24
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.
26
26
as a macro. In the general case, this means running an interpreter that
27
27
evaluates the code, which is represented as a typed abstract syntax tree. The
28
28
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
30
30
would mean that a macro’s implementation code (as opposed to the code it
31
31
expands to) invokes other macros. If macros are realized by interpretation,
32
32
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
61
61
in class ` Expr ` called ` run ` . Note that ` $ ` and ` run ` both map from ` Expr[T] `
62
62
to ` T ` but only ` $ ` is subject to the PCP, whereas ` run ` is just a normal method.
63
63
Run 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.
65
65
66
66
``` scala
67
67
package scala .quoted .staging
@@ -81,12 +81,25 @@ From [lampepfl/dotty-staging.g8](https://github.com/lampepfl/dotty-staging.g8).
81
81
82
82
It will create a project with the necessary dependencies and some examples.
83
83
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
+
84
97
## Example
85
98
86
99
Now 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
88
101
the 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
90
103
expression at runtime. Within the scope of ` run ` we can also invoke ` show ` on an expression
91
104
to get a source-like representation of the expression.
92
105
@@ -104,24 +117,3 @@ val f: Array[Int] => Int = run {
104
117
105
118
f.apply(Array (1 , 2 , 3 )) // Returns 6
106
119
```
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