Browse Source

Added constant, initializer and statement processing.

develop-theo
Theo Souchon 2 years ago
parent
commit
1f09f074a1
6 changed files with 94 additions and 91 deletions
  1. +58
    -42
      ccmeta.leg
  2. +1
    -0
      tests-parsimony/001.c
  3. +10
    -2
      tests-parsimony/002.c
  4. +25
    -1
      tests-parsimony/003.c
  5. +0
    -9
      tests-parsimony/004.c
  6. +0
    -37
      tests-parsimony/tmp.c

+ 58
- 42
ccmeta.leg View File

@ -5,6 +5,7 @@
#
# Last edited: 2022-09-30 16:23:05 by piumarta on zora-10.local
%{
/* compile: leg -o ccmeta.c ccmeta.leg
* cc -o ccmeta ccmeta.c -lgc -lm
@ -29,7 +30,7 @@
_DO(PostDecVariable) _DO(PostDecMember) _DO(PostDecIndex) \
_DO(GetVariable) _DO(GetMember) _DO(SetMember) _DO(GetIndex) _DO(SetIndex) \
_DO(Return) _DO(Break) _DO(Continue) _DO(Throw) _DO(Try) \
/* _DO(Quasiquote) _DO(Unquote) */ \
_DO(Quote) /* _DO(Quasiquote) _DO(Unquote) */ \
DO_C_PROTOS()
#define META_PROTO_MAX t_Try
@ -54,6 +55,7 @@ DO_PROTOS()
#undef _DO
} proto_t;
#define SYMBOL_PAYLOAD proto_t prototype;
#define DELTA 3
@ -1347,6 +1349,7 @@ hexQuad = hexadecimalDigit hexadecimalDigit hexadecimalDigit hex
constant = characterConstant
| floatingConstant
| integerConstant
#| META_AT m:meta_exp { $$ = m }
@ -1443,8 +1446,11 @@ sCharSequence = ( escapeSequence | !EOL [^\"\\] )*
# 6.5.1
primaryExpression = stringLiteral | constant | id
| META_AT META_LPAREN x:mexp { $$ = eval(globals, x) }
| l:LPAREN x:expression r:RPAREN { $$= new_C_subexpr(l, x, r) }
| META_AT ( META_LCB x:mstmt
| META_OPERATORS META_LPAREN x:mexp
| META_LPAREN x:mexp
) { $$ = eval(globals, x) }
| l:LPAREN x:expression r:RPAREN { $$= new_C_subexpr(l, x, r) }
| l:LPAREN x:compoundStatement r:RPAREN &{gnu} { $$= new_C_subexpr(l, x, r) }
# 6.5.2
@ -1825,6 +1831,7 @@ typedefName = &{ isTypedefName(yytext) } { $$= new_C_id(yytext) } -
initializer = l:LCURLY i:initializerList ( c:COMMA | {c=newNullObject()} ) r:RCURLY { $$= new_C_initializer(l, i, c, r) }
| assignmentExpression
| META_AT m:meta_exp { $$ = m }
initializerList = { listBegin() }
( d:designation { listAppend(d) }
@ -1869,18 +1876,18 @@ labeledStatement = i:id c:COLON
# 6.8.2
compoundStatement = @{ C_scopeBegin() }
l:LCURLY { listBegin() }
( x:localLabelDeclaration &{gnu} { listAppend(x) }
compoundStatement = @{ C_scopeBegin() }
l:LCURLY { listBegin() }
( x:localLabelDeclaration &{gnu} { listAppend(x) }
)*
( x:declaration { listAppend(x) }
| x:statement { listAppend(x) }
| x:functionDefinition &{gnu} { listAppend(x) }
( x:declaration { listAppend(x) }
| x:statement { listAppend(x) }
| x:functionDefinition &{gnu} { listAppend(x) }
| !'}' &{ errmsg= "statement expected" } error
)* { x= listEnd() }
r:RCURLY { $$= new_C_compound(l, x, r) }
)* { x= listEnd() }
r:RCURLY { $$= new_C_compound(l, x, r) }
@{ C_scopeEnd() }
| &{ C_scopeAbort() }
| &{ C_scopeAbort() }
# 6.8.3
@ -2151,9 +2158,10 @@ _FLOAT128 = '_Float128' !IDREST &{gnu} { $$= newToken("_Float1
# the semicolon has to be explicit with no space eaten afterwards to prevent the
# input buffer from moving past it before redirecting input from the imported file
metaCatch = META_AT ( META_LPAREN m:mexp { $$ = m }
| META_IMPORT s:META_STRING ";" { $$ = null; inputStackPush(get(s, String, value)) }
| META_LCB { $$ = null; lang= META }
metaCatch = META_AT ( META_OPERATORS META_LPAREN m:mexp { $$ = m }
| META_LPAREN m:mexp { $$ = m }
| META_IMPORT s:META_STRING ";" { $$ = null; inputStackPush(get(s, String, value)) }
| META_LCB { $$ = null; lang= META }
)
| &{ lang == META } - ( META_RCB { $$ = null; lang = C }
| s:meta_stmt { $$ = s }
@ -2162,6 +2170,9 @@ metaCatch = META_AT ( META_LPAREN m:mexp { $$ = m }
mexp = @{ lang = META } s:meta_exp META_RPAREN @{ lang = C } { $$= s }
| @{ lang = C } &{ 0 }
mstmt = @{ lang = META } s:meta_stmt META_RCB @{ lang = C } { $$= s }
| @{ lang = C } &{ 0 }
meta_stmt = s:meta_block { $$ = s }
| META_SEMICOLON { $$ = null }
| l:META_IDENT p:meta_paramList e:meta_block { $$ = newFunc(l, p, e, null) }
@ -2283,15 +2294,18 @@ meta_prod = l:meta_prefix
| META_MODULO r:meta_prefix { l = newBinary(Mod_proto, l, r) }
)* { $$ = l }
meta_prefix = META_PLUS n:meta_prefix { $$= n }
| META_NEGATE n:meta_prefix { $$= newUnary(Neg_proto, n) }
| META_TILDE n:meta_prefix { $$= newUnary(Com_proto, n) }
| META_PLING n:meta_prefix { $$= newUnary(Not_proto, n) }
| META_PLUSPLUS n:meta_prefix { $$= newPreIncrement(n) }
| META_MINUSMINUS n:meta_prefix { $$= newPreDecrement(n) }
# | META_BACKTICK n:meta_prefix { $$ = newUnary(Quasiquote_proto, n) }
# | META_AT n:meta_prefix { $$ = newUnary(Unquote_proto, n) }
| n:meta_postfix { $$= n }
meta_prefix = META_PLUS n:meta_prefix { $$= n }
| META_NEGATE n:meta_prefix { $$= newUnary(Neg_proto, n) }
| META_TILDE n:meta_prefix { $$= newUnary(Com_proto, n) }
| META_PLING n:meta_prefix { $$= newUnary(Not_proto, n) }
| META_PLUSPLUS n:meta_prefix { $$= newPreIncrement(n) }
| META_MINUSMINUS n:meta_prefix { $$= newPreDecrement(n) }
| META_BACKTICK META_INITIALIZER i:initializer { $$ = newUnary(Quote_proto ,i) }
| META_BACKTICK META_CONSTANT c:constant { $$ = newUnary(Quote_proto ,c) }
| META_BACKTICK META_STATEMENT c:statement { $$ = newUnary(Quote_proto ,c) }
# | META_BACKTICK n:meta_prefix { $$ = newUnary(Quasiquote_proto, n) }
# | META_AT n:meta_prefix { $$ = newUnary(Unquote_proto, n) }
| n:meta_postfix { $$= n }
meta_postfix = i:meta_value
( META_DOT s:META_IDENT a:meta_argumentList { i = newInvoke(i, s, a) }
@ -2318,8 +2332,7 @@ meta_argumentList = META_LPAREN m:meta_makeMap
meta_value = META_BACKTICK - m:meta2c_expr { $$ = m }
| n:META_FLOAT { $$ = newFloat(n) }
meta_value = n:META_FLOAT { $$ = newFloat(n) }
| n:meta_integer { $$ = newInteger(n) }
| s:meta_string { $$ = newString(s) }
| s:meta_symbol { $$ = s }
@ -2329,21 +2342,6 @@ meta_value = META_BACKTICK - m:meta2c_expr { $$
| p:meta_paramList e:meta_block { $$ = newFunc(null, p, e, null) }
| META_LPAREN ( i:meta_block | i:meta_exp ) META_RPAREN { $$ = i }
meta2c_statement = m:meta2c_expr { $$ = m }
| 'statement' { $$ = null }
meta2c_expr = m:meta2c_constant { $$ = m }
| 'expression' { $$ = null }
meta2c_constant = 'constant' - m:meta2c_value { $$ = m }
meta2c_value = m:meta_makeMap
( < [0-9]+ > { map_set(m, intern("__proto__"), intern("C_int")) } { map_set(m, intern("text"), makeString(unescape(yytext))) } { $$ = newMap(m) }
| s:meta2c_string { map_set(m, intern("__proto__"), intern("C_string")) } { map_set(m, intern("text"), s ) } { $$ = newMap(m) }
)
meta2c_string = META_DQUOTE < (!META_DQUOTE meta_char)* > META_DQUOTE {$$ = makeString(unescape(yytext)) }
meta_string = s:META_STRING - { $$ = s }
META_STRING = META_DQUOTE < (!META_DQUOTE meta_char)* > META_DQUOTE { $$ = makeString(unescape(yytext)) }
@ -2391,6 +2389,17 @@ META_FLOAT = < [-+]* [0-9]+ '.' [0-9]* ('e'[-+]*[0-9]+)? > - { $$ = ma
| < [-+]* [0-9]* '.' [0-9]+ ('e'[-+]*[0-9]+)? > - { $$ = makeFloat(strtold(yytext, 0)) }
| < [-+]* [0-9]+ ('e'[-+]*[0-9]+) > - { $$ = makeFloat(strtold(yytext, 0)) }
###### META operators ######
META_OPERATORS = META_INITIALIZER | META_CONSTANT | META_STATEMENT
META_INITIALIZER = 'initializer' ![(a-zA-Z0-9_] -
META_CONSTANT = 'constant' ![(a-zA-Z0-9_] -
META_STATEMENT = 'statement' ![(a-zA-Z0-9_] -
############################
#META_FUN = 'fun' ![a-zA-Z0-9_] -
#META_SYNTAX = 'syntax' ![a-zA-Z0-9_] -
META_VAR = 'var' ![a-zA-Z0-9_] -
@ -2851,6 +2860,10 @@ oop eval(oop scope, oop ast)
}
return map;
}
case t_Quote: {
oop obj = map_get(ast, rhs_symbol);
return obj;
}
#if 0
case t_Quasiquote: {
oop obj = map_get(ast, rhs_symbol);
@ -4554,6 +4567,9 @@ void outputTree(oop node, int depth)
CASE(GetVariable)
OUT(key)
break;
CASE(Quote)
OUT(rhs);
break;
/** TODO
* CASE(Quasiquote)
* PRINT(Quasiquote);
@ -4681,4 +4697,4 @@ int main(int argc, char **argv)
// Local Variables:
// indent-tabs-mode: nil
// End:
// End:

+ 1
- 0
tests-parsimony/001.c View File

@ -11,4 +11,5 @@
enum foo { FOO, BAR, BAZ };
char *x[] = @(newCinitializer([newCstring("X"), comma, newCstring("Y")]));
int i = @(newCint("42"));
int i = @({ __proto__: Token, text: "14" });
char *foos[] = { "FOO", "BAR", "BAZ" };

+ 10
- 2
tests-parsimony/002.c View File

@ -1,7 +1,15 @@
@{
saved = `initializer (1,2,"stringy thingy",21<<1);
cons = `constant 2;
}
@(a = `constant 10)
char t = @(`constant 't');
int i = @(cons);
int q = @(`constant 10);
int k = @(`initializer (1,2,"stringy thingy",21<<1));
int w = @(a);
char *text=@(`constant "text");
int i = @(saved);
int w = @(a);

+ 25
- 1
tests-parsimony/003.c View File

@ -1 +1,25 @@
int i = @({ __proto__: Token, text: "14" });
@{
nTimes = `constant 10;
forLoop = `statement for (int i = 0; i < 10; ++i) printf("%d\n", i); ;
whileLoop = `statement while (i < 10) { printf("%d\n", i); ++il; };
useForLoop = 1;
x = null;
}
@{forLoop = `statement for (int i = 0; i < 10; ++i) printf("%d\n", i);;}
@{if (useForLoop) { x = forLoop;} else { x = whileLoop; } }
int main()
{
@(x);
@(`statement for (int i = 0; i < @(nTimes); ++i) printf("%d\n", i););
@{if (useForLoop) {forLoop;} else {whileLoop;}}; // Impossible to modify useForLoop in a function
return 0;
}

+ 0
- 9
tests-parsimony/004.c View File

@ -1,9 +0,0 @@
@{if (1) print("yo");}
int main(){
if (1) {
return 0;
} else {
return 1;
}
}

+ 0
- 37
tests-parsimony/tmp.c View File

@ -1,37 +0,0 @@
@{
// File used only for rule creation help
newObject(type, fields) { fields.__proto__ = type; fields }
newCint(text) { newObject(C_int, { text: text }) }
newCstring(text) { newObject(C_string, { text: "\""+text+"\"" }) }
newCinitializer(list) { newObject(C_initializer, { leftCurly: lcurly, initList: list, rightCurly: rcurly }) }
//newCif(list) { newObject(C_if, {ifTok: iftok, lParen: lpar, condition: , oop rParen, oop consequent }) }
newCexpression() {}
//newCcondition() { newObject(C_conditional, {logicalOrExpression, question, expression, colon, conditionalExpression}) }
//newCbinary() { newObject() }
//newCcast() { newObject(C_cast, { lParen: lpar, typeName, rParen: rpar, expression }) }
//newCdeclaration() { newObject(C_declaration, {specifiers, declarators, semicolon: semicolon }) }
newCstatement() {}
newToken(text) { { __proto__: Token, text: text } }
comma = newToken(",");
semicolon = newToken(";");
lcurly = newToken("{");
rcurly = newToken("}");
lpar = newToken("(");
rpar = newToken(")");
iftok = newToken("if");
}
char *x[] = @(newCinitializer([newCstring("X"), comma, newCstring("Y")]));
int i = @(newCint("42"));

Loading…
Cancel
Save