// Goldie: GOLD Engine for D
// Written in the D programming language.

module goldie.goldie;

import tango.io.FilePath;
import tango.io.device.File;
import tango.text.Util;

import semitwist.os;
import semitwist.treeout;
import semitwist.ver;
import semitwist.util.all;

import goldie.base;
import goldie.lang;
import goldie.lexer;
import goldie.parser;

public static class Goldie
{
	/// The directory in which to look for language files
	/// whenever the language's filename isn't given.
	public static char[] langDir;
	
	private static char[] _verStr = "0.01";
	public static char[] verStr()
	{
		return _verStr.dup;
	}

	private static Ver _ver;
	public static Ver ver()
	{
		return _ver;
	}
	
	public static this()
	{
		// Set default, but only if no other static class/module ctor has already set it
		if(langDir is null)
			langDir = getExecPath()~"lang"~pathSep;
		
		_ver = _verStr.toVer();
	}
	
	public static char[] inferLanguageOf(char[] sourceFilename)
	{
		return (new FilePath(sourceFilename)).ext;
	}
	
	public static char[] inferLanguageFilenameOf(char[] sourceFilename)
	{
		return sourceFilename.inferLanguageOf().getLanguageFilename();
	}

	public static char[] getLanguageFilename(char[] language)
	{
		char[] dir = langDir;
		if(!dir.endsWith(pathSep))
			dir = dir~pathSep;

		return dir~language~".cgt";
	}
	
/*	public static GoldParser parseFile(char[] filename)
	{
		return loadLanguage(inferLanguageFile(filename)).parseFile(filename);
	}

	public static GoldParser parseCode(char[] source, char[] lang)
	{
		return loadLanguage(getLanguageFile(lang)).parseCode(source);
	}
*/

	public static GoldLanguage loadLanguageOf(char[] sourceFilename)
	{
		return sourceFilename.inferLanguageOf().loadLanguage();
	}

	public static GoldLanguage loadLanguage(char[] language)
	{
		return language.getLanguageFilename().loadCGT();
	}
	
	public static GoldLanguage loadCGT(char[] filename)
	{
		return new GoldLanguage(filename);
	}
}
