lex.c 5.99 KB
Newer Older
Chris Müller's avatar
Chris Müller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Cherry programming language
 * Copyright (C) 2013 Christoph Mueller
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

19
#include "./unittest.h"
Chris Müller's avatar
Chris Müller committed
20
#include <cherry.h>
Chris Müller's avatar
Chris Müller committed
21
22
23
#include <stdio.h>
#include <string.h>

24
static void test_lex_fixnum(cy_const_pointer_t data)
Chris Müller's avatar
Chris Müller committed
25
{
26
    cy_byte_t* fixnum = "0 5 0xFF 0xFFFF 1000 0777 0b0001 0b00011111";
Chris Müller's avatar
Chris Müller committed
27
    struct org_cherry_context* c = org_cherry_context_repl(fixnum);
Chris Müller's avatar
Chris Müller committed
28

Chris Müller's avatar
Chris Müller committed
29
30
	assert(org_cherry_lex(c) == TOK_DEC);
	assert(strcmp(org_cherry_token_string(c), "0") == 0);
Chris Müller's avatar
Chris Müller committed
31

Chris Müller's avatar
Chris Müller committed
32
33
	assert(org_cherry_lex(c) == TOK_DEC);
	assert(strcmp(org_cherry_token_string(c), "5") == 0);
Chris Müller's avatar
Chris Müller committed
34

Chris Müller's avatar
Chris Müller committed
35
36
	assert(org_cherry_lex(c) == TOK_HEX);
	assert(strcmp(org_cherry_token_string(c), "0xFF") == 0);
Chris Müller's avatar
Chris Müller committed
37

Chris Müller's avatar
Chris Müller committed
38
39
	assert(org_cherry_lex(c) == TOK_HEX);
	assert(strcmp(org_cherry_token_string(c), "0xFFFF") == 0);
Chris Müller's avatar
Chris Müller committed
40

Chris Müller's avatar
Chris Müller committed
41
42
	assert(org_cherry_lex(c) == TOK_DEC);
	assert(strcmp(org_cherry_token_string(c), "1000") == 0);
Chris Müller's avatar
Chris Müller committed
43

Chris Müller's avatar
Chris Müller committed
44
45
	assert(org_cherry_lex(c) == TOK_OCT);
	assert(strcmp(org_cherry_token_string(c), "0777") == 0);
Chris Müller's avatar
Chris Müller committed
46

Chris Müller's avatar
Chris Müller committed
47
48
	assert(org_cherry_lex(c) == TOK_BIN);
	assert(strcmp(org_cherry_token_string(c), "0b0001") == 0);
Chris Müller's avatar
Chris Müller committed
49

Chris Müller's avatar
Chris Müller committed
50
51
	assert(org_cherry_lex(c) == TOK_BIN);
	assert(strcmp(org_cherry_token_string(c), "0b00011111") == 0);
Chris Müller's avatar
Chris Müller committed
52
53
54
}


55
static void test_lex_float(cy_const_pointer_t data)
Chris Müller's avatar
Chris Müller committed
56
{
57
    cy_byte_t* fixnum = "1.0 4e+1 5e-10 1.0e20";
Chris Müller's avatar
Chris Müller committed
58
    struct org_cherry_context* c = org_cherry_context_repl(fixnum);
Chris Müller's avatar
Chris Müller committed
59

Chris Müller's avatar
Chris Müller committed
60
61
	assert(org_cherry_lex(c) == TOK_FLOAT);
	assert(strcmp(org_cherry_token_string(c), "1.0") == 0);
Chris Müller's avatar
Chris Müller committed
62
	
Chris Müller's avatar
Chris Müller committed
63
64
	assert(org_cherry_lex(c) == TOK_FLOAT);
	assert(strcmp(org_cherry_token_string(c), "4e+1") == 0);
Chris Müller's avatar
Chris Müller committed
65
	
Chris Müller's avatar
Chris Müller committed
66
67
	assert(org_cherry_lex(c) == TOK_FLOAT);
	assert(strcmp(org_cherry_token_string(c), "5e-10") == 0);
Chris Müller's avatar
Chris Müller committed
68
	
Chris Müller's avatar
Chris Müller committed
69
70
	assert(org_cherry_lex(c) == TOK_FLOAT);
	assert(strcmp(org_cherry_token_string(c), "1.0e20") == 0);
Chris Müller's avatar
Chris Müller committed
71
72
73
}


74
static void test_lex_string(cy_const_pointer_t data)
75
{
76
    cy_byte_t* str = " \"asdf\" \"test\\uFFFF\\UFFFFFFFF\\n\" ";
Chris Müller's avatar
Chris Müller committed
77
	struct org_cherry_context* c = org_cherry_context_repl(str);
78

Chris Müller's avatar
Chris Müller committed
79
80
	assert(org_cherry_lex(c) == TOK_STRING);
	assert(strcmp(org_cherry_token_string(c), "asdf") == 0);
81

Chris Müller's avatar
Chris Müller committed
82
83
	assert(org_cherry_lex(c) == TOK_STRING);
	assert(strcmp(org_cherry_token_string(c), "test\\uFFFF\\UFFFFFFFF\\n") == 0);
84
85
86
}


87
static void test_lex_comment(cy_const_pointer_t data)
88
{
89
	cy_byte_t* comment = "; comment\n";
90
	struct org_cherry_context* c = org_cherry_context(comment, 0, CY_DEFAULT);
91

Chris Müller's avatar
Chris Müller committed
92
93
	assert(org_cherry_lex(c) == TOK_COMMENT);
	assert(strcmp(org_cherry_token_string(c), "; comment") == 0);
94
95
96
}


97
static void test_lex_symbols(cy_const_pointer_t data)
98
{
99
	cy_byte_t* str = " true false null? + - ";
Chris Müller's avatar
Chris Müller committed
100
	struct org_cherry_context* c = org_cherry_context_repl(str);
101

Chris Müller's avatar
Chris Müller committed
102
103
	assert(org_cherry_lex(c) == TOK_TRUE);
	assert(org_cherry_lex(c) == TOK_FALSE);
104
	
Chris Müller's avatar
Chris Müller committed
105
106
	assert(org_cherry_lex(c) == TOK_SYMBOL);
	assert(strcmp(org_cherry_token_string(c), "null?") == 0);
107

Chris Müller's avatar
Chris Müller committed
108
109
	assert(org_cherry_lex(c) == TOK_SYMBOL);
	assert(strcmp(org_cherry_token_string(c), "+") == 0);
110

Chris Müller's avatar
Chris Müller committed
111
112
	assert(org_cherry_lex(c) == TOK_SYMBOL);
	assert(strcmp(org_cherry_token_string(c), "-") == 0);
113
114
115
}


116
static void test_lex_characters(cy_const_pointer_t data)
117
{
118
	cy_byte_t* str = " \\a \\A \\uFFFF \\newline \\space ";
Chris Müller's avatar
Chris Müller committed
119
	struct org_cherry_context* c = org_cherry_context_repl(str);
120

Chris Müller's avatar
Chris Müller committed
121
122
	assert(org_cherry_lex(c) == TOK_CHAR);
	assert(strcmp(org_cherry_token_string(c), "\\a") == 0);
123
	
Chris Müller's avatar
Chris Müller committed
124
125
	assert(org_cherry_lex(c) == TOK_CHAR);
	assert(strcmp(org_cherry_token_string(c), "\\A") == 0);
126
	
Chris Müller's avatar
Chris Müller committed
127
128
	assert(org_cherry_lex(c) == TOK_CHAR);
	assert(strcmp(org_cherry_token_string(c), "\\uFFFF") == 0);
129
	
Chris Müller's avatar
Chris Müller committed
130
131
	assert(org_cherry_lex(c) == TOK_CHAR);
	assert(strcmp(org_cherry_token_string(c), "\\newline") == 0);
132
	
Chris Müller's avatar
Chris Müller committed
133
134
	assert(org_cherry_lex(c) == TOK_CHAR);
	assert(strcmp(org_cherry_token_string(c), "\\space") == 0);
135
136
137
}


138
static void test_lex_core(cy_const_pointer_t data)
139
{
140
	cy_byte_t* str = " [ ] ( ) . ' ";
Chris Müller's avatar
Chris Müller committed
141
	struct org_cherry_context* c = org_cherry_context_repl(str);
142

Chris Müller's avatar
Chris Müller committed
143
144
145
146
147
148
149
	assert(org_cherry_lex(c) == TOK_SQUARELEFTBRACE);
	assert(org_cherry_lex(c) == TOK_SQUARERIGHTBRACE);
	assert(org_cherry_lex(c) == TOK_ROUNDLEFTBRACE);
	assert(org_cherry_lex(c) == TOK_ROUNDRIGHTBRACE);
	assert(org_cherry_lex(c) == TOK_DOT);
	assert(org_cherry_lex(c) == TOK_QUOTE);
	assert(org_cherry_lex(c) == TOK_EOF);
150
151
}

152
static void test_lex_rewind(cy_const_pointer_t data)
153
{
154
	cy_byte_t* str = " [ ] ( ) . ' ";
155
156
157
158
	struct org_cherry_context* c = org_cherry_context_repl(str);

	assert(org_cherry_lex(c) == TOK_SQUARELEFTBRACE);

159
	const cy_byte_t* position = org_cherry_pos(c);
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

	assert(org_cherry_lex(c) == TOK_SQUARERIGHTBRACE);
	assert(org_cherry_lex(c) == TOK_ROUNDLEFTBRACE);
	assert(org_cherry_lex(c) == TOK_ROUNDRIGHTBRACE);

	org_cherry_rewind(c, position);

	assert(org_cherry_lex(c) == TOK_SQUARERIGHTBRACE);
	assert(org_cherry_lex(c) == TOK_ROUNDLEFTBRACE);
	assert(org_cherry_lex(c) == TOK_ROUNDRIGHTBRACE);

	assert(org_cherry_lex(c) == TOK_DOT);
	assert(org_cherry_lex(c) == TOK_QUOTE);
	assert(org_cherry_lex(c) == TOK_EOF);
}


177

Chris Müller's avatar
Chris Müller committed
178
179
void test_suite_lex()
{
180
181
182
183
184
185
186
187
    unittest_run("org.cherry.parser/lex_core", test_lex_core, 0, 100);
    unittest_run("org.cherry.parser/lex_rewind", test_lex_rewind, 0, 100);
    unittest_run("org.cherry.parser/lex_fixnum", test_lex_fixnum, 0, 100);
    unittest_run("org.cherry.parser/lex_float", test_lex_float, 0, 100);
    unittest_run("org.cherry.parser/lex_string", test_lex_string, 0, 100);
    unittest_run("org.cherry.parser/lex_comment", test_lex_comment, 0, 100);
    unittest_run("org.cherry.parser/lex_symbols", test_lex_symbols, 0, 100);
    unittest_run("org.cherry.parser/lex_characters", test_lex_characters, 0, 100);
Chris Müller's avatar
Chris Müller committed
188
189
190
191
}