diff --git a/cc.c b/cc.c index 156148b..ba5a4ca 100644 --- a/cc.c +++ b/cc.c @@ -82,6 +82,12 @@ int main(int argc, char** argv) } } + /* Deal with special case of wanting to read from standard input */ + if(stdin == in) + { + global_token = read_all_tokens(in, global_token); + } + if(NULL == global_token) { file_print("Either no input files were given or they were empty\n", stderr); diff --git a/cc_types.c b/cc_types.c index f0b909d..adf3a21 100644 --- a/cc_types.c +++ b/cc_types.c @@ -82,6 +82,40 @@ struct type* lookup_type(char* s) struct type* type_name(); void require_match(char* message, char* required); + +int member_size; +struct type* build_member(struct type* last, int offset) +{ + struct type* member_type = type_name(); + struct type* i = calloc(1, sizeof(struct type)); + i->name = global_token->s; + i->members = last; + i->size = member_type->size; + member_size = member_type->size; + i->type = member_type; + i->offset = offset; + return i; +} + +struct type* build_union(struct type* last, int offset) +{ + int size = 0; + global_token = global_token->next; + require_match("ERROR in build_union\nMissing {\n", "{"); + while('}' != global_token->s[0]) + { + last = build_member(last, offset); + if(member_size > size) + { + size = member_size; + } + global_token = global_token->next; + require_match("ERROR in build_union\nMissing ;\n", ";"); + } + member_size = size; + return last; +} + void create_struct() { int offset = 0; @@ -99,17 +133,17 @@ void create_struct() struct type* last = NULL; while('}' != global_token->s[0]) { - struct type* member_type = type_name(); - i = calloc(1, sizeof(struct type)); - i->name = global_token->s; - i->members = last; - i->size = member_type->size; - i->type = member_type; - i->offset = offset; - offset = offset + member_type->size; + if(match(global_token->s, "union")) + { + last = build_union(last, offset); + } + else + { + last = build_member(last, offset); + } + offset = offset + member_size; global_token = global_token->next; require_match("ERROR in create_struct\x0A Missing ;\x0A", ";"); - last = i; } global_token = global_token->next; diff --git a/test/test.answers b/test/test.answers index 8834c8c..fe91b93 100644 --- a/test/test.answers +++ b/test/test.answers @@ -9,7 +9,7 @@ d27eb315d694324650b11a421d6990eee60ac5921a5625bbccb43d806f09e156 test/results/t 8cc38294fb1261843cfc3956fad5a451c95bbc2ed687435d4e2d59df2c4a8567 test/results/test08-binary cc8f252877a85c0d7832094ff574d7173ac33940917bc1591358b8970651a81c test/results/test09-binary 3857aee4183de41bd00b014d616a5d73f4bfc57aa60a6073bb4113d6ff2fb8d5 test/results/test10-binary -ab2341e89491192eb5b6dbb432661025b762ee1f0b419b8f98deda65c2b3f289 test/results/test100-binary +a5764dc3aa8b9021beb2388d42357653315a21b0111a5c15f9e82301d88b5a79 test/results/test100-binary dce2f0b35323cf6a2b01f74a9335100f2d8626028af545832dbdb503573db0e5 test/results/test11-binary 88602970fa07b5da7a42b4f2b2486fe03accc6796e05453c4ab934e986790bef test/results/test12-binary c85a57b5b1d65288efd47a3b12c6fca1efade9e7ec91e65efda5531d2c40d293 test/results/test13-binary diff --git a/test/test100/proof.answer b/test/test100/proof.answer index 538d6ef..d303d49 100644 --- a/test/test100/proof.answer +++ b/test/test100/proof.answer @@ -1 +1 @@ -8945ef74cc5a843cf004bca54d10d14da8a5369babbce9f2bc584ab98a59e84c test/test100/proof +aacd70427396379f16862809eacd1a881b74e0ceafd6b99304f74e2893e48b98 test/test100/proof