Add C4PropList literals

The syntax is { Key: value, "Key2": value2 , Key3 = value3 }
We might want to drop : or = later.
stable-5.2
Günther Brammer 2009-04-12 02:56:41 +02:00
parent 9a9a7fec62
commit 1964e442f1
3 changed files with 75 additions and 0 deletions

View File

@ -190,6 +190,8 @@ enum C4AulBCCType
AB_STRING, // constant: string
AB_C4ID, // constant: C4ID
AB_ARRAY, // semi-constant: array
AB_PROPLIST, // create a new proplist
AB_PROPSET, // set a property of a proplist
AB_IVARN, // initialization of named var
AB_JUMP, // jump
AB_JUMPAND, // jump if zero, else pop the stack

View File

@ -184,6 +184,12 @@ class C4AulExec
CheckOverflow(1);
(++pCurVal)->SetArray(Array);
}
void PushPropList(C4PropList * PropList)
{
CheckOverflow(1);
(++pCurVal)->SetPropList(PropList);
}
void PushValue(const C4Value &rVal)
{
@ -753,6 +759,23 @@ C4Value C4AulExec::Exec(C4AulBCC *pCPos, bool fPassErrors)
break;
}
case AB_PROPLIST:
{
PushPropList(new C4PropList);
break;
}
case AB_PROPSET:
{
C4Value *pPropSet = pCurVal - 2, *pKey = pCurVal -1, *pValue = pCurVal;
if(!pPropSet->ConvertTo(C4V_PropList))
throw new C4AulExecError(pCurCtx->Obj, FormatString("Propset: proplist expected, got %s!", pPropSet->GetTypeName()).getData());
if(!pKey->ConvertTo(C4V_String))
throw new C4AulExecError(pCurCtx->Obj, FormatString("Propset: string expected, got %s!", pPropSet->GetTypeName()).getData());
pPropSet->_getPropList()->SetProperty(pKey->_getStr(), *pValue);
PopValues(2);
break;
}
case AB_ARRAYA_R: case AB_ARRAYA_V:
{
C4Value &Index = pCurVal[0];

View File

@ -141,6 +141,7 @@ class C4AulParseState
void Parse_Block();
int Parse_Params(int iMaxCnt, const char * sWarn, C4AulFunc * pFunc = 0);
void Parse_Array();
void Parse_PropList();
void Parse_While();
void Parse_If();
void Parse_For();
@ -909,6 +910,8 @@ static const char * GetTTName(C4AulBCCType e)
case AB_STRING: return "STRING"; // constant: string
case AB_C4ID: return "C4ID"; // constant: C4ID
case AB_ARRAY: return "ARRAY"; // semi-constant: array
case AB_PROPLIST: return "PROPLIST"; // semi-constant: array
case AB_PROPSET: return "PROPSET";
case AB_IVARN: return "IVARN"; // initialization of named var
case AB_JUMP: return "JUMP"; // jump
case AB_JUMPAND: return "JUMPAND";
@ -1021,6 +1024,7 @@ void C4AulParseState::AddBCC(C4AulBCCType eType, intptr_t X)
case AB_BOOL:
case AB_STRING:
case AB_C4ID:
case AB_PROPLIST:
case AB_VARN_R:
case AB_VARN_V:
case AB_PARN_R:
@ -1112,6 +1116,10 @@ void C4AulParseState::AddBCC(C4AulBCCType eType, intptr_t X)
iStack-=X-1;
break;
case AB_PROPSET:
iStack -= 2;
break;
default:
assert(false);
}
@ -2296,6 +2304,43 @@ void C4AulParseState::Parse_Array()
AddBCC(AB_ARRAY, size);
}
void C4AulParseState::Parse_PropList()
{
AddBCC(AB_PROPLIST);
Shift();
// insert block in byte code
while (1)
{
if (TokenType == ATT_BLCLOSE)
{
Shift();
return;
}
C4String * pKey;
if (TokenType == ATT_IDTF)
{
pKey = Strings.RegString(Idtf);
AddBCC(AB_STRING, (intptr_t) pKey);
Shift();
}
else if (TokenType == ATT_STRING)
{
AddBCC(AB_STRING, cInt);
Shift();
}
else UnexpectedToken("string or identifier");
if (TokenType != ATT_COLON && (TokenType != ATT_OPERATOR || !SEqual(C4ScriptOpMap[cInt].Identifier,"=")))
UnexpectedToken("':' or '='");
Shift();
Parse_Expression();
AddBCC(AB_PROPSET);
if (TokenType == ATT_COMMA)
Shift();
else if (TokenType != ATT_BLCLOSE)
UnexpectedToken("'}' or ','");
}
}
void C4AulParseState::Parse_While()
{
// Save position for later jump back
@ -2741,6 +2786,11 @@ void C4AulParseState::Parse_Expression(int iParentPrio)
Parse_Array();
break;
}
case ATT_BLOPEN:
{
Parse_PropList();
break;
}
default:
{
// -> unexpected token