corinthia-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmke...@apache.org
Subject incubator-corinthia git commit: Flat: Separate functions for each expression type
Date Tue, 14 Jul 2015 11:11:57 GMT
Repository: incubator-corinthia
Updated Branches:
  refs/heads/master 9dcccd197 -> aa7176ab9


Flat: Separate functions for each expression type

Define separate constructor and accessor functions for each type of
expression. This gives us a little more type safety, albeit at runtime
in the form of assertion checks, thus catching any mistakes caused by
trying to access an Expression object in the wrong way for it's type
(e.g. trying to get the string value associated with a sequence).


Project: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/commit/aa7176ab
Tree: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/tree/aa7176ab
Diff: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/diff/aa7176ab

Branch: refs/heads/master
Commit: aa7176ab9b2a1ced56d32de2d6a30a9a0bbbb452
Parents: 9dcccd1
Author: Peter Kelly <peter@uxproductivity.com>
Authored: Tue Jul 14 18:09:34 2015 +0700
Committer: Peter Kelly <peter@uxproductivity.com>
Committed: Tue Jul 14 18:11:46 2015 +0700

----------------------------------------------------------------------
 experiments/flat/src/Builtin.c    |  24 +--
 experiments/flat/src/Expression.c | 305 ++++++++++++++++++++++++++-------
 experiments/flat/src/Expression.h |  53 +++++-
 experiments/flat/src/Grammar.c    |   6 +-
 4 files changed, 307 insertions(+), 81 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/aa7176ab/experiments/flat/src/Builtin.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Builtin.c b/experiments/flat/src/Builtin.c
index a5d3923..9b3c32e 100644
--- a/experiments/flat/src/Builtin.c
+++ b/experiments/flat/src/Builtin.c
@@ -20,29 +20,29 @@
 #include <stdlib.h>
 
 #define ref(name)       ExpressionNewIdent(name)
-#define seq(...)        makeExpression(SequenceExpr,__VA_ARGS__,NULL)
-#define choice(...)     makeExpression(ChoiceExpr,__VA_ARGS__,NULL)
-#define and(...)        makeExpression(AndExpr,__VA_ARGS__,NULL)
-#define not(...)        makeExpression(NotExpr,__VA_ARGS__,NULL)
-#define opt(...)        makeExpression(OptExpr,__VA_ARGS__,NULL)
-#define star(...)       makeExpression(StarExpr,__VA_ARGS__,NULL)
-#define plus(...)       makeExpression(PlusExpr,__VA_ARGS__,NULL)
+#define choice(...)     makeExpression(ExpressionNewChoice,__VA_ARGS__,NULL)
+#define seq(...)        makeExpression(ExpressionNewSequence,__VA_ARGS__,NULL)
+#define and(child)      ExpressionNewAnd(child)
+#define not(child)      ExpressionNewNot(child)
+#define opt(child)      ExpressionNewOpt(child)
+#define star(child)     ExpressionNewStar(child)
+#define plus(child)     ExpressionNewPlus(child)
 #define lit(value)      ExpressionNewLit(value)
-#define cls(...)        makeExpression(ClassExpr,__VA_ARGS__,NULL)
-#define dot()           makeExpression(DotExpr,NULL)
+#define cls(...)        makeExpression(ExpressionNewClass,__VA_ARGS__,NULL)
+#define dot()           ExpressionNewDot()
 #define range(lo,hi)    ExpressionNewRange(lo,hi)
 
-static Expression *makeExpression(ExprKind kind, ...)
+static Expression *makeExpression(Expression *(*fun)(int,Expression **), ...)
 {
     Expression *children[20];
     int count = 0;
     va_list ap;
-    va_start(ap,kind);
+    va_start(ap,fun);
     Expression *expr;
     while ((count < 20) && ((expr = va_arg(ap,Expression *)) != NULL))
         children[count++] = expr;
     va_end(ap);
-    return ExpressionNew(kind,count,children);
+    return fun(count,children);
 }
 
 Grammar *GrammarNewBuiltin(void)

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/aa7176ab/experiments/flat/src/Expression.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Expression.c b/experiments/flat/src/Expression.c
index 436a9cd..c4d30d5 100644
--- a/experiments/flat/src/Expression.c
+++ b/experiments/flat/src/Expression.c
@@ -30,18 +30,81 @@ struct Expression {
     Expression *children[];
 };
 
-Expression *ExpressionNew(ExprKind kind, int count, Expression **children)
+Expression *ExpressionNewChoice(int count, Expression **children)
 {
+    for (int i = 0; i < count; i++)
+        assert(children[i] != NULL);
     Expression *expr = (Expression *)calloc(1,sizeof(Expression)+count*sizeof(Expression
*));
-    expr->kind = kind;
-    expr->value = NULL;
+    expr->kind = ChoiceExpr;
     expr->count = count;
     memcpy(expr->children,children,count*sizeof(Expression *));
     return expr;
 }
 
+Expression *ExpressionNewSequence(int count, Expression **children)
+{
+    for (int i = 0; i < count; i++)
+        assert(children[i] != NULL);
+    Expression *expr = (Expression *)calloc(1,sizeof(Expression)+count*sizeof(Expression
*));
+    expr->kind = SequenceExpr;
+    expr->count = count;
+    memcpy(expr->children,children,count*sizeof(Expression *));
+    return expr;
+}
+
+Expression *ExpressionNewAnd(Expression *child)
+{
+    assert(child != NULL);
+    Expression *expr = (Expression *)calloc(1,sizeof(Expression)+1*sizeof(Expression *));
+    expr->kind = AndExpr;
+    expr->count = 1;
+    expr->children[0] = child;
+    return expr;
+}
+
+Expression *ExpressionNewNot(Expression *child)
+{
+    assert(child != NULL);
+    Expression *expr = (Expression *)calloc(1,sizeof(Expression)+1*sizeof(Expression *));
+    expr->kind = NotExpr;
+    expr->count = 1;
+    expr->children[0] = child;
+    return expr;
+}
+
+Expression *ExpressionNewOpt(Expression *child)
+{
+    assert(child != NULL);
+    Expression *expr = (Expression *)calloc(1,sizeof(Expression)+1*sizeof(Expression *));
+    expr->kind = OptExpr;
+    expr->count = 1;
+    expr->children[0] = child;
+    return expr;
+}
+
+Expression *ExpressionNewStar(Expression *child)
+{
+    assert(child != NULL);
+    Expression *expr = (Expression *)calloc(1,sizeof(Expression)+1*sizeof(Expression *));
+    expr->kind = StarExpr;
+    expr->count = 1;
+    expr->children[0] = child;
+    return expr;
+}
+
+Expression *ExpressionNewPlus(Expression *child)
+{
+    assert(child != NULL);
+    Expression *expr = (Expression *)calloc(1,sizeof(Expression)+1*sizeof(Expression *));
+    expr->kind = PlusExpr;
+    expr->count = 1;
+    expr->children[0] = child;
+    return expr;
+}
+
 Expression *ExpressionNewIdent(const char *ident)
 {
+    assert(ident != NULL);
     Expression *expr = (Expression *)calloc(1,sizeof(Expression)+1*sizeof(Expression *));
     expr->kind = IdentExpr;
     expr->value = strdup(ident);
@@ -52,6 +115,7 @@ Expression *ExpressionNewIdent(const char *ident)
 
 Expression *ExpressionNewLit(const char *value)
 {
+    assert(value != NULL);
     Expression *expr = (Expression *)calloc(1,sizeof(Expression));
     expr->kind = LitExpr;
     expr->value = strdup(value);
@@ -59,6 +123,26 @@ Expression *ExpressionNewLit(const char *value)
     return expr;
 }
 
+Expression *ExpressionNewClass(int count, Expression **children)
+{
+    for (int i = 0; i < count; i++) {
+        assert(children[i] != NULL);
+        assert(children[i]->kind == RangeExpr);
+    }
+    Expression *expr = (Expression *)calloc(1,sizeof(Expression)+count*sizeof(Expression
*));
+    expr->kind = ClassExpr;
+    expr->count = count;
+    memcpy(expr->children,children,count*sizeof(Expression *));
+    return expr;
+}
+
+Expression *ExpressionNewDot(void)
+{
+    Expression *expr = (Expression *)calloc(1,sizeof(Expression));
+    expr->kind = DotExpr;
+    return expr;
+}
+
 Expression *ExpressionNewRange(int lo, int hi)
 {
     Expression *expr = (Expression *)calloc(1,sizeof(Expression));
@@ -83,38 +167,6 @@ void ExpressionFree(Expression *expr)
     free(expr);
 }
 
-ExprKind ExpressionKind(Expression *expr)
-{
-    return expr->kind;
-}
-
-Expression *ExpressionChild(Expression *expr, int index)
-{
-    if ((index < 0) || (index >= expr->count))
-        return NULL;
-    return expr->children[index];
-}
-
-int ExpressionCount(Expression *expr)
-{
-    return expr->count;
-}
-
-const char *ExpressionValue(Expression *expr)
-{
-    return expr->value;
-}
-
-int ExpressionStart(Expression *expr)
-{
-    return expr->start;
-}
-
-int ExpressionEnd(Expression *expr)
-{
-    return expr->end;
-}
-
 static int ExprKindPrecedence(ExprKind kind)
 {
     switch (kind) {
@@ -201,63 +253,55 @@ void ExpressionPrint(Expression *expr, int highestPrecedence, const
char *indent
     }
     switch (expr->kind) {
         case ChoiceExpr:
-            for (int i = 0; i < ExpressionCount(expr); i++) {
+            for (int i = 0; i < ExprChoiceCount(expr); i++) {
                 if (i > 0) {
                     if (indent != NULL)
                         printf("\n%s/ ",indent);
                     else
                         printf(" / ");
                 }
-                ExpressionPrint(ExpressionChild(expr,i),highestPrecedence,NULL);
+                ExpressionPrint(ExprChoiceChildAt(expr,i),highestPrecedence,NULL);
             }
             break;
         case SequenceExpr: {
-            for (int i = 0; i < ExpressionCount(expr); i++) {
+            for (int i = 0; i < ExprSequenceCount(expr); i++) {
                 if (i > 0) {
                     printf(" ");
                 }
-                ExpressionPrint(ExpressionChild(expr,i),highestPrecedence,NULL);
+                ExpressionPrint(ExprSequenceChildAt(expr,i),highestPrecedence,NULL);
             }
             break;
         }
         case AndExpr:
-            assert(ExpressionCount(expr) == 1);
             printf("&");
-            ExpressionPrint(ExpressionChild(expr,0),highestPrecedence,NULL);
+            ExpressionPrint(ExprAndChild(expr),highestPrecedence,NULL);
             break;
         case NotExpr:
-            assert(ExpressionCount(expr) == 1);
             printf("!");
-            ExpressionPrint(ExpressionChild(expr,0),highestPrecedence,NULL);
+            ExpressionPrint(ExprNotChild(expr),highestPrecedence,NULL);
             break;
         case OptExpr:
-            assert(ExpressionCount(expr) == 1);
-            ExpressionPrint(ExpressionChild(expr,0),highestPrecedence,NULL);
+            ExpressionPrint(ExprOptChild(expr),highestPrecedence,NULL);
             printf("?");
             break;
         case StarExpr:
-            assert(ExpressionCount(expr) == 1);
-            ExpressionPrint(ExpressionChild(expr,0),highestPrecedence,NULL);
+            ExpressionPrint(ExprStarChild(expr),highestPrecedence,NULL);
             printf("*");
             break;
         case PlusExpr:
-            assert(ExpressionCount(expr) == 1);
-            ExpressionPrint(ExpressionChild(expr,0),highestPrecedence,NULL);
+            ExpressionPrint(ExprPlusChild(expr),highestPrecedence,NULL);
             printf("+");
             break;
         case IdentExpr:
-            assert(ExpressionValue(expr) != NULL);
-            printf("%s",ExpressionValue(expr));
+            printf("%s",ExprIdentValue(expr));
             break;
         case LitExpr:
-            assert(ExpressionValue(expr) != NULL);
-            printLiteral(ExpressionValue(expr));
+            printLiteral(ExprLitValue(expr));
             break;
         case ClassExpr:
             printf("[");
-            for (int i = 0; i < ExpressionCount(expr); i++) {
-                assert(ExpressionKind(ExpressionChild(expr,i)) == RangeExpr);
-                ExpressionPrint(ExpressionChild(expr,i),highestPrecedence,NULL);
+            for (int i = 0; i < ExprClassCount(expr); i++) {
+                ExpressionPrint(ExprClassChildAt(expr,i),highestPrecedence,NULL);
             }
             printf("]");
             break;
@@ -265,8 +309,8 @@ void ExpressionPrint(Expression *expr, int highestPrecedence, const char
*indent
             printf(".");
             break;
         case RangeExpr: {
-            int start = ExpressionStart(expr);
-            int end = ExpressionEnd(expr);
+            int start = ExprRangeStart(expr);
+            int end = ExprRangeEnd(expr);
             if (start+1 == end) {
                 printEscapedRangeChar(start);
             }
@@ -282,10 +326,153 @@ void ExpressionPrint(Expression *expr, int highestPrecedence, const
char *indent
         printf(")");
 }
 
-void ExpressionSetTarget(Expression *expr, Expression *target)
+ExprKind ExpressionKind(Expression *expr)
+{
+    return expr->kind;
+}
+
+int ExpressionCount(Expression *expr)
+{
+    return expr->count;
+}
+
+Expression *ExpressionChildAt(Expression *expr, int index)
+{
+    if ((index < 0) || (index >= expr->count))
+        return NULL;
+    return expr->children[index];
+}
+
+// Choice
+
+int ExprChoiceCount(Expression *expr)
+{
+    assert(expr->kind == ChoiceExpr);
+    return expr->count;
+}
+
+Expression *ExprChoiceChildAt(Expression *expr, int index)
+{
+    assert(expr->kind == ChoiceExpr);
+    assert(index >= 0);
+    assert(index < expr->count);
+    return expr->children[index];
+}
+
+// Sequence
+
+int ExprSequenceCount(Expression *expr)
+{
+    assert(expr->kind == SequenceExpr);
+    return expr->count;
+}
+
+Expression *ExprSequenceChildAt(Expression *expr, int index)
+{
+    assert(expr->kind == SequenceExpr);
+    assert(index >= 0);
+    assert(index < expr->count);
+    return expr->children[index];
+}
+
+// And, Not, Opt, Star, Plus
+
+Expression *ExprAndChild(Expression *expr)
+{
+    assert(expr->kind == AndExpr);
+    assert(expr->count == 1);
+    assert(expr->children[0] != NULL);
+    return expr->children[0];
+}
+
+Expression *ExprNotChild(Expression *expr)
+{
+    assert(expr->kind == NotExpr);
+    assert(expr->count == 1);
+    assert(expr->children[0] != NULL);
+    return expr->children[0];
+}
+
+Expression *ExprOptChild(Expression *expr)
+{
+    assert(expr->kind == OptExpr);
+    assert(expr->count == 1);
+    assert(expr->children[0] != NULL);
+    return expr->children[0];
+}
+
+Expression *ExprStarChild(Expression *expr)
+{
+    assert(expr->kind == StarExpr);
+    assert(expr->count == 1);
+    assert(expr->children[0] != NULL);
+    return expr->children[0];
+}
+
+Expression *ExprPlusChild(Expression *expr)
+{
+    assert(expr->kind == PlusExpr);
+    assert(expr->count == 1);
+    assert(expr->children[0] != NULL);
+    return expr->children[0];
+}
+
+// Ident, Lit
+
+const char *ExprIdentValue(Expression *expr)
+{
+    assert(expr->kind == IdentExpr);
+    return expr->value;
+}
+
+Expression *ExprIdentTarget(Expression *expr)
+{
+    assert(expr->kind == IdentExpr);
+    assert(expr->count == 1);
+    assert(expr->children[0] != NULL);
+    return expr->children[0];
+}
+
+void ExprIdentSetTarget(Expression *expr, Expression *target)
 {
     assert(expr->kind == IdentExpr);
     assert(expr->count == 1);
     assert(expr->children[0] == NULL);
     expr->children[0] = target;
 }
+
+const char *ExprLitValue(Expression *expr)
+{
+    assert(expr->kind == LitExpr);
+    return expr->value;
+}
+
+// Class
+
+int ExprClassCount(Expression *expr)
+{
+    assert(expr->kind == ClassExpr);
+    return expr->count;
+}
+
+Expression *ExprClassChildAt(Expression *expr, int index)
+{
+    assert(expr->kind == ClassExpr);
+    assert(index >= 0);
+    assert(index < expr->count);
+    return expr->children[index];
+}
+
+// Range
+
+int ExprRangeStart(Expression *expr)
+{
+    assert(expr->kind == RangeExpr);
+    return expr->start;
+}
+
+int ExprRangeEnd(Expression *expr)
+{
+    assert(expr->kind == RangeExpr);
+    return expr->end;
+}

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/aa7176ab/experiments/flat/src/Expression.h
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Expression.h b/experiments/flat/src/Expression.h
index 348deb9..e38614e 100644
--- a/experiments/flat/src/Expression.h
+++ b/experiments/flat/src/Expression.h
@@ -88,17 +88,56 @@ typedef enum {
 
 typedef struct Expression Expression;
 
-Expression *ExpressionNew(ExprKind kind, int count, Expression **children);
+Expression *ExpressionNewChoice(int count, Expression **children);
+Expression *ExpressionNewSequence(int count, Expression **children);
+Expression *ExpressionNewAnd(Expression *child);
+Expression *ExpressionNewNot(Expression *child);
+Expression *ExpressionNewOpt(Expression *child);
+Expression *ExpressionNewStar(Expression *child);
+Expression *ExpressionNewPlus(Expression *child);
 Expression *ExpressionNewIdent(const char *ident);
 Expression *ExpressionNewLit(const char *value);
+Expression *ExpressionNewClass(int count, Expression **children);
+Expression *ExpressionNewDot(void);
 Expression *ExpressionNewRange(int lo, int hi);
 void ExpressionFree(Expression *expr);
+void ExpressionPrint(Expression *expr, int highestPrecedence, const char *indent);
 
 ExprKind ExpressionKind(Expression *expr);
-Expression *ExpressionChild(Expression *expr, int index);
 int ExpressionCount(Expression *expr);
-const char *ExpressionValue(Expression *expr);
-int ExpressionStart(Expression *expr);
-int ExpressionEnd(Expression *expr);
-void ExpressionPrint(Expression *expr, int highestPrecedence, const char *indent);
-void ExpressionSetTarget(Expression *expr, Expression *target);
+Expression *ExpressionChildAt(Expression *expr, int index);
+
+// Choice
+
+int ExprChoiceCount(Expression *expr);
+Expression *ExprChoiceChildAt(Expression *expr, int index);
+
+// Sequence
+
+int ExprSequenceCount(Expression *expr);
+Expression *ExprSequenceChildAt(Expression *expr, int index);
+
+// And, Not, Opt, Star, Plus
+
+Expression *ExprAndChild(Expression *expr);
+Expression *ExprNotChild(Expression *expr);
+Expression *ExprOptChild(Expression *expr);
+Expression *ExprStarChild(Expression *expr);
+Expression *ExprPlusChild(Expression *expr);
+
+// Ident, Lit
+
+const char *ExprIdentValue(Expression *expr);
+Expression *ExprIdentTarget(Expression *expr);
+void ExprIdentSetTarget(Expression *expr, Expression *target);
+const char *ExprLitValue(Expression *expr);
+
+// Class
+
+int ExprClassCount(Expression *expr);
+Expression *ExprClassChildAt(Expression *expr, int index);
+
+// Range
+
+int ExprRangeStart(Expression *expr);
+int ExprRangeEnd(Expression *expr);

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/aa7176ab/experiments/flat/src/Grammar.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Grammar.c b/experiments/flat/src/Grammar.c
index 74e36b4..1fa2979 100644
--- a/experiments/flat/src/Grammar.c
+++ b/experiments/flat/src/Grammar.c
@@ -85,17 +85,17 @@ static Rule *GrammarLookup(Grammar *gram, const char *name)
 static void GrammarResolveRecursive(Grammar *gram, Expression *expr, const char *ruleName)
 {
     if (ExpressionKind(expr) == IdentExpr) {
-        const char *targetName = ExpressionValue(expr);
+        const char *targetName = ExprIdentValue(expr);
         Rule *targetRule = GrammarLookup(gram,targetName);
         if (targetRule == NULL) {
             fprintf(stderr,"%s: Cannot resolve reference %s\n",ruleName,targetName);
             exit(1);
         }
-        ExpressionSetTarget(expr,targetRule->expr);
+        ExprIdentSetTarget(expr,targetRule->expr);
     }
     else {
         for (int i = 0; i < ExpressionCount(expr); i++)
-            GrammarResolveRecursive(gram,ExpressionChild(expr,i),ruleName);
+            GrammarResolveRecursive(gram,ExpressionChildAt(expr,i),ruleName);
     }
 }
 


Mime
View raw message