Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial support for extern function

  • Loading branch information...
commit 6774809360744615d0f671a12a73c74b8ffc0158 1 parent 4ab9282
@NotFound authored
View
4 Makefile
@@ -19,6 +19,7 @@ OBJS = \
winxedxx_objectptr.o \
winxedxx_classes.o \
winxedxx_handle.o \
+ winxedxx_namespace.o \
winxedxx_util.o
FRONTEND = winxedxc.pbc
@@ -54,6 +55,9 @@ winxedxx_classes.o: winxedxx_classes.cxx winxedxx_types.h winxedxx_object.h winx
winxedxx_handle.o: winxedxx_handle.cxx winxedxx_types.h winxedxx_object.h winxedxx_default.h winxedxx_integer.h winxedxx_handle.h
$(CXX) $(CXXOPTS) -c winxedxx_handle.cxx
+winxedxx_namespace.o: winxedxx_namespace.cxx winxedxx_namespace.h
+ $(CXX) $(CXXOPTS) -c winxedxx_namespace.cxx
+
winxedxx_util.o: winxedxx_util.cxx winxedxx_types.h winxedxx_object.h winxedxx_default.h winxedxx_integer.h winxedxx_handle.h
$(CXX) $(CXXOPTS) -c winxedxx_util.cxx
View
1  winxedxc.winxed
@@ -99,6 +99,7 @@ class CompilerCxx
"winxedxx_default.o",
"winxedxx_classes.o",
"winxedxx_handle.o",
+ "winxedxx_namespace.o",
"winxedxx_util.o"
];
for (string obj in objs)
View
49 winxedxx.winxed
@@ -41,6 +41,7 @@ class ArrayExpr;
class HashExpr;
class FunctionExpr;
class FunctionId;
+class FunctionRef;
class NullCheckerExpr;
class ConcatString;
class CallExpr;
@@ -678,6 +679,16 @@ function emit_CallExpr(out, callexpr)
case funref instanceof FunctionId:
out.print(funref.subid);
break;
+ case funref instanceof FunctionRef:
+ var path = funref.sym.owner.getpath().path;
+ int n = elements(path);
+ var name = funref.getstart();
+ if (n > 0)
+ WxxUnsupported("Only extern function at root namespace level for a now");
+ else {
+ out.print("getRootNamespace().get(\"", name.getidentifier(), "\")");
+ }
+ break;
case funref.isidentifier():
if ((callexpr instanceof CallMemberExpr) && callexpr.subid != null)
out.print(callexpr.subid);
@@ -2090,7 +2101,10 @@ function get_main(ns)
{
var functions = ns.funcont.functions;
var result = find_if(functions, function(string name) {
- var modifiers = functions[name].modifiers;
+ var fun = functions[name];
+ if (fun instanceof FunctionExtern)
+ return false;
+ var modifiers = fun.modifiers;
return (modifiers != null && modifiers.pick("main") != null) ||
name == "main";
});
@@ -2108,6 +2122,7 @@ class Output
// Generated by winxedxx
#include "winxedxx.h"
+#include "winxedxx_namespace.h"
namespace WinxedXX
{
@@ -2116,6 +2131,9 @@ HEADER:>>
;
//----------------------------------------------
const string FOOTER1 = <<:FOOTER1
+
+static const int initializer = initialize();
+
} // namespace WinxedXX
FOOTER1:>>
@@ -2169,6 +2187,32 @@ CXXMAIN2:>>
for (var arg in args)
handle.print(arg);
}
+ function emit_initializer(var rootns)
+ {
+ const string INITIALIZER = <<:INITIALIZER
+static int initialize()
+{
+ WxxNamespace &rootns = getRootNamespace();
+INITIALIZER:>>
+ ;
+ const string INITIALIZEREND = <<:INITIALIZEREND
+ return 1;
+}
+INITIALIZEREND:>>
+ ;
+ self.print(INITIALIZER);
+ for (var f in rootns.items) {
+ if (! (f instanceof FunctionStatement))
+ continue;
+ string name = f.name;
+ if (f.subid != null) {
+ self.print("rootns.set(\"", name, "\", ",
+ "WxxObjectPtr(", f.subid, ")",
+ ");\n");
+ }
+ }
+ self.print(INITIALIZEREND);
+ }
function emit(parsed)
{
var ns = parsed.rootns;
@@ -2181,7 +2225,10 @@ CXXMAIN2:>>
emit_namespacest(self, ns);
+ self.emit_initializer(parsed.rootns);
+
self.print(FOOTER1);
+
if (mainfunc != "") {
self.print(CXXMAIN1);
self.print(mainfunc);
View
53 winxedxx_namespace.cxx
@@ -0,0 +1,53 @@
+// winxedxx_namespace.cxx
+
+// (C) 2012 Julián Albo "NotFound"
+
+#include "winxedxx_namespace.h"
+
+namespace WinxedXX
+{
+
+typedef std::map<std::string, WxxObjectPtr> symbols_t;
+
+WxxNamespace::WxxNamespace() :
+ parentns(0)
+{
+}
+
+WxxNamespace::WxxNamespace(const std::string &name, WxxNamespace *parent) :
+ parentns(parent)
+{
+}
+
+WxxObjectPtr WxxNamespace::get(const std::string &name)
+{
+ symbols_t::const_iterator it = symbols.find(name);
+ if (it == symbols.end())
+ return winxedxxnull;
+ return WxxObjectPtr(it->second);
+}
+
+void WxxNamespace::set(const std::string &name, const WxxObjectPtr &value)
+{
+ symbols[name] = value;
+}
+
+//**************************************************************
+
+class WxxRootNamespace : WxxNamespace
+{
+ friend WxxNamespace &getRootNamespace();
+};
+
+static WxxRootNamespace *rootnamespace = 0;
+
+WxxNamespace &getRootNamespace()
+{
+ if (! rootnamespace)
+ rootnamespace = new WxxRootNamespace();
+ return *rootnamespace;
+}
+
+} // namespace WinxedXX
+
+// End of winxedxx_namespace.cxx
View
35 winxedxx_namespace.h
@@ -0,0 +1,35 @@
+#ifndef INCLUDE_WINXEDXX_NAMESPACE_H
+#define INCLUDE_WINXEDXX_NAMESPACE_H
+
+// winxedxx_namespace.h
+
+// (C) 2012 Julián Albo "NotFound"
+
+#include "winxedxx_types.h"
+
+#include <string>
+#include <map>
+
+namespace WinxedXX
+{
+
+class WxxNamespace
+{
+protected:
+ WxxNamespace();
+ WxxNamespace(const std::string &name, WxxNamespace *parent);
+public:
+ WxxObjectPtr get(const std::string &name);
+ void set(const std::string &name, const WxxObjectPtr &value);
+private:
+ WxxNamespace *parentns;
+ std::map<std::string, WxxObjectPtr> symbols;
+};
+
+WxxNamespace &getRootNamespace();
+
+} // namespace WinxedXX
+
+#endif
+
+// End of winxedxx_namespace.h
Please sign in to comment.
Something went wrong with that request. Please try again.