Except, what happens if there's an error and Lookup is given an empty
string? Will it return token.ILLEGAL?

When you use a string slice like that the indexes which weren't assigned a
value are assigned a zero value. In this case, an empty string "". Rather
than add extra code to check for the empty string I felt it more intuitive
to use the map.
On Thursday, 8 May 2014 11:18:52 UTC-7, griff...@gmail.com wrote:

In the post about tokens, you say you avoided using slices to make the
Lookup function easier to write, but the code for a slice is almost
identical to the code for a map:

var tok_strings = []string{
ILLEGAL: "Illegal",
COMMENT: "Comment",
INTEGER: "Integer",
LPAREN: "(",
RPAREN: ")",
ADD: "+",
SUB: "-",
MUL: "*",
QUO: "/",
REM: "%",

The only change you need to make in your Lookup function is to typecast t
to Token.
Alex Griffin

On Saturday, May 3, 2014 7:39:04 PM UTC-5, Rob Thornton wrote:

I wrote a 10 part series talking about writing a compiler from scratch in

I'd love to hear feedback from the community and suggestions for
improvement. I'm sure I've cocked something up but hopefully nothing too
badly. The first post can be found at:

Go has helped me tremendously when it comes to compiler design and I hope
I've it justice. I want to take my hat off to everyone involved in making
such an awesome language.

A special thanks to Rob Pike who gave me the encouragement to learn
assembly and Ian Lance Taylor who took the time to answer my questions on
stacks. These thanks apply more to the 2nd series I'm in the middle of
writing rather than the one listed above, but I digress.
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts


Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 11 of 11 | next ›
Discussion Overview
groupgolang-nuts @
postedMay 4, '14 at 12:39a
activeMay 9, '14 at 3:58p



site design / logo © 2022 Grokbase