summaryrefslogtreecommitdiff
path: root/tiger-compiler/tcsh/src
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
commit967be9e750221ab2ab783f95df79bb26d290a45e (patch)
tree6802900a5e975f9f68b169f0f503f040056d6952 /tiger-compiler/tcsh/src
add: added projectsHEADmain
Diffstat (limited to 'tiger-compiler/tcsh/src')
-rw-r--r--tiger-compiler/tcsh/src/helper.hh16
-rw-r--r--tiger-compiler/tcsh/src/swig_real_type.hh67
-rw-r--r--tiger-compiler/tcsh/src/tiger_ast.i312
-rw-r--r--tiger-compiler/tcsh/src/tiger_astclone.i17
-rw-r--r--tiger-compiler/tcsh/src/tiger_bind.i13
-rw-r--r--tiger-compiler/tcsh/src/tiger_callgraph.i18
-rw-r--r--tiger-compiler/tcsh/src/tiger_combine.i14
-rw-r--r--tiger-compiler/tcsh/src/tiger_common.i73
-rw-r--r--tiger-compiler/tcsh/src/tiger_desugar.i30
-rw-r--r--tiger-compiler/tcsh/src/tiger_escapes.i11
-rw-r--r--tiger-compiler/tcsh/src/tiger_llvmtranslate.i39
-rw-r--r--tiger-compiler/tcsh/src/tiger_misc.i73
-rw-r--r--tiger-compiler/tcsh/src/tiger_object.i32
-rw-r--r--tiger-compiler/tcsh/src/tiger_overload.i14
-rw-r--r--tiger-compiler/tcsh/src/tiger_parse.i102
-rw-r--r--tiger-compiler/tcsh/src/tiger_type.i91
16 files changed, 922 insertions, 0 deletions
diff --git a/tiger-compiler/tcsh/src/helper.hh b/tiger-compiler/tcsh/src/helper.hh
new file mode 100644
index 0000000..cdbf760
--- /dev/null
+++ b/tiger-compiler/tcsh/src/helper.hh
@@ -0,0 +1,16 @@
+#ifndef SWIG_HELPER_H
+#define SWIG_HELPER_H
+
+#define STCONVERT(TYPE, TYPENAME) \
+void operator()(const TYPE & e) override \
+{ \
+ result = SWIG_NewPointerObj(SWIG_as_voidptr(&e), SWIGTYPE_ ## TYPENAME, 0); \
+}
+
+#define IFTYPECONVERT(TYPE, TYPENAME) \
+if (const TYPE* d = dynamic_cast<const TYPE*>(&e); d != nullptr) \
+{ \
+ result = SWIG_NewPointerObj(SWIG_as_voidptr(&e), SWIGTYPE_ ## TYPENAME, 0); \
+}
+
+#endif /* ! SWIG_HELPER_H */
diff --git a/tiger-compiler/tcsh/src/swig_real_type.hh b/tiger-compiler/tcsh/src/swig_real_type.hh
new file mode 100644
index 0000000..9420bf9
--- /dev/null
+++ b/tiger-compiler/tcsh/src/swig_real_type.hh
@@ -0,0 +1,67 @@
+#ifndef SWIG_REAL_TYPE_HH
+#define SWIG_REAL_TYPE_HH
+
+#include "helper.hh"
+
+namespace {
+ using namespace ast;
+ class SwigAstTypeVisitor : public ConstVisitor
+ {
+ public:
+ PyObject *result;
+ SwigAstTypeVisitor() : GenVisitor<misc::constify_traits>() {}
+ ~SwigAstTypeVisitor() {}
+
+#define STHELPER(TYPE) STCONVERT(TYPE, p_ast__ ## TYPE)
+ STHELPER(ArrayExp)
+ STHELPER(ArrayTy)
+ STHELPER(AssignExp)
+ STHELPER(Ast)
+ STHELPER(BreakExp)
+ STHELPER(CallExp)
+ STHELPER(CastExp)
+ STHELPER(ChunkList)
+ STHELPER(ClassTy)
+ STHELPER(Field)
+ STHELPER(FieldInit)
+ STHELPER(FieldVar)
+ STHELPER(ForExp)
+ STHELPER(FunctionDec)
+ STHELPER(IfExp)
+ STHELPER(IntExp)
+ STHELPER(LetExp)
+ STHELPER(MethodCallExp)
+ STHELPER(MethodDec)
+ STHELPER(NameTy)
+ STHELPER(NilExp)
+ STHELPER(ObjectExp)
+ STHELPER(OpExp)
+ STHELPER(RecordExp)
+ STHELPER(RecordTy)
+ STHELPER(SeqExp)
+ STHELPER(SimpleVar)
+ STHELPER(StringExp)
+ STHELPER(SubscriptVar)
+ STHELPER(TypeDec)
+ STHELPER(VarDec)
+ STHELPER(WhileExp)
+#undef STHELPER
+
+#define STCHUNKHELPER(TYPE) \
+ STCONVERT(TYPE ## Chunk, p_ast__ChunkT_ast__ ## TYPE ## Dec_t)
+ STCHUNKHELPER(Function)
+ STCHUNKHELPER(Method)
+ STCHUNKHELPER(Type)
+ STCHUNKHELPER(Var)
+#undef STCHUNKHELPER
+ };
+} // namespace
+
+PyObject *get_swig_real_ast_type(const ast::Ast& e)
+{
+ SwigAstTypeVisitor stv;
+ e.accept(stv);
+ return stv.result;
+}
+
+#endif /* ! SWIG_REAL_TYPE_HH */
diff --git a/tiger-compiler/tcsh/src/tiger_ast.i b/tiger-compiler/tcsh/src/tiger_ast.i
new file mode 100644
index 0000000..685100f
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_ast.i
@@ -0,0 +1,312 @@
+// -*- C++ -*-
+
+%module tiger_ast
+
+%include "std_string.i"
+%include "std_vector.i"
+%include "std_list.i"
+%include "std_except.i"
+
+%{
+ #include "helper.hh"
+ #include <sstream>
+ #include <fstream>
+ #include <ast/visitor.hh>
+ #include <ast/object-visitor.hh>
+ #include <ast/default-visitor.hh>
+ #include <ast/all.hh>
+ #include <ast/libast.hh>
+ #include <common.hh>
+%}
+
+%typemap(ret) ast::Ast* {
+ $result = get_swig_real_ast_type(*$1);
+}
+%apply ast::Ast* { ast::ChunkInterface*, ast::Exp*, ast::Dec*, ast::Ty*, ast::Var* }
+%apply ast::Ast* { ast::Ast&, ast::ChunkInterface&, ast::Exp&, ast::Dec&, ast::Ty&, ast::Var& }
+
+%include "common.hh"
+%include "ast/fwd.hh"
+
+%include "ast/location.hh"
+
+%include "ast/ast.hh"
+
+%extend ast::Ast
+{
+ std::string __str__() const
+ {
+ std::ostringstream o;
+ o << *$self;
+ return o.str();
+ }
+ bool operator==(const ast::Ast& o) const
+ {
+ return $self == &o;
+ }
+}
+
+%extend ast::OpExp {
+ %pythoncode %{
+ def oper_str_get(self) -> "std::string":
+ return _tiger_ast.str(self.oper_get())
+
+ @staticmethod
+ def oper_names():
+ return { getattr(_tiger_ast, t): t[len("OpExp_Oper_"):] for t in dir(_tiger_ast) if t.startswith("OpExp_Oper_") }
+
+ def oper_name_get(self) -> "std::string":
+ return OpExp.oper_names()[self.oper_get()]
+ %}
+}
+
+%extend ast::Typable
+{
+ %pythoncode %{
+ def type_get(self) -> "type::Type const *":
+ import tiger_type
+ return tiger_type._get_swig_real_type_type(_tiger_ast.Typable_type_get(self))
+ %}
+}
+
+// Visitors.
+%include "ast/visitor.hh"
+%template(AstConstVisitor) ast::GenVisitor<misc::constify_traits>;
+%template(AstVisitor) ast::GenVisitor<misc::id_traits>;
+%warnfilter(401);
+
+%{
+#include "swig_real_type.hh"
+%}
+
+%define HELPER(TYPE, SUPER_TYPE)
+ void operator()(const ast::##TYPE & e) override
+ {
+ CallbackVisitor_Argument arg{++id_, #TYPE, &e};
+ auto obj = make_argument_dict(arg);
+ call_callback(parent_obj_, obj);
+
+ auto save_arg = parent_arg_;
+ auto save_obj = parent_obj_;
+ parent_arg_ = arg;
+ parent_obj_ = obj;
+ SUPER_TYPE##::operator()(e);
+ parent_arg_ = save_arg;
+ parent_obj_ = save_obj;
+ }
+%enddef
+
+%warnfilter(451) CallbackVisitor_Argument::name;
+%inline
+{
+ ast::Ast* ast_upcast(ast::ChunkList* a)
+ {
+ return static_cast<ast::Ast *>(a);
+ }
+
+#ifdef SWIGPYTHON
+
+ struct CallbackVisitor_Argument
+ {
+ int id{};
+ const char* name{};
+ const ast::Ast* ast{};
+ };
+
+ class CallbackVisitor
+ : public ast::DefaultConstVisitor
+ , public ast::ObjectConstVisitor
+ {
+ public:
+ using super_type = ast::DefaultVisitor;
+
+ CallbackVisitor(PyObject* pyfunc)
+ : GenVisitor<misc::constify_traits>()
+ {
+ Py_XINCREF(pyfunc);
+ this->callback_ = pyfunc;
+ }
+ ~CallbackVisitor() {}
+
+ PyObject* make_argument_dict(const CallbackVisitor_Argument& a)
+ {
+ std::ostringstream o;
+ if (a.ast) o << *a.ast;
+ std::string name(a.name);
+ std::string dec_name;
+ if (auto dec = dynamic_cast<const ast::Dec*>(a.ast))
+ dec_name = std::string(dec->name_get());
+ auto s = o.str();
+ PyObject *obj = get_swig_real_ast_type(*a.ast);
+ return Py_BuildValue("{s:i,s:s,s:s,s:s,s:O}",
+ "id", a.id,
+ "name", name.c_str(),
+ "string", s.c_str(),
+ "dec", dec_name.c_str(),
+ "ast", obj);
+ }
+
+ PyObject* call_callback(PyObject* from, PyObject* to) const
+ {
+ auto arglist = Py_BuildValue("(OO)", from ? from : Py_None, to ? to : Py_None);
+ auto result = PyObject_CallObject(this->callback_, arglist);
+ Py_DECREF(arglist);
+ return result;
+ }
+
+ HELPER(ArrayExp, ast::DefaultConstVisitor)
+ HELPER(ArrayTy, ast::DefaultConstVisitor)
+ HELPER(AssignExp, ast::DefaultConstVisitor)
+ HELPER(Ast, ast::DefaultConstVisitor)
+ HELPER(BreakExp, ast::DefaultConstVisitor)
+ HELPER(CallExp, ast::DefaultConstVisitor)
+ HELPER(CastExp, ast::DefaultConstVisitor)
+ HELPER(ChunkList, ast::DefaultConstVisitor)
+ HELPER(Field, ast::DefaultConstVisitor)
+ HELPER(FieldInit, ast::DefaultConstVisitor)
+ HELPER(FieldVar, ast::DefaultConstVisitor)
+ HELPER(ForExp, ast::DefaultConstVisitor)
+ HELPER(FunctionChunk, ast::DefaultConstVisitor)
+ HELPER(FunctionDec, ast::DefaultConstVisitor)
+ HELPER(IfExp, ast::DefaultConstVisitor)
+ HELPER(IntExp, ast::DefaultConstVisitor)
+ HELPER(LetExp, ast::DefaultConstVisitor)
+ HELPER(NameTy, ast::DefaultConstVisitor)
+ HELPER(NilExp, ast::DefaultConstVisitor)
+ HELPER(OpExp, ast::DefaultConstVisitor)
+ HELPER(RecordExp, ast::DefaultConstVisitor)
+ HELPER(RecordTy, ast::DefaultConstVisitor)
+ HELPER(SeqExp, ast::DefaultConstVisitor)
+ HELPER(SimpleVar, ast::DefaultConstVisitor)
+ HELPER(StringExp, ast::DefaultConstVisitor)
+ HELPER(SubscriptVar, ast::DefaultConstVisitor)
+ HELPER(TypeChunk, ast::DefaultConstVisitor)
+ HELPER(TypeDec, ast::DefaultConstVisitor)
+ HELPER(VarChunk, ast::DefaultConstVisitor)
+ HELPER(VarDec, ast::DefaultConstVisitor)
+ HELPER(WhileExp, ast::DefaultConstVisitor)
+
+ HELPER(ClassTy, ast::ObjectConstVisitor)
+ HELPER(MethodChunk, ast::ObjectConstVisitor)
+ HELPER(MethodCallExp, ast::ObjectConstVisitor)
+ HELPER(MethodDec, ast::ObjectConstVisitor)
+ HELPER(ObjectExp, ast::ObjectConstVisitor)
+
+ private:
+ PyObject* callback_{};
+
+ int id_{};
+ CallbackVisitor_Argument parent_arg_{};
+ PyObject* parent_obj_{};
+ };
+
+ ast::ConstVisitor* get_callback_visitor(PyObject* pyfunc)
+ {
+ if (!PyCallable_Check(pyfunc)) {
+ PyErr_SetString(PyExc_TypeError, "Need a callable object!");
+ return NULL;
+ }
+
+ return new CallbackVisitor(pyfunc);
+ }
+
+#endif
+}
+
+// Types.
+%include "ast/escapable.hh"
+%include "ast/typable.hh"
+%include "ast/type-constructor.hh"
+
+%include "ast/field.hh"
+%include "ast/field-init.hh"
+
+// Exps.
+%include "ast/exp.hh"
+%include "ast/array-exp.hh"
+%include "ast/assign-exp.hh"
+%include "ast/break-exp.hh"
+%include "ast/call-exp.hh"
+%include "ast/cast-exp.hh"
+%include "ast/for-exp.hh"
+%include "ast/if-exp.hh"
+%include "ast/int-exp.hh"
+%include "ast/let-exp.hh"
+%include "ast/method-call-exp.hh"
+%include "ast/nil-exp.hh"
+%include "ast/object-exp.hh"
+%include "ast/op-exp.hh"
+%include "ast/record-exp.hh"
+%include "ast/seq-exp.hh"
+%include "ast/string-exp.hh"
+%include "ast/while-exp.hh"
+
+// Vars.
+%include "ast/var.hh"
+%include "ast/simple-var.hh"
+%include "ast/field-var.hh"
+%include "ast/subscript-var.hh"
+
+// Tys.
+%include "ast/ty.hh"
+%include "ast/name-ty.hh"
+%include "ast/record-ty.hh"
+%include "ast/array-ty.hh"
+%include "ast/class-ty.hh"
+
+// ChunkInterface.
+%ignore ast::Chunk::operator[];
+%include "ast/chunk-interface.hh"
+%include "ast/dec.hh"
+%include "ast/function-dec.hh"
+%include "ast/method-dec.hh"
+%include "ast/var-dec.hh"
+%include "ast/type-dec.hh"
+%include "ast/chunk-list.hh"
+%include "ast/chunk.hh"
+
+%extend ast::Chunk
+{
+ int __len__()
+ {
+ return std::distance((*$self).begin(), (*$self).end());
+ }
+
+ D* __getitem__(int pos) throw(std::out_of_range)
+ {
+ int size = std::distance((*$self).begin(), (*$self).end());
+ if (pos >= size || pos < -size)
+ throw std::out_of_range("trying to access chunk item");
+ auto front = (*$self).begin();
+ std::advance(front, pos >= 0 ? pos : size + pos);
+ return *front;
+ }
+}
+
+%template(FunctionChunk) ast::Chunk<ast::FunctionDec>;
+%template(MethodChunk) ast::Chunk<ast::MethodDec>;
+%template(TypeChunk) ast::Chunk<ast::TypeDec>;
+%template(VarChunk) ast::Chunk<ast::VarDec>;
+%template(exps_type)std::vector<ast::Exp*>;
+%template(fieldinits_type) std::vector<ast::FieldInit*>;
+%template(fields_type) std::vector<ast::Field*>;
+
+%extend ast::ChunkList
+{
+ int __len__()
+ {
+ return std::distance((*$self).begin(), (*$self).end());
+ }
+
+ ast::ChunkInterface* __getitem__(int pos)
+ {
+ int size = std::distance((*$self).begin(), (*$self).end());
+ if (pos >= size || pos < -size)
+ throw std::out_of_range("trying to access chunk item");
+ auto front = (*$self).begin();
+ std::advance(front, pos >= 0 ? pos : size + pos);
+ return *front;
+ }
+}
+
+%include "ast/libast.hh"
diff --git a/tiger-compiler/tcsh/src/tiger_astclone.i b/tiger-compiler/tcsh/src/tiger_astclone.i
new file mode 100644
index 0000000..ca8438a
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_astclone.i
@@ -0,0 +1,17 @@
+// -*- C++ -*-
+
+%module tiger_astclone
+
+%import "tiger_ast.i"
+
+%{
+ #include <astclone/libastclone.hh>
+ #include "swig_real_type.hh"
+%}
+%inline
+{
+ ast::Ast* clone(const ast::Ast& tree)
+ {
+ return astclone::clone<ast::Ast>(tree);
+ }
+}
diff --git a/tiger-compiler/tcsh/src/tiger_bind.i b/tiger-compiler/tcsh/src/tiger_bind.i
new file mode 100644
index 0000000..00034cf
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_bind.i
@@ -0,0 +1,13 @@
+// -*- C++ -*-
+
+%module tiger_bind
+
+%import "tiger_misc.i"
+
+%include "std_string.i"
+
+%{
+ #include <bind/libbind.hh>
+%}
+
+%include "bind/libbind.hh"
diff --git a/tiger-compiler/tcsh/src/tiger_callgraph.i b/tiger-compiler/tcsh/src/tiger_callgraph.i
new file mode 100644
index 0000000..429c451
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_callgraph.i
@@ -0,0 +1,18 @@
+// -*- C++ -*-
+
+%module tiger_callgraph
+
+%include "std_string.i"
+
+%{
+ #include <callgraph/fundec-graph.hh>
+ #include <callgraph/libcallgraph.hh>
+%}
+
+namespace callgraph
+{
+ class FundecGraph;
+ using CallGraph = FundecGraph;
+}
+
+%include "callgraph/libcallgraph.hh"
diff --git a/tiger-compiler/tcsh/src/tiger_combine.i b/tiger-compiler/tcsh/src/tiger_combine.i
new file mode 100644
index 0000000..5ac5278
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_combine.i
@@ -0,0 +1,14 @@
+// -*- C++ -*-
+
+%module tiger_combine
+
+%include "std_pair.i"
+
+%{
+ #include <combine/libcombine.hh>
+ using namespace combine;
+%}
+
+%template() std::pair<overload::overfun_bindings_type, misc::error>;
+
+%include "combine/libcombine.hh"
diff --git a/tiger-compiler/tcsh/src/tiger_common.i b/tiger-compiler/tcsh/src/tiger_common.i
new file mode 100644
index 0000000..ff43652
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_common.i
@@ -0,0 +1,73 @@
+// -*- C++ -*-
+
+%module tiger_common
+
+%include std_string.i
+
+%{
+ #include <fstream>
+ #include <sstream>
+ #include <iostream>
+ #include <cassert>
+ #include <string>
+ #include <misc/timer.hh>
+%}
+
+// Warning 454: Setting a pointer/reference variable may leak memory.
+%warnfilter(454) Cout;
+%warnfilter(454) Cerr;
+%warnfilter(454) Clog;
+
+// Ofstream.
+%inline %{
+ struct Ofstream
+ {
+ Ofstream (const std::string& filename) : closed_ (false)
+ {
+ stream_ = new std::ofstream (filename.c_str ());
+ }
+
+ ~Ofstream ()
+ {
+ if (!closed_)
+ close ();
+ }
+
+ std::ostream&
+ to ()
+ {
+ assert (stream_);
+ return *stream_;
+ }
+
+ void
+ close ()
+ {
+ assert (stream_);
+ stream_->close ();
+ delete stream_;
+ closed_ = true;
+ }
+
+ std::ofstream* stream_;
+ bool closed_;
+ };
+
+ std::ostream&
+ get_cout()
+ {
+ return std::cout;
+ }
+
+ std::ostream&
+ get_cerr()
+ {
+ return std::cerr;
+ }
+
+ std::ostream* Cout = &std::cout;
+ std::ostream* Cerr = &std::cerr;
+ std::ostream* Clog = &std::clog;
+
+ misc::timer timer;
+%}
diff --git a/tiger-compiler/tcsh/src/tiger_desugar.i b/tiger-compiler/tcsh/src/tiger_desugar.i
new file mode 100644
index 0000000..94f6b82
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_desugar.i
@@ -0,0 +1,30 @@
+// -*- C++ -*-
+
+%module tiger_desugar
+
+%import "tiger_ast.i"
+
+%{
+ #include <desugar/libdesugar.hh>
+ #include <ast/all.hh>
+ #include "swig_real_type.hh"
+%}
+
+%rename(desugar) ast_desugar;
+%rename(raw_desugar) ast_raw_desugar;
+
+%inline {
+ast::Ast* ast_desugar(const ast::Ast& tree,
+ bool desugar_for = false,
+ bool desugar_string_cmp = false)
+{
+ return desugar::desugar<ast::Ast>(tree, desugar_for, desugar_string_cmp);
+}
+ast::Ast* ast_raw_desugar(const ast::Ast& tree,
+ bool desugar_for = false,
+ bool desugar_string_cmp = false)
+{
+ return desugar::raw_desugar<ast::Ast>(tree, desugar_for, desugar_string_cmp);
+}
+}
+
diff --git a/tiger-compiler/tcsh/src/tiger_escapes.i b/tiger-compiler/tcsh/src/tiger_escapes.i
new file mode 100644
index 0000000..b176790
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_escapes.i
@@ -0,0 +1,11 @@
+// -*- C++ -*-
+
+%module tiger_escapes
+
+%include "std_string.i"
+
+%{
+ #include <escapes/libescapes.hh>
+%}
+
+%include "escapes/libescapes.hh"
diff --git a/tiger-compiler/tcsh/src/tiger_llvmtranslate.i b/tiger-compiler/tcsh/src/tiger_llvmtranslate.i
new file mode 100644
index 0000000..70fade9
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_llvmtranslate.i
@@ -0,0 +1,39 @@
+// -*- C++ -*-
+
+%module tiger_llvmtranslate
+
+%include "std_string.i"
+
+%{
+ #include <llvmtranslate/libllvmtranslate.hh>
+ #include <llvmtranslate/tasks.hh>
+ #include <llvm/IR/IRPrintingPasses.h>
+ #include <llvm/Linker/Linker.h>
+ #include <llvm/IR/Module.h>
+%}
+
+%inline
+{
+ std::string translate(const ast::Ast& tree)
+ {
+ auto module = llvmtranslate::translate(tree);
+ auto runtime = llvmtranslate::runtime_get(*module.first);
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 7
+ auto link = llvm::Linker::LinkModules(module.second.get(), runtime.get());
+#else
+ auto link = llvm::Linker::linkModules(*module.second, std::move(runtime));
+#endif
+ postcondition(!link); // Returns true on error
+
+ std::string str;
+ llvm::raw_string_ostream out(str);
+ llvm::PrintModulePass printer{out};
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 8
+ printer.run(*module.second);
+#else
+ llvm::ModuleAnalysisManager dummy_mam;
+ printer.run(*module.second, dummy_mam);
+#endif
+ return out.str();
+ }
+}
diff --git a/tiger-compiler/tcsh/src/tiger_misc.i b/tiger-compiler/tcsh/src/tiger_misc.i
new file mode 100644
index 0000000..7e1470f
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_misc.i
@@ -0,0 +1,73 @@
+// -*- C++ -*-
+
+%module tiger_misc
+
+%include "std_string.i"
+
+%{
+ #include <sstream>
+
+ #include <misc/file-library.hh>
+ #include <misc/symbol.hh>
+ #include <misc/error.hh>
+ // The need for this using directive seems to be a SWIG bug. It is
+ // needed by the _wrap_error___lshift____SWIG_5 in the C++ wrapper
+ // generated by swig (tiger_bind-wrap.cc).
+ using misc::error;
+
+ #include <misc/timer.hh>
+%}
+
+%exception {
+ try {
+ $function
+ } catch (const misc::error& e)
+ {
+ std::ostringstream o;
+ o << e;
+ PyObject *err = Py_BuildValue("is", e.status_get_value(), o.str().c_str());
+ PyErr_SetObject(PyExc_RuntimeError, err);
+ SWIG_fail;
+ }
+}
+
+%include "misc/file-library.hh"
+
+// Save some warnings from SWIG.
+%ignore misc::error::operator=;
+%ignore misc::error::operator bool;
+
+// SWIG has trouble wrapping some of misc::error's operator<<'s; ignore them.
+%ignore misc::error::operator<<;
+
+%include "misc/error.hh"
+
+// Wrap misc::error::operator<<.
+%extend misc::error
+{
+ std::string __str__() const
+ {
+ std::ostringstream o;
+ o << *$self;
+ return o.str();
+ }
+
+ %pythoncode %{
+ @staticmethod
+ def error_type_message() -> dict[int, str]:
+ return { getattr(_tiger_misc, t): t[len("error_error_type_"):].upper() for t in dir(_tiger_misc) if t.startswith("error_error_type_") }
+ %}
+}
+
+%ignore misc::symbol::operator=;
+%ignore misc::symbol::operator<<;
+%include "misc/symbol.hh"
+%extend misc::symbol
+{
+ std::string __str__() const
+ {
+ return ($self)->get();
+ }
+}
+
+%include "misc/timer.hh"
diff --git a/tiger-compiler/tcsh/src/tiger_object.i b/tiger-compiler/tcsh/src/tiger_object.i
new file mode 100644
index 0000000..cd81eec
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_object.i
@@ -0,0 +1,32 @@
+// -*- C++ -*-
+
+%module tiger_object
+
+%import "tiger_ast.i"
+
+%{
+ #include <object/libobject.hh>
+ #include <ast/all.hh>
+ #include "swig_real_type.hh"
+%}
+
+%import "tiger_misc.i"
+
+%rename(desugar) object_desugar;
+%rename(raw_desugar) object_raw_desugar;
+
+%inline {
+ast::Ast* object_desugar(const ast::Ast& tree,
+ const object::class_names_type& class_names)
+{
+ return object::desugar<ast::Ast>(tree, class_names);
+}
+ast::Ast* object_raw_desugar(const ast::Ast& tree,
+ const object::class_names_type& class_names)
+{
+ return object::raw_desugar<ast::Ast>(tree, class_names);
+}
+}
+
+%include "object/fwd.hh"
+%include "object/libobject.hh"
diff --git a/tiger-compiler/tcsh/src/tiger_overload.i b/tiger-compiler/tcsh/src/tiger_overload.i
new file mode 100644
index 0000000..7a9090c
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_overload.i
@@ -0,0 +1,14 @@
+// -*- C++ -*-
+
+%module tiger_overload
+
+%include "std_pair.i"
+
+%{
+ #include <overload/liboverload.hh>
+ using namespace overload;
+%}
+
+%template() std::pair<overfun_bindings_type, misc::error>;
+
+%include "overload/liboverload.hh"
diff --git a/tiger-compiler/tcsh/src/tiger_parse.i b/tiger-compiler/tcsh/src/tiger_parse.i
new file mode 100644
index 0000000..1d62bd8
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_parse.i
@@ -0,0 +1,102 @@
+// -*- C++ -*-
+
+%module tiger_parse
+
+%include "std_string.i"
+%include "std_pair.i"
+
+%{
+ #include <sstream>
+ #include <misc/error.hh>
+ #include <parse/location.hh>
+ #include <parse/tiger-driver.hh>
+ #include <parse/libparse.hh>
+%}
+
+%import "tiger_misc.i"
+
+%include "parse/fwd.hh"
+
+// Explicit instantiation of MetavarMap's.
+%include "parse/metavar-map.hh"
+%template(MetavarMap) parse::MetavarMap<ast::Exp>;
+%template(MetavarVar) parse::MetavarMap<ast::Var>;
+%template(MetavarNameTy) parse::MetavarMap<ast::NameTy>;
+%template(MetavarChunkList) parse::MetavarMap<ast::ChunkList>;
+%template(MetavarTweast) parse::MetavarMap<parse::Tweast>;
+
+/*------------------.
+| parse::position. |
+`------------------*/
+
+// Replace parse::position's ctor and ignore parse::position::initialize,
+// as Python is unable to parse the literal `1u' used as default value
+// of two of their arguments.
+%extend parse::position
+{
+ // External ctor. For more information, see
+ // http://www.nabble.com/Java---adding-code-to-a-constructor-td20210601.html
+ position (const std::string* f = nullptr,
+ unsigned int l = 1, unsigned int c = 1)
+ {
+ return new parse::position(f, l, c);
+ }
+}
+%ignore parse::position::position;
+%ignore parse::position::initialize;
+%extend parse::position
+{
+ std::string
+ __str__ () const
+ {
+ std::ostringstream o;
+ o << *$self;
+ return o.str ();
+ }
+}
+
+/*------------------.
+| parse::location. |
+`------------------*/
+
+// Ignore parse::location::initialize, as Python is unable to
+// parse the literal `1u' used as default value of two of its
+// arguments.
+%ignore parse::location::initialize;
+%include "parse/location.hh"
+%extend parse::location
+{
+ std::string
+ __str__ () const
+ {
+ std::ostringstream o;
+ o << *$self;
+ return o.str ();
+ }
+}
+
+/*-----------.
+| libparse. |
+`-----------*/
+
+%import "parse/tweast.hh"
+%include "parse/tiger-driver.hh"
+%inline
+{
+ namespace parse
+ {
+ // Parse a Tiger file, return the corresponding abstract syntax.
+ ast::ChunkList*
+ parse (const std::string& prelude, const std::string& fname,
+ misc::file_library& library)
+ {
+ std::pair<ast::ChunkList*, misc::error> res =
+ parse (prelude, fname, library, false, false, true);
+ res.second.exit_on_error();
+ return res.first;
+ }
+ }
+
+}
+
+%include "parse/libparse.hh"
diff --git a/tiger-compiler/tcsh/src/tiger_type.i b/tiger-compiler/tcsh/src/tiger_type.i
new file mode 100644
index 0000000..5679873
--- /dev/null
+++ b/tiger-compiler/tcsh/src/tiger_type.i
@@ -0,0 +1,91 @@
+// -*- C++ -*-
+
+%module tiger_type
+
+%include "std_string.i"
+
+%include "misc/singleton.hh"
+
+%{
+ #include "helper.hh"
+ #include <type/libtype.hh>
+ #include <misc/singleton.hh>
+
+ #include <type/fwd.hh>
+ #include <type/type.hh>
+ #include <type/array.hh>
+ #include <type/builtin-types.hh>
+ #include <type/class.hh>
+ #include <type/field.hh>
+ #include <type/function.hh>
+ #include <type/method.hh>
+ #include <type/named.hh>
+ #include <type/nil.hh>
+ #include <type/record.hh>
+
+ #include <type/visitor.hh>
+ #include <type/pretty-printer.hh>
+%}
+
+%typemap(ret) type::Type* {
+ $result = _get_swig_real_type_type(*$1);
+}
+
+%warnfilter(401);
+
+%include "type/fwd.hh"
+%include "type/type.hh"
+
+%extend type::Type
+{
+ std::string __str__() const
+ {
+ std::ostringstream o;
+ o << *$self;
+ return o.str();
+ }
+}
+%{
+namespace {
+ using namespace type;
+ class SwigTypeVisitor : public ConstVisitor
+ {
+ public:
+ PyObject *result;
+
+#define STHELPER(TYPE) STCONVERT(TYPE, p_type__ ## TYPE)
+ STHELPER(Array)
+ STHELPER(Class)
+ STHELPER(Function)
+ STHELPER(Int)
+ STHELPER(Method)
+ STHELPER(Named)
+ STHELPER(Nil)
+ STHELPER(Record)
+ STHELPER(String)
+ STHELPER(Void)
+#undef STHELPER
+ };
+} // namespace
+%}
+%inline %{
+PyObject *_get_swig_real_type_type(const type::Type& e)
+{
+ SwigTypeVisitor stv;
+ e.accept(stv);
+ return stv.result;
+}
+%}
+
+
+%include "type/array.hh"
+%include "type/builtin-types.hh"
+%include "type/class.hh"
+%include "type/field.hh"
+%include "type/function.hh"
+%include "type/method.hh"
+%include "type/named.hh"
+%include "type/nil.hh"
+%include "type/record.hh"
+
+%include "type/libtype.hh"