/* PUBLIC */ Literals formula_to_literal(Formula f) { if (f->type == NOT_FORM && f->kids[0]->type == ATOM_FORM) return new_literal(FALSE, copy_term(f->kids[0]->atom)); else if (f->type == ATOM_FORM) return new_literal(TRUE, copy_term(f->atom)); else return NULL; } /* formula_to_literal */
int main() { Literal *lit = new_literal(2, LITERAL_INT); printf("tipo: %d", lit->kind); }
m1_expression * character(M1_compiler *comp, char ch) { m1_expression *expr = expression(comp, EXPR_CHAR); expr->expr.l = new_literal(VAL_INT); expr->expr.l->value.ival = (int)ch; expr->expr.l->sym = sym_enter_int(comp, &comp->currentchunk->constants, (int)ch); return expr; }
m1_expression * integer(M1_compiler *comp, int value) { m1_expression *expr = expression(comp, EXPR_INT); /* make a new literal node. */ expr->expr.l = new_literal(VAL_INT); expr->expr.l->value.ival = value; /* store the constant in the constants segment. */ expr->expr.l->sym = sym_enter_int(comp, &comp->currentchunk->constants, value); return expr; }
m1_expression * number(M1_compiler *comp, double value) { m1_expression *expr = expression(comp, EXPR_NUMBER); /* make a new literal node */ expr->expr.l = new_literal(VAL_FLOAT); expr->expr.l->value.fval = value; /* store the constant in the constants segment. */ expr->expr.l->sym = sym_enter_num(comp, &comp->currentchunk->constants, value); return expr; }
m1_expression * string(M1_compiler *comp, char *str) { m1_expression *expr = expression(comp, EXPR_STRING); assert(str != NULL); expr->expr.l = new_literal(VAL_STRING); expr->expr.l->value.sval = str; assert(comp != NULL); assert(comp->currentchunk != NULL); assert(&comp->currentchunk->constants != NULL); /* store the string in the constants segment. */ expr->expr.l->sym = sym_enter_str(comp, &comp->currentchunk->constants, str); return expr; }
/* PUBLIC */ Literal apply_lit(Literal lit, Context c) { return new_literal(lit->sign, apply(lit->atom, c)); } /* apply_lit */
//constructs a SatState from an input cnf file SatState* sat_state_new(const char* file_name) { SatState* state = malloc(sizeof(SatState)); c2dLiteral tmp_num; FILE *file = fopen(file_name, "r"); char *line = (char*)malloc(BUF_LEN * sizeof(char)); char *line_start_p = line; Lit **buf_literals; c2dSize cur_clause_index = 0; while (fgets(line, BUF_LEN, file)) { if (strlen(line) < 2) continue; if (line[0] == 'c' || line[0] == '%' || line[0] == '0') continue; if (line[0] == 'p') { line = skip_a_string(skip_a_string(line)); line = read_an_interger(line, &tmp_num); state->num_vars = (c2dSize)tmp_num; line = read_an_interger(line, &tmp_num); state->num_cnf_clauses = (c2dSize)tmp_num; state->variables = malloc(sizeof(Var*) * (state->num_vars+1)); state->p_literals = malloc(sizeof(Lit*) * (state->num_vars+1)); state->n_literals = malloc(sizeof(Lit*) * (state->num_vars+1)); for (c2dSize i = 1; i <= state->num_vars; i++) { state->variables[i] = new_variable(i); state->variables[i]->p_literal = state->p_literals[i] = new_literal((c2dLiteral)i, state->variables[i]); state->variables[i]->n_literal = state->n_literals[i] = new_literal(-((c2dLiteral)i), state->variables[i]); state->p_literals[i]->op_lit = state->n_literals[i]; state->n_literals[i]->op_lit = state->p_literals[i]; } state->cnf_clauses = malloc(sizeof(Clause*) * (state->num_cnf_clauses + 1)); buf_literals = malloc(sizeof(Lit*) * 2 * state->num_vars); } else { c2dSize clause_size = 0; while ((line = read_an_interger(line, &tmp_num))) { if (tmp_num == 0) break; if (tmp_num > 0) { buf_literals[clause_size++] = state->p_literals[tmp_num]; } else { buf_literals[clause_size++] = state->n_literals[-tmp_num]; } } if (clause_size > 0) { ++cur_clause_index; state->cnf_clauses[cur_clause_index] = new_clause(cur_clause_index, clause_size, buf_literals); push_clause_to_vars(state->cnf_clauses[cur_clause_index]); if (cur_clause_index == state->num_cnf_clauses) break; } } line = line_start_p; } fclose(file); free(line_start_p); free(buf_literals); for (c2dSize i = 1; i <= state->num_vars; i++) { state->variables[i]->num_cnf_clauses = state->variables[i]->num_clauses; } state->cur_level = 1; state->dyn_cap = 2; state->num_learned_clauses = 0; state->learned_clauses = malloc(sizeof(Clause*) * state->dyn_cap); state->num_decided_literals = 0; state->decided_literals = malloc(state->num_vars * 2 * sizeof(Lit*)); state->num_implied_literals = 0; state->implied_literals = malloc(state->num_vars * 2 * sizeof(Lit*)); state->unit_resolution_s = UNIT_RESOLUTION_FIRST_TIME; state->tmp_lit_list = malloc(sizeof(Lit*) * state->num_vars * 2); state->seen = malloc(sizeof(BOOLEAN) * (state->num_vars+1)); state->lit_list = malloc(sizeof(Lit*) * state->num_vars * 2); return state; }