Skip to content

Commit

Permalink
choicetype:{block}
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Nov 2, 2021
1 parent 399bb1c commit 1661981
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 36 deletions.
17 changes: 5 additions & 12 deletions function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,7 @@ int CFunction::ExecuteInBrace(int line, CValue &result, CLocalVariable &lvar, in
lvar.AddDepth();
CDuplEvInfo* pdupl = &dupl_func;
if (lvar.GetDepth() != 1) {
switch (dupl_func.GetType()) {
case CHOICETYPE_POOL:
case CHOICETYPE_POOL_ARRAY:
case CHOICETYPE_NONOVERLAP_POOL:
case CHOICETYPE_SEQUENTIAL_POOL:
break;
default:
pdupl = NULL;
}
pdupl = statement[line-1].dupl_block.get();
}

// 実行
Expand All @@ -133,9 +125,10 @@ int CFunction::ExecuteInBrace(int line, CValue &result, CLocalVariable &lvar, in
switch(statement[i].type) {
case ST_OPEN: // "{"
i = ExecuteInBrace(i + 1, t_value, lvar, BRACE_DEFAULT, exitcode);
if(inmutiarea && pdupl) {
switch (dupl_func.GetType()) {
//choicetype_nonoverlap/sequential_pool must retain array same as CHOICETYPE_POOL_ARRAY
if(inmutiarea && pdupl->GetType()) {
switch (pdupl->GetType()) {
case CHOICETYPE_NONOVERLAP_POOL:
case CHOICETYPE_SEQUENTIAL_POOL:
case CHOICETYPE_POOL:
t_value = GetResultFromPoolArray(t_value);
default:
Expand Down
29 changes: 25 additions & 4 deletions function.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,28 @@ class CStatement
{
public:
int type; // ステートメントの種別
mutable std_shared_ptr < CDuplEvInfo > dupl_block; // pool:{ //...
int jumpto; // 飛び先行番号 break/continue/return/if/elseif/else/for/foreachで使用します
// 該当単位終端の"}"の位置が格納されています
int linecount; // 辞書ファイル中の行番号

private:
mutable std_shared_ptr<std::vector<CCell> > m_cell; // 数式の項の群 
mutable std_shared_ptr<std::vector<CSerial> > m_serial; // 数式の演算順序
mutable std_shared_ptr<std::vector<CCell> > m_cell; // 数式の項の群 
mutable std_shared_ptr<std::vector<CSerial> > m_serial; // 数式の演算順序

public:
CStatement(int t, int l)
CStatement(int t, int l, CDuplEvInfo* cher=NULL)
{
type = t;
linecount = l;
jumpto = 0;
dupl_block.reset(cher);
}
CStatement(void) {
type = ST_NOP;
linecount = 0;
jumpto = 0;
dupl_block = 0;
}
~CStatement(void) {}

Expand Down Expand Up @@ -145,7 +148,7 @@ class CFunction
CFunction(void);

public:
CFunction(CAyaVM &vmr, const yaya::string_t& n, int ct, const yaya::string_t& df, int lc) : pvm(&vmr) , name(n) , dupl_func(ct) , dicfilename(df) , linecount(lc)
CFunction(CAyaVM &vmr, const yaya::string_t& n, choicetype_t ct, const yaya::string_t& df, int lc) : pvm(&vmr) , name(n) , dupl_func(ct) , dicfilename(df) , linecount(lc)
{
namelen = name.size();
}
Expand All @@ -164,6 +167,24 @@ class CFunction
const yaya::string_t& GetFileName() const {return dicfilename;}
size_t GetLineNumBegin() const { return linecount;}
size_t GetLineNumEnd() const { return statement.empty() ? 0 : statement[statement.size()-1].linecount;}
choicetype_t GetDefaultBlockChoicetype(){
switch(dupl_func.GetType())
{
case CHOICETYPE_RANDOM:
case CHOICETYPE_NONOVERLAP:
case CHOICETYPE_SEQUENTIAL:
return CHOICETYPE_RANDOM;
case CHOICETYPE_VOID:
return CHOICETYPE_VOID;
case CHOICETYPE_ARRAY:
case CHOICETYPE_POOL:
case CHOICETYPE_POOL_ARRAY:
case CHOICETYPE_NONOVERLAP_POOL:
case CHOICETYPE_SEQUENTIAL_POOL:
default:
return CHOICETYPE_ARRAY;
}
}

protected:
int ExecuteInBrace(int line, CValue &result, CLocalVariable &lvar, int type, int &exitcode);
Expand Down
53 changes: 39 additions & 14 deletions parser0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,9 +815,9 @@ char CParser0::DefineFunctions(std::vector<yaya::string_t> &s, const yaya::strin
return 1;
}
// 重複回避オプションの判定
int chtype = CHOICETYPE_RANDOM;
choicetype_t chtype = CHOICETYPE_RANDOM;
if (d1.size()) {
int ci = 0, cn = sizeof(choicetype) / sizeof(choicetype[0]);
int ci = 0, cn = sizeof(choicetype) / sizeof(choicetype[0]);

for(ci = 0; ci < cn; ci++) {
if (!d1.compare(choicetype[ci].name)) {
Expand All @@ -834,7 +834,7 @@ char CParser0::DefineFunctions(std::vector<yaya::string_t> &s, const yaya::strin
// 作成
targetfunction = MakeFunction(d0, chtype, dicfilename, linecount);
if (targetfunction == -1) {
vm.logger().Error(E_E, 13, *it, dicfilename, linecount);
vm.logger().Error(E_E, 13, *it, dicfilename, linecount);
return 1;
}
continue;
Expand All @@ -850,6 +850,10 @@ char CParser0::DefineFunctions(std::vector<yaya::string_t> &s, const yaya::strin
}
}
else {
if ((*it)[it->size()-1]==L':' && *(it+1)==L"{"){
*(it+1)=L"";
*it+=L"{";
}
// 関数内のステートメントの定義 {}入れ子の計算もここで行う
if (!StoreInternalStatement(targetfunction, *it, depth, dicfilename, linecount))
retcode = 1;
Expand All @@ -870,7 +874,7 @@ char CParser0::DefineFunctions(std::vector<yaya::string_t> &s, const yaya::strin
*       指定された名前の関数が既に作成済の場合はエラーで、-1を返します
* -----------------------------------------------------------------------
*/
int CParser0::MakeFunction(const yaya::string_t& name, int chtype, const yaya::string_t& dicfilename, int linecount)
int CParser0::MakeFunction(const yaya::string_t& name, choicetype_t chtype, const yaya::string_t& dicfilename, int linecount)
{
int i = vm.function_parse().GetFunctionIndexFromName(name);
if(i != -1)
Expand All @@ -896,47 +900,68 @@ int CParser0::MakeFunction(const yaya::string_t& name, int chtype, const yaya::s
char CParser0::StoreInternalStatement(int targetfunc, yaya::string_t &str, int& depth, const yaya::string_t& dicfilename, int linecount)
{
// パラメータのないステートメント

auto&targetfunction=vm.function_parse().func[targetfunc];
if(!str.size())
return 1;
// {
if (!str.compare(L"{")) {
if (str[str.size()-1]==L'{') {
// blockと重複回避オプションを取得
choicetype_t chtype = targetfunction.GetDefaultBlockChoicetype();
yaya::string_t d0, d1;
if (Split(str, d0, d1, L":")){
// 重複回避オプションの判定
int ci = 0, cn = sizeof(choicetype) / sizeof(choicetype[0]);

for(ci = 0; ci < cn; ci++) {
if (!d0.compare(choicetype[ci].name)) {
chtype = choicetype[ci].type;
break;
}
}

if (ci == cn) { //not found
vm.logger().Error(E_E, 30, d0, dicfilename, linecount);
return 0;
}
}
depth++;
vm.function_parse().func[targetfunc].statement.push_back(CStatement(ST_OPEN, linecount));
targetfunction.statement.emplace_back(CStatement(ST_OPEN, linecount, new CDuplEvInfo(chtype)));
return 1;
}
// }
else if (!str.compare(L"}")) {
depth--;
vm.function_parse().func[targetfunc].statement.push_back(CStatement(ST_CLOSE, linecount));
targetfunction.statement.emplace_back(CStatement(ST_CLOSE, linecount));
return 1;
}
// others elseへ書き換えてしまう
else if (!str.compare(L"others")) {
vm.function_parse().func[targetfunc].statement.push_back(CStatement(ST_ELSE, linecount));
targetfunction.statement.emplace_back(CStatement(ST_ELSE, linecount));
return 1;
}
// else
else if (!str.compare(L"else")) {
vm.function_parse().func[targetfunc].statement.push_back(CStatement(ST_ELSE, linecount));
targetfunction.statement.emplace_back(CStatement(ST_ELSE, linecount));
return 1;
}
// break
else if (!str.compare(L"break")) {
vm.function_parse().func[targetfunc].statement.push_back(CStatement(ST_BREAK, linecount));
targetfunction.statement.emplace_back(CStatement(ST_BREAK, linecount));
return 1;
}
// continue
else if (!str.compare(L"continue")) {
vm.function_parse().func[targetfunc].statement.push_back(CStatement(ST_CONTINUE, linecount));
targetfunction.statement.emplace_back(CStatement(ST_CONTINUE, linecount));
return 1;
}
// return
else if (!str.compare(L"return")) {
vm.function_parse().func[targetfunc].statement.push_back(CStatement(ST_RETURN, linecount));
targetfunction.statement.emplace_back(CStatement(ST_RETURN, linecount));
return 1;
}
// --
else if (!str.compare(L"--")) {
vm.function_parse().func[targetfunc].statement.push_back(CStatement(ST_COMBINE, linecount));
targetfunction.statement.emplace_back(CStatement(ST_COMBINE, linecount));
return 1;
}

Expand Down
3 changes: 2 additions & 1 deletion parser0.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <vector>
#include "globaldef.h"
#include "selecter.h"

class CDefine
{
Expand Down Expand Up @@ -76,7 +77,7 @@ class CParser0
char IsCipheredDic(const yaya::string_t& filename);
void SeparateFactor(std::vector<yaya::string_t> &s, yaya::string_t &line);
char DefineFunctions(std::vector<yaya::string_t> &s, const yaya::string_t& dicfilename, int linecount, int &depth, int &targetfunction);
int MakeFunction(const yaya::string_t& name, int chtype, const yaya::string_t& dicfilename, int linecount);
int MakeFunction(const yaya::string_t& name, choicetype_t chtype, const yaya::string_t& dicfilename, int linecount);
char StoreInternalStatement(int targetfunc, yaya::string_t &str, int& depth, const yaya::string_t& dicfilename, int linecount);
char MakeStatement(int type, int targetfunc, yaya::string_t &str, const yaya::string_t& dicfilename, int linecount);
char StructWhen(yaya::string_t &str, std::vector<CCell> &cells, const yaya::string_t& dicfilename, int linecount);
Expand Down
10 changes: 5 additions & 5 deletions selecter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "value.h"
#include "variable.h"

enum {
enum choicetype_t {
CHOICETYPE_RANDOM = 0, /* 常に無作為にランダム(デフォルト)*/
CHOICETYPE_NONOVERLAP, /* ランダムだが一巡するまで重複選択しない */
CHOICETYPE_SEQUENTIAL, /* 順番に選択する */
Expand All @@ -35,7 +35,7 @@ enum {
CHOICETYPE_SEQUENTIAL_POOL, /* sequentialのスコープ無視版 */
};

const struct { yaya::char_t *name; int type; } choicetype[] = {
const struct { yaya::char_t *name; choicetype_t type; } choicetype[] = {
{ L"random", CHOICETYPE_RANDOM } ,
{ L"nonoverlap", CHOICETYPE_NONOVERLAP } ,
{ L"sequential", CHOICETYPE_SEQUENTIAL } ,
Expand Down Expand Up @@ -65,7 +65,7 @@ class CVecValue
class CDuplEvInfo
{
protected:
int type; // 選択種別
choicetype_t type; // 選択種別

std::vector<int> num; // --で区切られた領域毎の候補数
std::vector<int> roundorder; // 巡回順序
Expand All @@ -78,15 +78,15 @@ class CDuplEvInfo
CDuplEvInfo(void);

public:
CDuplEvInfo(int tp)
CDuplEvInfo(choicetype_t tp)
{
type = tp;
total = 0;
index = 0;
lastroundorder = -1;
}

int GetType(void) { return type; }
choicetype_t GetType(void) { return type; }

CValue Choice(CAyaVM &vm,int areanum, const std::vector<CVecValue> &values, int mode);
CValue ChoiceValue(CAyaVM& vm, CValue& value, int mode);
Expand Down

0 comments on commit 1661981

Please sign in to comment.