|
|
|
|
|
by loup-vaillant
4421 days ago
|
|
Start with untyped lambda calculus, augmented with numbers, and addition. That's what I did. Such a language is dead simple: you only have four kinds of expressions: numbers, additions, functions, and function calls. Write a parser and a printer (a pretty printer would be best, but you don't have to). Use one to test the other. Then write an interpreter, who takes an expression as input, evaluates it, and spits out a number. The whole thing should fit in a couple hundred lines, tops. The only real difficulty is to evaluate function calls. The rest is only a matter of catching runtime errors (like trying to add a number and a function). To properly implement your language, you may have to learn about closures and lexical scoping, though there are tricks to sidestep the issue. To test your language, check out the Y and Z combinators here https://en.wikipedia.org/wiki/Fixed_point_combinator and use the Z combinator to implement the factorial function. The next step is to flesh out your language a bit. I suggest you augment it with "let" bindings first: it's just syntax sugar over lambdas: you change your parser, but you don't have to touch the interpreter. |
|