@@ -92,17 +92,19 @@ The result is this. The error condition is not tested.
92
92
Tokens: [ Literal '1',Plus,InvalidChar 'x',Literal '2']
93
93
{% endhighlight %}
94
94
95
- # Racket code
95
+ # Typed Racket code
96
+
97
+ I read that Racket has many levels and types of languages and here I am using Typed Racket.
96
98
97
99
I have to note that this is my first ever Racket code and the intention is to port my haskell to Typed Racket.
98
- So I have explained what I learn using pieces of code. Neither the Racket code or Haskell code implementes
100
+ So I have explained what I learnt using pieces of code. Neither the Racket code or Haskell code implementes
99
101
the entire interpreter or even the lexer. The git repo. will have the entire code if I manage to learn
100
102
Racket sufficiently to code the whole interpreter in the book.
101
103
102
104
## Step 1
103
105
104
106
This compiles without errors. But the code is not tested. I will add more test code while I refactor.
105
- In this case I have a feeling the the code is more verbose than the equivalent Haskell code.
107
+ In this case I have a feeling that the code is more verbose than the equivalent Haskell code.
106
108
107
109
{% highlight racket %}
108
110
#lang typed/racket
@@ -183,3 +185,29 @@ But this prints {% highlight racket %}#<Literal>{% endhighlight %} without the v
183
185
This is the Racket way of printing the _ struct_ with the value.
184
186
185
187
{% highlight racket %}(Literal 2){% endhighlight %}
188
+
189
+ ## Step 2
190
+
191
+ This took longer than I expected as Racket seems to be very different than even Haskell. Many iterations later with help
192
+ from Racket experts I was able to understand how to code the function.
193
+
194
+ {% highlight racket %}
195
+ (: parse-exp (-> (U (Listof Char) )
196
+ (Listof(U (ErrorType Integer Char) (Operator Char) ))))
197
+ (define (parse-exp lst )
198
+ (match lst
199
+ [ '() (list(EndOfInput))]
200
+ [ (list x xs ...)
201
+ (match x
202
+ [ (? char-numeric?) (cons (Literal x) (parse-exp xs))]
203
+ [ #\+ (cons (Plus) (parse-exp xs)) ]
204
+ [ _ (list (Unexpected 0)) ] )] ))
205
+
206
+
207
+ {% endhighlight %}
208
+
209
+ The type of this function is only slightly different than the Haskell code and the error conditions
210
+ are not tested fully. The _ Offset_ of each charater is not tracked but that should not be difficult.
211
+
212
+ The output is {% highlight racket %}(list (Literal #\1) #<Plus > (Literal #\2) #<EndOfInput >){% endhighlight %}
213
+
0 commit comments