119 lines
2.3 KiB
C
119 lines
2.3 KiB
C
|
#include "common.h"
|
||
|
#include "chunk.h"
|
||
|
#include "debug.h"
|
||
|
#include "vm.h"
|
||
|
|
||
|
#include <time.h>
|
||
|
|
||
|
static void repl( VM* vm ) {
|
||
|
char line[512];
|
||
|
|
||
|
for ( ; ; ) {
|
||
|
printf( "> " );
|
||
|
if ( !fgets( line, sizeof(line), stdin ) ) {
|
||
|
printf( "\n" ); break;
|
||
|
}
|
||
|
interpret( vm, line );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static char* readFile( const char* path ) {
|
||
|
FILE* file = fopen( path, "rb" );
|
||
|
if ( file == NULL ) {
|
||
|
fprintf( stderr, "Error opening file: \"%s\"\n", path );
|
||
|
exit( 74 );
|
||
|
}
|
||
|
|
||
|
fseek( file, 0, SEEK_END );
|
||
|
size_t sz = ftell( file );
|
||
|
rewind( file );
|
||
|
|
||
|
char* src = (char*)malloc( sz + 1 );
|
||
|
if ( src == NULL ) {
|
||
|
fprintf( stderr, "Error allocating memory for file: \"%s\"\n", path );
|
||
|
exit( 74 );
|
||
|
}
|
||
|
size_t bread = fread( src, sizeof(char), sz, file );
|
||
|
if ( bread < sz ) {
|
||
|
fprintf( stderr, "Error reading file: \"%s\"\n", path );
|
||
|
exit( 74 );
|
||
|
}
|
||
|
src[bread] = '\0';
|
||
|
|
||
|
fclose( file );
|
||
|
return src;
|
||
|
}
|
||
|
|
||
|
static void runFile( VM* vm, const char* path ) {
|
||
|
char* source = readFile( path );
|
||
|
InterpretResult res = interpret( vm, source );
|
||
|
free( source );
|
||
|
if ( res == INTERPRET_COMPILE_ERROR ) exit( 65 );
|
||
|
if ( res == INTERPRET_RUNTIME_ERROR ) exit( 70 );
|
||
|
}
|
||
|
|
||
|
int main( int argc, char** argv ) {
|
||
|
(void)argc; (void)argv;
|
||
|
srand( time( NULL ) );
|
||
|
|
||
|
double dfp = 22.0 / 7.0;
|
||
|
printf( "DFP: %.64g\n", dfp );
|
||
|
|
||
|
float ffp = 22.0f / 7.0f;
|
||
|
printf( "FFP: %.64f\n", ffp );
|
||
|
|
||
|
VM vm;
|
||
|
initVM( &vm );
|
||
|
|
||
|
if ( argc == 1 ) {
|
||
|
repl( &vm );
|
||
|
} else if ( argc == 2 ) {
|
||
|
runFile( &vm, argv[1] );
|
||
|
} else {
|
||
|
fprintf( stderr, "Usage: clox [path]\n" );
|
||
|
exit( EXIT_FAILURE );
|
||
|
}
|
||
|
|
||
|
/*Chunk c;
|
||
|
initChunk( &c );
|
||
|
//c.bDebug = false;
|
||
|
|
||
|
writeConstant( &c, 4.2f, 1 );
|
||
|
writeConstant( &c, rand()/(float)RAND_MAX, 2 );
|
||
|
|
||
|
//for ( int i = 0; i < 10; ++i )
|
||
|
// writeChunk( &c, rand() % NUM_OP, 5 );
|
||
|
|
||
|
// const overload
|
||
|
for ( int i = 2; i < 6; ++i ) {
|
||
|
writeConstant( &c, (float)rand()/RAND_MAX, 6 );
|
||
|
}
|
||
|
|
||
|
writeConstant( &c, 69069, 9 );
|
||
|
writeChunk( &c, OP_NEGATE, 9 );
|
||
|
|
||
|
writeConstant( &c, 22, 9 );
|
||
|
writeChunk( &c, OP_ADD, 9 );
|
||
|
|
||
|
writeConstant( &c, 42, 9 );
|
||
|
writeChunk( &c, OP_DIVIDE, 9 );
|
||
|
|
||
|
writeChunk( &c, OP_RETURN, 10 );
|
||
|
|
||
|
//disassembleChunk( &c, "test" );
|
||
|
|
||
|
interpret( &vm, &c );
|
||
|
printChunkInfo( &c, "test" );
|
||
|
|
||
|
printf( "vm %u chunk %u valar %u\n", sizeof(VM), sizeof(Chunk), sizeof(ValueArray) );
|
||
|
|
||
|
freeChunk( &c );*/
|
||
|
|
||
|
freeVM( &vm );
|
||
|
|
||
|
return EXIT_SUCCESS;
|
||
|
}
|
||
|
|
||
|
//void* malloc( size_t sz ) {}
|
||
|
|