1 /* Hello world program compiler */
10 static int parse(FILE *, FILE *);
11 static void write_hw_header(FILE *, int);
12 static HwInstruction
*get_instr(int);
13 static void compiler_error(const char *, ...);
16 main(int argc
, char *argv
[])
20 char *file_name
= NULL
;
22 compiler_error("No input files!\n");
25 in
= fopen(file_name
, "rt");
27 compiler_error("Cannot open file %s\n", file_name
);
29 file_name
[strlen(file_name
)-1] = 'x';
30 out
= fopen(file_name
, "wb");
32 compiler_error("Cannot open file %s\n", file_name
);
34 fseek(out
, sizeof(HwHeader
), SEEK_SET
);
35 in_count
= parse(in
, out
);
36 write_hw_header(out
, in_count
);
44 parse(FILE *in
, FILE *out
)
49 HwInstruction
*inst
= NULL
;
52 fscanf(in
, "%c", (char *)&input
);
70 inst
= get_instr(input
);
74 fwrite((void *)&inst
->i_opcode
, sizeof(uint8_t), 1, out
);
81 write_hw_header(FILE *out
, int count
)
84 .hw_magic
= HW_HEADER_MAGIC
,
85 .hw_version
= HW_HEADER_VERSION
,
86 .hw_instr_count
= count
89 fseek(out
, 0L, SEEK_SET
);
90 fwrite(&head
, sizeof(HwHeader
), 1, out
);
93 static HwInstruction
*
97 HwInstruction
*hi
= NULL
;
98 for (i
= 0; i
< LAST_INSTRUCTION
; i
++) {
99 if (instructions
[i
].i_char
== inst
) {
100 hi
= &instructions
[i
];
108 compiler_error(const char *fmt
, ...)
112 fprintf(stderr
, "HW Compiler - (C) Akos Kovacs\n\nError: ");
113 vfprintf(stderr
, fmt
, ap
);