From 7e3127549f00e9ffa91c13ca9b8403264e3e2cb3 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 7 Sep 2009 15:48:32 +0000 Subject: [PATCH] Added simple const value caching. Fix CMake to default back to debug mode --- CMakeLists.txt | 12 ++-- include/chaiscript/chaiscript.hpp | 7 ++- .../chaiscript/language/chaiscript_eval.hpp | 61 ++++++++++++++----- 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c0ecd25..22fdb19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,13 +2,13 @@ cmake_minimum_required(VERSION 2.6) project(chaiscript) -#SET (CMAKE_BUILD_TYPE gdb) -#SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb") -#SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb") +SET (CMAKE_BUILD_TYPE gdb) +SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb") +SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb") -SET (CMAKE_BUILD_TYPE rel) -SET (CMAKE_C_FLAGS_REL " -Wall -O3") -SET (CMAKE_CXX_FLAGS_REL " -Wall -O3") +#SET (CMAKE_BUILD_TYPE rel) +#SET (CMAKE_C_FLAGS_REL " -Wall -O3") +#SET (CMAKE_CXX_FLAGS_REL " -Wall -O3") include_directories(include) diff --git a/include/chaiscript/chaiscript.hpp b/include/chaiscript/chaiscript.hpp index 467042b..5c3d838 100644 --- a/include/chaiscript/chaiscript.hpp +++ b/include/chaiscript/chaiscript.hpp @@ -68,14 +68,17 @@ namespace chaiscript int identifier; const char *filename; File_Position start, end; + bool is_cached; + Boxed_Value cached_value; std::vector children; 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) : - text(token_text), identifier(id), filename(fname) { + text(token_text), identifier(id), filename(fname), is_cached(false) { start.line = start_line; start.column = start_col; diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 40f01c1..35ef711 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -48,6 +48,11 @@ namespace chaiscript return retval; } + template + 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 */ @@ -55,12 +60,18 @@ namespace chaiscript Boxed_Value eval_id(Eval_System &ss, const TokenPtr &node) { 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") { - //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 { try { @@ -76,34 +87,47 @@ namespace chaiscript * Evaluates a floating point number */ template - Boxed_Value eval_float(Eval_System &, const TokenPtr &node) { - //return Boxed_Value(double(atof(node->text.c_str()))); - return const_var(double(atof(node->text.c_str()))); + Boxed_Value eval_float(Eval_System &ss, const TokenPtr &node) { + //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 */ template - Boxed_Value eval_int(Eval_System &, const TokenPtr &node) { - //return Boxed_Value(atoi(node->text.c_str())); - return const_var(int(atoi(node->text.c_str()))); + Boxed_Value eval_int(Eval_System &ss, const TokenPtr &node) { + //return const_var(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 */ template - Boxed_Value eval_quoted_string(Eval_System &, const TokenPtr &node) { - //return Boxed_Value(node->text); - return const_var(node->text); + Boxed_Value eval_quoted_string(Eval_System &ss, const TokenPtr &node) { + //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 */ template - 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) { //return Boxed_Value(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 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; } /**