Selaa lähdekoodia

Migrate everything to peg vm

master
MaximeBarniaudy 10 kuukautta sitten
vanhempi
commit
c2da606329
5 muutettua tiedostoa jossa 243 lisäystä ja 492 poistoa
  1. +1
    -4
      dowhile.meta
  2. +18
    -17
      fstring.meta
  3. +192
    -443
      grammar_parser.meta
  4. +1
    -1
      minproto.grammar
  5. +31
    -27
      rawgrammar.leg

+ 1
- 4
dowhile.meta Näytä tiedosto

@ -25,10 +25,7 @@ Do.__codeon__(str) {
// 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);
metaGrammar.stmt.prepend("\"whatever i want\" - b:stmt WHILE LPAREN c:expr RPAREN EOS { $$ = Do.new(b, c); }");
{

+ 18
- 17
fstring.meta Näytä tiedosto

@ -32,37 +32,36 @@ FormatString.__codeon__(str) {
str.push("\"");
}
__namespaces__.metaLanguage.primary.prepend("fstring");
nonSpaceEatingId = parseDefinition(
"nonSpaceEatingId = < LETTER ALNUM* > { intern(yytext) }
"nonSpaceEatingId = < LETTER ALNUM* > { $$ = intern(yytext) }
");
nonSpaceEatingBlock = parseDefinition(
"nonSpaceEatingBlock = LBRACE b:mkobj
( e:stmt { b.push(e) }
)* \"}\" { b }
( e:stmt { $$ = b.push(e) }
)* \"}\" { $$ = b }
");
fStringChar = parseDefinition(
"fStringChar = < (!\"\\\"\" !\"$\" char )+ > { yytext }
| \"\\\\$\" { \"$\" }
"fStringChar = < (!\"\\\"\" !\"$\" char )+ > { $$ = yytext.unescaped() }
| \"\\\\$\" { $$ = \"$\" }
");
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) }
( \"$\" 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);
metaGrammar.addRule(nonSpaceEatingBlock.name, nonSpaceEatingBlock.expression);
metaGrammar.addRule(nonSpaceEatingId.name, nonSpaceEatingId.expression);
metaGrammar.addRule(fStringChar.name, fStringChar.expression);
metaGrammar.addRule(fStringRule.name, fStringRule.expression);
metaGrammar.primary.prepend("fstring");
formatInt(integer) {
if (integer > 1000) {
@ -71,10 +70,12 @@ formatInt(integer) {
return f"$integer";
}
a = 69;
print(f"Reimu has $a power and ${6*7} point items and \$${ formatInt(1 << 24) } money.\n");
print(f"Reimu has $a power and ${6*7} point items and \$${ formatInt(1 << 32) } 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");
//}

+ 192
- 443
grammar_parser.meta
File diff suppressed because it is too large
Näytä tiedosto


+ 1
- 1
minproto.grammar Näytä tiedosto

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

+ 31
- 27
rawgrammar.leg Näytä tiedosto

@ -1,50 +1,54 @@
grammar = - ( d:definition { d; }
| end-of-file { nil; }
grammar = - ( d:definition { global yysval = d; }
| end-of-file { global yysval = nil; }
)
definition = i:identifier ASSIGN e:expression SEMI? { Definition.new(name: i, expression: e); }
definition = i:identifier ASSIGN e:expression SEMI? { $$ = Definition.new(name: i, expression: e); }
expression = s:sequence !BAR { s; }
expression = s:sequence !BAR { $$ = s; }
| s1:sequence { s1 = Alternation.new().push(s1); }
( BAR s2:sequence { s1.push(s2); }
) * { s1; }
) * { $$ = s1; }
startSequence = - s:sequence { global yysval = s; }
sequence = p:prefix ( q:prefix { p = Sequence.new().push(p).push(q); }
( q:prefix { p.push(q); }
) * ) ? { p; }
) * ) ? { $$ = p; }
prefix = AND a:action { ParseTimeAction.new(action: a); }
| AT a:action { ExecuteAction.new(action: a); }
prefix = AND a:action { $$ = ParseTimeAction.new(action: a); }
| AT a:action { $$ = ExecuteAction.new(action: a); }
|
( AND s:suffix { And.new(expression: s); }
| NOT s:suffix { Not.new(expression: s); }
| s:suffix { s; }
( AND s:suffix { $$ = And.new(expression: s); }
| NOT s:suffix { $$ = Not.new(expression: s); }
| s:suffix { $$ = s; }
)
suffix = p:primary
( QUERY { p = Optional.new(expression: p); }
| STAR { p = Star.new(expression: p); }
| PLUS { p = Plus.new(expression: p); }
) ? { p; }
) ? { $$ = p; }
primary = i1:identifier COLON i2:ruleCall !ASSIGN { Assignment.new(name: i1, rule: i2); }
| i:ruleCall !ASSIGN { i; }
| LPAREN e:expression RPAREN { e; }
| l:literal { l; }
| c:class { c; }
| DOT { Dot.new(); }
| a:action { a; }
| BEGIN e:expression END { Capture.new(expression: e); }
primary = i1:identifier COLON i2:ruleCall !ASSIGN { $$ = Assignment.new(name: i1, rule: i2); }
| i:ruleCall !ASSIGN { $$ = i; }
| LPAREN e:expression RPAREN { $$ = e; }
| l:literal { $$ = l; }
| c:class { $$ = c; }
| DOT { $$ = Dot.new(); }
| a:action { $$ = a; }
| BEGIN e:expression END { $$ = Capture.new(expression: e); }
identifier = < [-a-zA-Z_][-a-zA-Z_0-9]* > - { intern(yytext); }
identifier = < [-a-zA-Z_][-a-zA-Z_0-9]* > - { $$ = intern(yytext); }
ruleCall = n:identifier CCOLON r:identifier { NamespacedRuleCall.new(namespace: n, name: r); }
| i:identifier { RuleCall.new(name: i); }
ruleCall = n:identifier CCOLON r:identifier { $$ = NamespacedRuleCall
.new(namespace: GetVar.new(name: n).__eval__(), name: r);
}
| i:identifier { $$ = RuleCall.new(name: i); }
literal = ['] < ( !['] char )* > ['] - { StringLiteral.new(string: yytext); }
| ["] < ( !["] char )* > ["] - { StringLiteral.new(string: yytext); }
literal = ['] < ( !['] char )* > ['] - { $$ = StringLiteral.new(string: yytext.unescaped()); }
| ["] < ( !["] char )* > ["] - { $$ = StringLiteral.new(string: yytext.unescaped()); }
class = '[' < ( !']' range )* > ']' - { CharacterClass.new(value: yytext); }
class = '[' < ( !']' range )* > ']' - { $$ = CharacterClass.new(value: yytext.unescaped()); }
range = char '-' char | char
@ -53,7 +57,7 @@ char = '\\' [abefnrtv'"\[\]\\]
| '\\' [0-7][0-7]?
| !'\\' .
action = m:metaLanguage::block - { Action.new(parseTree: Block.new(body: m)); }
action = m:metaGrammar::block - { $$ = Action.new(parseTree: Block.new(body: m)); }
- = ( space | comment )*
space = ' ' | '\t' | end-of-line

Ladataan…
Peruuta
Tallenna