2000-12-08 21:42:35 +01:00
|
|
|
/*=========================================================================
|
|
|
|
|
|
|
|
func.h
|
|
|
|
|
|
|
|
Author: PKG
|
|
|
|
Created:
|
|
|
|
Project: Spongebob
|
|
|
|
Purpose:
|
|
|
|
|
|
|
|
Copyright (c) 2000 Climax Development Ltd
|
|
|
|
|
|
|
|
===========================================================================*/
|
|
|
|
|
|
|
|
#ifndef __CODEGEN_H__
|
|
|
|
#define __CODEGEN_H__
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Includes
|
|
|
|
-------- */
|
|
|
|
|
|
|
|
/* Std Lib
|
|
|
|
------- */
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Tyepdefs && Defines
|
|
|
|
------------------- */
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
// Statements
|
|
|
|
STMT_LIST, // list of statements [left-part, right-part] (left-part may be another list)
|
|
|
|
EMPTY_STMT, // empty statement
|
|
|
|
PRINT_STMT, // print [variable]
|
|
|
|
STOP_STMT, // stop
|
|
|
|
PAUSE_STMT, // pause
|
|
|
|
IF_STMT, // if [expression, ifcode]
|
|
|
|
IFELSE_STMT, // if [expression, ifcode, elsecode]
|
2000-12-20 21:15:24 +01:00
|
|
|
WHILE_STMT, // while [expression, code]
|
|
|
|
DOWHILE_STMT, // do [code] while [expression]
|
2000-12-15 21:29:33 +01:00
|
|
|
POP_STMT, // pop
|
2000-12-08 21:42:35 +01:00
|
|
|
|
|
|
|
// Expressions
|
|
|
|
ASSIGN_EXPR, // assignment [variable, value]
|
|
|
|
EQUAL_EXPR, // == [variable, value]
|
|
|
|
NOTEQUAL_EXPR, // != [variable, value]
|
2000-12-20 21:15:24 +01:00
|
|
|
LESSTHAN_EXPR, // < [variable, value]
|
|
|
|
GREATERTHAN_EXPR, // > [variable, value]
|
2000-12-08 21:42:35 +01:00
|
|
|
VARIABLE_EXPR, // variable
|
|
|
|
VALUE_EXPR, // value
|
|
|
|
PLUS_EXPR, // + [value, value]
|
2000-12-20 21:15:24 +01:00
|
|
|
SUBTRACT_EXPR, // - [value, value]
|
2000-12-22 18:10:46 +01:00
|
|
|
MULTIPLY_EXPR, // * [value, value]
|
|
|
|
DIVIDE_EXPR, // / [value, value]
|
2000-12-15 21:29:33 +01:00
|
|
|
FUNCTION_EXPR, // function [functionNumber]
|
2000-12-08 21:42:35 +01:00
|
|
|
}NodeType;
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Structure defintions
|
|
|
|
-------------------- */
|
|
|
|
|
|
|
|
class CTreeNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CTreeNode(NodeType _type);
|
|
|
|
CTreeNode(NodeType _type,CTreeNode *child1);
|
|
|
|
CTreeNode(NodeType _type,CTreeNode *child1,CTreeNode *child2);
|
|
|
|
CTreeNode(NodeType _type,CTreeNode *child1,CTreeNode *child2,CTreeNode *child3);
|
|
|
|
CTreeNode(NodeType _type,int _data);
|
|
|
|
|
|
|
|
int generateCode(int _write);
|
|
|
|
|
|
|
|
int getVariableIdx() {return m_variableIdx;}
|
|
|
|
int getValue() {return m_value;}
|
|
|
|
int getType() {return m_type;}
|
2000-12-15 21:29:33 +01:00
|
|
|
int getFunctionNumber() {return m_functionNumber;}
|
2000-12-08 21:42:35 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// Nothing else needs to know these so we may as well protect them
|
|
|
|
enum
|
|
|
|
{
|
2000-12-15 21:29:33 +01:00
|
|
|
// args stack data result
|
2000-12-08 21:42:35 +01:00
|
|
|
OP_NOP=0x1100, //
|
|
|
|
OP_STOP, //
|
|
|
|
OP_PAUSE, //
|
|
|
|
OP_PUSHVALUE, // value
|
|
|
|
OP_PUSHVARVALUE, // varidx
|
2000-12-15 21:29:33 +01:00
|
|
|
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
|
2000-12-20 21:15:24 +01:00
|
|
|
OP_IS_LESSTHAN_VALUE, // value, value pushes result ( 0 or 1 ) to stack
|
|
|
|
OP_IS_GREATERTHAN_VALUE,// value, value pushes result ( 0 or 1 ) to stack
|
2000-12-15 21:29:33 +01:00
|
|
|
OP_ASSIGN, // varidx, value
|
|
|
|
OP_ADD, // value, value pushes result to stack
|
2000-12-22 18:10:46 +01:00
|
|
|
OP_MULTIPLY, // value, value pushes result to stack
|
|
|
|
OP_DIVIDE, // value, value pushes result to stack
|
2000-12-20 21:15:24 +01:00
|
|
|
OP_NEG, // value pushes result to stack
|
2000-12-15 21:29:33 +01:00
|
|
|
OP_PRINT, // value
|
|
|
|
OP_CALL_FUNCTION, // functionNumber, argcount args pushes return value to stack
|
2000-12-08 21:42:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum{MAX_CHILD_NODES=3};
|
|
|
|
|
|
|
|
|
|
|
|
int emit(unsigned short _data,int _write);
|
|
|
|
int emitValue(CTreeNode *_child,int _write);
|
|
|
|
|
|
|
|
NodeType m_type;
|
|
|
|
CTreeNode *m_children[MAX_CHILD_NODES];
|
|
|
|
int m_numChildren;
|
2000-12-15 21:29:33 +01:00
|
|
|
int m_functionNumber;
|
2000-12-08 21:42:35 +01:00
|
|
|
|
|
|
|
signed short m_variableIdx;
|
|
|
|
signed short m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Globals
|
|
|
|
------- */
|
|
|
|
|
|
|
|
extern CTreeNode *s_baseTreeNode;
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Functions
|
|
|
|
--------- */
|
|
|
|
|
2000-12-11 21:28:41 +01:00
|
|
|
extern int parseFile(char *_filename,class CTreeNode *_baseNode);
|
|
|
|
|
2000-12-08 21:42:35 +01:00
|
|
|
extern int openOutputFile(char *_filename);
|
|
|
|
extern int closeOutputFile();
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#endif /* __CODEGEN_H__ */
|
|
|
|
|
|
|
|
/*===========================================================================
|
|
|
|
end */
|