当前位置: 首页 > news >正文

DSL概念、类别、为什么要写DSL 1

DSL概念

Martin Fowler defines a domain-specific language (DSL) as “a computer language that’s targeted to a particular kind of problem, rather than a general purpose language that’s aimed at any kind of software problem”

Domain-specific languages aren’t a new idea by any means. DSLs have been around since long before the start of computing. People have always developed specialized

vocabularies for specialized tasks.

Regular expressions and SQL are similarly specialized languages:Both are languages designed for a narrow domain—text processing and database querying, respectively.

Both are focused on letting you express what you mean, not how the implementation should work—that’s left to some magic engine in the background.

The reason these languages are so successful is that the focus they offer is incredibly useful. They reduce the complexity that you need to handle, and they’re flexible in

terms of what you can make them do.

DSL类别

External DSLs

DSLs that exist outside the confines of an existing language. SQL and regular expressions are two examples of external DSLs.

Common tools for building external DSLs include Lex, Yacc, ANTLR, GOLD Parser,

and Coco/R, among others. Those tools handle the first stage, translating text in a

known syntax to a format that a computer program can consume to produce execut-

able output. The part about “producing executable output” is usually left as an exer-

cise for the reader.

Graphical DSLs

uses shapes and lines to express intent rather than using text.

UML is a good example of a graphical DSL

Microsoft has the Visual Studio DSL Tools, which is a framework that allows you

to build tools similar to the class designer and generate code with them.

Fluent interfaces

Fluent interfaces are ways to structure your API so that operations flow naturally and

provide more readable code. They tend to be valid only when used by developersdur-

ing actual development, which limits their scope compared to other DSLs.

new Pipeline("rhino.png")

.Rotate(90)

.Watermark("Mocks")

.RoundCorners(100, Color.Bisque)

.Save("fluent-rhino.png");

User.FindAll(

Where.User.City == "London" &&

Where.User.RegisteredAt>= DateTime.Now.AddMonths(-3)

);

registry.AddInstanceOf<IWidget>()

.WithName("DarkGreen")

.UsingConcreteType<ColorWidget>()

.WithProperty("Color").EqualTo("DarkGreen");

 Internal or embedded DSLs

Internal DSLs are built on top of an existing language, but they don’t try to remain

true to the original programming language syntax. They try to express things in a way

that makes sense to both the author and the reader, not to the compiler.

Obviously, the expressiveness of an internal DSL is limited by whatever constraints

are imposed by the underlying language syntax. You can’t build a good DSL on top of

C# or Java; they have too much rigidity in their syntax to allow it. You probably could

build a good DSL on C++, but it would probably include preprocessor macros galore,

and I wouldn’t place any bets on how maintainable it would be.

The popular choices for building internal DSLs are dynamic languages; Lisp and

Smalltalk were probably the first common choices. Today, people mostly use Ruby,

Python, and Boo.

Rake

task :default => [:test]

task :test do

ruby "test/unittest.rb"

end

 

 

Other features that usually appear in dynamic languages are also useful when building DSLs: closures, macros, and duck typing.


The major advantage of an internal DSL is that it takes on all the power of the lan-

uage it’s written for. You don’t have to write the semantics of an if statement, or

edefine operator precedence, for instance. Sometimes that’s useful, and in one of my

DSL implementations I did redefine the if statement, but that’s probably not a good

hing to do in general, and it’s rarely necessary.

 

A DSL built on top of an existing language can also be problematic, because you

want to limit the options of the language to clarify what is going on. The DSL

houldn’t be a full-fledged programming language; you already have that in the base

anguage, after all.

The main purpose of an internal DSL is to reduce the amount of work required to

make the compiler happy and increase the clarity of the code in question.

The other purpose is to expose the domain. A DSLshould be readable by someone who is familiar with the domain, not the programming language.

为什么要写DSL

l 技术DSL:Making a technical issue or task simpler

This works well if your target audience is developers.

l 业务DSL:Expressing rules and actions in a way that’s close to the domain and understandable to businesspeople

A business DSL needs to be (at the very least) readable to a businessperson with no

background in programming. This type of DSL is mainly expressive in terms of the

domain, and it has a lot less emphasis on the programming features that may still exist.

l 扩展、自动化类的DSL : Automating tasks and actions, usually as part of adding scriptability and extensibility features to your applications.Automatic or extensible DSLs may also be called IT DSLs. This type of DSL is often

used to expose the internals of an application to the outside world.

如:Modern games are usually engines configured with some sort of scripting language. Another use for this style of DSL would be to get into the internals of an application and manage it.

 

参考图书: DSLs in Boo Domain-Specific Languages in .NET 

转载于:https://www.cnblogs.com/2018/archive/2012/09/02/2623629.html

相关文章:

  • Javascript 日期格式化
  • 成绩单课程翻译参考总表
  • NYOJ 86
  • C#处理oracle数据库的Clob类型
  • I have a dream
  • [笔记]8组LVDS_TX和LVDS_RX的调试心得
  • Struts2数据传输的背后机制:ValueStack(值栈)
  • A Medical Study Tool Developed with WPF
  • POJ题目分类---模拟[A一道删一道]
  • EJB之JPA(UUID与TABLE影射策略)
  • text-overflow:ellipsis溢出显示省略号兼容所有浏览器的解决办法
  • 场景中的物体显示 为BoundingBox
  • PHP程序员最常犯的11个MySQL错误
  • css书写顺序
  • 创建Metro风格的WPF界面
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • 【node学习】协程
  • angular2 简述
  • Angular2开发踩坑系列-生产环境编译
  • canvas实际项目操作,包含:线条,圆形,扇形,图片绘制,图片圆角遮罩,矩形,弧形文字...
  • Cookie 在前端中的实践
  • ES6--对象的扩展
  • JS+CSS实现数字滚动
  • js作用域和this的理解
  • leetcode388. Longest Absolute File Path
  • React16时代,该用什么姿势写 React ?
  • spring boot下thymeleaf全局静态变量配置
  • sublime配置文件
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 技术:超级实用的电脑小技巧
  • 力扣(LeetCode)357
  • 排序(1):冒泡排序
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 一起参Ember.js讨论、问答社区。
  • C# - 为值类型重定义相等性
  • 第二十章:异步和文件I/O.(二十三)
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • ## 临床数据 两两比较 加显著性boxplot加显著性
  • #微信小程序:微信小程序常见的配置传旨
  • (笔试题)分解质因式
  • (顶刊)一个基于分类代理模型的超多目标优化算法
  • (二)springcloud实战之config配置中心
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (转)IOS中获取各种文件的目录路径的方法
  • (转载)在C#用WM_COPYDATA消息来实现两个进程之间传递数据
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .NET/C# 在 64 位进程中读取 32 位进程重定向后的注册表
  • .NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)
  • .Net中ListT 泛型转成DataTable、DataSet
  • @四年级家长,这条香港优才计划+华侨生联考捷径,一定要看!
  • [ 网络基础篇 ] MAP 迈普交换机常用命令详解
  • [APIO2015]巴厘岛的雕塑
  • [BZOJ 2142]礼物(扩展Lucas定理)
  • [C#][opencvsharp]opencvsharp sift和surf特征点匹配
  • [C++]四种方式求解最大子序列求和问题