-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
127 lines (116 loc) · 3.21 KB
/
main.cpp
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
121
122
123
124
125
126
127
#include "mightstone.h"
#include "mstparser.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
int quit = 0;
MST_Object* QuitInterPreter(int nargs, MST_Object** args) {
quit = 1;
return NULL;
}
MST_Object* WriteSV(int nargs, MST_Object** args) {
ofstream ofs("main.sv");
if (!ofs) {
cout << "failed to create file" << endl;
exit(-1);
}
ofs << MST_SVCode();
return NULL;
}
MST_Object* PrintSV(int nargs, MST_Object** args) {
cout << MST_SVCode();
return NULL;
}
MST_Object* WaitEv(int nargs, MST_Object** args) {
MST_WaitTask();
return NULL;
}
MST_Object* Print(int nargs, MST_Object** args) {
if (nargs && args[0]) {
if (args[0]->Type == MST_Value) {
cout << GetIntMST_Obj(args[0]) << endl;
}
}
return NULL;
}
int main(int argc, char* argv[]) {
AddMST_ExFunc(string("quit"), QuitInterPreter);
AddMST_ExFunc(string("writesv"), WriteSV);
AddMST_ExFunc(string("printsv"), PrintSV);
AddMST_ExFunc(string("waitev"), WaitEv);
AddMST_ExFunc(string("print"), Print);
AddMST_ExFunc(string("tchart"), TimingChart);
if (argc > 1) {
for (int i = 0; i < argc-1; i++) {
FILE* fp = fopen(argv[i+1], "r");
if (!fp) {
cout << "could not open " << string(argv[i+1]) << endl;
exit(-1);
}
static char buff[0xffff];
int n = fread(buff, 1, 0xffff, fp);
if (n > 0xfffe || n < 0) {
exit(-1);
}
buff[n] = 0;
vector<MST_Object*> obj;
obj = MSTP_Parse(buff);
for (int j = 0; j < obj.size(); j++) {
if (obj[j]->Type == MST_Expression) {
MST_Object* ret = EvalMST_Expr((MST_Expr*)obj[j], 0);
if (ret) {
FreeMST_Object(ret);
}
} else if (obj[j]) {
FreeMST_Object(obj[j]);
}
}
}
ofstream ofs("main.sv");
if (!ofs) {
cout << "failed to create file" << endl;
exit(-1);
}
ofs << MST_SVCode();
} else {
MST_SetMode(1);
MSTP_SetMode(1);
vector<MST_Object*> obj;
while(!quit) {
obj = MSTP_Read();
for (int i = 0; i < obj.size(); i++) {
if (obj[i]) {
if (obj[i]->Type == MST_Expression) {
MST_Expr* expr = (MST_Expr*)obj[i];
MST_Object* ret = EvalMST_Expr(expr, 0);
if (ret && expr->Operator != MST_Bind) {
if (ret && ret->Type == MST_Value) {
MST_Val* val = (MST_Val*)ret;
if (!val->nDim && val->ItemWidth <= 32) {
cout << GetIntMST_Obj((MST_Object*)val) << endl;
}
}
}
if (ret) {
FreeMST_Object(ret);
}
} else if (obj[i]->Type == MST_Value) {
MST_Val* val = (MST_Val*)obj[i];
if (!val->nDim && val->ItemWidth <= 32) {
cout << GetIntMST_Obj((MST_Object*)val) << endl;
}
}
FreeMST_Object(obj[i]);
}
}
}
ofstream ofs("main.sv");
if (!ofs) {
cout << "failed to create file" << endl;
exit(-1);
}
ofs << MST_SVCode();
}
return 0;
}