Custom domain-specific language (DSL, domain-specific language) using the operator overload function of C ++, Scala, and other languages)

Source: Internet
Author: User

Http://hi.baidu.com/atry/blog/item/c4bc96ef7a1ac7e8cf1b3ef4.html

 

Domain-specific language (DSL) is a hot topic in recent years. One trend of DSL is to integrate DSL into common languages.

Many years ago, if we wanted to use DSL in a common language, we usually needed strings, such as database access. We had to splice a string of SQL statements. There are many disadvantages: a) not natural, special characters need to be escaped; B) vulnerabilities that are prone to SQL injection; c) Lack of syntax check, if a word is misspelled, the error can only be known at runtime, which is difficult to debug.

It's not until the 21st century. Many languages have built-in DSL. For example, new features E4X in the ecmascript language, such as JavaScript and ActionScript, can be easily used by XML and XPath. For example, in C #3.0, you can write SQL queries. This is a good phenomenon, but what we talk about in this article is "Custom" DSL. It can not only use the DSL provided by the language itself, but also create your own DSL.

This can be achieved through Operator overloading. I first came into contact with the operator overload as C ++. This feature was not intended to be used to design DSL, but to simplify the code for some daily operations, such as string connection. However, this is the 21st century. Finally, a group of abnormal programmers have tapped out the potential of this feature. Many libraries in boost use Operator Overloading to implement DSL, such as boost. xpressive (implemented regular expressions), boost. spirit (implemented the BNF paradigm), boost. phoenix (implements anonymous function declarations or lambda expressions ). I have also implemented static_lambda using the operator overload. I can convert an anonymous function into a static class and use it in the template parameters.

Based on my experience, this technology is useful. Boost. xpressive and boost. Spirit are both very useful.

However, there are still many fatal problems with DSL implemented using the C ++ operator overload:
1. The C ++ 98 template lacks the variable number of template parameters.
2. matching of the left and right values of C ++ 98 has serious defects.
3. c ++ Compilation speed is too slow.
4. c ++ does not support space overloading.

The first two problems are the problems of C ++ 98. In a few years, if C ++ 0x is popularized, it will be solved.
The third problem is the old and difficult problem. It is estimated that it will not be able to be solved when the sun turns into a red star. However, C ++ has its successor D language. The compilation speed is extremely fast and it is good to use it to customize DSL. However, there is no corresponding library or IDE for application development using D language, therefore, D-language development projects are rare.
The fourth problem may cause the DSL syntax to be non-clean. For example, you have to add <to the regular expression, and it looks a little messy. This problem cannot be solved because the operator overload is not intended to have such abnormal functions.

C ++ is originally positioned as an underlying language with advanced features. When we develop applications, we still use upper-level languages, such as Java, C #, and Ruby, to connect to the database and draw web pages. What does C ++ do? I care about applications and how to use embedded XML and SQL in my current Java project. The disgusting part of the preparedstatement used to write SQL statements in Java is that I have to go to the question mark and set a parameter to have to go to the question mark, which is very annoying.

So here I would like to recommend Scala, which is compatible with Java bytecode, and can also generate CLI compatible with. Net, which can be seamlessly integrated into the existing system. In Scala, there is a database connection library that can embed SQL statements into the Code:

object foo extends Application {

import scala.dbc._
import scala.dbc.Syntax._
import syntax.Statement._

val db = database("jdbc:postgresql://localhost/test","myUserName","")

val res = db.executeStatement {
select fields ("name" of characterVarying(50)) from "person"
}

for(val i <- res;
val f <- i.fields) {
Console.println(f.content.sqlString)
} }

The preceding SQL statements are not supported by the native language, but implemented by libraries.
However, as long as you understand the implementation, you will know that it is not a good choice at all, because the scala syntax can omit the "." And function call brackets used by access members, so in fact the above sentence

select fields ("name" of characterVarying(50)) from "person"

Equivalent

select.fields("name" of characterVarying(50)).from("person");

This is just a function call.

Although Scala only uses some tricks to implement this SQL syntax, Scala is a strongly typed language and has many features such as operator overloading and automatic inference, without the defects of C ++, it is indeed easy to implement any DSL. In addition, the Java Community has argued that closure has been around for many years, and there are a lot of functional programming, which is a mix of Haskell and Java. It can do the daily work of Java, there are also advanced features that children will not use. Therefore, Scala is a very interesting and practical thing. It should be promising and worth noting.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.