/* ASTPrintString.c */
/*****************************************************************************/
/*                                                                           */
/*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
/*    Copyright (C) 1994  Thomas R. Lawrence                                 */
/*                                                                           */
/*    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 of the License, 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., 675 Mass Ave, Cambridge, MA 02139, USA.              */
/*                                                                           */
/*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
/*                                                                           */
/*****************************************************************************/

#include "MiscInfo.h"
#include "Audit.h"
#include "Debug.h"
#include "Definitions.h"

#include "ASTPrintString.h"
#include "TrashTracker.h"
#include "Memory.h"
#include "ASTExpression.h"


struct ASTPrintStringRec
	{
		char*								MessageString;
		long								LineNumber;
	};


/* create a new AST string print */
ASTPrintStringRec*	NewPrintString(char* String, struct TrashTrackRec* TrashTracker,
											long LineNumber)
	{
		ASTPrintStringRec*	PrintString;

		PrintString = (ASTPrintStringRec*)AllocTrackedBlock(sizeof(ASTPrintStringRec),
			TrashTracker);
		if (PrintString == NIL)
			{
				return NIL;
			}
		SetTag(PrintString,"ASTPrintStringRec");

		PrintString->MessageString = String;
		PrintString->LineNumber = LineNumber;

		return PrintString;
	}


/* type check the string print node.  this returns eCompileNoError if */
/* everything is ok, and the appropriate type in *ResultingDataType. */
CompileErrors				TypeCheckPrintString(DataTypes* ResultingDataType,
											ASTPrintStringRec* PrintString, long* ErrorLineNumber,
											struct TrashTrackRec* TrashTracker)
	{
		CheckPtrExistence(PrintString);
		CheckPtrExistence(TrashTracker);

		*ResultingDataType = eBoolean;
		return eCompileNoError;
	}


/* generate code for a string print.  returns True if successful, or False if it fails. */
MyBoolean						CodeGenPrintString(struct PcodeRec* FuncCode,
											long* StackDepthParam, ASTPrintStringRec* PrintString)
	{
		long							StackDepth;

		CheckPtrExistence(FuncCode);
		CheckPtrExistence(PrintString);
		StackDepth = *StackDepthParam;

		/* do the thing */
		if (!AddPcodeInstruction(FuncCode,epPrintString,NIL))
			{
				return False;
			}
		if (!AddPcodeOperandString(FuncCode,PrintString->MessageString,
			PtrSize(PrintString->MessageString)))
			{
				return False;
			}

		/* return value */
		if (!AddPcodeInstruction(FuncCode,epLoadImmediateInteger,NIL))
			{
				return False;
			}
		if (!AddPcodeOperandInteger(FuncCode,True))
			{
				return False;
			}

		/* error instruction leaves a return code, after consuming its operand. */
		StackDepth += 1;
		ERROR(StackDepth != *StackDepthParam + 1,PRERR(ForceAbort,
			"CodeGenPrintString:  stack depth error after pushing return value"));

		*StackDepthParam = StackDepth;
		return True;
	}
