From c4811cf9254038f5a5a333a0da99e981216fc1a5 Mon Sep 17 00:00:00 2001 From: Nathan R Date: Thu, 8 Jul 2021 15:29:46 +0200 Subject: [PATCH] New tests, Constants OK? TODO : figure out isInteger --- ccmeta-test.ref | 16 ++++- ccmeta-test.txt | 16 ++++- ccmeta.leg | 159 ++++++++++++++++++++++++++---------------------- object.c | 10 ++- 4 files changed, 125 insertions(+), 76 deletions(-) diff --git a/ccmeta-test.ref b/ccmeta-test.ref index 5bfd2cc..8aeb91a 100644 --- a/ccmeta-test.ref +++ b/ccmeta-test.ref @@ -3,8 +3,22 @@ // comment +/* Test Int */ +3 +52 +13 +/* Test Float */ +13.700000 +42.000000 +1.000000 + +/* Test Char */ +'salut' +'bonjour tout le monde' +'je m appelle Nathan et je suis né en 1998' +'enchanté !' /* multi * line * comment - */ + */ \ No newline at end of file diff --git a/ccmeta-test.txt b/ccmeta-test.txt index 875be29..ec66916 100644 --- a/ccmeta-test.txt +++ b/ccmeta-test.txt @@ -3,8 +3,22 @@ ; // comment ; +/* Test Int */ +3; +52; +13; ; +/* Test Float */ +13.7; +42.0; +1e+0; +; +/* Test Char */ +'salut'; +'bonjour tout le monde'; +'je m appelle Nathan et je suis né en 1998'; +'enchanté !'; /* multi * line * comment - */ + */ \ No newline at end of file diff --git a/ccmeta.leg b/ccmeta.leg index b154c6c..15c2c7f 100644 --- a/ccmeta.leg +++ b/ccmeta.leg @@ -31,8 +31,7 @@ _DO(Return) _DO(Break) _DO(Continue) _DO(Throw) _DO(Try) \ /* _DO(Quasiquote) _DO(Unquote) */ \ _DO(Comment) _DO(Token) \ - _DO(C_if) \ - _DO(C_int) + _DO(C_if) _DO(C_int) _DO(C_float) _DO(C_char) typedef enum { t_UNDEFINED=0, @@ -320,23 +319,15 @@ oop newSymbol(oop name) } oop new_C_int(int value) { - oop integer = newObject(C_int_proto); - map_set(integer, value_symbol, makeInteger(value)); - return integer; + oop object = newObject(C_int_proto); + map_set(object, value_symbol, makeInteger(value)); + return object; } -oop newInteger(oop value) -{ - oop integer = newObject(Integer_proto); - map_set(integer, value_symbol, value); - return integer; -} - -oop newFloat(oop value) -{ - oop obj = newObject(Float_proto); - map_set(obj, value_symbol, value); - return obj; +oop new_C_float(float value) { + oop object = newObject(C_float_proto); + map_set(object, value_symbol, makeFloat(value)); + return object; } int digitValue(int c) @@ -406,6 +397,12 @@ oop newString(oop str) return string; } +oop new_C_char(char *s) { + oop object = newObject(C_char_proto); + map_set(object, value_symbol, makeString(s)); + return object; +} + oop newPreIncrement(oop rhs) { assert(is(Map, rhs)); oop proto= map_get(rhs, __proto___symbol); assert(null != proto); @@ -691,8 +688,8 @@ error = EOL* < (!EOL .)* EOL* (!EOL .)* > &{ error(yytext), 1 #| IDFIRST = [a-zA-Z_] | universalCharacterName | '$' &{gnu} IDREST = IDFIRST | [0-9] -#| -#|digit = [0-9] + +digit = [0-9] ### A.1.4 Universal character names @@ -708,8 +705,11 @@ hexQuad = hexadecimalDigit hexadecimalDigit hexadecimalDigit hexadecimalDigit # 6.4.4 -constant = integerConstant { $$ = new_C_int(atoi(yytext)); } - #| | floatingConstant | characterConstant +constant = characterConstant + | floatingConstant + | integerConstant + + # 6.4.4.1 @@ -717,7 +717,7 @@ integerConstant = < ( hexadecimalConstant | octalConstant | binaryConstant &{gnu} | decimalConstant - ) integerSuffix? > + ) integerSuffix? > { $$ = new_C_int(atoi(yytext)); } - decimalConstant = [1-9][0-9]* @@ -729,62 +729,62 @@ binaryConstant = '0'[bB][01]+ hexadecimalPrefix = '0'[xX] -octalDigit = [0-7] +octalDigit = [0-7] hexadecimalDigit = [0-9A-Fa-f] integerSuffix = ( [uU][lL]?[lL]? | [lL][lL]?[uU]? ) ( imaginarySuffix &{gnu} )? imaginarySuffix = [ij] -#| -#|# 6.4.4.2 -#| -#|floatingConstant = <( decimalFloatingConstant | hexadecimalFloatingConstant )> { $$= newFloat(yytext) } - -#| -#| -#|decimalFloatingConstant = fractionalConstant exponentPart? floatingSuffix? -#| | digitSequence exponentPart floatingSuffix? -#| -#|hexadecimalFloatingConstant = hexadecimalPrefix hexadecimalFractionalConstant binaryExponentPart floatingSuffix? -#| | hexadecimalPrefix hexadecimalDigitSequence binaryExponentPart floatingSuffix? -#| -#|fractionalConstant = digitSequence '.' digitSequence? -#| | '.' digitSequence -#| -#|exponentPart = [eE] [-+]? digitSequence -#| -#|digitSequence = digit+ -#| -#|hexadecimalFractionalConstant = hexadecimalDigitSequence '.' hexadecimalDigitSequence? -#| | '.' hexadecimalDigitSequence -#| -#|binaryExponentPart = [pP] [-+]? digitSequence -#| -#|hexadecimalDigitSequence = hexadecimalDigit+ -#| -#|floatingSuffix = [fFlL] imaginarySuffix? -#| -#|# 6.4.4.4 -#| -#|characterConstant = < "'" cCharSequence "'" > { $$= newChar(yytext) } - -#| | < "L'" cCharSequence "'" > { $$= newChar(yytext) } - -#| -#|cCharSequence = ( escapeSequence | !EOL [^\'\\] )* -#| -#|escapeSequence = simpleEscapeSequence -#| | octalEscapeSequence -#| | hexadecimalEscapeSequence -#| | universalCharacterName -#| | '\\' EOL -#| | '\\' Blank+ EOL &{gnu} -#| | '\\' . &{gnu} -#| -#|simpleEscapeSequence = '\\'([\'\"?\\abfnrtv] | 'e' &{gnu}) -#| -#|octalEscapeSequence = '\\' octalDigit octalDigit? octalDigit? -#| -#|hexadecimalEscapeSequence = '\\x' hexadecimalDigit+ -#| + +# 6.4.4.2 + +floatingConstant = <( decimalFloatingConstant | hexadecimalFloatingConstant )> { $$ = new_C_float(atof(yytext)); } - + + +decimalFloatingConstant = fractionalConstant exponentPart? floatingSuffix? + | digitSequence exponentPart floatingSuffix? + +hexadecimalFloatingConstant = hexadecimalPrefix hexadecimalFractionalConstant binaryExponentPart floatingSuffix? + | hexadecimalPrefix hexadecimalDigitSequence binaryExponentPart floatingSuffix? + +fractionalConstant = digitSequence '.' digitSequence? + | '.' digitSequence + +exponentPart = [eE] [-+]? digitSequence + +digitSequence = digit+ + +hexadecimalFractionalConstant = hexadecimalDigitSequence '.' hexadecimalDigitSequence? + | '.' hexadecimalDigitSequence + +binaryExponentPart = [pP] [-+]? digitSequence + +hexadecimalDigitSequence = hexadecimalDigit+ + +floatingSuffix = [fFlL] imaginarySuffix? + +# 6.4.4.4 + +characterConstant = < "'" cCharSequence "'" > { $$ = new_C_char(yytext) } - + | < "L'" cCharSequence "'" > { $$ = new_C_char(yytext) } - + +cCharSequence = ( escapeSequence | !EOL [^\'\\] )* + +escapeSequence = simpleEscapeSequence + | octalEscapeSequence + | hexadecimalEscapeSequence + | universalCharacterName + | '\\' EOL + | '\\' Blank+ EOL &{gnu} + | '\\' . &{gnu} + +simpleEscapeSequence = '\\'([\'\"?\\abfnrtv] | 'e' &{gnu}) + +octalEscapeSequence = '\\' octalDigit octalDigit? octalDigit? + +hexadecimalEscapeSequence = '\\x' hexadecimalDigit+ + #|### A.1.6 String literals #| #|# 6.4.5 @@ -2582,6 +2582,13 @@ void outputInt(int value) { ocol += strlen(toString); } +void outputFloat(float value) { + printf("%f", value); + char *toString = malloc(sizeof(value)); + sprintf(toString,"%f", value); + ocol += strlen(toString); +} + void outputNode(oop node) { if (!node) return; @@ -2596,6 +2603,9 @@ void outputNode(oop node) case Integer: outputInt(getInteger(node)); return; + case Float: + outputFloat(getFloat(node)); + return; default: fprintf(stderr, "\noutputNode: unknown node type %i\n", node->type); abort(); @@ -2615,6 +2625,11 @@ void outputNode(oop node) case t_C_int: outputNode(map_get(node, value_symbol)); break; + case t_C_float: + outputNode(map_get(node, value_symbol)); + break; + case t_C_char: + outputNode(map_get(node, value_symbol)); case t_C_if: outputNode(map_get(node, if_symbol)); outputNode(map_get(node, lparen_symbol)); diff --git a/object.c b/object.c index 4b4755b..d8755c4 100644 --- a/object.c +++ b/object.c @@ -226,6 +226,10 @@ int_t getInteger(oop obj) return get(obj, Integer, _value); } +float_t getFloat(oop obj) { + return get(obj, Float, _value); +} + #if (USE_TAG) int isIntegerValue(int_t value) { @@ -241,7 +245,8 @@ oop makeInteger(int_t value) #endif oop newInt = malloc(sizeof(struct Integer)); newInt->type = Integer; - newInt->Integer._value = value; + set(newInt, Integer, _value, value); + //newInt->Integer._value = value; return newInt; } @@ -249,7 +254,8 @@ oop makeFloat(flt_t value) { oop newFloat= malloc(sizeof(struct Float)); newFloat->type= Float; - newFloat->Float._value= value; + set(newFloat, Float, _value, value); + //newFloat->Float._value= value; return newFloat; }