#include "ascript/Module.h"
#include "ascript/Expr.h"

namespace AScript {

//-----------------------------------------------------------------------------
// Module
//-----------------------------------------------------------------------------
bool Module::IsModule() const { return true; }

Module::Module(const Module &module) :
	ObjectBase(module), _pSymbol(module._pSymbol),
	_pExprScript((module._pExprScript == NULL)? NULL : module._pExprScript->Clone()),
	_moduleTerminate(module._moduleTerminate)
{
}

Module::Module(Environment *pEnvOuter, const Symbol *pSymbol,
						Expr *pExprScript, ModuleTerminateType moduleTerminate) :
	ObjectBase(pEnvOuter, ENVTYPE_Module),
	_pSymbol(pSymbol), _pExprScript(pExprScript), _moduleTerminate(moduleTerminate)
{
	AssignValue(AScript_Symbol(__name__), Value(*this, GetName()), false);
}

Module::~Module()
{
	Expr::Delete(_pExprScript);
}

Module *Module::Clone() const
{
	return new Module(*this);
}

String Module::ToString(Signal sig, bool exprFlag)
{
	String str;
	str += "<module:";
	str += GetName();
	str += ">";
	return str;
}

//-----------------------------------------------------------------------------
// utility functions
//-----------------------------------------------------------------------------
namespace AScript_Module(math) {
Expr *CreateFuncExpr(const char *name, Expr *pExprArg)
{
	return new Expr_Caller(
			new Expr_Field(
					new Expr_Symbol(Symbol::Add("math")),
					new Expr_Symbol(Symbol::Add(name))),
			new Expr_Lister(pExprArg), NULL);
}
}

}
