int STR_Find(char *name) { int i; for(i = 0; i < str_StringCount; i++) { if(strcmp(StringInfo[i].name, name) == 0) { return i; } } // Add to list if(str_StringCount == MAX_STRINGS) { ERR_Exit(ERR_TOO_MANY_STRINGS, YES, "Current maximum: %d", MAX_STRINGS); } MS_Message(MSG_DEBUG, "Adding string %d:\n \"%s\"\n", str_StringCount, name); StringInfo[str_StringCount].name = MS_Alloc(strlen(name)+1, ERR_OUT_OF_MEMORY); strcpy(StringInfo[str_StringCount].name, name); str_StringCount++; return str_StringCount-1; }
static void OpenDebugFile(char *name) { if((acs_DebugFile = fopen(name, "w")) == NULL) { ERR_Exit(ERR_CANT_OPEN_DBGFILE, NO, "File: \"%s\".", name); } }
static void Write(void *buffer, size_t size, int address) { if(address+size > BufferSize) { ERR_Exit(ERR_PCODE_BUFFER_OVERFLOW, NO, NULL); } memcpy(pc_Buffer+address, buffer, size); }
static void Skip(size_t size) { if(pc_Address+size > BufferSize) { ERR_Exit(ERR_PCODE_BUFFER_OVERFLOW, NO, NULL); } pc_BufferPtr += size; pc_Address += size; }
static void Append(void *buffer, size_t size) { if(pc_Address+size > BufferSize) { ERR_Exit(ERR_PCODE_BUFFER_OVERFLOW, NO, NULL); } memcpy(pc_BufferPtr, buffer, size); pc_BufferPtr += size; pc_Address += size; }
void PC_AddScript(int number, int argCount) { scriptInfo_t *script; if(pc_ScriptCount == MAX_SCRIPT_COUNT) { ERR_Exit(ERR_TOO_MANY_SCRIPTS, YES, NULL); } script = &ScriptInfo[pc_ScriptCount]; script->number = number; script->address = pc_Address; script->argCount = argCount; pc_ScriptCount++; }
void PC_OpenObject(char *name, size_t size, int flags) { if(ObjectOpened == YES) { PC_CloseObject(); } if(strlen(name) >= MAX_FILE_NAME_LENGTH) { ERR_Exit(ERR_FILE_NAME_TOO_LONG, NO, "File: \"%s\".", name); } strcpy(ObjectName, name); pc_Buffer = MS_Alloc(size, ERR_ALLOC_PCODE_BUFFER); pc_BufferPtr = pc_Buffer; pc_Address = 0; ObjectFlags = flags; BufferSize = size; pc_ScriptCount = 0; ObjectOpened = YES; PC_AppendString("ACS"); PC_SkipLong(); // Script table offset }
void PC_CloseObject(void) { int i; scriptInfo_t *info; MS_Message(MSG_DEBUG, "---- PC_CloseObject ----\n"); STR_WriteStrings(); PC_WriteLong((U_LONG)pc_Address, 4); PC_AppendLong((U_LONG)pc_ScriptCount); for(i = 0; i < pc_ScriptCount; i++) { info = &ScriptInfo[i]; MS_Message(MSG_DEBUG, "Script %d, address = %d, arg count = %d\n", info->number, info->address, info->argCount); PC_AppendLong((U_LONG)info->number); PC_AppendLong((U_LONG)info->address); PC_AppendLong((U_LONG)info->argCount); } STR_WriteList(); if(MS_SaveFile(ObjectName, pc_Buffer, pc_Address) == FALSE) { ERR_Exit(ERR_SAVE_OBJECT_FAILED, NO, NULL); } }
tokenType_t TK_NextToken(void) { enum ImportModes prevMode; boolean validToken; if(AlreadyGot == TRUE) { int t = MasterSourcePos; MasterSourcePos = PrevMasterSourcePos; PrevMasterSourcePos = t; AlreadyGot = FALSE; return tk_Token; } tk_String = TokenStringBuffer; validToken = NO; PrevMasterSourcePos = MasterSourcePos; do { while(Chr == ASCII_SPACE) { NextChr(); } switch(ASCIIToChrCode[(byte)Chr]) { case CHR_EOF: tk_Token = TK_EOF; break; case CHR_LETTER: ProcessLetterToken(); break; case CHR_NUMBER: ProcessNumberToken(); break; case CHR_QUOTE: ProcessQuoteToken(); break; default: ProcessSpecialToken(); break; } if(tk_Token == TK_STARTCOMMENT) { SkipComment(); } else if(tk_Token == TK_CPPCOMMENT) { SkipCPPComment(); } else if((tk_Token == TK_EOF) && (NestDepth > 0)) { if (PopNestedSource(&prevMode)) { ImportMode = prevMode; if(!ExporterFlagged) { ERR_Exit(ERR_EXPORTER_NOT_FLAGGED, YES, NULL); } SY_ClearShared(); } } else { validToken = YES; } } while(validToken == NO); return tk_Token; }
void TK_Include(char *fileName) { char sourceName[MAX_FILE_NAME_LENGTH]; int size, i; nestInfo_t *info; boolean foundfile = FALSE; MS_Message(MSG_DEBUG, "*Including %s\n", fileName); if(NestDepth == MAX_NESTED_SOURCES) { ERR_Exit(ERR_INCL_NESTING_TOO_DEEP, YES, fileName); } info = &OpenFiles[NestDepth++]; info->name = tk_SourceName; info->start = FileStart; info->end = FileEnd; info->position = FilePtr; info->line = tk_Line; info->incLineNumber = IncLineNumber; info->lastChar = Chr; info->imported = NO; // Pascal 30/11/08 // Handle absolute paths if(MS_IsPathAbsolute(fileName)) { #if defined(_WIN32) || defined(__MSDOS__) sourceName[0] = '\0'; if(MS_IsDirectoryDelimiter(fileName[0])) { // The source file is absolute for the drive, but does not // specify a drive. Use the path for the current file to // get the drive letter, if it has one. if(IncludePaths[0][0] != '\0' && IncludePaths[0][1] == ':') { sourceName[0] = IncludePaths[0][0]; sourceName[1] = ':'; sourceName[2] = '\0'; } } strcat(sourceName, fileName); #else strcpy(sourceName, fileName); #endif foundfile = MS_FileExists(sourceName); } else { // Pascal 12/11/08 // Find the file in the include paths for(i = 0; i < NumIncludePaths; i++) { strcpy(sourceName, IncludePaths[i]); strcat(sourceName, fileName); if(MS_FileExists(sourceName)) { foundfile = TRUE; break; } } } if(!foundfile) { ERR_ErrorAt(tk_SourceName, tk_Line); ERR_Exit(ERR_CANT_FIND_INCLUDE, YES, fileName, tk_SourceName, tk_Line); } MS_Message(MSG_DEBUG, "*Include file found at %s\n", sourceName); // Now change the first include path to the file directory SetLocalIncludePath(sourceName); tk_SourceName = AddFileName(sourceName); size = MS_LoadFile(tk_SourceName, &FileStart); FileEnd = FileStart+size; FilePtr = FileStart; tk_Line = 1; IncLineNumber = FALSE; tk_Token = TK_NONE; AlreadyGot = FALSE; BumpMasterSourceLine('x',TRUE); // dummy x NextChr(); }
static void ProcessSpecialToken(void) { char c; c = Chr; NextChr(); switch(c) { case '+': switch(Chr) { case '=': tk_Token = TK_ADDASSIGN; NextChr(); break; case '+': tk_Token = TK_INC; NextChr(); break; default: tk_Token = TK_PLUS; break; } break; case '-': switch(Chr) { case '=': tk_Token = TK_SUBASSIGN; NextChr(); break; case '-': tk_Token = TK_DEC; NextChr(); break; default: tk_Token = TK_MINUS; break; } break; case '*': switch(Chr) { case '=': tk_Token = TK_MULASSIGN; NextChr(); break; case '/': tk_Token = TK_ENDCOMMENT; NextChr(); break; default: tk_Token = TK_ASTERISK; break; } break; case '/': switch(Chr) { case '=': tk_Token = TK_DIVASSIGN; NextChr(); break; case '/': tk_Token = TK_CPPCOMMENT; break; case '*': tk_Token = TK_STARTCOMMENT; NextChr(); break; default: tk_Token = TK_SLASH; break; } break; case '%': if(Chr == '=') { tk_Token = TK_MODASSIGN; NextChr(); } else { tk_Token = TK_PERCENT; } break; case '=': if(Chr == '=') { tk_Token = TK_EQ; NextChr(); } else { tk_Token = TK_ASSIGN; } break; case '<': if(Chr == '=') { tk_Token = TK_LE; NextChr(); } else if(Chr == '<') { NextChr(); if(Chr == '=') { tk_Token = TK_LSASSIGN; NextChr(); } else { tk_Token = TK_LSHIFT; } } else { tk_Token = TK_LT; } break; case '>': if(Chr == '=') { tk_Token = TK_GE; NextChr(); } else if(Chr == '>') { NextChr(); if(Chr == '=') { tk_Token = TK_RSASSIGN; NextChr(); } else { tk_Token = TK_RSHIFT; } } else { tk_Token = TK_GT; } break; case '!': if(Chr == '=') { tk_Token = TK_NE; NextChr(); } else { tk_Token = TK_NOT; } break; case '&': if(Chr == '&') { tk_Token = TK_ANDLOGICAL; NextChr(); } else if(Chr == '=') { tk_Token = TK_ANDASSIGN; NextChr(); } else { tk_Token = TK_ANDBITWISE; } break; case '|': if(Chr == '|') { tk_Token = TK_ORLOGICAL; NextChr(); } else if(Chr == '=') { tk_Token = TK_ORASSIGN; NextChr(); } else { tk_Token = TK_ORBITWISE; } break; case '(': tk_Token = TK_LPAREN; break; case ')': tk_Token = TK_RPAREN; break; case '{': tk_Token = TK_LBRACE; break; case '}': tk_Token = TK_RBRACE; break; case '[': tk_Token = TK_LBRACKET; break; case ']': tk_Token = TK_RBRACKET; break; case ':': tk_Token = TK_COLON; break; case ';': tk_Token = TK_SEMICOLON; break; case ',': tk_Token = TK_COMMA; break; case '.': tk_Token = TK_PERIOD; break; case '#': tk_Token = TK_NUMBERSIGN; break; case '^': if(Chr == '=') { tk_Token = TK_EORASSIGN; NextChr(); } else { tk_Token = TK_EORBITWISE; } break; case '~': tk_Token = TK_TILDE; break; case '\'': if(Chr == '\\') { NextChr(); switch(Chr) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': tk_Number = OctalChar(); break; case 'x': case 'X': NextChr(); EvalHexConstant(); if(Chr != '\'') { ERR_Exit(ERR_BAD_CHARACTER_CONSTANT, YES, NULL); } NextChr(); break; case 'a': tk_Number = '\a'; break; case 'b': tk_Number = '\b'; break; case 't': tk_Number = '\t'; break; case 'v': tk_Number = '\v'; break; case 'n': tk_Number = '\n'; break; case 'f': tk_Number = '\f'; break; case 'r': tk_Number = '\r'; break; case '\'': case '\\': tk_Number = Chr; break; default: ERR_Exit(ERR_BAD_CHARACTER_CONSTANT, YES, NULL); } tk_Token = TK_NUMBER; } else if(Chr == '\'') { ERR_Exit(ERR_BAD_CHARACTER_CONSTANT, YES, NULL); } else { tk_Number = Chr; tk_Token = TK_NUMBER; } NextChr(); if(Chr != '\'') { ERR_Exit(ERR_BAD_CHARACTER_CONSTANT, YES, NULL); } NextChr(); break; default: ERR_Exit(ERR_BAD_CHARACTER, YES, NULL); break; } }