Skip to content

Commit

Permalink
Don't use special directives to separate top-level definitions in a file
Browse files Browse the repository at this point in the history
  • Loading branch information
electroly committed Jan 2, 2024
1 parent 39f76d8 commit 189c210
Show file tree
Hide file tree
Showing 391 changed files with 51 additions and 575 deletions.
98 changes: 51 additions & 47 deletions src/compiler/SourceProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ static void loadEndCurrentBlock(
for (auto& line : *currentBlock) {
s << line << "\n";
}
members->push_back(std::make_unique<SourceMember>(
currentMemberType, boost::trim_copy(boost::replace_all_copy(s.str(), "##", "#")) + "\n", 0, 0));
members->push_back(std::make_unique<SourceMember>(currentMemberType, boost::trim_copy(s.str()) + "\n", 0, 0));
currentBlock->clear();
}
}
Expand All @@ -106,6 +105,23 @@ void SourceProgram::load(const std::string& filePath) {
loadFromContent(s.str());
}

static void eatLinesUntilEnd(
std::istringstream* stream,
std::vector<std::string>* currentBlock,
const std::string& endPhrase) {
std::string line;
while (std::getline(*stream, line)) {
currentBlock->push_back(line);

// The end phrase is case insensitive.
auto lc = boost::to_lower_copy(line);

if (lc.find(endPhrase) == 0) {
return;
}
}
}

void SourceProgram::loadFromContent(const std::string& content) {
// If this has Windows CRLF line endings, convert it to Unix LF line endings.
if (content.find("\r\n") != std::string::npos) {
Expand All @@ -121,62 +137,50 @@ void SourceProgram::loadFromContent(const std::string& content) {
std::string line;

while (std::getline(file, line)) {
auto isBlockDone = false;
SourceMemberType memberType; // set this when setting isBlockDone=true

if (line.find("#design") == 0) {
memberType = SourceMemberType::kDesign;
isBlockDone = true;
} else if (line.find("#global") == 0) {
memberType = SourceMemberType::kGlobal;
isBlockDone = true;
} else if (line.find("#picture") == 0) {
memberType = SourceMemberType::kPicture;
isBlockDone = true;
} else if (line.find("#procedure") == 0) {
memberType = SourceMemberType::kProcedure;
isBlockDone = true;
} else if (line.find("#type") == 0) {
memberType = SourceMemberType::kType;
isBlockDone = true;
}
currentBlock.push_back(line);

// These keywords are case insensitive.
auto lc = boost::to_lower_copy(line);

if (isBlockDone) {
// Detect the type of block by the starting keyword.
// There can be comment lines above this.
if (lc.find("design") == 0) {
currentMemberType = SourceMemberType::kDesign;
eatLinesUntilEnd(&file, &currentBlock, "end design");
loadEndCurrentBlock(&currentBlock, &members, currentMemberType);
} else if (lc.find("dim") == 0 || line.find("const") == 0) {
currentMemberType = SourceMemberType::kGlobal;
loadEndCurrentBlock(&currentBlock, &members, currentMemberType);
} else if (lc.find("picture") == 0) {
currentMemberType = SourceMemberType::kPicture;
eatLinesUntilEnd(&file, &currentBlock, "end picture");
loadEndCurrentBlock(&currentBlock, &members, currentMemberType);
} else if (lc.find("sub") == 0) {
currentMemberType = SourceMemberType::kProcedure;
eatLinesUntilEnd(&file, &currentBlock, "end sub");
loadEndCurrentBlock(&currentBlock, &members, currentMemberType);
} else if (lc.find("function") == 0) {
currentMemberType = SourceMemberType::kProcedure;
eatLinesUntilEnd(&file, &currentBlock, "end function");
loadEndCurrentBlock(&currentBlock, &members, currentMemberType);
} else if (lc.find("type") == 0) {
currentMemberType = SourceMemberType::kType;
eatLinesUntilEnd(&file, &currentBlock, "end type");
loadEndCurrentBlock(&currentBlock, &members, currentMemberType);
currentMemberType = memberType;
} else {
currentBlock.push_back(line);
}
}

loadEndCurrentBlock(&currentBlock, &members, currentMemberType);
// If there's anything in currentBlock, it's lines at the end that didn't include the start of a block.
// We will just discard those lines.
// Conveniently, when opening our own test programs, these discarded lines are the expected output that is invalid
// TMBASIC code anyway.
}

void SourceProgram::save(const std::string& filePath) const {
auto stream = std::ofstream(filePath);

for (const auto* member : sortMembers(this)) {
switch (member->memberType) {
case SourceMemberType::kDesign:
stream << "#design";
break;
case SourceMemberType::kGlobal:
stream << "#global";
break;
case SourceMemberType::kPicture:
stream << "#picture";
break;
case SourceMemberType::kProcedure:
stream << "#procedure";
break;
case SourceMemberType::kType:
stream << "#type";
break;
default:
assert(false);
break;
}
stream << "\n" << boost::trim_copy(boost::replace_all_copy(member->source, "#", "##")) << "\n\n";
stream << boost::trim_copy(member->source) << "\n\n";
}
}

Expand Down
1 change: 0 additions & 1 deletion src/test/programs/assignments/assign_dotted_1.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = [1]
x(0) = 2
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/assignments/assign_dotted_2.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim y = ["A"]
y(0) = "B"
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/assignments/assign_dotted_3.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = [{ foo: 111 }]
x(0).foo = 999
Expand Down
2 changes: 0 additions & 2 deletions src/test/programs/assignments/assign_dotted_4.bas
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#global
dim x = [{ foo: 111 }]
#procedure
sub Main()
x(0).foo = 999
print x(0).foo
Expand Down
2 changes: 0 additions & 2 deletions src/test/programs/assignments/assign_dotted_5.bas
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#global
dim x as List of Record(foo as Number)
#procedure
sub Main()
print Len(x)
x = [{ foo: 111 }]
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/assignments/assign_list.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim a as List of Number
print Len(a)
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/assignments/assign_string.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim a as String
a = "hello"
Expand Down
2 changes: 0 additions & 2 deletions src/test/programs/assignments/variable_shadowing_function.bas
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#procedure
function Foo() as String
return "hello"
end function

#procedure
sub Main()
dim foo = Foo()
print foo
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/assignments/variable_string.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = "hi"
print x
Expand Down
2 changes: 0 additions & 2 deletions src/test/programs/booleans/and_or_short_circuit.bas
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#procedure
function AlwaysThrows() as Boolean
print "should not have called this!"
throw 1, "error"
end function
#procedure
sub Main()
if true or AlwaysThrows() then
print "1 good"
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/booleans/binary_operators_boolean.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print "and"
print true and true
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/booleans/boolean_literal.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print true
print false
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/booleans/if_false.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x as Boolean
if x then
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/booleans/not_Boolean.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim a = true
print not a
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/console/print_anonymous_record.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print { foo: 4, bar: 5 }
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/console/print_boolean.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print true
print false
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/console/print_date.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print DateFromParts(2019, 2, 9)
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/console/print_datetime.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print DateTimeFromParts(2019, 2, 9, 14, 30, 59, 488)
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/console/print_datetimeoffset.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print DateTimeOffsetFromParts(2019, 2, 9, 14, 30, 59, 488, Hours(-4))
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/console/print_list_number.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print [ 1, 2, 3 ]
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/console/print_map_number_to_number.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x as Map from Number to Number
x(5) = 6
Expand Down
2 changes: 0 additions & 2 deletions src/test/programs/console/print_named_record.bas
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#type
type MyRecord
foo as Number
bar as Number
end type

#procedure
sub Main()
dim x = { foo: 4, bar: 5 } as MyRecord
print x
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/console/print_number.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print 5
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/console/print_optional_number.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x as Optional Number
print x
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/console/print_set_number.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x as Set of Number
x = x + 6
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/console/print_string.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print "hi"
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/constants/built_in_constants.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print "PI="; PI
print "EULER="; EULER
Expand Down
4 changes: 0 additions & 4 deletions src/test/programs/constants/const_global.bas
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#global
const NUM_CONST = 123
#global
const STR_CONST = "hello"
#global
const BOOL_CONST = true
#procedure
sub Main()
print NUM_CONST
print STR_CONST
Expand Down
2 changes: 0 additions & 2 deletions src/test/programs/constants/const_global_cannot_assign.bas
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#global
const MYCONST = 123
#procedure
sub Main()
MYCONST = 20
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/constants/const_local.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
const NUM_CONST = 123
print NUM_CONST
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/constants/const_local_cannot_assign.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
const MYCONST = 10
MYCONST = 20
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/DateFromParts.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print DateFromParts(1776, 7, 4)
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/DateTimeFromParts.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print DateTimeFromParts(1776, 7, 4, 5, 15, 30, 450)
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/DateTimeOffsetFromParts.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim offset = Hours(-6)
dim x = DateTimeOffsetFromParts(2021, 3, 12, 4, 30, 0, 0, offset)
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Day_Date.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = DateFromParts(2023, 12, 25)
print Day(x)
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Day_DateTime.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = DateTimeFromParts(2021, 3, 12, 4, 30, 0, 0)
print Day(x)
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Day_DateTimeOffset.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = DateTimeOffsetFromParts(2021, 3, 12, 4, 30, 59, 488, Hours(-4))
print Day(x)
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Days.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print Days(1.5)
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Hour_DateTime.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = DateTimeFromParts(2021, 3, 12, 14, 30, 0, 0)
print Hour(x)
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Hour_DateTimeOffset.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = DateTimeOffsetFromParts(2021, 3, 12, 14, 30, 59, 488, Hours(-4))
print Hour(x)
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Hours.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print Hours(1.5)
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Millisecond_DateTime.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = DateTimeFromParts(2021, 3, 12, 14, 30, 59, 488)
print Millisecond(x)
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Millisecond_DateTimeOffset.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = DateTimeOffsetFromParts(2021, 3, 12, 4, 30, 59, 488, Hours(-4))
print Millisecond(x)
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Milliseconds.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print Milliseconds(1.5)
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Minute_DateTime.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = DateTimeFromParts(2021, 3, 12, 14, 30, 0, 0)
print Minute(x)
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Minute_DateTimeOffset.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = DateTimeOffsetFromParts(2021, 3, 12, 4, 30, 59, 488, Hours(-4))
print Minute(x)
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Minutes.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
print Minutes(1.5)
end sub
Expand Down
1 change: 0 additions & 1 deletion src/test/programs/dates/Month_Date.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#procedure
sub Main()
dim x = DateFromParts(2023, 12, 25)
print Month(x)
Expand Down
Loading

0 comments on commit 189c210

Please sign in to comment.