﻿/*

  fr_script.h
  fr_script.libを利用するためのヘッダファイル

*/

#ifndef FR_SCRIPT_H
#define FR_SCRIPT_H

#pragma comment(lib, "fr_script.lib")
#define FR_SCRIPT_VERSIONNUMBER		10003

#include <sstream>

#ifdef __cplusplus

#ifndef LibExport
#define LibExport extern "C"
#endif  /* LibExport */

// 数値または文字列を保持する汎用型
struct ValueW {
    bool isString;
    int iVal;
    std::wstring sVal;

    ValueW() : isString(false), iVal(0) {}
    ValueW(int v) : isString(false), iVal(v) {}
    ValueW(const std::wstring& v) : isString(true), iVal(0), sVal(v) {}

    // ヘルパー: 数値への強制変換
    int toInt() const {
        if (!isString) return iVal;
        return _wtoi(sVal.c_str());
    }
    // ヘルパー: 文字列への強制変換
    std::wstring toStr() const {
        if (isString) return sVal;
        std::wstringstream ss; ss << iVal;
        return ss.str();
    }
};


#else /* __cplusplus */

#ifndef LibExport
#define LibExport extern
#endif  /* LibExport */

// Cではコールバックは無効
struct ValueW {
    bool isString;
    int iVal;
	int dummy;
};

#endif /* __cplusplus */

// コールバック用構造体(char版)
struct FRCallbackDataW {
    LPCWSTR symbol;
    ValueW* args;
    int argCount;
	ValueW* retVal;
};


// コールバック関数型
typedef int (*FRCallbackFunc)(FRCallbackDataW*);

// エラー発生時のコールバック
typedef void (*ErrorCallBack)(int errcd, int iLine, LPCWSTR errmes);

// ステップコールバック
typedef int (*StepCallBack)(int iLine);

// MessageBox代替関数
typedef int (WINAPI *PMSGBOXWAPI)(HWND, LPCWSTR, LPCWSTR, UINT);

typedef struct {
	HWND hWnd;
	FRCallbackFunc cb;
	ErrorCallBack ec;
	StepCallBack sc;
	PMSGBOXWAPI pMsgBoxW;
	LPARAM lParam;
} FR_SCRIPT_PARAM;

#define ERROR_PARSE_ERR		101
#define ERROR_DIV0			102
#define ERROR_DIV0M			103
#define ERROR_UNKNOWN_FUNCD	104
#define ERROR_UNKNOWN_FUNC	105
#define ERROR_OUT_OF_MEMORY	106
#define ERROR_INVALID_PARAM	107

#define STEP_CALLBACK_CONTINUE	0
#define STEP_CALLBACK_STOP		1


// FR-Script呼び出し
LibExport int fr_script(const wchar_t* script, FR_SCRIPT_PARAM* param);


#endif FR_SCRIPT_H