add while

This commit is contained in:
lyon 2021-09-05 10:25:23 +08:00
parent 5eb33d40ad
commit 4d1f264f0c
6 changed files with 78 additions and 5 deletions

View File

@ -8,12 +8,12 @@
"name": "test",
"type": "cppdbg",
"request": "launch",
// "program": "${workspaceFolder}/../build/src/test/pikascript_test",
"program": "${workspaceFolder}/../build/src/boot/demo06-pikamain/pikascript_demo06-pikamain",
"program": "${workspaceFolder}/../build/src/test/pikascript_test",
// "program": "${workspaceFolder}/../build/src/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
// "--gtest_filter=object_test.test2"
// "--gtest_filter=block.init"
// "--gtest_filter=block.ifrun1"
"--gtest_filter=block.ifrun1"
// "--gtest_filter=sysObj.print"
// "--gtest_filter=object_test.a_b"
// "--gtest_filter=object_test.voidRun"

View File

@ -28,6 +28,11 @@ char *block_getBody(PikaObj *self)
return obj_getStr(self, "body");
}
char *block_setBody(PikaObj *self, char *body)
{
return obj_setStr(self, "body", body);
}
uint8_t block_checkAssert(PikaObj *self)
{
Args *buffs = New_strBuff();

View File

@ -13,4 +13,5 @@ uint8_t block_checkAssert(PikaObj *self);
uint16_t block_getLineSize(PikaObj *self);
void block_setMode(PikaObj *self, char *mode);
char *block_getMode(PikaObj *self);
char *block_setBody(PikaObj *self, char *body);
#endif

View File

@ -7,6 +7,7 @@
#include <stdarg.h>
#include "PikaBlock.h"
#include "PikaIf.h"
#include "PikaWhile.h"
PikaObj *obj_getContext(PikaObj *self)
{
@ -616,6 +617,11 @@ Args *obj_runDirect(PikaObj *self, char *cmd)
if_pushLine(block, cmd);
goto exit;
}
if (strEqu(block_getMode(block), "while"))
{
while_pushLine(block, cmd);
goto exit;
}
goto exit;
}
/* the block is end */
@ -625,8 +631,12 @@ Args *obj_runDirect(PikaObj *self, char *cmd)
if (strEqu(block_getMode(block), "if"))
{
if_run(block);
obj_removeArg(self, "_block");
}
if (strEqu(block_getMode(block), "while"))
{
while_run(block);
}
obj_removeArg(self, "_block");
/* not finished */
}
}
@ -642,6 +652,17 @@ Args *obj_runDirect(PikaObj *self, char *cmd)
goto exit;
}
/* while block */
if (strIsStartWith(cmd, "while "))
{
obj_setInt(self, "_isInBlock", 1);
obj_setObjWithoutClass(self, "_block", block_init);
PikaObj *block = obj_getObj(self, "_block", 0);
while_setAssert(block, cmd);
/* this line processed ok */
goto exit;
}
/* run script */
if (strIsContain(cmd, '(') && strIsContain(cmd, ')'))
{

View File

@ -3,4 +3,44 @@
#include "dataMemory.h"
#include "dataString.h"
#include "dataStrs.h"
#include <stdarg.h>
#include "PikaBlock.h"
#include <stdarg.h>
void while_setAssert(PikaObj *self, char *line)
{
Args *buffs = New_strBuff();
char *assert = strsRemovePrefix(buffs, line, "while ");
assert = strsGetFirstToken(buffs, assert, ':');
block_setAssert(self, assert);
block_setMode(self, "while");
args_deinit(buffs);
}
void while_pushLine(PikaObj *self, char *line)
{
Args *buffs = New_strBuff();
char *bodyLine = strsRemovePrefix(buffs, line, " ");
block_pushLine(self, bodyLine);
goto exit;
exit:
args_deinit(buffs);
return;
}
void while_run(PikaObj *self)
{
Args *buffs = New_strBuff();
char *body = strsCopy(buffs, block_getBody(self));
while (block_checkAssert(self))
{
PikaObj *host = obj_getContext(self);
while (0 != block_getLineSize(self))
{
char *line = block_popLine(self);
obj_run(host, line);
}
block_setBody(self, body);
}
exit:
args_deinit(buffs);
}

View File

@ -1,3 +1,9 @@
#ifndef _pikaWhile__H
#define _pikaWhile__H
#include "PikaObj.h"
void while_setAssert(PikaObj *self, char *line);
void while_pushLine(PikaObj *self, char *line);
void while_run(PikaObj *self);
#endif