From 06962d9a48482194d13571f06b35f6b37e16eda4 Mon Sep 17 00:00:00 2001 From: MaximeBarniaudy Date: Tue, 9 Jul 2024 14:53:38 +0900 Subject: [PATCH] add do-while and f-string dynamic addition proofs of concepts --- dowhile.meta | 56 +++++++++++++++++++++++++++++++++ fstring.meta | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ minproto.grammar | 2 +- 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 dowhile.meta create mode 100644 fstring.meta diff --git a/dowhile.meta b/dowhile.meta new file mode 100644 index 0000000..3d5c655 --- /dev/null +++ b/dowhile.meta @@ -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); + + } + +} diff --git a/fstring.meta b/fstring.meta new file mode 100644 index 0000000..901282d --- /dev/null +++ b/fstring.meta @@ -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"); +//} diff --git a/minproto.grammar b/minproto.grammar index ac1ff8f..172b147 100644 --- a/minproto.grammar +++ b/minproto.grammar @@ -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 }