Przeglądaj źródła

add do-while and f-string dynamic addition proofs of concepts

master
MaximeBarniaudy 10 miesięcy temu
rodzic
commit
06962d9a48
3 zmienionych plików z 137 dodań i 1 usunięć
  1. +56
    -0
      dowhile.meta
  2. +80
    -0
      fstring.meta
  3. +1
    -1
      minproto.grammar

+ 56
- 0
dowhile.meta Wyświetl plik

@ -0,0 +1,56 @@
Do = Object.subtype(#Do);
Do.new(body, cond) {
self = super.new();
self.body = body;
self.cond = cond;
self;
}
Do.__eval__(env) {
local result = self.body.__eval__(env);
while (self.cond.__eval__(env)) {
result = self.body.__eval__(env);
}
result;
}
Do.__codeon__(str) {
str.push("do ");
self.body.__codeon__(str);
str.push(" while ( ");
self.cond.__codeon__(str);
str.push(" )");
}
// parse the grammar expression
// put it at the beginning of stmt (which is an alternation)
__namespaces__.metaLanguage.stmt.prepend("\"whatever i want\" - b:stmt WHILE LPAREN c:expr RPAREN EOS { Do.new(b, c); }");
// regenerate the expression to be executed
metaLanguage.addRule(#stmt, __namespaces__.metaLanguage.stmt);
{
local i = 1;
{
whatever i want {
i *= 2;
print(i, "\n");
} while (i < 1000);
whatever i want {
i *= 2;
print(i, "\n");
} while (i < 1000);
whatever i want {
i *= 2;
print(i, "\n");
} while (i < 1000);
}
}

+ 80
- 0
fstring.meta Wyświetl plik

@ -0,0 +1,80 @@
FormatString = Object.subtype(#FormatString);
FormatString.new(elements) {
self = super.new();
self.elements = elements;
self;
}
FormatString.__eval__(env) {
local result = "";
for(elem in self.elements) {
local elemval = elem.__eval__(env);
if (elemval.__name__ == #String) {
result.push(elemval);
} else {
elemval.__codeon__(result);
}
}
result;
}
FormatString.__codeon__(str) {
str.push("f\"");
for(elem in self.elements) {
if (elem.__name__ == #String) {
str.push(elem.escaped());
} else {
str.push("$");
elem.__codeon__(str);
}
}
str.push("\"");
}
__namespaces__.metaLanguage.primary.prepend("fstring");
nonSpaceEatingId = parseDefinition(
"nonSpaceEatingId = < LETTER ALNUM* > { intern(yytext) }
");
nonSpaceEatingBlock = parseDefinition(
"nonSpaceEatingBlock = LBRACE b:mkobj
( e:stmt { b.push(e) }
)* \"}\" { b }
");
fStringChar = parseDefinition(
"fStringChar = < (!\"\\\"\" !\"$\" char )+ > { yytext }
| \"\\\\$\" { \"$\" }
");
fStringRule = parseDefinition(
"fstring = \"f\\\"\" elements:mkobj
( \"$\" i:nonSpaceEatingId { elements.push(GetVar.new(name: i)) }
| \"$\" b:nonSpaceEatingBlock { elements.push(Block.new(body: b)) }
| c:fStringChar { elements.push(c) }
)* \"\\\"\" { FormatString.new(elements) }
");
// regenerate the expression to be executed
metaLanguage.addRule(nonSpaceEatingBlock.name, nonSpaceEatingBlock.expression);
metaLanguage.addRule(nonSpaceEatingId.name, nonSpaceEatingId.expression);
metaLanguage.addRule(fStringChar.name, fStringChar.expression);
metaLanguage.addRule(fStringRule.name, fStringRule.expression);
metaLanguage.addRule(#primary, __namespaces__.metaLanguage.primary);
formatInt(integer) {
if (integer > 1000) {
return f"${formatInt((integer - integer % 1000) / 1000)},${integer % 1000}";
}
return f"$integer";
}
a = 69;
print(f"Reimu has $a power and ${6*7} point items and \$${ formatInt(1 << 24) } money.\n");
n = 100;
print(f"Sum of integers from 1 to $n : ${local sum = 0; for (i in n + 1) sum += i}\n");
print(f"${formatInt.function}\n");
//{
// print(f"${__env__()}\n");
//}

+ 1
- 1
minproto.grammar Wyświetl plik

@ -1,6 +1,6 @@
# minproto.leg -- minimal prototype langauge for semantic experiments
#
# last edited: 2024-07-04 10:07:00 by piumarta on zora
# last edited: 2024-07-05 17:16:16 by piumarta on zora-1034.local
start = - ( s:stmt { global yysval = s }

Ładowanie…
Anuluj
Zapisz