Hi there,

I'm working through the very excellent "Write an Interpreter in Go" 
([https://interpreterbook.com/](https://interpreterbook.com/))

I'm trying to follow the book but implementing in nim rather than Go, but 
running into a wall trying to translate Go into Nim.

For example passing pointers to objects or returning pointers to objects.

An example in Go
    
    
    /* A lexer */
    type Lexer struct {
       input string
       position int
       readPosition int
       ch byte
    }
    
    /* Function to create a new lexer */
    func New(input string) *Lexer {
      l := &Lexer{input: input}
    }
    

Translating to nim I have:
    
    
    type TLexer = object
      input     : string
      position  : int
      readPosition : int
      ch        : byte
    
    proc new(input : string) : TLexer =
      let l = new TLexer(input, 0, 0, 0)
      return l
    

but I know I'm not doing this well. #1 Not sure how to create a Lexer object #2 
Not sure how to return a pointer from a proc.

Initially I tried defining the Lexer as
    
    
    type Lexer = ref object
     ...
    

But that did not compile.

I also tried defining the proc as
    
    
    proc New(input: string): ref Lexer =
     ....
    

But also didn't compile.

The nim way of working with objects is a bit new to me. I'm sure it's something 
very simple but not sure what is that little bit I'm nt getting. 

Reply via email to