私は現在、継続的な環境でプログラミングするための新しい言語を開発しています (電気工学と比較してください)。特定の言語構築についていくつかのアイデアがあります。
機能を説明で説明し、次に定義で説明しましょう。
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
ます。2 番目と 3 番目のコード ブロックは、この状況でのみ等しくなります (なぜならnot a
: しか残っていないからですb
)。x
最後のブロックは、少なくともa
またはであることを保証しb
ます。
さらに; U
は「ビットごとの or 演算子」ではありませんが、「and/or」演算子と呼んでいます。その定義は次のとおりです。
"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 プラットフォームの Lookup によく似ています。ここで、同期されたエージェントの各「with」ステートメントは、オブジェクトの特定の「フィルター」に取り組んでいます。型ベースではなく、変数ベースです (基本的にはまったく同じで、オブジェクトを識別する方法が異なるだけです)。
非常に洞察に満ちた情報と、私が研究できる素晴らしいトピックへのリンク/ヒントを提供してくれた皆さんに心から感謝します. ありがとう。
この構造がすでに存在するかどうかはわかりません。それが私の質問です。この言語機能はすでに存在しますか?