Forráskód Böngészése

Moving terminal code to own file

Sam Jaffe 7 éve
szülő
commit
85d4a6796e
5 módosított fájl, 103 hozzáadás és 59 törlés
  1. 10 4
      dice-roll.xcodeproj/project.pbxproj
  2. 0 55
      src/main.cxx
  3. 33 0
      src/simple_main.cxx
  4. 42 0
      src/terminal_helper.cxx
  5. 18 0
      src/terminal_helper.h

+ 10 - 4
dice-roll.xcodeproj/project.pbxproj

@@ -17,7 +17,8 @@
 		CD38F53721C89493007A732C /* exception.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD38F53621C89493007A732C /* exception.cxx */; };
 		CD38F53921C922E2007A732C /* roll_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD38F53821C922E2007A732C /* roll_test.cxx */; };
 		CD38F53B21C928B4007A732C /* exception_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD38F53A21C928B4007A732C /* exception_test.cxx */; };
-		CDED6A2721B2F28A00AB91D0 /* main.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDED6A2621B2F28A00AB91D0 /* main.cxx */; };
+		CD38F54F21C945C2007A732C /* terminal_helper.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD38F54E21C945C2007A732C /* terminal_helper.cxx */; };
+		CDED6A2721B2F28A00AB91D0 /* simple_main.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDED6A2621B2F28A00AB91D0 /* simple_main.cxx */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -93,10 +94,12 @@
 		CD38F53621C89493007A732C /* exception.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = exception.cxx; sourceTree = "<group>"; };
 		CD38F53821C922E2007A732C /* roll_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = roll_test.cxx; sourceTree = "<group>"; };
 		CD38F53A21C928B4007A732C /* exception_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = exception_test.cxx; sourceTree = "<group>"; };
+		CD38F54D21C945C2007A732C /* terminal_helper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = terminal_helper.h; sourceTree = "<group>"; };
+		CD38F54E21C945C2007A732C /* terminal_helper.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = terminal_helper.cxx; sourceTree = "<group>"; };
 		CD8F1ABC21B31E9E00CBB3CA /* roll.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = roll.h; sourceTree = "<group>"; };
 		CD8F1ABD21B31E9E00CBB3CA /* roll.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = roll.cxx; sourceTree = "<group>"; };
 		CDED6A2321B2F28A00AB91D0 /* simple_dice */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = simple_dice; sourceTree = BUILT_PRODUCTS_DIR; };
-		CDED6A2621B2F28A00AB91D0 /* main.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cxx; sourceTree = "<group>"; };
+		CDED6A2621B2F28A00AB91D0 /* simple_main.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = simple_main.cxx; sourceTree = "<group>"; };
 		CDED6A2F21B2F2DC00AB91D0 /* die.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = die.h; sourceTree = "<group>"; };
 		CDED6A3021B2F2DC00AB91D0 /* die.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = die.cxx; sourceTree = "<group>"; };
 		CDED6A5821B2F6E800AB91D0 /* random.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = random.h; sourceTree = "<group>"; };
@@ -178,7 +181,9 @@
 		CDED6A2521B2F28A00AB91D0 /* src */ = {
 			isa = PBXGroup;
 			children = (
-				CDED6A2621B2F28A00AB91D0 /* main.cxx */,
+				CDED6A2621B2F28A00AB91D0 /* simple_main.cxx */,
+				CD38F54D21C945C2007A732C /* terminal_helper.h */,
+				CD38F54E21C945C2007A732C /* terminal_helper.cxx */,
 				CDED6A3021B2F2DC00AB91D0 /* die.cxx */,
 				CD38F53621C89493007A732C /* exception.cxx */,
 				CD8F1ABD21B31E9E00CBB3CA /* roll.cxx */,
@@ -393,7 +398,8 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				CDED6A2721B2F28A00AB91D0 /* main.cxx in Sources */,
+				CDED6A2721B2F28A00AB91D0 /* simple_main.cxx in Sources */,
+				CD38F54F21C945C2007A732C /* terminal_helper.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 0 - 55
src/main.cxx

@@ -1,55 +0,0 @@
-//
-//  main.cpp
-//  dice-roll
-//
-//  Created by Sam Jaffe on 12/1/18.
-//  Copyright © 2018 Sam Jaffe. All rights reserved.
-//
-
-#include "die.h"
-#include "exception.h"
-#include "roll.h"
-
-#include <iostream>
-#include <sstream>
-
-void print(dice::dice_roll const & r) {
-  std::cout << int(r) << " (" << r << ")\n";
-}
-
-void print(std::vector<dice::dice_roll> const & rs) {
-  if (rs.size() != 1) {
-    std::cout << '\n';
-    for (int i = 0; i < rs.size(); ++i) {
-      std::cout << "  Result/" << i << ": ";
-      print(rs[i]);
-    }
-  } else {
-    print(rs[0]);
-  }
-}
-
-/**
- * @param str {@see make_dice(std::string const &)}
- */
-void eval(std::string const & str) {
-  try {
-    auto d = dice::from_string(str);
-    auto rs = dice::roller()(d);
-    std::cout << "Result of '" << d << "': ";
-    print(rs);
-  } catch (dice::unexpected_token const & ut) {
-    std::cerr << "Error in roll: '" << str << "': " << ut.what() << "\n";
-    std::cerr << "                " << ut.pointer(str.size() + 1) << std::endl;
-  }
-}
-
-int main(int, const char **) {
-  std::string line;
-  std::cout << "> ";
-  while (std::getline(std::cin, line)) {
-    eval(line);
-    std::cout << "> ";
-  }
-  return 0;
-}

+ 33 - 0
src/simple_main.cxx

@@ -0,0 +1,33 @@
+//
+//  main.cpp
+//  dice-roll
+//
+//  Created by Sam Jaffe on 12/1/18.
+//  Copyright © 2018 Sam Jaffe. All rights reserved.
+//
+
+#include "die.h"
+#include "exception.h"
+#include "roll.h"
+#include "terminal_helper.h"
+
+#include <iostream>
+#include <sstream>
+
+void eval(std::string const & str) {
+  try {
+    terminal::process_dice_string(str);
+  } catch (dice::unexpected_token const & ut) {
+    terminal::print_error_message(str, ut);
+  }
+}
+
+int main(int, const char **) {
+  std::string line;
+  std::cout << "> ";
+  while (std::getline(std::cin, line)) {
+    eval(line);
+    std::cout << "> ";
+  }
+  return 0;
+}

+ 42 - 0
src/terminal_helper.cxx

@@ -0,0 +1,42 @@
+//
+//  terminal_helper.cxx
+//  simple_dice
+//
+//  Created by Sam Jaffe on 12/18/18.
+//  Copyright © 2018 Sam Jaffe. All rights reserved.
+//
+
+#include "terminal_helper.h"
+
+namespace terminal { namespace {
+  void print(dice::dice_roll const & r) {
+    std::cout << int(r) << " (" << r << ")\n";
+  }
+  
+  void print(std::vector<dice::dice_roll> const & rs) {
+    if (rs.size() != 1) {
+      std::cout << '\n';
+      for (int i = 0; i < rs.size(); ++i) {
+        std::cout << "  Result/" << i << ": ";
+        print(rs[i]);
+      }
+    } else {
+      print(rs[0]);
+    }
+  }
+}}
+
+namespace terminal {
+  void process_dice_string(std::string const & str) {
+    auto d = dice::from_string(str);
+    auto rs = dice::roller()(d);
+    std::cout << "Result of '" << d << "': ";
+    print(rs);
+  }
+  
+  void print_error_message(std::string const & str,
+                           dice::unexpected_token const & ut) {
+    std::cerr << "Error in roll: '" << str << "': " << ut.what() << "\n";
+    std::cerr << "                " << ut.pointer(str.size() + 1) << std::endl;
+  }
+}

+ 18 - 0
src/terminal_helper.h

@@ -0,0 +1,18 @@
+//
+//  terminal_helper.hpp
+//  simple_dice
+//
+//  Created by Sam Jaffe on 12/18/18.
+//  Copyright © 2018 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+namespace terminal {
+  /**
+   * @param str {@see make_dice(std::string const &)}
+   */
+  void process_dice_string(std::string const & str);
+  void print_error_message(std::string const & str,
+                           dice::unexpected_token const & ut);
+}