bool IDockScreenDisplay::EvalBool (const CString &sCode, bool *retbResult, CString *retsError) // EvalBool // // Evaluates the given string { CCodeChainCtx Ctx; Ctx.SetScreen(m_pDockScreen); Ctx.SaveAndDefineSourceVar(m_pLocation); Ctx.SaveAndDefineDataVar(m_pData); char *pPos = sCode.GetPointer(); ICCItem *pExp = Ctx.Link(sCode, 1, NULL); ICCItem *pResult = Ctx.Run(pExp); // LATER:Event Ctx.Discard(pExp); if (pResult->IsError()) { *retsError = pResult->GetStringValue(); Ctx.Discard(pResult); return false; } *retbResult = !pResult->IsNil(); Ctx.Discard(pResult); return true; }
ALERROR CExtension::LoadGlobalsElement (SDesignLoadCtx &Ctx, CXMLElement *pDesc) // LoadGlobalsElement // // Loads <Globals> { CCodeChainCtx CCCtx; // Parse the code and keep it ICCItem *pCode = CCCtx.Link(pDesc->GetContentText(0), 0, NULL); if (pCode->IsError()) { Ctx.sError = strPatternSubst(CONSTLIT("%s globals: %s"), Ctx.sErrorFilespec, pCode->GetStringValue()); return ERR_FAIL; } SGlobalsEntry *pEntry = m_Globals.Insert(); pEntry->pCode = pCode; pEntry->sFilespec = Ctx.sErrorFilespec; #ifdef DEBUG_GLOBALS ::kernelDebugLogMessage("Loading globals in %s", Ctx.sErrorFilespec); #endif return NOERROR; }
CSpaceObject *IDockScreenDisplay::EvalListSource (const CString &sString, CString *retsError) // EvalListSource // // Returns the object from which we should display items { char *pPos = sString.GetPointer(); // See if we need to evaluate if (*pPos == '=') { CCodeChainCtx Ctx; Ctx.SetScreen(this); Ctx.SaveAndDefineSourceVar(m_pLocation); Ctx.SaveAndDefineDataVar(m_pData); ICCItem *pExp = Ctx.Link(sString, 1, NULL); ICCItem *pResult = Ctx.Run(pExp); // LATER:Event Ctx.Discard(pExp); if (pResult->IsError()) { *retsError = pResult->GetStringValue(); Ctx.Discard(pResult); return NULL; } // Convert to an object pointer CSpaceObject *pSource; if (strEquals(pResult->GetStringValue(), DATA_FROM_PLAYER)) pSource = m_pPlayer->GetShip(); else if (strEquals(pResult->GetStringValue(), DATA_FROM_STATION) || strEquals(pResult->GetStringValue(), DATA_FROM_SOURCE)) pSource = m_pLocation; else pSource = Ctx.AsSpaceObject(pResult); Ctx.Discard(pResult); return pSource; } // Otherwise, compare to constants else if (strEquals(sString, DATA_FROM_PLAYER)) return m_pPlayer->GetShip(); else return m_pLocation; }
ALERROR CLanguageDataBlock::InitFromXML (SDesignLoadCtx &Ctx, CXMLElement *pDesc) // InitFromXML // // Initializes from an XML block { int i; for (i = 0; i < pDesc->GetContentElementCount(); i++) { CXMLElement *pItem = pDesc->GetContentElement(i); CString sID = pItem->GetAttribute(ID_ATTRIB); if (sID.IsBlank()) { Ctx.sError = strPatternSubst(CONSTLIT("Invalid id in <Language> block")); return ERR_FAIL; } if (strEquals(pItem->GetTag(), TEXT_TAG)) { // Link the code CCodeChainCtx CCCtx; ICCItem *pCode = CCCtx.Link(pItem->GetContentText(0), 0, NULL); if (pCode->IsError()) { Ctx.sError = strPatternSubst(CONSTLIT("Language id: %s : %s"), sID, pCode->GetStringValue()); return ERR_FAIL; } // Add an entry bool bIsNew; SEntry *pEntry = m_Data.SetAt(sID, &bIsNew); if (!bIsNew) { Ctx.sError = strPatternSubst(CONSTLIT("Duplicate <Language> element: %s"), sID); return ERR_FAIL; } // If pCode is a string and not an identifier, then we can just // store it directly. if (pCode->IsIdentifier() && pCode->IsQuoted()) { pEntry->pCode = NULL; pEntry->sText = pCode->GetStringValue(); } // Otherwise we store the code else pEntry->pCode = pCode->Reference(); // Done CCCtx.Discard(pCode); } else if (strEquals(pItem->GetTag(), MESSAGE_TAG)) { // Add an entry bool bIsNew; SEntry *pEntry = m_Data.SetAt(sID, &bIsNew); if (!bIsNew) { Ctx.sError = strPatternSubst(CONSTLIT("Duplicate <Language> element: %s"), sID); return ERR_FAIL; } // Set the text pEntry->pCode = NULL; pEntry->sText = pItem->GetAttribute(TEXT_ATTRIB); } else { Ctx.sError = strPatternSubst(CONSTLIT("Invalid element in <Language> block: <%s>"), pItem->GetTag()); return ERR_FAIL; } } return NOERROR; }
void Run (CUniverse &Universe, CXMLElement *pCmdLine) { ALERROR error; int i; CCodeChain &CC = g_pUniverse->GetCC(); bool bNoLogo = pCmdLine->GetAttributeBool(NO_LOGO_SWITCH); // Prepare the universe CTopologyNode *pNode = g_pUniverse->GetFirstTopologyNode(); if (pNode == NULL) { printf("ERROR: No topology node found.\n"); return; } CSystem *pSystem; if (error = g_pUniverse->CreateStarSystem(pNode, &pSystem)) { printf("ERROR: Unable to create star system.\n"); return; } // Set the POV CSpaceObject *pPOV = pSystem->GetObject(0); g_pUniverse->SetPOV(pPOV); pSystem->SetPOVLRS(pPOV); // Prepare system g_pUniverse->UpdateExtended(); g_pUniverse->GarbageCollectLibraryBitmaps(); CString sCommand = pCmdLine->GetAttribute(RUN_SWITCH); CString sRunFile = pCmdLine->GetAttribute(RUN_FILE_SWITCH); // If this is a run file, then we parse it and run it if (!sRunFile.IsBlank() && !strEquals(sRunFile, CONSTLIT("true"))) { TArray<CString> Files; ParseStringList(sRunFile, PSL_FLAG_ALLOW_WHITESPACE, &Files); for (i = 0; i < Files.GetCount(); i++) RunFile(Files[i], bNoLogo); } // If we have a command, invoke it else if (!sCommand.IsBlank() && !strEquals(sCommand, CONSTLIT("True"))) { CCodeChainCtx Ctx; ICCItem *pCode = Ctx.Link(sCommand, 0, NULL); ICCItem *pResult = Ctx.Run(pCode); CString sOutput; if (pResult->IsIdentifier()) sOutput = pResult->Print(&CC, PRFLAG_NO_QUOTES | PRFLAG_ENCODE_FOR_DISPLAY); else sOutput = CC.Unlink(pResult); Ctx.Discard(pResult); Ctx.Discard(pCode); // Output result printf("%s\n", sOutput.GetASCIIZPointer()); } // Otherwise, we enter a command loop else { // Welcome if (!bNoLogo) { printf("(help) for function help.\n"); printf("\\q to quit.\n\n"); } // Loop while (true) { char szBuffer[1024]; if (!bNoLogo) printf(": "); gets_s(szBuffer, sizeof(szBuffer)-1); CString sCommand(szBuffer); // Escape codes if (*sCommand.GetASCIIZPointer() == '\\') { // Quit command if (strStartsWith(sCommand, CONSTLIT("\\q"))) break; else if (strStartsWith(sCommand, CONSTLIT("\\?")) || strStartsWith(sCommand, CONSTLIT("\\h"))) { printf("TLisp Shell\n\n"); printf("\\h Show this help\n"); printf("\\q Quit\n"); printf("\n(help) for function help.\n"); } } // Null command else if (sCommand.IsBlank()) NULL; // Command else { CCodeChainCtx Ctx; ICCItem *pCode = Ctx.Link(sCommand, 0, NULL); ICCItem *pResult = Ctx.Run(pCode); CString sOutput; if (pResult->IsIdentifier()) sOutput = pResult->Print(&CC, PRFLAG_NO_QUOTES | PRFLAG_ENCODE_FOR_DISPLAY); else sOutput = CC.Unlink(pResult); Ctx.Discard(pResult); Ctx.Discard(pCode); // Output result printf("%s\n", sOutput.GetASCIIZPointer()); } } } }
void RunFile (const CString &sFilespec, bool bNoLogo) { ALERROR error; CCodeChain &CC = g_pUniverse->GetCC(); CCodeChainCtx Ctx; // Verify the file CString sRunFile = sFilespec; if (!strEndsWith(sRunFile, CONSTLIT(".")) && pathGetExtension(sRunFile).IsBlank()) sRunFile.Append(CONSTLIT(".tlisp")); // Open the file CFileReadBlock InputFile(sRunFile); if (error = InputFile.Open()) { printf("error : Unable to open file '%s'.\n", sRunFile.GetASCIIZPointer()); return; } if (!bNoLogo) printf("%s\n", sRunFile.GetASCIIZPointer()); // Parse CString sInputFile(InputFile.GetPointer(0), InputFile.GetLength(), TRUE); CString sOutput; int iOffset = 0; while (true) { int iCharCount; ICCItem *pCode = Ctx.Link(sInputFile, iOffset, &iCharCount); if (pCode->IsNil()) break; else if (pCode->IsError()) { printf("error : %s\n", pCode->GetStringValue().GetASCIIZPointer()); Ctx.Discard(pCode); return; } iOffset += iCharCount; // Execute ICCItem *pResult = Ctx.Run(pCode); // Compose output if (pResult->IsIdentifier()) sOutput = pResult->Print(&CC, PRFLAG_NO_QUOTES | PRFLAG_ENCODE_FOR_DISPLAY); else sOutput = CC.Unlink(pResult); // Free Ctx.Discard(pResult); Ctx.Discard(pCode); } // Output result printf("%s\n", sOutput.GetASCIIZPointer()); }
void CCommandLineDisplay::OnKeyDown (int iVirtKey, DWORD dwKeyState) // OnKeyDown // // Handle WM_KEYDOWN { switch (iVirtKey) { case VK_BACK: InputBackspace(); break; case VK_DELETE: InputDelete(); break; case VK_RETURN: { CString sInput = GetInput(); if (!sInput.IsBlank()) { CCodeChain &CC = g_pUniverse->GetCC(); InputEnter(); CCodeChainCtx Ctx; ICCItem *pCode = Ctx.Link(sInput, 0, NULL); ICCItem *pResult = Ctx.Run(pCode); CString sOutput; if (pResult->IsIdentifier()) sOutput = pResult->Print(&CC, PRFLAG_NO_QUOTES | PRFLAG_ENCODE_FOR_DISPLAY); else sOutput = CC.Unlink(pResult); Ctx.Discard(pResult); Ctx.Discard(pCode); Output(sOutput); } break; } case VK_TAB: { AutoCompleteSearch(); break; } case VK_UP: InputHistoryUp(); break; case VK_DOWN: InputHistoryDown(); break; case VK_LEFT: if (m_iCursorPos > 0) m_iCursorPos--; m_bInvalid = true; break; case VK_RIGHT: if (m_iCursorPos < m_sInput.GetLength()) m_iCursorPos++; m_bInvalid = true; break; case VK_HOME: m_iCursorPos = 0; m_bInvalid = true; break; case VK_END: m_iCursorPos = m_sInput.GetLength(); m_bInvalid = true; break; case VK_PRIOR: if (m_iScrollPos < 2*MAX_LINES) m_iScrollPos++; m_bInvalid = true; break; case VK_NEXT: if (m_iScrollPos > 0) m_iScrollPos--; m_bInvalid = true; break; } }