-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathforthInterface.cpp
More file actions
180 lines (144 loc) · 2.95 KB
/
forthInterface.cpp
File metadata and controls
180 lines (144 loc) · 2.95 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "forthInterface.h"
std::string g_sOut;
std::string g_sIn;
int g_nInPos = 0;
extern "C"
{
#include "pforth/pf_all.h"
extern CFunc0 CustomFunctionTable[];
int sdTerminalOut( char c )
{
//printf("%c", c);
g_sOut = g_sOut + c;
return c;
}
int sdTerminalEcho( char c )
{
//printf("%c", c);
g_sOut = g_sOut + c;
return c;
}
int sdTerminalIn( void )
{
if(g_nInPos >= g_sIn.length())
return '\n';
if(g_nInPos < 0)
return '\n';
char c = g_sIn[g_nInPos];
g_nInPos++;
if(g_nInPos >= g_sIn.length())
g_nInPos = -1;
return c;
}
int sdQueryTerminal( void )
{
return g_nInPos < g_sIn.length();
}
int sdTerminalFlush( void )
{
//return fflush(PF_STDOUT);
return true;
}
void sdTerminalInit( void )
{
}
void sdTerminalTerm( void )
{
}
}
PForthTask g_cftd = 0;
PForthDictionary g_dic = 0;
ExecToken g_entryPoint = 0;
#define DEFAULT_RETURN_DEPTH (512)
#define DEFAULT_USER_DEPTH (512)
#define DEFAULT_HEADER_SIZE (120000)
#define DEFAULT_CODE_SIZE (300000)
void forthInit()
{
pfInit();
g_cftd = pfCreateTask( DEFAULT_USER_DEPTH, DEFAULT_RETURN_DEPTH );
pfSetCurrentTask( g_cftd );
//g_dic = pfLoadDictionary( "pforth.dic", &g_entryPoint );
g_dic = pfBuildDictionary( DEFAULT_HEADER_SIZE, DEFAULT_CODE_SIZE );
}
bool forthCall(std::string sCmd)
{
g_nInPos = 0;
g_sIn = sCmd;
g_sOut = "";
if(ffRefill() > 0)
{
cell_t exception = ffInterpret();
if( exception == 0 )
{
// ffInterpret ran the forth code no problem
// ffOK now checks the stack.
exception = ffOK();
}
// This will print out an error message, if any.
switch( exception )
{
case 0:
return true;
break;
case THROW_BYE:
g_sOut = g_sOut + "ffInterpret exception: bye\n";
return false;
break;
case THROW_ABORT:
default:
ffDotS();
pfReportThrow( exception );
pfHandleIncludeError();
pfResetForthTask();
return false;
break;
}
}
else
{
g_sOut = g_sOut + "An exception occured in ffRefill\n";
return false;
}
//return g_sOut;
return true;
}
std::string g_sForthError;
std::string & forthGetError()
{
return g_sForthError;
}
std::string & forthGetOutput()
{
return g_sOut;
}
void forthClearError()
{
}
void forthClearOutput()
{
g_sOut = "";
}
std::string forthGetState()
{
return "NA";
}
void forthClose()
{
}
void praxisDefinePForthCFunction(const char * name, CFunc0 func)
{
static int i = 0;
if(i<PF_MAX_CUSTOM_FUNCTIONS)
{
CustomFunctionTable[i] = func;
CreateGlueToC( name, i, C_RETURNS_VOID, 0 );
i++;
printf("Defined PForth C function %s, index is now %d\n", name, i);
}
else
{
printf("Maximum number of custom PForth C functions reached.\n");
printf("Increase PF_MAX_CUSTOM_FUNCTIONS.\n");
}
}