我目前正在开发一种用于在连续环境中进行编程的新语言(将其与电气工程进行比较),并且我对某种语言构造有了一些想法。
让我通过解释然后通过定义来解释该功能:
x = a U b;
wherex
是变量,a
andb
是其他变量(或静态值)。这就像 and 之间的联合a
一样b
;没有重复,也没有特定的顺序。
with(x) {
// regular 'with' usage; using the global interpretation of "x"
x = 5;
// effectively will do:
// x = a U b U 5;
// a = 5;
// b = 5;
// Thus, when "a" or "b" changes, "x" is still equal to "5".
}
with(x = a) {
// this code block is executed when the "x" variable
// has the "a" variable assigned. All references in
// this code-block to "x" are references to "a". So saying:
x = 5;
// would only change the variable "a". If the variable "a"
// later on changes, x still equals to 5, in this fashion:
// 'x = a U b U 5;'
// '[currentscope] = 5;'
// thus, 'a = 5;'
}
with(x = b) {
// same but with "b"
}
with(x != a) {
// here the "x" variable refers to any variable
// but "a"; thus saying
x = 5;
// is equal to the rewriting of
// 'x = a U b U 5;'
// 'b = 5;' (since it was the scope of this block)
}
with(x = (a U b)) {
// guaranteed that "x" is 'a U b'; interacting with "x"
// will interact with both "a" and "b".
x = 5;
// makes both "a" and "b" equal to 5; also the "x" variable
// is updated to contain:
// 'x = a U b U 5;'
// '[currentscope] = 5;'
// 'a U b = 5;'
// and thus: 'a = 5; b = 5;'.
}
// etc.
在上面,所有代码块都被执行,但是每个块中的“范围”会发生变化,如何x
解释。在第一个块中,x
保证为a
: 因此与x
该块内部的交互将在 上交互a
。只有在这种情况下,第二个和第三个代码块才相等(因为not a
: 那么只剩下b
)。最后一个块保证x
至少是a
or b
。
此外; U
不是“按位或运算符”,但我称它为“和/或”运算符。它的定义是:
"U" = "and" U "or"
(在我的博客http://cplang.wordpress.com/2009/12/19/binop-and-or/上,有更多关于此运算符的(数学)背景信息。它松散地基于集合。使用不同的语法,在这个问题中改变了它。)
更新:更多示例。
print = "Hello world!" U "How are you?"; // this will print
// both values, but the
// order doesn't matter.
// 'userkey' is a variable containing a key.
with(userkey = "a") {
print = userkey; // will only print "a".
}
with(userkey = ("shift" U "a")) {
// pressed both "shift" and the "a" key.
print = userkey; // will "print" shift and "a", even
// if the user also pressed "ctrl":
// the interpretation of "userkey" is changed,
// such that it only contains the matched cases.
}
with((userkey = "shift") U (userkey = "a")) {
// same as if-statement above this one, showing the distributivity.
}
x = 5 U 6 U 7;
y = x + x; // will be:
// y = (5 U 6 U 7) + (5 U 6 U 7)
// = 10 U 11 U 12 U 13 U 14
somewantedkey = "ctrl" U "alt" U "space"
with(userkey = somewantedkey) {
// must match all elements of "somewantedkey"
// (distributed the Boolean equals operated)
// thus only executed when all the defined keys are pressed
}
with(somewantedkey = userkey) {
// matches only one of the provided "somewantedkey"
// thus when only "space" is pressed, this block is executed.
}
Update2:更多示例和更多上下文。
with(x = (a U b)) {
// this
}
// can be written as
with((x = a) U (x = b)) {
// this: changing the variable like
x = 5;
// will be rewritten as:
// a = 5 and b = 5
}
一些背景信息:我正在构建一种“与时间无关”的语言,例如 Java 是“与平台无关的”。语言中陈述的一切都是“原样”,并且不断地积极执行。这表示; 程序员不知道元素的顺序(除非使用构造明确说明),也不知道语句何时执行。该语言与“时间”概念完全分离,即它是连续执行的:
with(true) {
a = 0; // only runs once (lazy execution)
}
with(a < 5) {
a++;
} // this is a loop-structure;
// how and when it's executed isn't known however.
with(a) {
// everytime the "a" variable changes, this code-block is executed.
with(true) {
b = 3; // only 5 times (again lazy execution, but it's a sub-with)
}
with(b < 2) { // dependent on "b"
// runs only 3 times * 5 times = 15 times.
}
with(b > 1) { // dependent on "b"
b = b - 1; // runs 4 times * 5 times = 20 times.
}
}
更新 3:
琢磨了一下这个语言特征的类型;它与 Netbeans Platform 的 Lookup 非常相似,其中每个“with”语句都是一个同步代理,处理它的特定对象“过滤器”。这不是基于类型,而是基于变量(基本上完全相同;只是识别对象的不同方式)。
我非常感谢你们为我提供了非常有见地的信息以及我可以研究的重要主题的链接/提示。谢谢。
我不知道这种结构是否已经存在,所以这是我的问题:这种语言特性是否已经存在?