Exemple #1
0
object *expand_clauses(object *clauses) {
    object *first;
    object *rest;
    
    if (is_the_empty_list(clauses)) {
        return false;
    }
    else {
        first = car(clauses);
        rest = cdr(clauses);
        if (is_cond_else_clause(first)) {
            if (is_the_empty_list(rest)) {
                return sequence_to_exp(cond_actions(first));
            }
            else {
                fprintf(stderr, "else clause isn't last cond->if");
                exit(1);
            }
        }
        else {
            return make_if(cond_predicate(first),
                           sequence_to_exp(cond_actions(first)),
                           expand_clauses(rest));
        }
    }
}
Exemple #2
0
static pSlipObject expand_clauses(pSlip gd, pSlipObject clauses)
{
	pSlipObject first;
	pSlipObject rest;

	if (sIsObject_EmptyList(gd, clauses) == S_TRUE)
	{
		return gd->singleton_False;
	}
	else
	{
		first = car(clauses);
		rest  = cdr(clauses);

		if (is_cond_else_clause(gd, first) == S_TRUE)
		{
			if (sIsObject_EmptyList(gd, rest) == S_TRUE)
			{
				return sequence_to_exp(gd, cond_actions(first));
			}
			else
			{
				throw_error(gd, "else clause isn't last cond->if");
				return gd->singleton_False;
			}
		}
		else
		{
			return make_if(gd, cond_predicate(first), sequence_to_exp(gd, cond_actions(first)), expand_clauses(gd, rest));
		}
	}
}
Exemple #3
0
static ErlDrvData tun_start(ErlDrvPort port, char *args)
{
    struct tun_state *state;
    int fd;

    int mode;
    char dev_name[IFNAMSIZ];

    state = (struct tun_state*) sys_alloc(sizeof(struct tun_state));
    if (state == NULL) {
        errno = ENOMEM;         /* appropriate to set errno? */
        return ERL_DRV_ERROR_ERRNO;
    }

    if (!parse_args(args, &mode, state->dev, IFNAMSIZ - 1)) {
        return ERL_DRV_ERROR_BADARG;
    }

    fd = make_if(mode, state->dev);
    if (fd < 0) {
        return ERL_DRV_ERROR_GENERAL;
    }
    state->port = port;
    state->fd = fd;
    state->active = ACTIVE_FALSE;
    set_input(state, 0);

    return (ErlDrvData)state;
}
Exemple #4
0
/* Build necessary code for an if statement */
void build_if_stmt(environment *env, NODE *node, int if_count, tac_quad *false_jump, tac_quad *loop_jump, int flag, int return_type) {
	char *s_tmp;
	value *val1, *val2, *temporary;
	if (node==NULL || (type_of(node)!=IF && type_of(node)!=WHILE)) return;
	/* LHS is condition */
	val1 = make_simple(env, node->left, flag, return_type);
	
	/* Generate if statement */
	s_tmp = malloc(sizeof(char) * 25);
	sprintf(s_tmp, "__if%dtrue", if_count);
	append_code(make_if(val1, s_tmp));

	/* Output false branch (i.e. else part) */	
	if (type_of(node->right)==ELSE) {
		/* Build code for false part */
		build_else_part(env, node->right, 0, flag, return_type);
	}
	
	/* Generate goto end of if statement */
	if (false_jump != NULL) {
		append_code(false_jump);
	}
	else {
		s_tmp = malloc(sizeof(char) * 25);
		sprintf(s_tmp, "__if%dend", if_count);
		append_code(make_goto(s_tmp));
	}
	
	/* Generate label for start of true branch */
	s_tmp = malloc(sizeof(char) * 25);
	sprintf(s_tmp, "__if%dtrue", if_count);
	append_code(make_label(s_tmp));
	
	/* Output true branch */
	if (type_of(node->right)==ELSE) {
		/* Build code for true part */
		build_else_part(env, node->right, 1, flag, return_type);
	}
	else {
		/* True part is whole right branch */
		make_simple(env, node->right, flag, return_type);
	}
	
	/* Check if extra loop jump has been specified (for WHILE loops etc) */
	if (loop_jump) {
		append_code(loop_jump);
	}
	
	/* Generate end of IF stmt label */
	s_tmp = malloc(sizeof(char) * 25);
	sprintf(s_tmp, "__if%dend", if_count);
	append_code(make_label(s_tmp));
}
Exemple #5
0
//one arg: clauses
static cellpoint expand_clauses(void)
{
	if (is_true(is_null(args_ref(1)))){
		reg = a_false;
	}else {
		args_push(car(args_ref(1)));
		reg = is_cond_else_clause();
		if (is_true(reg)){
			reg = cdr(args_ref(1));
			if(is_true(is_null(reg))){
				//calls cond_actions
				args_push(car(args_ref(1)));
				reg = cond_actions();
				//calls sequence_2_exp
				args_push(reg);
				reg = sequence_2_exp();
			}else {
				printf("Error: ELSE clause isn't last clause in cond expression.\n");
				error_handler();
			}
		}else {
			//calls cond_predicate
			args_push(car(args_ref(1)));
			reg = cond_predicate();
			stack_push(&vars_stack, reg);
			//calls sequence_2_exp
			args_push(car(args_ref(1)));
			reg = cond_actions();
			args_push(reg);
			reg = sequence_2_exp();
			stack_push(&vars_stack, reg);
			//calls expand_clauses to expand the rest clauses
			args_push(cdr(args_ref(1)));
			reg = expand_clauses();
			//calls make_if
			args_push(reg);
			args_push(stack_pop(&vars_stack));
			args_push(stack_pop(&vars_stack));
			reg = make_if();
		}
	}
	args_pop(1);
	return reg;
}
Exemple #6
0
object *expand_clauses(object *clauses) {
    object *first;
    object *rest;

    if (is_empty(clauses))
        return false;

    first = car(clauses);
    rest  = cdr(clauses);

    if (!is_cond_else_clause(first))
        return make_if(cond_predicate(first), sequence_to_exp(cond_actions(first)), expand_clauses(rest));

    if (is_empty(rest))
        return sequence_to_exp(cond_actions(first));

    fprintf(stderr, "else clause isn't last cond->if");
    exit(EXIT_FAILURE);
}
Exemple #7
0
static data_t *expand_clauses(const data_t *clauses) {
	data_t *first, *rest;

	if(clauses == NULL)
		return make_symbol("#f");

	first = car(clauses);
	rest = cdr(clauses);

	if(is_cond_else_clause(first)) {
		if(rest == NULL) {
			return sequence_to_exp(get_cond_actions(first));
		} else {
			printf("ELSE clause isn't last -- COND-IF");
			return make_symbol("error");
		}
	} 
	return make_if(get_cond_predicate(first), sequence_to_exp(get_cond_actions(first)), expand_clauses(rest));
}
Exemple #8
0
static object *expand_clauses(object *clauses)
{
    if (is_empty_list(clauses)) {
        return get_boolean(0);
    } else {
        object *first = car(clauses);
        object *rest = cdr(clauses);
        if (cond_predicate(first) == lookup_symbol("else")) {
            if (is_empty_list(rest)) {
                return sequence_to_exp(cond_actions(first));
            } else {
                error("else clause must be last in cond expression");
            }
        } else {
            return make_if(cond_predicate(first),
                    sequence_to_exp(cond_actions(first)),
                    expand_clauses(rest));
        }
    }
}
Exemple #9
0
sb_element_t *if_parse_e_f ( sb_token_t *tlist[], unsigned char i, sb_parse_match_rule_t *rule ) {
  sb_element_t *condition, *truepath = NULL, *falsepath = NULL;
  sb_element_t *e;
  sbparse_return_t r;

  condition = tlist [ i + 1 ] -> data.e;

  // need to get the contained (TRUE PATH) listing..
  //
  if ( g_sb_debug ) {
    printf ( "DESCEND\n" );
  }
  g_sb_indent += 1;
  r = sbparse_buffer();
  truepath = r.e;
  g_sb_indent -= 1;

  if ( ! truepath ) {
    return ( NULL ); // failed to parse contained code, so just abort
  }

  // and check for a false path..
  //
  if ( r.tokentype == sbt_else ) {
    if ( g_sb_debug ) {
      printf ( "DESCEND-ELSE\n" );
    }
    g_sb_indent += 1;
    r = sbparse_buffer();
    falsepath = r.e;
    g_sb_indent -= 1;
  }

  // build the if
  //
  e = make_if ( condition, truepath, falsepath );

  return ( e );
}
Exemple #10
0
static EXPRESSION *simplify_expression(MODULE *module, FUNCTION *func, BLOCK *block, EXPRESSION *expr, STATEMENT *before)
{
    int i;
    
    int source_line = CAST_TO_AST(expr)->source_line;
    
    if (!has_graph(func) && is_short_circuit(expr))
    {
        TYPE *new_temp_type = CAST_TO_EXPRESSION(tree_get_child(expr, 0))->type;
        EXPRESSION *new_temp = make_new_temp(module, func, new_temp_type, source_line);
        STATEMENT *new_assign = make_assignment(new_temp, tree_get_child(expr, 0), source_line);
        tree_add_before(CAST_TO_NODE(block), CAST_TO_NODE(new_assign), CAST_TO_NODE(before));
        STATEMENT *new_assign2 = make_assignment(new_temp, tree_get_child(expr, 1), source_line);
        EXPRESSION *new_cond = new_temp;
        if (tree_is_type(expr, EXPR_OR))
            new_cond = make_unary_expression(EXPR_NOT, new_cond, source_line);
        STATEMENT *new_if = make_if(new_cond, make_block(NULL, new_assign2, 0), NULL, 0);
        tree_add_before(CAST_TO_NODE(block), CAST_TO_NODE(new_if), CAST_TO_NODE(before));
        return new_temp;
    }
    
    if (has_graph(func) && is_short_circuit(expr))
    {
        GRAPH *graph = func->graph;
        
        EXPRESSION *sub0 = tree_get_child(expr, 0);
        EXPRESSION *sub1 = tree_get_child(expr, 1);
        
        STATEMENT *new_test = make_test(sub0, source_line);
        
        EDGE_TYPE inner_type = tree_is_type(expr, EXPR_OR) ? EDGE_NO : EDGE_YES;
        EDGE_TYPE outer_type = tree_is_type(expr, EXPR_OR) ? EDGE_YES : EDGE_NO;
        
        add_vertex(graph, CAST_TO_NODE(new_test));
        HASH *subhash = get_from_hash(graph->forward, before, sizeof(void *));
        HASH_ITERATOR iter;
        for (hash_iterator(subhash, &iter); hash_iterator_valid(&iter); hash_iterator_next(&iter))
        {
            EDGE_TYPE type = (EDGE_TYPE) iter.entry->data;
            if (outer_type & type)
                add_edge(graph, CAST_TO_NODE(new_test), iter.entry->key, type);
            if (inner_type & type)
                inner_type = type;
        }
        inject_before(graph, CAST_TO_NODE(new_test), CAST_TO_NODE(before), inner_type);        
        
        return sub1;
    }
    
    if (is_simple(expr))
        return expr;
    
    if (tree_is_type(expr, EXPR_CALL))
    {
        EXPRESSION *args = CAST_TO_EXPRESSION(tree_get_child(expr, 1));
        args = atomise_expression(module, func, block, args, before);
        tree_get_child(expr, 1) = args;
        return expr;
    }
    
    for (i = 0; i < tree_num_children(expr); i++)
    {
        EXPRESSION *child = tree_get_child(expr, i);
        if (!is_atomic(child))
        {
            TYPE *new_temp_type = CAST_TO_EXPRESSION(child)->type;
            EXPRESSION *new_temp = make_new_temp(module, func, new_temp_type, CAST_TO_AST(child)->source_line);
            STATEMENT *new_assign = make_assignment(new_temp, child, CAST_TO_AST(child)->source_line);
            
            if (has_graph(func))
            {
                GRAPH *graph = func->graph;
                add_vertex(graph, CAST_TO_NODE(new_assign));
                inject_before(graph, CAST_TO_NODE(new_assign), CAST_TO_NODE(before), 0);
            }
            else
                tree_add_before(CAST_TO_NODE(block), CAST_TO_NODE(new_assign), CAST_TO_NODE(before));
            
            tree_get_child(expr, i) = new_temp;
        }
    }
    
    return expr;
}
Exemple #11
0
int Parser::expression(int pos, int status) {
	int isputs = 0;

	if(tok.skip("$")) { // global varibale

		if(is_asgmt()) asgmt();

	} else if(tok.skip("require")) {
	
		make_require();
	
	} else if(tok.skip("def")) { blocksCount++;

		make_func();

	} else if(tok.skip("module")) { blocksCount++;
		module = tok.tok[tok.pos++].val;
		eval(0, NON);
		module = "";
	} else if(funcs.inside == false && !tok.is("def", 1) &&
			!tok.is("module", 1) && !tok.is("$", 1) &&
			!tok.is(";", 1) && module == "") {	// main func entry

		funcs.inside = true;
		funcs.now++;
		funcs.append("main", ntv.count, 0); // append funcs
		ntv.genas("push ebp");
		ntv.genas("mov ebp esp");
		uint32_t espBgn = ntv.count + 2; ntv.genas("sub esp 0");
		ntv.gencode(0x8b); ntv.gencode(0x75); ntv.gencode(0x0c); // mov esi, 0xc(%ebp)
		
		eval(0, BLOCK_NORMAL);

		ntv.gencode(0x81); ntv.gencode(0xc4); ntv.gencode_int32(ADDR_SIZE * (var.focus().size() + 6)); // add %esp nn
		ntv.gencode(0xc9);// leave
		ntv.gencode(0xc3);// ret
		ntv.gencode_int32_insert(ADDR_SIZE * (var.focus().size() + 6), espBgn);
		funcs.inside = false;

	} else if(is_asgmt()) {

		asgmt();

	} else if((isputs=tok.skip("puts")) || tok.skip("print")) {

		do {
			ExprType et = expr_entry();
			ntv.genas("push eax");
			if(et.is_type(T_STRING)) {
				ntv.gencode(0xff); ntv.gencode(0x56); ntv.gencode(4);// call *0x04(esi) putString
			} else {
				ntv.gencode(0xff); ntv.gencode(0x16); // call (esi) putNumber
			}
			ntv.genas("add esp 4");
		} while(tok.skip(","));
		// for new line
		if(isputs) {
			ntv.gencode(0xff); ntv.gencode(0x56); ntv.gencode(8);// call *0x08(esi) putLN
		}

	} else if(tok.skip("for")) { blocksCount++;

		asgmt();
		if(!tok.skip(",")) error("error: %d: expected ','", tok.tok[tok.pos].nline);
		make_while();

	} else if(tok.skip("while")) { blocksCount++;

		make_while();

	} else if(tok.skip("return")) {

		make_return();

	} else if(tok.skip("if")) { blocksCount++;

		make_if();
	
	} else if(tok.skip("else")) {

		uint32_t end;
		ntv.gencode(0xe9); end = ntv.count; ntv.gencode_int32(0);// jmp while end
		ntv.gencode_int32_insert(ntv.count - pos - 4, pos);
		eval(end, BLOCK_NORMAL);
		return 1;

	} else if(tok.skip("elsif")) {

		uint32_t endif, end;
		ntv.gencode(0xe9); endif = ntv.count; ntv.gencode_int32(0);// jmp while end
		ntv.gencode_int32_insert(ntv.count - pos - 4, pos);
		expr_entry(); // if condition
		ntv.gencode(0x83); ntv.gencode(0xf8); ntv.gencode(0x00);// cmp eax, 0
		ntv.gencode(0x75); ntv.gencode(0x05); // jne 5
		tok.skip(";");
		ntv.gencode(0xe9); end = ntv.count; ntv.gencode_int32(0);// jmp while end
		eval(end, BLOCK_NORMAL);
		ntv.gencode_int32_insert(ntv.count - endif - 4, endif);
		return 1;

	} else if(tok.skip("break")) {

		make_break();

	} else if(tok.skip("end")) { blocksCount--;

		if(status == NON) return 1;
		if(status == BLOCK_NORMAL) {
			ntv.gencode_int32_insert(ntv.count - pos - 4, pos);
		} else if(status == BLOCK_FUNC) funcs.inside = false;
		return 1;

	} else if(!tok.skip(";")) {
		expr_entry();
	}
	
	return 0;
}