Live data from Hacker News

Implementing a programming language in D: Lexical Analysis

blog.felixangell.com

1–10 of 22 posts

Re: Implementing a programming language in D: Lexical Analysis

#2
Wow, perfect timing! I'm currently writing my first lexer/parser in D as well (http://github.com/landaire/hsc) for a Scheme-like language used in Halo 3. I found Rob Pike's talk on lexical scanning to be pretty useful (https://www.youtube.com/watch?v=HxaD_trXwRE) and I've modeled mine pretty heavily after the text/template lexer: https://golang.org/src/text/template/parse/lex.go.

Re: Implementing a programming language in D: Lexical Analysis

#3
I've never seen the D language before and I have to admit, it looks very elegant and has piqued my interest. Thanks for your post.

I'm wondering why you used D rather than Rust? Was it just that you were curious about the D language or is there something about Rust that you don't like?

Re: Implementing a programming language in D: Lexical Analysis

#4
post #3

I've never seen the D language before and I have to admit, it looks very elegant and has piqued my interest. Thanks for your post. I'm wondering why you used D rather than Rust? Was it just that you were curious about the D language or is there something about Rust that you don't like?

If you want to see what a lexical analyzer looks like in Rust, here's one that I wrote for Lua:

https://github.com/aswyk/oxidation/blob/master/oxidation/src...

Of course, Rust's own lexer is also written in Rust.

ETA: I can't promise my lexer is actually any good... it's really the first non-trivial thing I've written in Rust. But it works ;)

Re: Implementing a programming language in D: Lexical Analysis

#5
post #3

I've never seen the D language before and I have to admit, it looks very elegant and has piqued my interest. Thanks for your post. I'm wondering why you used D rather than Rust? Was it just that you were curious about the D language or is there something about Rust that you don't like?

Hey! Yeah D is a really nice language. I have been poking around in Rust, but I don't feel I'm good enough at it to write a blog series with the language.

Re: Implementing a programming language in D: Lexical Analysis

#6
post #3

I've never seen the D language before and I have to admit, it looks very elegant and has piqued my interest. Thanks for your post. I'm wondering why you used D rather than Rust? Was it just that you were curious about the D language or is there something about Rust that you don't like?

I'm not the poster, but having tried my hands at both D and rust, I chose D over for my new project at $work. There is far less upfront mental gymnastic with D compared to Rust and that is a nice benefit when you are trying to get things done.

I continue to dabble in Rust, but D is so much more comfortable.

Re: Implementing a programming language in D: Lexical Analysis

#8
post #3

I've never seen the D language before and I have to admit, it looks very elegant and has piqued my interest. Thanks for your post. I'm wondering why you used D rather than Rust? Was it just that you were curious about the D language or is there something about Rust that you don't like?

If you want to see what a lexical analyzer looks like in Rust, here's one that I wrote for Lua: https://github.com/aswyk/oxidation/blob/master/oxidation/src... Of course, Rust's own lexer is also written in Rust. ETA: I can't promise my lexer is actually any good... it's really the first non-trivial thing I've written in Rust. But it works ;)

Thanks for that link! I'm currently writing a compiler for a minuscule language[1] and it's great to be able to look at how other people tackle the same problem and learn.

[1] https://github.com/gnuvince/minilang-rs/

Re: Implementing a programming language in D: Lexical Analysis

#9
post #3

I've never seen the D language before and I have to admit, it looks very elegant and has piqued my interest. Thanks for your post. I'm wondering why you used D rather than Rust? Was it just that you were curious about the D language or is there something about Rust that you don't like?

One advantage I could see of Rust over D for language stuff is sum types (enums in Rust). So for example you can write:

    enum MyLanguage {
      Var(String),
      Int(i32),
      Sum(Box, Box),
      Let(String, Box, Box)
    }
(Apologies if I got a few things wrong; I just mean the general idea)

Seems like without a construct like this, you'd have to use subclasses or something similar, which (to me) isn't quite as nice.

Re: Implementing a programming language in D: Lexical Analysis

#10
post #3

I've never seen the D language before and I have to admit, it looks very elegant and has piqued my interest. Thanks for your post. I'm wondering why you used D rather than Rust? Was it just that you were curious about the D language or is there something about Rust that you don't like?

One advantage I could see of Rust over D for language stuff is sum types (enums in Rust). So for example you can write: enum MyLanguage { Var(String), Int(i32), Sum(Box , Box ), Let(String, Box , Box ) } (Apologies if I got a few things wrong; I just mean the general idea) Seems like without a construct like this, you'd have to use subclasses or something similar, which (to me) isn't quite as nice.

Yeah, tagged unions and the pattern matching make things so much nicer. I love a lot of things about Rust, it's got a lot of things right.
Post reply on HN