瀏覽代碼

Add comments to some functions and parsefrom fix (hack one)

develop-theo
Theo Souchon 2 年之前
父節點
當前提交
2fdc3233dd
共有 6 個檔案被更改,包括 237 行新增162 行删除
  1. +51
    -18
      src/ccmeta.leg
  2. +172
    -140
      src/parsimonyLibrary/boot.mc
  3. +4
    -0
      src/parsimonyLibrary/dynamicObjectExtDecl.mc
  4. +6
    -2
      src/parsimonyLibrary/dynamicObjectParsFrom.mc
  5. +4
    -0
      src/tests/tests-parsimony/realObjectED.out
  6. +0
    -2
      src/tests/tests-parsimony/toFix/realObjectPF.c

+ 51
- 18
src/ccmeta.leg 查看文件

@ -1343,6 +1343,9 @@ language printLang = C;
oop outputProgram= 0;
/// @brief Check if everyExternalDeclaration is define in parsimony and if so call it with the s parameter
/// @param s
/// @return Return the parameter s
oop everyExternalDeclaration(oop s)
{
oop eed = map_get(globals, intern("everyExternalDeclaration"));
@ -1352,11 +1355,14 @@ oop everyExternalDeclaration(oop s)
return s;
}
/// @brief Check if the object correspond to what is expected
/// @param id
/// @param s
/// @return Return the parameter s
oop ensure(int id, oop s)
{
if (is(Map, s)) {
oop protoSymbol = map_get(s, __proto___symbol);
// int protoNumber = get(map_get(map_get(s, __proto___symbol), __name___symbol), Symbol, prototype);
switch(id) {
case t_C_id: {
if (map_get(protoSymbol, __name___symbol) != map_get(C_id_proto, __name___symbol)) { // map_get for tree copy because __name__ : C_id != __name__ : C_id
@ -1374,6 +1380,9 @@ oop ensure(int id, oop s)
return s;
}
/// @brief Make a copy of the tree in input
/// @param obj
/// @return The copy of the object
oop clone(oop obj)
{
switch(getType(obj)) {
@ -1397,6 +1406,9 @@ oop clone(oop obj)
return obj;
}
/// @brief Make a deep copy of the tree in input
/// @param obj
/// @return The deep copy of the object
oop treeCopy(oop obj)
{
switch(getType(obj)) {
@ -1423,6 +1435,10 @@ oop treeCopy(oop obj)
return obj;
}
/// @brief Evaluate the remaining Parsimony code not evaluated
/// @param scope
/// @param obj
/// @return The same object with everything evaluated inside
oop treeCopyUnquoting(oop scope, oop obj)
{
switch(getType(obj)) {
@ -1498,9 +1514,6 @@ hexQuad = hexadecimalDigit hexadecimalDigit hexadecimalDigit hex
# constant rule
# constant = META_AT @{puts("-----------------------------------------")}
# < [a-zA-Z][a-zA-Z0-9]* >
# x:@{ printf("<%s>\n", yytext); exit(0) } { $$ = x }
constant = characterConstant
| floatingConstant
| integerConstant
@ -1598,10 +1611,11 @@ sCharSequence = ( escapeSequence | !EOL [^\"\\] )* #"
# primaryExpression rule
primaryExpression = stringLiteral | constant | id
| META_AT ( META_LCB mstmts
| mvalue
)
primaryExpression = META_AT ( META_LCB mstmts
#| < [a-zA-Z][a-zA-Z0-9]* > @{ printf("<%s>\n", yytext); exit(0) }
| mvalue
)
| stringLiteral | constant | id
| 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) }
@ -2312,6 +2326,7 @@ mvalue = ( i:META_IDENT { i= newGetVariable(i) } ( a:meta_argumentList {
)
| META_LPAREN ( i:meta_exp ) META_RPAREN
) { $$= eval(globals, i) }
#) { i = eval(globals, i); puts("mvalue-yylval");println(i);$$= i }
mstmts = ( s:eval_stmt )* META_RCB { $$= s } # return the last stmt
@ -3684,6 +3699,7 @@ void readEvalPrint(oop scope, char *fileName)
int jbt= sigsetjmp(jbs->jb, 0);
if (0 == jbt) {
while (yyparse(&ctx)) {
// println(yylval);
if (!yylval) { // EOF
assert(top == inputStack);
break;
@ -3774,19 +3790,37 @@ oop prim_parseFrom(oop scope, oop params)
if (!is(String, name)) return null;
char *s = get(name, String, value);
assert(inputStack);
struct _yycontext *yy = inputStack->yy;
if (!strcmp(s, "declaration" )) return yylval = yyparsefrom(yy, yy_declaration ) ? __ : null ;
else if (!strcmp(s, "expression" )) return yylval = yyparsefrom(yy, yy_expression ) ? __ : null ;
else if (!strcmp(s, "statement" )) return yylval = yyparsefrom(yy, yy_statement ) ? __ : null ;
else if (!strcmp(s, "id" )) return yylval = yyparsefrom(yy, yy_id ) ? __ : null ;
else if (!strcmp(s, "func" )) return yylval = yyparsefrom(yy, yy_functionDefinition) ? __ : null ;
else if (!strcmp(s, "error" )) return yylval = yyparsefrom(yy, yy_error) ? __ : null ;
struct _yycontext *zz = inputStack->yy;
struct _yycontext ctx, *yy = &ctx;
memcpy(&ctx, zz, sizeof(struct _yycontext));
yy->__thunkslen= YY_STACK_SIZE;
yy->__thunks= (yythunk *)YY_MALLOC(yy, sizeof(yythunk) * yy->__thunkslen);
yy->__valslen= YY_STACK_SIZE;
yy->__vals= (YYSTYPE *)YY_MALLOC(yy, sizeof(YYSTYPE) * yy->__valslen);
yy->__thunkpos= 0;
int res;
if (!strcmp(s, "declaration" )) res = yyparsefrom(yy, yy_declaration );
else if (!strcmp(s, "expression" )) res = yyparsefrom(yy, yy_expression );
else if (!strcmp(s, "statement" )) res = yyparsefrom(yy, yy_statement );
else if (!strcmp(s, "id" )) res = yyparsefrom(yy, yy_id );
else if (!strcmp(s, "func" )) res = yyparsefrom(yy, yy_functionDefinition);
else if (!strcmp(s, "error" )) res = yyparsefrom(yy, yy_error );
else {
fprintf(stderr, "parseFrom: %s not implemented", s);
exit(1);
}
printf("%s expected\n", s);
return null;
oop value;
if (res) {
value = __;
zz->__buf= yy->__buf;
zz->__buflen= yy->__buflen;
zz->__pos= yy->__pos;
zz->__limit= yy->__limit;
} else {
printf("%s expected\n", s);
value = null;
}
return value;
}
#include <sys/resource.h>
@ -3810,7 +3844,6 @@ void outputText(char *text)
void outputNode(oop node)
{
if (!node) return;
switch (getType(node)) {
case Undefined:

+ 172
- 140
src/parsimonyLibrary/boot.mc 查看文件

@ -1,161 +1,193 @@
@{
println() {
apply(print, __arguments__);
print("\n");
__arguments__;
}
__requires__ = {};
require(__fileName__) {
if (__requires__[__fileName__] != null) {
return __requires__[__fileName__]
}
import(__fileName__);
__requires__[__fileName__]= scope();
return scope();
}
millis() {
microseconds() / 1000;
}
//------------ creation C structure ------------//
newId(x, comm) {
if (comm == null) {
{ identifier: x, __proto__: C_id };
} else {
{ identifier: x, comment: { text : comm, __proto__: Comment }, __proto__: C_id };
// create the function println
println(){
apply(print, __arguments__);
print("\n");
__arguments__;
}
}
newText(x) { { value: x, __proto__: C_string }; }
newComment(com) { { text : com, __proto__: Comment }; }
//--- Token ---//
__requires__ = {};
// Import the file
require(__fileName__)
{
if (__requires__[__fileName__] != null)
{
return __requires__[__fileName__]
}
import(__fileName__);
__requires__[__fileName__] = scope();
return scope();
}
// Get the current time in milliseconds
millis()
{
microseconds() / 1000;
}
newToken(txt, com) {
if (com == null) return { text: txt, comment: newComment(""), __proto__: Token };
{ text: txt, comment: newComment(com), __proto__: Token };
}
// Get the last element of s
last(s)
{
s[length(s) - 1];
}
newSemicolon(com) { newToken(";", com) }
// adding the element e at the end of the list l
append(s, e)
{
s[length(s)] = e;
}
newComma() { newToken(",", " "); }
// adding the element e at the end of the list l
push(s, e)
{
append(s, e);
}
newStar(com) { newToken("*", com); }
// remove the last element of the list s or remove the element e if not null
pop(s, e)
{
if (e == null)
{
e = length(s) - 1
}
ret = s[e];
t = {};
for (i in s)
{
if (i == e)
continue;
append(t, s[i]);
}
s = treeCopy(t);
ret;
}
newBinary(com) { newToken("=", com); }
//--- Declaration ---//
// add or don't change an element in a dictionnary
intern(s, e)
{
lo = 0;
hi = length(s) - 1;
while (lo <= hi)
{
mid = (lo + hi) / 2;
if (e > s[mid])
lo = mid + 1;
else if (e < s[mid])
hi = mid - 1;
else
return e;
}
for (i = length(s); i > lo; i = i - 1)
{
s[i] = s[i - 1];
}
return s[lo] = e;
}
newStruct() {
{
declarators: null,
specifier: null,
semicolon: newSemicolon("\n"),
__proto__: C_declaration
} // TO FIX
}
// merge the two list s1 and s2
fusion(s1, s2)
{
for (i in s2)
{
append(s1, s2[i]);
}
return s1;
}
newEnum(x) { { attributeL: null, expression: null, name: newId(x), __proto__: C_enum }; }
// If g then we add a filter on the application of the function f to each element of seq
map(f, seq, g)
{
out = {};
if (g)
{
for (i in seq)
{
e = seq[i];
if (g(e))
{
e = f(e);
}
append(out, e);
}
}
else
{
for (i in seq)
{
append(out, f(seq[i]));
}
}
out;
}
newFunction() {}
notToken(x){x.__proto__ != Token}
// Select element e of s if the the result of the application of f on e is true
select(f, s)
{
out = {};
for (i in s)
{
e = s[i];
if (f(e))
{
append(out, e);
}
}
out;
}
//-------------------- end ---------------------//
// Select element e of s if the the result of the application of f on e is false
reject(f, s)
{
out = {};
for (i in s)
{
e = s[i];
if (!f(e))
{
append(out, e);
}
}
out;
}
// Get the last element of s
last(s) {
s[length(s)-1];
}
//------------ C structure ------------//
newId(x, comm)
{
if (comm == null)
{
{identifier : x, __proto__ : C_id};
}
else
{
{identifier : x, comment : {text : comm, __proto__ : Comment}, __proto__ : C_id};
}
}
// adding the element e at the end of the list l
append(s, e) {
s[length(s)] = e;
}
newText(x) { {value : x, __proto__ : C_string}; }
push(s, e) {
append(s, e);
}
newComment(com) { {text : com, __proto__ : Comment}; }
pop(s, e) {
if (e == null) {
e = length(s) - 1
}
ret = s[e];
t = {};
for (i in s) {
if (i == e) continue;
append(t, s[i]);
}
s = treeCopy(t);
ret;
}
// add or don't change an element in a dictionnary
intern(s, e) {
lo = 0;
hi = length(s) - 1;
while (lo <= hi) {
mid = (lo+hi)/2;
if (e > s[mid]) lo = mid + 1;
else if (e < s[mid]) hi = mid - 1;
else return e;
newToken(txt, com)
{
if (com == null) return {text : txt, comment : newComment(""), __proto__ : Token};
{text : txt, comment : newComment(com), __proto__ : Token};
}
for (i = length(s); i > lo; i = i - 1) {
s[i] = s[i-1];
}
return s[lo] = e;
}
fusion(s1, s2) {
for (i in s2) {
append(s1, s2[i]);
}
return s1;
}
// If g then we add a filter on the application of the function f to each element of seq
map(f, seq, g) {
out = {};
if (g) {
for (i in seq) {
e = seq[i];
if (g(e)) { e = f(e); }
append(out, e);
}
} else {
for (i in seq) {
append(out, f(seq[i]));
}
}
out;
}
newSemicolon(com){newToken(";", com)}
notToken(x) { x.__proto__ != Token }
newComma() { newToken(",", " "); }
// Select element e of s if the the result of the application of f on e is true
select(f, s) {
out = {};
for (i in s) {
e = s[i];
if (f(e)) { append(out, e); }
}
out;
}
// Select element e of s if the the result of the application of f on e is false
reject(f, s) {
out = {};
for (i in s) {
e = s[i];
if (!f(e)) { append(out, e); }
}
out;
}
nil;
newStar(com) { newToken("*", com); }
newBinary(com) { newToken("=", com); }
newEnum(x) { {attributeL : null, expression : null, name : newId(x), __proto__ : C_enum}; }
//-------------------- end ---------------------//
nil;
}

+ 4
- 0
src/parsimonyLibrary/dynamicObjectExtDecl.mc 查看文件

@ -1,3 +1,7 @@
// This example serves to illustrate the possibility
// of adding the object aspect to C
// using the everyExternalDeclaration function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

+ 6
- 2
src/parsimonyLibrary/dynamicObjectParsFrom.mc 查看文件

@ -1,3 +1,7 @@
// This example serves to illustrate the possibility
// of adding the object aspect to C
// using the parseFrom primitive.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -166,7 +170,7 @@ typedef struct __oop *oop;
tmp[0] = makeln(`declaration struct @@(newId(id, " ")) *self = (struct @@(newId(id, " ")) *) __self;, 1);
decl.compoundS.expression = treeCopy(fusion(tmp, decl.compoundS.expression));
program.objects.function = "none";
nil;
decl;
}
constructor() {
@ -185,7 +189,7 @@ typedef struct __oop *oop;
append(decl.compoundS.expression, makeln(`statement return (struct __oop *) self;));
decl.compoundS.expression = fusion(t, decl.compoundS.expression);
program.objects.function = "none";
nil;
decl;
}
// TODO

+ 4
- 0
src/tests/tests-parsimony/realObjectED.out 查看文件

@ -1,3 +1,7 @@
// This example serves to illustrate the possibility
// of adding the object aspect to C
// using the everyExternalDeclaration function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

+ 0
- 2
src/tests/tests-parsimony/toFix/realObjectPF.c 查看文件

@ -26,7 +26,5 @@ int main()
{
oop p = newPoint(3, 4);
@send2 magnitude();
return 0;
}

Loading…
取消
儲存