Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
120 lines (97 loc) · 2.12 KB
/
Copy pathmain.c
File metadata and controls
120 lines (97 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*main.c*/
/**
* @brief Main program to scan, parse, and execute nuPython programs.
*
* @note Tony Park
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h> // true, false
#include <string.h> // strcspn
#include "token.h" // token defs
#include "scanner.h"
#include "parser.h"
#include "programgraph.h"
#include "ram.h"
#include "execute.h"
/**
* @brief main()
*
* usage: program.exe [filename.py]
*
* If a filename is given, the file is opened and serves as
* input to the program. If a filename is not given, then
* input is taken from the keyboard until $ is input.
*
* @return 0 => success
*/
int main(int argc, char* argv[])
{
FILE* input = NULL;
bool keyboardInput = false;
//
// where is the input coming from?
//
if (argc < 2) {
//
// no args, just the program name:
//
input = stdin;
keyboardInput = true;
}
else {
//
// assume 2nd arg is a nuPython file:
//
char* filename = argv[1];
input = fopen(filename, "r");
if (input == NULL) // unable to open:
{
printf("**ERROR: unable to open input file '%s' for input.\n", filename);
return 0;
}
keyboardInput = false;
}
if (keyboardInput) // prompt the user if appropriate:
{
printf("nuPython input (enter $ when you're done)>\n");
}
//
// call parser to check program syntax:
//
struct TokenQueue* tokens = parser_parse(input);
if (tokens == NULL)
{
//
// program has a syntax error, error msg already output:
//
printf("**parsing failed...\n");
}
else
{
printf("**parsing successful, valid syntax\n");
//
// TODO:
//
printf("**building program graph...\n");
struct STMT* program = programgraph_build(tokens);
// programgraph_print(program);
printf("**executing...\n");
struct RAM* memory = ram_init();
execute(program, memory);
printf("**done\n");
ram_print(memory);
//
// cleanup:
//
ram_destroy(memory);
programgraph_destroy(program);
tokenqueue_destroy(tokens);
}
//
// done:
//
if (!keyboardInput)
fclose(input);
return 0;
}