home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
POLYEDIT.LZH
/
ML
/
CODE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-02-15
|
4KB
|
215 lines
/*
* 中間コード制御
*
* T.Kobayashi 1993.8.21
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "code.h"
#include "alloc.h"
#include "err.h"
#include "inlib.h"
static char *CodeBuf ;
static char *NextPtr ;
static int CodeSize ;
extern int ParseDebug ;
/* コードバッファの初期化 */
void CodeInit( size )
int size ;
{
CodeBuf = (char*)MemoryAlloc( size );
if ( CodeBuf == NULL )
{
ParseFatal( "内部コード用バッファが確保できません。!!" );
}
CodeSize = size ;
NextPtr = CodeBuf ;
}
/* コードバッファの解放 */
void CodeExit()
{
if ( CodeBuf != NULL )
MemoryFree( CodeBuf );
}
/* 整数を登録 */
CodeConst *CodeSetInt( i )
int i ;
{
CodeConst *buf ;
#if PDEBUG
if ( ParseDebug )
printf( " int\t%d\n", i );
#endif
buf = CodeAlloc( sizeof( CodeConst ) );
buf->type = CODE_CONST ;
buf->data.id.type = TYPE_INT ;
buf->data.id.i = i ;
return buf ;
}
/* 定数を登録 */
CodeConst *CodeSetConst( c )
DataStruct *c ;
{
CodeConst *buf ;
#if PDEBUG
if ( ParseDebug )
{
printf( " const " );
DataPrint( c );
printf( "\n" );
}
#endif
buf = CodeAlloc( sizeof( CodeConst ) );
buf->type = CODE_CONST ;
buf->data = *c ;
return buf ;
}
/* 演算子を登録 */
CodeOperator *CodeSetOperator( ope )
int ope ;
{
CodeOperator *buf ;
#if PDEBUG
if ( ParseDebug )
printf( " ope\t%d\n", ope );
#endif
buf = CodeAlloc( sizeof( CodeOperator ) );
buf->type = CODE_OPERATOR ;
buf->otype = ope ;
return buf ;
}
/* 文を登録 */
CodeSentense *CodeSetSentense( file, line )
char *file ;
int line ;
{
CodeSentense *buf ;
#if PDEBUG
if ( ParseDebug )
printf( " sent\n" );
#endif
buf = CodeAlloc( sizeof( CodeSentense ) );
buf->type = CODE_SENTENSE ;
buf->stype = -1 ;
buf->file = file ;
buf->line = line ;
buf->next = NULL ;
return buf ;
}
/* 識別子を登録 */
CodeIdent *CodeSetIdent( type, ident )
int type ;
int ident ;
{
CodeIdent *buf ;
#if PDEBUG
if ( ParseDebug )
printf( " ident\t%d\t%d\n", type, ident );
#endif
buf = CodeAlloc( sizeof( CodeIdent ) );
buf->type = CODE_IDENT ;
buf->itype = type ;
buf->ident = ident ;
return buf ;
}
/* 配列を登録 */
CodeArray *CodeSetArray( size )
int size ;
{
CodeArray *buf ;
#if PDEBUG
if ( ParseDebug )
printf( " array\t%d\n", size );
#endif
buf = CodeAlloc( sizeof( CodeArray ) );
buf->type = CODE_ARRAY ;
buf->asize = size ;
return buf ;
}
/* 登録 */
void *CodeAlloc( size )
int size ;
{
CodeStruct *buf ;
if ( NextPtr + size > CodeBuf + CodeSize )
{
ParseFatal( "内部コード用バッファがオーバフローしました。!!" );
return NULL ;
}
else
{
buf = (CodeStruct*)NextPtr ;
buf->com.size = size ;
NextPtr += size ;
return (void*)buf ;
}
}
/* 最初のポインタ */
CodeStruct *CodeTop()
{
return (CodeStruct*)CodeBuf ;
}
/* 次に登録するポインタ */
CodeStruct *CodeNextPtr()
{
return (CodeStruct*)NextPtr ;
}
/* 次のコードへ */
CodeStruct *CodeNext( code )
CodeStruct *code ;
{
assert( code != NULL );
code = (CodeStruct*)( (char*)code + code->com.size );
if ( (char*)code >= NextPtr )
return NULL ;
else
return code ;
}
/* 次の文へ */
CodeStruct *CodeNextSentense( code )
CodeStruct *code ;
{
assert( code->type & CODE_SENTENSE );
code = code->sent.next ;
if ( (char*)code >= NextPtr )
return NULL ;
else
return code ;
}