2018-09-09 12:15:27 -05:00
|
|
|
//
|
|
|
|
// main.c
|
|
|
|
// cinc
|
|
|
|
//
|
|
|
|
// Created by Peter Terpstra on 9/7/18.
|
|
|
|
// Copyright © 2018 Peter Terpstra. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
2018-10-31 08:32:41 -05:00
|
|
|
#include <unistd.h>
|
2018-09-09 12:15:27 -05:00
|
|
|
#include "tokenize.h"
|
2018-09-09 17:31:02 -05:00
|
|
|
#include "ast.h"
|
|
|
|
#include "parser.h"
|
2018-09-09 18:08:00 -05:00
|
|
|
#include "generate.h"
|
2018-09-09 12:15:27 -05:00
|
|
|
|
2018-09-16 16:44:08 -05:00
|
|
|
#define COMPILER_DEBUG 1
|
2018-09-09 20:12:20 -05:00
|
|
|
|
2018-09-09 12:15:27 -05:00
|
|
|
int main(int argc, const char * argv[]) {
|
2018-09-16 16:44:08 -05:00
|
|
|
char* prgstr="int main(){int j=0;for(int i=0;i<7;i++){if(i==5){break;}j+=2;}return j;}";
|
|
|
|
#if COMPILER_DEBUG
|
2018-09-09 20:12:20 -05:00
|
|
|
printf("Program:\n");
|
|
|
|
printf("%s",prgstr);
|
2018-09-16 16:44:08 -05:00
|
|
|
#endif
|
2018-09-09 12:15:27 -05:00
|
|
|
Token* tokens=tokenize(prgstr);
|
2018-09-16 16:44:08 -05:00
|
|
|
#if COMPILER_DEBUG
|
2018-09-09 20:12:20 -05:00
|
|
|
printf("Tokens:\n");
|
|
|
|
Token* tok=tokens;
|
|
|
|
while (tok) {
|
|
|
|
print_tok(tok);
|
|
|
|
tok=tok->next;
|
|
|
|
}
|
2018-09-16 16:44:08 -05:00
|
|
|
#endif
|
2018-09-09 17:31:02 -05:00
|
|
|
AstNode* ast=parse(tokens);
|
2018-09-16 16:44:08 -05:00
|
|
|
#if COMPILER_DEBUG
|
2018-09-09 20:12:20 -05:00
|
|
|
printf("AST:\n");
|
|
|
|
print_tree(ast, 0);
|
2018-09-16 16:44:08 -05:00
|
|
|
#endif
|
2018-09-09 18:08:00 -05:00
|
|
|
char* prg=generate_prg(ast);
|
2018-09-16 16:44:08 -05:00
|
|
|
#if COMPILER_DEBUG
|
2018-09-09 20:12:20 -05:00
|
|
|
printf("Output assembly:\n");
|
|
|
|
printf("%s",prg);
|
2018-09-16 16:44:08 -05:00
|
|
|
#endif
|
|
|
|
FILE* outfile=fopen("/Users/peterterpstra/Desktop/projects/xcode/cinc/cinc/out.s","w");
|
2018-09-09 18:08:00 -05:00
|
|
|
fputs(prg, outfile);
|
|
|
|
fclose(outfile);
|
|
|
|
free(prg);
|
2018-09-16 16:44:08 -05:00
|
|
|
free_tree(ast);
|
|
|
|
free_toklist(tokens);
|
2018-10-31 08:32:41 -05:00
|
|
|
execlp("gcc","gcc","-masm=intel","-o","/Users/peterterpstra/Desktop/projects/xcode/cinc/cinc/out","/Users/peterterpstra/Desktop/projects/xcode/cinc/cinc/out.s",(char*)NULL);
|
|
|
|
perror("cinc");
|
2018-09-09 12:15:27 -05:00
|
|
|
}
|