Skip to content

Commit 74647b2

Browse files
author
robertDurst
committed
README update and a few more edge cases for function nesting transpilation made correcter
1 parent 2bfb6ef commit 74647b2

File tree

9 files changed

+103
-61
lines changed

9 files changed

+103
-61
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![sailfish jumping](https://media.giphy.com/media/l0fDZGf4DpQ5i/giphy.gif)
44

5-
The Sailfish programming language is an experimental project I am undertaking during my Junior Spring as part of my compiler independent study under Professor Dale Skrien at Colby College. This is the initial version, a dirty version that will be used for bootstrapping.
5+
The Sailfish programming language is an experimental project I am undertaking during my Junior Spring as part of my compiler independent study under Professor Dale Skrien at Colby College. This is by no means something that should ever be used for anything that touches production. This is a toy language. Something that will often break, has no garauntees of being backwards compatible, and at any time may go unmaintained. That being said, if you're also a student or programming language enthusiast and want to work on something fun/cool, open an issue, or fork this repo. Let's have some fun!
66

77
***
88

@@ -16,6 +16,22 @@ For now, you can either build from source:
1616

1717
Or use the install script [here](https://github.com/sailfish-lang/sailfish-lang-install-script.git).
1818

19+
***
20+
21+
## Language Limitations
22+
23+
There exist a number of known issues with the Sailfishc compiler. Some of these issues are limitations of the languages, others are simply results of incorrect implementation.
24+
25+
1. mutual recursion
26+
2. importing udt's within udt's impossible
27+
3. name collisions if udt's/script have similarly named attributes/methods/variables
28+
4. No udt constructors
29+
5. no udt static methods
30+
6. double dispatch does not transpile properly
31+
7. functions are not first class
32+
8. declaring more than one attribute accessor within function call params does not transpile correctly
33+
34+
1935
***
2036

2137
## Sailfish Wiki

examples/Foo.fish

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Uat {
44
}
55

66
Ufn {
7-
(fun bar(int i)(int) {
8-
return (i + own.i)
7+
(fun bar(int i, int a)(int) {
8+
return (i + own.i + a)
99
})
1010

1111
(fun self(void)(Foo) {

examples/function.fish

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(fun foo(int i)(str) {
2+
Tree (
3+
( | i == 5 | { return "I received a five!"})
4+
( | i == 6 | { return "I gotta size?!?"})
5+
)
6+
return "Just some number that is not a five or six."
7+
})
8+
9+
start {
10+
11+
}

examples/helloworld.fish

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
start {
2+
printStr("Hello World!")
3+
}

examples/mergesort.fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ start {
7878
dec intListHandler a = new intListHandler { list: [5, 10, 12, 3, 4, 2, 11, 1], size: 8}
7979
a = mergesort(a, 0, a.size)
8080
a...display(void)
81-
}
81+
}

examples/test.fish

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@ start {
99
dec Foo d = new Foo {i: 5, s: c}
1010

1111
f = b.s.s
12-
printInt(f...bar(f...bar(10)))
13-
printInt(b.s...bar(9))
14-
printInt(b.s...bar(i))
15-
#printInt(b.s...bar(d.s.i))
16-
}
12+
printInt(b.s...bar(d.s.i))
13+
}

src/sailfish/sailfishc.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,9 +1660,8 @@ sailfishc::parsePrimary()
16601660
return parseListType();
16611661

16621662
auto type = parseIdentifier();
1663-
1664-
transpiler->genPrimary(((currentToken->kind != TokenKind::TRIPLE_DOT) &&
1665-
(currentToken->kind != TokenKind::DOT)),
1663+
transpiler->genPrimary((currentToken->kind != TokenKind::TRIPLE_DOT),
1664+
(currentToken->kind != TokenKind::DOT),
16661665
udttable->hasUDT(type), type);
16671666

16681667
return type;

src/transpiler/transpiler.cpp

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Transpiler::Transpiler()
8181
currentTabs = 0;
8282
decName = "";
8383
decType = "";
84+
bufferToAdd = 0;
8485
}
8586

8687
std::string
@@ -328,6 +329,9 @@ Transpiler::genAttributeAccess(bool nextIsTripleDot, bool udtNameIsUDT,
328329
attributeAccessStack.push_back(std::make_tuple(
329330
std::get<0>(top) + "->" + extractChainAAType(std::get<1>(top)),
330331
attribute));
332+
333+
attributeAccessStack.erase(attributeAccessStack.begin() +
334+
attributeAccessStack.size() - 2);
331335
}
332336
else
333337
attributeAccessStack.push_back(std::make_tuple(udtname, attribute));
@@ -367,13 +371,13 @@ Transpiler::genListItem(const std::string& index, const std::string& value)
367371
}
368372

369373
void
370-
Transpiler::genPrimary(bool isNotBeforeMemberAccessor, bool isNotUdtName,
371-
const std::string& type)
374+
Transpiler::genPrimary(bool nextIsNotTripleDot, bool nextIsNotSingleDot,
375+
bool isNotUdtName, const std::string& type)
372376
{
373377
if (methodAccessStack.size() == 0 ||
374378
(methodAccessStack.size() != 0 && type != "void"))
375379
{
376-
if (isNotBeforeMemberAccessor)
380+
if (nextIsNotTripleDot && nextIsNotSingleDot)
377381
{
378382
if (isNotUdtName)
379383
{
@@ -384,65 +388,75 @@ Transpiler::genPrimary(bool isNotBeforeMemberAccessor, bool isNotUdtName,
384388
buffer += builtinTypesTranslator(type);
385389
}
386390
}
387-
else
391+
else if (nextIsNotSingleDot)
388392
{
389-
if (attributeAccessStack.size() != 0)
390-
{
391-
buffer += std::get<0>(
392-
attributeAccessStack.at(attributeAccessStack.size() - 1));
393-
attributeAccessStack.pop_back();
394-
}
393+
++bufferToAdd;
395394
}
396395
}
397396
}
398397

399398
void
400399
Transpiler::genFinalFunctionCallArg(bool noVoids, bool isUdt)
401400
{
402-
if (noVoids)
401+
for (int i = 0; i < bufferToAdd + 1 && attributeAccessStack.size() > 0; i++)
403402
{
404-
if (attributeAccessStack.size() != 0 && isUdt)
405-
{
406-
buffer += std::get<1>(
407-
attributeAccessStack.at(attributeAccessStack.size() - 1));
408-
attributeAccessStack.pop_back();
409-
}
410-
else if (attributeAccessStack.size() != 0 && !isUdt)
411-
{
412-
buffer +=
413-
std::get<0>(
414-
attributeAccessStack.at(attributeAccessStack.size() - 1)) +
415-
"->" +
416-
std::get<1>(
417-
attributeAccessStack.at(attributeAccessStack.size() - 1));
418-
attributeAccessStack.pop_back();
419-
}
420-
else if (methodAccessStack.size() != 0)
421-
buffer +=
422-
std::get<0>(methodAccessStack.at(methodAccessStack.size() - 1));
403+
if (i != 0)
404+
buffer += ",";
405+
buffer += std::get<0>(attributeAccessStack.at(
406+
attributeAccessStack.size() - 1)) +
407+
"->" +
408+
std::get<1>(
409+
attributeAccessStack.at(attributeAccessStack.size() - 1));
410+
attributeAccessStack.erase(attributeAccessStack.begin() +
411+
attributeAccessStack.size() - 1);
423412
}
424-
else
413+
bufferToAdd = 0;
414+
415+
if (methodAccessStack.size() != 0)
425416
{
426-
if (attributeAccessStack.size() != 0 && isUdt)
417+
if (noVoids)
427418
{
428-
buffer += ", " + std::get<1>(attributeAccessStack.at(
429-
attributeAccessStack.size() - 1));
430-
attributeAccessStack.pop_back();
419+
if (attributeAccessStack.size() != 0 && isUdt)
420+
{
421+
buffer += std::get<1>(
422+
attributeAccessStack.at(attributeAccessStack.size() - 1));
423+
attributeAccessStack.erase(attributeAccessStack.begin() +
424+
attributeAccessStack.size() - 1);
425+
}
426+
else if (attributeAccessStack.size() != 0 && !isUdt)
427+
{
428+
buffer += std::get<0>(attributeAccessStack.at(
429+
attributeAccessStack.size() - 1)) +
430+
"->" +
431+
std::get<1>(attributeAccessStack.at(
432+
attributeAccessStack.size() - 1));
433+
// attributeAccessStack.pop_back();
434+
attributeAccessStack.erase(attributeAccessStack.begin() +
435+
attributeAccessStack.size() - 1);
436+
}
437+
else
438+
buffer += std::get<0>(
439+
methodAccessStack.at(methodAccessStack.size() - 1));
431440
}
432-
else if (attributeAccessStack.size() != 0 && !isUdt)
441+
else
433442
{
434-
buffer +=
435-
", " +
436-
std::get<0>(
437-
attributeAccessStack.at(attributeAccessStack.size() - 1)) +
438-
"->" +
439-
std::get<1>(
440-
attributeAccessStack.at(attributeAccessStack.size() - 1));
441-
attributeAccessStack.pop_back();
443+
if (attributeAccessStack.size() != 0 && isUdt)
444+
{
445+
buffer += ", " + std::get<1>(attributeAccessStack.at(
446+
attributeAccessStack.size() - 1));
447+
attributeAccessStack.erase(attributeAccessStack.begin() +
448+
attributeAccessStack.size() - 1);
449+
}
450+
else if (attributeAccessStack.size() != 0 && !isUdt)
451+
{
452+
buffer += ", " + std::get<0>(attributeAccessStack.at(0)) +
453+
"->" + std::get<1>(attributeAccessStack.at(0));
454+
// attributeAccessStack.;
455+
attributeAccessStack.erase(attributeAccessStack.begin());
456+
}
457+
else
458+
buffer += ", " + std::get<0>(methodAccessStack.at(
459+
methodAccessStack.size() - 1));
442460
}
443-
else if (methodAccessStack.size() != 0)
444-
buffer +=
445-
", " +
446-
std::get<0>(methodAccessStack.at(methodAccessStack.size() - 1));
447461
}
448462
}

src/transpiler/transpiler.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66
#include "../stdlib_c/stdlib_c.h"
77
#include <fstream>
8+
#include <iostream>
89
#include <string>
910
#include <tuple>
1011
#include <vector>
@@ -20,6 +21,7 @@ class Transpiler
2021
std::string decType;
2122
int currentTabs;
2223
std::ofstream output;
24+
int bufferToAdd;
2325

2426
// methods
2527
std::string getTabs();
@@ -100,6 +102,6 @@ class Transpiler
100102
void genLiteral(const std::string&);
101103
void genListInit(const std::string& type, const std::string size);
102104
void genListItem(const std::string&, const std::string&);
103-
void genPrimary(bool, bool, const std::string&);
105+
void genPrimary(bool, bool, bool, const std::string&);
104106
void genFinalFunctionCallArg(bool noVoids, bool isUdt);
105107
};

0 commit comments

Comments
 (0)