#include "sig/parse.hpp"
#include "sig/ffi_type.hpp"
-#include <apr-1/apr_pools.h>
-#include <apr-1/apr_strings.h>
+#include "Pooling.hpp"
#include <unistd.h>
#include <iostream>
#include <ext/stdio_filebuf.h>
+#include <set>
+#include <map>
+#include <sstream>
+
+#include "Parser.hpp"
+#include "Cycript.tab.hh"
#undef _assert
#undef _trace
CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \
} while (false)
-/* Objective-C Handle<> {{{ */
-template <typename Type_>
-class _H {
- typedef _H<Type_> This_;
-
- private:
- Type_ *value_;
-
- _finline void Retain_() {
- if (value_ != nil)
- [value_ retain];
- }
-
- _finline void Clear_() {
- if (value_ != nil)
- [value_ release];
- }
-
- public:
- _finline _H(const This_ &rhs) :
- value_(rhs.value_ == nil ? nil : [rhs.value_ retain])
- {
- }
-
- _finline _H(Type_ *value = NULL, bool mended = false) :
- value_(value)
- {
- if (!mended)
- Retain_();
- }
-
- _finline ~_H() {
- Clear_();
- }
-
- _finline operator Type_ *() const {
- return value_;
- }
-
- _finline This_ &operator =(Type_ *value) {
- if (value_ != value) {
- Type_ *old(value_);
- value_ = value;
- Retain_();
- if (old != nil)
- [old release];
- } return *this;
- }
-};
-/* }}} */
-/* APR Pool Helpers {{{ */
-void *operator new(size_t size, apr_pool_t *pool) {
- return apr_palloc(pool, size);
-}
-
-void *operator new [](size_t size, apr_pool_t *pool) {
- return apr_palloc(pool, size);
-}
-
-class CYPool {
- private:
- apr_pool_t *pool_;
-
- public:
- CYPool() {
- apr_pool_create(&pool_, NULL);
- }
-
- ~CYPool() {
- apr_pool_destroy(pool_);
- }
-
- operator apr_pool_t *() const {
- return pool_;
- }
-};
-/* }}} */
#define _pooled _H<NSAutoreleasePool> _pool([[NSAutoreleasePool alloc] init], true);
}
// XXX: this is not a safe handle
-class CYString {
+class CYJSString {
private:
JSStringRef string_;
public:
template <typename Arg0_>
- CYString(Arg0_ arg0) {
+ CYJSString(Arg0_ arg0) {
string_ = CYCopyJSString(arg0);
}
template <typename Arg0_, typename Arg1_>
- CYString(Arg0_ arg0, Arg1_ arg1) {
+ CYJSString(Arg0_ arg0, Arg1_ arg1) {
string_ = CYCopyJSString(arg0, arg1);
}
- ~CYString() {
+ ~CYJSString() {
JSStringRelease(string_);
}
}
CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) {
- return CYCopyCFString(CYString(context, value));
+ return CYCopyCFString(CYJSString(context, value));
}
double CYCastDouble(JSContextRef context, JSValueRef value) {
- (id) objectForKey:(id)key {
JSValueRef exception(NULL);
- JSValueRef value(JSObjectGetProperty(context_, object_, CYString(key), &exception));
+ JSValueRef value(JSObjectGetProperty(context_, object_, CYJSString(key), &exception));
CYThrow(context_, exception);
return CYCastNSObject(context_, value);
}
- (void) setObject:(id)object forKey:(id)key {
JSValueRef exception(NULL);
- JSObjectSetProperty(context_, object_, CYString(key), CYCastJSValue(context_, object), kJSPropertyAttributeNone, &exception);
+ JSObjectSetProperty(context_, object_, CYJSString(key), CYCastJSValue(context_, object), kJSPropertyAttributeNone, &exception);
CYThrow(context_, exception);
}
- (void) removeObjectForKey:(id)key {
JSValueRef exception(NULL);
// XXX: this returns a bool... throw exception, or ignore?
- JSObjectDeleteProperty(context_, object_, CYString(key), &exception);
+ JSObjectDeleteProperty(context_, object_, CYJSString(key), &exception);
CYThrow(context_, exception);
}
void CYSetProperty(JSContextRef context, JSObjectRef object, const char *name, JSValueRef value) {
JSValueRef exception(NULL);
- JSObjectSetProperty(context, object, CYString(name), value, kJSPropertyAttributeNone, &exception);
+ JSObjectSetProperty(context, object, CYJSString(name), value, kJSPropertyAttributeNone, &exception);
CYThrow(context, exception);
}
}
char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
- return CYPoolCString(pool, CYString(context, value));
+ return CYPoolCString(pool, CYJSString(context, value));
}
// XXX: this macro is unhygenic
case sig::string_P: {
if (char *utf8 = *reinterpret_cast<char **>(data))
- value = JSValueMakeString(context, CYString(utf8));
+ value = JSValueMakeString(context, CYJSString(utf8));
else goto null;
} break;
if (NSMutableArray *entry = [Bridge_ objectForKey:name])
switch ([[entry objectAtIndex:0] intValue]) {
case 0:
- return JSEvaluateScript(JSGetContext(), CYString([entry objectAtIndex:1]), NULL, NULL, 0, NULL);
+ return JSEvaluateScript(JSGetContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL);
case 1:
return CYMakeFunction(context, [name cy$symbol], [[entry objectAtIndex:1] UTF8String]);
case 2:
{NULL, NULL, NULL, 0}
};
-void CYConsole(FILE *fin, FILE *fout, FILE *ferr) {
- std::string line;
+CYDriver::CYDriver(const std::string &filename) :
+ filename_(filename),
+ source_(NULL)
+{
+ ScannerInit();
+}
+
+CYDriver::~CYDriver() {
+ ScannerDestroy();
+}
- __gnu_cxx::stdio_filebuf<char> bin(fin, std::ios::in);
- std::istream sin(&bin);
+extern int cydebug;
- for (;;) {
- NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
+void cy::parser::error(const cy::parser::location_type &loc, const std::string &msg) {
+ std::cerr << loc << ": " << msg << std::endl;
+}
+
+void CYConsole(FILE *fin, FILE *fout, FILE *ferr) {
+ //cydebug = 1;
- fputs(">>> ", fout);
- fflush(fout);
+ for (;;) { _pooled
+ CYDriver driver("<stdin>");
+ cy::parser parser(driver);
+ if (parser.parse() != 0)
+ continue;
- if (!std::getline(sin, line))
+ if (driver.source_ == NULL) {
+ fputs("driver.source == NULL\n", fout);
break;
+ }
+
+ std::ostringstream str;
+ driver.source_->Part(str);
- JSStringRef script(JSStringCreateWithUTF8CString(line.c_str()));
+ JSStringRef script(JSStringCreateWithUTF8CString(str.str().c_str()));
JSContextRef context(JSGetContext());
fputs("\n", fout);
fflush(fout);
}
-
- [pool release];
}
}
CYSetProperty(context, global, "ffi", JSObjectMakeConstructor(context, Functor_, &ffi));
- CYSetProperty(context, global, "objc_msgSend", JSObjectMakeFunctionWithCallback(context, CYString("objc_msgSend"), &$objc_msgSend));
+ CYSetProperty(context, global, "objc_msgSend", JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
Bridge_ = [[NSMutableDictionary dictionaryWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain];
length_ = JSStringCreateWithUTF8CString("length");
JSValueRef exception(NULL);
- JSValueRef value(JSObjectGetProperty(JSGetContext(), global, CYString("Array"), &exception));
+ JSValueRef value(JSObjectGetProperty(JSGetContext(), global, CYJSString("Array"), &exception));
CYThrow(context, exception);
Array_ = JSValueToObject(JSGetContext(), value, &exception);
CYThrow(context, exception);