From: David Kerkeslager Date: Sun, 6 Aug 2017 16:51:44 +0000 (-0400) Subject: Added a boolean type X-Git-Url: https://code.kerkeslager.com/?a=commitdiff_plain;h=e09efc1f5f4f75362d8e1cae6e97c7c5ded38b2c;p=fur Added a boolean type --- diff --git a/examples/10_boolean.fur b/examples/10_boolean.fur new file mode 100644 index 0000000..6a0e4e3 --- /dev/null +++ b/examples/10_boolean.fur @@ -0,0 +1,3 @@ +print(true) +print('\n') +print(false) diff --git a/examples/10_boolean.fur.output.txt b/examples/10_boolean.fur.output.txt new file mode 100644 index 0000000..d252328 --- /dev/null +++ b/examples/10_boolean.fur.output.txt @@ -0,0 +1,2 @@ +true +false \ No newline at end of file diff --git a/generation.py b/generation.py index 4dfd115..28f08cd 100644 --- a/generation.py +++ b/generation.py @@ -23,6 +23,15 @@ def generate_string_literal(c_string_literal): ''.join(c_escape(ch for ch in c_string_literal.value)), ) + +CONSTANT_EXPRESSION_MAPPING = { + 'true': 'TRUE', + 'false': 'FALSE', +} + +def generate_constant_expression(c_constant_expression): + return CONSTANT_EXPRESSION_MAPPING[c_constant_expression.value] + def generate_symbol_expression(c_symbol_expression): return 'Environment_get(environment, SYMBOL_LIST[{}] /* symbol: {} */)'.format( c_symbol_expression.symbol_list_index, @@ -39,6 +48,7 @@ def generate_expression(c_argument): LITERAL_TYPE_MAPPING = { transformation.CIntegerLiteral: generate_integer_literal, transformation.CStringLiteral: generate_string_literal, + transformation.CConstantExpression: generate_constant_expression, transformation.CSymbolExpression: generate_symbol_expression, } diff --git a/templates/program.c b/templates/program.c index 0f79b14..c9c7394 100644 --- a/templates/program.c +++ b/templates/program.c @@ -33,12 +33,14 @@ const char * const SYMBOL_LIST[] = { enum Type { + BOOLEAN, INTEGER, STRING }; union Instance { + bool boolean; int32_t integer; String* string; }; @@ -49,6 +51,16 @@ struct Object Instance instance; }; +const Object TRUE = { + BOOLEAN, + true +}; + +const Object FALSE = { + BOOLEAN, + false +}; + struct EnvironmentNode; typedef struct EnvironmentNode EnvironmentNode; struct EnvironmentNode @@ -270,6 +282,10 @@ void builtin$print(Object output) { switch(output.type) { + case BOOLEAN: + fputs(output.instance.boolean ? "true" : "false", stdout); + break; + case INTEGER: printf("%" PRId32, output.instance.integer); break; diff --git a/transformation.py b/transformation.py index e3658bc..668f4ff 100644 --- a/transformation.py +++ b/transformation.py @@ -16,6 +16,13 @@ CStringLiteral = collections.namedtuple( ], ) +CConstantExpression = collections.namedtuple( + 'CConstantExpression', + [ + 'value' + ], +) + CSymbolExpression = collections.namedtuple( 'CSymbolExpression', [ @@ -99,8 +106,10 @@ CProgram = collections.namedtuple( ) BUILTINS = { + 'false': [], 'pow': ['math.h'], 'print': ['stdio.h'], + 'true': [], } def transform_expression(builtin_dependencies, symbol_list, expression): @@ -111,6 +120,9 @@ def transform_expression(builtin_dependencies, symbol_list, expression): return transform_function_call_expression(builtin_dependencies, symbol_list, expression) if isinstance(expression, parsing.FurSymbolExpression): + if expression.value in ['true', 'false']: + return CConstantExpression(value=expression.value) + if expression.value not in symbol_list: symbol_list.append(expression.value) @@ -162,6 +174,7 @@ def transform_negation_expression(builtin_dependencies, symbol_list, negation_ex def transform_function_call_expression(builtin_dependencies, symbol_list, function_call): if function_call.function.value in BUILTINS.keys(): + # TODO Check that the builtin is actually callable builtin_dependencies.add(function_call.function.value) return CFunctionCallExpression(