/*
 *  NekoML Standard Library
 *  Copyright (c)2005 Motion-Twin
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License or the LICENSE file for more details.
 */

type stack_item {
	CFunction;
	Module : string;
	Pos : (string,int);
}

type stack = stack_item array;

neko("

	@make_stack = function(a) {
		var a = $acopy(a);
		var i = 0;
		var l = $asize(a);
		while( i < l ) {
			var k = a[i];
			a[i] =
				if( k == null ) CFunction else
				if( $typeof(k) == $tstring ) Module(k) else
				Pos(k[0],k[1]);
			i = i + 1;
		}
		return @Array.@make(a);
	}

")

function call() : stack {
	neko("@make_stack($callstack())");
}

function exc() : stack {
	neko("@make_stack($excstack())");
}

function dump(ch,stack) {
	Array.iter (function(s) {
		match s {
		| CFunction -> IO.write ch "Called from a C function\n"
		| Module m -> IO.printf ch "Called from %s (no debug available)\n" m
		| Pos (file,line) -> IO.printf ch "Called from %s line %d\n" (file,line)
		}
	}) stack
}

function print() {
	dump(IO.stdout,call());
}
