This commit is contained in:
Paul 2000-12-15 20:29:33 +00:00
parent 48629e2f3d
commit e98f64c419
20 changed files with 1052 additions and 637 deletions

View file

@ -39,7 +39,7 @@
typedef struct
{
signed short (*m_func)(signed short *_args);
signed short (*m_func)(unsigned short *_args);
int m_argCount;
} FunctionDef;
@ -48,14 +48,20 @@ typedef struct
Function Prototypes
------------------- */
static signed short func_setCharacterExpression(unsigned short *_args);
static signed short func_setText(unsigned short *_args);
/*----------------------------------------------------------------------
Vars
---- */
static FunctionDef s_functionDefs[]=
{
{ func_setCharacterExpression, 2 }, // character, expression
{ func_setText, 1 }, // textId
};
static int s_numFunctionDefs=sizeof(s_functionDefs)/sizeof(FunctionDef);
static const int s_numFunctionDefs=sizeof(s_functionDefs)/sizeof(FunctionDef);
/*----------------------------------------------------------------------
@ -64,6 +70,42 @@ static int s_numFunctionDefs=sizeof(s_functionDefs)/sizeof(FunctionDef);
Params:
Returns:
---------------------------------------------------------------------- */
signed short callFunction(int _functionNumber,int _argCount,unsigned short *_args)
{
FunctionDef *fd;
ASSERT(_functionNumber<s_numFunctionDefs);
fd=&s_functionDefs[_functionNumber];
ASSERT(_argCount==fd->m_argCount);
return fd->m_func(_args);
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
static signed short func_setCharacterExpression(unsigned short *_args)
{
return _args[0];
}
/*----------------------------------------------------------------------
Function:
Purpose:
Params:
Returns:
---------------------------------------------------------------------- */
static signed short func_setText(unsigned short *_args)
{
return _args[0];
}
/*===========================================================================
end */

View file

@ -37,6 +37,9 @@
Functions
--------- */
signed short callFunction(int _functionNumber,int _argCount,unsigned short *_args);
/*---------------------------------------------------------------------- */
#endif /* __SCRIPT_FUNCTION_H__ */

View file

@ -18,6 +18,10 @@
#include "script\script.h"
#ifndef __SCRIPT_FUNCTION_H__
#include "script\function.h"
#endif
#ifndef __SYSTEM_DBG_H__
#include "system\dbg.h"
#endif
@ -59,6 +63,10 @@ signed short CScript::s_globalVars[NUM_GLOBAL_VARS]=
};
// Buffer for passing arguments to functions
unsigned short CScript::s_argBuffer[MAX_FUNCTION_ARGS];
/*----------------------------------------------------------------------
Function:
Purpose:
@ -149,6 +157,7 @@ void CScript::executeNextInstruction()
{
unsigned short instruction;
signed short val1,val2,val3;
int i;
#ifdef FULL_CODE_OUTPUT
PAUL_DBGMSG("pc:0x%04d sp:%03d",m_pc*2,m_sp);
@ -199,7 +208,14 @@ PAUL_DBGMSG("pc:0x%04d sp:%03d",m_pc*2,m_sp);
push(val2);
break;
case OP_JMP: // jump
case OP_POP: // value
val1=pop();
#ifdef FULL_CODE_OUTPUT
PAUL_DBGMSG("POP %d",val1);
#endif
break;
case OP_JMP: // jump
val1=pop();
#ifdef FULL_CODE_OUTPUT
PAUL_DBGMSG("JMP %d",val1);
@ -207,7 +223,7 @@ PAUL_DBGMSG("pc:0x%04d sp:%03d",m_pc*2,m_sp);
jump(val1);
break;
case OP_JMPF: // jump, value
case OP_JMPF: // jump, value
val1=pop();
val2=pop();
#ifdef FULL_CODE_OUTPUT
@ -216,7 +232,7 @@ PAUL_DBGMSG("pc:0x%04d sp:%03d",m_pc*2,m_sp);
if(val2==0)jump(val1);
break;
case OP_JMPT: // jump, value
case OP_JMPT: // jump, value
val1=pop();
val2=pop();
#ifdef FULL_CODE_OUTPUT
@ -225,7 +241,7 @@ PAUL_DBGMSG("pc:0x%04d sp:%03d",m_pc*2,m_sp);
if(val2!=0)jump(val1);
break;
case OP_IS_EQUAL_VALUE: // value, value pushes result ( 0 or 1 ) to stack
case OP_IS_EQUAL_VALUE: // value, value pushes result ( 0 or 1 ) to stack
val1=pop();
val2=pop();
#ifdef FULL_CODE_OUTPUT
@ -234,7 +250,7 @@ PAUL_DBGMSG("pc:0x%04d sp:%03d",m_pc*2,m_sp);
push(val1==val2);
break;
case OP_IS_NOTEQUAL_VALUE: // value, value pushes result ( 0 or 1 ) to stack
case OP_IS_NOTEQUAL_VALUE: // value, value pushes result ( 0 or 1 ) to stack
val1=pop();
val2=pop();
#ifdef FULL_CODE_OUTPUT
@ -243,7 +259,7 @@ PAUL_DBGMSG("pc:0x%04d sp:%03d",m_pc*2,m_sp);
push(val1!=val2);
break;
case OP_ASSIGN: // varidx, value
case OP_ASSIGN: // varidx, value
val1=pop();
val2=pop();
#ifdef FULL_CODE_OUTPUT
@ -252,7 +268,7 @@ PAUL_DBGMSG("pc:0x%04d sp:%03d",m_pc*2,m_sp);
setVar(val1,val2);
break;
case OP_ADD: // value, value pushes result to stack
case OP_ADD: // value, value pushes result to stack
val1=pop();
val2=pop();
#ifdef FULL_CODE_OUTPUT
@ -261,11 +277,29 @@ PAUL_DBGMSG("pc:0x%04d sp:%03d",m_pc*2,m_sp);
push(val1+val2);
break;
case OP_PRINT: // value
case OP_PRINT: // value
val1=pop();
PAUL_DBGMSG("PRINT %d",val1);
break;
case OP_CALL_FUNCTION: // functionnumber, argcount args pushes return value to stack
val1=readNextInstruction();
val2=readNextInstruction();
#ifdef FULL_CODE_OUTPUT
PAUL_DBGMSG("CALL_FUNCTION %d ( %d args )",val1,val2);
#endif
ASSERT(val2<MAX_FUNCTION_ARGS); // Too many args.. just increase the #define to fix this
for(i=0;i<val2;i++)
{
s_argBuffer[i]=pop();
}
val3=callFunction(val1,val2,s_argBuffer);
#ifdef FULL_CODE_OUTPUT
PAUL_DBGMSG("( return value is %d )",val3);
#endif
push(val3);
break;
default:
PAUL_DBGMSG("ILLEGAL OPCODE@%d ( %d )",m_pc,instruction);
m_state=CRASHED_ILLEGAL_OPCODE;

View file

@ -75,25 +75,28 @@ private:
enum
{
STACK_SIZE=20,
STACK_SIZE=10,
MAX_FUNCTION_ARGS=5,
};
enum
{
// args stack data result
// args stack data result
OP_NOP=0x1100, //
OP_STOP, //
OP_PAUSE,
OP_PUSHVALUE, // value
OP_PUSHVARVALUE, // varidx
OP_JMP, // jump
OP_JMPF, // jump, value
OP_JMPT, // jump, value
OP_IS_EQUAL_VALUE, // value, value pushes result ( 0 or 1 ) to stack
OP_IS_NOTEQUAL_VALUE, // value, value pushes result ( 0 or 1 ) to stack
OP_ASSIGN, // varidx, value
OP_ADD, // value, value pushes result to stack
OP_PRINT, // value
OP_POP, // value
OP_JMP, // jump
OP_JMPF, // jump, value
OP_JMPT, // jump, value
OP_IS_EQUAL_VALUE, // value, value pushes result ( 0 or 1 ) to stack
OP_IS_NOTEQUAL_VALUE, // value, value pushes result ( 0 or 1 ) to stack
OP_ASSIGN, // varidx, value
OP_ADD, // value, value pushes result to stack
OP_PRINT, // value
OP_CALL_FUNCTION, // functionnumber, argcount args pushes return value to stack
};
@ -115,6 +118,8 @@ private:
static signed short s_globalVars[NUM_GLOBAL_VARS];
signed short m_localVars[NUM_LOCAL_VARS];
static unsigned short s_argBuffer[MAX_FUNCTION_ARGS];
ScriptState m_state;
};