明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1933|回复: 15

[基础] DIESEL的概念及其函数

[复制链接]
发表于 2020-7-27 07:37 | 显示全部楼层 |阅读模式
本帖最后由 1291500406 于 2020-8-2 09:09 编辑

D I E S E L

Dumb Interpretively Evaluated String Expression Language

This “Dumb Interpretively Executed String Expression Language” is the kernel of a macro language you can customise by adding C code and embedding it into your program.
It is short, written in portable C, and is readily integrated into any program. It is useful primarily to programs which need a very rudimentary macro expansion facility without the complexity of a full language such as Lisp or FORTH.
DIESEL copies its input directly to the output until a macro character, “$” or quoted string is encountered. Quoted strings may be used to suppress evaluation of sequences of characters which would otherwise be interpreted as macros. Quote marks may be included in quoted strings by two adjacent quote marks. For example:
    "$(if,1,True,False)="""$(if,1,True,False)""""
Status retrieval, computation, and display are performed by DIESEL functions. The available functions are as follows. User-defined functions are not implemented; what you see is all you've got. Naturally, if you embed DIESEL in your application, you'll add functions that provide access to information and actions within your own program. DIESEL's arithmetic functions accept either floating point or integer arguments, and perform all calculations in floating point.
DIESEL String Functions
$(+,val1,val2,…valn)
The sum of the numbers val1, val2,…valn is returned.
$(−,val1,val2,…valn)
The result of subtracting the numbers val2 through valn from val1 is returned.
$(*,val1,val2,…valn)
The result of multiplying the numbers val1,val2,…valn is returned.
$(/,val1,val2,…valn)
The result of dividing the number val1 by val2,…valn is returned.
$(=,val1,val2)
If the numbers val1 and val2 are equal 1 is returned, otherwise 0 is returned.
$(<,val1,val2)
If the number val1 is less than val2 1 is returned, otherwise 0 is returned.
$(>,val1,val2)
If the number val1 is greater than val2 1 is returned, otherwise 0 is returned.
$(!=,val1,val2)
If the numbers val1 and val2 are not equal 1 is returned, otherwise 0 is returned.
$(<=,val1,val2)
If the number val1 is less than or equal to val2 1 is returned, otherwise 0 is returned.
$(>=,val1,val2)
If the number val1 is greater than or equal to val2 1 is returned, otherwise 0 is returned.
$(AND,val1,val2,…valn)
The bitwise logical AND of the integers val1 through valn is returned.
$(EQ,val1,val2)
If the strings val1 and val2 are identical 1 is returned, otherwise 0.
$(EVAL,str)
The string str is passed to the DIESEL evaluator and the result of evaluating it is returned.
$(FIX,value)
The real number value is truncated to an integer by discarding any fractional part.
$(IF,expr,dotrue,dofalse)
If expr is nonzero, dotrue is evaluated and returned. Otherwise, dofalse is evaluated and returned. Note that the branch not chosen by expr is not evaluated.
$(INDEX,which,string)
string is assumed to contain one or more values delimited by the macro argument separator character, comma. which selects one of these values to be extracted, with the first item numbered zero.
$(NTH,which,arg0,arg1,argN)
Evaluates and returns the argument selected by which. If which is 0, arg0 is returned, and so on. Note the difference between $(NTH) and $(INDEX); $(NTH) returns one of a series of arguments to the function while $(INDEX) extracts a value from a comma-delimited string passed as a single argument. Arguments not selected by which are not evaluated.
$(OR,val1,val2,…valn)
The bitwise logical OR of the integers val1 through valn is returned.
$(STRFILL,string,ncopies)
Returns the result of concatenating ncopies of string.
$(STRLEN,string)
Returns the length of string in characters.
$(SUBSTR,string,start,length)
Returns the substring of string starting at character start and extending for length characters. Characters in the string are numbered from 1. If length is omitted, the entire remaining length of the string is returned.
$(UPPER,string)
The string is returned converted to upper case according to the rules of the current locale.
$(XOR,val1,val2,…valn)
The bitwise logical XOR of the integers val1 through valn is returned.
Starting DIESEL
You invoke DIESEL within your program by calling:
    int status;
    char instring[whatever], outstring[MAXSTR + 1];

    status = diesel(instring, outstring);
The output from the evaluation will be stored in outstring when control is returned to your program. If no errors were detected during evaluation, status will be zero. Otherwise status gives the character position within instring at which the error was detected. If an error occurs, DIESEL will include an error diagnostic, documented below, in outstring.
DIESEL Mechanics
Generally, if you mess something up in a DIESEL expression it's pretty obvious what went wrong. DIESEL embeds an error indication in the output stream depending on the nature of the error:
$?
Syntax error (usually a missing right parenthesis or runaway string).
$(func,??)
Incorrect arguments to func.
$(func)??
Unknown function func.
$++
Output string too long—evaluation truncated.
Variable Extensions
The base-line DIESEL includes no user-defined variables. This allows DIESEL to avoid allocating any local memory and renders it totally reentrant. If you compile DIESEL with the tag VARIABLES defined, the following additional functions are included which provide variable definition and access. Note that these functions call malloc() and strdup() and thus consume heap storage.
$(GETVAR,varname)
Returns the value stored in varname. If no variable with the name varname exists, a bad argument error is reported.
$(SETVAR,varname,value)
Stores the string value into varname. If no variable called varname exists, a new variable is created.
Unix Extensions
If you compile DIESEL with the tag UNIXTENSIONS defined, the following additional functions will be available:
$(GETENV,varname)
Returns the variable varname from the environment. If no such variable is defined, returns the null string.
$(TIME)
Returns the current time in Unix fashion, as the number of seconds elapsed since 00:00:00 GMT January 1, 1970.
$(EDTIME,time,picture)
Edit the Unix time time to format picture. If time is 0, the current date and time is edited (this is just shorthand for the equivalent “$(EDTIME,$(TIME),picture)”).
Assume the date is: Thursday, 2 September 1993 4:53:17
Format phrases:
        D               2
        DD              02
        DDD             Thu
        DDDD            Thursday
        M               9
        MO              09
        MON             Sep
        MONTH           September
        YY              93
        YYYY            1993
        H               4
        HH              04
        MM              53
        SS              17
        AM/PM           AM
        am/pm           am
        A/P             A
        a/p             a
If any of the “AM/PM” phrases appear in the picture, the “H” and “HH” phrases will edit the time according to the 12 hour civil clock (12:00–12:59—1:00–11:59) instead of the 24 hour clock (00:00–23:59).
Distribution Details
DIESEL is in the public domain; you can do anything you like with it.
DIESEL is supplied as a GZIPped TAR archive, diesel.tar.gz.
The following files will be present in the directory after the extraction is complete:
BCMAKE.BAT      Batch file to build with Borland C on MS-DOS.
DIESEL.C        Complete source code, including a built-in test
                program.
MAKEFILE        Make file for Unix.
MANIFEST        List of files in the distribution.
MSCMAKE.BAT     Batch file to build with Microsoft C 7.0.
REGRESS.DSL     Diesel regression test script.
REGRESS.MAS     Regression test master output file.
UTEST.DSL       Test for the Unix extensions to DIESEL.
All files are distributed in MS-DOS end of line convention (CR/LF). You may have to convert them to the native end of line convention of your system before compiling. DIESEL has been built and regression tested without errors on the following systems:
    Sun SPARCStation 2, SunOS 4.1.1
    MS-DOS, Borland C 2.0
    MS-DOS, Microsoft C 7.0
DIESEL Power Not Enough?
If DIESEL looks intriguing, but too small and simple for the application you have in mind, please check out ATLAST, my FORTH-based open application toolkit. ATLAST, although much larger and more complicated than DIESEL, is a full-fledged programming language which provides the speed of compiled code, multiple data types, user-defined functions and data types, and just about everything you'll need to make a closed application user programmable.

DIESEL(Direct Interpretively Evaluated String Expression Language)是指“直接解释求值的字符串表达式语言”。使用的DIESEL可以控制AutoCAD系统变量MODEMACRO的值,也可用来编写菜单宏,但只能用来处理字符串。

    DIESEL的宏表达式格式如下:
    $(字符串函数名,函数参数1,函数参数2…)
    其中,根据函数的不同,函数参数的个数可以为零或者多个,但最多为9个。在宏表达式的不同元素之间,不可以有任何空格。所有的宏表达式都必须以“$”符号开头。
    可以在DIESEL中使用的函数如表35-1所示。
表35-1 DIESEL函数一览表

函数名

格式

说明

+

$(+,val1,val2,…)

返回字符串为全部数字之和

-

$(-,val1,val2,…)

返回字符串为val1减去其他所有数字之差

*
$(*,val1,val2,…)

返回字符串为全部数字之积

/

$(/,val1,val2,…)

返回字符串为val1除以其他所有数字之商

=

$(=,val1,val2)

如果两个数字val1和val2相等,则返回字符串为1,否则为0

<  

$(<,val1,val2)

如果数字val1小于数字val2,则返回字符串为1,否则为0

>  

$(>,val1,val2)

如果数字val1大于数字val2,则返回字符串为1,否则为0

= $(!=,val1,val2) 如果两个数字val1和val2不相等,则返回字符串为1,否则为0
<=

$(<=,val1,val2)

如果数字val1小于等于数字val2,则返回字符串为1,否则为0

>=

$(>=,val1,val2)

如果数字val1大于等于数字val2,则返回字符串为1,否则为0

and

$(and,val1[,val2,..., val9])

返回整数val1~val9的按位逻辑与

angtos

$(angtos,value[,mode,precision])

按指定的格式和精度返回角度值(注1)


表35-1 DIESEL函数一览表

edtime

$(edtime,time,picture)

返回基于指定图片的格式化的日期和时间(注2)

eq

$(eq,str1,str2)

如果两个字符串str1和str2相同,则字符串返回1;否则返回0

eval

$(eval,str)

将一个字符串传给DIESEL计算器,并返回计算结果

fix

$(fix,value)

返回一个实数数字的整数部分

getenv

$(getenv,varname)

返回环境变量varname的当前值;对于未定义的环境变量,则返回空字符串

getvar

$(getvar,varname)

返回系统变量varname的值

if

$(if,expr,dotrue[,dofalse])

如果expr非零,则计算并返回dotrue的值;否则,计算并返回dofalse的值

index

$(index,which,string)

返回由which选定的、以逗号分隔的字符串中指定的成员,其中第一项编号为0

linelen

$(linelen)

返回用户所能见到的最长状态栏的字符长度。

nth

$(nth,which,arg0[,arg1,...,arg7])

计算并返回由which选定的参数的值,其中第一项编号为0

or

$(or,val1[,val2,...,val9])

返回整数val1~val9的按位逻辑或

rtos

$(rtos,value [,mode,precision])

按指定的格式和精度返回实数值

strlen


$(STRLEN,string)

返回字符串的字符长度

substr

$(substr,string, tart[,length])

返回字符串string的子串,该子串从字符start开始,长度为length。


字符串中的字符从1开始编号;如果length被省略,则返回从字符start开始、到string末尾为止的子串

upper

$(upper,string)

返回字符串string的大写

xor

$(xor,val1[,val2,...,val9])

返回整数val1~val9的按位逻辑异或


    注1 该函数根据参数“mode”和“precision”指定的格式,给定的参数“value”作为角度值进行编辑,如果参数“mode”和“precision”被省略,则使用由“unites”命令指定的当前值。参数“mode”的取值及其含义见表35-2。
               表35-2 “mode”的取值及其含义
Mode值

字符串格式

0



1

度/分/秒

2

百分度

3

弧度

4

勘测单位


                   注2 该函数返回基于指定图片的格式化的日期和时间。                   其中,“time”参数为给定的AutoCAD日期,通常使用函数“$(getvar,date)”来获取;                   “picture”参数由日期和时间的特定表示法替代的格式短语组成。格式短语的定义如表35-3所示。所有不能解释为格式短语的字符将被完整地复制到函数返回结果中。               表35-3 “edtime”函数格式短语定义
格式

输出示例

格式

输出示例

D

9

H

5

DD

09

HH

05

DDD

Tue

MM

47

DDDD

Tuesday

SS

17

^M

6

MSEC

506

MO

06

AM/PM

AM

MON

Jun

am/pm

pm

MONTH

June

A/P

P

YY

01

a/p

p

YYYY

2001






    例如以如下格式使用该函数:下例使用上表中的日期和时间。
                   $(edtime,$(getvar,date),DDD"," DD MON YYYY - H:MMam/pm)                   它可能的返回结果为:                   Tue, 9 Jun 2001 - 5:47pm                   注意 “picture”参数中逗号必须放在引号中,否则它将被当作参数分隔符。

例子:
(menucmd "M=$(index,0,$(substr,$(getvar,dwgname),1[,$(-,$(strlen,$(getvar,dwgname)),4)]))")

(menucmd "M=$(edtime,$(getvar,date),DDDD\",\" D MONTH YYYY)")

(menucmd "I=moreicons")       Loadsthe MOREICONS image tile menu
(menucmd "I=*")               Displaysthe menu

(setq s (menucmd "P11.3=?"))  Gets the status of the menu item
(if (= s "")                  If the status is an empty string,
(menucmd "P11.3=~")         disable the menu item)


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

评分

参与人数 1明经币 +1 收起 理由
xvjiex + 1 赞一个!

查看全部评分

"觉得好,就打赏"
还没有人打赏,支持一下
 楼主| 发表于 2020-7-27 20:10 | 显示全部楼层
本帖最后由 1291500406 于 2020-7-27 20:16 编辑
mokson 发表于 2020-7-27 08:24
中英文俱全,一手好资料。


最近有几个人问我,DIESEL语言方面问题,才准备的这个资料
字段表达式太少了,总结起来很难,它的同步更新能力,能省很多事

反应器也能更新内容,不过要加载程序和回调函数,不是很方便


发表于 2022-3-7 15:14 | 显示全部楼层
(menucmd "M=$(edtime,$(getvar,date), D\/MONTH\/YYYY)")
中文cad 发现只能返回: 7/三月/2022

如何返回这种日期格式:
7/Mar/2022
发表于 2020-9-2 20:44 | 显示全部楼层
很早就看过了,国外arup大神建立的,可以就只有一个范例,系统时间的,其他基本函数没学会
发表于 2020-7-27 08:24 | 显示全部楼层
中英文俱全,一手好资料。
发表于 2020-7-27 08:37 | 显示全部楼层
学习了,“日知其所亡,月无忘其所能,可谓好学也已矣。”
发表于 2020-7-27 10:46 | 显示全部楼层
Thanks for sharing ^^
发表于 2020-7-28 08:07 | 显示全部楼层
我只是用它做键盘快捷键,很方便!
发表于 2020-7-28 14:06 | 显示全部楼层
老大多来点实例让大家学习学习
发表于 2020-7-28 15:04 | 显示全部楼层
这个好,偶尔要用查了资料,过了段时间又去找,这下免得到处找
发表于 2020-7-28 19:51 | 显示全部楼层
很详细的资料,学无止境,自己接触的还是少,感谢楼主分享资料
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-26 21:07 , Processed in 0.939220 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表