4-variable-simplifier/main.c

135 lines
2.6 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <assert.h>
#include <debug.h>
#include <cmdln.h>
#include <print.h>
#include <evaluate.h>
#include <get_cached_simplifications.h>
int main(int argc, char* const* argv)
{
struct cmdln_flags flags = parse_args(argc, argv);
struct simplifications simps = get_cached_simplifications(&flags);
if (flags.print_all_and_quit)
{
for (int i = 0; i < N; i++)
{
int cost = simps.data[i].cost;
if (cost != INT_MAX)
{
printf("0b%016b: [%2i]: ", i, cost);
print(&flags, &simps, i);
puts("");
}
}
}
else if (flags.print_max_operators_needed_and_quit)
{
int max_cost = 0;
for (int i = 0; i < N; i++)
{
int cost = simps.data[i].cost;
if (cost != INT_MAX && max_cost < cost)
{
max_cost = cost;
}
}
printf("I can simplify any tree down to %i operators or less.\n", max_cost);
}
else if (flags.command)
{
uint16_t truthtable = evaluate(flags.command);
// printf("truthtable = 0b%016b\n", truthtable);
if (simps.data[truthtable].kind == ek_unreachable)
{
puts("Unreachable");
}
else
{
printf("%2i: ", simps.data[truthtable].cost);
print(&flags, &simps, truthtable);
puts("");
}
}
else
{
if (!flags.quiet)
{
puts("Use C-style syntax for boolean operators and expressions.");
puts("Available variables: 'w', 'x', 'y' and 'z'.");
puts("Extended operators: nor is '!|', orn is '|!', nand is '!&', andn is '&!'.");
puts("Comparison operators are also suppported.");
}
for (char* line; (line = readline(">>> ")); free(line))
{
if (!*line) continue;
add_history(line);
uint16_t truthtable = evaluate(line);
// printf("truthtable = 0b%016b\n", truthtable);
if (simps.data[truthtable].kind == ek_unreachable)
{
puts("unreachable");
}
else
{
printf("%2i: ", simps.data[truthtable].cost);
print(&flags, &simps, truthtable);
puts("");
}
}
}
return 0;
}