SBSPSS/Utils/Scripter/function.cpp

186 lines
3.9 KiB
C++
Raw Normal View History

2000-12-14 23:32:17 +01:00
/*=========================================================================
function.cpp
Author: PKG
Created:
Project: Spongebob
Purpose:
Copyright (c) 2000 Climax Development Ltd
===========================================================================*/
/*----------------------------------------------------------------------
Includes
-------- */
#include "function.h"
#ifndef _LEXER_H
#include "lexer.h"
#endif
#ifndef _PARSER_H
#include "parser.h"
#endif
/* Std Lib
------- */
#include <stdio.h>
#include <string.h>
/* Data
---- */
/*----------------------------------------------------------------------
Tyepdefs && Defines
------------------- */
/*----------------------------------------------------------------------
Structure defintions
-------------------- */
2000-12-15 21:29:33 +01:00
struct FunctionDef
{
char *m_name;
int m_argCount;
};
2000-12-14 23:32:17 +01:00
/*----------------------------------------------------------------------
Function Prototypes
------------------- */
/*----------------------------------------------------------------------
Vars
---- */
static FunctionDef s_functionNames[]=
{
2001-01-05 20:25:07 +01:00
{ "setCharacterAnimation", 2 }, // characterId,animationId
2001-01-09 21:04:39 +01:00
{ "setText", 2 }, // characterId,textId
2001-01-05 20:25:07 +01:00
{ "setResponseOptions", 1 }, // optionsId
{ "getResponse", 0 }, //
2001-07-14 00:10:16 +02:00
{ "getAmmoCount", 1 }, // ammoId
{ "setAmmoCount", 2 }, // ammoId,amount
{ "isHoldingWeapon", 1 }, // weaponId
2001-07-18 18:48:49 +02:00
{ "giveWeapon", 1 }, // weaponId
2000-12-14 23:32:17 +01:00
};
static int s_functionCount=sizeof(s_functionNames)/sizeof(FunctionDef);
extern mylexer s_lexer;
extern myparser s_parser;
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
2000-12-15 21:29:33 +01:00
extern int lookupFunctionName(char *_name)
2000-12-14 23:32:17 +01:00
{
int i;
FunctionDef *fp;
fp=s_functionNames;
for(i=0;i<s_functionCount;i++)
{
if(strcmp(fp->m_name,_name)==0)
{
2000-12-15 21:29:33 +01:00
return i;
2000-12-14 23:32:17 +01:00
}
fp++;
}
printf("Unknown function name _%s\n",_name);
s_lexer.error();
2000-12-15 21:29:33 +01:00
return -1;
2000-12-14 23:32:17 +01:00
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns: Argument count for the requestion function
---------------------------------------------------------------------- */
extern int getFunctionArgCount(int _functionNumber)
{
return s_functionNames[_functionNumber].m_argCount;
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns: A treenode that contains all of the functions arguments
---------------------------------------------------------------------- */
2000-12-15 21:29:33 +01:00
extern CTreeNode *getFunctionArgs(int _functionNumber)
2000-12-14 23:32:17 +01:00
{
2000-12-15 21:29:33 +01:00
int argCount,i;
2000-12-14 23:32:17 +01:00
int tokenType;
2000-12-15 21:29:33 +01:00
argCount=s_functionNames[_functionNumber].m_argCount;
if(argCount)
2000-12-14 23:32:17 +01:00
{
CTreeNode *base;
2000-12-15 21:29:33 +01:00
base=NULL;
for(i=0;i<argCount;i++)
2000-12-14 23:32:17 +01:00
{
tokenType=s_parser.yygettoken();
switch(tokenType)
{
case VARIABLE:
2000-12-15 21:29:33 +01:00
{
int var;
var=*((int*)s_parser.yylvalptr);
if(base)
base=new CTreeNode(STMT_LIST,base,new CTreeNode(VARIABLE_EXPR,(int)var));
else
base=new CTreeNode(STMT_LIST,new CTreeNode(VARIABLE_EXPR,(int)var));
2000-12-14 23:32:17 +01:00
break;
2000-12-15 21:29:33 +01:00
}
2000-12-14 23:32:17 +01:00
case VALUE:
2000-12-15 21:29:33 +01:00
{
signed short value;
value=*((signed short*)s_parser.yylvalptr);
if(base)
base=new CTreeNode(STMT_LIST,base,new CTreeNode(VALUE_EXPR,(int)value));
else
base=new CTreeNode(STMT_LIST,new CTreeNode(VALUE_EXPR,(int)value));
2000-12-14 23:32:17 +01:00
break;
2000-12-15 21:29:33 +01:00
}
2000-12-14 23:32:17 +01:00
default:
printf("UNEXPECTED TOKEN '%s' FOR ARGUMENT %d\n",s_lexer.yytext,i+1);
s_lexer.error();
break;
}
2000-12-15 21:29:33 +01:00
if(i<argCount-1)
{
if(s_parser.yygettoken()!=COMMA)
{
printf("EXPETCING ',' BUT FOUND '%s'\n",s_lexer.yytext);
s_lexer.error();
break;
}
}
2000-12-14 23:32:17 +01:00
}
2000-12-15 21:29:33 +01:00
return base;
2000-12-14 23:32:17 +01:00
}
2000-12-15 21:29:33 +01:00
return new CTreeNode(EMPTY_STMT);
2000-12-14 23:32:17 +01:00
}
/*===========================================================================
end */