From: David Kerkeslager Date: Tue, 8 Aug 2017 04:53:44 +0000 (-0400) Subject: Better names X-Git-Url: https://code.kerkeslager.com/?a=commitdiff_plain;h=f4f13322a77eaf87328a4279c766228bb7391978;p=fur Better names --- diff --git a/generation.py b/generation.py index 36ae24d..26945a2 100644 --- a/generation.py +++ b/generation.py @@ -12,17 +12,7 @@ def generate_integer_literal(c_integer_literal): return 'integerLiteral({})'.format(c_integer_literal.value) def generate_string_literal(c_string_literal): - def c_escape(ch): - return { - '\n': r'\n', - '"': r'\"', - '\\': r'\\', - }.get(ch, ch) - - return 'stringLiteral("{}")'.format( - ''.join(c_escape(ch for ch in c_string_literal.value)), - ) - + return 'stringLiteral(STRING_LITERAL_LIST[{}])'.format(c_string_literal.index) CONSTANT_EXPRESSION_MAPPING = { 'true': 'TRUE', @@ -98,6 +88,7 @@ def generate(c_program): builtins=list(sorted(c_program.builtin_set)), statements=[generate_statement(statement) for statement in c_program.statements], standard_libraries=list(sorted(c_program.standard_libraries)), + string_literal_list=c_program.string_literal_list, symbol_list=c_program.symbol_list, ) diff --git a/templates/program.c b/templates/program.c index 2fea651..8116e25 100644 --- a/templates/program.c +++ b/templates/program.c @@ -8,18 +8,20 @@ #include <{{standard_library}}> {% endfor %} -struct String; -typedef struct String String; enum Type; typedef enum Type Type; union Instance; typedef union Instance Instance; struct Object; typedef struct Object Object; -struct Runtime; -typedef struct Runtime Runtime; -const char * const SYMBOL_LIST[] = { +const char* const STRING_LITERAL_LIST[] = { +{% for string_literal in string_literal_list %} + "{{ string_literal }}", +{% endfor %} +}; + +const char* const SYMBOL_LIST[] = { {% for symbol in symbol_list %} "{{ symbol }}", {% endfor %} @@ -36,7 +38,7 @@ union Instance { bool boolean; int32_t integer; - char* string; + const char* string; }; struct Object @@ -84,8 +86,7 @@ void Environment_destruct(Environment* self) EnvironmentNode* next; for(EnvironmentNode* node = self->root; node != NULL; node = next) { - // We don't need to destruct the permanent strings, because those will be destructed at the end when the Runtime is destructed - // The above comment represents all heap-allocated objects currently, so we don't need to destruct Objects (yet) + // No objects are allocated on the heap (yet!) so we don't need to free anything else next = node->next; free(node); } @@ -105,7 +106,7 @@ Object Environment_get(Environment* self, const char* const symbol) { for(EnvironmentNode* node = self->root; node != NULL; node = node->next) { - // We can compare pointers because pointers are unique in the SYMBOLS_LIST + // We can compare pointers because pointers are unique in the SYMBOL_LIST if(node->key == symbol) { return node->value; @@ -124,7 +125,7 @@ Object integerLiteral(int32_t literal) return result; } -Object stringLiteral(char* literal) +Object stringLiteral(const char* literal) { Object result; result.type = STRING; diff --git a/transformation.py b/transformation.py index 67fd73c..b75b3a0 100644 --- a/transformation.py +++ b/transformation.py @@ -12,6 +12,7 @@ CIntegerLiteral = collections.namedtuple( CStringLiteral = collections.namedtuple( 'CStringLiteral', [ + 'index', 'value', ], ) @@ -70,6 +71,7 @@ CProgram = collections.namedtuple( 'builtin_set', 'statements', 'standard_libraries', + 'string_literal_list', 'symbol_list', ], ) @@ -145,9 +147,19 @@ def transform_expression(accumulators, expression): symbol_list_index=accumulators.symbol_list.index(expression.value), ) + if isinstance(expression, parsing.FurStringLiteralExpression): + value = expression.value + + try: + index = accumulators.string_literal_list.index(value) + except ValueError: + index = len(accumulators.string_literal_list) + accumulators.string_literal_list.append(value) + + return CStringLiteral(index=index, value=value) + LITERAL_TYPE_MAPPING = { parsing.FurIntegerLiteralExpression: CIntegerLiteral, - parsing.FurStringLiteralExpression: CStringLiteral, } if type(expression) in LITERAL_TYPE_MAPPING: @@ -221,13 +233,15 @@ Accumulators = collections.namedtuple( [ 'builtin_set', 'symbol_list', + 'string_literal_list', ], ) def transform(program): accumulators = Accumulators( builtin_set=set(), - symbol_list = [], + symbol_list=[], + string_literal_list=[], ) c_statements = [ @@ -243,6 +257,7 @@ def transform(program): builtin_set=accumulators.builtin_set, statements=c_statements, standard_libraries=standard_libraries, + string_literal_list=accumulators.string_literal_list, symbol_list=accumulators.symbol_list, )