Live data from Hacker News

Writing Your Own Programming Language

github.com

51–60 of 95 posts

Re: Writing Your Own Programming Language

#51
post #3

Honest question: why do tutorials in this topic seem to always use functional languages/syntax as examples? Our compilers class at MST had us re-implement a lisp compiler, but didn't touch on why we used lisp specifically (other than the professor liking it; we were a largely C++ school). Do they think functional languages are simpler / less complex / easier to understand? Is there something inherently easier to impl…

Prefix notation (ie. + 1 1) instead of infix notation (ie. 1 + 1) is much easier parse, as there are no precedence rules and you don't require different conventions for expressions and function calls.

Re: Writing Your Own Programming Language

#52

Earlier quoted context omitted.

Why is it so often stated, that Scheme doesn't need a parser? I mean the AST is simpler then in other languages, but at the end of the day, you have to parse SEXPs, don't you? I mean let's say you evaluate > (+ 2 3) You have to parse the string "(+ 2 3)" and will then evaluate it to 5?

... reading sexp's is technically parsing but it is so far removed from something like a C parser that it's really not the same animal. I mean, you can write a function to build a list from a sexp in a few minutes, while even a basic hand-written C parser will take days/weeks/months depending on your familiarity. So, that's a reason why it is said. (edit: also, if the course implements scheme IN scheme, then you don'…

> ... reading sexp's is technically parsing but it is so far removed from something like a C parser that it's really not the same animal.

Yeah it is obviously a lot easier to parse than traditional, imperative languages like C. I just do not like the phrase, as it gives this "magic" vibe to Scheme. I mean its syntax is beautiful and simple, but there is definitely no magic there.

Re: Writing Your Own Programming Language

#53
post #3

Honest question: why do tutorials in this topic seem to always use functional languages/syntax as examples? Our compilers class at MST had us re-implement a lisp compiler, but didn't touch on why we used lisp specifically (other than the professor liking it; we were a largely C++ school). Do they think functional languages are simpler / less complex / easier to understand? Is there something inherently easier to impl…

If you were trying to implement a language with assignment statements like:

  x := y + z
or

  f(y + z)
or

  g(f(x, y))
you'd need to be able to evaluate expressions first. Maybe it's just simpler to illustrate a smaller language first?

Re: Writing Your Own Programming Language

#54
There are a TON of resources like this which focus on lexing and parsing which is all fine and dandy but interpreting the resulting Abstract Syntax Tree will be extremely slow.

Are there any resources on the code generation side of things? Even getting from a high level language down to SSA seems like a big leap (nevermind going from SSA to assembly).

Re: Writing Your Own Programming Language

#55

Peter Norvig's "(How to Write a (Lisp) Interpreter (in Python))" ( http://norvig.com/lispy.html ) covers a superset of this material and makes more sense, and actually has a portable implementation you can run yourself. If you're going to do this, use Norvig as a guide.

When I think about writing my own language, I think of something with as few parenthesis as possible and all the examples use Lisp.

Re: Writing Your Own Programming Language

#56
post #42
post #21

Earlier quoted context omitted.

I hesitated on using BNF. It seemed overkill Presumably because there's no real nesting of terms in assembly code? Personally, I find BNF so simple that I'd probably still use it for describing the grammar, but lots of parser generator technology is heavier machinery than required (though a recursive descent parser built from a regular grammar is essentially a DFA if your language/compiler offers tail call eliminatio…

Is regular grammar same as context-free grammar?

It is not - see https://en.wikipedia.org/wiki/Chomsky_hierarchy for the where these terms come from. Context-free grammars are strict supersets of regular grammars and cover the vast majority of programming language constructs.

Re: Writing Your Own Programming Language

#57
post #40

Earlier quoted context omitted.

I'm writing an assembler for a virtual 16 bit CPU I built, and I hesitated on using BNF. It seemed overkill so I ended up hand-rolling a parser; for an assembler it's straightforward enough, but I can easily see how it could get ridiculous to implement a compiler this way.

Is your codebase public? I would like to take a look.

Sure. It's a work in progress, my latest changes are not on Github yet, I'll probably push tonight.

Keep in mind it's a personal project, the assembler is written in not the prettiest idiosyncratic Python.

https://github.com/jbchouinard/sixteen/

Re: Writing Your Own Programming Language

#59
post #55

Peter Norvig's "(How to Write a (Lisp) Interpreter (in Python))" ( http://norvig.com/lispy.html ) covers a superset of this material and makes more sense, and actually has a portable implementation you can run yourself. If you're going to do this, use Norvig as a guide.

When I think about writing my own language, I think of something with as few parenthesis as possible and all the examples use Lisp.

Have you considered that the two might be correlated?
Post reply on HN