Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Proposal] A more flexible and efficient syntax for variable assignment #285

Open
Lotayou opened this issue Dec 20, 2019 · 1 comment
Labels
syntax Suggestions to improve the syntax

Comments

@Lotayou
Copy link

Lotayou commented Dec 20, 2019

Issue with current version

The current assignment syntax seems cumbersome to me.

a = b translates to 昔之「甲」者。今「乙」是矣。

So, how about chain assignment like a = b = c = d?

昔之「丙」者。今「丁」是矣。
昔之「乙」者。今「丙」是矣。
昔之「甲」者。今「乙」是矣。

Also, how about self assignment like x=f(x)

施「法」之术于「斯」。昔之「斯」者。今其是矣。

What's even worse, this might lead to ambiguity when dealing with multiple assignment like x, y = f(x,y)

施「法」之术于「斯」于「外」。昔之「斯」者「外」者。今其是矣。

In my opinion, such Chinese syntax is hard to compile to equivalent programming codes and could lead to unecessary copy/assign operations. Can we think of something to make it more concise and efficient?

My proposal

I understand that a compromise must be made between Chinese fluency and execution efficiency. I can think of two more concise syntaxes.

Syntax 1

wenyan Python
赋「甲」以「乙」 a=b
赋「甲」以「乙」以「丙」以「丁」 a=b=c=d
施「法」之术于「斯」。以之赋「斯」。 x=f(x)
施「法」之术于「斯」于「外」。以之赋「斯」赋「外」。 x, y=f(x, y)

We use "以“ to represent the assignment operator "=",and for assigning the output of a function, we can use ”之“,which in my opinion can be reserved as a keyword of the wenyan-language.

However, this would definitely compromise efficiency, as x=f(x) is actually executed as _ = f(x), x = _ with one more copy and assignment.
I don't know if these two are equivalent in javascript. But there is this thing called right-value reference in C++ language, and such different would cost much if x is a very large object.

Also in python 3.8 a new feature called assignment operation is introduced, allow the assignment expression itself to be evaluated again. Say you want to read lines from a file and search for a special pattern, the traditional way would be

l = file.readline()
while l != 'result':
    l = file.readline()

With assignment operator, now it becomes

while (l := file.readline()) != 'result:
    pass

So here is the point. A bad syntax does not only affect readibility, it also drags down the efficiency of compiled codes.

Syntax 2

If we use ”于“ to indicate an assignment before the current expression, it could lead to improved efficiency for chain assignment and self assignment at the expense of readability (The chain order is reversed)

wenyan Python
赋「乙」于「甲」 a=b
赋「丁」于「丙」于「乙」于「甲」 a=b=c=d
施「法」之术于「斯」。赋于己。 x=f(x)

Syntax 3

I think the best way is to combine the merits of syntax 1 and 2, leading to more flexible balance between fluency and efficiency.

wenyan Python
赋「乙」于「甲」 or 赋「甲」以「乙」 a=b
赋「丙」以「丁」,于「乙」,于「甲」 a=b=c=d
施「法」之术于「斯」。赋于己。 x=f(x)
施「法」之术于「斯」于「外」。赋于「斯」「外」。 x=f(x)

Advanced topic

List comprehension

Say if you want to take entrywise square for a list of integers and return the squared list, a C++ learner would probably write this:

吾有一列,名之曰「数列」。充「数列」以一以二以三
吾有一列,名之曰「方列」。
凡「数列」中之「元」。
    乘「元」以「元」。名之曰「方」。充「方列」以「方」。
云云
intlist = [1,2,3]
squarelist = []
for item in intlist:
    square = item * item
    squarelist.append(item)

Is there a more elegant way to do this thing in wenyan? As you may know, Python support list comprehension by mapping a function to every entry of a list. Here we can achieve the same thing with much concise and elegant wenyan

凡「数列」中之「元」。乘「元」以「元」。充于「方列」  # squarelist = [i*i for i in intlist]

This shows the power of ”于“ keyword,where the key is to re-use the return value of a math operation or a function for chained assignment.

Conclusion

The grammar discrepancy is the key issue for designing a Chinese programming language and the corresponding compiler. Existing solutions either directly copy the syntax of an existing language (like 易语言)or drop some complicated syntax supports to maintain chinese fluency. It's still a challenge to design proper syntax for the wenyan language. Python would be the ideal target language for a Chinese wenyan compiler. Since it has more flexible grammar and dynamic data type check. And to fully utilize the grammer trick, a effective way is to use keyword ”于“ to invoke right-value assignment.

Hope my discussion aids your development of THE BEST programming language in Chinese!

@Lotayou
Copy link
Author

Lotayou commented Dec 20, 2019

Nested Function calling

#155 @UltimatePea proposed that Nested function calling like var e = (f (g a) b (h c d)) or in python e = f(g(a), b, h(c,d)), can be implemented by introducing temporary variables

施「庚」於「甲」名之曰「其一」。
施「辛」於「丙」於「丁」名之曰「其二」。
施「己」於「其一」於「乙」於「其二」名之曰「戊」。

or using lambda expressions

施「己」
於數。欲得是數,施「庚」於「甲」。
於「乙」
於數。欲得是數,施「辛」於「丙」於「丁」。

Actually by simulating the stack of functions, there is a third solution, which allow a serialized syntax that is more elegant and doesn't involve complex syntax like lambda expression. The compilation should not be to hard with the help of an extra stack.

入「甲」求「庚」入「乙」「丙」「丁」取二求「辛」求「己」得「戊」

We have four keywords in the above expression:

  • 入[x]:push an item x into the stack

  • 求[f]:receives a function f, pops out elements from the stack for evaluation, and push the result back into the stack. By default, all the elements from the stacks are poped out as function arguments.

  • 取[n]求[f]:Only pop the top n items for function evaluation

  • 得[x]:pop the top of the stack and assign it to variable x

The execution procedure is as follows:

operation Stack expression
入「甲」 a a
求「庚」 _1 g(a)
入「乙」「丙」「丁」 _1, b, c, d g(a), b, c, d
取二求「辛」 _1, b, _2 g(a), b, h(c, d)
求「己」 _3 f(g(a), b, h(c, d))
得「戊」 empty e = f(g(a), b, h(c, d))

Examples

python 文言
e = f(g(a), b, h(c, d)) 入「甲」求「庚」入「乙」「丙」「丁」取二求「辛」求「己」得「戊」
e = d(c(b(a))) 入「甲」求「乙」「丙」「丁」得「戊」
x = f(e, d(c,x), b(a,x)) 入「戊」「丙」「斯」取二求「丁」入「甲」「斯」取二求「乙」求「己」得「斯」

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
syntax Suggestions to improve the syntax
Projects
None yet
Development

No branches or pull requests

2 participants