Scheme translated into Java and C ++ tools

First, why write this tool?

The company has several projects need to work with the contents of a function, and these projects, some in Java, with some C ++, as well as some real conditions, not all projects are calling a unified service interface (such as: may not run the case of the network), so there are two options:

a. respectively, to achieve the same two features in Java and C ++.

B. realize a generation tool for generating the same logic Java and C ++ code.

Now the project has just begun, not sure which one to use in the future of these two programs, but I still need to prepare in advance.

 

Second, some of the content of this article does not

I do not want involved in a technical article to any business, and in order to avoid any kind of sensitive corporate information leakage, so this key code for this tool does not appear, namely:

Test code a. The code in this article is written for the convenience of description, but will not contain real project code.

b. This article talk about technology, without involving business processes.

 

Third, why do with the Scheme for the source language?

First of all to say about the source language is a Scheme-like language, rather than the Scheme itself, but the syntax is very similar.

So why do form Scheme for the source language? Java does not directly use Java would just need to be translated into C ++ yet?

I chose to use Scheme because:

a. The language is a dynamically typed language, it does not appear the type of object in a statement on grammar.

b.Scheme-like is actually a lisp-like (S expression), this syntax is very simple, and the various language elements highly unified, very easy to implement.

Based on the above two points, later if we write a graphical tool to generate the language (used directly for editing algorithm to the business side of the user) will be relatively easy - If, after writing a GUI tool for editing the algorithm is directly generated Java the code is a bit too difficult, although you can still do it, but the effort may be 10 times or even ten times the gap. Therefore, in order to facilitate possible future deal, and now the realization of language itself convenience, here the choice of a "seemingly" no type of language, grammar and syntax to do is simply better. At the same time, because this whole language is what I was doing, so after more expressive increase on the basis of current grammar and syntax above will not be difficult.

 

Fourth, on the type

Scheme language is a dynamically typed language, and my goal is language Java and C ++ two statically typed language, so this Scheme-like language is only in the form of dynamic languages, and this system must be on or before a statically typed language Row. So now that there is no statement about the type of language in the text, then we know how to translate after Java and C ++ what type it?

Type identification into the following situations:

a. service code interfaces directly with out the easiest ...... subsequent supplements.

B. Internal function ...... subsequent variable definitions added.

. C for an internal function, the situation is the most complex, such as: (define (fun ma) (ma)) m can be seen that such a function is a function (or the lambda), which accepts a parameter a, but we have from this function can not determine what type a, m is the return value of yes. This type Scheme which is also when running only when it is certain, but we need to translate directly to static code, so we actually have to go through some means to determine the type of follow-up ...... add.

d.……

 

Fifth, the expression and function

In Scheme, are all expressions of value, so we can write code like this: (let ((a (if (<1 2) 1 2))) a), and in C ++ and Java expressions there is no value, so we can not write this: double x = {if (true) 1; else 2;}, so grammatically map itself is too big difference. To simplify development, or to function in Scheme, corresponds to the realization of a function in Java and C ++ code, which would make this translation Biro easy.

For example: In Scheme if a function is achieved these interfaces <T> T _if (Supplier <Boolean> cond, Supplier <T> exeBody1, Supplier <T> exeBody2) in Java; achieve such a in C ++ function template <class T> T _if (std :: function <bool ()> cond, std :: function <T ()> exeBody1, std :: function <T ()> exeBody2); - as used herein, lambda parameter formula the reason will be mentioned later.

Another example: In Scheme + is a function, such interface is achieved a double add (double d1, double d2) In Java, a function to achieve the same also in C ++.

and many more.

Implementation of these functions are one-line, C ++ compiler can all be optimized using inline way out, so it will not have any performance problems, and the Java language does not do this kind of action inline, but I do not know JVM It will perform inline processing at runtime.

 

VI, on the delay calculation

The main delay is calculated in two different ways:

……

 

7, on short circuit

……

 

Today, a little tired, finished another day to fix it.

Reproduced in: https: //www.cnblogs.com/naturemickey/p/4674924.html

Guess you like

Origin blog.csdn.net/weixin_33840661/article/details/93461765