【发布时间】:2011-01-31 12:16:45
【问题描述】:
我目前正在开发一种用于在连续环境中进行编程的新语言(将其与电气工程相比),并且我对某种语言构造有了一些想法。
让我先解释一下,然后再定义一下:
x = a U b;
其中x 是一个变量,a 和b 是其他变量(或静态值)。这就像 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 或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.
}
更新 2:更多示例和更多上下文。
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”语句都是一个同步代理,处理它的特定对象“过滤器”。这不是基于类型,而是基于变量(基本相同;只是识别对象的不同方式)。
非常感谢大家为我提供了非常有见地的信息以及指向我可以研究的重要主题的链接/提示。谢谢。
我不知道这种结构是否已经存在,所以这是我的问题:这种语言功能是否已经存在?
【问题讨论】:
-
AND/OR 在布尔意义上与 OR 相同。
-
那么使用“和/或”术语可能不是一个好主意——也许你可以使用一个不那么模棱两可的术语,并暗示你真正想要达到的目标?
-
除了使用您承认不完全准确的词外,您还没有具体说明您的意思。尝试给出一些具有预期输出的示例...
-
听起来您正在寻找一种高度声明性的语言,而不是一种程序性语言。我为你必须这样做的大脑空间鼓掌!
-
我觉得如果把“if”换成“with”会清晰很多
标签: operators language-features dataflow