示例#1
0
StateMachinePtr construct_asm(BindingsPtr bindings, NodePtr root,
      const AbstractStateMachineTable& asmt) {
   assert(root && root->get_op() == Op::abstract_state_machine);
   assert(root->size() == 4 || root->size() == 5);
   int argi = 0;
   std::string name = root->get_operand(argi++)->get_token().get_text();
   std::string base_sm;
   if (root->size() == 5) {
      base_sm = root->get_operand(argi++)->get_token().get_text();
   }
   NodePtr vars = root->get_operand(argi++);
   NodePtr funcs = root->get_operand(argi++);
   NodePtr rules = root->get_operand(argi++);
   StateMachinePtr sm = std::make_shared<StateMachine>(bindings, name);
   if (base_sm != "") {
      AbstractStateMachineTable::const_iterator it = asmt.find(base_sm);
      if (it == asmt.end()) {
	 throw Exception(root->get_operand(1)->get_location(),
	    "no such abstract state machine");
      }
      sm->import_asm(it->second);
   }
   assert(vars->get_op() == Op::sm_vars);
   if (vars->size() > 0) {
      add_vars(sm, vars->get_operand(0));
   }
   assert(funcs->get_op() == Op::sm_functions);
   if (funcs->size() > 0) {
      add_functions(sm, funcs->get_operand(0));
   }
   add_rules(sm, bindings, rules);
   return sm;
}
示例#2
0
static void add_functions(StateMachinePtr sm, NodePtr root) {
   if (root->get_op() == Op::sm_function_definitions) {
      add_functions(sm, root->get_operand(0));
      root = root->get_operand(1);
   }
   assert(root->get_op() == Op::function_definition);
   std::string fname = root->get_operand(0)->get_token().get_text();
   sm->add_local_function(fname, root->get_operand(1));
}
示例#3
0
void
MVM_add_executable(MVM_VirtualMachine *mvm, MVM_Executable *executable)
{
    int i;

    mvm->executable = executable;
    add_functions(mvm, executable);
    convert_code(mvm, executable, executable->code,
                 executable->code_size, NULL);
    for (i = 0; i < executable->function_count; i++) {
        convert_code(mvm, executable, executable->function[i].code,
                     executable->function[i].code_size, &executable->function[i]);
    }
    add_static_variables(mvm, executable);
}
示例#4
0
int					convert_to_binary(t_function *functions, char *filename)
{
	t_list		*bytes;
	t_list		*bytes_end;
	t_list		*size_bytes;

	bytes = NULL;
	bytes_end = NULL;
	if (!(add_exec_magic_to_bytes(&bytes, &bytes_end)))
		return (0);
	if (!(size_bytes = add_header_to_bytes(functions, &bytes_end)))
		return (0);
	if (!(add_functions(functions->next, &bytes_end)))
		return (0);
	if (!(update_size_bytes(size_bytes, functions->next)))
		return (0);
	return (write_bytes_to_file(filename, bytes));
}
示例#5
0
void higher_order_functions() {
  constexpr auto h = add_functions(2*x, x+2);
  STATIC_ASSERT_EQUALS(h(x=5), (2*x + x+2)(x=5));
}
示例#6
0
文件: tree.c 项目: esiqveland/skole
void bind_names ( node_t* root )
{
    /* TODO: bind tree nodes to symtab entries */

	node_t* child;
	if(root != NULL) {
		for(int i = 0; i < root->n_children; i++) {
			child = root->children[i];

            int counter = 0;

            if(child == NULL)
                continue; // move along, nothing to see here

			switch(child->type.index) {
                case FUNCTION_LIST:
                    // add an upper scope to hold the functions (can functions be defined in a function?)
                    // need this so that the scope in another function can know about all the other functions that are defined.
                    scope_add();
                    for(int i = 0; i < child->n_children; i++) {
                        // populate symbol table with known functions
                        add_functions(child->children[i]);
                    }
                    bind_names(child);
                    scope_remove();
                    break;

                case FUNCTION: ;
                    /* reset depth, reset offset, add arguments */
                    dybde = 0;
                    local_offset = -4;
                    scope_add();

                    // add vars in the var list under (arguments)
                    node_t* varlist = child->children[1];
                    if( varlist != NULL) {
                        int count = 8+(4*(varlist->n_children-1));
                        for( int i = 0; i < varlist->n_children; i++ ) {
                            char* key = varlist->children[i]->data;

                            symbol_t* s = malloc(sizeof(symbol_t));
                            s->stack_offset = count;
                            count -= 4;
                            s->n_args = 0;
                            s->depth = dybde;
                            s->label = key;

                            symbol_insert( key, s );
                        }
                    }

                    // traverse child 2 ( a BLOCK )
                    bind_names(child->children[2]);
                    scope_remove();
                    break;

                case BLOCK: ;
                    /* start new scope, add depth */
					scope_add();
                    dybde++;
                    local_offset = -4;
					bind_names( child );
					dybde--;
					scope_remove();
					break;

                case DECLARATION_LIST: ;
					for( int dec = 0; dec < child->n_children; dec++) {
						node_t* declaration = child->children[dec];
						// add vars in the var list under
						node_t* varlist = declaration->children[0];

                        for( int i = 0; i < varlist->n_children; i++ ) {
                            char* key = varlist->children[i]->data;

							symbol_t* s = malloc(sizeof(symbol_t));
                            s->stack_offset = local_offset;
                            local_offset -= 4;
							s->n_args = 0;
							s->depth = dybde;
							s->label = key;

							symbol_insert( key, s );
						}
					}
                    //return; // safe to return now ?
                    //bind_names(child); // not needed?
					break;

                case VARIABLE: ;
                    char* key = child->data;
                    symbol_t* temp = NULL;
                    symbol_get( &temp, key );
                    if( temp != NULL ) {
                        child->entry = temp;
                    } else { // symbol not yet declared, do some error ?
                        fprintf( stderr, "Error: Symbol \"%s\" not yet declared\n", key );
                    }
					bind_names( child );
					break;
                case TEXT: ;
					/* set data to index in the table of strings */
                    uint32_t* index = malloc(sizeof(uint32_t));
                    *index = strings_add(child->data);
                    child->data = index;
                    break;

                default: ;
					bind_names( child );
			}
		}
	}	
}
示例#7
0
void main(int argc, char *argv[]){

			FILE *fp;
			FILE *fp2;
			buffer B;
			FILE *filename,*filename2;
			ast a;
			FPTR fptr;
			SPTR sptr;
		//	printf("Hello inside main\n");
			sptr=symbol_table_create(20,20);
			fptr=global(sptr);
	//		printf("DOne\n");
				char testcopy[]="testcopy.txt";
				parse_Tree PT;
				int T[52][49];
				
				filename=fopen(argv[2],"w");
			//	filename2=fopen(argv[3],"w");
				fp2=fopen("testcopy.txt","r");
				fp=fopen(argv[1],"r");			
			int num;
		printf("Enter the desired Option !!\n\n");
		do{
		
		printf("  1 : Select Option 1 for Lexical Analysis \n  2 : Select Option 2 for Parse Tree \n  3 : Select Option 3 for Abstract Syntax Tree \n  4 : Select Option 4 for Symbol Table \n  5 : Select Option 5 for Semantic Analysis \n  6 : Select Option 6 for Code Generation\n");
		scanf("%d",&num);
		switch(num){
			case 1:
		
				
				//line_number=1;
				//int k=BUFFERSIZE;
				buf=0;
				ahead=0;
				token_info TI;
				printf("***************************LEXICAL ANALYSIS***************************\n\n");
				printf("   LEXEME_NAME    |     TOKEN_NAME  |     LINE_NUMBER |\n\n");
				fprintf(filename,"***************************LEXICAL ANALYSIS***************************\n\n");
				fprintf(filename,"   LEXEME_NAME    |     TOKEN_NAME  |     LINE_NUMBER |\n\n");
				TI=find_next_token(fp,B);
				while(strcmp(TI.lexeme_name,"$")!=0){
		
				printf("| %15s | %15s |    %d\n",TI.lexeme_name,TI.token_name,TI.line);
				fprintf(filename,"| %15s | %15s |    %d\n",TI.lexeme_name,TI.token_name,TI.line);
				TI=find_next_token(fp,B);
				}
				TI=find_next_token(fp,B);
				printf("| %15s | %15s |    %d\n",TI.lexeme_name,TI.token_name,TI.line);
				fprintf(filename,"| %15s | %15s |    %d\n",TI.lexeme_name,TI.token_name,TI.line);
				printf("\n\n\n");
				
				
	
			break;
	
	
			case 2:
						
				create_parsing_table(T);
				PT=parse_input(argv[1],T);
			//	printf("end of parsing\n");
				PT->temp=PT->head;
			//	printf("end of parsing2222\n");
				
				printf("*************************************Printing the Parse Tree*************************************\n\n\n");
	            printf("|           LEXEME_NAME|         TOKEN_NAME   |         PARENT_NODE |       IS_LEAF_NODE |      LINE NUMBER  |\n\n\n");
	           fprintf(filename,"**********************************Printing the Parse Tree**********************************\n\n\n");
	           fprintf(filename,"|           LEXEME_NAME |         TOKEN_NAME |         PARENT_NODE |       IS_LEAF_NODE |      LINE NUMBER |\n\n\n");
				parse_tree_print(PT,filename);
			return;
			 case 3: 
			 
			    create_parsing_table(T);
				PT=parse_input(argv[1],T);
			//	printf("end of parsing\n");
				PT->temp=PT->head;
				create_ast(PT,a);
				printf("*************************************Printing the AST Tree *************************************\n\n\n");
	printf(" |LEXEME_NAME         |TOKEN_NAME          |PARENT_NODE         |IS_LEAF_NODE        |LINE NUMBER\n\n\n");
	fprintf(filename,"*************************************Printing the AST Tree*************************************\n\n\n");
	fprintf(filename," |LEXEME_NAME         |TOKEN_NAME          |PARENT_NODE         |IS_LEAF_NODE        |LINE NUMBER\n\n\n");
				
				ast_print(PT,filename);
				return;
			case 4:
			
			//printf("Hello inside case 4\n");
				create_parsing_table(T);
				PT=parse_input(argv[1],T);
			//	printf("end of parsing\n");
				PT->temp=PT->head;
				create_ast(PT,a);
				//ast_print(PT,filename2);
				PT->temp=PT->head;
				
				add_functions(sptr,fptr,PT);
				
				printf("\n\n***********************************************Printing the Symbol Table*************************************************\n\n\n");
	printf("     LEXEME_NAME     |         TOKEN_NAME   |        SCOPE    |     DATA_TYPE   |     SIZE   |    OFFSET  |   LINE NUMBER\n\n\n");
	fprintf(filename,"*************************************Printing the Symbol Table***************************************\n\n\n");
	fprintf(filename,"     LEXEME_NAME     |         TOKEN_NAME  |           SCOPE    |     DATA_TYPE   |     SIZE   |    OFFSET  |   LINE NUMBER\n\n\n");
				symbol_table_print(sptr,filename);
				
				printf("\n\n");
				fprintf(filename,"\n\n");
				return;
			
			case 5: 
			//printf("Hello inside case 4\n");
				create_parsing_table(T);
				PT=parse_input(argv[1],T);
			//	printf("end of parsing\n");
				PT->temp=PT->head;
				create_ast(PT,a);
				//ast_print(PT,filename2);
				PT->temp=PT->head;
				
				add_functions(sptr,fptr,PT);
				PT->temp=PT->head;
				check_semantic(PT,fptr,sptr);
				return;
				
			case 6 :
			
				create_parsing_table(T);
				PT=parse_input(argv[1],T);
			//	printf("end of parsing\n");
				PT->temp=PT->head;
				create_ast(PT,a);
				//ast_print(PT,filename2);
				PT->temp=PT->head;
				
				add_functions(sptr,fptr,PT);
				PT->temp=PT->head;
				assembly_code(PT,sptr,filename);
				return;
			
			
			
			
			
			default:
				printf("\nWrong choice!!!Enter Between 1 to 4");
		}
		
	} while(1);
	
	fclose(fp);
		
	fclose(filename);
//	fclose(filename2);
	fclose(fp2);
	
	
	return 0;
}