Skip to content

Commit

Permalink
fixed long standing bug with a function that takes one named argument
Browse files Browse the repository at this point in the history
  • Loading branch information
wmww committed Apr 19, 2017
1 parent dc5b80e commit f2fabee
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 25 deletions.
25 changes: 23 additions & 2 deletions other/pinecone.pn
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

//
Foo :: {x: Int, y: Dub}

Foo :: {Int} -> {Foo}:
Expand All @@ -11,6 +12,26 @@ foo: Foo: 2
print: foo.x
print: foo.y

bar: tru, 47
bar: tru, 47, ("yo", 2.9)

print: bar.c.a
\\

//
print: "8\n_____"

myFunc: 8

myFunc :: {arg: Int}: (

print: in.arg
)
\\

func :: {} -> {a: Int}: 8

foo: func
bar: Int
bar: foo
print: bar

print: bar.b
43 changes: 31 additions & 12 deletions src/Actions/MakeTupleAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class CppTupleCastAction: public ActionData
CppTupleCastAction(Action actionIn, Type returnType):
ActionData(returnType, Void, Void)
{
if (actionIn->getReturnType()->getType()!=TypeBase::TUPLE || getReturnType()->getType()!=TypeBase::TUPLE || !actionIn->getReturnType()->matches(getReturnType()))
if ((actionIn->getReturnType()->getType()!=TypeBase::TUPLE && getReturnType()->getType()!=TypeBase::TUPLE) || !actionIn->getReturnType()->matches(getReturnType()))
{
throw PineconeError("CppCastAction was only designed to cast matching tuples, which is not how it is being used", INTERNAL_ERROR);
}
Expand Down Expand Up @@ -173,6 +173,12 @@ class CppTupleCastAction: public ActionData
{
addListToProgWithCppCasting((ListAction*)&*action, getReturnType(), prog);
}
else if (getReturnType()->getType()!=TypeBase::TUPLE)
{
action->addToProg(prog);
prog->code(".");
prog->code(action->getReturnType()->getAllSubTypes()[0][0].name);
}
else
{
string funcName=action->getReturnType()->getCompactString()+"=>"+getReturnType()->getCompactString();
Expand All @@ -184,23 +190,36 @@ class CppTupleCastAction: public ActionData
prog->pushFunc(funcName, "", Void, argType, getReturnType());
prog->declareVar("-out", getReturnType());

auto inTypes=*action->getReturnType()->getAllSubTypes();
auto outTypes=*getReturnType()->getAllSubTypes();

for (int i=0; i<int(inTypes.size()); i++)
if (action->getReturnType()->getType()==TypeBase::TUPLE)
{
//if (inTypes[i].type==outTypes[i].type)
auto inTypes=*action->getReturnType()->getAllSubTypes();

for (int i=0; i<int(inTypes.size()); i++)
{
prog->name("-out");
prog->code(".");
prog->code(outTypes[i].name);
prog->code(" = ");
prog->name("in");
prog->code(".");
prog->code(inTypes[i].name);
prog->endln();
//if (inTypes[i].type==outTypes[i].type)
{
prog->name("-out");
prog->code(".");
prog->code(outTypes[i].name);
prog->code(" = ");
prog->name("in");
prog->code(".");
prog->code(inTypes[i].name);
prog->endln();
}
}
}
else
{
prog->name("-out");
prog->code(".");
prog->code(outTypes[0].name);
prog->code(" = ");
prog->name("in");
prog->endln();
}

prog->code("return ");
prog->name("-out");
Expand Down
27 changes: 22 additions & 5 deletions src/Namespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,23 +255,32 @@ Action NamespaceData::getActionForTokenWithInput(Token token, Type left, Type ri
{
vector<Action> matches;
vector<AstNodeBase*> nodes;
bool foundNodes=false;
AstNode tupleNode; // this is needed for memory management, so if a tuple node is needed it can be kept around until the function exits

string searchText = (token->getOp() ? token->getOp()->getText() : token->getText());

getNodes(nodes, searchText, true, dynamic, false);

nodesToMatchingActions(matches, nodes, left, right);

if (left->getType()==TypeBase::TUPLE && token->getType()==TokenData::IDENTIFIER)
{
auto match=left->getSubType(searchText);

if (match.type)
{
matches.push_back(getElemFromTupleAction(left, searchText));
if (right->isVoid())
{
tupleNode=AstActionWrapper::make(getElemFromTupleAction(left, searchText));
nodes.push_back(&*tupleNode);
}
}
}

if (!nodes.empty())
foundNodes=true;

nodesToMatchingActions(matches, nodes, left, right);

if (!matches.empty())
{
if (matches.size() == 1)
Expand All @@ -291,6 +300,9 @@ Action NamespaceData::getActionForTokenWithInput(Token token, Type left, Type ri
nodes.clear();
getNodes(nodes, searchText, false, false, true);

if (!nodes.empty())
foundNodes=true;

for (auto i: nodes)
{
if (typeid(*i)!=typeid(AstFuncBody))
Expand Down Expand Up @@ -323,14 +335,17 @@ Action NamespaceData::getActionForTokenWithInput(Token token, Type left, Type ri
}
}

if (dynamic && token->getType()==TokenData::IDENTIFIER && left->isVoid() && right->isCreatable())
if (!foundNodes && dynamic && token->getType()==TokenData::IDENTIFIER && left->isVoid() && right->isCreatable())
{
return addVar(right, token->getText());
}

if (throwSourceError)
{
throw PineconeError("'"+token->getText()+"' not found", SOURCE_ERROR, tokenForError);
if (foundNodes)
throw PineconeError("correct overload of '"+token->getText()+"' not found", SOURCE_ERROR, tokenForError);
else
throw PineconeError("'"+token->getText()+"' not found", SOURCE_ERROR, tokenForError);
}
else
{
Expand Down Expand Up @@ -367,6 +382,8 @@ void NamespaceData::nodesToMatchingActions(vector<Action>& out, vector<AstNodeBa

if (action->getInLeftType()->matches(leftInType) && action->getInRightType()->matches(rightInType))
out.push_back(action);

//Action converter=getConverter(action, leftInType, rightInType);
}
}

29 changes: 23 additions & 6 deletions src/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,17 +610,34 @@ bool TypeBase::matches(Type other)
return true;

if (getType()==WHATEV || other->getType()==WHATEV)
{
return true;
}
else if (other->getType()!=getType())
if (getType()==TUPLE)
{
return false;
auto subTypes=getAllSubTypes();

if (subTypes->size()==1)
{
if ((*subTypes)[0].type->matches(other))
return true;
}
}
else

if (other->getType()==TUPLE)
{
return matchesSameTypeType(other);
auto subTypes=other->getAllSubTypes();

if (subTypes->size()==1)
{
if ((*subTypes)[0].type->matches(shared_from_this()))
return true;
}
}

if (other->getType()!=getType())
return false;

return matchesSameTypeType(other);
}

Type TypeBase::actuallyIs(Type target)
Expand Down

0 comments on commit f2fabee

Please sign in to comment.