Skip to content

Commit

Permalink
Handled functions return statement, input params WIP, literals now re…
Browse files Browse the repository at this point in the history
…turn symbols when parsed by the visitor
  • Loading branch information
GabrieleMessina authored Feb 11, 2024
1 parent 1428cd6 commit 2d3480f
Show file tree
Hide file tree
Showing 14 changed files with 968 additions and 793 deletions.
31 changes: 23 additions & 8 deletions specification/grammar/grammar_tests.qut
Original file line number Diff line number Diff line change
Expand Up @@ -238,29 +238,44 @@ else{
// }
}*/

//Functions tests
{
//Functions tests I
/*{
//What we want to achieve:
// qustring entireBook;
// int resultStartIndex = entireBook.find("some string.");
// or
// int resultStartIndex = find(entireBook, "some string.");

bool A (int c = 10){ //TODO: handle return type and return statement
bool a = true;
bool a = false;
print a;
print c;
return a;
print a;
}

void B ()
A();
bool z = A();
print z;

B();
void B (){
bool z = A();
print z;
}

B();
}*/

/*bool A (int c = 10, bool aa){ //TODO: right now everything should has a default value, so we can handle params with no default value after those with default values, but we should think about it because this could be problematic.
//Functions tests II
{
bool A (int c = 10, bool aa){ //TODO: right now everything should has a default value, so we can handle params with no default value after those with default values, but we should think about it because this could be problematic.
bool a = true;
print a;
print c;
}*/
print aa;
}

int z = 1;
int zz = 1;
A(z, zz);
//print A(); //TODO: invali, A() is expression while print works on term.
}
2 changes: 2 additions & 0 deletions specification/grammar/qutes_lexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ STRING_TYPE : 'string' ;
QUBIT_TYPE : 'qubit' ;
QUINT_TYPE : 'quint' ;
FLOAT_TYPE : 'float' ;
VOID_TYPE : 'void' ;
RETURN : 'return' ;
ADD : '+' ;
SUB : '-' ;
NOT : 'not' ;
Expand Down
17 changes: 10 additions & 7 deletions specification/grammar/qutes_parser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@ statement
| variableType functionName ROUND_PARENTHESIS_OPEN functionDeclarationParams? ROUND_PARENTHESIS_CLOSE statement #FunctionStatement
| variableDeclaration END_OF_STATEMENT #DeclarationStatement
| qualifiedName ASSIGN expr END_OF_STATEMENT #AssignmentStatement
| RETURN expr END_OF_STATEMENT #ReturnStatement
| expr END_OF_STATEMENT #ExpressionStatement
| END_OF_STATEMENT #EmptyStatement
;

variableDeclaration
: variableType variableName (ASSIGN expr)? //TODO: rename variableName to variableDeclarationName
;

block
: CURLY_PARENTHESIS_OPEN statement* CURLY_PARENTHESIS_CLOSE
;

functionDeclarationParams
: variableDeclaration (COMMA functionDeclarationParams)*
: variableDeclaration (COMMA functionDeclarationParams)?
;

//TODO: rename variableName to variableDeclarationName
variableDeclaration
: variableType variableName (ASSIGN expr)?
;

functionCallParams
: qualifiedName (COMMA functionCallParams)*
: qualifiedName (COMMA functionCallParams)?
;

expr
Expand All @@ -47,7 +49,7 @@ expr
| parenExpr
;

functionCall
functionCall //Function name should be a qualifiedName here.
: functionName ROUND_PARENTHESIS_OPEN functionCallParams? ROUND_PARENTHESIS_CLOSE
;

Expand Down Expand Up @@ -79,6 +81,7 @@ type
| STRING_TYPE
| QUBIT_TYPE
| QUINT_TYPE
| VOID_TYPE
;

variableType
Expand Down
465 changes: 238 additions & 227 deletions src/qutes_antlr/qutes_lexer.py

Large diffs are not rendered by default.

142 changes: 73 additions & 69 deletions src/qutes_antlr/qutes_lexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,82 @@ STRING_TYPE=3
QUBIT_TYPE=4
QUINT_TYPE=5
FLOAT_TYPE=6
ADD=7
SUB=8
NOT=9
PAULIY=10
PAULIZ=11
HADAMARD=12
MEASURE=13
PRINT=14
EQUAL=15
GREATER=16
GREATEREQUAL=17
LOWER=18
LOWEREQUAL=19
ASSIGN=20
END_OF_STATEMENT=21
VAR_STATEMENT=22
IF_STATEMENT=23
ELSE_STATEMENT=24
WHILE_STATEMENT=25
DO_STATEMENT=26
CURLY_PARENTHESIS_OPEN=27
CURLY_PARENTHESIS_CLOSE=28
ROUND_PARENTHESIS_OPEN=29
ROUND_PARENTHESIS_CLOSE=30
SQUARE_PARENTHESIS_OPEN=31
SQUARE_PARENTHESIS_CLOSE=32
DOT=33
STRING_ENCLOSURE=34
COMMA=35
BOOL_LITERAL=36
INT_LITERAL=37
FLOAT_LITERAL=38
HEX_LITERAL=39
BIN_LITERAL=40
QUBIT_LITERAL=41
QUINT_LITERAL=42
SYMBOL_LITERAL=43
STRING_LITERAL=44
WS=45
NEWLINE=46
VOID_TYPE=7
RETURN=8
ADD=9
SUB=10
NOT=11
PAULIY=12
PAULIZ=13
HADAMARD=14
MEASURE=15
PRINT=16
EQUAL=17
GREATER=18
GREATEREQUAL=19
LOWER=20
LOWEREQUAL=21
ASSIGN=22
END_OF_STATEMENT=23
VAR_STATEMENT=24
IF_STATEMENT=25
ELSE_STATEMENT=26
WHILE_STATEMENT=27
DO_STATEMENT=28
CURLY_PARENTHESIS_OPEN=29
CURLY_PARENTHESIS_CLOSE=30
ROUND_PARENTHESIS_OPEN=31
ROUND_PARENTHESIS_CLOSE=32
SQUARE_PARENTHESIS_OPEN=33
SQUARE_PARENTHESIS_CLOSE=34
DOT=35
STRING_ENCLOSURE=36
COMMA=37
BOOL_LITERAL=38
INT_LITERAL=39
FLOAT_LITERAL=40
HEX_LITERAL=41
BIN_LITERAL=42
QUBIT_LITERAL=43
QUINT_LITERAL=44
SYMBOL_LITERAL=45
STRING_LITERAL=46
WS=47
NEWLINE=48
'int'=1
'bool'=2
'string'=3
'qubit'=4
'quint'=5
'float'=6
'+'=7
'-'=8
'not'=9
'pauliy'=10
'pauliz'=11
'hadamard'=12
'measure'=13
'print'=14
'=='=15
'>'=16
'>='=17
'<'=18
'<='=19
'='=20
';'=21
'var'=22
'if'=23
'else'=24
'while'=25
'do'=26
'{'=27
'}'=28
'('=29
')'=30
'['=31
']'=32
'.'=33
'"'=34
','=35
'void'=7
'return'=8
'+'=9
'-'=10
'not'=11
'pauliy'=12
'pauliz'=13
'hadamard'=14
'measure'=15
'print'=16
'=='=17
'>'=18
'>='=19
'<'=20
'<='=21
'='=22
';'=23
'var'=24
'if'=25
'else'=26
'while'=27
'do'=28
'{'=29
'}'=30
'('=31
')'=32
'['=33
']'=34
'.'=35
'"'=36
','=37
Loading

0 comments on commit 2d3480f

Please sign in to comment.