Skip to content

Loading…

Remove 'with' statement & lambda call-with-suffix syntax - issue #438 #441

Merged
3 commits merged into from

1 participant

@ghost

No description provided.

@jckarter
Owner

Looks good.

@ghost ghost merged commit fa7b282 into jckarter:master
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 10, 2012
  1. remove with statement

    agemogolk committed
  2. remove call-with-suffix syntax sugar

    agemogolk committed
View
6 compiler/analyzer.cpp
@@ -2535,10 +2535,8 @@ static StatementAnalysis analyzeStatement(StatementPtr stmt, EnvPtr env, Analysi
case BLOCK : {
Block *block = (Block *)stmt.ptr();
- if (!block->desugared)
- block->desugared = desugarBlock(block);
- for (unsigned i = 0; i < block->desugared->statements.size(); ++i) {
- StatementAnalysis sa = analyzeBlockStatement(block->desugared->statements[i], env, ctx);
+ for (unsigned i = 0; i < block->statements.size(); ++i) {
+ StatementAnalysis sa = analyzeBlockStatement(block->statements[i], env, ctx);
if (sa != SA_FALLTHROUGH)
return sa;
}
View
24 compiler/clay.hpp
@@ -512,7 +512,6 @@ struct Finally;
struct OnError;
struct Unreachable;
struct EvalStatement;
-struct WithStatement;
struct StaticAssertStatement;
struct FormalArg;
@@ -666,7 +665,6 @@ typedef Pointer<Finally> FinallyPtr;
typedef Pointer<OnError> OnErrorPtr;
typedef Pointer<Unreachable> UnreachablePtr;
typedef Pointer<EvalStatement> EvalStatementPtr;
-typedef Pointer<WithStatement> WithStatementPtr;
typedef Pointer<StaticAssertStatement> StaticAssertStatementPtr;
typedef Pointer<FormalArg> FormalArgPtr;
@@ -1182,16 +1180,14 @@ struct Indexing : public Expr {
struct Call : public Expr {
ExprPtr expr;
ExprListPtr parenArgs;
- ExprListPtr lambdaArgs;
ExprListPtr _allArgs;
ExprListPtr allArgs();
- Call(ExprPtr expr, ExprListPtr parenArgs, ExprListPtr lambdaArgs)
- : Expr(CALL), expr(expr), parenArgs(parenArgs), lambdaArgs(lambdaArgs),
+ Call(ExprPtr expr, ExprListPtr parenArgs)
+ : Expr(CALL), expr(expr), parenArgs(parenArgs),
_allArgs(NULL) {}
- Call(ExprPtr expr, ExprListPtr parenArgs);
};
struct FieldRef : public Expr {
@@ -1375,14 +1371,10 @@ inline ExprListPtr Call::allArgs() {
if (_allArgs == NULL) {
_allArgs = new ExprList();
_allArgs->add(parenArgs);
- _allArgs->add(lambdaArgs);
}
return _allArgs;
}
-inline Call::Call(ExprPtr expr, ExprListPtr parenArgs)
- : Expr(CALL), expr(expr), parenArgs(parenArgs), lambdaArgs(new ExprList()),
- _allArgs(NULL) {}
//
// Stmt
@@ -1412,7 +1404,6 @@ enum StatementKind {
ONERROR,
UNREACHABLE,
EVAL_STATEMENT,
- WITH,
STATIC_ASSERT_STATEMENT
};
@@ -1423,7 +1414,6 @@ struct Statement : public ANode {
};
struct Block : public Statement {
- BlockPtr desugared;
vector<StatementPtr> statements;
Block()
: Statement(BLOCK) {}
@@ -1704,16 +1694,6 @@ struct EvalStatement : public Statement {
: Statement(EVAL_STATEMENT), args(args), evaled(false) {}
};
-
-struct WithStatement : public Statement {
- vector<IdentifierPtr> lhs;
- ExprPtr rhs;
- Location withLocation;
- WithStatement( vector<IdentifierPtr> i, ExprPtr r, Location const &l)
- : Statement(WITH), lhs(i), rhs(r), withLocation(l) {}
-};
-
-
struct StaticAssertStatement : public Statement {
ExprPtr cond;
ExprListPtr message;
View
8 compiler/clone.cpp
@@ -122,7 +122,7 @@ ExprPtr clone(ExprPtr x)
case CALL : {
Call *y = (Call *)x.ptr();
- out = new Call(clone(y->expr), clone(y->parenArgs), clone(y->lambdaArgs));
+ out = new Call(clone(y->expr), clone(y->parenArgs));
break;
}
@@ -431,12 +431,6 @@ StatementPtr clone(StatementPtr x)
break;
}
- case WITH : {
- WithStatement *with= (WithStatement*)x.ptr();
- out = new WithStatement(with->lhs, with->rhs, with->withLocation);
- break;
- }
-
case STATIC_ASSERT_STATEMENT : {
StaticAssertStatement *staticAssert = (StaticAssertStatement *)x.ptr();
out = new StaticAssertStatement(clone(staticAssert->cond), clone(staticAssert->message));
View
2 compiler/codegen.cpp
@@ -3655,8 +3655,6 @@ static void codegenBlockStatement(BlockPtr block,
llvm::ArrayRef<StatementPtr> evaled = desugarEvalStatement(eval, env);
for (unsigned i = 0; i < evaled.size(); ++i)
codegenBlockStatement(block, i, evaled[i], env, ctx, terminated);
- } else if (stmt->stmtKind == WITH) {
- error("unexpected with statement");
} else {
terminated = codegenStatement(stmt, env, ctx);
}
View
57 compiler/desugar.cpp
@@ -405,61 +405,4 @@ llvm::ArrayRef<TopLevelItemPtr> desugarEvalTopLevel(EvalTopLevelPtr eval, EnvPtr
}
}
-static StatementPtr desugarWithBlock(WithStatementPtr with,
- llvm::ArrayRef<StatementPtr> statements, unsigned int i)
-{
- ++i;
- BlockPtr b = new Block();
- for (; i < statements.size(); ++i) {
- StatementPtr stmt = statements[i];
- if (stmt->stmtKind == WITH) {
- b->statements.push_back(desugarWithBlock((WithStatement *)stmt.ptr(), statements, i));
- break;
- }
- b->statements.push_back(statements[i]);
- }
-
- vector<FormalArgPtr> formalArgs;
- for(size_t i = 0; i < with->lhs.size(); i++) {
- formalArgs.push_back(new FormalArg(with->lhs.at(i), NULL, TEMPNESS_DONTCARE));
- }
-
- ExprPtr la = new Lambda(VALUE_CAPTURE, formalArgs, false, b.ptr());
- la->location = with->withLocation;
-
- if (with->rhs->exprKind != CALL) {
- error("the right hand side of a with statement must be a call.");
- }
-
- CallPtr call = (Call*)with->rhs.ptr();
-
- //prepend the lambda to the arguments for monad
- call->parenArgs->exprs.insert(call->parenArgs->exprs.begin(),la);
-
- //form the return yield expression
-
- ExprListPtr rexprs = new ExprList();
-
- rexprs->add(new Unpack(call.ptr()));
-
- StatementPtr r = new Return(RETURN_VALUE, rexprs);
- r->location = with->location;
-
- return r.ptr();
-}
-
-BlockPtr desugarBlock(BlockPtr block)
-{
- for (unsigned i = 0; i < block->statements.size(); ++i) {
- StatementPtr stmt = block->statements[i];
- if (stmt->stmtKind == WITH) {
- StatementPtr stn = desugarWithBlock((WithStatement *)stmt.ptr(), block->statements, i);
- block->statements[i] = stn;
- block->statements.resize( i + 1);
- break;
- }
- }
- return block;
-}
-
}
View
1 compiler/desugar.hpp
@@ -13,7 +13,6 @@ ExprListPtr desugarVariadicAssignmentRight(VariadicAssignment *x);
StatementPtr desugarForStatement(ForPtr x);
StatementPtr desugarCatchBlocks(llvm::ArrayRef<CatchPtr> catchBlocks);
StatementPtr desugarSwitchStatement(SwitchPtr x);
-BlockPtr desugarBlock(BlockPtr x);
ExprListPtr desugarEvalExpr(EvalExprPtr eval, EnvPtr env);
llvm::ArrayRef<StatementPtr> desugarEvalStatement(EvalStatementPtr eval, EnvPtr env);
View
3 compiler/evaluator.cpp
@@ -2390,9 +2390,6 @@ TerminationPtr evalStatement(StatementPtr stmt,
error("unreachable statement");
return NULL;
}
- case WITH :
- error("unexpected with statement");
- return NULL;
case STATIC_ASSERT_STATEMENT : {
StaticAssertStatement *x = (StaticAssertStatement *)stmt.ptr();
View
7 compiler/lambdas.cpp
@@ -348,10 +348,8 @@ void convertFreeVars(StatementPtr x, EnvPtr env, LambdaContext &ctx)
case BLOCK : {
Block *y = (Block *)x.ptr();
- if (!y->desugared)
- y->desugared = desugarBlock(y);
- for (unsigned i = 0; i < y->desugared->statements.size(); ++i) {
- StatementPtr z = y->desugared->statements[i];
+ for (unsigned i = 0; i < y->statements.size(); ++i) {
+ StatementPtr z = y->statements[i];
if (z->stmtKind == BINDING)
env = convertFreeVarsFromBinding((Binding *)z.ptr(), env, ctx);
else
@@ -643,7 +641,6 @@ void convertFreeVars(ExprPtr &x, EnvPtr env, LambdaContext &ctx)
Call *y = (Call *)x.ptr();
convertFreeVars(y->expr, env, ctx);
convertFreeVars(y->parenArgs, env, ctx);
- convertFreeVars(y->lambdaArgs, env, ctx);
break;
}
View
2 compiler/lexer.cpp
@@ -140,7 +140,7 @@ static void initKeywords() {
"switch", "case", "break", "continue", "for", "in",
"true", "false", "try", "catch", "throw",
"finally", "onerror", "staticassert",
- "eval", "with", "when", "newtype",
+ "eval", "when", "newtype",
"__FILE__", "__LINE__", "__COLUMN__", "__ARG__", NULL};
keywords = new std::set<llvm::StringRef>();
for (const char **p = s; *p; ++p)
View
74 compiler/parser.cpp
@@ -402,7 +402,7 @@ static bool stringLiteralSuffix(ExprPtr &x) {
ExprPtr str;
if (!stringLiteral(str)) return false;
ExprListPtr strArgs = new ExprList(str);
- x = new Call(NULL, strArgs, new ExprList());
+ x = new Call(NULL, strArgs);
x->location = location;
return true;
}
@@ -418,42 +418,13 @@ static bool indexingSuffix(ExprPtr &x) {
return true;
}
-static bool lambda(ExprPtr &x);
-
-static bool optCallLambdaList(ExprListPtr &args) {
- args = new ExprList();
-
- int p = save();
- if (!symbol(":")) {
- restore(p);
- return true;
- }
-
- while (true) {
- ExprPtr lambdaArg;
- Location startLocation = currentLocation();
- if (!lambda(lambdaArg)) return false;
- lambdaArg->startLocation = startLocation;
- lambdaArg->endLocation = currentLocation();
-
- args->add(lambdaArg);
- p = save();
- if (!symbol("::")) {
- restore(p);
- return true;
- }
- }
-}
-
static bool callSuffix(ExprPtr &x) {
Location location = currentLocation();
if (!symbol("(")) return false;
ExprListPtr args;
if (!optExpressionList(args)) return false;
if (!symbol(")")) return false;
- ExprListPtr lambdaArgs;
- if (!optCallLambdaList(lambdaArgs)) return false;
- x = new Call(NULL, args, lambdaArgs);
+ x = new Call(NULL, args);
x->location = location;
return true;
}
@@ -1104,35 +1075,9 @@ static bool localBinding(StatementPtr &x) {
return true;
}
-static bool blockItems(vector<StatementPtr> &stmts, bool);
-static bool withStatement(StatementPtr &x) {
- Location startLocation = currentLocation();
-
- if (!keyword("with")) return false;
- vector<IdentifierPtr> lhs;
-
- int p = save();
- if (!identifierList(lhs) || !opsymbol("=")) {
- lhs.clear();
- restore(p);
- }
- Location location = currentLocation();
-
- ExprPtr rhs = NULL;
- if (!suffixExpr(rhs)) return false;
- if (!symbol(";")) return false;
-
- WithStatementPtr w = new WithStatement(lhs, rhs, location);
-
- x = w.ptr();
- x->location = location;
- return true;
-}
-
static bool blockItem(StatementPtr &x) {
int p = save();
if (labelDef(x)) return true;
- if (restore(p), withStatement(x)) return true;
if (restore(p), localBinding(x)) return true;
if (restore(p), statement(x)) return true;
return false;
@@ -1350,24 +1295,11 @@ static bool switchStatement(StatementPtr &x) {
return true;
}
-static bool exprStatementNeedsSemicolon(ExprPtr expr) {
- if (expr->exprKind == CALL) {
- CallPtr call = (Call*)expr.ptr();
- unsigned lambdaCount = call->lambdaArgs->size();
- if (lambdaCount != 0) {
- LambdaPtr lastLambda = (Lambda*)call->lambdaArgs->exprs.back().ptr();
- assert(lastLambda->exprKind == LAMBDA);
- return lastLambda->body->stmtKind != BLOCK;
- }
- }
- return true;
-}
-
static bool exprStatement(StatementPtr &x) {
Location location = currentLocation();
ExprPtr y;
if (!expression(y)) return false;
- if (exprStatementNeedsSemicolon(y) && !symbol(";")) return false;
+ if (!symbol(";")) return false;
x = new ExprStatement(y);
x->location = location;
return true;
View
7 compiler/printer.cpp
@@ -213,7 +213,7 @@ static void printExpr(llvm::raw_ostream &out, const Expr *x) {
}
case CALL : {
const Call *y = (const Call *)x;
- out << "Call(" << y->expr << ", " << y->parenArgs << ", " << y->lambdaArgs << ")";
+ out << "Call(" << y->expr << ", " << y->parenArgs << ")";
break;
}
case FIELD_REF : {
@@ -323,11 +323,6 @@ static void printExpr(llvm::raw_ostream &out, const Expr *x) {
static void printStatement(llvm::raw_ostream &out, const Statement *x) {
switch (x->stmtKind) {
- case WITH : {
- const WithStatement *y = (const WithStatement *)x;
- out << "WithStatement(" << y->lhs << ", " << y->rhs << ")";
- break;
- }
case BLOCK : {
const Block *y = (const Block *)x;
out << "Block(" << bigVec(y->statements) << ")";
View
29 test/example/monads/main.clay
@@ -1,29 +0,0 @@
-import printer.(println);
-
-variant V(Int, StringLiteralRef);
-
-
-[C] ignore (c:C, ..T)
-{
- c();
-}
-
-
-[C,V when Variant?(V) ]
-dispatch(c:C, v:V)
-{
- return ..c(*v);
-}
-
-test(m : V)
-{
- with ignore(3);
- with s = dispatch(m);
- println(s);
-}
-
-main()
-{
- test(V(5));
- test(V(StringLiteralRef(":)")));
-}
View
2 test/example/monads/out.txt
@@ -1,2 +0,0 @@
-5
-:)
View
12 test/lang/variants/match/main.clay
@@ -2,14 +2,16 @@ import printer.(println);
variant Foo (Int, Double, StringLiteralRef);
foo(x) {
- match(x): (i:Int) => { println("I saw an Int ", i); }
- :: (f:Double) => { println("I saw a Double ", f); }
- :: (s:StringLiteralRef) => { println("I saw a string ", s); }
+ match(x, (i:Int) => { println("I saw an Int ", i); }
+ , (f:Double) => { println("I saw a Double ", f); }
+ , (s:StringLiteralRef) => { println("I saw a string ", s); }
+ );
}
bar(x) {
- match(x): (i:Int) => { println("I saw an Int ", i); }
- :: (x) => { println("I saw a non-Int ", x); }
+ match(x, (i:Int) => { println("I saw an Int ", i); }
+ , (x) => { println("I saw a non-Int ", x); }
+ );
}
main() {
View
18 test/lib-clay/lambdas/callsugar/main.clay
@@ -1,18 +0,0 @@
-import printer.(println);
-
-ifNonzero(x, ifTrue, ifFalse) {
- if (x != 0)
- return ..ifTrue(x);
- else
- return ..ifFalse();
-}
-
-main() {
- ifNonzero(1): x -> {
- println(x, " is not zero");
- } :: () -> {
- println("zero");
- }
-
- println(1, ifNonzero(1): x -> StringLiteralRef(" is not zero") :: () -> StringLiteralRef(" is zero"));
-}
View
2 test/lib-clay/lambdas/callsugar/out.txt
@@ -1,2 +0,0 @@
-1 is not zero
-1 is not zero
View
4 test/lib-clay/threads/locks/mutex/main.clay
@@ -7,9 +7,7 @@ var counter = Synchronized[UInt, Mutex](0u);
counterThread() {
for (x in range(1000))
- synchronized(counter): c -> {
- c +: 1;
- }
+ synchronized(counter, c -> {c +: 1;} );
}
main() {
View
4 test/lib-clay/threads/locks/spinlock/main.clay
@@ -7,9 +7,7 @@ var counter = Synchronized[UInt, Spinlock](0u);
counterThread() {
for (x in range(1000))
- synchronized(counter): c -> {
- c +: 1;
- }
+ synchronized(counter, c -> {c +: 1;} );
}
main() {
View
21 tools/bindgen.clay
@@ -416,12 +416,15 @@ visitToplevel(cursor:CXCursor, parent:CXCursor, data:CXClientData) : Int
else
printTo(g_bindingsOut, "cdecl");
- maybe(attributes.asmLabel): asmLabel -> {
- printTo(g_bindingsOut, ", ", stringRepr(asmLabel));
- } :: () -> {
- if (clayKeyword?(name))
- printTo(g_bindingsOut, ", ", stringRepr(name));
- }
+ maybe(attributes.asmLabel,
+ asmLabel -> {
+ printTo(g_bindingsOut, ", ", stringRepr(asmLabel));
+ },
+ () -> {
+ if (clayKeyword?(name))
+ printTo(g_bindingsOut, ", ", stringRepr(name));
+ }
+ );
printTo(g_bindingsOut, ") ", clayIdentifier(name), "(");
var argCount = clang_getNumArgTypes(type);
@@ -636,9 +639,9 @@ main(argc, argv)
try {
clang_argc, clang_argv, options = parseOptions(argc, argv);
- maybe(options.outputFileName): outputFileName -> {
- g_bindingsOut = File(outputFileName, CREATE);
- }
+ maybe(options.outputFileName,
+ outputFileName -> {g_bindingsOut = File(outputFileName, CREATE);}
+ );
} catch (ex:Usage) {
print(ex);
return 2;
Something went wrong with that request. Please try again.