Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Update local bindings (multiple value, typespec, patterns) fixes issues #35 & #246 #322

Merged
merged 35 commits into from

1 participant

@ghost

Local bindings update

  • multiple value bindings
  • typespecs
  • patterns/predicates in local scope
  • var/ref/forward semantics preserved
  • alias binding not modified

Added simple test

Jeremy Clifford added some commits
Jeremy Clifford Add binding pred/patterns and use argument list for optional typed id…
…entifiers.

Update binding cloning
Update binding parsing.
090ee61
Jeremy Clifford Remove predicate from localbinding code. 27dddf5
Jeremy Clifford Local binding typespec/patternvars parsed correctly. 45f2d6c
Jeremy Clifford Add VAR variadic assignment, typespec, patternvars.
Implicit unpack broken!
27f8d00
Jeremy Clifford Change multi into count. 8299981
Jeremy Clifford Debugging ref bindings ca40051
Jeremy Clifford Remove ref debug (not the issue - appears to be exceptions broken) d712078
Jeremy Clifford Remove more debugging stuff from codegen f8cab41
Jeremy Clifford Update evalBinding 14afc23
Jeremy Clifford Merge branch 'mathlib' into bindings c997dda
Jeremy Clifford Merge branch 'master' into bindings e87c5de
Jeremy Clifford Debugging 539b2c0
Jeremy Clifford debugging ea0e2b7
Jeremy Clifford Debugging 04d7da2
Jeremy Clifford debugging stuff 6a62bd3
Jeremy Clifford Merge remote-tracking branch 'upstream/master' into bindings
Conflicts:
	lib-clay/math/libm/libm.windows.clay
1ff5f8e
Jeremy Clifford Bindings updates c27bbb5
Jeremy Clifford Merge branch 'master' into bindings 4de1bc2
Jeremy Clifford replace swtich in codegenbinding c5d6a40
Jeremy Clifford Bindings 70bb75f
Jeremy Clifford Binding typespec checking implemented for 'var' only
Binding patterns/predicates working
31305c3
Jeremy Clifford Typespec & binding patterns - all tests pass 1fc96ab
Jeremy Clifford Rework binding analysis
Add binding match errors
b348c60
Jeremy Clifford Add variadic ref/forward bindings 6fd1727
Jeremy Clifford Remove whitespace 59d3ad3
Jeremy Clifford Merge remote-tracking branch 'upstream/master' into bindings
Conflicts:
	compiler/src/clay.hpp
	compiler/src/error.cpp
9f2353d
Jeremy Clifford Remove some debug msgs 7f4a2bf
Jeremy Clifford Update evaluator bindings e40f03a
Jeremy Clifford Remove blah from test 2b7a03b
Jeremy Clifford Fix binding eval and clone ef8e522
Jeremy Clifford Fix codegen missing inline stuff 42a8b19
Jeremy Clifford Tidy up some whitespace 7059108
Jeremy Clifford Add variadic binding test 1621e73
Jeremy Clifford Update variadic binding test output fed1697
test/uncategorized/16/main.clay
@@ -7,7 +7,7 @@ enum Kind (
);
main() {
- var a, b, = UGLY, BAD, ;
+ var a, b = UGLY, BAD, ;
@jckarter Owner

Why was the comma removed? Does the trailing comma not work anymore?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
test/bindings/variadic/main.clay
((6 lines not shown))
+ var ..a = ..y;
+ nthValue(#0,..a) = 189;
+ return ..a;
+}
+
+main() {
+
+ var [..I] a:Int32, ..c:I = 4, 5, 4, 5, 6, 7;
+ var ..b = ..foo(..c);
+ ref ..j = ..b;
+ println(..j);
+ println(..c);
+
+ var zero, one, ..rest = integers(#10);
+ println(..weaveValues(", ", ..rest));
+}
@jckarter Owner

Maybe also add a println(..weaveValues(", ", ..I)); to show that I binds to the right values Int, Int, ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Jeremy Clifford Update bindings test to show inferred types.
Allow trailing comma when parsing bindings.
Replace comma in uncat/16 test.
72a83a0
@ghost

I've allowed a trailing comma in the binding list. Eventually i'll add some more tests for the bindings.

@jckarter jckarter merged commit d13a1c8 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 25, 2012
  1. Add binding pred/patterns and use argument list for optional typed id…

    Jeremy Clifford authored
    …entifiers.
    
    Update binding cloning
    Update binding parsing.
  2. Remove predicate from localbinding code.

    Jeremy Clifford authored
Commits on Apr 26, 2012
  1. Local binding typespec/patternvars parsed correctly.

    Jeremy Clifford authored
Commits on May 3, 2012
  1. Add VAR variadic assignment, typespec, patternvars.

    Jeremy Clifford authored
    Implicit unpack broken!
  2. Change multi into count.

    Jeremy Clifford authored
  3. Debugging ref bindings

    Jeremy Clifford authored
  4. Remove more debugging stuff from codegen

    Jeremy Clifford authored
  5. Update evalBinding

    Jeremy Clifford authored
  6. Merge branch 'mathlib' into bindings

    Jeremy Clifford authored
  7. Merge branch 'master' into bindings

    Jeremy Clifford authored
Commits on May 5, 2012
  1. Debugging

    Jeremy Clifford authored
  2. debugging

    Jeremy Clifford authored
Commits on May 9, 2012
  1. Debugging

    Jeremy Clifford authored
Commits on May 20, 2012
  1. debugging stuff

    Jeremy Clifford authored
Commits on May 28, 2012
  1. Merge remote-tracking branch 'upstream/master' into bindings

    Jeremy Clifford authored
    Conflicts:
    	lib-clay/math/libm/libm.windows.clay
Commits on Jul 15, 2012
  1. Bindings updates

    Jeremy Clifford authored
Commits on Aug 2, 2012
  1. Merge branch 'master' into bindings

    Jeremy Clifford authored
Commits on Aug 3, 2012
  1. replace swtich in codegenbinding

    Jeremy Clifford authored
Commits on Aug 5, 2012
  1. Bindings

    Jeremy Clifford authored
Commits on Aug 8, 2012
  1. Binding typespec checking implemented for 'var' only

    Jeremy Clifford authored
    Binding patterns/predicates working
Commits on Aug 9, 2012
  1. Typespec & binding patterns - all tests pass

    Jeremy Clifford authored
  2. Rework binding analysis

    Jeremy Clifford authored
    Add binding match errors
  3. Add variadic ref/forward bindings

    Jeremy Clifford authored
  4. Remove whitespace

    Jeremy Clifford authored
  5. Merge remote-tracking branch 'upstream/master' into bindings

    Jeremy Clifford authored
    Conflicts:
    	compiler/src/clay.hpp
    	compiler/src/error.cpp
Commits on Aug 10, 2012
  1. Remove some debug msgs

    Jeremy Clifford authored
  2. Update evaluator bindings

    Jeremy Clifford authored
  3. Remove blah from test

    Jeremy Clifford authored
  4. Fix binding eval and clone

    Jeremy Clifford authored
  5. Fix codegen missing inline stuff

    Jeremy Clifford authored
  6. Tidy up some whitespace

    Jeremy Clifford authored
  7. Add variadic binding test

    Jeremy Clifford authored
  8. Update variadic binding test output

    Jeremy Clifford authored
Commits on Aug 11, 2012
  1. Update bindings test to show inferred types.

    Jeremy Clifford authored
    Allow trailing comma when parsing bindings.
    Replace comma in uncat/16 test.
This page is out of date. Refresh to see the latest.
View
103 compiler/src/analyzer.cpp
@@ -620,7 +620,6 @@ MultiPValuePtr analyzeExpr(ExprPtr expr, EnvPtr env)
static MultiPValuePtr analyzeExpr2(ExprPtr expr, EnvPtr env)
{
LocationContext loc(expr->location);
-
switch (expr->exprKind) {
case BOOL_LITERAL : {
@@ -855,7 +854,7 @@ MultiPValuePtr analyzeStaticObject(ObjectPtr x)
TypePtr t = staticType(x);
return new MultiPValue(PVData(t, true));
}
- evaluateToplevelPredicate(y->patternVars, y->predicate, y->env);
+ evaluatePredicate(y->patternVars, y->predicate, y->env);
return analyzeExpr(y->expr, y->env);
}
@@ -1019,7 +1018,7 @@ MultiPValuePtr analyzeGVarInstance(GVarInstancePtr x)
}
}
x->analyzing = true;
- evaluateToplevelPredicate(x->gvar->patternVars, x->gvar->predicate, x->env);
+ evaluatePredicate(x->gvar->patternVars, x->gvar->predicate, x->env);
PVData pv = analyzeOne(x->expr, x->env);
x->analyzing = false;
if (!pv.ok())
@@ -1493,7 +1492,7 @@ MultiPValuePtr analyzeAliasIndexing(GlobalAliasPtr x,
varParams->add(params->values[i]);
addLocal(bodyEnv, x->varParam, varParams.ptr());
}
- evaluateToplevelPredicate(x->patternVars, x->predicate, bodyEnv);
+ evaluatePredicate(x->patternVars, x->predicate, bodyEnv);
AnalysisCachingDisabler disabler;
return analyzeExpr(x->expr, bodyEnv);
}
@@ -2103,7 +2102,7 @@ static EnvPtr analyzeStatementExpressionStatements(vector<StatementPtr> const &s
StatementAnalysis analyzeStatement(StatementPtr stmt, EnvPtr env, AnalysisContext* ctx)
{
LocationContext loc(stmt->location);
-
+
switch (stmt->stmtKind) {
case BLOCK : {
@@ -2296,31 +2295,99 @@ void initializeStaticForClones(StaticForPtr x, unsigned count)
EnvPtr analyzeBinding(BindingPtr x, EnvPtr env)
{
LocationContext loc(x->location);
-
+
switch (x->bindingKind) {
-
case VAR :
case REF :
case FORWARD : {
- MultiPValuePtr right = analyzeMulti(x->values, env, x->names.size());
- if (!right)
- return NULL;
- if (right->size() != x->names.size())
- arityMismatchError(x->names.size(), right->size());
- EnvPtr env2 = new Env(env);
- for (unsigned i = 0; i < right->size(); ++i) {
- PVData const &pv = right->values[i];
- addLocal(env2, x->names[i], new PValue(pv.type, false));
+
+ vector<TypePtr> key;
+ vector<PatternCellPtr> cells;
+ vector<MultiPatternCellPtr> multiCells;
+
+ MultiPValuePtr mpv = analyzeMulti(x->values, env, x->args.size());
+ if (!mpv)
+ return NULL;
+
+ for (unsigned i = 0; i < mpv->size(); ++i) {
+ PVData const &pv = mpv->values[i];
+ key.push_back(pv.type);
+ }
+
+ if (x->varg.ptr()) {
+ if (key.size() < x->args.size())
+ arityMismatchError(x->args.size(), key.size());
+ }
+ else {
+ if (x->args.size() != key.size())
+ arityMismatchError(x->args.size(), key.size());
+ }
+
+ const vector<PatternVar> &pvars = x->patternVars;
+ EnvPtr patternEnv = new Env(env);
+ initializePatternEnv(patternEnv, pvars, cells, multiCells);
+
+ const vector<FormalArgPtr> &formalArgs = x->args;
+ for (unsigned i = 0; i < formalArgs.size(); ++i) {
+ FormalArgPtr y = formalArgs[i];
+ if (y->type.ptr())
+ if (!unifyPatternObj(evaluateOnePattern(y->type, patternEnv), key[i].ptr()))
+ matchBindingError(new MatchBindingError(i, key[i], y));
+ }
+
+ if (x->varg.ptr() && x->varg->type.ptr()) {
+ ExprPtr unpack = new Unpack(x->varg->type.ptr());
+ unpack->location = x->varg->type->location;
+ MultiStaticPtr types = new MultiStatic();
+ for (unsigned i = formalArgs.size(); i < key.size(); ++i)
+ types->add(key[i].ptr());
+ if (!unifyMulti(evaluateMultiPattern(new ExprList(unpack), patternEnv), types))
+ matchBindingError(new MatchMultiBindingError(formalArgs.size(), types, x->varg));
+ }
+
+ EnvPtr staticEnv = new Env(env);
+ for (unsigned i = 0; i < pvars.size(); ++i) {
+ if (pvars[i].isMulti) {
+ MultiStaticPtr ms = derefDeep(multiCells[i].ptr());
+ if (!ms)
+ error(pvars[i].name, "unbound pattern variable");
+ addLocal(staticEnv, pvars[i].name, ms.ptr());
+ x->patternTypes.push_back(ms.ptr());
+ }
+ else {
+ ObjectPtr v = derefDeep(cells[i].ptr());
+ if (!v)
+ error(pvars[i].name, "unbound pattern variable");
+ addLocal(staticEnv, pvars[i].name, v.ptr());
+ x->patternTypes.push_back(v.ptr());
+ }
}
+
+ evaluatePredicate(x->patternVars, x->predicate, staticEnv);
+
+ EnvPtr env2 = new Env(staticEnv);
+ for (unsigned i = 0; i < formalArgs.size(); ++i) {
+ FormalArgPtr y = formalArgs[i];
+ addLocal(env2, y->name, new PValue(key[i], false));
+ }
+ if (x->varg.ptr()) {
+ MultiPValuePtr varArgs = new MultiPValue();
+ for (unsigned i = formalArgs.size(); i < key.size(); ++i) {
+ PVData parg(key[i], false);
+ varArgs->values.push_back(parg);
+ }
+ addLocal(env2, x->varg->name, varArgs.ptr());
+ }
+
return env2;
}
case ALIAS : {
- ensureArity(x->names, 1);
+ ensureArity(x->args, 1);
ensureArity(x->values->exprs, 1);
EnvPtr env2 = new Env(env);
ExprPtr y = foreignExpr(env, x->values->exprs[0]);
- addLocal(env2, x->names[0], y.ptr());
+ addLocal(env2, x->args[0]->name, y.ptr());
return env2;
}
View
63 compiler/src/clay.hpp
@@ -384,19 +384,19 @@ enum ObjectKind {
INSTANCE,
OVERLOAD,
PROCEDURE,
- ENUMERATION,
+ ENUMERATION, // -- 20
ENUM_MEMBER,
GLOBAL_VARIABLE,
EXTERNAL_PROCEDURE,
EXTERNAL_ARG,
- EXTERNAL_VARIABLE,
+ EXTERNAL_VARIABLE, // -- 25
EVAL_TOPLEVEL,
GLOBAL_ALIAS,
IMPORT,
MODULE_DECLARATION,
- MODULE_HOLDER,
+ MODULE_HOLDER, // -- 30
MODULE,
ENV,
@@ -405,7 +405,7 @@ enum ObjectKind {
TYPE,
- PATTERN,
+ PATTERN, // -- 35
MULTI_PATTERN,
VALUE_HOLDER,
@@ -1437,15 +1437,34 @@ enum BindingKind {
FORWARD
};
+struct PatternVar;
+
struct Binding : public Statement {
int bindingKind;
- vector<IdentifierPtr> names;
+ vector<PatternVar> patternVars;
+ vector<ObjectPtr> patternTypes;
+ ExprPtr predicate;
+ vector<FormalArgPtr> args;
+ FormalArgPtr varg;
ExprListPtr values;
+
+ Binding(int bindingKind,
+ const vector<FormalArgPtr> &args,
+ ExprListPtr values)
+ : Statement(BINDING), bindingKind(bindingKind),
+ args(args), values(values) {}
Binding(int bindingKind,
- const vector<IdentifierPtr> &names,
- ExprListPtr values)
+ const vector<PatternVar> &patternVars,
+ const vector<ObjectPtr> &patternTypes,
+ ExprPtr predicate,
+ const vector<FormalArgPtr> &args,
+ FormalArgPtr varg,
+ ExprListPtr values)
: Statement(BINDING), bindingKind(bindingKind),
- names(names), values(values) {}
+ patternVars(patternVars),
+ predicate(predicate),
+ args(args), varg(varg),
+ values(values) {}
};
struct Assignment : public Statement {
@@ -1914,7 +1933,6 @@ struct Overload : public TopLevelItem {
bool nameIsPattern;
vector<PatternCellPtr> cells;
vector<MultiPatternCellPtr> multiCells;
- EnvPtr patternEnv;
PatternPtr callablePattern;
vector<PatternPtr> argPatterns;
MultiPatternPtr varArgPattern;
@@ -3195,7 +3213,9 @@ enum MatchCode {
MATCH_ARITY_ERROR,
MATCH_ARGUMENT_ERROR,
MATCH_MULTI_ARGUMENT_ERROR,
- MATCH_PREDICATE_ERROR
+ MATCH_PREDICATE_ERROR,
+ MATCH_BINDING_ERROR,
+ MATCH_MULTI_BINDING_ERROR
};
struct MatchResult : public Object {
@@ -3268,6 +3288,26 @@ struct MatchPredicateError : public MatchResult {
: MatchResult(MATCH_PREDICATE_ERROR), predicateExpr(predicateExpr) {}
};
+struct MatchBindingError : public MatchResult {
+ unsigned argIndex;
+ TypePtr type;
+ FormalArgPtr arg;
+ MatchBindingError(unsigned argIndex, TypePtr type, FormalArgPtr arg)
+ : MatchResult(MATCH_BINDING_ERROR), argIndex(argIndex),
+ type(type), arg(arg) {}
+};
+
+struct MatchMultiBindingError : public MatchResult {
+ unsigned argIndex;
+ MultiStaticPtr types;
+ FormalArgPtr varArg;
+ MatchMultiBindingError(unsigned argIndex, MultiStaticPtr types, FormalArgPtr varArg)
+ : MatchResult(MATCH_MULTI_BINDING_ERROR),
+ argIndex(argIndex), types(types), varArg(varArg) {}
+};
+
+void initializePatternEnv(EnvPtr patternEnv, const vector<PatternVar> &pvars, vector<PatternCellPtr> &cells, vector<MultiPatternCellPtr> &multiCells);
+
MatchResultPtr matchInvoke(OverloadPtr overload,
ObjectPtr callable,
const vector<TypePtr> &argsKey);
@@ -3374,6 +3414,7 @@ InvokeEntry* lookupInvokeEntry(ObjectPtr callable,
const vector<ValueTempness> &argsTempness,
MatchFailureError &failures);
+void matchBindingError(MatchResultPtr result);
void matchFailureLog(MatchFailureError const &err);
void matchFailureError(MatchFailureError const &err);
@@ -3590,7 +3631,7 @@ TypePtr evaluateType(ExprPtr expr, EnvPtr env);
void evaluateMultiType(ExprListPtr exprs, EnvPtr env, vector<TypePtr> &out);
IdentifierPtr evaluateIdentifier(ExprPtr expr, EnvPtr env);
bool evaluateBool(ExprPtr expr, EnvPtr env);
-void evaluateToplevelPredicate(const vector<PatternVar> &patternVars,
+void evaluatePredicate(const vector<PatternVar> &patternVars,
ExprPtr expr, EnvPtr env);
ValueHolderPtr intToValueHolder(int x);
View
16 compiler/src/clone.cpp
@@ -23,6 +23,11 @@ void clone(const vector<PatternVar> &x, vector<PatternVar> &out)
out = x;
}
+void clone(const vector<ObjectPtr> &x, vector<ObjectPtr> &out)
+{
+ out = x;
+}
+
void clone(const vector<IdentifierPtr> &x, vector<IdentifierPtr> &out)
{
for (unsigned i = 0; i < x.size(); ++i)
@@ -278,9 +283,14 @@ StatementPtr clone(StatementPtr x)
case BINDING : {
Binding *y = (Binding *)x.ptr();
- vector<IdentifierPtr> names;
- clone(y->names, names);
- out = new Binding(y->bindingKind, names, clone(y->values));
+ vector<FormalArgPtr> args;
+ vector<PatternVar> patternVars;
+ vector<ObjectPtr> patternTypes;
+ clone(y->args, args);
+ clone(y->patternVars, patternVars);
+ clone(y->patternTypes, patternTypes);
+ out = new Binding(y->bindingKind, patternVars, patternTypes, cloneOpt(y->predicate),
+ args, cloneOpt(y->varg), clone(y->values));
break;
}
View
123 compiler/src/codegen.cpp
@@ -1081,7 +1081,7 @@ void codegenExpr(ExprPtr expr,
if (!x->desugared)
x->desugared = desugarVariadicOp(x);
codegenExpr(x->desugared, env, ctx, out);
- break;
+ break;
}
case EVAL_EXPR : {
@@ -1548,7 +1548,7 @@ void codegenExternalProcedure(ExternalProcedurePtr x, bool codegenBody)
else {
llvmFuncName.append(x->name->str.begin(), x->name->str.end());
}
-
+
llvm::DIFile file(NULL);
int line = 0, column = 0;
if (x->llvmFunc == NULL) {
@@ -2463,8 +2463,9 @@ void codegenCallCode(InvokeEntry* entry,
vector<llvm::Value *> llArgs;
for (unsigned i = 0; i < args->size(); ++i) {
CValuePtr cv = args->values[i];
- if (cv->type != entry->argsKey[i])
+ if (cv->type != entry->argsKey[i]){
argumentTypeError(i, entry->argsKey[i], cv->type);
+ }
llArgs.push_back(cv->llValue);
}
assert(out->size() == entry->returnTypes.size());
@@ -2850,7 +2851,7 @@ void codegenCodeBody(InvokeEntry* entry)
switch(entry->isInline){
case INLINE :
if(inlineEnabled())
- llFunc->addFnAttr(llvm::Attribute::InlineHint);
+ llFunc->addFnAttr(llvm::Attribute::InlineHint);
break;
case NEVER_INLINE :
llFunc->addFnAttr(llvm::Attribute::NoInline);
@@ -2858,7 +2859,7 @@ void codegenCodeBody(InvokeEntry* entry)
default:
break;
}
-
+
for (unsigned i = 1; i <= llArgTypes.size(); ++i) {
llFunc->addAttribute(i, llvm::Attribute::NoAlias);
}
@@ -3540,7 +3541,7 @@ bool codegenStatement(StatementPtr stmt,
CodegenContext* ctx)
{
DebugLocationContext loc(stmt->location, ctx);
-
+
switch (stmt->stmtKind) {
case BLOCK : {
@@ -4062,19 +4063,16 @@ void codegenCollectLabels(const vector<StatementPtr> &statements,
EnvPtr codegenBinding(BindingPtr x, EnvPtr env, CodegenContext* ctx)
{
+
DebugLocationContext loc(x->location, ctx);
int line, column;
llvm::DIFile file = getDebugLineCol(x->location, line, column);
-
- switch (x->bindingKind) {
-
+ switch(x->bindingKind){
case VAR : {
- MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, x->names.size());
- if (mpv->size() != x->names.size())
- arityMismatchError(x->names.size(), mpv->size());
+ MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, x->args.size());
MultiCValuePtr mcv = new MultiCValue();
- for (unsigned i = 0; i < x->names.size(); ++i) {
+ for (unsigned i = 0; i < mpv->values.size(); ++i) {
CValuePtr cv = codegenAllocNewValue(mpv->values[i].type, ctx);
mcv->add(cv);
if (llvmDIBuilder != NULL) {
@@ -4082,7 +4080,7 @@ EnvPtr codegenBinding(BindingPtr x, EnvPtr env, CodegenContext* ctx)
llvm::DIVariable debugVar = llvmDIBuilder->createLocalVariable(
llvm::dwarf::DW_TAG_auto_variable, // tag
debugBlock, // scope
- x->names[i]->str, // name
+ x->args[i]->name->str, // name
file, // file
line, // line
llvmTypeDebugInfo(cv->type), // type
@@ -4099,30 +4097,38 @@ EnvPtr codegenBinding(BindingPtr x, EnvPtr env, CodegenContext* ctx)
}
int tempMarker = markTemps(ctx);
int marker = cgMarkStack(ctx);
- codegenMultiInto(x->values, env, ctx, mcv, x->names.size());
+ codegenMultiInto(x->values, env, ctx, mcv, x->args.size());
cgDestroyAndPopStack(marker, ctx, false);
clearTemps(tempMarker, ctx);
EnvPtr env2 = new Env(env);
- for (unsigned i = 0; i < x->names.size(); ++i) {
+ for (unsigned i = 0; i < x->patternVars.size(); ++i)
+ addLocal(env2, x->patternVars[i].name, x->patternTypes[i]);
+ for (unsigned i = 0; i < x->args.size(); ++i) {
CValuePtr cv = mcv->values[i];
cgPushStackValue(cv, ctx);
- addLocal(env2, x->names[i], cv.ptr());
-
+ addLocal(env2, x->args[i]->name, cv.ptr());
llvm::SmallString<128> buf;
llvm::raw_svector_ostream ostr(buf);
- ostr << x->names[i]->str << ":" << cv->type;
+ ostr << x->args[i]->name->str << ":" << cv->type;
cv->llValue->setName(ostr.str());
-
+ }
+ if (x->varg.ptr()) {
+ unsigned nFixed = x->args.size();
+ MultiCValuePtr varArgs = new MultiCValue();
+ for (unsigned i = nFixed; i < mcv->values.size(); ++i) {
+ CValuePtr cv = mcv->values[i];
+ cgPushStackValue(cv, ctx);
+ varArgs->add(cv);
+ }
+ addLocal(env2, x->varg->name, varArgs.ptr());
}
return env2;
}
case REF : {
- MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, x->names.size());
- if (mpv->size() != x->names.size())
- arityMismatchError(x->names.size(), mpv->size());
+ MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, x->args.size());
MultiCValuePtr mcv = new MultiCValue();
- for (unsigned i = 0; i < x->names.size(); ++i) {
+ for (unsigned i = 0; i < mpv->values.size(); ++i) {
PVData const &pv = mpv->values[i];
if (pv.isTemp)
argumentError(i, "ref can only bind to an lvalue");
@@ -4134,7 +4140,7 @@ EnvPtr codegenBinding(BindingPtr x, EnvPtr env, CodegenContext* ctx)
llvm::DIVariable debugVar = llvmDIBuilder->createLocalVariable(
llvm::dwarf::DW_TAG_auto_variable, // tag
debugBlock, // scope
- x->names[i]->str, // name
+ x->args[i]->name->str, // name
file, // file
line, // line
llvmDIBuilder->createReferenceType(
@@ -4152,28 +4158,38 @@ EnvPtr codegenBinding(BindingPtr x, EnvPtr env, CodegenContext* ctx)
}
int tempMarker = markTemps(ctx);
int marker = cgMarkStack(ctx);
- codegenMulti(x->values, env, ctx, mcv, x->names.size());
+ codegenMulti(x->values, env, ctx, mcv, x->args.size());
cgDestroyAndPopStack(marker, ctx, false);
clearTemps(tempMarker, ctx);
EnvPtr env2 = new Env(env);
- for (unsigned i = 0; i < x->names.size(); ++i) {
+ for (unsigned i = 0; i < x->patternVars.size(); ++i)
+ addLocal(env2, x->patternVars[i].name, x->patternTypes[i]);
+ for (unsigned i = 0; i < x->args.size(); ++i) {
CValuePtr cv = derefValue(mcv->values[i], ctx);
- addLocal(env2, x->names[i], cv.ptr());
-
+ addLocal(env2, x->args[i]->name, cv.ptr());
llvm::SmallString<128> buf;
llvm::raw_svector_ostream ostr(buf);
- ostr << x->names[i]->str << ":" << cv->type;
+ ostr << x->args[i]->name->str << ":" << cv->type;
cv->llValue->setName(ostr.str());
+
+ }
+ if (x->varg.ptr()) {
+ unsigned nFixed = x->args.size();
+ MultiCValuePtr varArgs = new MultiCValue();
+ for (unsigned i = nFixed; i < mcv->values.size(); ++i) {
+ CValuePtr cv = derefValue(mcv->values[i], ctx);
+ varArgs->add(cv);
+ }
+ addLocal(env2, x->varg->name, varArgs.ptr());
}
return env2;
}
case FORWARD : {
- MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, x->names.size());
- if (mpv->size() != x->names.size())
- arityMismatchError(x->names.size(), mpv->size());
+
+ MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, x->args.size());
MultiCValuePtr mcv = new MultiCValue();
- for (unsigned i = 0; i < x->names.size(); ++i) {
+ for (unsigned i = 0; i < mpv->values.size(); ++i) {
PVData const &pv = mpv->values[i];
CValuePtr cv;
if (pv.isTemp) {
@@ -4190,7 +4206,7 @@ EnvPtr codegenBinding(BindingPtr x, EnvPtr env, CodegenContext* ctx)
llvm::DIVariable debugVar = llvmDIBuilder->createLocalVariable(
llvm::dwarf::DW_TAG_auto_variable, // tag
debugBlock, // scope
- x->names[i]->str, // name
+ x->args[i]->name->str, // name
file, // file
line, // line
pv.isTemp
@@ -4210,11 +4226,13 @@ EnvPtr codegenBinding(BindingPtr x, EnvPtr env, CodegenContext* ctx)
}
int tempMarker = markTemps(ctx);
int marker = cgMarkStack(ctx);
- codegenMulti(x->values, env, ctx, mcv, x->names.size());
+ codegenMulti(x->values, env, ctx, mcv, x->args.size());
cgDestroyAndPopStack(marker, ctx, false);
clearTemps(tempMarker, ctx);
EnvPtr env2 = new Env(env);
- for (unsigned i = 0; i < x->names.size(); ++i) {
+ for (unsigned i = 0; i < x->patternVars.size(); ++i)
+ addLocal(env2, x->patternVars[i].name, x->patternTypes[i]);
+ for (unsigned i = 0; i < x->args.size(); ++i) {
CValuePtr rcv, cv;
rcv = mcv->values[i];
if (mpv->values[i].isTemp) {
@@ -4224,23 +4242,38 @@ EnvPtr codegenBinding(BindingPtr x, EnvPtr env, CodegenContext* ctx)
else {
cv = derefValue(rcv, ctx);
}
- addLocal(env2, x->names[i], cv.ptr());
-
+ addLocal(env2, x->args[i]->name, cv.ptr());
llvm::SmallString<128> buf;
llvm::raw_svector_ostream ostr(buf);
- ostr << x->names[i]->str << ":" << cv->type;
+ ostr << x->args[i]->name->str << ":" << cv->type;
cv->llValue->setName(ostr.str());
-
+ }
+ if (x->varg.ptr()) {
+ unsigned nFixed = x->args.size();
+ MultiCValuePtr varArgs = new MultiCValue();
+ for (unsigned i = nFixed; i < mcv->values.size(); ++i) {
+ CValuePtr rcv, cv;
+ rcv = mcv->values[i];
+ if (mpv->values[i].isTemp) {
+ cv = rcv;
+ cgPushStackValue(cv, ctx);
+ }
+ else {
+ cv = derefValue(rcv, ctx);
+ }
+ varArgs->add(cv);
+ }
+ addLocal(env2, x->varg->name, varArgs.ptr());
}
return env2;
}
case ALIAS : {
- ensureArity(x->names, 1);
+ ensureArity(x->args, 1);
ensureArity(x->values->exprs, 1);
EnvPtr env2 = new Env(env);
ExprPtr y = foreignExpr(env, x->values->exprs[0]);
- addLocal(env2, x->names[0], y.ptr());
+ addLocal(env2, x->args[0]->name, y.ptr());
return env2;
}
@@ -6727,7 +6760,7 @@ void codegenMain(ModulePtr module)
args->add(new NameRef(argc));
args->add(new NameRef(argv));
CallPtr mainCall = new Call(operator_expr_callMain(), args);
-
+
ReturnPtr ret = new Return(RETURN_VALUE, new ExprList(mainCall.ptr()));
mainBody->statements.push_back(ret.ptr());
@@ -6783,10 +6816,10 @@ void codegenEntryPoints(ModulePtr module, bool importedExternals)
codegenTopLevelLLVM(module);
initializeCtorsDtors();
generateLLVMCtorsAndDtors();
-
codegenModuleEntryPoints(module, importedExternals);
ObjectPtr mainProc = lookupPublic(module, Identifier::get("main"));
+
if (mainProc != NULL)
codegenMain(module);
View
21 compiler/src/desugar.cpp
@@ -64,9 +64,16 @@ ExprPtr desugarVariadicOp(VariadicOpPtr x) {
return call.ptr();
}
-static vector<IdentifierPtr> identV(IdentifierPtr x) {
- vector<IdentifierPtr> v;
- v.push_back(x);
+static vector<FormalArgPtr> identV(IdentifierPtr x) {
+ vector<FormalArgPtr> v;
+ v.push_back(new FormalArg(x, NULL));
+ return v;
+}
+
+static vector<FormalArgPtr> identVtoFormalV(vector<IdentifierPtr> x) {
+ vector<FormalArgPtr> v;
+ for(int i=0; i<x.size(); ++i)
+ v.push_back(new FormalArg(x[i], NULL));
return v;
}
@@ -128,7 +135,7 @@ StatementPtr desugarForStatement(ForPtr x) {
BlockPtr whileBody = new Block();
whileBody->location = x->body->location;
vector<StatementPtr> &ws = whileBody->statements;
- ws.push_back(new Binding(FORWARD, x->variables, new ExprList(getValueCall.ptr())));
+ ws.push_back(new Binding(FORWARD, identVtoFormalV(x->variables), new ExprList(getValueCall.ptr())));
ws.push_back(x->body);
StatementPtr whileStmt = new While(valueStatements, hasValueCall.ptr(), whileBody.ptr());
@@ -147,7 +154,7 @@ StatementPtr desugarCatchBlocks(const vector<CatchPtr> &catchBlocks) {
BindingPtr expBinding =
new Binding(VAR,
- vector<IdentifierPtr>(1, expVar),
+ identVtoFormalV(vector<IdentifierPtr>(1, expVar)),
new ExprList(activeException.ptr()));
expBinding->location = firstCatchLocation;
@@ -172,7 +179,7 @@ StatementPtr desugarCatchBlocks(const vector<CatchPtr> &catchBlocks) {
getter->location = x->exceptionVar->location;
BindingPtr binding =
new Binding(VAR,
- vector<IdentifierPtr>(1, x->exceptionVar),
+ identVtoFormalV(vector<IdentifierPtr>(1, x->exceptionVar)),
new ExprList(getter.ptr()));
binding->location = x->exceptionVar->location;
@@ -196,7 +203,7 @@ StatementPtr desugarCatchBlocks(const vector<CatchPtr> &catchBlocks) {
getter->location = x->exceptionVar->location;
BindingPtr binding =
new Binding(VAR,
- vector<IdentifierPtr>(1, x->exceptionVar),
+ identVtoFormalV(vector<IdentifierPtr>(1, x->exceptionVar)),
new ExprList(getter.ptr()));
binding->location = x->exceptionVar->location;
block->statements.push_back(binding.ptr());
View
8 compiler/src/error.cpp
@@ -426,6 +426,14 @@ void argumentInvalidStaticObjectError(unsigned int index, ObjectPtr obj)
argumentError(index, sout.str());
}
+void matchBindingError(MatchResultPtr result)
+{
+ string buf;
+ llvm::raw_string_ostream sout(buf);
+ printMatchError(sout, result);
+ error(sout.str());
+}
+
static void matchFailureMessage(MatchFailureError const &err, string &outBuf)
{
llvm::raw_string_ostream sout(outBuf);
View
86 compiler/src/evaluator.cpp
@@ -322,7 +322,7 @@ bool evaluateBool(ExprPtr expr, EnvPtr env)
return vh->as<bool>();
}
-void evaluateToplevelPredicate(const vector<PatternVar> &patternVars,
+void evaluatePredicate(const vector<PatternVar> &patternVars,
ExprPtr predicate, EnvPtr env)
{
for (unsigned i = 0; i < patternVars.size(); ++i) {
@@ -2374,33 +2374,40 @@ void evalCollectLabels(const vector<StatementPtr> &statements,
EnvPtr evalBinding(BindingPtr x, EnvPtr env)
{
LocationContext loc(x->location);
-
switch (x->bindingKind) {
case VAR : {
- MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, x->names.size());
- if (mpv->size() != x->names.size())
- arityMismatchError(x->names.size(), mpv->size());
+ MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, x->args.size());
MultiEValuePtr mev = new MultiEValue();
- for (unsigned i = 0; i < x->names.size(); ++i) {
+ for (unsigned i = 0; i < x->args.size(); ++i) {
EValuePtr ev = evalAllocValue(mpv->values[i].type);
mev->add(ev);
}
int marker = evalMarkStack();
- evalMultiInto(x->values, env, mev, x->names.size());
+ evalMultiInto(x->values, env, mev, x->args.size());
evalDestroyAndPopStack(marker);
EnvPtr env2 = new Env(env);
- for (unsigned i = 0; i < x->names.size(); ++i)
- addLocal(env2, x->names[i], mev->values[i].ptr());
+ for (unsigned i = 0; i < x->patternVars.size(); ++i)
+ addLocal(env2, x->patternVars[i].name, x->patternTypes[i]);
+ for (unsigned i = 0; i < x->args.size(); ++i) {
+ addLocal(env2, x->args[i]->name, mev->values[i].ptr());
+ }
+ if (x->varg.ptr()) {
+ unsigned nFixed = x->args.size();
+ MultiEValuePtr varArgs = new MultiEValue();
+ for (unsigned i = nFixed; i < mev->values.size(); ++i) {
+ EValuePtr ev = mev->values[i];
+ varArgs->add(ev);
+ }
+ addLocal(env2, x->varg->name, varArgs.ptr());
+ }
return env2;
}
case REF : {
- MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, x->names.size());
- if (mpv->size() != x->names.size())
- arityMismatchError(x->names.size(), mpv->size());
+ MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, x->args.size());
MultiEValuePtr mev = new MultiEValue();
- for (unsigned i = 0; i < x->names.size(); ++i) {
+ for (unsigned i = 0; i < x->args.size(); ++i) {
PVData const &pv = mpv->values[i];
if (pv.isTemp)
argumentError(i, "ref can only bind to an rvalue");
@@ -2408,48 +2415,75 @@ EnvPtr evalBinding(BindingPtr x, EnvPtr env)
mev->add(evPtr);
}
int marker = evalMarkStack();
- evalMulti(x->values, env, mev, x->names.size());
+ evalMulti(x->values, env, mev, x->args.size());
evalDestroyAndPopStack(marker);
EnvPtr env2 = new Env(env);
- for (unsigned i = 0; i < x->names.size(); ++i) {
+ for (unsigned i = 0; i < x->patternVars.size(); ++i)
+ addLocal(env2, x->patternVars[i].name, x->patternTypes[i]);
+ for (unsigned i = 0; i < x->args.size(); ++i) {
EValuePtr evPtr = mev->values[i];
- addLocal(env2, x->names[i], derefValue(evPtr).ptr());
+ addLocal(env2, x->args[i]->name, derefValue(evPtr).ptr());
+ }
+ if (x->varg.ptr()) {
+ unsigned nFixed = x->args.size();
+ MultiEValuePtr varArgs = new MultiEValue();
+ for (unsigned i = nFixed; i < mev->values.size(); ++i) {
+ EValuePtr ev = derefValue(mev->values[i]);
+ varArgs->add(ev);
+ }
+ addLocal(env2, x->varg->name, varArgs.ptr());
}
return env2;
}
case FORWARD : {
- MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, x->names.size());
- if (mpv->size() != x->names.size())
- arityMismatchError(x->names.size(), mpv->size());
+ MultiPValuePtr mpv = safeAnalyzeMulti(x->values, env, x->args.size());
MultiEValuePtr mev = new MultiEValue();
- for (unsigned i = 0; i < x->names.size(); ++i) {
+ for (unsigned i = 0; i < x->args.size(); ++i) {
PVData const &pv = mpv->values[i];
mev->add(evalAllocValueForPValue(pv));
}
int marker = evalMarkStack();
- evalMulti(x->values, env, mev, x->names.size());
+ evalMulti(x->values, env, mev, x->args.size());
evalDestroyAndPopStack(marker);
EnvPtr env2 = new Env(env);
- for (unsigned i = 0; i < x->names.size(); ++i) {
+ for (unsigned i = 0; i < x->patternVars.size(); ++i)
+ addLocal(env2, x->patternVars[i].name, x->patternTypes[i]);
+ for (unsigned i = 0; i < x->args.size(); ++i) {
if (mpv->values[i].isTemp) {
EValuePtr ev = mev->values[i];
- addLocal(env2, x->names[i], ev.ptr());
+ addLocal(env2, x->args[i]->name, ev.ptr());
}
else {
EValuePtr evPtr = mev->values[i];
- addLocal(env2, x->names[i], derefValue(evPtr).ptr());
+ addLocal(env2, x->args[i]->name, derefValue(evPtr).ptr());
+ }
+ }
+ if (x->varg.ptr()) {
+ unsigned nFixed = x->args.size();
+ MultiEValuePtr varArgs = new MultiEValue();
+ for (unsigned i = nFixed; i < mev->values.size(); ++i) {
+ EValuePtr rev, ev;
+ rev = mev->values[i];
+ if (mpv->values[i].isTemp) {
+ ev = rev;
+ }
+ else {
+ ev = derefValue(rev);
+ }
+ varArgs->add(ev);
}
+ addLocal(env2, x->varg->name, varArgs.ptr());
}
return env2;
}
case ALIAS : {
- ensureArity(x->names, 1);
+ ensureArity(x->args, 1);
ensureArity(x->values->exprs, 1);
EnvPtr env2 = new Env(env);
ExprPtr y = foreignExpr(env, x->values->exprs[0]);
- addLocal(env2, x->names[0], y.ptr());
+ addLocal(env2, x->args[0]->name, y.ptr());
return env2;
}
View
4 compiler/src/lambdas.cpp
@@ -269,8 +269,8 @@ static EnvPtr convertFreeVarsFromBinding(BindingPtr binding, EnvPtr env, LambdaC
{
convertFreeVars(binding->values, env, ctx);
EnvPtr env2 = new Env(env);
- for (unsigned j = 0; j < binding->names.size(); ++j)
- addLocal(env2, binding->names[j], binding->names[j].ptr());
+ for (unsigned j = 0; j < binding->args.size(); ++j)
+ addLocal(env2, binding->args[j]->name, binding->args[j]->name.ptr());
return env2;
}
View
61 compiler/src/matchinvoke.cpp
@@ -2,6 +2,29 @@
namespace clay {
+
+void initializePatternEnv(EnvPtr patternEnv, const vector<PatternVar> &pvars,
+ vector<PatternCellPtr> &cells, vector<MultiPatternCellPtr> &multiCells)
+{
+
+ for (unsigned i = 0; i < pvars.size(); ++i) {
+ if (pvars[i].isMulti) {
+ MultiPatternCellPtr multiCell = new MultiPatternCell(NULL);
+ multiCells.push_back(multiCell);
+ cells.push_back(NULL);
+ addLocal(patternEnv, pvars[i].name, multiCell.ptr());
+ }
+ else {
+ PatternCellPtr cell = new PatternCell(NULL);
+ cells.push_back(cell);
+ multiCells.push_back(NULL);
+ addLocal(patternEnv, pvars[i].name, cell.ptr());
+ }
+ }
+
+
+}
+
static void initializePatterns(OverloadPtr x)
{
if (x->patternsInitializedState == 1)
@@ -13,31 +36,17 @@ static void initializePatterns(OverloadPtr x)
CodePtr code = x->code;
const vector<PatternVar> &pvars = code->patternVars;
- x->patternEnv = new Env(x->env);
- for (unsigned i = 0; i < pvars.size(); ++i) {
- if (pvars[i].isMulti) {
- MultiPatternCellPtr multiCell = new MultiPatternCell(NULL);
- x->multiCells.push_back(multiCell);
- x->cells.push_back(NULL);
- addLocal(x->patternEnv, pvars[i].name, multiCell.ptr());
- }
- else {
- PatternCellPtr cell = new PatternCell(NULL);
- x->cells.push_back(cell);
- x->multiCells.push_back(NULL);
- addLocal(x->patternEnv, pvars[i].name, cell.ptr());
- }
- }
-
+ EnvPtr patternEnv = new Env(x->env);
+ initializePatternEnv(patternEnv, pvars, x->cells, x->multiCells);
assert(x->target.ptr());
- x->callablePattern = evaluateOnePattern(x->target, x->patternEnv);
+ x->callablePattern = evaluateOnePattern(x->target, patternEnv);
const vector<FormalArgPtr> &formalArgs = code->formalArgs;
for (unsigned i = 0; i < formalArgs.size(); ++i) {
FormalArgPtr y = formalArgs[i];
PatternPtr pattern;
if (y->type.ptr())
- pattern = evaluateOnePattern(y->type, x->patternEnv);
+ pattern = evaluateOnePattern(y->type, patternEnv);
x->argPatterns.push_back(pattern);
}
@@ -45,7 +54,7 @@ static void initializePatterns(OverloadPtr x)
ExprPtr unpack = new Unpack(code->formalVarArg->type.ptr());
unpack->location = code->formalVarArg->type->location;
x->varArgPattern = evaluateMultiPattern(new ExprList(unpack),
- x->patternEnv);
+ patternEnv);
}
x->patternsInitializedState = 1;
@@ -202,6 +211,20 @@ void printMatchError(llvm::raw_ostream &os, MatchResultPtr result)
os << "predicate \"" << shortString(e->predicateExpr->asString()) << "\" failed";
break;
}
+ case MATCH_BINDING_ERROR : {
+ MatchBindingError *e = (MatchBindingError *)result.ptr();
+ os << "pattern \"" << shortString(e->arg->type->asString());
+ os << "\" did not match type \"";
+ printStaticName(os, e->type.ptr());
+ os << "\" of binding " << e->argIndex + 1;
+ break;
+ }
+ case MATCH_MULTI_BINDING_ERROR : {
+ MatchMultiBindingError *e = (MatchMultiBindingError *)result.ptr();
+ os << "variadic binding type pattern did not match types of values ";
+ os << "starting at " << e->argIndex + 1;
+ break;
+ }
default :
assert(false);
break;
View
4 compiler/src/objects.cpp
@@ -33,9 +33,9 @@ bool _objectValueEquals(ObjectPtr a, ObjectPtr b)
int aKind = a->objKind, bKind = b->objKind;
if (aKind != bKind)
return false;
-
+
switch (aKind) {
-
+
case IDENTIFIER : {
Identifier *a1 = (Identifier *)a.ptr();
Identifier *b1 = (Identifier *)b.ptr();
View
125 compiler/src/parser.cpp
@@ -1076,17 +1076,28 @@ static bool bindingKind(int &bindingKind) {
return true;
}
+static bool optPatternVarsWithCond(vector<PatternVar> &x, ExprPtr &y);
+static bool bindingsBody(vector<FormalArgPtr> &args,
+ FormalArgPtr &varg);
+
static bool localBinding(StatementPtr &x) {
Location location = currentLocation();
int bk;
if (!bindingKind(bk)) return false;
- vector<IdentifierPtr> y;
- if (!identifierList(y)) return false;
+ vector<PatternVar> patternVars;
+ ExprPtr predicate;
+ if (!optPatternVarsWithCond(patternVars, predicate)) return false;
+ vector<FormalArgPtr> args;
+ FormalArgPtr varg;
+ if (!bindingsBody(args,varg)) return false;
+ int p = save();
+ if (!symbol(",")) restore(p);
if (!opsymbol("=")) return false;
ExprListPtr z;
if (!expressionList(z)) return false;
if (!symbol(";")) return false;
- x = new Binding(bk, y, z);
+ vector<ObjectPtr> patternTypes;
+ x = new Binding(bk, patternVars, patternTypes, predicate, args, varg, z);
x->location = location;
return true;
}
@@ -1625,7 +1636,6 @@ static bool optStaticParams(vector<IdentifierPtr> &params,
return true;
}
-
//
// code
@@ -1811,6 +1821,94 @@ static bool arguments(vector<FormalArgPtr> &args,
return true;
}
+
+
+static bool valueBindingArg(FormalArgPtr &x) {
+ Location location = currentLocation();
+ IdentifierPtr y;
+ ExprPtr z;
+ if (!identifier(y)) return false;
+ if (!optTypeSpec(z)) return false;
+ x = new FormalArg(y, z);
+ x->location = location;
+ return true;
+}
+
+static bool bindingArg(FormalArgPtr &x) {
+ int p = save();
+ if (valueBindingArg(x)) return true;
+ return false;
+}
+
+static bool bindingArgs(vector<FormalArgPtr> &x) {
+ FormalArgPtr y;
+ if (!bindingArg(y)) return false;
+ x.clear();
+ while (true) {
+ x.push_back(y);
+ int p = save();
+ if (!symbol(",") || !bindingArg(y)) {
+ restore(p);
+ break;
+ }
+ }
+ return true;
+}
+
+
+static bool bindingVarArg(FormalArgPtr &x) {
+ Location location = currentLocation();
+ if (!ellipsis()) return false;
+ IdentifierPtr name;
+ if (!identifier(name)) return false;
+ ExprPtr type;
+ if (!optVarArgTypeSpec(type)) return false;
+ x = new FormalArg(name, type);
+ x->location = location;
+ return true;
+}
+
+static bool optBindingVarArg(FormalArgPtr &x) {
+ int p = save();
+ if (!bindingVarArg(x)) {
+ restore(p);
+ x = NULL;
+ }
+ return true;
+}
+
+static bool trailingBindingVarArg(FormalArgPtr &x) {
+ if (!symbol(",")) return false;
+ if (!bindingVarArg(x)) return false;
+ return true;
+}
+
+static bool optTrailingBindingVarArg(FormalArgPtr &x) {
+ int p = save();
+ if (!trailingBindingVarArg(x)) {
+ restore(p);
+ x = NULL;
+ }
+ return true;
+}
+
+static bool bindingArgsWithVArgs(vector<FormalArgPtr> &args,
+ FormalArgPtr &varg) {
+ if (!bindingArgs(args)) return false;
+ if (!optTrailingBindingVarArg(varg)) return false;
+ return true;
+}
+
+static bool bindingsBody(vector<FormalArgPtr> &args,
+ FormalArgPtr &varg) {
+ int p = save();
+ if (bindingArgsWithVArgs(args, varg)) return true;
+ restore(p);
+ args.clear();
+ varg = NULL;
+ return optBindingVarArg(varg);
+}
+
static bool predicate(ExprPtr &x) {
if (!keyword("when")) return false;
return expression(x);
@@ -1881,6 +1979,25 @@ static bool optPatternVarsWithCond(vector<PatternVar> &x, ExprPtr &y) {
return true;
}
+static bool patternVars(vector<PatternVar> &x) {
+ if (!symbol("[")) return false;
+ if (!optPatternVarList(x)) return false;
+ if (!symbol("]")) {
+ x.clear();
+ return false;
+ }
+ return true;
+}
+
+static bool optPatternVars(vector<PatternVar> &x) {
+ int p = save();
+ if (!patternVars(x)) {
+ restore(p);
+ x.clear();
+ }
+ return true;
+}
+
static bool exprBody(StatementPtr &x) {
if (!opsymbol("=")) return false;
Location location = currentLocation();
View
2  compiler/src/printer.cpp
@@ -348,7 +348,7 @@ static void printStatement(llvm::raw_ostream &out, const Statement *x) {
default :
assert(false);
}
- out << ", " << y->names << ", " << y->values << ")";
+ out << ", " << y->args << ", " << y->values << ")";
break;
}
case ASSIGNMENT : {
View
6 compiler/src/types.cpp
@@ -384,7 +384,7 @@ void initializeEnumType(EnumTypePtr t) {
CompileContextPusher pusher(t.ptr());
EnvPtr env = new Env(t->enumeration->env);
- evaluateToplevelPredicate(t->enumeration->patternVars, t->enumeration->predicate, env);
+ evaluatePredicate(t->enumeration->patternVars, t->enumeration->predicate, env);
t->initialized = true;
}
@@ -624,7 +624,7 @@ void initializeRecordFields(RecordTypePtr t) {
addLocal(env, r->varParam, rest.ptr());
}
- evaluateToplevelPredicate(r->patternVars, r->predicate, env);
+ evaluatePredicate(r->patternVars, r->predicate, env);
RecordBodyPtr body = r->body;
if (body->isComputed) {
@@ -736,7 +736,7 @@ static void initializeVariantType(VariantTypePtr t) {
}
}
- evaluateToplevelPredicate(t->variant->patternVars,
+ evaluatePredicate(t->variant->patternVars,
t->variant->predicate,
variantEnv);
View
4 packages/archlinux/clay-git/PKGBUILD
@@ -7,8 +7,8 @@ pkgdesc='Clay is a programming language designed for Generic Programming.'
arch=('i686' 'x86_64')
url='http://claylabs.com/clay/'
license=('BSD')
-depends=('llvm=3.0' 'clang=3.0') #'python' 'ocaml' really needed?
-optdepends=('llvm-clay-git: llvm+clang with Clay patches')
+depends=('llvm=3.1' 'clang=3.1') #'python' 'ocaml' really needed?
+optdepends=()
makedepends=('git' 'cmake>=2.6')
provides=('clay')
conflicts=('clay-hg')
View
21 test/bindings/variadic/main.clay
@@ -0,0 +1,21 @@
+import printer.(println);
+
+foo(x, ..y) {
+ var ..a = ..y;
+ nthValue(#0,..a) = 189;
+ return ..a;
+}
+
+main() {
+
+ var [..I] a:Int32, ..c:I = 4, 5ul, 4.0, 5u, 6.8f, 7s;
+ var ..b = ..foo(..c);
+ ref ..j = ..b;
+ println(..weaveValues(", ", ..j));
+ println(..weaveValues(", ", ..c));
+ println(..weaveValues(", ", ..mapValues(Type, ..c)));
+ println(..weaveValues(", ", ..I));
+
+ var zero, one, ..rest = integers(#10);
+ println(..weaveValues(", ", ..rest));
+}
View
5 test/bindings/variadic/out.txt
@@ -0,0 +1,5 @@
+189, 5, 6.8000002, 7
+5, 4, 5, 6.8000002, 7
+UInt64, Float64, UInt32, Float32, Int16
+UInt64, Float64, UInt32, Float32, Int16
+2, 3, 4, 5, 6, 7, 8, 9
View
4 test/hashmaps/main.clay
@@ -20,7 +20,7 @@ printSortedContents(h) {
main() {
var h = HashMap[Int,Int]();
-
+
for (i in range(10))
h[i*i] = i;
@@ -36,7 +36,7 @@ main() {
var hh = HashMap[Int,Int]();
hh = h;
-
+
println();
printSortedContents(hh);
Something went wrong with that request. Please try again.