Hash Tables 20
This commit is contained in:
parent
3b234814fa
commit
a7b7348f61
20 changed files with 868 additions and 837 deletions
|
|
@ -1,47 +1,73 @@
|
|||
module clox.main;
|
||||
|
||||
import std.stdio;
|
||||
import std.file;
|
||||
import core.sys.posix.unistd : STDIN_FILENO;
|
||||
import core.stdc.stdio;
|
||||
import core.stdc.stdlib;
|
||||
|
||||
import clox.util;
|
||||
import clox.chunk;
|
||||
import clox.dbg;
|
||||
import clox.vm;
|
||||
import clox.object;
|
||||
|
||||
extern(C) int isatty(int);
|
||||
int interpretResult(VM.InterpretResult result){
|
||||
if(result == VM.InterpretResult.CompileError)
|
||||
return 65;
|
||||
else if(result == VM.InterpretResult.RuntimeError)
|
||||
return 70;
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct Lox{
|
||||
VM vm;
|
||||
this(int _){
|
||||
vm = VM(0);
|
||||
}
|
||||
int runFile(string path){
|
||||
string source = path.readText();
|
||||
VM.InterpretResult result = vm.interpret(source);
|
||||
final switch(result){
|
||||
case VM.InterpretResult.CompileError: return 65;
|
||||
case VM.InterpretResult.RuntimeError: return 70;
|
||||
case VM.InterpretResult.Ok: return 0;
|
||||
int runPrompt(){
|
||||
char[1024 * 4] line;
|
||||
while(true){
|
||||
printf(colour!("lox> ", Colour.Green).ptr);
|
||||
if(!fgets(line.ptr, line.sizeof, stdin)){
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
vm.interpret(line.ptr);
|
||||
}
|
||||
int runPrompt(){
|
||||
while(true){
|
||||
write("lox> ");
|
||||
string line = stdin.readln();
|
||||
if(!line){
|
||||
writeln();
|
||||
return 0;
|
||||
}
|
||||
vm.interpret(line);
|
||||
}
|
||||
}
|
||||
int runStdin(){
|
||||
char* source = readStdin();
|
||||
scope(exit)
|
||||
source.free();
|
||||
return vm.interpret(source).interpretResult;
|
||||
}
|
||||
int runFile(const char* path){
|
||||
char* source = path.readFile();
|
||||
if(!source)
|
||||
return 74;
|
||||
scope(exit)
|
||||
source.free();
|
||||
return vm.interpret(source).interpretResult;
|
||||
}
|
||||
|
||||
int runMain(ulong argc, const char** argv){
|
||||
vm.initialise();
|
||||
scope(exit)
|
||||
vm.free();
|
||||
|
||||
if(argc == 1 && isatty(STDIN_FILENO)){
|
||||
return runPrompt();
|
||||
} else if(argc == 1){
|
||||
return runStdin();
|
||||
} else if(argc == 2){
|
||||
return runFile(argv[1]);
|
||||
} else {
|
||||
stderr.fprintf("Usage: lox [path]\n");
|
||||
return 64;
|
||||
}
|
||||
}
|
||||
|
||||
int main(string[] argv){
|
||||
Lox lox = Lox(0);
|
||||
if(isatty(stdin.fileno))
|
||||
return lox.runPrompt();
|
||||
else
|
||||
return lox.runFile("/dev/stdin");
|
||||
version(D_BetterC){
|
||||
extern(C) int main(int argc, const char** argv){
|
||||
return runMain(argc, argv);
|
||||
}
|
||||
} else {
|
||||
int main(string[] args){
|
||||
import std.string, std.algorithm, std.array;
|
||||
return runMain(args.length, cast(const char**)args.map!toStringz.array);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue