Permalink
Browse files

Hack support for loading the corelib from a file.

  • Loading branch information...
1 parent f8f54b7 commit 51d69029b4b771a9417333664c5625c41ed059fe @munificent committed Aug 16, 2012
Showing with 72 additions and 38 deletions.
  1. +2 −0 core/README
  2. +21 −0 core/core.mag
  3. +1 −24 src/VM/VM.cpp
  4. +1 −1 src/VM/VM.h
  5. +47 −13 src/main.cpp
View
@@ -0,0 +1,2 @@
+This directory contains the Magpie "standard library" for the C++ Magpie VM.
+Once the VM is more mature, this will be merged with lib/.
View
@@ -0,0 +1,21 @@
+defclass Bool
+end
+defclass Class
+end
+defclass Nothing
+end
+defclass Num
+end
+defclass Record
+end
+defclass String
+end
+defclass NoMatchError
+end
+def (is Num) + (is Num) native "num +"
+def (is String) + (is String) native "string +"
+def (is Num) - (is Num) native "num -"
+def (is Num) * (is Num) native "num *"
+def (is Num) / (is Num) native "num /"
+def print(arg) native "print"
+def (is String) count native "string count"
View
@@ -40,32 +40,9 @@ namespace magpie
nothing_ = new NothingObject();
}
- void VM::init()
+ void VM::init(gc<String> coreSource)
{
// Load the core module.
- // TODO(bob): Put this in an actual file somewhere.
- gc<String> coreSource = String::create(
- "defclass Bool\n"
- "end\n"
- "defclass Class\n"
- "end\n"
- "defclass Nothing\n"
- "end\n"
- "defclass Num\n"
- "end\n"
- "defclass Record\n"
- "end\n"
- "defclass String\n"
- "end\n"
- "defclass NoMatchError\n"
- "end\n"
- "def (is Num) + (is Num) native \"num +\"\n"
- "def (is String) + (is String) native \"string +\"\n"
- "def (is Num) - (is Num) native \"num -\"\n"
- "def (is Num) * (is Num) native \"num *\"\n"
- "def (is Num) / (is Num) native \"num /\"\n"
- "def print(arg) native \"print\"\n"
- "def (is String) count native \"string count\"\n");
coreModule_ = compileModule("<core>", coreSource);
runModule(coreModule_);
View
@@ -25,7 +25,7 @@ namespace magpie
virtual void reachRoots();
- void init();
+ void init(gc<String> coreSource);
bool loadModule(const char* fileName, gc<String> source);
gc<Object> evaluateReplExpression(gc<Expr> expr);
View
@@ -1,6 +1,8 @@
#include <fstream>
#include <iostream>
+#include <mach-o/dyld.h>
+
#include "Ast.h"
#include "Compiler.h"
#include "Fiber.h"
@@ -12,6 +14,31 @@
using namespace magpie;
using namespace std;
+// Finds the path to the core lib.
+void getCoreLibPath(char* path)
+{
+ char relativePath[PATH_MAX];
+
+ // TODO(bob): Move platform-specific stuff out to another file.
+ uint32_t size = PATH_MAX;
+ int result = _NSGetExecutablePath(relativePath, &size);
+ ASSERT(result == 0, "Executable path too long.");
+
+ // Find the core lib relative to the executable.
+ // TODO(bob): Hack. Try to work from the build directory too.
+ if (strstr(relativePath, "build/Debug/magpie") != 0 ||
+ strstr(relativePath, "build/Release/magpie") != 0)
+ {
+ strncat(relativePath, "/../../../core/core.mag", PATH_MAX);
+ }
+ else
+ {
+ strncat(relativePath, "/../core/core.mag", PATH_MAX);
+ }
+
+ // Canonicalize the path.
+ realpath(relativePath, path);
+}
// Reads a file from the given path into a String.
gc<String> readFile(const char* path)
@@ -51,11 +78,8 @@ gc<String> readLine(bool isContinued)
return String::create(line.c_str());
}
-int repl()
+int repl(VM& vm)
{
- VM vm;
- vm.init();
-
while (true)
{
gc<String> source;
@@ -89,22 +113,32 @@ int repl()
}
}
-int runFile(const char* fileName)
+int runFile(VM& vm, const char* fileName)
{
- VM vm;
- vm.init();
-
gc<String> source = readFile(fileName);
bool success = vm.loadModule(fileName, source);
return success ? 0 : 1;
}
int main(int argc, const char* argv[])
{
- if (argc == 1) return repl();
- if (argc == 2) return runFile(argv[1]);
+ if (argc > 2)
+ {
+ // TODO(bob): Show usage, etc.
+ std::cout << "magpie [script]" << std::endl;
+ return 0;
+ }
+
+ char path[PATH_MAX];
+ getCoreLibPath(path);
+
+ VM vm;
+
+ gc<String> coreSource = readFile(path);
+
+ vm.init(coreSource);
+
+ if (argc == 1) return repl(vm);
- // TODO(bob): Show usage, etc.
- std::cout << "magpie <script>" << std::endl;
- return 1;
+ return runFile(vm, argv[1]);
}

0 comments on commit 51d6902

Please sign in to comment.