-
Notifications
You must be signed in to change notification settings - Fork 4
/
SList.cpp
101 lines (86 loc) · 2.39 KB
/
SList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// SList.cpp
// Scheme++
//
// Created by Josh Sun on 2017-05-10.
// Copyright © 2017 Josh Sun. All rights reserved.
//
#include "SList.hpp"
SList::SList(){};
SList::SList(sType type): type(type) {};
SList::SList(std::string s) : value(s), type (SYMBOL) {}
SList::SList(double s) : value(std::to_string(s)), type(NUMBER) {}
SList::SList(SLists s) : list(s), type(LIST) {}
SList::SList(proc s) : p(s) , type(PROC) {}
bool double_is_int(std::string dVal) {
double trouble = atof(dVal.c_str());
return fabs(trouble) == floor(fabs(trouble));
}
std::string double_to_int(std::string dVal) {
return std::to_string((int)atof(dVal.c_str()));
}
void SList::setType (sType t) {
type = t;
}
void SList::push(SList s) {
list.push_back(s);
}
void SList::pushList(SLists s) {
for (auto vi = s.begin(); vi != s.end(); vi++)
list.push_back(*vi);
}
SList::sType SList::getType() const {
return type;
}
std::string SList::getTypeString() const {
if (type == SYMBOL) return "SYMBOL";
else if (type == NUMBER) return "NUMBER";
else if (type == LIST) return "LIST";
else if (type == PROC) return "PROC";
else return "LAMBDA";
}
SLists SList::getList () const {
return list;
}
SList::proc SList::getProc() const {
return p;
}
std::string SList::val() const {return value;}
std::string SList::listToString() {
if (type == SYMBOL) return value;
if (type == NUMBER) return double_is_int(value) ? double_to_int(value) : value;
std::string strVal = "";
for (auto vi = list.begin(); vi != list.end(); vi++) {
strVal += vi->listToString();
if (vi!=list.end()-1)
strVal += " ";
}
return strVal;
}
//for debugging
std::string SList::getPrintString() const {
std::string s = "";
if (type == NUMBER) {
s+=(double_is_int(value) ? double_to_int(value) : value);
} else if (type == SYMBOL) {
if (value == "#t")
return "true";
else if (value == "#f")
return "false";
else
return value;
} else if (type == PROC) {
return "PROC";
} else if (type == LAMBDA) {
return "LAMBDA";
} else {
s+="(";
for (auto vi = list.cbegin(); vi != list.cend(); vi++) {
s+=vi->getPrintString();
if (vi!=list.cend()-1) s+= " ";
}
if (s.back() == ' ') s.pop_back();
s+=") ";
}
return s;
}