示例#1
0
int main() {
  struct _type_t var1, var2, var3, *var4;
  _i32 i = 10, j = 20;

  ast.env.n_vars = 0;
  new_var(&ast, &var1, "variable1", 1, &i);
  new_var(&ast, &var2, "variable2", 1, &j);
  new_var(&ast, &var3, "str", STR, "hello world");

  PRINT_INTEGER(var1);
  PRINT_INTEGER(var2);
  PRINT_STR(var3);

  if (get_var(&ast, &var4, "variable1")) {
    PRINT_VAR((*var4));
  } else {
    printf("failed to get variable\n");
  }

  assign(&ast, "variable1", 15);

  PRINT_VAR(var1);

  assign(&ast, "variable1", 20);

  PRINT_VAR(var1);
  
  return 0;
}
示例#2
0
文件: copy.cpp 项目: kebekus/srt
struct node *copy_branch(struct tree *dst, struct node *src)
{
    switch (src->token) {
    case token_x:
        return new_var(dst, token_x);
    case token_y:
        return new_var(dst, token_y);
    case token_z:
        return new_var(dst, token_z);
    case token_a:
        return new_var(dst, token_a);
    case token_num:
        return new_num(dst, src->value);
    case token_pow:
        return new_pow(dst, copy_branch(dst, src->left), src->value);
    case token_mul:
        return new_node(dst, copy_branch(dst, src->left), token_mul, copy_branch(dst, src->right));
    case token_add:
        return new_node(dst, copy_branch(dst, src->left), token_add, copy_branch(dst, src->right));
    case token_sub:
        return new_node(dst, copy_branch(dst, src->left), token_sub, copy_branch(dst, src->right));
    case token_neg:
        return new_node(dst, 0, token_neg, copy_branch(dst, src->right));
    case token_sqrt:
        return new_node(dst, 0, token_sqrt, copy_branch(dst, src->right));
    case token_err:
        return 0;
    default:
        error::set_num(error::unknown_token);
        return 0;
    }
}
示例#3
0
文件: Aiger.hpp 项目: anithag/kleestr
      unsigned aiger_add_xnor ( aiger *aig, unsigned lhs, unsigned rhs )
      {
        result_type t1 = new_var ();
        result_type t2 = new_var ();

        aiger_add_and ( aig, t1, aiger_not ( lhs ), aiger_not ( rhs ) ); 
        aiger_add_and ( aig, t2, lhs, rhs ); 

        return aiger_add_or ( aig, t1, t2 ); 
      }
示例#4
0
文件: var.cpp 项目: Grainspring/xoc
/*
Return VAR if there is already related to 's',
otherwise create a new VAR.
'var_name': name of the variable, it is optional.
's': string's content.
*/
VAR * VAR_MGR::register_str(CHAR const* var_name, SYM * s, UINT align)
{
	IS_TRUE0(s != NULL);
	VAR * v;
	if ((v = m_str_tab.get(s)) != NULL) {
		return v;
	}
	v = new_var();
	CHAR buf[64];
	if (var_name == NULL) {
		sprintf(buf, ".rodata_%zu", m_str_count++);
		VAR_name(v) = m_ru_mgr->add_to_symtab(buf);
	} else {
		VAR_name(v) = m_ru_mgr->add_to_symtab(var_name);
	}
	VAR_str(v) = s;
	VAR_data_type(v) = D_STR;
	VAR_elem_type(v) = D_UNDEF;
	IS_TRUE0(sizeof(CHAR) == 1);
	VAR_data_size(v) = xstrlen(SYM_name(s)) + 1;
	VAR_align(v) = align;
	VAR_is_global(v) = 1; //store in .data or .rodata

	assign_var_id(v);
	m_str_tab.set(s, v);
	return v;
}
示例#5
0
static void
new_arena( SV * sva, const void *p ) {
  const where *w = p;
  int err;
  /*fprintf(stderr, "%s, line %d: New arena: %p\n", (const char *) (w + 1), w->line, sva); */
  SV *sv = sva + 1;
  SV *svend = &sva[SvREFCNT( sva )];

  while ( sv < svend ) {
    if ( live_sv( sv ) ) {
      /* New variable */
      new_var( sv, w );
    }
    else {
      /* Pretend any new free SVs were already in the free list otherwise
       * when we compare the new free list with the old one it'll look as
       * if lots of variables that never existed have been freed.
       */
      if ( err = list_append( &current_free, sv ), ERR_None != err ) {
        nomem(  );
      }
    }
    ++sv;
  }
  /*fprintf(stderr, "%s, line %d: End new arena: %p\n", (const char *) (w + 1), w->line, sva); */
}
示例#6
0
pgxc_ctl_var *confirm_var(char *name)
{
	pgxc_ctl_var *rc;
	if ((rc = find_var(name)))
		return rc;
	return new_var(name);
}
示例#7
0
intercodes translate_VarDec(struct Node *node,operand *place) {
	intercodes code1,code2,code3,code4;
	struct Node *child = node->child;
	assert(child != NULL);
	//ID
	if (strcmp(child->type,"ID") == 0) {
		int id = getVarID(child);
		enum varType type;
		union varp vp = getVar(child,&type);
		operand v1 = new_var(id);
		if (place != NULL) {
			*place = v1;
		}
		if (type == varInt || type == varFloat) { 
			return NULL;
		}
		else if (type == varArray || type == varStruct) {
			int size = get_structure_size(vp,type);
			operand c1 = new_constant(size);
			code1 = gen_assign(DEC_K,v1,c1);
			return code1;
		}
		else {
			printf("type is varError%d\n",type == varError);
			exit(-1);
		}
	}
	//VarDec INT LB INT RB
	return translate_VarDec(child,NULL);
}
示例#8
0
VariableSymbol* NodeBuilder::get_cstr_const_var(String s, ProcedureDefinition* p)
{
  NodeBuilder nb(p->get_symbol_table());
  MultiValueBlock* vb = nb.new_cstr_value_block(s);
  VariableSymbol* var = new_var("", vb->get_type());
  nb.add_variable_definition(p, var, vb);
  return var;
}
示例#9
0
文件: Aiger.hpp 项目: anithag/kleestr
 unsigned aiger_add_ite ( aiger *aig, unsigned I, unsigned T, unsigned E )
 {
   result_type t = new_var (); 
   aiger_add_and ( aig 
       , t
       , aiger_add_or ( aig, aiger_not (I), T )
       , aiger_add_or ( aig, I, E ) );
   return t;
 }
示例#10
0
文件: main.c 项目: npc3/DumbLisp
int main(int argc, char **argv) {
    char *file_to_eval = NULL;
    int replize = argc < 1;
    for(int i = 1; i < argc; i++) {
        if(!strcmp("-f", argv[i]))
            file_to_eval = argv[++i];
        else if(!strcmp("--verbose", argv[i]))
            VERBOSE = true;
        else if(!strcmp("-i", argv[i]))
            replize = true;
    }

    init_alloc_system();
    init_symboltable();
    register_builtin_functions();

    new_var(new_symbol("nil"), (LispObject*)nil);
    new_var(new_symbol("t"), (LispObject*)new_symbol("t"));

    nexception_points++;
    if(setjmp(exception_points[nexception_points - 1]) == 0) {
        eval_file("prelude.l");
        if(file_to_eval)
            eval_file(file_to_eval);
        else
            repl();
    } else {
        fprintf(stderr, "%s", error_string);
        printf("Stack trace:\n");
        for(int i = 0; i < call_stack->size; i++) {
            printf("  ");
            obj_print(vector_getitem(call_stack, i));
            printf("\n");
        }
        while(scopes->size > 1)
            pop_scope();
        while(call_stack->size > 1)
            vector_remove(call_stack, -1);
        if(replize)
            repl();
    }
}
示例#11
0
文件: parse.c 项目: j16r/poseidon
int parse_line(Variable_List * vars, char * line)
{
    int i, j;
    int len;
    int token_len;
    char temp[13];
    
    if((len = check_errors(line)) == ERROR)
    {
	printf("ERROR: Syntax error\n");
        return 0;
    }

    i = 0;
    while(i < len)
    {
	// variable
	if(line[i] == '$')
	{
            i++;
	    for (j = 0; j < length_token(i,line); j++)
                temp[j] = line[i+j];
	    temp[j] = '\0';
	    i+=j;

	    if(find_next(i,'=',line) > 0)
		new_var(vars, temp, INTEGER, 0);
	}
        // statement / function call
	if((line[i] >= 'a' && line[i] <= 'z') ||
	   (line[i] >= 'A' && line[i] <= 'Z') ||
	   line[i] == '_')
	{
	    token_len = length_token(i, line);
	    for (j = 0; j < token_len; j++)
                temp[j] = line[i+j];
	    temp[j] = '\0';
	    i+=j;

	    if (strcmp("list", temp) == 0)
		list_vars(vars);

	    if (strcmp("help", temp) == 0)
		help();

	    if (strcmp("exit", temp) == 0 || strcmp(temp,"quit") == 0)
		return -1;
	
	}
        i++;
    }
    return 0;
}
示例#12
0
void run_program(char** instructions, int n_instructions, var** input_variables, int n_inputs, var** temp_variables, int n_temps, var** output_variables, int n_outputs) {
	var** vars = (var **) malloc(256 * sizeof(var *));
	vars['0'] = new_var('0', 0);
	vars['1'] = new_var('1', 1);
	
	int i;
	for (i = 0; i < n_inputs; i++) {
		vars[(int) (input_variables[i] -> name)] = input_variables[i];
	}

	for (i = 0; i < n_temps; i++) {
		vars[(int) (temp_variables[i] -> name)] = temp_variables[i];
	}

	for (i = 0; i < n_outputs; i++) {
		vars[(int) (output_variables[i] -> name)] = output_variables[i];
	}


	execute_program(instructions, n_instructions, vars);
	print_output(output_variables, n_outputs);
}
示例#13
0
int get_output_variables(char** instructions, int num_instructions, var*** output_variables) {
	char instruction[16];
	char number_of_variables[4];
	char variables[64];
	sscanf(instructions[1], "%s %s %[^\n]\n", instruction, number_of_variables, variables);

	int n_output_variables = atoi(number_of_variables);
	output_variables[0] = (var **) malloc(n_output_variables * sizeof(var *));
	
	int i;
	for (i = 0; i < n_output_variables; i++) {
		output_variables[0][i] = new_var(variables[i*2], 0);
	}
	return n_output_variables;
}
示例#14
0
文件: var.cpp 项目: Grainspring/xoc
//'var_name': name of the variable, it is optional.
VAR * VAR_MGR::register_var(SYM * var_name, DATA_TYPE dt, DATA_TYPE elem_type,
							UINT pointer_base_size, UINT mem_size, UINT align,
							UINT flag)
{
	IS_TRUE0(var_name != NULL);
	VAR * v = new_var();
	VAR_name(v) = var_name;
	VAR_data_type(v) = dt;
	VAR_data_size(v) = mem_size;
	VAR_elem_type(v) = elem_type;
	VAR_align(v) = align;
	VAR_pointer_base_size(v) = pointer_base_size;
	VAR_flag(v) = flag;
	assign_var_id(v);
	return v;
}
示例#15
0
intercodes translate_array(struct Node *node,operand place,union varp *list,enum varType *listtype) {
	struct Node *child = node->child;
	assert(child != NULL);
	intercodes code1 = NULL;
	intercodes code2,code3,code4;
	int size = 0;
	operand t1;
	//ID[Exp]
	if (strcmp(child->sibling->type,"ID") == 0) {
		int id = getVarID(child->child);
		int flag = getFlag(child->child->text);
		if (flag == 1)
			t1 = new_var(id);
		else 
			t1 = new_reference(id);
		enum varType type;
		union varp vp = getVar(child->child,&type);
		if (vp.ap->basetype == varStruct) 
			size = get_structure_size(vp.ap->base,vp.ap->basetype);
		if (vp.ap->basetype == varInt || vp.ap->basetype == varFloat) 
			size = 4;
		if (list != NULL) {
			*list = vp.ap->base;
			*listtype = vp.ap->basetype;
		}
	}
	//Exp[Exp][Exp]
//	if (child->child->sibling != NULL && strcmp(child->child->sibling->type,"LB") == 0) {
//	}
	//handle Exp2
	operand t2 = new_tmp();
	code2 = translate_Exp(child->sibling->sibling,t2);
	code1 = link(code1,code2);
	//calculate offset
	operand c1 = new_constant(size);
	operand t3 = new_tmp();
	code3 = gen_binop(MUL_K,t3,t2,c1);
	//Add array address
	operand t4 = new_tmp_id(place->u.tmp_no);
	code4 = gen_binop(ADD_K,t4,t1,t3);
	code1 = link(code1,code4);
	place->kind = ADDRESS;
	return code1;
}
示例#16
0
int get_temporary_variables(char** instructions, int num_instructions, var*** temporary_variables) {
	int is_temp_var['z' - 'a' - 1];
	int i = 0;
	int g = 0;

	while (g < 26) {
		is_temp_var[g] = 0;
		g++;
	}

	for (i = 2; i < num_instructions; i++) {
		while (instructions[i][g] != 0) {
			if (instructions[i][g] >= 'a' && instructions[i][g] <= 'z') {
				is_temp_var[instructions[i][g] - 'a'] = 1;
			}
			g++;
		}
		g = 0;
	}

	int count = 0;
	for (i = 0; i < 26; i++) {
		if(is_temp_var[i] != 0) {
			count++;
		}
	}

	temporary_variables[0] = (var **) malloc(count * sizeof(var *));
	g = 0;
	for (i = 0; i < 26; i++) {
		if (is_temp_var[i] != 0) {
			temporary_variables[0][g] = new_var(i + 'a', 0);
			g++;
		}
	}
	return count;
}
示例#17
0
int main()
{
    int j;
    for(j=0;j<20;j++) new_var();
}
示例#18
0
//translate
intercodes translate_Exp(struct Node *node, operand place) {
	struct Node *child = node->child;
	assert(child != NULL);
	intercodes code1 = NULL;
	intercodes code2 = NULL;
	intercodes code3 = NULL;
	intercodes code4 = NULL;
	intercodes code5 = NULL;
	//INT
	if (strcmp(child->type,"INT") == 0) {
		operand c1 = new_constant(atoi(child->text));
		code1 = gen_assign(ASSIGN_K,place,c1);
		return code1;
	}
	//ID
	if (strcmp(child->type,"ID") == 0 && child->sibling == NULL) {
		int id = getVarID(child);
		operand v1 = new_var(id);
		code1 = gen_assign(ASSIGN_K,place,v1);
		return code1;
	}
	//Exp1 ASSINGOP Exp2
	if (strcmp(child->type,"Exp") == 0 && strcmp(child->sibling->type,"ASSIGNOP") == 0) {
		//Exp1 is ID (ID = Exp2)
//		printf("%s\n",child->child->type);
		if (strcmp(child->child->type,"ID") == 0) {
			operand t1 =  new_tmp();
			int id = getVarID(child->child);
			operand v1 = new_var(id);
			code1 = translate_Exp(child->sibling->sibling,t1);
			code2 = gen_assign(ASSIGN_K,v1,t1);
		//	code1 = link(code1,code2);
			if (place != NULL) {
				code3 = gen_assign(ASSIGN_K,place,v1);
				code1 = link(code1,code3);
			}
			return code1;
		}
		//Exp[Exp] = Exp2
	}
	//Exp1 PLUS/MINUS/STAR/DIV Exp2
	if (strcmp(child->type,"Exp") == 0 &&
		(strcmp(child->sibling->type,"PLUS") == 0 
		|| strcmp(child->sibling->type,"MINUS") == 0
		|| strcmp(child->sibling->type,"STAR") == 0
		|| strcmp(child->sibling->type,"DIV") == 0)) {
		operand t1 = new_tmp();
		operand t2 = new_tmp();
		code1 = translate_Exp(child,t1);
		code2 = translate_Exp(child->sibling->sibling,t2);
		//printf("%d %d\n", t1->u.tmp_no, t2 -> u.tmp_no);
		if (strcmp(child->sibling->type,"PLUS") == 0) {
			code3 = gen_binop(ADD_K,place,t1,t2);
		}
		else if (strcmp(child->sibling->type,"MINUS") == 0)
			code3 = gen_binop(SUB_K,place,t1,t2);
		else if (strcmp(child->sibling->type,"STAR") == 0)
			code3 = gen_binop(MUL_K,place,t1,t2);
		else if (strcmp(child->sibling->type,"DIV") == 0)
			code3 = gen_binop(DIV_K,place,t1,t2);
		//code1 = link(code1,code2);
		//code1 = link(code1,code3);
		return code1;
	}
	//MINUS Exp1
	if (strcmp(child->type,"MINUS") == 0)  {
		operand t1 = new_tmp();
		code1 = translate_Exp(child,t1);
		operand c1 = new_constant(0);
		code2 = gen_binop(SUB_K,place,c1,t1);
		code1 = link(code1,code2);
		return code1;
	}
	//Exp1 RELOP Exp2
	//NOT Exp1
	//Exp1 AND Exp2
	//Exp1 OR Exp2
	if (strcmp(child->type,"NOT") == 0 
	|| strcmp(child->sibling->type,"RELOP") == 0
	|| strcmp(child->sibling->type,"AND") == 0
	|| strcmp(child->sibling->type,"OR") == 0) {
		operand label1 = new_label();
		operand label2 = new_label();
		operand c1 = new_constant(0);
		operand c2 = new_constant(1);
		code1 = gen_assign(ASSIGN_K,place,c1);
		code2 = translate_Cond(node,label1,label2);
		code3 = gen_one(LABEL_K,label1);
		code4 = gen_assign(ASSIGN_K,place,c2);
		code5 = gen_one(LABEL_K,label2);
		code1 = link(code1,code2);
		code1 = link(code1,code3);
		code1 = link(code1,code4);
		code1 = link(code1,code5);
		return code1;
	}
	//ID LP RP
	if (strcmp(child->sibling->sibling->type,"LP") == 0 && strcmp(child->sibling->sibling->type,"RP") == 0) {
		char *func_name = child->text;
		if (strcmp(func_name,"read") == 0) {
			if (place == NULL) {
				operand t1 = new_tmp();
				code1 = gen_one(READ_K,t1);
			}
			else 
				code1 = gen_one(READ_K,place);
			return code1;
		}
		struct funMes *func_node = getFunMes(child);
		assert(func_node != NULL);
		operand f1 = new_function(func_name);
		if (place != NULL && place->kind != ADDRESS) 
			code1 = gen_assign(CALL_K,place,f1);
		else if (place != NULL && place->kind == ADDRESS) {
			operand t2 = new_tmp();
			code1 = gen_assign(CALL_K,t2,f1);
			code2 = gen_assign(ASSIGN_K,place,t2);
			code1 = link(code1,code2);
		}
		else {
			operand t2 = new_tmp();
			code1 = gen_assign(CALL_K,t2,f1);
		}
		return code1;
	}
	//ID LP Args RP
	if (strcmp(child->sibling->type,"LP") == 0 && strcmp(child->sibling->sibling->type,"Args") == 0) {
		char *func_name = child->text;
		operand *arg_list = (operand *)malloc(sizeof(operand) * 10);
		int arg_num = 0;
		code1 = translate_Args(child->sibling->sibling,arg_list,&arg_num);
		if (strcmp(func_name,"write") == 0) {
			assert(arg_num == 1);
			operand t1;
			if (arg_list[0]->kind == ADDRESS) {
				t1 = new_tmp();
				code2 = gen_assign(ASSIGN_K,t1,arg_list[0]);
				code1 = link(code1,code2);
			}
			else 
				t1 = arg_list[0];
			code3 = gen_one(WRITE_K,t1);
			code1 = link(code1,code3);
			return code1;
		}
		int i;
		for (i = 0;i < arg_num;i++) {
			code2 = gen_one(ARG_K,arg_list[i]);
			code1 = link(code1,code2);
		}
		operand f1 = new_function(func_name);
		if (place != NULL && place->kind != ADDRESS) 
			code3 = gen_assign(CALL_K,place,f1);
		else if (place != NULL && place->kind == ADDRESS) {
			operand t2 = new_tmp();
			code1 = gen_assign(CALL_K,t2,f1);
			code2 = gen_assign(ASSIGN_K,place,t2);
			code1 = link(code1,code2);
		}
		else {
			operand t2 = new_tmp();
			code3 = gen_assign(CALL_K,t2,f1);
		}
		code1 = link(code1,code3);
		return code1;
	}
	//LP Exp RP
	if (strcmp(child->type,"LP") == 0) {
		return translate_Exp(child->sibling,place);
	}
	//Exp1 LB Exp2 RB
	if (strcmp(child->type,"LB") == 0) {
		return translate_array(node,place,NULL,NULL);
	}
	//Exp DOT ID
	return NULL;

}
示例#19
0
文件: Aiger.hpp 项目: anithag/kleestr
 result_type operator() (logic::tag::nand_tag const&, result_type lhs, result_type rhs )
 {
   result_type t = new_var();
   aiger_add_and ( aig, t, lhs, rhs );
   return aiger_not ( t );
 }
示例#20
0
文件: Aiger.hpp 项目: anithag/kleestr
 result_type operator() (logic::tag::var_tag const& var, boost::any args)
 {
   return new_var(); 
 }
示例#21
0
文件: Aiger.hpp 项目: anithag/kleestr
 unsigned aiger_add_or ( aiger *aig, unsigned lhs, unsigned rhs )
 {
   result_type t = new_var ();
   aiger_add_and ( aig, t, aiger_not (lhs), aiger_not (rhs) ); 
   return aiger_not ( t );  
 }
示例#22
0
VariableSymbol* NodeBuilder::new_int_var(LString name, bool addr_taken)
{
  return new_var(name, get_int_type());
}
示例#23
0
 Expression new_var() {
     cur_var++ ;
     return new_var("x",cur_var) ;
 }