Last active
December 21, 2017 10:39
-
-
Save yordis/ccc501482d35f07e2a11be4dc89cbe4d to your computer and use it in GitHub Desktop.
FBP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define port_data_size 64 | |
#define node_port_count 64 | |
#define node_parameter_count 64 | |
#define net_connection_count 64 | |
#define net_node_count 64 | |
typedef unsigned char bool; | |
typedef unsigned char byte; | |
typedef unsigned long cell; | |
enum { | |
false = 0, | |
true = !false | |
}; | |
typedef struct node node; | |
typedef struct net net; | |
struct node { | |
bool (*process)(net*, | |
cell, | |
cell, | |
cell, | |
cell); | |
cell ports [node_port_count]; | |
cell parameters[node_parameter_count]; | |
}; | |
struct net { | |
cell connections[net_connection_count][4]; | |
node nodes [net_node_count]; | |
}; | |
bool number(net *net, | |
cell self, | |
cell target, | |
cell source_port, | |
cell destination_port) { | |
node *node; | |
node = &net->nodes[self]; | |
node->ports[0] = node->parameters[0]; | |
return true; | |
} | |
bool adder(net *net, | |
cell self, | |
cell target, | |
cell source, | |
cell destination) { | |
net->nodes[self].ports[2] = net->nodes[self].ports[0] + net->nodes[self].ports[1]; | |
return true; | |
} | |
bool equals(net *net, | |
cell self, | |
cell target, | |
cell source, | |
cell destination) { | |
net->nodes[self].ports[2] = net->nodes[self].ports[0] == net->nodes[self].ports[1]; | |
return true; | |
} | |
bool printer(net *net, | |
cell self, | |
cell target, | |
cell source, | |
cell destination) { | |
printf("%lu\n", net->nodes[self].ports[0]); | |
return true; | |
} | |
bool run(net *net) { | |
cell iterator; | |
cell first; | |
cell second; | |
cell source; | |
cell destination; | |
if(net == NULL) { | |
return false; | |
} | |
for(iterator = 0; | |
iterator < net_connection_count; | |
iterator++) { | |
first = net->connections[iterator][0]; | |
second = net->connections[iterator][1]; | |
source = net->connections[iterator][2]; | |
destination = net->connections[iterator][3]; | |
if(net->nodes[first].process(net, first, second, source, destination) == false) { | |
return false; | |
} | |
net->nodes[second].ports[destination] = net->nodes[first].ports[source]; | |
if(net->nodes[second].process(net, second, first, source, destination) == false) { | |
return false; | |
} | |
} | |
return true; | |
} | |
bool add_connection(net *net, | |
cell slot, | |
cell sender, | |
cell receiver, | |
cell source_port, | |
cell destination_port) { | |
if(net == NULL) { | |
return false; | |
} | |
net->connections[slot][0] = sender; | |
net->connections[slot][1] = receiver; | |
net->connections[slot][2] = source_port; | |
net->connections[slot][3] = destination_port; | |
return true; | |
} | |
int main(void) { | |
net foo = {}; | |
add_connection(&foo, 0, 0, 2, 0, 0); | |
add_connection(&foo, 1, 1, 2, 0, 1); | |
add_connection(&foo, 2, 2, 3, 2, 0); | |
foo.nodes[0].process = number; | |
foo.nodes[1].process = number; | |
foo.nodes[2].process = adder; | |
foo.nodes[3].process = printer; | |
foo.nodes[0].parameters[0] = 1; | |
foo.nodes[1].parameters[0] = 2; | |
run(&foo); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment