From 147e101a032bd4bf3077989b32a8f8b9eae65396 Mon Sep 17 00:00:00 2001 From: Krishna-13-cyber Date: Wed, 25 May 2022 22:05:07 +0530 Subject: [PATCH 1/6] bitwise left-shift assignment --- include/ast.h | 14 +++++ include/code_printer.h | 2 + src/ast.c | 19 ++++++ src/code_printer.c | 19 ++++++ src/lexer.l | 5 +- src/parser.y | 74 ++++++++++++++++++++++++ test.py | 2 +- tests/run_anywhere/bwl_assign.sim | 3 + tests/run_anywhere/bwl_assign.sim.output | 1 + 9 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 tests/run_anywhere/bwl_assign.sim create mode 100644 tests/run_anywhere/bwl_assign.sim.output diff --git a/include/ast.h b/include/ast.h index 3b90a36..c74a753 100644 --- a/include/ast.h +++ b/include/ast.h @@ -14,6 +14,7 @@ #define AST_NODE_DECLARATION 6 #define AST_NODE_ARRAY_DECLARATION 2000 #define AST_NODE_ASSIGNMENT 7 +#define AST_NODE_BWL_ASSIGNMENT 9999 #define AST_NODE_ARRAY_ASSIGNMENT 2001 #define AST_NODE_ARRAY_ACCESS 2002 #define AST_NODE_ARITHMETIC_EXP 8 @@ -59,6 +60,7 @@ #define AST_OPR_LGL_OR 43 // or #define AST_OPR_ASSIGNMENT 44 // := +#define AST_OPR_BWL_ASSIGNMENT 9998 // <<= #define AST_CONST_INT 45 // INT CONSTANT #define AST_CONST_BOOL 46 // BOOL CONSTANT @@ -100,6 +102,7 @@ struct ast_node_compound_statement; struct ast_node_declaration; struct ast_node_array_declaration; struct ast_node_assignment; +struct ast_node_bwl_assignment; struct ast_node_array_assignment; struct ast_node_array_access; struct ast_node_expression; @@ -125,6 +128,7 @@ 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_bwl_assignment ast_node_bwl_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 +164,7 @@ struct ast_node ast_node_declaration *declaration; ast_node_array_declaration *array_declaration; ast_node_assignment *assignment; + ast_node_bwl_assignment *bwl_assignment; ast_node_array_assignment *array_assignment; ast_node_conditional_if *if_else; ast_node_loop_for *loop_for; @@ -205,6 +210,14 @@ struct ast_node_array_declaration ast_node_expression *expression; }; + struct ast_node_bwl_assignment +{ + int node_type; + + sym_ptr symbol_entry; + ast_node_expression *expression; +}; + struct ast_node_array_assignment { int node_type; @@ -363,6 +376,7 @@ 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_bwl_assignment *create_bwl_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..d19e809 100644 --- a/include/code_printer.h +++ b/include/code_printer.h @@ -32,6 +32,7 @@ #define _OPR_LGL_OR " | " #define _OPR_ASSIGNMENT " = " +#define _OPR_BWL_ASSIGNMENT " <<= " #define _DT_INT_ "int" #define _DT_VOID_ "void" @@ -54,6 +55,7 @@ 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_bwl_assignment_printer(ast_node_bwl_assignment *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..245f45d 100644 --- a/src/ast.c +++ b/src/ast.c @@ -40,6 +40,10 @@ ast_node_statements *create_statement_node(int node_type, void *child) stmt->child_nodes.assignment = child; break; + case AST_NODE_BWL_ASSIGNMENT: + stmt->child_nodes.bwl_assignment = child; + break; + case AST_NODE_ARRAY_ASSIGNMENT: stmt->child_nodes.array_assignment = child; break; @@ -144,6 +148,17 @@ ast_node_assignment *create_assignment_node(sym_ptr symbol, ast_node_expression return assgn; } +ast_node_bwl_assignment *create_bwl_assignment_node(sym_ptr symbol, ast_node_expression *exp) +{ + ast_node_bwl_assignment *assgn = (ast_node_bwl_assignment*)malloc(sizeof(ast_node_bwl_assignment)); + + assgn->node_type = AST_NODE_BWL_ASSIGNMENT; + assgn->expression = exp; + assgn->symbol_entry = symbol; + + return assgn; +} + ast_node_array_assignment *create_array_assignment_node(sym_ptr symbol, ast_node_expression *index, ast_node_expression *exp) { ast_node_array_assignment *assign = (ast_node_array_assignment*)malloc(sizeof(ast_node_array_assignment)); @@ -602,6 +617,10 @@ void ast_node_type(int node_type) printf("ast assignment"); break; + case AST_NODE_BWL_ASSIGNMENT: + printf("ast bitwise left shift assignment"); + break; + case AST_NODE_ARRAY_ASSIGNMENT: printf("ast array assignment"); break; diff --git a/src/code_printer.c b/src/code_printer.c index 7ff71b0..0db4006 100644 --- a/src/code_printer.c +++ b/src/code_printer.c @@ -29,6 +29,10 @@ 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_BWL_ASSIGNMENT: + ast_bwl_assignment_printer(((ast_node_statements*)temp)->child_nodes.bwl_assignment, handle); + break; + case AST_NODE_ARRAY_ASSIGNMENT: ast_array_assignment_printer(((ast_node_statements*)temp)->child_nodes.array_assignment, handle); break; @@ -174,6 +178,17 @@ void ast_assignment_printer(ast_node_assignment *assg, FILE* handle) } +void ast_bwl_assignment_printer(ast_node_bwl_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) @@ -696,6 +711,10 @@ 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_BWL_ASSIGNMENT: + ast_bwl_assignment_printer(((ast_node_statements*)temp)->child_nodes.bwl_assignment, handle); + break; case AST_NODE_ARRAY_ASSIGNMENT: ast_array_assignment_printer(((ast_node_statements*)temp)->child_nodes.array_assignment, handle); diff --git a/src/lexer.l b/src/lexer.l index bcb7fdc..f880375 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -35,6 +35,7 @@ bitwise_operators ("~"|"&"|"|"|">>"|"<<") logical_operators ("not"|"and"|"or") function ("return"|"void"|"def") assignment_operator ([:][=]) +assignment_bwl_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 +323,9 @@ comma ([,]) {assignment_operator} { return OPR_ASSIGNMENT; } - +{assignment_bwl_operator} { + return OPR_BWL_ASSIGNMENT; +} {io} { if (!strcmp(yytext, "digital_write")) { diff --git a/src/parser.y b/src/parser.y index 29bfe90..6ee7b63 100644 --- a/src/parser.y +++ b/src/parser.y @@ -33,6 +33,7 @@ ast_node *ast = NULL; struct ast_node_declaration *declaration; struct ast_node_array_declaration *array_declaration; struct ast_node_assignment *assignment; + struct ast_node_bwl_assignment *bwl_assignment; struct ast_node_array_assignment *array_assignment; struct ast_node_array_access *array_access; struct ast_node_expression *expression; @@ -75,6 +76,7 @@ ast_node *ast = NULL; %right OPR_BW_NOT OPR_LGL_NOT %token OPR_ASSIGNMENT +%token OPR_BWL_ASSIGNMENT %token SEMICOLON COLON COMMA @@ -106,6 +108,7 @@ ast_node *ast = NULL; %type declaration declaration_assignment %type array_declaration array_declaration_assignment %type assignment +%type bwl_assignment %type arithmetic_expression boolean_expression relational_expression logical_expression return_statement function_call_datatypes %type range_expression %type array_assignment @@ -183,6 +186,9 @@ statement: compound_statement { | assignment { $$ = create_statement_node(AST_NODE_ASSIGNMENT, (void*)$1); } + | bwl_assignment { + $$ = create_statement_node(AST_NODE_BWL_ASSIGNMENT, (void*)$1); + } | array_assignment { $$ = create_statement_node(AST_NODE_ARRAY_ASSIGNMENT, (void*)$1); } @@ -442,6 +448,74 @@ assignment: INT_IDENTIFIER OPR_ASSIGNMENT arithmetic_expression SEMICOLON { } ; +bwl_assignment: INT_IDENTIFIER OPR_BWL_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_bwl_assignment_node($1, $3); + + printf("%s <<= %d\n", $1->identifier, $1->value); + } + | BOOL_IDENTIFIER OPR_BWL_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_bwl_assignment_node($1, $3); + + printf("%s <<= %d\n", $1->identifier, $1->value); + } + | CHAR_IDENTIFIER OPR_BWL_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_bwl_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 e8c1592..c44dde1 100644 --- a/test.py +++ b/test.py @@ -28,7 +28,7 @@ print(f" ") 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/bwl_assign.sim b/tests/run_anywhere/bwl_assign.sim new file mode 100644 index 0000000..e108f19 --- /dev/null +++ b/tests/run_anywhere/bwl_assign.sim @@ -0,0 +1,3 @@ +int a:=5; +a<<=1; +print(a); \ No newline at end of file diff --git a/tests/run_anywhere/bwl_assign.sim.output b/tests/run_anywhere/bwl_assign.sim.output new file mode 100644 index 0000000..9a03714 --- /dev/null +++ b/tests/run_anywhere/bwl_assign.sim.output @@ -0,0 +1 @@ +10 \ No newline at end of file From 0758b59307c671be015ff948c3bf20fa920db341 Mon Sep 17 00:00:00 2001 From: Krishna-13-cyber Date: Thu, 26 May 2022 22:24:38 +0530 Subject: [PATCH 2/6] fix --- src/lexer.l | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lexer.l b/src/lexer.l index f880375..46f5b2e 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -35,7 +35,7 @@ bitwise_operators ("~"|"&"|"|"|">>"|"<<") logical_operators ("not"|"and"|"or") function ("return"|"void"|"def") assignment_operator ([:][=]) -assignment_bwl_operator ([<<][=]) +assignment_bwl_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_]*) From 8f0ea0e2a9202b19586ab9d6e6a96cd99acaba8d Mon Sep 17 00:00:00 2001 From: Krishna-13-cyber Date: Fri, 3 Jun 2022 13:52:17 +0530 Subject: [PATCH 3/6] bitwise shift assignment operators added --- include/ast.h | 14 +++++ include/code_printer.h | 2 + src/ast.c | 19 ++++++ src/code_printer.c | 19 ++++++ src/lexer.l | 5 ++ src/parser.y | 74 ++++++++++++++++++++++++ tests/run_anywhere/bwl_assign.sim | 3 - tests/run_anywhere/bwl_assign.sim.output | 1 - tests/run_anywhere/bws_assign.sim | 6 ++ tests/run_anywhere/bws_assign.sim.output | 2 + 10 files changed, 141 insertions(+), 4 deletions(-) delete mode 100644 tests/run_anywhere/bwl_assign.sim delete mode 100644 tests/run_anywhere/bwl_assign.sim.output create mode 100644 tests/run_anywhere/bws_assign.sim create mode 100644 tests/run_anywhere/bws_assign.sim.output diff --git a/include/ast.h b/include/ast.h index c74a753..c0b3ff0 100644 --- a/include/ast.h +++ b/include/ast.h @@ -15,6 +15,7 @@ #define AST_NODE_ARRAY_DECLARATION 2000 #define AST_NODE_ASSIGNMENT 7 #define AST_NODE_BWL_ASSIGNMENT 9999 +#define AST_NODE_BWR_ASSIGNMENT 9997 #define AST_NODE_ARRAY_ASSIGNMENT 2001 #define AST_NODE_ARRAY_ACCESS 2002 #define AST_NODE_ARITHMETIC_EXP 8 @@ -61,6 +62,7 @@ #define AST_OPR_ASSIGNMENT 44 // := #define AST_OPR_BWL_ASSIGNMENT 9998 // <<= +#define AST_OPR_BWR_ASSIGNMENT 9996 // <<= #define AST_CONST_INT 45 // INT CONSTANT #define AST_CONST_BOOL 46 // BOOL CONSTANT @@ -103,6 +105,7 @@ struct ast_node_declaration; struct ast_node_array_declaration; struct ast_node_assignment; struct ast_node_bwl_assignment; +struct ast_node_bwr_assignment; struct ast_node_array_assignment; struct ast_node_array_access; struct ast_node_expression; @@ -129,6 +132,7 @@ 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_bwl_assignment ast_node_bwl_assignment; +typedef struct ast_node_bwr_assignment ast_node_bwr_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; @@ -165,6 +169,7 @@ struct ast_node ast_node_array_declaration *array_declaration; ast_node_assignment *assignment; ast_node_bwl_assignment *bwl_assignment; + ast_node_bwr_assignment *bwr_assignment; ast_node_array_assignment *array_assignment; ast_node_conditional_if *if_else; ast_node_loop_for *loop_for; @@ -218,6 +223,14 @@ struct ast_node_array_declaration ast_node_expression *expression; }; + struct ast_node_bwr_assignment +{ + int node_type; + + sym_ptr symbol_entry; + ast_node_expression *expression; +}; + struct ast_node_array_assignment { int node_type; @@ -377,6 +390,7 @@ ast_node_declaration *create_declaration_node(sym_ptr symbol, ast_node_expressio 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_bwl_assignment *create_bwl_assignment_node(sym_ptr symbol, ast_node_expression *exp); +ast_node_bwr_assignment *create_bwr_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 d19e809..79e3434 100644 --- a/include/code_printer.h +++ b/include/code_printer.h @@ -33,6 +33,7 @@ #define _OPR_ASSIGNMENT " = " #define _OPR_BWL_ASSIGNMENT " <<= " +#define _OPR_BWR_ASSIGNMENT " >>= " #define _DT_INT_ "int" #define _DT_VOID_ "void" @@ -56,6 +57,7 @@ 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_bwl_assignment_printer(ast_node_bwl_assignment *assg, FILE* handle); +void ast_bwr_assignment_printer(ast_node_bwr_assignment *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 245f45d..2c9ee59 100644 --- a/src/ast.c +++ b/src/ast.c @@ -44,6 +44,10 @@ ast_node_statements *create_statement_node(int node_type, void *child) stmt->child_nodes.bwl_assignment = child; break; + case AST_NODE_BWR_ASSIGNMENT: + stmt->child_nodes.bwr_assignment = child; + break; + case AST_NODE_ARRAY_ASSIGNMENT: stmt->child_nodes.array_assignment = child; break; @@ -159,6 +163,17 @@ ast_node_bwl_assignment *create_bwl_assignment_node(sym_ptr symbol, ast_node_exp return assgn; } +ast_node_bwr_assignment *create_bwr_assignment_node(sym_ptr symbol, ast_node_expression *exp) +{ + ast_node_bwr_assignment *assgn = (ast_node_bwr_assignment*)malloc(sizeof(ast_node_bwr_assignment)); + + assgn->node_type = AST_NODE_BWR_ASSIGNMENT; + assgn->expression = exp; + assgn->symbol_entry = symbol; + + return assgn; +} + ast_node_array_assignment *create_array_assignment_node(sym_ptr symbol, ast_node_expression *index, ast_node_expression *exp) { ast_node_array_assignment *assign = (ast_node_array_assignment*)malloc(sizeof(ast_node_array_assignment)); @@ -621,6 +636,10 @@ void ast_node_type(int node_type) printf("ast bitwise left shift assignment"); break; + case AST_NODE_BWR_ASSIGNMENT: + printf("ast bitwise right shift assignment"); + break; + case AST_NODE_ARRAY_ASSIGNMENT: printf("ast array assignment"); break; diff --git a/src/code_printer.c b/src/code_printer.c index 0db4006..542ac08 100644 --- a/src/code_printer.c +++ b/src/code_printer.c @@ -33,6 +33,10 @@ void ast_compound_statement_printer(ast_node_compound_statement *cmpd_stmt, FILE ast_bwl_assignment_printer(((ast_node_statements*)temp)->child_nodes.bwl_assignment, handle); break; + case AST_NODE_BWR_ASSIGNMENT: + ast_bwr_assignment_printer(((ast_node_statements*)temp)->child_nodes.bwr_assignment, handle); + break; + case AST_NODE_ARRAY_ASSIGNMENT: ast_array_assignment_printer(((ast_node_statements*)temp)->child_nodes.array_assignment, handle); break; @@ -189,6 +193,17 @@ void ast_bwl_assignment_printer(ast_node_bwl_assignment *assg, FILE* handle) } +void ast_bwr_assignment_printer(ast_node_bwr_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) @@ -715,6 +730,10 @@ int code_printer(ast_node* ast, int pru_id, int test) case AST_NODE_BWL_ASSIGNMENT: ast_bwl_assignment_printer(((ast_node_statements*)temp)->child_nodes.bwl_assignment, handle); break; + + case AST_NODE_BWR_ASSIGNMENT: + ast_bwr_assignment_printer(((ast_node_statements*)temp)->child_nodes.bwr_assignment, handle); + break; case AST_NODE_ARRAY_ASSIGNMENT: ast_array_assignment_printer(((ast_node_statements*)temp)->child_nodes.array_assignment, handle); diff --git a/src/lexer.l b/src/lexer.l index 46f5b2e..6ec47df 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -36,6 +36,7 @@ logical_operators ("not"|"and"|"or") function ("return"|"void"|"def") assignment_operator ([:][=]) assignment_bwl_operator ([<][<][=]) +assignment_bwr_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_]*) @@ -326,6 +327,10 @@ comma ([,]) {assignment_bwl_operator} { return OPR_BWL_ASSIGNMENT; } +{assignment_bwr_operator} { + return OPR_BWR_ASSIGNMENT; +} + {io} { if (!strcmp(yytext, "digital_write")) { diff --git a/src/parser.y b/src/parser.y index 6ee7b63..e20b9b4 100644 --- a/src/parser.y +++ b/src/parser.y @@ -34,6 +34,7 @@ ast_node *ast = NULL; struct ast_node_array_declaration *array_declaration; struct ast_node_assignment *assignment; struct ast_node_bwl_assignment *bwl_assignment; + struct ast_node_bwr_assignment *bwr_assignment; struct ast_node_array_assignment *array_assignment; struct ast_node_array_access *array_access; struct ast_node_expression *expression; @@ -77,6 +78,7 @@ ast_node *ast = NULL; %token OPR_ASSIGNMENT %token OPR_BWL_ASSIGNMENT +%token OPR_BWR_ASSIGNMENT %token SEMICOLON COLON COMMA @@ -109,6 +111,7 @@ ast_node *ast = NULL; %type array_declaration array_declaration_assignment %type assignment %type bwl_assignment +%type bwr_assignment %type arithmetic_expression boolean_expression relational_expression logical_expression return_statement function_call_datatypes %type range_expression %type array_assignment @@ -189,6 +192,9 @@ statement: compound_statement { | bwl_assignment { $$ = create_statement_node(AST_NODE_BWL_ASSIGNMENT, (void*)$1); } + | bwr_assignment { + $$ = create_statement_node(AST_NODE_BWR_ASSIGNMENT, (void*)$1); + } | array_assignment { $$ = create_statement_node(AST_NODE_ARRAY_ASSIGNMENT, (void*)$1); } @@ -516,6 +522,74 @@ bwl_assignment: INT_IDENTIFIER OPR_BWL_ASSIGNMENT arithmetic_expression SEMICOLO } ; +bwr_assignment: INT_IDENTIFIER OPR_BWR_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_bwr_assignment_node($1, $3); + + printf("%s >>= %d\n", $1->identifier, $1->value); + } + | BOOL_IDENTIFIER OPR_BWR_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_bwr_assignment_node($1, $3); + + printf("%s >>= %d\n", $1->identifier, $1->value); + } + | CHAR_IDENTIFIER OPR_BWR_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_bwr_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/tests/run_anywhere/bwl_assign.sim b/tests/run_anywhere/bwl_assign.sim deleted file mode 100644 index e108f19..0000000 --- a/tests/run_anywhere/bwl_assign.sim +++ /dev/null @@ -1,3 +0,0 @@ -int a:=5; -a<<=1; -print(a); \ No newline at end of file diff --git a/tests/run_anywhere/bwl_assign.sim.output b/tests/run_anywhere/bwl_assign.sim.output deleted file mode 100644 index 9a03714..0000000 --- a/tests/run_anywhere/bwl_assign.sim.output +++ /dev/null @@ -1 +0,0 @@ -10 \ No newline at end of file diff --git a/tests/run_anywhere/bws_assign.sim b/tests/run_anywhere/bws_assign.sim new file mode 100644 index 0000000..f65f760 --- /dev/null +++ b/tests/run_anywhere/bws_assign.sim @@ -0,0 +1,6 @@ +int a:=5; +int b:=4; +a<<=1; +b>>=1; +println(a); +print(b); \ No newline at end of file diff --git a/tests/run_anywhere/bws_assign.sim.output b/tests/run_anywhere/bws_assign.sim.output new file mode 100644 index 0000000..20825a8 --- /dev/null +++ b/tests/run_anywhere/bws_assign.sim.output @@ -0,0 +1,2 @@ +10 +2 \ No newline at end of file From 1c258ad547ba31b0cbf9895b2d8175ea71800c3d Mon Sep 17 00:00:00 2001 From: Krishna-13-cyber Date: Mon, 18 Jul 2022 12:27:04 +0530 Subject: [PATCH 4/6] Updated with fixing the required changes --- test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.py b/test.py index c44dde1..e8c1592 100644 --- a/test.py +++ b/test.py @@ -28,7 +28,7 @@ print(f" ") 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 caf1d62c5785c3a89288ff51017d190a1cedd65c Mon Sep 17 00:00:00 2001 From: Krishna-13-cyber Date: Thu, 28 Jul 2022 17:30:07 +0530 Subject: [PATCH 5/6] Updated bitwise right assignment with correct operator in comment --- include/ast.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/ast.h b/include/ast.h index c0b3ff0..9526445 100644 --- a/include/ast.h +++ b/include/ast.h @@ -62,7 +62,7 @@ #define AST_OPR_ASSIGNMENT 44 // := #define AST_OPR_BWL_ASSIGNMENT 9998 // <<= -#define AST_OPR_BWR_ASSIGNMENT 9996 // <<= +#define AST_OPR_BWR_ASSIGNMENT 9996 // >>= #define AST_CONST_INT 45 // INT CONSTANT #define AST_CONST_BOOL 46 // BOOL CONSTANT From 989b122eda401aeb9f33c2521548257969430ce2 Mon Sep 17 00:00:00 2001 From: Krishna-13-cyber Date: Fri, 12 Aug 2022 00:00:25 +0530 Subject: [PATCH 6/6] Updated the bitwise-shift assignment operators by fixing the known changes --- src/lexer.l | 2 ++ src/parser.y | 12 ++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/lexer.l b/src/lexer.l index 6ec47df..4417816 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -324,9 +324,11 @@ comma ([,]) {assignment_operator} { return OPR_ASSIGNMENT; } + {assignment_bwl_operator} { return OPR_BWL_ASSIGNMENT; } + {assignment_bwr_operator} { return OPR_BWR_ASSIGNMENT; } diff --git a/src/parser.y b/src/parser.y index e20b9b4..ab9f7f1 100644 --- a/src/parser.y +++ b/src/parser.y @@ -474,7 +474,7 @@ bwl_assignment: INT_IDENTIFIER OPR_BWL_ASSIGNMENT arithmetic_expression SEMICOLO $1->value = $1->value << $3->value; $$ = create_bwl_assignment_node($1, $3); - printf("%s <<= %d\n", $1->identifier, $1->value); + printf("%s := %d\n", $1->identifier, $1->value); } | BOOL_IDENTIFIER OPR_BWL_ASSIGNMENT boolean_expression SEMICOLON { if ($1 == NULL) @@ -496,7 +496,7 @@ bwl_assignment: INT_IDENTIFIER OPR_BWL_ASSIGNMENT arithmetic_expression SEMICOLO $1->value = $1->value << $3->value; $$ = create_bwl_assignment_node($1, $3); - printf("%s <<= %d\n", $1->identifier, $1->value); + printf("%s := %d\n", $1->identifier, $1->value); } | CHAR_IDENTIFIER OPR_BWL_ASSIGNMENT arithmetic_expression SEMICOLON { if ($1 == NULL) @@ -518,7 +518,7 @@ bwl_assignment: INT_IDENTIFIER OPR_BWL_ASSIGNMENT arithmetic_expression SEMICOLO $1->value = $1->value << $3->value; $$ = create_bwl_assignment_node($1, $3); - printf("%s <<= %c\n", $1->identifier, $1->value); + printf("%s := %c\n", $1->identifier, $1->value); } ; @@ -542,7 +542,7 @@ bwr_assignment: INT_IDENTIFIER OPR_BWR_ASSIGNMENT arithmetic_expression SEMICOLO $1->value = $1->value >> $3->value; $$ = create_bwr_assignment_node($1, $3); - printf("%s >>= %d\n", $1->identifier, $1->value); + printf("%s := %d\n", $1->identifier, $1->value); } | BOOL_IDENTIFIER OPR_BWR_ASSIGNMENT boolean_expression SEMICOLON { if ($1 == NULL) @@ -564,7 +564,7 @@ bwr_assignment: INT_IDENTIFIER OPR_BWR_ASSIGNMENT arithmetic_expression SEMICOLO $1->value = $1->value >> $3->value; $$ = create_bwr_assignment_node($1, $3); - printf("%s >>= %d\n", $1->identifier, $1->value); + printf("%s := %d\n", $1->identifier, $1->value); } | CHAR_IDENTIFIER OPR_BWR_ASSIGNMENT arithmetic_expression SEMICOLON { if ($1 == NULL) @@ -586,7 +586,7 @@ bwr_assignment: INT_IDENTIFIER OPR_BWR_ASSIGNMENT arithmetic_expression SEMICOLO $1->value = $1->value >> $3->value; $$ = create_bwr_assignment_node($1, $3); - printf("%s >>= %c\n", $1->identifier, $1->value); + printf("%s := %c\n", $1->identifier, $1->value); } ;