Added simple const value caching. Fix CMake to default back to debug mode

This commit is contained in:
Jonathan Turner 2009-09-07 15:48:32 +00:00
parent 4713325877
commit 7e3127549f
3 changed files with 58 additions and 22 deletions

View File

@ -2,13 +2,13 @@ cmake_minimum_required(VERSION 2.6)
project(chaiscript) project(chaiscript)
#SET (CMAKE_BUILD_TYPE gdb) SET (CMAKE_BUILD_TYPE gdb)
#SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb") SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb")
#SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb") SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb")
SET (CMAKE_BUILD_TYPE rel) #SET (CMAKE_BUILD_TYPE rel)
SET (CMAKE_C_FLAGS_REL " -Wall -O3") #SET (CMAKE_C_FLAGS_REL " -Wall -O3")
SET (CMAKE_CXX_FLAGS_REL " -Wall -O3") #SET (CMAKE_CXX_FLAGS_REL " -Wall -O3")
include_directories(include) include_directories(include)

View File

@ -68,14 +68,17 @@ namespace chaiscript
int identifier; int identifier;
const char *filename; const char *filename;
File_Position start, end; File_Position start, end;
bool is_cached;
Boxed_Value cached_value;
std::vector<TokenPtr> children; std::vector<TokenPtr> children;
TokenPtr annotation; TokenPtr annotation;
Token(const std::string &token_text, int id, const char *fname) : text(token_text), identifier(id), filename(fname) { } Token(const std::string &token_text, int id, const char *fname) :
text(token_text), identifier(id), filename(fname), is_cached(false) { }
Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) : Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
text(token_text), identifier(id), filename(fname) { text(token_text), identifier(id), filename(fname), is_cached(false) {
start.line = start_line; start.line = start_line;
start.column = start_col; start.column = start_col;

View File

@ -48,6 +48,11 @@ namespace chaiscript
return retval; return retval;
} }
template <typename Eval_System>
void cache_const(Eval_System &ss, const TokenPtr &node, Boxed_Value value) {
node->cached_value = value;
node->is_cached = true;
}
/** /**
* Evaluates a variable or function name identifier * Evaluates a variable or function name identifier
*/ */
@ -55,12 +60,18 @@ namespace chaiscript
Boxed_Value eval_id(Eval_System &ss, const TokenPtr &node) { Boxed_Value eval_id(Eval_System &ss, const TokenPtr &node) {
if (node->text == "true") { if (node->text == "true") {
//return Boxed_Value(true); //return const_var(true);
return const_var(true); if (!node->is_cached) {
cache_const(ss, node, const_var(true));
}
return node->cached_value;
} }
else if (node->text == "false") { else if (node->text == "false") {
//return Boxed_Value(false); //return const_var(false);
return const_var(false); if (!node->is_cached) {
cache_const(ss, node, const_var(true));
}
return node->cached_value;
} }
else { else {
try { try {
@ -76,34 +87,47 @@ namespace chaiscript
* Evaluates a floating point number * Evaluates a floating point number
*/ */
template <typename Eval_System> template <typename Eval_System>
Boxed_Value eval_float(Eval_System &, const TokenPtr &node) { Boxed_Value eval_float(Eval_System &ss, const TokenPtr &node) {
//return Boxed_Value(double(atof(node->text.c_str()))); //return const_var(double(atof(node->text.c_str())));
return const_var(double(atof(node->text.c_str())));
if (!node->is_cached) {
cache_const(ss, node, const_var(double(atof(node->text.c_str()))));
}
return node->cached_value;
} }
/** /**
* Evaluates an integer * Evaluates an integer
*/ */
template <typename Eval_System> template <typename Eval_System>
Boxed_Value eval_int(Eval_System &, const TokenPtr &node) { Boxed_Value eval_int(Eval_System &ss, const TokenPtr &node) {
//return Boxed_Value(atoi(node->text.c_str())); //return const_var(atoi(node->text.c_str()));
return const_var(int(atoi(node->text.c_str())));
if (!node->is_cached) {
cache_const(ss, node, const_var(int(atoi(node->text.c_str()))));
}
return node->cached_value;
} }
/** /**
* Evaluates a quoted string * Evaluates a quoted string
*/ */
template <typename Eval_System> template <typename Eval_System>
Boxed_Value eval_quoted_string(Eval_System &, const TokenPtr &node) { Boxed_Value eval_quoted_string(Eval_System &ss, const TokenPtr &node) {
//return Boxed_Value(node->text); //return const_var(node->text);
return const_var(node->text);
if (!node->is_cached) {
cache_const(ss, node, const_var(node->text));
}
return node->cached_value;
} }
/** /**
* Evaluates a char group * Evaluates a char group
*/ */
template <typename Eval_System> template <typename Eval_System>
Boxed_Value eval_single_quoted_string(Eval_System &, const TokenPtr &node) { Boxed_Value eval_single_quoted_string(Eval_System &ss, const TokenPtr &node) {
/*
if (node->text.size() == 1) { if (node->text.size() == 1) {
//return Boxed_Value(char(node->text[0])); //return Boxed_Value(char(node->text[0]));
return const_var(char(node->text[0])); return const_var(char(node->text[0]));
@ -112,6 +136,15 @@ namespace chaiscript
//return Boxed_Value(char((int)node->text[0] * 0xff + (int)node->text[0])); //return Boxed_Value(char((int)node->text[0] * 0xff + (int)node->text[0]));
return const_var(char((int)node->text[0] * 0xff + (int)node->text[0])); return const_var(char((int)node->text[0] * 0xff + (int)node->text[0]));
} }
*/
if (!node->is_cached) {
cache_const(ss, node,
node->text.size() == 1 ?
const_var(char(node->text[0])) :
const_var(char((int)node->text[0] * 0xff + (int)node->text[0])));
}
return node->cached_value;
} }
/** /**