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>
|
|
|
|
#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-09 20:12:20 -05:00
|
|
|
#define PARSER_DEBUG 0
|
|
|
|
|
2018-09-09 12:15:27 -05:00
|
|
|
int main(int argc, const char * argv[]) {
|
2018-09-09 20:12:20 -05:00
|
|
|
char* prgstr="int main() {\n return 173;\n}\n";
|
|
|
|
if (PARSER_DEBUG) {
|
|
|
|
printf("Program:\n");
|
|
|
|
printf("%s",prgstr);
|
|
|
|
}
|
2018-09-09 12:15:27 -05:00
|
|
|
Token* tokens=tokenize(prgstr);
|
2018-09-09 20:12:20 -05:00
|
|
|
if (PARSER_DEBUG) {
|
|
|
|
printf("Tokens:\n");
|
|
|
|
Token* tok=tokens;
|
|
|
|
while (tok) {
|
|
|
|
print_tok(tok);
|
|
|
|
tok=tok->next;
|
|
|
|
}
|
|
|
|
}
|
2018-09-09 17:31:02 -05:00
|
|
|
AstNode* ast=parse(tokens);
|
2018-09-09 12:15:27 -05:00
|
|
|
free_toklist(tokens);
|
2018-09-09 20:12:20 -05:00
|
|
|
if (PARSER_DEBUG) {
|
|
|
|
printf("AST:\n");
|
|
|
|
print_tree(ast, 0);
|
|
|
|
}
|
2018-09-09 18:08:00 -05:00
|
|
|
char* prg=generate_prg(ast);
|
|
|
|
free_tree(ast);
|
2018-09-09 20:12:20 -05:00
|
|
|
if (PARSER_DEBUG) {
|
|
|
|
printf("Output assembly:\n");
|
|
|
|
printf("%s",prg);
|
|
|
|
}
|
|
|
|
FILE* outfile=fopen("/Users/peterterpstra/Desktop/projects/xcode/cinc/cinc/out.mys","w");
|
2018-09-09 18:08:00 -05:00
|
|
|
fputs(prg, outfile);
|
|
|
|
fclose(outfile);
|
|
|
|
free(prg);
|
2018-09-09 12:15:27 -05:00
|
|
|
}
|