📄 unitdllfunctiontcompile.pas
字号:
unit UnitDllFunctionTCompile;
//////////////////////////////////////////////////////////////////////////
// //
// 数学函数动态编译器TCompile类 的dll函数封装 作者:侯思松 2003.3.19 //
// 有改进意见或发现任何错误请转告我,本人不胜感激。 //
// E-Mail:HouSisong@263.net //
// ( 转载时请保留本说明:) ) //
// //
//////////////////////////////////////////////////////////////////////////
///<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<///
/// <<功能简介>>: ///
/// TCompile可以在程序运行过程中动态完成数学函数表达式字符串的编译执行, ///
/// (可以带参数,布尔运算,定积分;动态生成机器码执行,不是解释执行)执行速度超快!!! ///
///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>///
interface
uses
windows,Compile_Hss,math,SysUtils;
type
cm_Handle = DWord;
const
cm_Max_ParameterName_Length =256;
type
cm_TUserParameterList =record
CName :array [0..cm_Max_ParameterName_Length-1] of Char; //参数名称
CAddress :PExtended; //参数地址
end;
type cm_PTUserParameterList = ^cm_TUserParameterList;
//创建编译类,成功返回编译类句柄,失败返回0; 不用时调用cm_CloseTCmHandle关闭句柄
//其它函数需要传递该值作为第一个参数
function cm_CreateTCompile():cm_Handle;stdcall;
//关闭编译类句柄,释放空间
function cm_CloseTCmHandle(const hcm: cm_Handle):Longbool;stdcall;
// 调用函数返回表达式的值(实参数值列表); //等价于 cm_SetFunctionParameter + cm_GetValue
procedure cm_GetFunctionValue(const hcm: cm_Handle; const PList: PExtended ;const PResult: PExtended);stdcall; // (编译后才能调用)
// 调用函数返回表达式的值;
procedure cm_GetValue(const hcm: cm_Handle;const PResult: PExtended);stdcall;//(编译后才能调用)
// 按当前设置的参数表传入参数值(实参数值列表)
procedure cm_SetFunctionParameter(const hcm: cm_Handle; const PList: PExtended);stdcall; // (编译前后都能调用)
// 设置需要编译的字符串(要编译的字符串,虚参数列表字符串,是否自动编译);
// 比如:Value:='Sqr(x)+Sqr(y)'; ParameterList:='x,y' ;
function cm_SetText(const hcm: cm_Handle; const TextValue:PChar;
const ParameterList : PChar;const IfCompile:longbool):longbool;stdcall;//(编译前调用,这是最先要做的)
// 编译当前字符串
function cm_Compile(const hcm: cm_Handle):longbool;stdcall;
// 测试是否使用了未定义的变量
function cm_IfHaveUnDefineParameter(const hcm: cm_Handle):longbool;stdcall; //(编译后才能调用)
//设置一个外部变量(外部变量名称,外部变量地址); 这样就可以和高级语言或另一个编译类共享变量了
function cm_SetExteriorParameter(const hcm: cm_Handle;const PName:PChar;const PAddress:PExtended):longbool;stdcall;
//(编译前调用,如果是在编译后,需要调用cm_RefreshExeAddressCodeInPointer刷新地址)
// 设置外部数组(数组名称,数组地址);
function cm_SetExteriorArrayParameter(const hcm: cm_Handle;const ArrayPName:PChar;const ArrayPAddress:PExtended):longbool;stdcall;
//(编译前调用,如果是在编译后,需要调用cm_RefreshExeAddressCodeInPointer刷新地址)
//刷新变更地址 //(设置完所有的外部变量以后需要调用一次该函数)
procedure cm_RefreshExeAddressCodeInPointer(const hcm: cm_Handle);stdcall;
// 处理预定义宏(要代换的标识符,代换为的描述字符串); // 可以用来处理常数,甚至定义新的函数!
// 如 Key:='a'; Value:='-0.5' , 或 Key:='f(x,y)',Value:='Max(x,Sin(y))' 等;
function cm_Define(const hcm: cm_Handle;const Key,Value : PChar):longbool;stdcall; //(编译前调用)
// 处理常数定义(要代换的标识符,代换的值) // 常数定义, Value必须是一个可计算的值
// 如 Key:='a'; Value:='2' , 或 Key:='b' , Value:='2*sin(PI/2)' 等;
// 该功能完全可以用预定义宏(Define)来代替,
// 但当值为常数时这样处理有可能使最后得到的编译函数速度更快,并加快编译速度
function cm_DefineConst(const hcm: cm_Handle;const Key,Value: PChar):longbool;stdcall; //(编译前调用)
//根据参数名称PName得到参数地址值
function cm_GetParameterAddress(const hcm: cm_Handle;const PName:PChar):PExtended;stdcall;
//按参数名称PName设置参数值dValue
function cm_SetParameterN(const hcm: cm_Handle;const PName:PChar;const dValue:Extended):longbool;stdcall;
//按参数地址PAddress设置参数值dValue
procedure cm_SetParameterA(const hcm: cm_Handle;const PAddress:PExtended;const dValue:Extended);stdcall;
//得到参数PName的值
procedure cm_GetParameterValue(const hcm: cm_Handle;const PName:PChar;const PResult: PExtended);stdcall;
//得到参数的总数目(不包括常数)
Function cm_GetUserParameterCount(const hcm: cm_Handle):DWord;stdcall;
//通过PList返回参数列表(不包括常数)
procedure cm_GetUserParameterList(const hcm: cm_Handle;const PList:cm_PTUserParameterList);stdcall;
//测试参数PName是否已经存在
function cm_IfHaveParameter(const hcm: cm_Handle;const PName:PChar):longBool;stdcall;
//设置随机函数Rnd()的初始种子值为完全随机种子(系统用当前精确到毫秒的时间设置)
procedure cm_SetRandomizeT(const hcm: cm_Handle);stdcall;
//设置随机函数Random()的初始种子值
procedure cm_SetRandomizeI(const hcm: cm_Handle;const RandomSeed :integer);stdcall;
procedure cm_SetEnabledNote(const hcm: cm_Handle;const Value:longbool);stdcall; //是否允许使用注释 (默认false)
procedure cm_SetEnabledOptimizeDiv(const hcm: cm_Handle;const Value:longbool); stdcall; //是否要优化常数浮点除法运算 (默认true)
procedure cm_SetEnabledOptimizeStack(const hcm: cm_Handle;const Value:longbool);stdcall; //是否要优化堆栈 (默认true)
procedure cm_SetEnabledOptimizeConst(const hcm: cm_Handle;const Value:longbool); stdcall;//是否要优化常数运算 (默认true)
Function cm_GetVersion():double;stdcall; // 获得版本号
function cm_GetErrorCode(const hcm: cm_Handle):integer;stdcall; //返回错误代码号
Function cm_GetExeCodeLength(const hcm: cm_Handle):integer; stdcall; //返回编译以后的程序指令区代码长度(字节)
Function cm_GetExeParameterLength(const hcm: cm_Handle):integer;stdcall; //返回编译以后的程序数据区代码长度(字节)
// 调用函数返回表达式的值;
function VB_cm_GetValue(const hcm: cm_Handle):double;stdcall;//(编译后才能调用)
// 调用函数返回表达式的值(实参数值列表); //等价于 cm_SetFunctionParameter + cm_GetValue
function VB_cm_GetFunctionValue(const hcm: cm_Handle; const PList: PDouble ):Double;stdcall; // (编译后才能调用)
// 按当前设置的参数表传入参数值(实参数值列表)
procedure VB_cm_SetFunctionParameter(const hcm: cm_Handle; const PList: PDouble );stdcall; // (编译前后都能调用)
//按参数名称PName设置参数值dValue
function VB_cm_SetParameterN(const hcm: cm_Handle;const PName:PChar;const dValue:double):longbool;stdcall;
//按参数地址PAddress设置参数值dValue
procedure VB_cm_SetParameterA(const hcm: cm_Handle;const PAddress:PExtended;const dValue:Double);stdcall;
//得到参数PName的值
function VB_cm_GetParameterValueN(const hcm: cm_Handle;const PName:PChar):double;stdcall;
//得到参数PName的值
function VB_cm_GetParameterValueA(const PAddress:PExtended):double;stdcall;
//获取当前CPU周期计数(CPU周期数)
function VB_GetCPUCount():double;stdcall;
(*
//错误号定义
const
csTCompile_NoError = 0; //没有发现错误!
csTCompile_NoKnownError = 1; //不知道的错误!
csTCompile_NoErrorCode = 2; //找不到错误号所对应的错误描述!
csTCompile_CompileHexCodeError = 3; //编译时指令的十六进制代码错误!
csTCompile_HexMod2_EQ_1_Error = 4; //编译时传入指令长度错误!
csTCompile_PMMarker_Error = 5; //编译得到参数名称时发生错误!
csTCompile_FMMarker_Error = 6; //编译得到函数名称时发生错误!
csTCompile_Wording_Error = 7; //语法发生错误!
csTCompile_Bracket_Error = 8; //语法错误,在 ( ) 处!
csTCompile_Optimize_Error = 9; //编译优化时发生错误!
csTCompile_Define_Error =10; //函数编译错误(或超出定义域)!
csTCompile_Handwriting_Error =11; //函数书写格式错误!
csTCompile_FFHandwriting_Error =12; //积分函数书写格式错误!
csTCompile_ReadFloat_Error =13; //编译读取常数数字时发生错误!
csTCompile_ReadMarker_Error =14; //编译读取标识符时发生错误!
csTCompile_Read_Error =15; //语法错误,有不识别的字符!
csTCompile_Note_Match_Error =16; //注释符号不匹配! { } 或 /* */
csTCompile_FPList_Error =17; //参数列表错误!
csTCompile_IFHandwriting_Error =18; //If函数书写格式错误!
*)
implementation
function cm_CreateTCompile():cm_Handle;stdcall;
begin
result:=DWord(TCompile.Create());
end;
function cm_CloseTCmHandle(const hcm: cm_Handle):Longbool;stdcall;
begin
if (hcm<>0) and (TCompile(hcm) is TCompile) then
begin
try
TCompile(hcm).Free;
result:=true;
except
result:=false;
end;
end
else
result:=false;
end;
procedure cm_GetValue(const hcm: cm_Handle;const PResult: PExtended);stdcall;
begin
PResult^:=TCompile(hcm).GetValue();
end;
procedure cm_GetFunctionValue(const hcm: cm_Handle; const PList: PExtended ;const PResult: PExtended);stdcall;
type
PArrayExtended=array of Extended;
begin
PResult^:=TCompile(hcm).GetFunctionValue(PArrayExtended(PList));
end;
procedure cm_SetFunctionParameter(const hcm: cm_Handle; const PList: PExtended);stdcall;
type
PArrayExtended=array of Extended;
begin
TCompile(hcm).SetFunctionParameter(PArrayExtended(PList));
end;
function cm_SetText(const hcm: cm_Handle; const TextValue:PChar;
const ParameterList : PChar;const IfCompile:longbool):longbool;stdcall;
begin
result:=TCompile(hcm).SetText(TextValue,ParameterList,IfCompile);
end;
function cm_Compile(const hcm: cm_Handle):longbool;stdcall;
begin
result:=TCompile(hcm).Compile();
end;
function cm_IfHaveUnDefineParameter(const hcm: cm_Handle):longbool;stdcall;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -