Skip to content

packlnd/mini-compiler

Repository files navigation

mini-compiler

File extension: .patrik

Example usage:

File: hello.patrik:

func main:
    print 1+2
python compiler.py hello.patrik
output: hello.asm

nasm -f macho hello.asm
output: hello.o

ld hello.o
output: 3

Grammar:

Goal = MainMehtod
MainMethod = func main : INDENT (Statement)* DEDENT
Statement = print Expression
Expression = Integer + Integer
Integer

Example programs

1.

func main:
    print 1
    print 1+2

Tokens:

[FUNC, MAIN, COLON, INDENT, PRINT, INT(1), PRINT, INT(1), PLUS, INT(2), DEDENT]

Parse tree:

Goal(
    MainMethod([
        PrintStatement(
            IntExpression(1)
        ),
        PrintStatement(
            AddExpression(1, 2)
        ),
    ])
)

Assembly:

Output:

1
3

2.

func main:
    print 39

Tokens:

[FUNC, MAIN, COLON, INDENT, PRINT, INT(39), DEDENT]

Parse tree:

Goal(
    MainMethod([
        PrintStatement(
            IntExpression(39)
        ),
    ])
)

Output:

39

Ideas

http://www.csc.kth.se/~phaller/compilers/labs/lab7.html

About

🐲 a compiler for a small python-like language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published