]> git.saurik.com Git - cycript.git/blobdiff - Library.cpp
Add struct definitions and namespace struct names.
[cycript.git] / Library.cpp
index 5c5c97d16f35a0149bcc663b38a6f02dd7660ec7..ac9d8b08db673d9ef5b1596d1b27601298aaa371 100644 (file)
 **/
 /* }}} */
 
-#include <dlfcn.h>
-
 #include "cycript.hpp"
 
-#include "Pooling.hpp"
-
-#include <sys/mman.h>
-
 #include <iostream>
 #include <set>
 #include <map>
 #include <sstream>
 #include <cmath>
 
-#include "Error.hpp"
-#include "Execute.hpp"
-#include "Parser.hpp"
-#include "String.hpp"
+#include <dlfcn.h>
 
-#include "Cycript.tab.hh"
-#include "Driver.hpp"
+#include <sys/mman.h>
 
 #include "ConvertUTF.h"
+#include "Driver.hpp"
+#include "Error.hpp"
+#include "Execute.hpp"
+#include "Pooling.hpp"
+#include "String.hpp"
+#include "Syntax.hpp"
 
 template <>
 ::pthread_key_t CYLocal<CYPool>::key_ = Key_();
@@ -107,15 +103,20 @@ bool CYGetOffset(const char *value, ssize_t &index) {
 }
 /* }}} */
 /* JavaScript *ify {{{ */
-void CYStringify(std::ostringstream &str, const char *data, size_t size) {
-    unsigned quot(0), apos(0);
-    for (const char *value(data), *end(data + size); value != end; ++value)
-        if (*value == '"')
-            ++quot;
-        else if (*value == '\'')
-            ++apos;
-
-    bool single(quot > apos);
+void CYStringify(std::ostringstream &str, const char *data, size_t size, bool c) {
+    bool single;
+    if (c)
+        single = false;
+    else {
+        unsigned quot(0), apos(0);
+        for (const char *value(data), *end(data + size); value != end; ++value)
+            if (*value == '"')
+                ++quot;
+            else if (*value == '\'')
+                ++apos;
+
+        single = quot > apos;
+    }
 
     str << (single ? '\'' : '"');
 
@@ -176,6 +177,13 @@ void CYStringify(std::ostringstream &str, const char *data, size_t size) {
 }
 
 void CYNumerify(std::ostringstream &str, double value) {
+    if (std::isinf(value)) {
+        if (value < 0)
+            str << '-';
+        str << "Infinity";
+        return;
+    }
+
     char string[32];
     // XXX: I want this to print 1e3 rather than 1000
     sprintf(string, "%.17g", value);
@@ -217,21 +225,23 @@ double CYCastDouble(const char *value) {
     return CYCastDouble(value, strlen(value));
 }
 
-CYUTF8String CYPoolCode(CYPool &pool, std::istream &stream) {
-    CYLocalPool local;
-    CYDriver driver(stream);
+_visible bool CYStartsWith(const CYUTF8String &haystack, const CYUTF8String &needle) {
+    return haystack.size >= needle.size && strncmp(haystack.data, needle.data, needle.size) == 0;
+}
 
-    cy::parser parser(driver);
-    _assert(parser.parse() == 0);
+CYUTF8String CYPoolCode(CYPool &pool, std::streambuf &stream) {
+    CYLocalPool local;
+    CYDriver driver(local, stream);
+    _assert(!driver.Parse());
     _assert(driver.errors_.empty());
 
     CYOptions options;
     CYContext context(options);
-    driver.program_->Replace(context);
+    driver.script_->Replace(context);
 
     std::stringbuf str;
     CYOutput out(str, options);
-    out << *driver.program_;
+    out << *driver.script_;
     return $pool.strdup(str.str().c_str());
 }
 
@@ -240,7 +250,7 @@ CYPool &CYGetGlobalPool() {
     return pool;
 }
 
-void CYThrow(const char *format, ...) {
+_visible void CYThrow(const char *format, ...) {
     va_list args;
     va_start(args, format);
     throw CYPoolError(format, args);