ソースを参照

Factor out gen_clamp

rexim 4 年 前
コミット
d6055b1c69
1 ファイル変更21 行追加24 行削除
  1. 21 24
      lag.c

+ 21 - 24
lag.c

@@ -447,33 +447,30 @@ void gen_max_impl(FILE *stream, const char *prefix, Type_Def type_def)
 char *clamp_args[] = {"x", "a", "b"};
 #define CLAMP_ARITY (sizeof(clamp_args) / sizeof(clamp_args[0]))
 
-void gen_clamp_sig(FILE *stream, Type_Def type_def)
+void gen_clamp(FILE *stream, Stmt stmt, Type type, Fun_Def min_def, Fun_Def max_def)
 {
+    Type_Def type_def = type_defs[type];
     Short_String name = shortf("clamp%s", type_def.suffix);
     gen_func_sig(stream, type_def.name, name.cstr, type_def.name, clamp_args, CLAMP_ARITY);
-}
-
-void gen_clamp_decl(FILE *stream, Type_Def type_def)
-{
-    gen_clamp_sig(stream, type_def);
-    fprintf(stream, ";\n");
-}
 
-void gen_clamp_impl(FILE *stream, Type type, Fun_Def min_def, Fun_Def max_def)
-{
-    Type_Def type_def = type_defs[type];
-    const char *min_name = min_def.name_for_type[type];
-    assert(min_name != NULL);
-    const char *max_name = max_def.name_for_type[type];
-    assert(max_name != NULL);
+    if (stmt == STMT_DECL) {
+        fprintf(stream, ";\n");
+    } else if (stmt == STMT_IMPL) {
+        fprintf(stream, "\n");
+        fprintf(stream, "{\n");
 
-    gen_clamp_sig(stream, type_def);
-    fprintf(stream, "\n");
-    fprintf(stream, "{\n");
-    static_assert(CLAMP_ARITY == 3, "Unexpected clamp arity");
-    fprintf(stream, "    return %s(%s(%s, %s), %s);\n", 
-            min_name, max_name, clamp_args[1], clamp_args[0], clamp_args[2]);
-    fprintf(stream, "}\n");
+        static_assert(CLAMP_ARITY == 3, "Unexpected clamp arity");
+        const char *min_name = min_def.name_for_type[type];
+        assert(min_name != NULL);
+        const char *max_name = max_def.name_for_type[type];
+        assert(max_name != NULL);
+        fprintf(stream, "    return %s(%s(%s, %s), %s);\n", 
+                min_name, max_name, clamp_args[1], clamp_args[0], clamp_args[2]);
+        fprintf(stream, "}\n");
+    } else {
+        assert(0 && "unreachable");
+        exit(69);
+    }
 }
 
 static char *sqrlen_arg_name = "a";
@@ -616,7 +613,7 @@ int main()
         gen_minmax_decl(stream, "min", type_defs[TYPE_UNSIGNED_INT]);
         gen_minmax_decl(stream, "max", type_defs[TYPE_UNSIGNED_INT]);
         for (Type type = 0; type < COUNT_TYPES; ++type) {
-            gen_clamp_decl(stream, type_defs[type]);
+            gen_clamp(stream, STMT_DECL, type, fun_defs[FUN_MIN], fun_defs[FUN_MAX]);
         }
         fprintf(stream, "\n");
         for (size_t n = VECTOR_MIN_SIZE; n <= VECTOR_MAX_SIZE; ++n) {
@@ -675,7 +672,7 @@ int main()
         gen_max_impl(stream, "max", type_defs[TYPE_UNSIGNED_INT]);
         fputc('\n', stream);
         for (Type type = 0; type < COUNT_TYPES; ++type) {
-            gen_clamp_impl(stream, type, fun_defs[FUN_MIN], fun_defs[FUN_MAX]);
+            gen_clamp(stream, STMT_IMPL, type, fun_defs[FUN_MIN], fun_defs[FUN_MAX]);
             fputc('\n', stream);
         }
         for (size_t n = VECTOR_MIN_SIZE; n <= VECTOR_MAX_SIZE; ++n) {