-
Notifications
You must be signed in to change notification settings - Fork 4
Home
Lang was a delve into creating the necessary libraries for implementing a compiler on some language. As I wrote the initial rules of how the language should look, I discovered the lisp-style function calls were simpler to parse and I decided to implement a sort of lisp-like language lexer, parser, and optimizer, eventually creating an interpreter, basilisk.
This section describes the basic makeup of the libraries.
lang/lexer is a lexer library which contains one exported function lexer.Lex(), which takes a string of source code and returns a channel. It returns tokens along this channel.
lang/token contains the token.Token type and the token.ItemType type. It contains a list of constants which are all enumerated ItemTypes. It also contains a few functions to identify what type subset a token falls in.
- token.IsKeyword takes a token and returns a bool
- token.Keyword takes a string and returns a bool
- token.IsConstant takes a token and returns a bool
- token.Constant takes a string and returns a bool
lang/parser is a parser library that generates a parse tree and returns it along a channel. It contains one exported function parser.Parse(), which takes a channel (the one lexer.Lex returned), as well as a chan *parser.Tree which it returns the parse tree on.
lang/optim is a optimization library that generates an Abstract Syntax Tree, it then uses its internal functions to semi-recursively evaluate the value of the pieces of the AST it can evaluate. Unknown operations, undefined variables, etc. are deferred until the tree is as evaluated as it can become, and then it returns a *optim.Tree, which has a String() interface, so it is printable.