#!/usr/bin/env php
<?php
/**
 * This file is part of the Tea programming language project
 *
 * @author 		Benny <benny@meetdreams.com>
 * @copyright 	(c)2019 YJ Technology Ltd. [http://tealang.org]
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
 */

/// The complier script for the Tea programming language

namespace Tea;

define('BASE_PATH', dirname(__DIR__) . '/');
define('LOG_PATH', BASE_PATH . 'log/');

const DEBUG = false;

const KEY_INIT = 'init';
const OPTION_KEYS = [KEY_INIT];

const USAGE = 'Usage: tea tea/tests/syntax
Usage: tea --init myproject/hello';

require BASE_PATH . 'compiler/__unit.php';

try {
	$opts = process_cli_options($argv, OPTION_KEYS);

	$target = $opts[0] ?? null;
	if ($target === null) {
		error("Missed target for compile.");
		halt(USAGE);
	}

	// init
	if (isset($opts[KEY_INIT])) {
		$init = new TeaInitializer($target);
		$init->process();
		halt("Init '$target' success.");
	}

	// compile
	$realpath = realpath($target);
	if ($realpath === false) {
		error("Target '{$target}' not found.");
		halt(USAGE);
	}

	echo NL;
	$realpath = FileHelper::normalize_path($realpath . DS);
	$compiler = new Compiler($realpath);
	$count = $compiler->make();

	halt("$count programs compiled.");
}
catch (\Exception $e) {
	halt($e->getMessage());
	// echo $e->getTraceAsString(), NL, NL;
}
