Skip to content

Commit ea09136

Browse files
author
robertDurst
committed
extract testing... and semantic analysis tests pass
1 parent 852cd8f commit ea09136

7 files changed

+94
-65
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ADD_LIBRARY(SailfishcLibs
1515
./src/semantics/SymbolTable.cpp
1616
./src/stdlib_c/stdlib_c.cpp
1717
./src/stdlib_c/Lists.cpp
18+
./src/tests/SemanticAnalysisTest.cpp
1819
)
1920

2021
add_executable(sailfishc ./src/main/main.cpp ${DirSOURCES})

examples/Foo.fish

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
Uat {
22
int i
3-
Foo s
43
}
54

65
Ufn {
7-
(fun bar(int i, int a)(int) {
8-
return (i + own.i + a)
9-
})
10-
11-
(fun self(void)(Foo) {
12-
return own.s
6+
(fun bar(int i)(int) {
7+
return (i + own.i)
138
})
149
}

src/main/CommandLine.cpp

+9-56
Original file line numberDiff line numberDiff line change
@@ -133,62 +133,15 @@ handleCommandLine(int argc, char* const* argv)
133133
{
134134
if (std::string("--test").compare(argv[1]) == 0)
135135
{
136-
try
137-
{
138-
std::vector<std::string> expected = {
139-
"Mismatched types. Expected/LeftHand is: int.",
140-
"Mismatched types. Expected/LeftHand is: i.",
141-
"Mismatched types. Expected/LeftHand is: flt.",
142-
"Mismatched types. Expected/LeftHand is: int or flt.",
143-
"Mismatched types. Expected/LeftHand is: int.",
144-
"Mismatched types. Expected/LeftHand is: int or flt.",
145-
"Mismatched types. Expected/LeftHand is: int.",
146-
"Mismatched types. Expected/LeftHand is: int.",
147-
"Mismatched types. Expected/LeftHand is: int.",
148-
"Mismatched types. Expected/LeftHand is: int or flt.",
149-
"Mismatched types. Expected/LeftHand is: int or flt.",
150-
"Mismatched types. Expected/LeftHand is: bool.",
151-
"Mismatched types. Expected/LeftHand is: bool.",
152-
"Mismatched types. Expected/LeftHand is: int or flt.",
153-
"Mismatched types. Expected/LeftHand is: int or flt.",
154-
"Mismatched types. Expected/LeftHand is: bool.",
155-
"Mismatched types. Expected/LeftHand is: int.",
156-
"Unexpected redeclaration of f, originally defined as type "
157-
"flt.",
158-
"Missing keys in udt initialization for type: Foo",
159-
"Nonexistent member function.",
160-
"Mismatched types. Expected/LeftHand is: flt.",
161-
"Mismatched types. Expected/LeftHand is: int.",
162-
"Mismatched types. Expected/LeftHand is: flt.",
163-
"Nonexistent attribute.",
164-
"Mismatched types. Expected/LeftHand is: int.",
165-
"Function input parameter type mismatch in function call "
166-
"soFun",
167-
"Mismatched types. Expected/LeftHand is: flt.",
168-
"Mismatched list types. Expected is: [int].",
169-
"Mismatched types. Expected/LeftHand is: bool.",
170-
};
171-
172-
sailfishc* sfc = new sailfishc(argv[2], false);
173-
sfc->parse();
174-
auto errors = sfc->getErrors();
175-
176-
int i = 0;
177-
for (auto const& e : errors)
178-
{
179-
// std::cout << e->getMsg() << " " << expected[i] <<
180-
// std::endl; assert(e->getMsg() == expected[i++]);
181-
std::cout << e->getMsg() << std::endl;
182-
}
183-
}
184-
catch (const std::string msg)
185-
{
186-
std::cerr << msg;
187-
}
188-
catch (char const* msg)
189-
{
190-
std::cerr << msg;
191-
}
136+
Prettify::Formatter red(Prettify::FG_RED);
137+
Prettify::Formatter green(Prettify::FG_GREEN);
138+
Prettify::Formatter normal(Prettify::RESET);
139+
140+
bool result = SEMANTIC_ANALYSIS_TEST(argv[2]);
141+
if (result)
142+
std::cout << green << "SUCCESSFUL TEST!" << '\n' << normal;
143+
else
144+
std::cout << red << "TEST FAILED!" << '\n' << normal;
192145
}
193146
else if (std::string("--compile_c").compare(argv[1]) == 0)
194147
{

src/main/CommandLine.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "../common/display.h"
99
#include "../lexar/Lexar.h"
1010
#include "../sailfish/sailfishc.h"
11-
#include <assert.h>
11+
#include "../tests/SemanticAnalysisTest.h"
1212
#include <iostream>
1313
#include <stdlib.h>
1414
#include <string>

src/sailfish/sailfishc.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,10 @@ sailfishc::parseSourcePart()
525525
break;
526526
default:
527527
parseScript();
528-
transpiler->write(semanticerrorhandler->getErrors().size() == 0);
528+
529+
// don't write to out.c if we are testing, aka we don' care about errors
530+
if (shouldDisplayErrors)
531+
transpiler->write(semanticerrorhandler->getErrors().size() == 0);
529532
}
530533
}
531534

src/tests/SemanticAnalysisTest.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Robert Durst 2019
3+
* Sailfish Programming Language
4+
*/
5+
#include "SemanticAnalysisTest.h"
6+
7+
bool
8+
SEMANTIC_ANALYSIS_TEST(const std::string& filename)
9+
{
10+
try
11+
{
12+
std::vector<std::string> expected = {
13+
"Mismatched types. Expected/LeftHand is: int.",
14+
"Mismatched types. Expected/LeftHand is: i.",
15+
"Mismatched types. Expected/LeftHand is: flt.",
16+
"Mismatched types. Expected/LeftHand is: int or flt.",
17+
"Mismatched types. Expected/LeftHand is: int.",
18+
"Mismatched types. Expected/LeftHand is: int or flt.",
19+
"Mismatched types. Expected/LeftHand is: int.",
20+
"Mismatched types. Expected/LeftHand is: int.",
21+
"Mismatched types. Expected/LeftHand is: int.",
22+
"Mismatched types. Expected/LeftHand is: int or flt.",
23+
"Mismatched types. Expected/LeftHand is: int or flt.",
24+
"Mismatched types. Expected/LeftHand is: bool.",
25+
"Mismatched types. Expected/LeftHand is: bool.",
26+
"Mismatched types. Expected/LeftHand is: int or flt.",
27+
"Mismatched types. Expected/LeftHand is: int or flt.",
28+
"Mismatched types. Expected/LeftHand is: bool.",
29+
"Mismatched types. Expected/LeftHand is: int.",
30+
"Unexpected redeclaration of f, originally defined as type "
31+
"flt.",
32+
"Missing keys in udt initialization for type: Foo",
33+
"Nonexistent member function.",
34+
"Mismatched types. Expected/LeftHand is: flt.",
35+
"Mismatched types. Expected/LeftHand is: int.",
36+
"Mismatched types. Expected/LeftHand is: flt.",
37+
"Nonexistent attribute.",
38+
"Mismatched types. Expected/LeftHand is: int.",
39+
"Function input parameter type mismatch in function call "
40+
"soFun",
41+
"Mismatched types. Expected/LeftHand is: flt.",
42+
"Mismatched list types. Expected is: [int].",
43+
"Mismatched types. Expected/LeftHand is: bool.",
44+
};
45+
46+
sailfishc* sfc = new sailfishc(filename, false);
47+
sfc->parse();
48+
auto errors = sfc->getErrors();
49+
50+
int i = 0;
51+
for (auto const& e : errors)
52+
assert(e->getMsg() == expected[i++]);
53+
54+
return true;
55+
}
56+
catch (const std::string msg)
57+
{
58+
std::cerr << msg;
59+
return false;
60+
}
61+
catch (char const* msg)
62+
{
63+
std::cerr << msg;
64+
return false;
65+
}
66+
}

src/tests/SemanticAnalysisTest.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Robert Durst 2019
3+
* Sailfish Programming Language
4+
*/
5+
#pragma once
6+
#include "../sailfish/sailfishc.h"
7+
#include <assert.h>
8+
#include <string>
9+
#include <vector>
10+
11+
bool SEMANTIC_ANALYSIS_TEST(const std::string&);

0 commit comments

Comments
 (0)