Skip to content

Commit

Permalink
Adaugă suport pentru if și while la tema 2 TC
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielMajeri committed Apr 6, 2021
1 parent 36426c2 commit e2e91b9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
9 changes: 8 additions & 1 deletion tc/tema2/HelloWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ public static void main(String[] args) {

String myString = "Test";
int x = 5;
System.out.println(myString);
if (x > 3) {
System.out.println(myString);
}

System.out.println(HelperClass.compute(convert(x)));

while (x < 10) {
x = x + 1;
System.out.println("Hello!");
}
}

// This is a private static method
Expand Down
6 changes: 6 additions & 0 deletions tc/tema2/java-lexer.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ private return yy::parser::make_PRIVATE_KEYWORD();
final return yy::parser::make_FINAL_KEYWORD();
static return yy::parser::make_STATIC_KEYWORD();

if return yy::parser::make_IF_KEYWORD();
while return yy::parser::make_WHILE_KEYWORD();
return return yy::parser::make_RETURN_KEYWORD();

"(" return yy::parser::make_OPEN_PARENS();
Expand All @@ -54,6 +56,10 @@ return return yy::parser::make_RETURN_KEYWORD();
"," return yy::parser::make_COMMA();
";" return yy::parser::make_SEMICOLON();
"." return yy::parser::make_DOT();
"+" return yy::parser::make_PLUS();
"-" return yy::parser::make_MINUS();
"<" return yy::parser::make_LESS_THAN();
">" return yy::parser::make_GREATER_THAN();

{INTEGER} return yy::parser::make_NUMBER(std::stoi(yytext));

Expand Down
46 changes: 41 additions & 5 deletions tc/tema2/java-parser.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
%token FINAL_KEYWORD;
%token STATIC_KEYWORD;

%token IF_KEYWORD;
%token WHILE_KEYWORD;
%token RETURN_KEYWORD;

%token OPEN_PARENS;
Expand All @@ -43,6 +45,12 @@
%token COMMA;
%token SEMICOLON;
%token DOT;
%token PLUS;
%token MINUS;
%token LESS_THAN;
%token GREATER_THAN;

%left PLUS MINUS;

%token <std::string> IDENTIFIER;
%token <int> NUMBER;
Expand Down Expand Up @@ -97,8 +105,28 @@ statement:
variable_declaration
| assignment
| method_call SEMICOLON
| if_statement
| while_statement
| return_statement

if_statement:
IF_KEYWORD condition block
{
std::cout << "if block\n";
}

while_statement:
WHILE_KEYWORD condition block
{
std::cout << "while block\n";
}

condition:
OPEN_PARENS expression CLOSE_PARENS

block:
OPEN_BRACKET statements CLOSE_BRACKET

variable_declaration:
type_identifier name optional_initializer SEMICOLON
{
Expand All @@ -107,6 +135,9 @@ variable_declaration:

assignment:
name initializer SEMICOLON
{
std::cout << "Assigning to variable " << $1 << '\n';
}

method_call:
method_reference
Expand All @@ -127,9 +158,17 @@ actual_parameters:
value:
name | expression

%nterm expression;
expression:
literal
expression binary_operator expression
| method_call
| name
| literal

%nterm binary_operator;
binary_operator:
PLUS | MINUS
| LESS_THAN | GREATER_THAN | EQUALS

%nterm <std::string> method_reference;
method_reference:
Expand Down Expand Up @@ -181,10 +220,7 @@ optional_initializer:
%empty | initializer

initializer:
EQUALS literal
{
std::cout << "Initializing to " << $2 << '\n';
}
EQUALS expression

%nterm <std::string> literal;
literal:
Expand Down

0 comments on commit e2e91b9

Please sign in to comment.