Skip to content

Commit

Permalink
WIP break continue
Browse files Browse the repository at this point in the history
  • Loading branch information
cdoucy committed Oct 29, 2023
1 parent 3d3a613 commit 090f21c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 24 deletions.
4 changes: 3 additions & 1 deletion doc/operator-precedence.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

```
program -> statement* EOF
statement -> expressionStatement | declarationStatement | assignmentStatement | incDecStatement | returnStatement | print | block | while | for | if | function
statement -> expressionStatement | declarationStatement | assignmentStatement | incDecStatement | returnStatement | breakStatement | continueStatement | print | block | while | for | if | function
expressionStatement -> expression ';'
declarationStatement -> declaration ';'
assignementStatement -> assignement ';'
incDecStatement -> incDec ';'
returnStatement -> return (expression)? ';'
breakStatement -> break ';'
continueStatement -> continue ';'
declaration -> Type Identifier ('=' expression)?
assignment -> Identifier assimentOperator expression
assignmentOperator -> '=' | '+= | '+-' | '*=' | '%=' | '/='
Expand Down
8 changes: 3 additions & 5 deletions error-input.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
fnc f() int {
}

f()();
print(f());
{
return 1;
}
1 change: 1 addition & 0 deletions inc/token/token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Token
IF, ELSE,
FOR,
FNC, RETURN,
BREAK, CONTINUE,

// Identifier
IDENTIFIER
Expand Down
2 changes: 2 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ void runFileMode(const std::string &filePath)

int main(int ac, char **av)
{
for (int i = 0; i < 10; i++) continue;

try {
if (ac < 2)
runInteractiveMode();
Expand Down
14 changes: 14 additions & 0 deletions tst/lexer/lexer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ TEST(LexerTest, KeyWords)
.expected = {
Token{Token::RETURN, "return"}
}
},
LexerTest{
.description = "7. break",
.expression = "break",
.expected = {
Token{Token::BREAK, "break"}
}
},
LexerTest{
.description = "8. continue",
.expression = "continue",
.expected = {
Token{Token::CONTINUE, "continue"}
}
}

};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,75 +1,90 @@
#include <gtest/gtest.h>
#include "Parser.hpp"

struct FunctionsErrorTest
struct ParserErrorTest
{
std::string description;
std::string program;
};

void testFunctionsError(const std::vector<FunctionsErrorTest> &testCases)
void testParserError(const std::vector<ParserErrorTest> &testCases)
{
for (const auto &tc: testCases) {
std::cout << tc.description << std::endl;

Parser parser;

EXPECT_THROW(parser.feed(tc.program), SyntaxError);
}
}

TEST(ParserTest, FunctionsDeclarationError)
{
const std::vector<FunctionsErrorTest> testCases{
FunctionsErrorTest{
const std::vector<ParserErrorTest> testCases{
ParserErrorTest{
.description = "1. Function declaration without identifier",
.program = "fnc () {}"
},
FunctionsErrorTest{
ParserErrorTest{
.description = "1. Function declaration without identifier",
.program = "fnc () {}"
},
FunctionsErrorTest{
ParserErrorTest{
.description = "2. Function declaration without parenthesis",
.program = "fnc f {}"
},
FunctionsErrorTest{
ParserErrorTest{
.description = "3. Function declaration with already used parameter name",
.program = "fnc f (int x, int x){}"
},
FunctionsErrorTest{
ParserErrorTest{
.description = "4. Function declaration without close parenthesis",
.program = "fnc f (int x, int y {}"
},
FunctionsErrorTest{
ParserErrorTest{
.description = "5. Function declaration without body",
.program = "fnc f (int x, int y)"
},
FunctionsErrorTest{
ParserErrorTest{
.description = "6. Function declaration with return type and without body",
.program = "fnc f (int x, int y) int"
}
};

testFunctionsError(testCases);
testParserError(testCases);
}

TEST(ParserTest, CallError)
{
const std::vector<FunctionsErrorTest> testCases{
FunctionsErrorTest{
const std::vector<ParserErrorTest> testCases{
ParserErrorTest{
.description = "1. Missing param or closed parenthesis",
.program = "f(;"
},
FunctionsErrorTest{
ParserErrorTest{
.description = "2. Missing closed parenthesis",
.program = "f(1, 2;"
},
FunctionsErrorTest{
ParserErrorTest{
.description = "3. Missing expression after comma",
.program = "f(1, );"
}
};

testFunctionsError(testCases);
testParserError(testCases);
}

TEST(ParserTest, BreakContinueError)
{
const std::vector<ParserErrorTest> testCases{
ParserErrorTest{
.description = "1. Missing semi after break",
.program = "break"
},
ParserErrorTest{
.description = "2. Missing semi after continue",
.program = "continue"
},
};

testParserError(testCases);
}
2 changes: 1 addition & 1 deletion tst/tst.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ set(
${PROJECT_ROOT}/tst/evaluator/evaluator_test.cpp
${PROJECT_ROOT}/tst/evaluator/statements_test.cpp
${PROJECT_ROOT}/tst/parser/parser_test.cpp
${PROJECT_ROOT}/tst/parser/functions_error_test.cpp
${PROJECT_ROOT}/tst/parser/parser_error_test.cpp
)

0 comments on commit 090f21c

Please sign in to comment.