Artifact Content
Not logged in

Artifact abae545f4409680b811fbd5ac043cf811b1fad4a


/**
 * Authors: k.inaba
 * License: NYSL 0.9982 (http://www.kmonos.net/nysl/
 *
 * Entry point for Polemy interpreter.
 */

import std.stdio;
import std.algorithm;
import polemy.value;
import polemy.lex;
import polemy.parse;
import polemy.ast;
import polemy.eval;

/// Tenuki Read-Eval-Print-Loop
class REPL
{
	Table ctx;
	string buf;
	Value  lastVal;
	int lineno = 1;
	int nextlineno = 1;
	this() { ctx = createGlobalContext(); }

	bool tryRun( string s )
	{
		scope(failure)
			{ buf = ""; lineno = nextlineno; }

		buf ~= s;
		nextlineno ++;
		try 
			{ lastVal = eval(parseString(buf, "<REPL>", lineno), ctx, false, "@v"); }
		catch( UnexpectedEOF )
			{ return false; } // wait
		buf = "";
		lineno = nextlineno;
		return true;
	}

	bool singleInteraction()
	{
		writef(">> ", lineno);
		string line = readln();
		if( line.startsWith("exit") || line.startsWith("quit") )
			return false;
		try {
			if( tryRun(line) )
				writeln(lastVal);
		} catch(Throwable e) {
			writeln(e);
		}
		return true;
	}
}

version(unittest) {
	bool success = false;
	static ~this(){ if(!success){writeln("(press enter to exit)"); readln();} }
}

/// Entry point. If args.length==1, invoke REPL.
/// Otherwise interpret the argument as a filename.
void main( string[] args )
{
	version(unittest) success=true;

	if( args.length <= 1 )
	{
		writeln("Welcome to Polemy 0.1.0");
		for(auto r = new REPL; r.singleInteraction();) {}
	}
	else
	{
		evalFile(args[1]);
	}
}