Last active
August 29, 2015 14:23
-
-
Save createuniverses/f195c36dcf4792e65cf8 to your computer and use it in GitHub Desktop.
Demonstration of an EvaluateString function for pForth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string> | |
std::string g_sOut; | |
std::string g_sIn; | |
int g_nInPos = 0; | |
extern "C" | |
{ | |
#include "csrc/pf_all.h" | |
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 ) | |
{ | |
} | |
} | |
std::string EvaluateForth(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: | |
break; | |
case THROW_BYE: | |
g_sOut = g_sOut + "ffInterpret exception: bye\n"; | |
break; | |
case THROW_ABORT: | |
default: | |
ffDotS(); | |
pfReportThrow( exception ); | |
pfHandleIncludeError(); | |
pfResetForthTask(); | |
break; | |
} | |
} | |
else | |
{ | |
g_sOut = g_sOut + "An exception occured in ffRefill\n"; | |
} | |
return g_sOut; | |
} | |
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) | |
int main(int argc, char **argv) | |
{ | |
char buffer[512]; | |
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 ); | |
while(1) | |
{ | |
fprintf(stdout, "> "); | |
fgets(buffer, 512, stdin); | |
if ((buffer[0] != '\n') || (strlen(buffer) > 1)) | |
{ | |
std::string sIn = std::string(buffer); | |
std::string sOut = EvaluateForth(sIn); | |
fprintf(stdout, "%s\n", sOut.c_str()); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment