Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added subtract-assignment operator #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define AST_NODE_DECLARATION 6
#define AST_NODE_ARRAY_DECLARATION 2000
#define AST_NODE_ASSIGNMENT 7
#define AST_NODE_SUB_ASSIGNMENT 5005
#define AST_NODE_ARRAY_ASSIGNMENT 2001
#define AST_NODE_ARRAY_ACCESS 2002
#define AST_NODE_ARITHMETIC_EXP 8
Expand Down Expand Up @@ -59,6 +60,7 @@
#define AST_OPR_LGL_OR 43 // or

#define AST_OPR_ASSIGNMENT 44 // :=
#define AST_OPR_SUB_ASSIGNMENT 5006 // -=

#define AST_CONST_INT 45 // INT CONSTANT
#define AST_CONST_BOOL 46 // BOOL CONSTANT
Expand Down Expand Up @@ -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_sub_assignment;
struct ast_node_array_assignment;
struct ast_node_array_access;
struct ast_node_expression;
Expand All @@ -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_sub_assignment ast_node_sub_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;
Expand Down Expand Up @@ -160,6 +164,7 @@ struct ast_node
ast_node_declaration *declaration;
ast_node_array_declaration *array_declaration;
ast_node_assignment *assignment;
ast_node_sub_assignment *sub_assignment;
ast_node_array_assignment *array_assignment;
ast_node_conditional_if *if_else;
ast_node_loop_for *loop_for;
Expand Down Expand Up @@ -205,6 +210,14 @@ struct ast_node_array_declaration
ast_node_expression *expression;
};

struct ast_node_sub_assignment
{
int node_type;

sym_ptr symbol_entry;
ast_node_expression *expression;
};

struct ast_node_array_assignment
{
int node_type;
Expand Down Expand Up @@ -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_sub_assignment *create_sub_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);
Expand Down
2 changes: 2 additions & 0 deletions include/code_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#define _OPR_LGL_OR " | "

#define _OPR_ASSIGNMENT " = "
#define _OPR_SUB_ASSIGNMENT " -= "

#define _DT_INT_ "int"
#define _DT_VOID_ "void"
Expand All @@ -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_sub_assignment_printer(ast_node_sub_assignment *sub_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);
Expand Down
15 changes: 15 additions & 0 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ ast_node_statements *create_statement_node(int node_type, void *child)
stmt->child_nodes.assignment = child;
break;

case AST_NODE_SUB_ASSIGNMENT:
stmt->child_nodes.sub_assignment = child;
break;

case AST_NODE_ARRAY_ASSIGNMENT:
stmt->child_nodes.array_assignment = child;
break;
Expand Down Expand Up @@ -144,6 +148,17 @@ ast_node_assignment *create_assignment_node(sym_ptr symbol, ast_node_expression
return assgn;
}

ast_node_sub_assignment *create_sub_assignment_node(sym_ptr symbol, ast_node_expression *exp)
{
ast_node_sub_assignment *sub_assgn = (ast_node_sub_assignment*)malloc(sizeof(ast_node_sub_assignment));

sub_assgn->node_type = AST_NODE_ASSIGNMENT;
sub_assgn->expression = exp;
sub_assgn->symbol_entry = symbol;

return sub_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));
Expand Down
21 changes: 20 additions & 1 deletion src/code_printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_SUB_ASSIGNMENT:
ast_sub_assignment_printer(((ast_node_statements*)temp)->child_nodes.sub_assignment, handle);
break;

case AST_NODE_ARRAY_ASSIGNMENT:
ast_array_assignment_printer(((ast_node_statements*)temp)->child_nodes.array_assignment, handle);
break;
Expand Down Expand Up @@ -174,6 +178,17 @@ void ast_assignment_printer(ast_node_assignment *assg, FILE* handle)

}

void ast_sub_assignment_printer(ast_node_sub_assignment *sub_assg, FILE* handle)
{
if (sub_assg != NULL && handle != NULL)
{
fprintf(handle, "\t%s -= ", sub_assg->symbol_entry->identifier);
ast_expression_printer(sub_assg->expression, handle);
fprintf(handle, "%s", ";\n");
}

}

void ast_array_assignment_printer(ast_node_array_assignment *assign, FILE* handle)
{
if (assign != NULL && handle != NULL)
Expand Down Expand Up @@ -696,7 +711,11 @@ 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_SUB_ASSIGNMENT:
ast_sub_assignment_printer(((ast_node_statements*)temp)->child_nodes.sub_assignment, handle);
break;

case AST_NODE_ARRAY_ASSIGNMENT:
ast_array_assignment_printer(((ast_node_statements*)temp)->child_nodes.array_assignment, handle);
break;
Expand Down
5 changes: 5 additions & 0 deletions src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ bitwise_operators ("~"|"&"|"|"|">>"|"<<")
logical_operators ("not"|"and"|"or")
function ("return"|"void"|"def")
assignment_operator ([:][=])
sub_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_]*)
Expand Down Expand Up @@ -323,6 +324,10 @@ comma ([,])
return OPR_ASSIGNMENT;
}

{sub_assignment_operator} {
return OPR_SUB_ASSIGNMENT;
}

{io} {
if (!strcmp(yytext, "digital_write"))
{
Expand Down
74 changes: 74 additions & 0 deletions src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -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_sub_assignment *sub_assignment;
struct ast_node_array_assignment *array_assignment;
struct ast_node_array_access *array_access;
struct ast_node_expression *expression;
Expand Down Expand Up @@ -75,6 +76,7 @@ ast_node *ast = NULL;
%right OPR_BW_NOT OPR_LGL_NOT

%token OPR_ASSIGNMENT
%token OPR_SUB_ASSIGNMENT

%token SEMICOLON COLON COMMA

Expand Down Expand Up @@ -106,6 +108,7 @@ ast_node *ast = NULL;
%type <declaration> declaration declaration_assignment
%type <array_declaration> array_declaration array_declaration_assignment
%type <assignment> assignment
%type <sub_assignment> sub_assignment
%type <expression> arithmetic_expression boolean_expression relational_expression logical_expression return_statement function_call_datatypes
%type <range_expression> range_expression
%type <array_assignment> array_assignment
Expand Down Expand Up @@ -183,6 +186,9 @@ statement: compound_statement {
| assignment {
$$ = create_statement_node(AST_NODE_ASSIGNMENT, (void*)$1);
}
| sub_assignment {
$$ = create_statement_node(AST_NODE_SUB_ASSIGNMENT, (void*)$1);
}
| array_assignment {
$$ = create_statement_node(AST_NODE_ARRAY_ASSIGNMENT, (void*)$1);
}
Expand Down Expand Up @@ -442,6 +448,74 @@ assignment: INT_IDENTIFIER OPR_ASSIGNMENT arithmetic_expression SEMICOLON {
}
;

sub_assignment: INT_IDENTIFIER OPR_SUB_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_sub_assignment_node($1, $3);

printf("%s := %d\n", $1->identifier, $1->value);
}
| BOOL_IDENTIFIER OPR_SUB_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_sub_assignment_node($1, $3);

printf("%s := %d\n", $1->identifier, $1->value);
}
| CHAR_IDENTIFIER OPR_SUB_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_sub_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)
{
Expand Down
4 changes: 4 additions & 0 deletions tests/run_anywhere/subassign.sim
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int a := 5;
int b := 10;
b-=a;
print(a);
1 change: 1 addition & 0 deletions tests/run_anywhere/subassign.sim.output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5