[go: up one dir, main page]

Menu

[r1]: / frontend.c  Maximize  Restore  History

Download this file

115 lines (98 with data), 2.2 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
/*
interactive.c
an interactive frontend for the lithp2 engine
(A total rewrite from "lithp" to be more compatible,
and easier to work with.)
Scott Lawrence
2006-Nov-27
*/
#include <stdio.h>
#include <string.h> /* for strcmp */
#include "lithp.h"
#include "version.h"
#define BUFSIZE (2048)
/* wipe out some useless characters from the input string */
void trim( char * buf )
{
int pos = 0;
if( !buf ) return;
for( pos=0; buf[pos] != '\0' && pos < BUFSIZE ; pos++ )
{
switch( buf[pos] ) {
/* anything from a 'newline' on gets eliminated */
/* this might become obsolete with a better parser later */
case( '\n' ):
case( '\r' ):
buf[pos] = '\0';
return;
default:
break;
}
}
}
/* a simple user input-parsing loop */
void cmdloop()
{
LITHP_STATE * ls;
int done = 0;
char buf[BUFSIZE];
char *str;
ls = lithp_create();
while( !done )
{
/* print the prompt */
printf( "-> " );
fflush( stdout );
/* read in a command */
str = fgets( buf, BUFSIZE, stdin );
trim( str );
if( !str ) {
if( feof( stdin )) {
/* silently exit on EOF -- ctrl-D */
printf( "\n" );
done = 1;
} else if( ferror( stdin )) {
clearerr( stdin );
printf( "E: File error %d\n", ferror( stdin ));
done = 1;
}
} else {
if( !strcmp( str, "Exit." )) done = 1;
if( !done ) {
lithp_read( ls, str );
lithp_evaluate( ls );
}
}
}
lithp_destroy( ls );
}
/* print out a standard usage string */
void usage( char * argv0 )
{
/*
fprintf( stderr, "Usage: %s [file.lsp]\n", argv0 );
fprintf( stderr, "\tSpecify lsp file or use interactive mode.\n");
*/
fprintf( stderr, "Usage: %s\n", argv0 );
fprintf( stderr, "\tInteractive mode only for now.\n");
}
/* main() - argv parsing, etc */
int main( int argc, char ** argv )
{
/*
if( argc == 2 )
{
printf( "Should parse in file %s\n", argv[1] );
} else */
if( argc == 1 ) {
printf( "Generic Common Lithp v%s r%s\n", VERSION, REVISION );
printf( "\t\"%s\"\n", TPOTV );
printf( "[ctrl]-[d] or \"Exit.\" to exit\n" );
cmdloop();
printf( "Exiting.\n" );
} else {
usage( argv[0] );
return( -1 );
}
return( 0 );
}