PageRenderTime 24ms CodeModel.GetById 20ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/Lexer.cpp

https://bitbucket.org/hardtack/resoup
C++ | 65 lines | 62 code | 2 blank | 1 comment | 2 complexity | cfa8584e5c7704fb72291a4faeaa28aa MD5 | raw file
 1#include "Lexer.h"
 2#include "LispObject.h"
 3#include <string.h>
 4#include "Functions.h"
 5
 6using namespace Functions;
 7
 8CHARQUEUE* Lexer::lexing (const char *str) throw(StringNotClosed &){
 9	int len = strlen(str);
10	CHARQUEUE* qu = new CHARQUEUE();
11	for(int idx = 0; idx < len; idx++){
12		char c = str[idx];
13		switch(c){
14			case '(' :
15				qu->push(stringcpy("("));
16				break;
17			case ')':
18				qu->push(stringcpy(")"));
19				break;
20			case '\'':
21				qu->push(stringcpy("'"));
22				break;
23			case '\n':
24			case ' ':
25			case '\t':
26				break;
27				/*String*/
28			case '\"':{
29				char *string  = new char[2];
30				string[0] = c;
31				string[1] = '\0';
32				do{
33					char *tmp = string;
34					int len = strlen(tmp)+2;
35					string = new char[len];
36					strcpy(string,tmp);
37					string[strlen(tmp)] = str[++idx];
38					string[strlen(tmp) + 1] = '\0';
39					delete[] tmp;
40				} while(str[idx] != '"');
41				
42				qu->push(string);
43				break;
44			}
45			default:{
46				char *string  = new char[2];
47				string[0] = c;
48				string[1] = '\0';
49				while(str[idx + 1] != ' ' && str[idx + 1] != ')' && str[idx + 1] != '('
50					  && str[idx + 1] != '\"' && str[idx + 1] != '\0' && str[idx + 1] != '\n'){
51					char *tmp = string;
52					int len = strlen(tmp)+2;
53					string = new char[len];
54					strcpy(string,tmp);
55					string[strlen(tmp)] = str[++idx];
56					string[strlen(tmp) + 1] = '\0';
57					delete[] tmp;
58				}
59				qu->push(string);
60				break;
61			}
62		}
63	}
64	return qu;
65}