Refactor compilation process around the new concept of packages.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of SourcePawn.
7 * SourcePawn is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * SourcePawn. If not, see http://www.gnu.org/licenses/.
22 #include "../include/keima.h"
34 class MessageReporter : public IMessageReporter
37 void onOutOfMemory() {
38 fprintf(stderr, "Out of memory!\n");
40 void onUncaughtException(const char *message) {
41 fprintf(stderr, "Uncaught exception: %s\n", message);
43 IStackFrameIterator *iter = Keima::NewStackFrameIterator();
46 const char *filename = iter->source();
48 filename = "<anonymous>";
49 if (const char *tail = strrchr(filename, '/'))
52 if (const char *tail = strrchr(filename, '\\'))
56 while (!iter->done() && index < 10) {
57 fprintf(stderr, "[%d] %s (%s line %d)\n",
66 virtual void beginCompilationErrors(const char *file) {
67 fprintf(stderr, "Displaying compilation errors for \"%s\"\n", file);
69 virtual void reportCompilationError(const char *message, unsigned line, unsigned col) {
70 fprintf(stderr, " line %d, col %d: %s\n", line, col, message);
72 virtual void endCompilationErrors() {
76 class MainCollector : public Ke::IPublicFunctionListener
82 void registerPublic(Handle<Function> fun) {
83 Local<String> str = fun->name();
84 if (str && strcmp(str->chars(), "main") == 0)
88 void unregisterPublic(Handle<Function> fun) {
90 fun_ = Handle<Function>();
93 Handle<Function> fun() const {
101 intptr_t PrintFloat(void *blah, intptr_t *params)
103 fprintf(stdout, "%f", *reinterpret_cast<float *>(¶ms[1]));
107 intptr_t PrintNum(void *blah, intptr_t *params)
109 fprintf(stdout, "%i", *reinterpret_cast<int *>(¶ms[1]));
113 intptr_t PrintString(void *blah, intptr_t *params)
115 fprintf(stdout, "%s", *reinterpret_cast<const char **>(¶ms[1]));
119 int main(int argc, char **argv)
122 fprintf(stderr, "Usage: <file>\n");
126 if (!Keima::Initialize())
129 ke::AutoFree<char> fullpath;
130 if (!ke::OS::GetAbsolutePath(argv[1], fullpath))
133 ke::AutoFree<char> path, name;
134 if (!ke::OS::SplitPath(*fullpath, path, name))
139 Keima::InstallMessageReporter(&s_Reporter);
142 Keima::SetPublicFunctionListener(&entry);
145 Local<Package> package = Package::New(Handle<Package>(), Handle<String>(), *path, Package::Normal);
151 if (!Keima::Compile(package, *name)) {
156 Keima::CollectGarbage();
158 Keima::BindNative("PrintFloat", (void *)PrintFloat);
159 Keima::BindNative("PrintNum", (void *)PrintNum);
160 Keima::BindNative("PrintString", (void *)PrintString);
162 Handle<Function> fun = entry.fun();
165 fprintf(stdout, "Not running; no public 'main'.\n");
167 if (!Keima::Invoke(fun, NULL, 0, true, &rval))
177 /* Overload a few things to prevent libstdc++ linking */
178 #if defined __linux__ || defined __APPLE__
179 extern "C" void __cxa_pure_virtual(void)
183 void *operator new(size_t size)
188 void *operator new[](size_t size)
193 void operator delete(void *ptr)
198 void operator delete[](void * ptr)