Skip to content

Commit

Permalink
Fixed Merge Conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
TALLURI SAI TEJA committed Apr 14, 2019
2 parents f03479f + 53550c7 commit 8ff88ba
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 9 deletions.
7 changes: 7 additions & 0 deletions A6/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/*.c.*
mysclp
parser.tab.c
parser.tab.h
lex.yy.c
.vscode/*
A4_tests/*.c.*
10 changes: 10 additions & 0 deletions A6/README
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ For the AST you need to create objects of the following classes:
- Return_ASt

You will need to submit ONLY the scanner.l, parser.y, ast.cc, local-environment.cc, ast-eval.cc, ast-compile.cc, and icode.cc files.


TODO:

Add support for call and return:

modify grammar to support function declarations, function calls, return statements.
write ast functions for call and return asts.
eval
write compile functions for call and return asts.
4 changes: 2 additions & 2 deletions A6/ast-compile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ template class Number_Ast<int>;
Code_For_Ast & Ast::create_store_stmt(Register_Descriptor * store_register) {

}
//TODO: release registers

Code_For_Ast & Assignment_Ast::compile() {
Code_For_Ast rhs_res = rhs->compile();
Expand Down Expand Up @@ -300,6 +299,7 @@ Code_For_Ast & Conditional_Expression_Ast::compile() {
output->get_icode_list().insert(output->get_icode_list().end(),rhs_code.get_icode_list().begin(), rhs_code.get_icode_list().end());
output->append_ics(*rhs_or_stmt);
output->append_ics(*label2);

lhs_result->get_reg()->reset_use_for_expr_result(); //Free the registers
rhs_result->get_reg()->reset_use_for_expr_result(); //Free the registers
cond_result->get_reg()->reset_use_for_expr_result(); //Free the registers
Expand Down Expand Up @@ -409,6 +409,7 @@ Code_For_Ast & Logical_Expr_Ast::compile() {
Move_IC_Stmt *load_one_stmt;
Const_Opd<int> *constant;


/* not is a special case. For !x, load 1, give operands x and a to not(in that order). NOT NULL.
not_t in assembly is sltu. That's the reason for this weird behaviour.
*/
Expand Down Expand Up @@ -491,7 +492,6 @@ Code_For_Ast & Selection_Statement_Ast::compile() {
}

Code_For_Ast & Iteration_Statement_Ast::compile() {

Code_For_Ast cond_code = this->cond->compile();
Label_IC_Stmt * label1 = new Label_IC_Stmt(j, this->get_new_label());
Label_IC_Stmt * label2 = new Label_IC_Stmt(j, this->get_new_label());
Expand Down
82 changes: 79 additions & 3 deletions A6/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ template class Number_Ast<double>;
template class Number_Ast<int>;

#include "symbol-table.hh"

#include "icode.hh"

Ast::Ast(){}

Expand All @@ -22,7 +22,7 @@ void Ast::set_data_type(Data_Type dt){
}

bool Ast::is_value_zero() {
return false; /* TODO: I'm writing this to mitigate errors in check_ast. */
return false;
}

bool Ast::check_ast(){}
Expand Down Expand Up @@ -496,11 +496,13 @@ void Sequence_Ast::print(ostream & file_buffer){
}
}


Print_Ast::Print_Ast(Ast *v, int line) {
this->var = v;
this->lineno = line;
}


Print_Ast::~Print_Ast() {}

void Print_Ast::print(ostream & file_buffer) {
Expand Down Expand Up @@ -547,4 +549,78 @@ void Call_Ast::set_actual_param_list(list<Ast *> & param_list){
actual_param_list = param_list;
}

void Call_Ast::print(ostream & file_buffer){}
void Call_Ast::print(ostream & file_buffer){}


/*call*/
Call_Ast::Call_Ast(string name, int line) {
this->procedure_name = name;
this->lineno = line;
}

Call_Ast::~Call_Ast() {}

Data_Type Call_Ast::get_data_type() {
return this->node_data_type;
}

void Call_Ast::set_register(Register_Descriptor * reg) {
this->return_value_reg = reg;
}

void Call_Ast::check_actual_formal_param(Symbol_Table & formal_param_list) {
list<Ast*>::iterator it = this->actual_param_list.begin();
for(; it != this->actual_param_list.end(); it++) {
Symbol_Table_Entry actual_var = (*it)->get_symbol_entry();
if(formal_param_list.variable_in_formal_list_check(actual_var.get_variable_name())) {
if(formal_param_list.get_symbol_table_entry(actual_var.get_variable_name()).get_data_type()
!= actual_var.get_data_type() )
cerr << "cs316: Error: Line "<<lineno<<": Actual and formal parameters do not match\n";
}
}
}

void Call_Ast::set_actual_param_list(list<Ast *> & param_list) {
this->actual_param_list = param_list;
}

void Call_Ast::print(ostream & file_buffer) {
//TODO:
}


Return_Ast::Return_Ast(Ast * ret_val, string name, int line) {
this->return_value = ret_val;
this->proc_name = name;
this->lineno = line;
}


Return_Ast::~Return_Ast() {}


Data_Type Return_Ast::get_data_type() {
return this->node_data_type();
}

void Return_Ast::print(ostream & file_buffer) {
//TODO:
}

















>>>>>>> 53550c70c0f75696d7871e6cdb2c347dda605cc5
1 change: 1 addition & 0 deletions A6/ast.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include<iomanip>
#include<typeinfo>
#include<list>
#include "reg-alloc.hh"

#define AST_SPACE " "
#define AST_NODE_SPACE " "
Expand Down
108 changes: 104 additions & 4 deletions A6/parser.y
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%{
#include "program.hh"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -60,15 +61,112 @@

%%

program : global_variable_declaration_list procedure_definition
// program : global_variable_declaration_list procedure_definition
// {
// (*global_sym_table).set_table_scope(global);
// program_object.set_global_table(*global_sym_table);
// program_object.set_procedure($2, yylineno);
// }
// ;

program : declaration_list definition_list
{
(*global_sym_table).set_table_scope(global);
program_object.set_global_table(*global_sym_table);
program_object.set_procedure($2, yylineno);
}
;
;

declaration_list : declaration_list procedure_declaration
{

}
| declaration_list variable_declaration
{

}
| /*empty */
{

procedure_definition : VOID NAME '(' ')'
}
;

procedure_declaration : VOID NAME '(' param_list ')' ';'
{
$$ = new Procedure(void_data_type, *$2, yylineno);
program_object.set_proc_to_map(*$2, $$);
}
| INTEGER NAME '(' param_list ')' ';'
{
$$ = new Procedure(int_data_type, *$2, yylineno);
program_object.set_proc_to_map(*$2, $$);
}
| FLOAT NAME '(' param_list ')' ';'
{
$$ = new Procedure(float_data_type, *$2, yylineno);
program_object.set_proc_to_map(*$2, $$);
}
;

declaration_param_list : /* empty */
{
$$ = new Symbol_Table();
}
| declaration_param_list ',' INTEGER NAME
{
/* TODO */
}
| declaration_param_list ',' INTEGER
{
/* TODO */
}
| declaration_param_list ',' FLOAT NAME
{
/* TODO */
}
| declaration_param_list ',' FLOAT
{
/* TODO */
}
;

param_list : /* empty */
{
$$ = new Symbol_Table();
}
| param_list ',' INTEGER NAME
{
Symbol_Table_Entry *ste = new Symbol_Table_Entry();
/*TODO: */
}
| param_list ',' FLOAT NAME
{
/*TODO: */
}
;

procedure_definition_list : procedure_definition
{

}
| procedure_definition_list procedure_definition
{

}
;

// procedure_definition : VOID NAME '(' ')'
// '{'
// local_variable_declaration_list statement_list
// '}'
// {
// $$ = new Procedure(void_data_type,*$2, yylineno);
// (*local_sym_table).set_table_scope(local);
// (*$$).set_local_list((*local_sym_table));
// (*$$).set_ast_list(*($7));
// }
// ;

procedure_definition : VOID NAME '(' param_list ')'
'{'
local_variable_declaration_list statement_list
'}'
Expand All @@ -77,6 +175,8 @@ procedure_definition : VOID NAME '(' ')'
(*local_sym_table).set_table_scope(local);
(*$$).set_local_list((*local_sym_table));
(*$$).set_ast_list(*($7));
local_sym_table = new Symbol_Table;
program_object.set_procedure($$, yylineno);
}
;

Expand Down
8 changes: 8 additions & 0 deletions A6/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ if_kw if
do_kw do
else_kw else

return_kw return

/*relational operators*/
eq ==
lt <
Expand Down Expand Up @@ -115,6 +117,12 @@ print_kw print
return PRINT;
}

/*return*/
{return_kw} {
store_token_name("RETURN", yytext, yylineno);
return RETURN;
}

{name} {
/* cout<<yytext<<endl; */
store_token_name("NAME", yytext, yylineno);
Expand Down
7 changes: 7 additions & 0 deletions A6/test_case.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ void func1();
float f1,f2;
int func2(int,float,int);
float func3(int p,float q,float r);
int function();
// void main();

int func4(){
i1=4;
Expand All @@ -19,13 +21,15 @@ int func2(int a,float b,int i1){
i1=5;
return a+i1+i2+2;
}
// void func5(int x);

float func3(int a,float b,float c){
b =b+f2;
c=c+10.5;
return b+c;
}


void main(){
func1();
// print i2; print f2;
Expand All @@ -36,4 +40,7 @@ void main(){
f2 = func3(i1,f1,f2);
print f1; print f2;

}
void func5(int x) {
x = 2;
}

0 comments on commit 8ff88ba

Please sign in to comment.