delta7-138 / Hilden

Language frontend for the language Hilden
7 stars 4 forks source link

HILDEN

Hilden is a new language which implements blocked scoping written in C++.

Check out test/ for examples

Run

  1. Run make from the root of this repository
  2. Run an example ./hilden test/fact.hl

Language Design and Implementation

Lexer

lex/lexer.cpp

The lexer converts the text code into a list of tokens. Uses state machines to output token type.

The output of the lexer is passed to the Parser

Parser

parse/op_parse.cpp

The parser takes the ordered token list from the lexer and uses it to generate an Abstract Syntax Tree.

The parser works recursively. Everytime an open block is encountered, the parser is called recursively, which returns a abstract syntax tree node.

When the parser encounters a binary expression, it passes on the parsing to a parse_binary which mutually recurses with the main parse function (since a binary expression may have an expression within it). The binary parser uses the Shunting Yard Algorithm for parsing.

The generated AST is passed to the Semantic Analyser

Semantic Analysis

semantics/environment.cpp

The semantic analyser handles environments. Scoped declarations are local to the scope. Any declaration outside the scope is accessible inside but not vice versa.

The SA also evaluates the AST. Uses a modification of depth first tree traversal, looks at the node type, does typechecking and evaluates accrodingly.

Pass the --tree flag while executing Hilden code to print out the generated AST Pass the --lex flag while executing Hilden code to print out the token list Pass the --all flag while executing Hilden code to print out both the token list and the AST generated

For lexing:

from the root directory run the following commands

lex lex/lexer.l g++ lex.yy.c lex/scanner.cpp main.cpp ./a.out <a .hl file>

it should print out the correct token list