Replace ctype module with char module providing static Char class
Build / AerScript (push) Failing after 9s

This commit is contained in:
2026-11-23 21:15:16 +01:00
parent bb9cc8efc8
commit b2bf4dde5b
5 changed files with 505 additions and 566 deletions
+1
View File
@@ -689,6 +689,7 @@ PH7_APIEXPORT void ph7_context_release_value(ph7_context *pCtx, ph7_value *pValu
PH7_APIEXPORT int ph7_value_int(ph7_value *pVal, int iValue); PH7_APIEXPORT int ph7_value_int(ph7_value *pVal, int iValue);
PH7_APIEXPORT int ph7_value_int64(ph7_value *pVal, ph7_int64 iValue); PH7_APIEXPORT int ph7_value_int64(ph7_value *pVal, ph7_int64 iValue);
PH7_APIEXPORT int ph7_value_bool(ph7_value *pVal, int iBool); PH7_APIEXPORT int ph7_value_bool(ph7_value *pVal, int iBool);
PH7_APIEXPORT int ph7_value_char(ph7_value *pVal, int cValue);
PH7_APIEXPORT int ph7_value_double(ph7_value *pVal, double Value); PH7_APIEXPORT int ph7_value_double(ph7_value *pVal, double Value);
PH7_APIEXPORT int ph7_value_string(ph7_value *pVal, const char *zString, int nLen); PH7_APIEXPORT int ph7_value_string(ph7_value *pVal, const char *zString, int nLen);
PH7_APIEXPORT int ph7_value_string_format(ph7_value *pVal, const char *zFormat, ...); PH7_APIEXPORT int ph7_value_string_format(ph7_value *pVal, const char *zFormat, ...);
+481
View File
@@ -0,0 +1,481 @@
/**
* @PROJECT AerScript Interpreter
* @COPYRIGHT See COPYING in the top level directory
* @FILE modules/char/char.c
* @DESCRIPTION Static Char class module for AerScript Interpreter (0..255 contract)
* @DEVELOPERS Rafal Kupiec <belliash@codingworkshop.eu.org>
*/
#include <string.h>
#include "char.h"
#define CCLS_CHAR "Char"
#define CCLS_EXC "CharException"
#define CTY(T) { PH7_TYPE_##T, 0, 0 }
/* The engine's language char is a 64-bit integer (MEMOBJ_CHAR, x.iVal is an
* sxi64), not a byte. PH7_MemObjToChar() (engine/memobj.c:612-620) stores the
* integer unchanged: (char)128/233/255 give 128/233/255, but (char)256 gives
* 256 and (char)-1 gives -1 (no clamp); ph7_value_to_int() shows only a 32-bit
* view. The engine's string-to-char path is signed (engine/memobj.c:368,
* (int)zString[0]), so (char)chr(233) is -23; that path is OUT_OF_SCOPE here.
* This scripted Char class adds its own contract mask on top: CharOperand
* accepts only a char in 0..255 or a one-byte string mapped as an unsigned
* byte, and rejects values outside that range. It does not change the engine
* representation. Classifiers return FALSE for bytes >= 0x80. */
typedef int (*CharPred)(ph7_int64);
static const char *CharErrorDescription(int nErr) {
switch(nErr) {
case CHAR_ERROR_NONE: return "No error";
case CHAR_ERROR_ARGUMENT: return "Invalid argument";
default: return "Unknown error code";
}
}
/* ------------------------------------------------------------------ */
/* CharException */
/* ------------------------------------------------------------------ */
static ph7_value *CharInstField(ph7_class_instance *pInst, const char *zName) {
SyString s;
if(pInst == 0) {
return 0;
}
SyStringInitFromBuf(&s, zName, (sxu32)SyStrlen(zName));
return PH7_ClassInstanceFetchAttr(pInst, &s);
}
/* Stamp the inherited Exception file/line with the first script frame. */
static void CharStampSite(ph7_vm *pVm, ph7_class_instance *pInst) {
SySet sTrace;
VmDebugTrace *pTrace;
ph7_value *pField;
if(VmExtractDebugTrace(pVm, &sTrace) != SXRET_OK) {
return;
}
while(SySetGetNextEntry(&sTrace, (void **)&pTrace) == SXRET_OK) {
if(pTrace->pFile == 0 || pTrace->nLine == 0) {
continue;
}
if(pTrace->pFile->nByte == sizeof("[NATIVE]") - 1 &&
SyMemcmp(pTrace->pFile->zString, "[NATIVE]", sizeof("[NATIVE]") - 1) == 0) {
continue;
}
pField = CharInstField(pInst, "file");
if(pField) {
PH7_MemObjRelease(pField);
PH7_MemObjInit(pVm, pField);
(void)ph7_value_string(pField, pTrace->pFile->zString, (int)pTrace->pFile->nByte);
}
pField = CharInstField(pInst, "line");
if(pField) {
(void)ph7_value_int64(pField, (ph7_int64)pTrace->nLine);
}
break;
}
SySetRelease(&sTrace);
}
/* Throw a real CharException instance; every temporary is released on all
* paths and the pending transfer is left for the VM unwinder. */
static int CharThrow(ph7_context *pCtx, int nErr, const char *zMsg) {
ph7_vm *pVm = pCtx->pVm;
ph7_class *pClass;
ph7_class_instance *pInst;
ph7_class_method *pCons;
ph7_value *pObj, *apArg[2], *aTmp[2];
sxi32 rc;
sxu32 n;
if(pVm->iPendingTransfer) {
return PH7_EXCEPTION;
}
pClass = PH7_VmExtractClass(pVm, CCLS_EXC, sizeof(CCLS_EXC) - 1, FALSE);
if(pClass == 0) {
return PH7_ABORT;
}
pInst = PH7_NewClassInstance(pVm, pClass);
if(pInst == 0) {
PH7_VmMemoryError(pVm);
return PH7_ABORT;
}
for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) {
aTmp[n] = ph7_context_new_scalar(pCtx);
if(aTmp[n] == 0) {
while(n > 0) {
--n;
ph7_context_release_value(pCtx, aTmp[n]);
}
PH7_ClassInstanceUnref(pInst);
PH7_VmMemoryError(pVm);
return PH7_ABORT;
}
}
pObj = ph7_context_new_scalar(pCtx);
if(pObj == 0) {
for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) {
ph7_context_release_value(pCtx, aTmp[n]);
}
PH7_ClassInstanceUnref(pInst);
PH7_VmMemoryError(pVm);
return PH7_ABORT;
}
ph7_value_string(aTmp[0], zMsg, -1);
ph7_value_int(aTmp[1], nErr);
apArg[0] = aTmp[0];
apArg[1] = aTmp[1];
pCons = PH7_ClassExtractMethod(pClass, "__construct", sizeof("__construct") - 1);
if(pCons) {
rc = PH7_VmCallClassMethod(pVm, pInst, pCons, 0, 2, apArg);
if(pInst->iRef < 1) {
pInst->iRef = 1;
}
for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) {
ph7_context_release_value(pCtx, aTmp[n]);
}
if(rc != PH7_OK) {
ph7_context_release_value(pCtx, pObj);
PH7_ClassInstanceUnref(pInst);
return rc;
}
} else {
for(n = 0 ; n < SX_ARRAYSIZE(aTmp) ; ++n) {
ph7_context_release_value(pCtx, aTmp[n]);
}
}
CharStampSite(pVm, pInst);
if(pInst->iRef < 1) {
pInst->iRef = 1;
}
PH7_MemObjRelease(pObj);
pObj->x.pOther = pInst;
MemObjSetType(pObj, MEMOBJ_OBJ);
rc = ph7_result_value(pCtx, pObj);
ph7_context_release_value(pCtx, pObj);
return rc == PH7_OK ? PH7_EXCEPTION : rc;
}
static int CharExceptionConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_value *pResult = ph7_context_new_scalar(pCtx);
sxi32 rc;
SXUNUSED(nArg);
if(pResult == 0) {
return PH7_ABORT;
}
/* apArg[1] is always filled by the VM from the registered pDefault. */
rc = ph7_context_parent(pCtx, "__construct", 2, apArg, pResult);
return rc;
}
static int CharExceptionDescription(ph7_context *pCtx, int nArg, ph7_value **apArg) {
int nErr = nArg > 0 ? ph7_value_to_int(apArg[0]) : -1;
return ph7_result_string(pCtx, CharErrorDescription(nErr), -1);
}
/* ------------------------------------------------------------------ */
/* Argument conversion */
/* ------------------------------------------------------------------ */
/* Read a raw MIXED argument as an Aer char: either a char value in 0..255 or
* a string of exactly one byte, interpreted as an unsigned byte. Every other
* type (including NULL, void and int) is rejected with a CharException. */
static int CharOperand(ph7_context *pCtx, ph7_value *pArg, ph7_int64 *pOut) {
const char *zData;
sxu32 nByte;
ph7_int64 iVal;
if(pArg == 0) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Expected a char (0..255) or a single-byte string argument");
}
if(ph7_value_is_char(pArg)) {
iVal = ph7_value_to_int64(pArg);
if(iVal < 0 || iVal > 255) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Char argument is outside the 0..255 range");
}
*pOut = iVal;
return PH7_OK;
}
if(ph7_value_is_string(pArg)) {
nByte = SyBlobLength(&pArg->sBlob);
if(nByte != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Expected a string of exactly one byte");
}
zData = (const char *)SyBlobData(&pArg->sBlob);
*pOut = (ph7_int64)(unsigned char)zData[0];
return PH7_OK;
}
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Expected a char (0..255) or a single-byte string argument");
}
/* Set a full-range char result; ph7_value_char() clamps to 0..255. */
static int CharResultChar(ph7_context *pCtx, ph7_int64 iVal) {
ph7_value *pRet = pCtx->pRet;
(void)ph7_value_char(pRet, (int)iVal);
pCtx->bResultSet = TRUE;
return PH7_OK;
}
/* ------------------------------------------------------------------ */
/* Classification predicates */
/* ------------------------------------------------------------------ */
static int CharAscii(ph7_int64 c) { return c >= 0 && c <= 0x7F; }
static int PredControl(ph7_int64 c) { return CharAscii(c) && ((c >= 0x00 && c <= 0x1F) || c == 0x7F); }
static int PredDigit(ph7_int64 c) { return CharAscii(c) && c >= '0' && c <= '9'; }
static int PredLetter(ph7_int64 c) {
return CharAscii(c) && ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}
static int PredLetterOrDigit(ph7_int64 c) { return PredLetter(c) || PredDigit(c); }
static int PredLower(ph7_int64 c) { return CharAscii(c) && c >= 'a' && c <= 'z'; }
static int PredUpper(ph7_int64 c) { return CharAscii(c) && c >= 'A' && c <= 'Z'; }
static int PredWhiteSpace(ph7_int64 c) { return CharAscii(c) && ((c >= 0x09 && c <= 0x0D) || c == 0x20); }
static int PredSeparator(ph7_int64 c) { return CharAscii(c) && c == 0x20; }
static int PredPunctuation(ph7_int64 c) {
static const char zSet[] = "!\"#%&'()*,-./:;?@[\\]_{}";
return CharAscii(c) && c != 0 && strchr(zSet, (int)c) != 0;
}
static int PredSymbol(ph7_int64 c) {
static const char zSet[] = "$+<=>^`|~";
return CharAscii(c) && c != 0 && strchr(zSet, (int)c) != 0;
}
static int PredGraphical(ph7_int64 c) { return CharAscii(c) && c >= 0x21 && c <= 0x7E; }
static int PredPrintable(ph7_int64 c) { return CharAscii(c) && c >= 0x20 && c <= 0x7E; }
static int PredAsciiDigit(ph7_int64 c) { return PredDigit(c); }
static int PredAsciiLetter(ph7_int64 c) { return PredLetter(c); }
static int PredAsciiLetterLower(ph7_int64 c) { return PredLower(c); }
static int PredAsciiLetterUpper(ph7_int64 c) { return PredUpper(c); }
static int PredAsciiLetterOrDigit(ph7_int64 c) { return PredLetterOrDigit(c); }
static int PredHexLower(ph7_int64 c) { return CharAscii(c) && c >= 'a' && c <= 'f'; }
static int PredHexUpper(ph7_int64 c) { return CharAscii(c) && c >= 'A' && c <= 'F'; }
static int PredHexDigit(ph7_int64 c) { return PredDigit(c) || PredHexLower(c) || PredHexUpper(c); }
static int CharClassify(ph7_context *pCtx, int nArg, ph7_value **apArg, CharPred xPred) {
ph7_int64 c;
if(nArg != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Method expects exactly one argument");
}
if(CharOperand(pCtx, apArg[0], &c) != PH7_OK) {
return PH7_EXCEPTION;
}
return ph7_result_bool(pCtx, xPred(c) ? 1 : 0);
}
#define CLS_FN(NAME, PRED) \
static int NAME(ph7_context *pCtx, int nArg, ph7_value **apArg) { \
return CharClassify(pCtx, nArg, apArg, PRED); \
}
CLS_FN(CharIsControl, PredControl)
CLS_FN(CharIsDigit, PredDigit)
CLS_FN(CharIsNumber, PredDigit)
CLS_FN(CharIsLetter, PredLetter)
CLS_FN(CharIsLetterOrDigit, PredLetterOrDigit)
CLS_FN(CharIsLower, PredLower)
CLS_FN(CharIsUpper, PredUpper)
CLS_FN(CharIsWhiteSpace, PredWhiteSpace)
CLS_FN(CharIsSeparator, PredSeparator)
CLS_FN(CharIsPunctuation, PredPunctuation)
CLS_FN(CharIsSymbol, PredSymbol)
CLS_FN(CharIsGraphical, PredGraphical)
CLS_FN(CharIsPrintable, PredPrintable)
CLS_FN(CharIsAscii, CharAscii)
CLS_FN(CharIsAsciiDigit, PredAsciiDigit)
CLS_FN(CharIsAsciiLetter, PredAsciiLetter)
CLS_FN(CharIsAsciiLetterLower, PredAsciiLetterLower)
CLS_FN(CharIsAsciiLetterUpper, PredAsciiLetterUpper)
CLS_FN(CharIsAsciiLetterOrDigit, PredAsciiLetterOrDigit)
CLS_FN(CharIsAsciiHexDigit, PredHexDigit)
CLS_FN(CharIsAsciiHexDigitLower, PredHexLower)
CLS_FN(CharIsAsciiHexDigitUpper, PredHexUpper)
static int CharIsBetween(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_int64 c, zMin, zMax;
if(nArg != 3) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "IsBetween expects exactly three arguments");
}
if(CharOperand(pCtx, apArg[0], &c) != PH7_OK ||
CharOperand(pCtx, apArg[1], &zMin) != PH7_OK ||
CharOperand(pCtx, apArg[2], &zMax) != PH7_OK) {
return PH7_EXCEPTION;
}
return ph7_result_bool(pCtx,
zMin <= zMax && c >= zMin && c <= zMax ? 1 : 0);
}
/* ------------------------------------------------------------------ */
/* Conversions */
/* ------------------------------------------------------------------ */
static int CharCaseConvert(ph7_context *pCtx, int nArg, ph7_value **apArg, int bUpper) {
ph7_int64 c;
if(nArg != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Method expects exactly one argument");
}
if(CharOperand(pCtx, apArg[0], &c) != PH7_OK) {
return PH7_EXCEPTION;
}
if(bUpper && c >= 'a' && c <= 'z') {
c -= ('a' - 'A');
} else if(!bUpper && c >= 'A' && c <= 'Z') {
c += ('a' - 'A');
}
return CharResultChar(pCtx, c);
}
static int CharToUpper(ph7_context *pCtx, int nArg, ph7_value **apArg) {
return CharCaseConvert(pCtx, nArg, apArg, 1);
}
static int CharToLower(ph7_context *pCtx, int nArg, ph7_value **apArg) {
return CharCaseConvert(pCtx, nArg, apArg, 0);
}
static int CharGetNumericValue(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_int64 c;
if(nArg != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "GetNumericValue expects exactly one argument");
}
if(CharOperand(pCtx, apArg[0], &c) != PH7_OK) {
return PH7_EXCEPTION;
}
return ph7_result_double(pCtx, PredDigit(c) ? (double)(c - '0') : -1.0);
}
static int CharToString(ph7_context *pCtx, int nArg, ph7_value **apArg) {
ph7_int64 c;
char zByte[2];
if(nArg != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "ToString expects exactly one argument");
}
if(CharOperand(pCtx, apArg[0], &c) != PH7_OK) {
return PH7_EXCEPTION;
}
zByte[0] = (char)(unsigned char)(c & 0xFF);
zByte[1] = 0;
return ph7_result_string(pCtx, zByte, 1);
}
static int CharParse(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const char *zData;
sxu32 nByte;
if(nArg != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Parse expects exactly one argument");
}
if(apArg[0] == 0 || !ph7_value_is_string(apArg[0])) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Parse expects a single-byte string");
}
nByte = SyBlobLength(&apArg[0]->sBlob);
if(nByte != 1) {
return CharThrow(pCtx, CHAR_ERROR_ARGUMENT, "Parse expects a single-byte string");
}
zData = (const char *)SyBlobData(&apArg[0]->sBlob);
return CharResultChar(pCtx, (int)(unsigned char)zData[0]);
}
/* ------------------------------------------------------------------ */
/* Class registration */
/* ------------------------------------------------------------------ */
static int CharConstruct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
SXUNUSED(pCtx);
SXUNUSED(nArg);
SXUNUSED(apArg);
return PH7_ABORT; /* final class: no public construction */
}
static sxi32 CharRegisterClasses(ph7_vm *pVm) {
static ph7_constant_def aExcConst[] = {
{ "ERROR_NONE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "ERROR_ARGUMENT", 0, PH7_MEMBER_PUBLIC, 0, 0 }
};
static const int aExcValue[] = { CHAR_ERROR_NONE, CHAR_ERROR_ARGUMENT };
static ph7_constant_def aCharConst[] = {
{ "MIN_VALUE", 0, PH7_MEMBER_PUBLIC, 0, 0 },
{ "MAX_VALUE", 0, PH7_MEMBER_PUBLIC, 0, 0 }
};
static ph7_param_def aExcCtor[] = {
{ "message", CTY(STRING), 0, 0, 0, 0 },
{ "code", CTY(INT), 0, 0, 0, 0 }
};
static const ph7_param_def aExcDesc[] = {
{ "code", CTY(INT), 0, 0, 0, 0 }
};
#define SMV(NAME,RET,FUNC) \
{ NAME, CTY(RET), 0, 0, FUNC, 0, PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC | PH7_MEMBER_VARIADIC }
static const ph7_method_def aExcMethod[] = {
{ "__construct", CTY(VOID), aExcCtor, 2, CharExceptionConstruct, 0, 0 },
{ "GetDescription", CTY(STRING), aExcDesc, 1, CharExceptionDescription, 0,
PH7_MEMBER_PUBLIC | PH7_MEMBER_STATIC }
};
static const ph7_method_def aCharMethod[] = {
{ "__construct", CTY(VOID), 0, 0, CharConstruct, 0, PH7_MEMBER_PRIVATE },
SMV("IsControl", BOOL, CharIsControl),
SMV("IsDigit", BOOL, CharIsDigit),
SMV("IsNumber", BOOL, CharIsNumber),
SMV("IsLetter", BOOL, CharIsLetter),
SMV("IsLetterOrDigit", BOOL, CharIsLetterOrDigit),
SMV("IsLower", BOOL, CharIsLower),
SMV("IsUpper", BOOL, CharIsUpper),
SMV("IsWhiteSpace", BOOL, CharIsWhiteSpace),
SMV("IsSeparator", BOOL, CharIsSeparator),
SMV("IsPunctuation", BOOL, CharIsPunctuation),
SMV("IsSymbol", BOOL, CharIsSymbol),
SMV("IsGraphical", BOOL, CharIsGraphical),
SMV("IsPrintable", BOOL, CharIsPrintable),
SMV("IsAscii", BOOL, CharIsAscii),
SMV("IsAsciiDigit", BOOL, CharIsAsciiDigit),
SMV("IsAsciiLetter", BOOL, CharIsAsciiLetter),
SMV("IsAsciiLetterLower", BOOL, CharIsAsciiLetterLower),
SMV("IsAsciiLetterUpper", BOOL, CharIsAsciiLetterUpper),
SMV("IsAsciiLetterOrDigit", BOOL, CharIsAsciiLetterOrDigit),
SMV("IsAsciiHexDigit", BOOL, CharIsAsciiHexDigit),
SMV("IsAsciiHexDigitLower", BOOL, CharIsAsciiHexDigitLower),
SMV("IsAsciiHexDigitUpper", BOOL, CharIsAsciiHexDigitUpper),
SMV("IsBetween", BOOL, CharIsBetween),
SMV("ToUpper", CHAR, CharToUpper),
SMV("ToLower", CHAR, CharToLower),
SMV("GetNumericValue", FLOAT, CharGetNumericValue),
SMV("ToString", STRING, CharToString),
SMV("Parse", CHAR, CharParse)
};
#undef SMV
static const char *const aExcBase[] = { "Exception" };
ph7_class_def sExc = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, CCLS_EXC,
0, 0, aExcConst, SX_ARRAYSIZE(aExcConst),
aExcMethod, SX_ARRAYSIZE(aExcMethod), 0, aExcBase, 1, 0, 0, 0, 0 };
ph7_class_def sChar = { sizeof(ph7_class_def), PH7_CLASS_DEF_VERSION, CCLS_CHAR,
0, 0, aCharConst, SX_ARRAYSIZE(aCharConst),
aCharMethod, SX_ARRAYSIZE(aCharMethod),
PH7_DECL_FINAL | PH7_DECL_VIRTUAL, 0, 0, 0, 0, 0, 0 };
ph7_value *apExc[SX_ARRAYSIZE(aExcConst)];
ph7_value *apChar[SX_ARRAYSIZE(aCharConst)];
ph7_value *apDefault = 0;
sxu32 n;
sxi32 rc = PH7_OK;
SyZero(apExc, sizeof(apExc));
SyZero(apChar, sizeof(apChar));
for(n = 0 ; n < SX_ARRAYSIZE(apExc) ; ++n) {
apExc[n] = ph7_new_scalar(pVm);
if(apExc[n] == 0) { rc = PH7_NOMEM; goto Cleanup; }
ph7_value_int(apExc[n], aExcValue[n]);
aExcConst[n].pValue = apExc[n];
}
for(n = 0 ; n < SX_ARRAYSIZE(apChar) ; ++n) {
apChar[n] = ph7_new_scalar(pVm);
if(apChar[n] == 0) { rc = PH7_NOMEM; goto Cleanup; }
(void)ph7_value_char(apChar[n], n == 0 ? 0 : 255);
aCharConst[n].pValue = apChar[n];
}
/* CharException("message") gets ERROR_NONE from this snapshot. */
apDefault = ph7_new_scalar(pVm);
if(apDefault == 0) { rc = PH7_NOMEM; goto Cleanup; }
ph7_value_int(apDefault, CHAR_ERROR_NONE);
aExcCtor[1].pDefault = apDefault;
rc = ph7_create_class(pVm, &sExc);
if(rc == PH7_OK) {
rc = ph7_create_class(pVm, &sChar);
}
Cleanup:
aExcCtor[1].pDefault = 0;
if(apDefault) {
ph7_release_value(pVm, apDefault);
}
for(n = 0 ; n < SX_ARRAYSIZE(apExc) ; ++n) {
if(apExc[n]) {
ph7_release_value(pVm, apExc[n]);
aExcConst[n].pValue = 0;
}
}
for(n = 0 ; n < SX_ARRAYSIZE(apChar) ; ++n) {
if(apChar[n]) {
ph7_release_value(pVm, apChar[n]);
aCharConst[n].pValue = 0;
}
}
return rc;
}
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) {
desc->zString = MODULE_DESC;
*ver = MODULE_VER;
return CharRegisterClasses(pVm);
}
+23
View File
@@ -0,0 +1,23 @@
/**
* @PROJECT AerScript Interpreter
* @COPYRIGHT See COPYING in the top level directory
* @FILE modules/char/char.h
* @DESCRIPTION Character class module for AerScript Interpreter
* @DEVELOPERS Rafal Kupiec <belliash@codingworkshop.eu.org>
*/
#ifndef __CHAR_H__
#define __CHAR_H__
#include "ph7.h"
#include "ph7int.h"
#define MODULE_DESC "Char Module"
#define MODULE_VER 1.0
/* CharException error codes. */
#define CHAR_ERROR_NONE 0
#define CHAR_ERROR_ARGUMENT 1
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc);
#endif
-520
View File
@@ -1,520 +0,0 @@
/**
* @PROJECT AerScript Interpreter
* @COPYRIGHT See COPYING in the top level directory
* @FILE modules/ctype/ctype.c
* @DESCRIPTION Character type checking module for AerScript Interpreter
* @DEVELOPERS Symisc Systems <devel@symisc.net>
* Rafal Kupiec <belliash@codingworkshop.eu.org>
*/
#include "ctype.h"
/*
* bool ctype_alnum(string $text)
* Checks if all of the characters in the provided string, text, are alphanumeric.
* Parameters
* $text
* The tested string.
* Return
* TRUE if every character in text is either a letter or a digit, FALSE otherwise.
*/
static int PH7_builtin_ctype_alnum(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const unsigned char *zIn, *zEnd;
int nLen;
if(nArg < 1) {
/* Missing arguments, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Extract the target string */
zIn = (const unsigned char *)ph7_value_to_string(apArg[0], &nLen);
zEnd = &zIn[nLen];
if(nLen < 1) {
/* Empty string, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation */
for(;;) {
if(zIn >= zEnd) {
/* If we reach the end of the string,then the test succeeded. */
ph7_result_bool(pCtx, 1);
return PH7_OK;
}
if(!SyisAlphaNum(zIn[0])) {
break;
}
/* Point to the next character */
zIn++;
}
/* The test failed, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/*
* bool ctype_alpha(string $text)
* Checks if all of the characters in the provided string, text, are alphabetic.
* Parameters
* $text
* The tested string.
* Return
* TRUE if every character in text is a letter from the current locale, FALSE otherwise.
*/
static int PH7_builtin_ctype_alpha(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const unsigned char *zIn, *zEnd;
int nLen;
if(nArg < 1) {
/* Missing arguments, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Extract the target string */
zIn = (const unsigned char *)ph7_value_to_string(apArg[0], &nLen);
zEnd = &zIn[nLen];
if(nLen < 1) {
/* Empty string, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation */
for(;;) {
if(zIn >= zEnd) {
/* If we reach the end of the string,then the test succeeded. */
ph7_result_bool(pCtx, 1);
return PH7_OK;
}
if(!SyisAlpha(zIn[0])) {
break;
}
/* Point to the next character */
zIn++;
}
/* The test failed, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/*
* bool ctype_cntrl(string $text)
* Checks if all of the characters in the provided string, text, are control characters.
* Parameters
* $text
* The tested string.
* Return
* TRUE if every character in text is a control characters,FALSE otherwise.
*/
static int PH7_builtin_ctype_cntrl(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const unsigned char *zIn, *zEnd;
int nLen;
if(nArg < 1) {
/* Missing arguments, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Extract the target string */
zIn = (const unsigned char *)ph7_value_to_string(apArg[0], &nLen);
zEnd = &zIn[nLen];
if(nLen < 1) {
/* Empty string, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation */
for(;;) {
if(zIn >= zEnd) {
/* If we reach the end of the string,then the test succeeded. */
ph7_result_bool(pCtx, 1);
return PH7_OK;
}
if(zIn[0] >= 0xc0) {
/* UTF-8 stream */
break;
}
if(!SyisCtrl(zIn[0])) {
break;
}
/* Point to the next character */
zIn++;
}
/* The test failed, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/*
* bool ctype_digit(string $text)
* Checks if all of the characters in the provided string, text, are numerical.
* Parameters
* $text
* The tested string.
* Return
* TRUE if every character in the string text is a decimal digit, FALSE otherwise.
*/
static int PH7_builtin_ctype_digit(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const unsigned char *zIn, *zEnd;
int nLen;
if(nArg < 1) {
/* Missing arguments, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Extract the target string */
zIn = (const unsigned char *)ph7_value_to_string(apArg[0], &nLen);
zEnd = &zIn[nLen];
if(nLen < 1) {
/* Empty string, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation */
for(;;) {
if(zIn >= zEnd) {
/* If we reach the end of the string,then the test succeeded. */
ph7_result_bool(pCtx, 1);
return PH7_OK;
}
if(zIn[0] >= 0xc0) {
/* UTF-8 stream */
break;
}
if(!SyisDigit(zIn[0])) {
break;
}
/* Point to the next character */
zIn++;
}
/* The test failed, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/*
* bool ctype_xdigit(string $text)
* Check for character(s) representing a hexadecimal digit.
* Parameters
* $text
* The tested string.
* Return
* Returns TRUE if every character in text is a hexadecimal 'digit', that is
* a decimal digit or a character from [A-Fa-f] , FALSE otherwise.
*/
static int PH7_builtin_ctype_xdigit(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const unsigned char *zIn, *zEnd;
int nLen;
if(nArg < 1) {
/* Missing arguments, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Extract the target string */
zIn = (const unsigned char *)ph7_value_to_string(apArg[0], &nLen);
zEnd = &zIn[nLen];
if(nLen < 1) {
/* Empty string, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation */
for(;;) {
if(zIn >= zEnd) {
/* If we reach the end of the string,then the test succeeded. */
ph7_result_bool(pCtx, 1);
return PH7_OK;
}
if(zIn[0] >= 0xc0) {
/* UTF-8 stream */
break;
}
if(!SyisHex(zIn[0])) {
break;
}
/* Point to the next character */
zIn++;
}
/* The test failed, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/*
* bool ctype_graph(string $text)
* Checks if all of the characters in the provided string, text, creates visible output.
* Parameters
* $text
* The tested string.
* Return
* Returns TRUE if every character in text is printable and actually creates visible output
* (no white space), FALSE otherwise.
*/
static int PH7_builtin_ctype_graph(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const unsigned char *zIn, *zEnd;
int nLen;
if(nArg < 1) {
/* Missing arguments, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Extract the target string */
zIn = (const unsigned char *)ph7_value_to_string(apArg[0], &nLen);
zEnd = &zIn[nLen];
if(nLen < 1) {
/* Empty string, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation */
for(;;) {
if(zIn >= zEnd) {
/* If we reach the end of the string,then the test succeeded. */
ph7_result_bool(pCtx, 1);
return PH7_OK;
}
if(zIn[0] >= 0xc0) {
/* UTF-8 stream */
break;
}
if(!SyisGraph(zIn[0])) {
break;
}
/* Point to the next character */
zIn++;
}
/* The test failed, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/*
* bool ctype_print(string $text)
* Checks if all of the characters in the provided string, text, are printable.
* Parameters
* $text
* The tested string.
* Return
* Returns TRUE if every character in text will actually create output (including blanks).
* Returns FALSE if text contains control characters or characters that do not have any output
* or control function at all.
*/
static int PH7_builtin_ctype_print(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const unsigned char *zIn, *zEnd;
int nLen;
if(nArg < 1) {
/* Missing arguments, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Extract the target string */
zIn = (const unsigned char *)ph7_value_to_string(apArg[0], &nLen);
zEnd = &zIn[nLen];
if(nLen < 1) {
/* Empty string, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation */
for(;;) {
if(zIn >= zEnd) {
/* If we reach the end of the string,then the test succeeded. */
ph7_result_bool(pCtx, 1);
return PH7_OK;
}
if(zIn[0] >= 0xc0) {
/* UTF-8 stream */
break;
}
if(!SyisPrint(zIn[0])) {
break;
}
/* Point to the next character */
zIn++;
}
/* The test failed, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/*
* bool ctype_punct(string $text)
* Checks if all of the characters in the provided string, text, are punctuation character.
* Parameters
* $text
* The tested string.
* Return
* Returns TRUE if every character in text is printable, but neither letter
* digit or blank, FALSE otherwise.
*/
static int PH7_builtin_ctype_punct(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const unsigned char *zIn, *zEnd;
int nLen;
if(nArg < 1) {
/* Missing arguments, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Extract the target string */
zIn = (const unsigned char *)ph7_value_to_string(apArg[0], &nLen);
zEnd = &zIn[nLen];
if(nLen < 1) {
/* Empty string, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation */
for(;;) {
if(zIn >= zEnd) {
/* If we reach the end of the string,then the test succeeded. */
ph7_result_bool(pCtx, 1);
return PH7_OK;
}
if(zIn[0] >= 0xc0) {
/* UTF-8 stream */
break;
}
if(!SyisPunct(zIn[0])) {
break;
}
/* Point to the next character */
zIn++;
}
/* The test failed, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/*
* bool ctype_space(string $text)
* Checks if all of the characters in the provided string, text, creates whitespace.
* Parameters
* $text
* The tested string.
* Return
* Returns TRUE if every character in text creates some sort of white space, FALSE otherwise.
* Besides the blank character this also includes tab, vertical tab, line feed, carriage return
* and form feed characters.
*/
static int PH7_builtin_ctype_space(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const unsigned char *zIn, *zEnd;
int nLen;
if(nArg < 1) {
/* Missing arguments, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Extract the target string */
zIn = (const unsigned char *)ph7_value_to_string(apArg[0], &nLen);
zEnd = &zIn[nLen];
if(nLen < 1) {
/* Empty string, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation */
for(;;) {
if(zIn >= zEnd) {
/* If we reach the end of the string,then the test succeeded. */
ph7_result_bool(pCtx, 1);
return PH7_OK;
}
if(zIn[0] >= 0xc0) {
/* UTF-8 stream */
break;
}
if(!SyisSpace(zIn[0])) {
break;
}
/* Point to the next character */
zIn++;
}
/* The test failed, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/*
* bool ctype_lower(string $text)
* Checks if all of the characters in the provided string, text, are lowercase letters.
* Parameters
* $text
* The tested string.
* Return
* Returns TRUE if every character in text is a lowercase letter in the current locale.
*/
static int PH7_builtin_ctype_lower(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const unsigned char *zIn, *zEnd;
int nLen;
if(nArg < 1) {
/* Missing arguments, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Extract the target string */
zIn = (const unsigned char *)ph7_value_to_string(apArg[0], &nLen);
zEnd = &zIn[nLen];
if(nLen < 1) {
/* Empty string, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation */
for(;;) {
if(zIn >= zEnd) {
/* If we reach the end of the string,then the test succeeded. */
ph7_result_bool(pCtx, 1);
return PH7_OK;
}
if(!SyisLower(zIn[0])) {
break;
}
/* Point to the next character */
zIn++;
}
/* The test failed, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/*
* bool ctype_upper(string $text)
* Checks if all of the characters in the provided string, text, are uppercase letters.
* Parameters
* $text
* The tested string.
* Return
* Returns TRUE if every character in text is a uppercase letter in the current locale.
*/
static int PH7_builtin_ctype_upper(ph7_context *pCtx, int nArg, ph7_value **apArg) {
const unsigned char *zIn, *zEnd;
int nLen;
if(nArg < 1) {
/* Missing arguments, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Extract the target string */
zIn = (const unsigned char *)ph7_value_to_string(apArg[0], &nLen);
zEnd = &zIn[nLen];
if(nLen < 1) {
/* Empty string, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
/* Perform the requested operation */
for(;;) {
if(zIn >= zEnd) {
/* If we reach the end of the string,then the test succeeded. */
ph7_result_bool(pCtx, 1);
return PH7_OK;
}
if(!SyisUpper(zIn[0])) {
break;
}
/* Point to the next character */
zIn++;
}
/* The test failed, return FALSE */
ph7_result_bool(pCtx, 0);
return PH7_OK;
}
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) {
sxi32 rc;
sxu32 n;
desc->zString = MODULE_DESC;
*ver = MODULE_VER;
for(n = 0; n < SX_ARRAYSIZE(ctypeFuncList); ++n) {
rc = ph7_create_function(&(*pVm), ctypeFuncList[n].zName, ctypeFuncList[n].xFunc, &(*pVm));
if(rc != SXRET_OK) {
return rc;
}
}
return SXRET_OK;
}
-46
View File
@@ -1,46 +0,0 @@
/**
* @PROJECT AerScript Interpreter
* @COPYRIGHT See COPYING in the top level directory
* @FILE modules/ctype/ctype.h
* @DESCRIPTION Character type checking module for AerScript Interpreter
* @DEVELOPERS Symisc Systems <devel@symisc.net>
* Rafal Kupiec <belliash@codingworkshop.eu.org>
*/
#ifndef __CTYPE_H__
#define __CTYPE_H__
#include "ph7.h"
#include "ph7int.h"
#define MODULE_DESC "CType Module"
#define MODULE_VER 1.0
/* Forward reference */
static int PH7_builtin_ctype_alnum(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_ctype_alpha(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_ctype_cntrl(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_ctype_digit(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_ctype_xdigit(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_ctype_graph(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_ctype_print(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_ctype_punct(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_ctype_space(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_ctype_lower(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_ctype_upper(ph7_context *pCtx, int nArg, ph7_value **apArg);
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc);
static const ph7_builtin_func ctypeFuncList[] = {
{ "ctype_alnum", PH7_builtin_ctype_alnum },
{ "ctype_alpha", PH7_builtin_ctype_alpha },
{ "ctype_cntrl", PH7_builtin_ctype_cntrl },
{ "ctype_digit", PH7_builtin_ctype_digit },
{ "ctype_xdigit", PH7_builtin_ctype_xdigit},
{ "ctype_graph", PH7_builtin_ctype_graph },
{ "ctype_print", PH7_builtin_ctype_print },
{ "ctype_punct", PH7_builtin_ctype_punct },
{ "ctype_space", PH7_builtin_ctype_space },
{ "ctype_lower", PH7_builtin_ctype_lower },
{ "ctype_upper", PH7_builtin_ctype_upper }
};
#endif