From eb63643b01259ff96e9bdb2e8621ee4bb0d263b7 Mon Sep 17 00:00:00 2001 From: Krishna-13-cyber Date: Wed, 27 Apr 2022 20:48:15 +0530 Subject: [PATCH 1/5] multiplication-division assign operators added --- include/ast.h | 28 ++++ include/code_printer.h | 4 + src/ast.c | 37 ++++- src/code_printer.c | 28 ++++ src/lexer.l | 9 +- src/parser.y | 156 ++++++++++++++++++- test.py | 2 +- tests/run_anywhere/mul-div-assign.sim | 4 + tests/run_anywhere/mul-div-assign.sim.output | 1 + 9 files changed, 264 insertions(+), 5 deletions(-) create mode 100644 tests/run_anywhere/mul-div-assign.sim create mode 100644 tests/run_anywhere/mul-div-assign.sim.output diff --git a/include/ast.h b/include/ast.h index 3b90a36..0be9ae3 100644 --- a/include/ast.h +++ b/include/ast.h @@ -14,6 +14,8 @@ #define AST_NODE_DECLARATION 6 #define AST_NODE_ARRAY_DECLARATION 2000 #define AST_NODE_ASSIGNMENT 7 +#define AST_NODE_MUL_ASSIGNMENT 6007 +#define AST_NODE_DIV_ASSIGNMENT 6008 #define AST_NODE_ARRAY_ASSIGNMENT 2001 #define AST_NODE_ARRAY_ACCESS 2002 #define AST_NODE_ARITHMETIC_EXP 8 @@ -59,6 +61,8 @@ #define AST_OPR_LGL_OR 43 // or #define AST_OPR_ASSIGNMENT 44 // := +#define AST_OPR_MUL_ASSIGNMENT 6009 // *= +#define AST_OPR_DIV_ASSIGNMENT 6010 // /= #define AST_CONST_INT 45 // INT CONSTANT #define AST_CONST_BOOL 46 // BOOL CONSTANT @@ -100,6 +104,8 @@ struct ast_node_compound_statement; struct ast_node_declaration; struct ast_node_array_declaration; struct ast_node_assignment; +struct ast_node_mul_assignment; +struct ast_node_div_assignment; struct ast_node_array_assignment; struct ast_node_array_access; struct ast_node_expression; @@ -125,6 +131,8 @@ typedef struct ast_node_compound_statement ast_node_compound_statement; typedef struct ast_node_declaration ast_node_declaration; typedef struct ast_node_array_declaration ast_node_array_declaration; typedef struct ast_node_assignment ast_node_assignment; +typedef struct ast_node_mul_assignment ast_node_mul_assignment; +typedef struct ast_node_div_assignment ast_node_div_assignment; typedef struct ast_node_array_assignment ast_node_array_assignment; typedef struct ast_node_array_access ast_node_array_access; typedef struct ast_node_expression ast_node_expression; @@ -160,6 +168,8 @@ struct ast_node ast_node_declaration *declaration; ast_node_array_declaration *array_declaration; ast_node_assignment *assignment; + ast_node_mul_assignment *mul_assignment; + ast_node_div_assignment *div_assignment; ast_node_array_assignment *array_assignment; ast_node_conditional_if *if_else; ast_node_loop_for *loop_for; @@ -205,6 +215,22 @@ struct ast_node_array_declaration ast_node_expression *expression; }; + struct ast_node_mul_assignment +{ + int node_type; + + sym_ptr symbol_entry; + ast_node_expression *expression; +}; + + struct ast_node_div_assignment +{ + int node_type; + + sym_ptr symbol_entry; + ast_node_expression *expression; +}; + struct ast_node_array_assignment { int node_type; @@ -363,6 +389,8 @@ ast_node_compound_statement *add_compound_statement_node(ast_node_compound_state ast_node_declaration *create_declaration_node(sym_ptr symbol, ast_node_expression *exp); ast_node_array_declaration *create_array_declaration_node(sym_ptr symbol, ast_node_expression *size, char *initial_string); ast_node_assignment *create_assignment_node(sym_ptr symbol, ast_node_expression *exp); +ast_node_mul_assignment *create_mul_assignment_node(sym_ptr symbol, ast_node_expression *exp); +ast_node_div_assignment *create_div_assignment_node(sym_ptr symbol, ast_node_expression *exp); ast_node_array_assignment *create_array_assignment_node(sym_ptr symbol, ast_node_expression *index, ast_node_expression *exp); ast_node_array_access *create_array_access_node(sym_ptr symbol, ast_node_expression *index); ast_node_expression *create_expression_node(int node_type, int opt, int value, ast_node *left, ast_node *right); diff --git a/include/code_printer.h b/include/code_printer.h index 189f5e6..cd9dee8 100644 --- a/include/code_printer.h +++ b/include/code_printer.h @@ -32,6 +32,8 @@ #define _OPR_LGL_OR " | " #define _OPR_ASSIGNMENT " = " +#define _OPR_MUL_ASSIGNMENT " *= " +#define _OPR_DIV_ASSIGNMENT " /= " #define _DT_INT_ "int" #define _DT_VOID_ "void" @@ -54,6 +56,8 @@ void ast_compound_statement_printer(ast_node_compound_statement *cmpd_stmt, FILE void ast_declaration_printer(ast_node_declaration *decl, FILE* handle); void ast_array_declaration_printer(ast_node_array_declaration *decl, FILE* handle); void ast_assignment_printer(ast_node_assignment *assg, FILE* handle); +void ast_mul_assignment_printer(ast_node_mul_assignment *mul_assg, FILE* handle); +void ast_div_assignment_printer(ast_node_div_assignment *div_assg, FILE* handle); void ast_array_assignment_printer(ast_node_array_assignment *assign, FILE *handle); void ast_array_access_printer(ast_node_array_access *access, FILE* handle); void ast_expression_printer(ast_node_expression *node, FILE* handle); diff --git a/src/ast.c b/src/ast.c index 3fb7bb8..e523124 100644 --- a/src/ast.c +++ b/src/ast.c @@ -40,6 +40,14 @@ ast_node_statements *create_statement_node(int node_type, void *child) stmt->child_nodes.assignment = child; break; + case AST_NODE_MUL_ASSIGNMENT: + stmt->child_nodes.mul_assignment = child; + break; + + case AST_NODE_DIV_ASSIGNMENT: + stmt->child_nodes.div_assignment = child; + break; + case AST_NODE_ARRAY_ASSIGNMENT: stmt->child_nodes.array_assignment = child; break; @@ -143,6 +151,27 @@ ast_node_assignment *create_assignment_node(sym_ptr symbol, ast_node_expression return assgn; } +ast_node_mul_assignment *create_mul_assignment_node(sym_ptr symbol, ast_node_expression *exp) +{ + ast_node_mul_assignment *mul_assgn = (ast_node_mul_assignment*)malloc(sizeof(ast_node_mul_assignment)); + + mul_assgn->node_type = AST_NODE_MUL_ASSIGNMENT; + mul_assgn->expression = exp; + mul_assgn->symbol_entry = symbol; + + return mul_assgn; +} + +ast_node_div_assignment *create_div_assignment_node(sym_ptr symbol, ast_node_expression *exp) +{ + ast_node_div_assignment *div_assgn = (ast_node_div_assignment*)malloc(sizeof(ast_node_div_assignment)); + + div_assgn->node_type = AST_NODE_DIV_ASSIGNMENT; + div_assgn->expression = exp; + div_assgn->symbol_entry = symbol; + + return div_assgn; +} ast_node_array_assignment *create_array_assignment_node(sym_ptr symbol, ast_node_expression *index, ast_node_expression *exp) { @@ -598,8 +627,12 @@ void ast_node_type(int node_type) printf("ast array declaration"); break; - case AST_NODE_ASSIGNMENT: - printf("ast assignment"); + case AST_NODE_DIV_ASSIGNMENT: + printf("ast division assignment"); + break; + + case AST_NODE_MUL_ASSIGNMENT: + printf("ast multiplication assignment"); break; case AST_NODE_ARRAY_ASSIGNMENT: diff --git a/src/code_printer.c b/src/code_printer.c index 7ff71b0..70a076b 100644 --- a/src/code_printer.c +++ b/src/code_printer.c @@ -29,6 +29,14 @@ void ast_compound_statement_printer(ast_node_compound_statement *cmpd_stmt, FILE ast_assignment_printer(((ast_node_statements*)temp)->child_nodes.assignment, handle); break; + case AST_NODE_MUL_ASSIGNMENT: + ast_mul_assignment_printer(((ast_node_statements*)temp)->child_nodes.mul_assignment, handle); + break; + + case AST_NODE_DIV_ASSIGNMENT: + ast_div_assignment_printer(((ast_node_statements*)temp)->child_nodes.div_assignment, handle); + break; + case AST_NODE_ARRAY_ASSIGNMENT: ast_array_assignment_printer(((ast_node_statements*)temp)->child_nodes.array_assignment, handle); break; @@ -174,6 +182,26 @@ void ast_assignment_printer(ast_node_assignment *assg, FILE* handle) } +void ast_mul_assignment_printer(ast_node_mul_assignment *assg, FILE* handle) +{ + if (assg != NULL && handle != NULL) + { + fprintf(handle, "\t%s *= ", assg->symbol_entry->identifier); + ast_expression_printer(assg->expression, handle); + fprintf(handle, "%s", ";\n"); + } + +} +void ast_div_assignment_printer(ast_node_div_assignment *assg, FILE* handle) +{ + if (assg != NULL && handle != NULL) + { + fprintf(handle, "\t%s /= ", assg->symbol_entry->identifier); + ast_expression_printer(assg->expression, handle); + fprintf(handle, "%s", ";\n"); + } + +} void ast_array_assignment_printer(ast_node_array_assignment *assign, FILE* handle) { if (assign != NULL && handle != NULL) diff --git a/src/lexer.l b/src/lexer.l index d13a1a7..e9c0945 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -35,6 +35,8 @@ bitwise_operators ("~"|"&"|"|"|">>"|"<<") logical_operators ("not"|"and"|"or") function ("return"|"void"|"def") assignment_operator ([:][=]) +mul_assignment_operator ([*][=]) +div_assignment_operator ([/][=]) io ("digital_read"|"digital_write"|"delay"|"pwm"|"start_counter"|"stop_counter"|"read_counter"|"init_message_channel"|"receive_message"|"send_message"|"send_int"|"send_char"|"send_bool"|"send_ints"|"send_chars"|"send_bools") print ("print"|"println") identifier ([a-zA-Z_][a-zA-Z0-9_]*) @@ -322,7 +324,12 @@ comma ([,]) {assignment_operator} { return OPR_ASSIGNMENT; } - +{mul_assignment_operator} { + return OPR_MUL_ASSIGNMENT; +} +{div_assignment_operator} { + return OPR_DIV_ASSIGNMENT; +} {io} { if (!strcmp(yytext, "digital_write")) { diff --git a/src/parser.y b/src/parser.y index 29bfe90..63606a9 100644 --- a/src/parser.y +++ b/src/parser.y @@ -33,6 +33,8 @@ ast_node *ast = NULL; struct ast_node_declaration *declaration; struct ast_node_array_declaration *array_declaration; struct ast_node_assignment *assignment; + struct ast_node_mul_assignment *mul_assignment; + struct ast_node_div_assignment *div_assignment; struct ast_node_array_assignment *array_assignment; struct ast_node_array_access *array_access; struct ast_node_expression *expression; @@ -75,6 +77,8 @@ ast_node *ast = NULL; %right OPR_BW_NOT OPR_LGL_NOT %token OPR_ASSIGNMENT +%token OPR_MUL_ASSIGNMENT +%token OPR_DIV_ASSIGNMENT %token SEMICOLON COLON COMMA @@ -106,6 +110,8 @@ ast_node *ast = NULL; %type declaration declaration_assignment %type array_declaration array_declaration_assignment %type assignment +%type mul_assignment +%type div_assignment %type arithmetic_expression boolean_expression relational_expression logical_expression return_statement function_call_datatypes %type range_expression %type array_assignment @@ -183,6 +189,12 @@ statement: compound_statement { | assignment { $$ = create_statement_node(AST_NODE_ASSIGNMENT, (void*)$1); } + | mul_assignment { + $$ = create_statement_node(AST_NODE_MUL_ASSIGNMENT, (void*)$1); + } + | div_assignment { + $$ = create_statement_node(AST_NODE_DIV_ASSIGNMENT, (void*)$1); + } | array_assignment { $$ = create_statement_node(AST_NODE_ARRAY_ASSIGNMENT, (void*)$1); } @@ -435,13 +447,155 @@ assignment: INT_IDENTIFIER OPR_ASSIGNMENT arithmetic_expression SEMICOLON { } $1->data_type = DT_CHAR_; - $1->value = $3->value; + $1->value = $3->value ; $$ = create_assignment_node($1, $3); printf("%s := %c\n", $1->identifier, $1->value); } ; +mul_assignment: INT_IDENTIFIER OPR_MUL_ASSIGNMENT arithmetic_expression SEMICOLON { + if ($1 == NULL) + { + yyerror("variable already defined"); + } + + if ($1->is_function == 1) + { + yyerror("identifier is a function, cannot assign value"); + } + + if ($1->is_constant == 1) + { + yyerror("identifer is a pin number constant, cannot assign value"); + } + + $1->data_type = DT_INTEGER; + $1->value = $1->value * $3->value; + $$ = create_mul_assignment_node($1, $3); + + printf("%s *= %d\n", $1->identifier, $1->value); + } + | BOOL_IDENTIFIER OPR_MUL_ASSIGNMENT boolean_expression SEMICOLON { + if ($1 == NULL) + { + yyerror("variable already defined"); + } + + if ($1->is_function == 1) + { + yyerror("identifier is a function, cannot assign value"); + } + + if ($1->is_constant == 1) + { + yyerror("identifer is a pin number constant, cannot assign value"); + } + + $1->data_type = DT_BOOLEAN; + $1->value = $1->value * $3->value; + $$ = create_mul_assignment_node($1, $3); + + printf("%s *= %d\n", $1->identifier, $1->value); + } + | CHAR_IDENTIFIER OPR_MUL_ASSIGNMENT arithmetic_expression SEMICOLON { + if ($1 == NULL) + { + yyerror("variable already defined"); + } + + if ($1->is_function == 1) + { + yyerror("identifier is a function, cannot assign value"); + } + + if ($1->is_constant == 1) + { + yyerror("identifier is a pin number constant, cannot assign value"); + } + + $1->data_type = DT_CHAR_; + $1->value = $1->value *$3->value; + $$ = create_mul_assignment_node($1, $3); + + printf("%s *= %c\n", $1->identifier, $1->value); + } + ; +div_assignment: INT_IDENTIFIER OPR_DIV_ASSIGNMENT arithmetic_expression SEMICOLON { + if ($1 == NULL) + { + yyerror("variable already defined"); + } + + if ($1->is_function == 1) + { + yyerror("identifier is a function, cannot assign value"); + } + + if ($1->is_constant == 1) + { + yyerror("identifer is a pin number constant, cannot assign value"); + } + if ($3->value == 0) + { + yyerror("division by 0"); + } + $1->data_type = DT_INTEGER; + $1->value = $1->value / $3->value; + $$ = create_div_assignment_node($1, $3); + + printf("%s /= %d\n", $1->identifier, $1->value); + } + | BOOL_IDENTIFIER OPR_DIV_ASSIGNMENT boolean_expression SEMICOLON { + if ($1 == NULL) + { + yyerror("variable already defined"); + } + + if ($1->is_function == 1) + { + yyerror("identifier is a function, cannot assign value"); + } + + if ($1->is_constant == 1) + { + yyerror("identifer is a pin number constant, cannot assign value"); + } + if ($3->value == 0) + { + yyerror("division by 0"); + } + $1->data_type = DT_BOOLEAN; + $1->value = $1->value / $3->value; + $$ = create_div_assignment_node($1, $3); + + printf("%s /= %d\n", $1->identifier, $1->value); + } + | CHAR_IDENTIFIER OPR_DIV_ASSIGNMENT arithmetic_expression SEMICOLON { + if ($1 == NULL) + { + yyerror("variable already defined"); + } + if ($1->is_function == 1) + { + yyerror("identifier is a function, cannot assign value"); + } + + if ($1->is_constant == 1) + { + yyerror("identifier is a pin number constant, cannot assign value"); + } + if ($3->value == 0) + { + yyerror("division by 0"); + } + $1->data_type = DT_CHAR_; + $1->value = $1->value / $3->value; + $$ = create_div_assignment_node($1, $3); + + printf("%s /= %c\n", $1->identifier, $1->value); + } + ; array_assignment: INT_ARR_IDENTIFIER LSQUARE arithmetic_expression RSQUARE OPR_ASSIGNMENT arithmetic_expression SEMICOLON { if ($1 == NULL) { diff --git a/test.py b/test.py index e945808..eff9f17 100644 --- a/test.py +++ b/test.py @@ -26,7 +26,7 @@ if fnmatch(file, "*.sim"): print("Running test ", file) - transpile_output = subprocess.run(f"bin/simppru --preprocess -t tests/run_anywhere/{file}", shell=True, capture_output=True) + transpile_output = subprocess.run(f"bin/simppru-1.4 --preprocess -t tests/run_anywhere/{file}", shell=True, capture_output=True) if transpile_output.returncode != 0: print(f"**** ****TEST FAILED**** ****: {file}") diff --git a/tests/run_anywhere/mul-div-assign.sim b/tests/run_anywhere/mul-div-assign.sim new file mode 100644 index 0000000..02225bb --- /dev/null +++ b/tests/run_anywhere/mul-div-assign.sim @@ -0,0 +1,4 @@ +int a:= 9; +int b:= 10; +b*=a; +print(b); \ No newline at end of file diff --git a/tests/run_anywhere/mul-div-assign.sim.output b/tests/run_anywhere/mul-div-assign.sim.output new file mode 100644 index 0000000..0fa6a7b --- /dev/null +++ b/tests/run_anywhere/mul-div-assign.sim.output @@ -0,0 +1 @@ +90 \ No newline at end of file From e0855601d0e81962e14ca6204805f17c416309fb Mon Sep 17 00:00:00 2001 From: Krishna-13-cyber Date: Sun, 15 May 2022 12:22:26 +0530 Subject: [PATCH 2/5] Added testcase --- src/ast.c | 4 ++-- src/code_printer.c | 10 +++++++++- src/parser.y | 2 +- tests/run_anywhere/mul-div-assign.sim | 4 ---- tests/run_anywhere/mul-div-assign.sim.output | 1 - tests/run_anywhere/mul-div.sim | 6 ++++++ tests/run_anywhere/mul-div.sim.output | 2 ++ 7 files changed, 20 insertions(+), 9 deletions(-) delete mode 100644 tests/run_anywhere/mul-div-assign.sim delete mode 100644 tests/run_anywhere/mul-div-assign.sim.output create mode 100644 tests/run_anywhere/mul-div.sim create mode 100644 tests/run_anywhere/mul-div.sim.output diff --git a/src/ast.c b/src/ast.c index e523124..3a87944 100644 --- a/src/ast.c +++ b/src/ast.c @@ -627,13 +627,13 @@ void ast_node_type(int node_type) printf("ast array declaration"); break; - case AST_NODE_DIV_ASSIGNMENT: +/* case AST_NODE_DIV_ASSIGNMENT: printf("ast division assignment"); break; case AST_NODE_MUL_ASSIGNMENT: printf("ast multiplication assignment"); - break; + break; */ case AST_NODE_ARRAY_ASSIGNMENT: printf("ast array assignment"); diff --git a/src/code_printer.c b/src/code_printer.c index 70a076b..d17fa86 100644 --- a/src/code_printer.c +++ b/src/code_printer.c @@ -724,7 +724,15 @@ int code_printer(ast_node* ast, int pru_id, int test) case AST_NODE_ASSIGNMENT: ast_assignment_printer(((ast_node_statements*)temp)->child_nodes.assignment, handle); break; - + + case AST_NODE_MUL_ASSIGNMENT: + ast_mul_assignment_printer(((ast_node_statements*)temp)->child_nodes.mul_assignment, handle); + break; + + case AST_NODE_DIV_ASSIGNMENT: + ast_div_assignment_printer(((ast_node_statements*)temp)->child_nodes.div_assignment, handle); + break; + case AST_NODE_ARRAY_ASSIGNMENT: ast_array_assignment_printer(((ast_node_statements*)temp)->child_nodes.array_assignment, handle); break; diff --git a/src/parser.y b/src/parser.y index 63606a9..c599b60 100644 --- a/src/parser.y +++ b/src/parser.y @@ -514,7 +514,7 @@ mul_assignment: INT_IDENTIFIER OPR_MUL_ASSIGNMENT arithmetic_expression SEMICOLO } $1->data_type = DT_CHAR_; - $1->value = $1->value *$3->value; + $1->value = $1->value * $3->value; $$ = create_mul_assignment_node($1, $3); printf("%s *= %c\n", $1->identifier, $1->value); diff --git a/tests/run_anywhere/mul-div-assign.sim b/tests/run_anywhere/mul-div-assign.sim deleted file mode 100644 index 02225bb..0000000 --- a/tests/run_anywhere/mul-div-assign.sim +++ /dev/null @@ -1,4 +0,0 @@ -int a:= 9; -int b:= 10; -b*=a; -print(b); \ No newline at end of file diff --git a/tests/run_anywhere/mul-div-assign.sim.output b/tests/run_anywhere/mul-div-assign.sim.output deleted file mode 100644 index 0fa6a7b..0000000 --- a/tests/run_anywhere/mul-div-assign.sim.output +++ /dev/null @@ -1 +0,0 @@ -90 \ No newline at end of file diff --git a/tests/run_anywhere/mul-div.sim b/tests/run_anywhere/mul-div.sim new file mode 100644 index 0000000..271a504 --- /dev/null +++ b/tests/run_anywhere/mul-div.sim @@ -0,0 +1,6 @@ +int a:=5; +int b:=10; +a*=b; +b/=2; +println(a); +print(b); \ No newline at end of file diff --git a/tests/run_anywhere/mul-div.sim.output b/tests/run_anywhere/mul-div.sim.output new file mode 100644 index 0000000..4e233b1 --- /dev/null +++ b/tests/run_anywhere/mul-div.sim.output @@ -0,0 +1,2 @@ +50 +5 \ No newline at end of file From 4958afe5c28043d54884cb671107eb8832f19da9 Mon Sep 17 00:00:00 2001 From: Krishna-13-cyber Date: Thu, 19 May 2022 21:38:47 +0530 Subject: [PATCH 3/5] minor fix --- src/ast.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ast.c b/src/ast.c index 3a87944..85269c8 100644 --- a/src/ast.c +++ b/src/ast.c @@ -627,13 +627,17 @@ void ast_node_type(int node_type) printf("ast array declaration"); break; -/* case AST_NODE_DIV_ASSIGNMENT: + case AST_NODE_ASSIGNMENT: + printf("ast assignment"); + break; + + case AST_NODE_DIV_ASSIGNMENT: printf("ast division assignment"); break; case AST_NODE_MUL_ASSIGNMENT: printf("ast multiplication assignment"); - break; */ + break; case AST_NODE_ARRAY_ASSIGNMENT: printf("ast array assignment"); From 7ddfed4322ff19b0c62c5755ec076ffba141de76 Mon Sep 17 00:00:00 2001 From: Krishna-13-cyber Date: Thu, 30 Jun 2022 22:16:00 +0530 Subject: [PATCH 4/5] fixed the required changes and removed redundant --- test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.py b/test.py index eff9f17..e945808 100644 --- a/test.py +++ b/test.py @@ -26,7 +26,7 @@ if fnmatch(file, "*.sim"): print("Running test ", file) - transpile_output = subprocess.run(f"bin/simppru-1.4 --preprocess -t tests/run_anywhere/{file}", shell=True, capture_output=True) + transpile_output = subprocess.run(f"bin/simppru --preprocess -t tests/run_anywhere/{file}", shell=True, capture_output=True) if transpile_output.returncode != 0: print(f"**** ****TEST FAILED**** ****: {file}") From cdd2de6a0f7a96c3dc787fd0c1643e6fa068923e Mon Sep 17 00:00:00 2001 From: Krishna-13-cyber Date: Thu, 11 Aug 2022 23:45:17 +0530 Subject: [PATCH 5/5] Updated the multiple-divide assignment operators by fixing the known changes --- src/ast.c | 1 + src/code_printer.c | 2 ++ src/lexer.l | 3 +++ src/parser.y | 17 ++++++++++------- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/ast.c b/src/ast.c index 85269c8..fc20982 100644 --- a/src/ast.c +++ b/src/ast.c @@ -151,6 +151,7 @@ ast_node_assignment *create_assignment_node(sym_ptr symbol, ast_node_expression return assgn; } + ast_node_mul_assignment *create_mul_assignment_node(sym_ptr symbol, ast_node_expression *exp) { ast_node_mul_assignment *mul_assgn = (ast_node_mul_assignment*)malloc(sizeof(ast_node_mul_assignment)); diff --git a/src/code_printer.c b/src/code_printer.c index d17fa86..37e182b 100644 --- a/src/code_printer.c +++ b/src/code_printer.c @@ -192,6 +192,7 @@ void ast_mul_assignment_printer(ast_node_mul_assignment *assg, FILE* handle) } } + void ast_div_assignment_printer(ast_node_div_assignment *assg, FILE* handle) { if (assg != NULL && handle != NULL) @@ -202,6 +203,7 @@ void ast_div_assignment_printer(ast_node_div_assignment *assg, FILE* handle) } } + void ast_array_assignment_printer(ast_node_array_assignment *assign, FILE* handle) { if (assign != NULL && handle != NULL) diff --git a/src/lexer.l b/src/lexer.l index e9c0945..5ebf57d 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -324,12 +324,15 @@ comma ([,]) {assignment_operator} { return OPR_ASSIGNMENT; } + {mul_assignment_operator} { return OPR_MUL_ASSIGNMENT; } + {div_assignment_operator} { return OPR_DIV_ASSIGNMENT; } + {io} { if (!strcmp(yytext, "digital_write")) { diff --git a/src/parser.y b/src/parser.y index c599b60..8c50db6 100644 --- a/src/parser.y +++ b/src/parser.y @@ -447,12 +447,13 @@ assignment: INT_IDENTIFIER OPR_ASSIGNMENT arithmetic_expression SEMICOLON { } $1->data_type = DT_CHAR_; - $1->value = $3->value ; + $1->value = $3->value; $$ = create_assignment_node($1, $3); printf("%s := %c\n", $1->identifier, $1->value); } ; + mul_assignment: INT_IDENTIFIER OPR_MUL_ASSIGNMENT arithmetic_expression SEMICOLON { if ($1 == NULL) { @@ -473,7 +474,7 @@ mul_assignment: INT_IDENTIFIER OPR_MUL_ASSIGNMENT arithmetic_expression SEMICOLO $1->value = $1->value * $3->value; $$ = create_mul_assignment_node($1, $3); - printf("%s *= %d\n", $1->identifier, $1->value); + printf("%s := %d\n", $1->identifier, $1->value); } | BOOL_IDENTIFIER OPR_MUL_ASSIGNMENT boolean_expression SEMICOLON { if ($1 == NULL) @@ -495,7 +496,7 @@ mul_assignment: INT_IDENTIFIER OPR_MUL_ASSIGNMENT arithmetic_expression SEMICOLO $1->value = $1->value * $3->value; $$ = create_mul_assignment_node($1, $3); - printf("%s *= %d\n", $1->identifier, $1->value); + printf("%s := %d\n", $1->identifier, $1->value); } | CHAR_IDENTIFIER OPR_MUL_ASSIGNMENT arithmetic_expression SEMICOLON { if ($1 == NULL) @@ -517,9 +518,10 @@ mul_assignment: INT_IDENTIFIER OPR_MUL_ASSIGNMENT arithmetic_expression SEMICOLO $1->value = $1->value * $3->value; $$ = create_mul_assignment_node($1, $3); - printf("%s *= %c\n", $1->identifier, $1->value); + printf("%s := %c\n", $1->identifier, $1->value); } ; + div_assignment: INT_IDENTIFIER OPR_DIV_ASSIGNMENT arithmetic_expression SEMICOLON { if ($1 == NULL) { @@ -543,7 +545,7 @@ div_assignment: INT_IDENTIFIER OPR_DIV_ASSIGNMENT arithmetic_expression SEMICOLO $1->value = $1->value / $3->value; $$ = create_div_assignment_node($1, $3); - printf("%s /= %d\n", $1->identifier, $1->value); + printf("%s := %d\n", $1->identifier, $1->value); } | BOOL_IDENTIFIER OPR_DIV_ASSIGNMENT boolean_expression SEMICOLON { if ($1 == NULL) @@ -568,7 +570,7 @@ div_assignment: INT_IDENTIFIER OPR_DIV_ASSIGNMENT arithmetic_expression SEMICOLO $1->value = $1->value / $3->value; $$ = create_div_assignment_node($1, $3); - printf("%s /= %d\n", $1->identifier, $1->value); + printf("%s := %d\n", $1->identifier, $1->value); } | CHAR_IDENTIFIER OPR_DIV_ASSIGNMENT arithmetic_expression SEMICOLON { if ($1 == NULL) @@ -593,9 +595,10 @@ div_assignment: INT_IDENTIFIER OPR_DIV_ASSIGNMENT arithmetic_expression SEMICOLO $1->value = $1->value / $3->value; $$ = create_div_assignment_node($1, $3); - printf("%s /= %c\n", $1->identifier, $1->value); + printf("%s := %c\n", $1->identifier, $1->value); } ; + array_assignment: INT_ARR_IDENTIFIER LSQUARE arithmetic_expression RSQUARE OPR_ASSIGNMENT arithmetic_expression SEMICOLON { if ($1 == NULL) {