coolgo

command module
v0.0.0-...-a7f65dd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

test-badge

CoolGo: Cool Compiler using Go Language

Context Free Grammar

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)* | epsilon

IfExpression -> 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] Expression

Identifier -> [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:

let x = 2 * 3 + 4;
Output
1. Lexical Analysis
{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
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 😄 🎉

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL