#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "Mnemonic.h"
#include "Parser.h"
#include "Code.h"
#define FILENAME_LEN 16
#define LINE_SIZE 128
#define TYPE_LEN 15
#define INSTRUCTION_LEN 17
int main(int argc, char *argv[]){
/* ファイルのオープン */
FILE *fp, *fp_out; // 入出力ファイル用ポインタ
char filename[FILENAME_LEN]; // 出力ファイル名用
char chr[LINE_SIZE]; // アセンブリプログラム1行分
char adv_chr[LINE_SIZE]; // advanceに通した後のchr
char out_chr[INSTRUCTION_LEN]; //出力用
int first = 1;
fp = fopen(argv[1], "r");
if(fp == NULL){
printf("%s file not open\n", argv[1]);
}
strcpy(filename, argv[1]);
char *ext = strrchr(filename, '.'); // 拡張子が始まるポインタ
if (ext != NULL) {
strcpy(ext, "_t.hack"); // 拡張子置き換え(上書きコピー)
}
fp_out = fopen(filename, "w");
while (fgets(chr, LINE_SIZE, fp) != NULL) {
/*
Parser
*/
// printf("----------------------------------\n");
// printf("chr: %s\n", chr);
/* advance */
advance(chr, adv_chr);
/* instructionType */
InstructionType type;
type = instructionType(adv_chr); // Typeを見つける
CCommand code = {"", "", ""};
switch (type) {
case C_INSTRUCTION:
/* dest, comp, jump */
code = parse(adv_chr); // Parser.comp, Parser.dest, Parser.jump を実行
// printf("code:(dest) %s, (comp) %s, (jump) %s \n", code.dest, code.comp, code.jump);
strcpy(out_chr, "111");
strcpy(out_chr+3, compCode(code.comp)); // Code.comp
strcpy(out_chr+10, destCode(code.dest)); // Code.dest
strcpy(out_chr+13, jumpCode(code.jump)); // Code.jump
break;
case L_INSTRUCTION:
/* symbol */
symbol(adv_chr);
out_chr[0] = '0';
decimalToBinary15(adv_chr, out_chr+1);
break;
case A_INSTRUCTION:
/* symbol */
symbol(adv_chr);
out_chr[0] = '0';
decimalToBinary15(adv_chr, out_chr+1);
break;
};
// printf("out_chr: %s\n",out_chr);
// /*
// Code
// */
// // データを書き込む
if (!first) {
fprintf(fp_out, "\n");
}
first = 0;
fprintf(fp_out, "%s", out_chr);
}
fclose(fp);
fclose(fp_out);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: