Skip to content

Commit ee08dba

Browse files
author
robertDurst
committed
hack together a working version that supports a binary tree
1 parent fc72a34 commit ee08dba

File tree

5 files changed

+195
-55
lines changed

5 files changed

+195
-55
lines changed

examples/stack.fish

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Uat {
2+
stack next
3+
int data
4+
}
5+
6+
Ufn{
7+
(fun hasNext(void)(bool) {
8+
Tree ( (| own.next == empty | { return false } ))
9+
return true
10+
})
11+
12+
(fun append(stack s)(void) {
13+
own.next = s
14+
})
15+
}

examples/test.fish

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
import intListHandler : "../examples/intListHandler.fish"
1+
import treenode : "../examples/treenode.fish"
22

33
start {
4-
dec intListHandler i = new intListHandler { size: 100, list: []}
5-
dec [int] is = []
4+
dec treenode root = new treenode { data: 10, left: empty, right: empty }
5+
6+
dec treenode a = new treenode { data: 7, left: empty, right: empty }
7+
dec treenode b = new treenode { data: 60, left: empty, right: empty }
8+
dec treenode c = new treenode { data: 9, left: empty, right: empty }
9+
dec treenode d = new treenode { data: 8, left: empty, right: empty }
10+
dec treenode e = new treenode { data: 11, left: empty, right: empty }
11+
12+
root...addNode(a)
13+
root...addNode(b)
14+
root...addNode(c)
15+
root...addNode(d)
16+
root...addNode(e)
17+
18+
root...inorderTraversal(void)
619
}

examples/treenode.fish

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Uat {
2+
int data
3+
treenode left
4+
treenode right
5+
}
6+
7+
Ufn{
8+
(fun hasLeft(void)(bool) {
9+
Tree ( (| own.left == empty | { return false } ))
10+
return true
11+
})
12+
13+
(fun hasRight(void)(bool) {
14+
Tree ( (| own.right == empty | { return false } ))
15+
return true
16+
})
17+
18+
(fun setLeft(treenode tn)(void) {
19+
own.left = tn
20+
})
21+
22+
(fun setRight(treenode tn)(void) {
23+
own.right = tn
24+
})
25+
26+
(fun addNode(treenode tn)(void) {
27+
Tree (
28+
( | (tn.data < own.data) and (!own...hasLeft(void)) | {
29+
own.left = tn
30+
})
31+
32+
( | (tn.data < own.data) and (own...hasLeft(void)) | {
33+
own.left...addNode(tn)
34+
})
35+
36+
( | (tn.data >= own.data) and (!own...hasRight(void)) | {
37+
own.right = tn
38+
})
39+
40+
( | (tn.data >= own.data) and (own...hasRight(void)) | {
41+
own.right...addNode(tn)
42+
})
43+
)
44+
})
45+
46+
(fun inorderTraversal(void)(void) {
47+
Tree (
48+
# left
49+
( | own...hasLeft(void) | {
50+
own.left...inorderTraversal(void)
51+
})
52+
)
53+
54+
# root
55+
printInt(own.data)
56+
57+
Tree (
58+
# right
59+
( | own...hasRight(void) | {
60+
own.right...inorderTraversal(void)
61+
})
62+
)
63+
})
64+
}

src/sailfish/sailfishc.cpp

Lines changed: 99 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ sailfishc::checkType(const std::string& t0, const std::string& t1)
126126
auto right =
127127
symboltable->hasVariable(t1) ? symboltable->getSymbolType(t1) : t1;
128128

129+
// if either is a udt, make sure we check udt type, not U which is returned
130+
// by symbol table
131+
left = left == "U" && udttable->hasUDT(t0) ? t0 : left;
132+
right = right == "U" && udttable->hasUDT(t1) ? t1 : right;
133+
129134
// should actually check types here
130135

131136
// edge case lists
@@ -261,16 +266,24 @@ sailfishc::checkFunctionCall(const std::string& name,
261266
bool flag = false;
262267
for (int i = 0; i < inputs.size(); i++)
263268
{
269+
auto left = symboltable->hasVariable(inputs[i])
270+
? symboltable->getSymbolType(inputs[i])
271+
: inputs[i];
272+
auto right = symboltable->hasVariable(fcInputs[i])
273+
? symboltable->getSymbolType(fcInputs[i])
274+
: fcInputs[i];
275+
264276
if (inputs[i].at(0) == '[')
265277
inputs[i] = extractListType(inputs[i]);
266278

267279
if (fcInputs[i].at(0) == '[')
268280
fcInputs[i] = extractListType(fcInputs[i]);
269281

270-
if (inputs[i] != fcInputs[i])
282+
if (left != right)
271283
semanticerrorhandler->handle(std::make_unique<Error>(Error(
272284
currentToken->col, currentToken->line,
273-
"Function input parameter type mismatch in function call " +
285+
"Function input parameter type mismatch in "
286+
"function call " +
274287
name,
275288
"Expected " + inputs[i] + " and received: ", fcInputs[i],
276289
".")));
@@ -740,46 +753,48 @@ sailfishc::parseFunctionInOut(const std::string& name)
740753
bool seenVoid = false;
741754
int argCount = 0;
742755
std::string outputBuffer = "";
743-
recursiveParse(
744-
true, TokenKind::RPAREN,
745-
[&outputBuffer, &types, &seenVoid, &argCount, this]() {
746-
++argCount;
747-
748-
auto sands = this->parseVariable();
749-
auto name = std::get<0>(sands);
750-
auto type = std::get<1>(sands);
751-
752-
if (type != "void")
753-
if (outputBuffer != "")
754-
outputBuffer +=
755-
", " + builtinTypesTranslator(type) + " " + name;
756-
else
757-
outputBuffer += builtinTypesTranslator(type) + " " + name;
758-
else if (!isUdt)
759-
{
760-
if (outputBuffer != "")
761-
outputBuffer += ", " + builtinTypesTranslator(type);
762-
else
763-
outputBuffer += builtinTypesTranslator(type);
764-
}
765-
766-
if (type == "void")
767-
seenVoid = true;
768-
769-
if (argCount > 1 && seenVoid)
770-
{
771-
semanticerrorhandler->handle(std::make_unique<Error>(
772-
Error(currentToken->col, currentToken->line,
773-
"Illegal multi-void definition of formals "
774-
"in function signature",
775-
"", "", "")));
776-
}
777-
778-
if (type != "void")
779-
symboltable->addSymbol(std::get<0>(sands), type);
780-
781-
types += "_" + type;
782-
});
756+
recursiveParse(true, TokenKind::RPAREN,
757+
[&outputBuffer, &types, &seenVoid, &argCount, this]() {
758+
++argCount;
759+
760+
auto sands = this->parseVariable();
761+
auto name = std::get<0>(sands);
762+
auto type = std::get<1>(sands);
763+
764+
auto outedType = builtinTypesTranslator(type);
765+
if (udttable->hasUDT(type))
766+
outedType = "struct " + outedType + "*";
767+
768+
if (type != "void")
769+
if (outputBuffer != "")
770+
outputBuffer += ", " + outedType + " " + name;
771+
else
772+
outputBuffer += outedType + " " + name;
773+
else if (!isUdt)
774+
{
775+
if (outputBuffer != "")
776+
outputBuffer += ", " + outedType;
777+
else
778+
outputBuffer += outedType;
779+
}
780+
781+
if (type == "void")
782+
seenVoid = true;
783+
784+
if (argCount > 1 && seenVoid)
785+
{
786+
semanticerrorhandler->handle(std::make_unique<Error>(
787+
Error(currentToken->col, currentToken->line,
788+
"Illegal multi-void definition of formals "
789+
"in function signature",
790+
"", "", "")));
791+
}
792+
793+
if (type != "void")
794+
symboltable->addSymbol(std::get<0>(sands), type);
795+
796+
types += "_" + type;
797+
});
783798
advanceAndCheckToken(TokenKind::RPAREN); // consume r paren
784799

785800
// outputs
@@ -790,6 +805,9 @@ sailfishc::parseFunctionInOut(const std::string& name)
790805

791806
if (udttable->hasUDT(output))
792807
output = "struct " + output + "*";
808+
else
809+
output = builtinTypesTranslator(output);
810+
793811
advanceAndCheckToken(TokenKind::RPAREN); // consume r paren
794812

795813
if (isUdt)
@@ -1209,6 +1227,9 @@ sailfishc::parseE7(const std::string& T0)
12091227
auto type = T0;
12101228
if (!isPrimitive(T0))
12111229
type = this->symboltable->getSymbolType(T0);
1230+
if (type == "U")
1231+
type = T0;
1232+
12121233
this->checkType(type, T1);
12131234
return T1;
12141235
},
@@ -1300,7 +1321,9 @@ sailfishc::parseE10(const std::string& T0)
13001321
{
13011322
auto type = parseMemberAccess(T0);
13021323

1303-
return parseE1(type);
1324+
type = parseE1(type);
1325+
1326+
return type;
13041327
}
13051328

13061329
return parseE11(T0);
@@ -1398,12 +1421,16 @@ sailfishc::parseAttributeAccess(const std::string& udtname,
13981421
advanceAndCheckToken(TokenKind::DOT); // consume '.'
13991422
auto attribute = parseIdentifier();
14001423

1401-
if (udtname != "own")
1402-
targetBuffer += udtname;
1424+
if (currentToken->kind == TokenKind::TRIPLE_DOT)
1425+
attributeAccessName = attribute;
14031426
else
1404-
targetBuffer += "this";
1405-
1406-
targetBuffer += "->" + builtinTypesTranslator(attribute);
1427+
{
1428+
if (udtname != "own")
1429+
targetBuffer += udtname;
1430+
else
1431+
targetBuffer += "this";
1432+
targetBuffer += "->" + builtinTypesTranslator(attribute);
1433+
}
14071434

14081435
// check if type exists
14091436
if (!st->hasVariable(attribute))
@@ -1479,10 +1506,24 @@ sailfishc::parseFunctionCall()
14791506
});
14801507

14811508
if (methodAccessName != "")
1509+
{
14821510
if (nonVoidInputs == 0)
1483-
targetBuffer += methodAccessName;
1511+
{
1512+
if (attributeAccessName != "")
1513+
targetBuffer += "this->" + attributeAccessName;
1514+
else
1515+
targetBuffer += methodAccessName;
1516+
}
14841517
else
1485-
targetBuffer += ", " + methodAccessName;
1518+
{
1519+
if (attributeAccessName != "")
1520+
targetBuffer += ", this->" + attributeAccessName;
1521+
else
1522+
targetBuffer += ',' + methodAccessName;
1523+
}
1524+
}
1525+
1526+
attributeAccessName = "";
14861527

14871528
types += ")";
14881529

@@ -1650,8 +1691,14 @@ sailfishc::parsePrimary()
16501691

16511692
if (methodAccessName == "" ||
16521693
(methodAccessName != "" && type != "void"))
1653-
// if (!udttable->hasUDT(symboltable->getSymbolType(type)))
1654-
targetBuffer += builtinTypesTranslator(type);
1694+
if ((currentToken->kind != TokenKind::TRIPLE_DOT) &&
1695+
(currentToken->kind != TokenKind::DOT))
1696+
{
1697+
if (udttable->hasUDT(type))
1698+
targetBuffer += "struct " + type + "*";
1699+
else
1700+
targetBuffer += builtinTypesTranslator(type);
1701+
}
16551702

16561703
return type;
16571704
}

src/sailfish/sailfishc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class sailfishc
8080
int currentTabs = 0;
8181
std::string targetBuffer = "";
8282
std::string methodAccessName = "";
83+
std::string attributeAccessName = "";
8384

8485
// ------- Transpiler -------- //
8586
std::ofstream output;

0 commit comments

Comments
 (0)