mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-12 20:52:50 -06:00
started the linearizer
This commit is contained in:
@@ -21,6 +21,8 @@ libpgf_la_SOURCES = \
|
|||||||
pgf/printer.h \
|
pgf/printer.h \
|
||||||
pgf/typechecker.cxx \
|
pgf/typechecker.cxx \
|
||||||
pgf/typechecker.h \
|
pgf/typechecker.h \
|
||||||
|
pgf/linearizer.cxx \
|
||||||
|
pgf/linearizer.h \
|
||||||
pgf/data.cxx \
|
pgf/data.cxx \
|
||||||
pgf/data.h \
|
pgf/data.h \
|
||||||
pgf/expr.cxx \
|
pgf/expr.cxx \
|
||||||
|
|||||||
86
src/runtime/c/pgf/linearizer.cxx
Normal file
86
src/runtime/c/pgf/linearizer.cxx
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
#include "data.h"
|
||||||
|
#include "linearizer.h"
|
||||||
|
|
||||||
|
PgfLinearizer::~PgfLinearizer()
|
||||||
|
{
|
||||||
|
while (first != NULL) {
|
||||||
|
TreeNode *next = first->next;
|
||||||
|
delete first;
|
||||||
|
first = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfExpr PgfLinearizer::eabs(PgfBindType btype, PgfText *name, PgfExpr body)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfExpr PgfLinearizer::eapp(PgfExpr fun, PgfExpr arg)
|
||||||
|
{
|
||||||
|
TreeNode *args = this->args;
|
||||||
|
this->args = NULL;
|
||||||
|
TreeNode *node = (TreeNode*) m->match_expr(this, arg);
|
||||||
|
node->next_arg = args;
|
||||||
|
this->args = node;
|
||||||
|
|
||||||
|
m->match_expr(this, fun);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfExpr PgfLinearizer::elit(PgfLiteral lit)
|
||||||
|
{
|
||||||
|
return m->match_lit(this, lit);
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfExpr PgfLinearizer::emeta(PgfMetaId meta)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfExpr PgfLinearizer::efun(PgfText *name)
|
||||||
|
{
|
||||||
|
ref<PgfConcrLin> lin = namespace_lookup(concr->lins, name);
|
||||||
|
TreeNode *node = new TreeNode(this, lin);
|
||||||
|
return (PgfExpr) node;
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfExpr PgfLinearizer::evar(int index)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfExpr PgfLinearizer::etyped(PgfExpr expr, PgfType ty)
|
||||||
|
{
|
||||||
|
return m->match_expr(this, expr);
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfExpr PgfLinearizer::eimplarg(PgfExpr expr)
|
||||||
|
{
|
||||||
|
return m->match_expr(this, expr);
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfLiteral PgfLinearizer::lint(size_t size, uintmax_t *v)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfLiteral PgfLinearizer::lflt(double v)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfLiteral PgfLinearizer::lstr(PgfText *v)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PgfType PgfLinearizer::dtyp(size_t n_hypos, PgfTypeHypo *hypos,
|
||||||
|
PgfText *cat,
|
||||||
|
size_t n_exprs, PgfExpr *exprs)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PgfLinearizer::free_ref(object x)
|
||||||
|
{
|
||||||
|
}
|
||||||
60
src/runtime/c/pgf/linearizer.h
Normal file
60
src/runtime/c/pgf/linearizer.h
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#ifndef LINEARIZER_H
|
||||||
|
#define LINEARIZER_H
|
||||||
|
|
||||||
|
class PGF_INTERNAL_DECL PgfLinearizer : public PgfUnmarshaller {
|
||||||
|
ref<PgfConcr> concr;
|
||||||
|
PgfMarshaller *m;
|
||||||
|
|
||||||
|
struct TreeNode {
|
||||||
|
TreeNode *next;
|
||||||
|
TreeNode *next_arg;
|
||||||
|
TreeNode *args;
|
||||||
|
|
||||||
|
ref<PgfConcrLin> lin;
|
||||||
|
|
||||||
|
TreeNode(PgfLinearizer *linearizer, ref<PgfConcrLin> lin) {
|
||||||
|
this->next = linearizer->root;
|
||||||
|
this->next_arg = NULL;
|
||||||
|
this->args = linearizer->args;
|
||||||
|
this->lin = lin;
|
||||||
|
|
||||||
|
if (linearizer->first == NULL)
|
||||||
|
linearizer->first = this;
|
||||||
|
|
||||||
|
linearizer->root = this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TreeNode *root;
|
||||||
|
TreeNode *first;
|
||||||
|
TreeNode *args;
|
||||||
|
|
||||||
|
public:
|
||||||
|
PgfLinearizer(ref<PgfConcr> concr, PgfMarshaller *m) {
|
||||||
|
this->concr = concr;
|
||||||
|
this->m = m;
|
||||||
|
this->root = NULL;
|
||||||
|
this->first = NULL;
|
||||||
|
this->args = NULL;
|
||||||
|
};
|
||||||
|
|
||||||
|
~PgfLinearizer();
|
||||||
|
|
||||||
|
virtual PgfExpr eabs(PgfBindType btype, PgfText *name, PgfExpr body);
|
||||||
|
virtual PgfExpr eapp(PgfExpr fun, PgfExpr arg);
|
||||||
|
virtual PgfExpr elit(PgfLiteral lit);
|
||||||
|
virtual PgfExpr emeta(PgfMetaId meta);
|
||||||
|
virtual PgfExpr efun(PgfText *name);
|
||||||
|
virtual PgfExpr evar(int index);
|
||||||
|
virtual PgfExpr etyped(PgfExpr expr, PgfType typ);
|
||||||
|
virtual PgfExpr eimplarg(PgfExpr expr);
|
||||||
|
virtual PgfLiteral lint(size_t size, uintmax_t *v);
|
||||||
|
virtual PgfLiteral lflt(double v);
|
||||||
|
virtual PgfLiteral lstr(PgfText *v);
|
||||||
|
virtual PgfType dtyp(size_t n_hypos, PgfTypeHypo *hypos,
|
||||||
|
PgfText *cat,
|
||||||
|
size_t n_exprs, PgfExpr *exprs);
|
||||||
|
virtual void free_ref(object x);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "writer.h"
|
#include "writer.h"
|
||||||
#include "printer.h"
|
#include "printer.h"
|
||||||
#include "typechecker.h"
|
#include "typechecker.h"
|
||||||
|
#include "linearizer.h"
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pgf_exn_clear(PgfExn* err)
|
pgf_exn_clear(PgfExn* err)
|
||||||
@@ -1846,6 +1847,27 @@ int pgf_has_linearization(PgfDB *db, PgfConcrRevision revision,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PGF_API
|
||||||
|
PgfText *pgf_linearize(PgfDB *db, PgfConcrRevision revision,
|
||||||
|
PgfExpr expr, PgfMarshaller *m,
|
||||||
|
PgfExn* err)
|
||||||
|
{
|
||||||
|
PGF_API_BEGIN {
|
||||||
|
DB_scope scope(db, READER_SCOPE);
|
||||||
|
|
||||||
|
ref<PgfConcr> concr = PgfDB::revision2concr(revision);
|
||||||
|
PgfLinearizer linearizer(concr, m);
|
||||||
|
m->match_expr(&linearizer, expr);
|
||||||
|
|
||||||
|
PgfText *res = (PgfText*) malloc(sizeof(PgfText)+1);
|
||||||
|
res->size = 0;
|
||||||
|
res->text[0] = 0;
|
||||||
|
return res;
|
||||||
|
} PGF_API_END
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
PGF_API
|
PGF_API
|
||||||
PgfLiteral pgf_get_global_flag(PgfDB *db, PgfRevision revision,
|
PgfLiteral pgf_get_global_flag(PgfDB *db, PgfRevision revision,
|
||||||
PgfText *name,
|
PgfText *name,
|
||||||
|
|||||||
@@ -569,6 +569,11 @@ PGF_API_DECL
|
|||||||
int pgf_has_linearization(PgfDB *db, PgfConcrRevision revision,
|
int pgf_has_linearization(PgfDB *db, PgfConcrRevision revision,
|
||||||
PgfText *name, PgfExn *err);
|
PgfText *name, PgfExn *err);
|
||||||
|
|
||||||
|
PGF_API_DECL
|
||||||
|
PgfText *pgf_linearize(PgfDB *db, PgfConcrRevision revision,
|
||||||
|
PgfExpr expr, PgfMarshaller *m,
|
||||||
|
PgfExn* err);
|
||||||
|
|
||||||
PGF_API_DECL
|
PGF_API_DECL
|
||||||
PgfLiteral pgf_get_global_flag(PgfDB *db, PgfRevision revision,
|
PgfLiteral pgf_get_global_flag(PgfDB *db, PgfRevision revision,
|
||||||
PgfText *name,
|
PgfText *name,
|
||||||
|
|||||||
@@ -578,7 +578,13 @@ hasLinearization c name =
|
|||||||
|
|
||||||
-- | Linearizes an expression as a string in the language
|
-- | Linearizes an expression as a string in the language
|
||||||
linearize :: Concr -> Expr -> String
|
linearize :: Concr -> Expr -> String
|
||||||
linearize lang e = error "TODO: linearize"
|
linearize c e =
|
||||||
|
unsafePerformIO $
|
||||||
|
withForeignPtr (c_revision c) $ \c_revision ->
|
||||||
|
bracket (newStablePtr e) freeStablePtr $ \c_e ->
|
||||||
|
withForeignPtr marshaller $ \m ->
|
||||||
|
bracket (withPgfExn "linearize" (pgf_linearize (c_db c) c_revision c_e m)) free $ \c_text ->
|
||||||
|
peekText c_text
|
||||||
|
|
||||||
-- | Generates all possible linearizations of an expression
|
-- | Generates all possible linearizations of an expression
|
||||||
linearizeAll :: Concr -> Expr -> [String]
|
linearizeAll :: Concr -> Expr -> [String]
|
||||||
|
|||||||
@@ -197,6 +197,8 @@ foreign import ccall pgf_drop_lin :: Ptr PgfDB -> Ptr Concr -> Ptr PgfText -> Pt
|
|||||||
|
|
||||||
foreign import ccall pgf_has_linearization :: Ptr PgfDB -> Ptr Concr -> Ptr PgfText -> Ptr PgfExn -> IO CInt
|
foreign import ccall pgf_has_linearization :: Ptr PgfDB -> Ptr Concr -> Ptr PgfText -> Ptr PgfExn -> IO CInt
|
||||||
|
|
||||||
|
foreign import ccall pgf_linearize :: Ptr PgfDB -> Ptr Concr -> StablePtr Expr -> Ptr PgfMarshaller -> Ptr PgfExn -> IO (Ptr PgfText)
|
||||||
|
|
||||||
foreign import ccall pgf_get_global_flag :: Ptr PgfDB -> Ptr PGF -> Ptr PgfText -> Ptr PgfUnmarshaller -> Ptr PgfExn -> IO (StablePtr Literal)
|
foreign import ccall pgf_get_global_flag :: Ptr PgfDB -> Ptr PGF -> Ptr PgfText -> Ptr PgfUnmarshaller -> Ptr PgfExn -> IO (StablePtr Literal)
|
||||||
|
|
||||||
foreign import ccall pgf_set_global_flag :: Ptr PgfDB -> Ptr PGF -> Ptr PgfText -> StablePtr Literal -> Ptr PgfMarshaller -> Ptr PgfExn -> IO ()
|
foreign import ccall pgf_set_global_flag :: Ptr PgfDB -> Ptr PGF -> Ptr PgfText -> StablePtr Literal -> Ptr PgfMarshaller -> Ptr PgfExn -> IO ()
|
||||||
|
|||||||
Reference in New Issue
Block a user