mirror of
https://github.com/sigmasternchen/libargo
synced 2025-03-15 05:08:54 +00:00
restructure for code generation
This commit is contained in:
parent
ee136f5130
commit
46e6d00b57
5 changed files with 74 additions and 28 deletions
|
@ -5,7 +5,7 @@ CC = gcc
|
|||
|
||||
BIN_NAME = marshaller
|
||||
|
||||
marshaller: gen/lex.yy.c gen/y.tab.c
|
||||
marshaller: src/codegen.c gen/lex.yy.c gen/y.tab.c
|
||||
$(CC) -Isrc/ -o $@ $^
|
||||
|
||||
gen/y.tab.c gen/y.tab.h: src/parser.y
|
||||
|
|
42
marshaller/src/codegen.c
Normal file
42
marshaller/src/codegen.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "codegen.h"
|
||||
|
||||
struct declarsinfo* declarations;
|
||||
|
||||
extern int yyparse();
|
||||
|
||||
void generateCodeStruct(FILE* output, struct structinfo* info) {
|
||||
fprintf(output, "Struct %s", info->names[0]);
|
||||
if (info->names[1]) {
|
||||
fprintf(output, ", %s", info->names[1]);
|
||||
}
|
||||
fprintf(output, "\n");
|
||||
|
||||
for (size_t i = 0; i < info->memberno; i++) {
|
||||
fprintf(output, " Member %s (%s%s)\n",
|
||||
info->members[i]->name,
|
||||
info->members[i]->type->type,
|
||||
info->members[i]->type->isPointer ? " pointer" : ""
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void generateCode(FILE* output) {
|
||||
for (size_t i = 0; i < declarations->structno; i++) {
|
||||
generateCodeStruct(output, declarations->structs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int result = yyparse();
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
generateCode(stdout);
|
||||
|
||||
return 0;
|
||||
}
|
29
marshaller/src/codegen.h
Normal file
29
marshaller/src/codegen.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
#ifndef CODEGEN_H
|
||||
#define CODEGEN_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
struct typeinfo {
|
||||
char* type;
|
||||
bool isPointer;
|
||||
};
|
||||
|
||||
struct memberinfo {
|
||||
struct typeinfo* type;
|
||||
char* name;
|
||||
};
|
||||
|
||||
struct structinfo {
|
||||
char* names[2];
|
||||
size_t memberno;
|
||||
struct memberinfo** members;
|
||||
};
|
||||
|
||||
struct declarsinfo {
|
||||
size_t structno;
|
||||
struct structinfo** structs;
|
||||
};
|
||||
|
||||
extern struct declarsinfo* declarations;
|
||||
|
||||
#endif
|
|
@ -1,27 +1,6 @@
|
|||
#ifndef HELPER_H
|
||||
#define HELPER_H
|
||||
|
||||
struct typeinfo {
|
||||
char* type;
|
||||
bool isPointer;
|
||||
};
|
||||
|
||||
struct memberinfo {
|
||||
struct typeinfo* type;
|
||||
char* name;
|
||||
};
|
||||
|
||||
struct structinfo {
|
||||
char* names[2];
|
||||
size_t memberno;
|
||||
struct memberinfo** members;
|
||||
};
|
||||
|
||||
struct declarsinfo {
|
||||
size_t structno;
|
||||
struct structinfo** structs;
|
||||
};
|
||||
|
||||
void _panic(const char* f, const char* s);
|
||||
#define panic(s) _panic(__func__, s)
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <errno.h>
|
||||
|
||||
#include <helper.h>
|
||||
#include <codegen.h>
|
||||
|
||||
int yylex();
|
||||
void yyerror(char*);
|
||||
|
@ -54,7 +55,7 @@ char* prefix(char* string, const char* prefix);
|
|||
|
||||
file: declars
|
||||
{
|
||||
|
||||
declarations = $1;
|
||||
}
|
||||
;
|
||||
|
||||
|
@ -134,11 +135,6 @@ type: LONG { $$ = newTypeInfo(false, "long"); }
|
|||
|
||||
%%
|
||||
|
||||
int main(void) {
|
||||
yyparse();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void _panic(const char* f, const char* s) {
|
||||
fprintf(stderr, "panic: %s: %s: %s\n", f, s, strerror(errno));
|
||||
exit(3);
|
||||
|
|
Loading…
Reference in a new issue