/*
 * compiler program copyright (C) 2009 H.Niwa
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <errno.h>
#include <setjmp.h>
#include <sys/time.h>
#include <math.h>
#include <libgen.h>
#include <setjmp.h>

#include <string>
#include <complex>

#include "syserr.h"

#include "bin_node.h"
#include "gc.h"
#include "var.h"
#include "pred.h"
#include "context.h"
#include "unify.h"
#include "builtin.h"
#include "sysmodule.h"
#include "compiler.h"

extern void PushStack(Context* cx, Node* goals, Node* md, Node* env);
extern int PopStack(Context* cx, Node* &goals, Node* &md, Node* &env);

/*
 * compiler functions 
 */


int CompGetVarSub(Node* &nvar, Node* nkey, Node* nlist);

int CompCheckReserve(Context* cx, Node* goalscar, List* module);
int CompSetVar(Context* cx, Node* goalscar, List* module);
int CompGetVar(Context* cx, Node* goalscar, List* module);
int CompCheckVar(Context* cx, Node* goalscar, List* module);
int CompNewFunc(Context* cx, Node* goalscar, List* module);
int CompEndFunc(Context* cx, Node* goalscar, List* module);
int CompStructRecord(Context* cx, Node* goalscar, List* module);

/* ---------------------------------------------------------*/

int compmodule(Context* cx, Node* goalscar, Node* goalscdr, 
				Node* goals, List* module, int& r)
{
	Node* retn;
	int	rn;

	std::string	s;

	if (goalscar->Val()->Car()->kind() == ATOM) {
		((Atom*)(goalscar->Val()->Car()))->toString(s);

		if (s == "CheckReserved") {
			r = CompCheckReserved(cx, goalscar, module);
			return 1;
		} else if (s == "GetVar") {
			r = CompGetVar(cx, goalscar, module);
			return 1;
		} else if (s == "SetVar") {
			r = CompSetVar(cx, goalscar, module);
			return 1;
		} else if (s == "CheckVar") {
			r = CompCheckVar(cx, goalscar, module);
			return 1;
		} else if (s == "NewFunc") {
			r = CompNewFunc(cx, goalscar, module);
			return 1;
		} else if (s == "EndFunc") {
			r = CompEndFunc(cx, goalscar, module);
			return 1;
		}
	}
	r = -1;
	syserr("::compiler %s: The predicate that did not exist in the compiler module is used. \n",
			s.c_str());
	return 1;
}


int CompGetVarSub(Node* &nout, Node* nkey, Node* nlist)
{
	Node* ne, * n;
	nout = Nil;

	for (ne = nlist; ne != Nil; ne = ne->Cdr()) {
		for (n = ne->Car(); n->kind() != ATOM; n = n->Cdr()) {
			if (n->Car()->Car()->Eq(nkey)) {
				nout = n->Car()->Cdr()->Car();
				return 1;
			}
		}
	}
	return 0;
}


int CompCheckReserved(Context* cx, Node* goalscar, List* module)
{
	goalscar = goalscar->Cdr()->Val();
	int ll = ListLength(goalscar);
	if (ll != 2) {
		syserr("usage : <CheckReserved NAME LIST> \n");
		return 0;
	}

	Node* nname = goalscar->Car()->Val();
	int rn;

	if ((rn = FuncArg(cx, nname, goalscar, module)) <= 0) {
		syserr("CheckReserved: failed in the evaluation of the argument. \n");
		return 0;
	}

	if (nname->kind() != ATOM) {
		syserr("usage : <CheckReserved NAME LIST> \n");
		return 0;
	}

	Node* nlist = goalscar->Cdr()->Car()->Val();

	if ((rn = FuncArg(cx, nlist, goalscar, module)) <= 0) {
		syserr("CheckReserved: failed in the evaluation of the argument. \n");
		return 0;
	}

	if (nlist->kind() != LIST) {
		syserr("usage : <CheckReserved NAME LIST> \n");
		return 0;
	}

	for (; nlist->kind() != ATOM; nlist = nlist->Cdr()) {
		if (nlist->Car()->Eq(nname)) {
			return 1;
		}
	}
	
	return -1;
}

int CompSetVar(Context* cx, Node* goalscar, List* module)
{
	/* SetVar VAR KEY Var LIST */

	goalscar = goalscar->Cdr()->Val();
	int ll = ListLength(goalscar);
	if (ll != 4) {
		syserr("usage : <SetVar VAR KEY Var LIST> \n");
		return 0;
	}

	Node* nvar = goalscar->Car()->Val();

	if (nvar->kind() != UNDEF) {
		syserr("usage : <SetVar VAR KEY Var LIST> \n");
		return 0;
	}

	Node* nkey = goalscar->Cdr()->Car()->Val();
	int rn;

	if ((rn = FuncArg(cx, nkey, goalscar, module)) <= 0) {
		syserr("SetVar: failed in the evaluation of the argument. \n");
		return 0;
	}

	Node* nval = goalscar->Cdr()->Cdr()->Car()->Val();

	if ((rn = FuncArg(cx, nval, goalscar, module)) <= 0) {
		syserr("SetVar: failed in the evaluation of the argument. \n");
		return 0;
	}

	Node* nlist = goalscar->Cdr()->Cdr()->Cdr()->Car()->Val();

	if ((rn = FuncArg(cx, nlist, goalscar, module)) <= 0) {
		syserr("SetVar: failed in the evaluation of the argument. \n");
		return 0;
	}

	if ((nlist->kind() != LIST) && (nlist != Nil)) {
		syserr("usage : <SetVar VAR KEY Var LIST> \n");
		return 0;
	}

	Node* nout;
	if (CompGetVarSub(nout, nkey, nlist)) {
		return -1;
	}
		
	nlist = Cons(Cons(MkList(nkey, nval), nlist->Car()), nlist->Cdr());
		
	Node* env = Nil->Cons(Nil);

	SetEnv(env, nvar);
	((Undef*)(nvar->Val()))->Set(nlist);

	PushStack(cx, Nil, Nil, env);

	return 1;
}

int CompGetVar(Context* cx, Node* goalscar, List* module)
{
	/* GetVar VAR KEY LIST */

	goalscar = goalscar->Cdr()->Val();
	int ll = ListLength(goalscar);
	if (ll != 3) {
		syserr("usage : <GetVar VAR KEY LIST> \n");
		return 0;
	}

	Node* nvar = goalscar->Car()->Val();

	if (nvar->kind() != UNDEF) {
		syserr("usage : <GetVar VAR KEY LIST> \n");
		return 0;
	}

	Node* nkey = goalscar->Cdr()->Car()->Val();
	int rn;

	if ((rn = FuncArg(cx, nkey, goalscar, module)) <= 0) {
		syserr("GetVar: failed in the evaluation of the argument. \n");
		return 0;
	}

	Node* nlist = goalscar->Cdr()->Cdr()->Car()->Val();

	if ((rn = FuncArg(cx, nlist, goalscar, module)) <= 0) {
		syserr("GetVar: failed in the evaluation of the argument. \n");
		return 0;
	}

	if (nlist->kind() != LIST) {
		syserr("usage : <GetVar VAR KEY LIST> \n");
		return 0;
	}

	Node* nout;
	if (!CompGetVarSub(nout, nkey, nlist)) {
		return -1;
	}
		
	Node* env = Nil->Cons(Nil);

	SetEnv(env, nvar);
	((Undef*)(nvar->Val()))->Set(nout);

	PushStack(cx, Nil, Nil, env);

	return 1;
}

int CompCheckVar(Context* cx, Node* goalscar, List* module)
{
	/* GetVar KEY LIST */

	goalscar = goalscar->Cdr()->Val();
	int ll = ListLength(goalscar);
	if (ll != 2) {
		syserr("usage : <CheckVar KEY LIST> \n");
		return 0;
	}

	Node* nkey = goalscar->Car()->Val();
	int rn;

	if ((rn = FuncArg(cx, nkey, goalscar, module)) <= 0) {
		syserr("CheckVar: failed in the evaluation of the argument. \n");
		return 0;
	}

	Node* nlist = goalscar->Cdr()->Car()->Val();

	if ((rn = FuncArg(cx, nlist, goalscar, module)) <= 0) {
		syserr("CheckVar: failed in the evaluation of the argument. \n");
		return 0;
	}

	nlist = nlist->Val();
	if (nlist->kind() != LIST) {
		syserr("usage : <CheckVar KEY LIST> \n");
		return 0;
	}

	Node* nout;
	if (!CompGetVarSub(nout, nkey, nlist)) {
		return -1;
	}
		
	return 1;
}

int CompNewFunc(Context* cx, Node* goalscar, List* module)
{
	/* NewFunc VAR LIST */
	
	goalscar = goalscar->Cdr()->Val();
	int ll = ListLength(goalscar);
	if (ll != 2) {
		syserr("usage : <NewFunc VAR LIST> \n");
		return 0;
	}

	Node* nvar = goalscar->Car()->Val();

	if (nvar->kind() != UNDEF) {
		syserr("usage : <NewFunc VAR LIST> \n");
		return 0;
	}

	Node* nlist = goalscar->Cdr()->Car()->Val();

	int rn;
	
	if ((rn = FuncArg(cx, nlist, goalscar, module)) <= 0) {
		syserr("NewFunc: failed in the evaluation of the argument. \n");
		return 0;
	}

	if (nlist->kind() != LIST) {
		syserr("usage : <NewFunc VAR LIST> \n");
		return 0;
	}

	nlist = Cons(Nil, nlist);
	
	Node* env = Nil->Cons(Nil);

	SetEnv(env, nvar);
	((Undef*)(nvar->Val()))->Set(nlist);

	PushStack(cx, Nil, Nil, env);

	return 1;
}

int CompEndFunc(Context* cx, Node* goalscar, List* module)
{
	/* EndFunc VAR LIST */
	
	goalscar = goalscar->Cdr()->Val();
	int ll = ListLength(goalscar);
	if (ll != 2) {
		syserr("usage : <EndFunc VAR LIST> \n");
		return 0;
	}

	Node* nvar = goalscar->Car()->Val();

	if (nvar->kind() != UNDEF) {
		syserr("usage : <EndFunc VAR LIST> \n");
		return 0;
	}

	Node* nlist = goalscar->Cdr()->Car()->Val();

	int rn;
	
	if ((rn = FuncArg(cx, nlist, goalscar, module)) <= 0) {
		syserr("EndFunc: failed in the evaluation of the argument. \n");
		return 0;
	}

	if (nlist->kind() != LIST) {
		syserr("usage : <EndFunc VAR LIST> \n");
		return 0;
	}

	nlist = nlist->Cdr();
	
	Node* env = Nil->Cons(Nil);

	SetEnv(env, nvar);
	((Undef*)(nvar->Val()))->Set(nlist);

	PushStack(cx, Nil, Nil, env);

	return 1;
}



