munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
9.02k stars 1.06k forks source link

Suggestion: Information on Static Type Checking #396

Closed cm1776 closed 5 years ago

cm1776 commented 5 years ago

Could you please provide information (probably in the appendix) on how to implement static type checking? Links to tutorials, books, and videos on the same topic would be most welcome.

egordorichev commented 5 years ago

I'd link #221

If you are looking into static type checking, you will need AST tree asap, no matter if you are using java/c version of the compiler. If you are following the c path, you still will need it. Then, throw out of the window the resolver code, if you are still using, if not, write an AST visitor. How I did it, was that expression visiting methods return the type (string) of the expression, that they visited (for example, visitNumberExpression() returns "double"), in statement expressions you compare the types with expected types. As well as that, you are going to need to check, that all variables are defined, but it's a much more simple thing, that it sounds.

Here is my C version of the resolver (please note, that the language is not lox, but it evolved from the book)

munificent commented 5 years ago

Static types are a big topic and out of scope for the book. It's going to be plenty big enough as it is!