Skip to content

jakseluz/GreekBase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GREEK BASE

Authors:

Project description:

  • Own programming language „GreekBase”.
  • The aim for this project is to create an experimental programming language inspired by Ada's rules and syntax for Compilation Theory and Compilers classes. We want to change it a bit and make it more convenient with some small enhancements.
  • Implemented in Python using ANTLR4.
  • Language compiled to C.

Target group

  • This project aims to cater to people who are interested in Ada, want a quick setup with simple GUI. The only thing you need is a prior knowledge of C language (or just how to compile it further), so it's an interesting programming trivia for example for people on the second semester of ISI on AGH. You can explore GB examples that are stored in inputs folder :)

Project structure

GreekBase
├── antlr/
│   ├── GreekBaseLexer.g4             ← grammar for tokens
│   ├── GreekBaseParser.g4            ← grammar for parser
│   └── generated/                    ← folder containing files generated by ANTLR
│       ├── GreekBaseLexer.py         ← generated lexer (token scanner)
│       ├── GreekBaseParser.py        ← generated parser
│       └── GreekBaseParserVisitor.py ← generated visitor (used by AST tree builder)
├── src/
│   ├── ast_builder.py                ← used for generating AST tree from ANTLR tree
│   ├── astGreek.py                   ← AST tree node classes
│   ├── error_listener.py             ← syntax ANTLR errors handling etc.
│   ├── semantic_checker.py           ← semantic errors handling etc.
│   ├── codegen.py                    ← C (from AST) code generator
│   ├── compiler_core.py              ← logic of compiler used in main and gui
│   └── gui.py                        ← graphic interface for compiler
├── inputs/                           ← example source files
├── output/                           ← generated C source files
├── requirements.txt                  ← list of packages required to run the compiler (compatible with pip)
├── run.sh                            ← bash (Linux etc.) script for generating ANTLR files
├── run.bat                           ← bat (Windows) equivalent of the above one
└── main.py                           ← main file that starts the compiler

Reguirements

Project uses python packages e.g. for GUI and colouring. They can be installed (or overviewed: ./requirements.txt):

pip install -r ./requirements.txt

Details

1. Grammar

Lexer's tokens

Parser's grammar

2. Tutorial

  1. Please run ./run.sh in order to generate the parser and its tools needed later, but they should be already generated in ./antlr/generated/.

Example input files are in ./inputs directory and they have .gb extension (.adan also supported to honor the archetype). For now, printing the Abstract Syntax Tree is possible, as well as receiving syntax errors, semantic errors and warnings in the console or GUI. Output C source file is (by default) saved to ./output/ directory if there are no [Error]s.

  1. You can use the graphical interface by running the ./main.py file without any command arguments.

  2. For specific information about the compiler usage in the command line interface, you can run:

python main.py --help
  1. Compile the GreekBase language source file by typing its path as the first argument:
python main.py example.gb
  1. There is an option: -o output_path:
python main.py example.gb -o my_path/my_name.c
  1. If there is a need for an abstract syntax tree program object print:
python main.py example.gb -o my_path/my_name.c --ast

3. Example

  1. Source file (.gb):
x : int := 5;
y : int := 10;

function Dodaj()
is
begin
    print y;
end function;

if x < y then
    x := x + 1;
else y := y - 1;
end if;

/* bezsensowna pętla - jak cały ten program
- nie ma
ABSOLUTNIE żadnego sensu
*/
while x < y and x /= y {
    print x;
    x := x + 1;
    Dodaj();
    --coś tutaj napisałem
}
Dodaj();
print x;
print y;
  1. AST tree:
Program(line=1, column=0, statements=[VariableDeclaration(line=1, column=0, varType=<class 'int'>, id='x', varValue=IntLiteral(line=1, column=11, value=5)), VariableDeclaration(line=2, column=0, varType=<class 'int'>, id='y', varValue=IntLiteral(line=2, column=11, value=10)), FunctionDeclaration(line=4, column=0, name='Dodaj', parameters=[], return_type=None, statements=[PrintStatement(line=7, column=4, value='y')]), IfStatement(line=10, column=0, condition=Condition(line=10, column=3, left=Identifier(line=10, column=3, value='x', type=None), operator='<', right=Identifier(line=10, column=7, value='y', type=None)), then_branch=[Assignment(line=11, column=4, id='x', value=AdditionOperator(line=11, column=9, left=Identifier(line=11, column=9, value='x', type=None), operator='+', right=IntLiteral(line=11, column=13, value=1)))], else_branch=[Assignment(line=12, column=5, id='y', value=AdditionOperator(line=12, column=10, left=Identifier(line=12, column=10, value='y', type=None), operator='-', right=IntLiteral(line=12, column=14, value=1)))]), LoopStatement(line=19, column=0, condition=Condition(line=19, column=6, left=Condition(line=19, column=6, left=Identifier(line=19, column=6, value='x', type=None), operator='<', right=Identifier(line=19, column=10, value='y', type=None)), operator='and', right=Condition(line=19, column=16, left=Identifier(line=19, column=16, value='x', type=None), operator='/=', right=Identifier(line=19, column=21, value='y', type=None))), then=[PrintStatement(line=20, column=4, value='x'), Assignment(line=21, column=4, id='x', value=AdditionOperator(line=21, column=9, left=Identifier(line=21, column=9, value='x', type=None), operator='+', right=IntLiteral(line=21, column=13, value=1))), FunctionCall(line=22, column=4, name='Dodaj', parameters=[])]), FunctionCall(line=25, column=0, name='Dodaj', parameters=[]), PrintStatement(line=26, column=0, value='x'), PrintStatement(line=27, column=0, value='y')])
  1. Semantic check:

(not for this example, here everything is fine)

  1. Code in C:
#include <stdio.h>
void Dodaj(){
printf("%d", y);
}
int main(){
int x = 5;
int y = 10;

if(x < y){
x = x + 1;
}else{ 
y = y - 1;
}
while(x < y && x != y){
printf("%d", x);
x = x + 1;
Dodaj();
}
Dodaj();
printf("%d", x);
printf("%d", y);
return 0;
}

4. Features:

  • bulit-in types: int, float, char, simplified string, bool
  • if conditions
  • built-in arithmetic: multiplication, division, modulo, addition, substraction, bracketing
  • variables and their declarations
  • loops (while)
  • assignments
  • print statements
  • functions
  • etc.

About

Own programming language „GreekBase”

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages