Added a boolean type
authorDavid Kerkeslager <kerkeslager@gmail.com>
Sun, 6 Aug 2017 16:51:44 +0000 (12:51 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Sun, 6 Aug 2017 16:51:44 +0000 (12:51 -0400)
examples/10_boolean.fur [new file with mode: 0644]
examples/10_boolean.fur.output.txt [new file with mode: 0644]
generation.py
templates/program.c
transformation.py

diff --git a/examples/10_boolean.fur b/examples/10_boolean.fur
new file mode 100644 (file)
index 0000000..6a0e4e3
--- /dev/null
@@ -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 (file)
index 0000000..d252328
--- /dev/null
@@ -0,0 +1,2 @@
+true
+false
\ No newline at end of file
index 4dfd115..28f08cd 100644 (file)
@@ -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,
     }
 
index 0f79b14..c9c7394 100644 (file)
@@ -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;
index e3658bc..668f4ff 100644 (file)
@@ -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(