Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ahmedheakl/coolgo
Cool Compiler using GoLang
https://github.com/ahmedheakl/coolgo
Last synced: about 2 months ago
JSON representation
Cool Compiler using GoLang
- Host: GitHub
- URL: https://github.com/ahmedheakl/coolgo
- Owner: ahmedheakl
- License: apache-2.0
- Created: 2023-05-05T20:43:36.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-05-09T08:17:46.000Z (over 1 year ago)
- Last Synced: 2024-06-19T20:57:59.306Z (7 months ago)
- Language: Go
- Size: 1.1 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![test-badge](https://github.com/ahmedheakl/coolgo/workflows/test/badge.svg)
# CoolGo: Cool Compiler using Go Language## Context Free Grammar
```ebnf
Program -> (Statment)*Statment -> LetStatement | ReturnStatement | ExpressionStatement
LetStatement -> LET Identifier ASSIGN Expression SEMICOLON?
ReturnStatement -> RETURN Expression
ExpressionStatement -> Expression
Expression -> FunctionExpression |
CallExpression |
IfExpression |
PrefixExpression |
InfixExpression |
TRUE |
FALSE |
(Expression)FunctionExpression -> FUNCTION LPAREN Parameters RPAREN LBRACE BlockStatment RBRACE
Parameters -> Identifier (COMMA Identifier)* | epsilon
BlockStatement -> (Statement)*CallExpression -> Idenitifer RPAREN Arguments LPAREN
Arguments -> Expression (COMMA Expression)* | epsilonIfExpression -> IF LPAREN Expression RPAREN LBRACE BlockStatement RBRACE (ELSE LBRACE BlockStatement RBRACE)?
PrefixExpression -> INT | Identifier | [MINUS BANG] Expression
InfixExpression -> Expression [PLUS MINUS ASTERISK SLASH EQ NOT_EQ LT GT] ExpressionIdentifier -> [a-zA-Z][a-zA-Z]*
INT -> [1-9][0-9]*// Keywords
LET -> "let"
FUNCTION -> "fn"
RETURN -> "return"
IF -> "if"
ELSE -> "else"
TRUE -> "true"
FALSE -> "false"// Operators
EQ -> "=="
ASSIGN -> "="
LT -> "<"
GT -> ">"
NOT_EQ -> "!="
PLUS -> "+"
MINUS -> "-"
ASTERISK -> "*"
SLASH -> "/"
BANG -> "!"// Delimiters
COMMA -> ","
SEMICOLON -> ";"
LPAREN -> "("
RPAREN -> ")"
LBRACE -> "{"
RBRACE -> "}"WS -> [\t\r\n ]
```## Parsing
The parsing is done in top-down parsing, left-to-right fashion.
Examples:
```rust
let x = 2 * 3 + 4;
```### Output
#### 1. Lexical Analysis
```bash
{Type:LET Literal:let}
{Type:IDENT Literal:x}
{Type:= Literal:=}
{Type:INT Literal:2}
{Type:* Literal:*}
{Type:INT Literal:3}
{Type:+ Literal:+}
{Type:INT Literal:4}
{Type:; Literal:;}
```#### 2. Parsing
```rust
let x = ((2 * 3) + 4);
BEGIN ParseProgram
BEGIN parseStatement
BEGIN parseLetStatement
BEGIN parseExpression
BEGIN parseIntegerLiteral
BEGIN parseInfixExpression
BEGIN parseExpression
BEGIN parseIntegerLiteral
BEGIN parseInfixExpression
BEGIN parseExpression
BEGIN parseIntegerLiteral
```## To run the project
1. Make sure you have go installed
2. Run `go run .`, and you're good to go :smile: :tada: