mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 22:28:45 +01:00
Start hacking in a REPL.
This commit is contained in:
44
src/main.c
44
src/main.c
@ -1,10 +1,14 @@
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "compiler.h"
|
||||
#include "vm.h"
|
||||
|
||||
// TODO(bob): Don't hardcode this.
|
||||
#define MAX_LINE 1024
|
||||
|
||||
void failIf(int condition, int exitCode, const char* format, ...)
|
||||
{
|
||||
if (!condition) return;
|
||||
@ -39,11 +43,10 @@ char* readFile(const char* path, size_t* length)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int main(int argc, const char * argv[])
|
||||
int runFile(const char* path)
|
||||
{
|
||||
// TODO(bob): Validate command line arguments.
|
||||
size_t length;
|
||||
char* source = readFile(argv[1], &length);
|
||||
char* source = readFile(path, &length);
|
||||
VM* vm = newVM();
|
||||
ObjBlock* block = compile(vm, source, length);
|
||||
|
||||
@ -62,3 +65,38 @@ int main(int argc, const char * argv[])
|
||||
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
int runRepl()
|
||||
{
|
||||
VM* vm = newVM();
|
||||
|
||||
for (;;)
|
||||
{
|
||||
printf("> ");
|
||||
char line[MAX_LINE];
|
||||
fgets(line, MAX_LINE, stdin);
|
||||
// TODO(bob): Handle failure.
|
||||
size_t length = strlen(line);
|
||||
ObjBlock* block = compile(vm, line, length);
|
||||
|
||||
if (block != NULL)
|
||||
{
|
||||
Value result = interpret(vm, block);
|
||||
printf("= ");
|
||||
printValue(result);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
freeVM(vm);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[])
|
||||
{
|
||||
if (argc == 1) return runRepl();
|
||||
if (argc == 2) return runFile(argv[1]);
|
||||
|
||||
fprintf(stderr, "Usage: wren [file]");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user