Upload
chen
View
36
Download
0
Embed Size (px)
DESCRIPTION
ScriptBasic Belső Modulok. Peter Verh á s Február 2002. Tartalom. Modulok általános felépítése Beolvasó, ... , építő modulok részletesen. Ismétlés. ScriptBasic belső modulok READER LEXER SYNTAXER BULDER EXECUTOR. Mi egy modul. Egy forrás fájlban implementált Függvények gyűjteménye - PowerPoint PPT Presentation
Citation preview
S B
ScriptBasic Belső Modulok
Peter VerhásFebruár 2002
S BTartalom
• Modulok általános felépítése• Beolvasó, ... , építő modulok
részletesen
S BIsmétlés
• ScriptBasic belső modulok– READER– LEXER– SYNTAXER– BULDER– EXECUTOR
S BMi egy modul
• Egy forrás fájlban implementált• Függvények gyűjteménye• typedef struct {} objektum adat
definíció• Jól definiált hívási felület és
eredmény
S BObjektum adat, függvények
• Működési paraméterek• Pillanatnyi objektum státusz
reader.c
input
lines read
MODUL
S BModul függvények
• Inicializáló és operációs függvények
• Belső függvények• Eredmény lekérdező függvények
ScriptBasic main()
Reader Lexer Syntax Build
S BTechnikai megjegyzés
• A ScriptBasic C-ben van írva, nem C++• A fejléc fájlok mind a C kód
megjegyzéseiben vannak• A headerer.pl generál *.c *.h
fájlokat• A fejléc fájlokban a függvény prototípus
karbantartás automatizált• A // megjegyzéseket a headerer.pl
szedi ki (nem teszi bele a *.h-ba)• (Létezik headerer.bas is )
S BBeolvasó
• Fájlból, vagy valamilyen más forrásból a memóriába olvassa be a programsorokat
• A beolvasott sorokat tárolja
S BBeolvasó objektum struktúra 1/3
typedef struct _ReadObject { void * (*fpOpenFile)(char *, void *); int (*fpGetCharacter)(void *, void *); void (*fpCloseFile)(void *, void *); void *pFileHandleClass;
void *(*memory_allocating_function)(size_t, void *); void (*memory_releasing_function)(void *, void *); void *pMemorySegment;
ptConfigTree pConfig;
S BBeolvasó objektum struktúra 2/3
char *Buffer; // buffer to read a line long dwBuffer; // size of Buffer in bytes long cBuffer; // number of characters in buffer
pSourceLine Result; // the lines of the file(s) read
// iteration variables pSourceLine CurrentLine; // the current line in the iteration long NextCharacterPosition; // position of next character // to be returned during iteration char fForceFinalNL; // if TRUE then an extra new line is // added to the last line // if it was terminated by EOF
S BBeolvasó objektum struktúra 3/3
pReportFunction report; void * reportptr; // this pointer is passed // to the report function. The caller should set it. int iErrorCounter; unsigned long fErrorFlags;
pImportedFileList pImportList;
char *FirstUNIXline; struct _PreprocObject *pPREP; } ReadObject, *pReadObject;
S BBeolvasó függvények
• reader_InitStructure• reader_ReadLines
• reader_StartIteration• reader_NextCharacter/NextLine• reader_FileName/LineNumber
S BLexikális elemző
• A beolvasótól átveszi a karaktereket, és tokenizál
• Token típusokenum LexemeType { LEX_T_DOUBLE = 1, LEX_T_LONG, LEX_T_STRING, LEX_T_ASYMBOL, LEX_T_NSYMBOL, LEX_T_CHARACTER, LEX_T_SKIP, LEX_T_SKIP_SYMBOL, LEX_T_DUMMY };
S BToken struktúra
typedef struct _Lexeme { enum LexemeType type; // type of the lexeme union { double dValue; // double value long lValue; // long value char *sValue; // string or symbol value } value; long sLen; //length of string or symbol char *szFileName; // where the lexeme is long lLineNumber; // where the lexeme is struct _Lexeme *next; // link to the next lexeme } Lexeme, *pLexeme;
S B Lexikális elemző objektum struktúra 1/4
typedef struct _LexObject { // returns the next character from the input stream int (*pfGetCharacter)(void *);// returns a pointer to the file name that the last// character came from char * (*pfFileName)(void *);// returns the line number of the line that the last// character came from long (*pfLineNumber)(void *); void *pvInput;
void *(*memory_allocating_function)(size_t, void *); void (*memory_releasing_function)(void *, void *); void *pMemorySegment;
S B Lexikális elemző objektum struktúra 2/4
char *SSC; // Start Symbol Character char *SCC; // Symbol Continuation Character char *SFC; // Symbol Final Character char *SStC; // Start String Character char *SKIP; // characters to be skipped // (though they are symbol // terminators and valid in strings) // this is usually space and tab
// Escape replacements. The first character is// the escape character and// for each odd n the nth character is// the replacement for the (n-1)th character char *ESCS; long fFlag; // various options
S B Lexikális elemző objektum struktúra 3/4
pReportFunction report;// this pointer is passed to the report// function. The caller should set it. void *reportptr; int iErrorCounter; unsigned long fErrorFlags;
// should point to a buffer of size char *buffer; long cbBuffer; // this number of bytes
S B Lexikális elemző objektum struktúra 4/4
pLexNASymbol pNASymbols; // Array of non alpha symbols// the longest Non Alpha Symbol// length including the final zchar int cbNASymbolLength;
// Array of tokenizable alpha symbols pLexNASymbol pASymbols;
// Array of command symbols for debug purposes// (used by ex_pprint via lex_SymbolicName) pLexNASymbol pCSymbols; pLexeme pLexResult; // list of tokens pLexeme pLexCurrentLexeme; // the actual lexeme struct _PreprocObject *pPREP; }LexObject, *pLexObject;
S BLexikális elemző függvények
• lex_InitStructure• lex_ReadInput• lex_StartIteration• lex_NextLexeme• lex_Save/RestorePosition• lex_EOF• lex_Type• lex_Double/String/Long/StrLen• Lex_Finish
S BSzintaxis elemző
• A token listát olvassa és• Szintaxis fát generál• A szintaxis fa eNODE-okból áll
– A szintaxis elemzőt nem tárgyaljuk• Nem érdemes, speciális, nem tipikus
– De a generált BASIC belső kódot igen
S BeNODE struktúra
typedef struct _eNODE {
long OpCode; // the code of operation unsigned long NodeId; // the id of the node char *szFileName;// where the lexeme is long lLineNumber;// from which this syntax node is made
union {...}Parameter;
} eNODE,*peNODE;
S BeNODE Paraméter struktúra
typedef struct _eNODE {
... union { // when the node is a command struct {...} CommandArgument; // when the node is an operation struct {...} Arguments; // when the node is a constant struct {...} Constant; // when the node is a variable struct {...} Variable; // when node is a user functions struct {...} UserFunction; }Parameter;
} eNODE,*peNODE;
S BParancs Argumentum
// when the node is a command struct { union { struct _SymbolLABEL *pLabel; struct _eNODE *pNode; struct _eNODE_l *pNodeList; long lLongValue; double dDoubleValue; char *szStringValue; }Argument; long sLen; struct _eNODE *next; }CommandArgument;
S BTovábbi paraméterek
// when the node is an operation struct { struct _eNODE_l *Argument; }Arguments;
// when the node is a constant struct { union { double dValue; long lValue; char *sValue; }Value; long sLen; //the length of the string constant }Constant;
// when the node is a variable struct { unsigned long Serial; }Variable;
// when node is a user functions struct { pSymbolUF pFunction; // pointer to the function struct _eNODE_l *Argument; }UserFunction;
S BeNODE lista
// node listtypedef struct _eNODE_l{ unsigned long NodeId; // the id of the node
char *szFileName;// where the lexeme is long lLineNumber;// from which this syntax node is made
peNODE actualm; struct _eNODE_l *rest; } eNODE_l, *peNODE_l;
S BCímke és változó struktúra
typedef struct _SymbolLABEL { // label for GOTO // and alikes long Serial; // serial value of the label unsigned long node; // where the label is placed (the
node id) } SymbolLABEL, *pSymbolLABEL;
typedef struct _SymbolVAR { //Variable long Serial; // serial number of the variable } SymbolVAR, *pSymbolVAR;
S BSzintaxis elemzés példa
a$ = 23 + "5.5" & "E1"
2 OpCode=eNTYPE_GVR
Serial=1
1 OpCode=560 LET
Argumentnext
11actualm
10actualm
rest
5 OpCode=+
Argument
9 OpCode= &
Argument
12 OpCode=eNTYPE_STR
sValue=„E1”
6
actualm
rest
Argument
4 OpCode=eNTYPE_LNG
lValue=23
8 OpCode=eNTYPE_STR
sValue=„5.5”
13actualm
rest
rest
7
actualm
rest
3 OpCode= EX_LEX_EXP
Argumentnext
S BSzintaxis elemző függvények
• ex_init• ex_Command_l
S BÉpítő (builder)
• A szintaxis elemző pointer fáját alakítja át tömbbé
• eNODE cNODE
S BcNODE struktúra
typedef struct _cNODE {
long OpCode; // the code of operation
union {...}Parameter;
} cNODE,*pcNODE;
typedef struct _eNODE {
long OpCode; // the code of operation unsigned long NodeId; // the id of the node char *szFileName;// where the lexeme is long lLineNumber;// from which this syntax node is
made
union {...}Parameter;
} eNODE,*peNODE;
S BcNODE Parameter struktúra
union { // when the node is a command struct { ... }CommandArgument; // when the node is an operation struct { ... }Arguments; // when the node is a constant union { ... }Constant; // when the node is a variable struct { ... }Variable; // when node is a user functions struct { ... }UserFunction; // when the node is a node list
head struct { ... }NodeList; }Parameter;
eNODE union { // when the node is a command struct {...} CommandArgument; // when the node is an operation struct {...} Arguments; // when the node is a constant struct {...} Constant; // when the node is a variable struct {...} Variable; // when node is a user functions struct {...} UserFunction; }Parameter;
S B cNODE Parancs Argumentum struktúra
// when the node is a command struct { unsigned long next; union { unsigned long pNode; // node id of the node long lLongValue; double dDoubleValue;// serial value of the string from the string table unsigned long szStringValue; }Argument; }CommandArgument;
S B Építő modul függvények
build_Build
build_SaveCode
build_LoadCode
build_SaveCCode
build_LookupFunctionByName
build_LookupVariableByName
S B
Köszönöm a figyelmet.