Skip to content

Commit acd39b3

Browse files
author
robertDurst
committed
Basic Parser2.0 tests
1 parent c4085f7 commit acd39b3

File tree

8 files changed

+306
-73
lines changed

8 files changed

+306
-73
lines changed

examples/test2.fish

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
import foo : "foo.fish"
22
import bar : "bar.fish"
33

4-
(fun some_function([int] i)(void){
5-
i = i + 1
6-
return i
7-
})
4+
(fun foo (int i, flt f)(void){ return i })
85

96
start {
10-
dec str s = "Throw youresel"
11-
dec [int] i = [1,2,3]
12-
f.f = f...foo() + (5 * 11 ** (4 and 6))
13-
z = 7
14-
157
Tree (
16-
(|1 == 2| {
17-
z = 2
18-
})
19-
20-
(|1 != 2 and (!true)| {
21-
z = new Foo { z: 1, f: "hello from the otherside" }
22-
--z
23-
})
8+
(|1 == 2 and (2 < 3)| {})
9+
(|!true| {})
2410
)
11+
12+
dec flt f = 12.0
13+
dec [int] is = [1,2,3]
14+
dec Foo f = new Foo {f: i}
15+
f.f += f...f() + 1
16+
++f.f
2517
}

examples/test_uda.fish

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Uat {
2+
flt f
3+
}
4+
5+
Ufn {
6+
(fun foo(int i)(void){})
7+
}

src/main/CommandLine.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ handleCommandLine(int argc, char* const* argv)
164164

165165
Parser2* p = new Parser2(filename);
166166
auto n = p->parse();
167-
p->postorder(n);
167+
p->postorder(
168+
n, [](std::string s) -> void { std::cout << s << '\n'; });
168169
}
169170
catch (const std::string msg)
170171
{

src/parser/Parser2.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,31 +117,36 @@ class Parser2
117117
Parser2(const std::string& filename);
118118
std::shared_ptr<NodeLexeme> parse();
119119

120+
template <typename F>
120121
void
121-
lexemeIt(Lexeme l, int indent)
122+
lexemeIt(Lexeme l, F f)
122123
{
123124
auto i = l.index();
124125
if (i == 0)
125-
postorder(std::get<std::shared_ptr<NodeLexeme>>(l), indent + 4);
126+
postorder(std::get<std::shared_ptr<NodeLexeme>>(l), f);
126127
else if (i == 1)
127-
postorder(std::get<std::shared_ptr<LeafLexeme>>(l), indent + 4);
128+
postorder(std::get<std::shared_ptr<LeafLexeme>>(l), f);
128129
}
130+
131+
template <typename F>
129132
void
130-
postorder(std::shared_ptr<NodeLexeme> n, int indent = 0)
133+
postorder(std::shared_ptr<NodeLexeme> n, F f)
131134
{
132135
if (n != nullptr && n->op != OP::NULL_VAL)
133136
{
134137
// root
135-
std::cout << std::setw(indent) << disp(n->op) << '\n';
138+
f(disp(n->op));
136139
// left
137-
lexemeIt(n->left, indent);
140+
lexemeIt(n->left, f);
138141
// right
139-
lexemeIt(n->right, indent);
142+
lexemeIt(n->right, f);
140143
}
141144
}
145+
146+
template <typename F>
142147
void
143-
postorder(std::shared_ptr<LeafLexeme> n, int indent = 0)
148+
postorder(std::shared_ptr<LeafLexeme> n, F f)
144149
{
145-
std::cout << std::setw(indent) << n->value << '\n';
150+
f(n->value);
146151
}
147152
};

test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
cmake_minimum_required(VERSION 2.6)
22

3+
set(CMAKE_CXX_STANDARD 17)
4+
35
# Add sailfishc libs. TODO: write this more eloquently
46
ADD_LIBRARY(SailfishcLibs
57
../src/lexar/Lexar.cpp
68
../src/lexar/Lexar2.cpp
79
../src/lexar/Token2.cpp
810
../src/parser/Parser.cpp
11+
../src/parser/Parser2.cpp
12+
../src/parser/Lexeme.cpp
913
visitors/InOrderTraversal.cpp
1014
../src/visitor/Visitor.cpp
1115
../src/semantics/SymbolTable.cpp
1216
../src/semantics/TypeChecker.cpp
1317
../src/errorhandler/SemanticErrorHandler.cpp
18+
../src/errorhandler/Parser2ErrorHandler.cpp
1419
../src/errorhandler/SymbolTableErrorHandler.cpp
1520
../src/errorhandler/ParserErrorHandler.cpp
1621
../src/common/ReservedWords.cpp

test/examples/parser2_script.fish

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import foo : "foo.fish"
2+
import bar : "bar.fish"
3+
4+
(fun foo (int i, flt f)(void){ return i })
5+
6+
start {
7+
Tree (
8+
(|1 == 2 and (2 < 3)| {})
9+
(|!true| {})
10+
)
11+
12+
dec flt f = 12.0
13+
dec [int] is = [1,2,3]
14+
dec Foo f = new Foo {f: i}
15+
f.f += f...f() + 1
16+
++f.f
17+
}

test/examples/parser2_udt.fish

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Uat {
2+
flt f
3+
}
4+
5+
Ufn {
6+
(fun foo(int i)(void){})
7+
}

0 commit comments

Comments
 (0)