Bläddra i källkod

Trying to implement constants, and TODO in object.c in order to understand why does isInteger cause a seg fault

master
Nathan R 3 år sedan
förälder
incheckning
7220b23482
2 ändrade filer med 79 tillägg och 49 borttagningar
  1. +78
    -48
      ccmeta.leg
  2. +1
    -1
      object.c

+ 78
- 48
ccmeta.leg Visa fil

@ -29,9 +29,10 @@
_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(Quasiquote) _DO(Unquote) */ \
_DO(Comment) _DO(Token) \
_DO(C_if)
_DO(C_if) \
_DO(C_int)
typedef enum {
t_UNDEFINED=0,
@ -81,7 +82,7 @@ oop globals= 0;
_DO(update) _DO(this) _DO(fixed) _DO(operator) _DO(map) _DO(func) \
_DO(try) _DO(catch) _DO(finally) _DO(exception) \
_DO(__line__) _DO(__file__) \
_DO(comment) \
_DO(comment) \
_DO(text) _DO(if) _DO(lparen) _DO(rparen) _DO(else)
@ -318,6 +319,12 @@ oop newSymbol(oop name)
return symbol;
}
oop new_C_int(int value) {
oop integer = newObject(C_int_proto);
map_set(integer, value_symbol, makeInteger(value));
return integer;
}
oop newInteger(oop value)
{
oop integer = newObject(Integer_proto);
@ -698,36 +705,37 @@ hexQuad = hexadecimalDigit hexadecimalDigit hexadecimalDigit hexadecimalDigit
#|###
#|
#|### A.1.5 Constants
#|
#|# 6.4.4
#|
#|constant = characterConstant | floatingConstant | integerConstant
#|
#|# 6.4.4.1
#|
#|integerConstant = < ( hexadecimalConstant
#| | octalConstant
#| | binaryConstant &{gnu}
#| | decimalConstant
#| ) integerSuffix? > { $$= newInt(yytext) } -
#|
#|decimalConstant = [1-9][0-9]*
#|
#|octalConstant = '0'[0-9]*
#|
#|hexadecimalConstant = hexadecimalPrefix [a-fA-F0-9]+
#|
#|binaryConstant = '0'[bB][01]+
#|
#|hexadecimalPrefix = '0'[xX]
#|
#|octalDigit = [0-7]
# 6.4.4
constant = integerConstant { $$ = new_C_int(atoi(yytext)); }
#| | floatingConstant | characterConstant
# 6.4.4.1
integerConstant = < ( hexadecimalConstant
| octalConstant
| binaryConstant &{gnu}
| decimalConstant
) integerSuffix? >
decimalConstant = [1-9][0-9]*
octalConstant = '0'[0-9]*
hexadecimalConstant = hexadecimalPrefix [a-fA-F0-9]+
binaryConstant = '0'[bB][01]+
hexadecimalPrefix = '0'[xX]
octalDigit = [0-7]
hexadecimalDigit = [0-9A-Fa-f]
#|integerSuffix = ( [uU][lL]?[lL]? | [lL][lL]?[uU]? ) ( imaginarySuffix &{gnu} )?
#|
#|imaginarySuffix = [ij]
integerSuffix = ( [uU][lL]?[lL]? | [lL][lL]?[uU]? ) ( imaginarySuffix &{gnu} )?
imaginarySuffix = [ij]
#|
#|# 6.4.4.2
#|
@ -1258,11 +1266,12 @@ hexadecimalDigit = [0-9A-Fa-f]
#|
externalDeclaration = <Space+> { yylval= newComment(yytext); }
| ( SEMI &{gnu}
| c:constant { yylval = c;} #| TODO
#| | declaration
#| | functionDefinition
#| | meta
| &. &{ errmsg= "declaration expected" } error
) { yylval= $$; }
) { yylval= $$; }
#|functionDefinition = @{ declarationBegin() }
#| ( s:functionDeclarationSpecifiers | &{gnu} {s=0} )
@ -2566,16 +2575,30 @@ void outputText(char *text)
ocol += strlen(text);
}
void outputInt(int value) {
printf("%i", value);
char *toString = malloc(sizeof(value));
sprintf(toString,"%d", value);
ocol += strlen(toString);
}
void outputNode(oop node)
{
if (!node) return;
switch (node->type) {
case Undefined: return;
case String: outputText(get(node, String, value)); return;
case Map: break;
default:
fprintf(stderr, "\noutputNode: unknown node type %i\n", node->type);
abort();
case Undefined:
return;
case String:
outputText(get(node, String, value));
return;
case Map:
break;
case Integer:
outputInt(getInteger(node));
return;
default:
fprintf(stderr, "\noutputNode: unknown node type %i\n", node->type);
abort();
}
assert(is(Map, node));
oop proto= map_get(node, __proto___symbol);
@ -2583,17 +2606,24 @@ void outputNode(oop node)
// proto_number is the enum version of the proto symbol
proto_t proto_number= get(map_get(proto, __name___symbol), Symbol, prototype);
switch (proto_number) {
case t_Comment: outputNode(map_get(node, text_symbol)); break;
case t_Token: outputNode(map_get(node, name_symbol)); break;
case t_C_if:
outputNode(map_get(node, if_symbol));
outputNode(map_get(node, lparen_symbol));
outputNode(map_get(node, condition_symbol));
outputNode(map_get(node, rparen_symbol));
outputNode(map_get(node, consequent_symbol));
outputNode(map_get(node, else_symbol)); // null if no else clause
outputNode(map_get(node, alternate_symbol)); // null if no else clause
break;
case t_Comment:
outputNode(map_get(node, text_symbol));
break;
case t_Token:
outputNode(map_get(node, name_symbol));
break;
case t_C_int:
outputNode(map_get(node, value_symbol));
break;
case t_C_if:
outputNode(map_get(node, if_symbol));
outputNode(map_get(node, lparen_symbol));
outputNode(map_get(node, condition_symbol));
outputNode(map_get(node, rparen_symbol));
outputNode(map_get(node, consequent_symbol));
outputNode(map_get(node, else_symbol)); // null if no else clause
outputNode(map_get(node, alternate_symbol)); // null if no else clause
break;
}
#if 0
while (orow < node->row) { printf("\n"); ++orow; ocol= 0; }

+ 1
- 1
object.c Visa fil

@ -237,7 +237,7 @@ int isIntegerValue(int_t value)
oop makeInteger(int_t value)
{
#if (USE_TAG)
if (isIntegerValue(value)) return (oop)(((intptr_t)value << 1) | 1);
if (!isIntegerValue(value)) return (oop)(((intptr_t)value << 1) | 1); //TODO
#endif
oop newInt = malloc(sizeof(struct Integer));
newInt->type = Integer;

Laddar…
Avbryt
Spara