Move int/float into Constant

This commit is contained in:
Jason Turner
2016-04-10 17:19:48 -06:00
parent 4bf619c80f
commit 61dfcb00c0
3 changed files with 54 additions and 79 deletions

View File

@@ -32,11 +32,11 @@ namespace chaiscript
/// Types of AST nodes available to the parser and eval
enum class AST_Node_Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Arg_List, Variable, Equation, Var_Decl,
enum class AST_Node_Type { Error, Id, Char, Str, Eol, Fun_Call, Arg_List, Variable, Equation, Var_Decl,
Comparison, Addition, Subtraction, Multiplication, Division, Modulus, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String,
Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Continue, Map_Pair, Value_Range,
Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl, Shift, Equality, Bitwise_And, Bitwise_Xor, Bitwise_Or,
Logical_And, Logical_Or, Reference, Switch, Case, Default, Ternary_Cond, Noop, Class, Binary, Arg, Global_Decl
Logical_And, Logical_Or, Reference, Switch, Case, Default, Ternary_Cond, Noop, Class, Binary, Arg, Global_Decl, Constant
};
namespace
@@ -44,11 +44,11 @@ namespace chaiscript
/// Helper lookup to get the name of each node type
const char *ast_node_type_to_string(AST_Node_Type ast_node_type) {
static const char * const ast_node_types[] = { "Internal Parser Error", "Int", "Float", "Id", "Char", "Str", "Eol", "Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl",
static const char * const ast_node_types[] = { "Internal Parser Error", "Id", "Char", "Str", "Eol", "Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl",
"Comparison", "Addition", "Subtraction", "Multiplication", "Division", "Modulus", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String",
"Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Continue", "Map_Pair", "Value_Range",
"Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl", "Shift", "Equality", "Bitwise_And", "Bitwise_Xor", "Bitwise_Or",
"Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Ternary Condition", "Noop", "Class", "Binary", "Arg"};
"Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Ternary Condition", "Noop", "Class", "Binary", "Arg", "Constant"};
return ast_node_types[static_cast<int>(ast_node_type)];
}

View File

@@ -129,72 +129,38 @@ namespace chaiscript
mutable std::atomic_uint_fast32_t m_loc;
};
struct Int_AST_Node final : AST_Node {
Int_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, Boxed_Value t_bv) :
AST_Node(std::move(t_ast_node_text), AST_Node_Type::Int, std::move(t_loc)),
m_value(std::move(t_bv)) { assert(text != ""); }
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &) const override {
return m_value;
}
private:
Boxed_Value m_value;
};
struct Constant_AST_Node final : AST_Node {
Constant_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, Boxed_Value t_value)
: AST_Node(t_ast_node_text, AST_Node_Type::Constant, std::move(t_loc)),
m_value(std::move(t_value))
{
}
struct Float_AST_Node final : AST_Node {
Float_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, Boxed_Value t_bv) :
AST_Node(std::move(t_ast_node_text), AST_Node_Type::Float, std::move(t_loc)),
m_value(std::move(t_bv)) { }
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &) const override {
return m_value;
}
private:
Boxed_Value m_value;
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &) const override {
return m_value;
}
Boxed_Value m_value;
};
struct Id_AST_Node final : AST_Node {
Id_AST_Node(const std::string &t_ast_node_text, Parse_Location t_loc) :
AST_Node(t_ast_node_text, AST_Node_Type::Id, std::move(t_loc)),
m_value(get_value(t_ast_node_text)), m_loc(0)
m_loc(0)
{ }
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
if (!m_value.is_undef())
{
return m_value;
} else {
try {
return t_ss.get_object(this->text, m_loc);
}
catch (std::exception &) {
throw exception::eval_error("Can not find object: " + this->text);
}
try {
return t_ss.get_object(this->text, m_loc);
}
catch (std::exception &) {
throw exception::eval_error("Can not find object: " + this->text);
}
}
private:
static Boxed_Value get_value(const std::string &t_text)
{
if (t_text == "true") {
return const_var(true);
} else if (t_text == "false") {
return const_var(false);
} else if (t_text == "Infinity") {
return const_var(std::numeric_limits<double>::infinity());
} else if (t_text == "NaN") {
return const_var(std::numeric_limits<double>::quiet_NaN());
} else if (t_text == "_") {
return Boxed_Value(std::make_shared<dispatch::Placeholder_Object>());
} else {
return Boxed_Value();
}
}
Boxed_Value m_value;
mutable std::atomic_uint_fast32_t m_loc;
};
@@ -209,16 +175,6 @@ namespace chaiscript
AST_Node(std::move(t_ast_node_text), AST_Node_Type::Str, std::move(t_loc)) { }
};
struct Eol_AST_Node final : AST_Node {
Eol_AST_Node(std::string t_ast_node_text, Parse_Location t_loc) :
AST_Node(std::move(t_ast_node_text), AST_Node_Type::Eol, std::move(t_loc)) { }
std::string pretty_print() const override
{
return "\n";
}
};
struct Fun_Call_AST_Node final : AST_Node {
Fun_Call_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_NodePtr> t_children) :

View File

@@ -741,20 +741,20 @@ namespace chaiscript
if (Hex_()) {
auto match = Position::str(start, m_position);
auto bv = buildInt(16, match, true);
m_match_stack.emplace_back(make_node<eval::Int_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
m_match_stack.emplace_back(make_node<eval::Constant_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
return true;
}
if (Binary_()) {
auto match = Position::str(start, m_position);
auto bv = buildInt(2, match, true);
m_match_stack.push_back(make_node<eval::Int_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
m_match_stack.push_back(make_node<eval::Constant_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
return true;
}
if (Float_()) {
auto match = Position::str(start, m_position);
auto bv = buildFloat(match);
m_match_stack.push_back(make_node<eval::Float_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
m_match_stack.push_back(make_node<eval::Constant_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
return true;
}
else {
@@ -762,11 +762,11 @@ namespace chaiscript
auto match = Position::str(start, m_position);
if (!match.empty() && (match[0] == '0')) {
auto bv = buildInt(8, match, false);
m_match_stack.push_back(make_node<eval::Int_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
m_match_stack.push_back(make_node<eval::Constant_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
}
else if (!match.empty()) {
auto bv = buildInt(10, match, false);
m_match_stack.push_back(make_node<eval::Int_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
m_match_stack.push_back(make_node<eval::Constant_AST_Node>(std::move(match), start.line, start.col, std::move(bv)));
} else {
return false;
}
@@ -824,16 +824,35 @@ namespace chaiscript
const auto start = m_position;
if (Id_()) {
m_match_stack.push_back(make_node<eval::Id_AST_Node>(
[&]()->std::string{
if (*start == '`') {
//Id Literal
return Position::str(start+1, m_position-1);
} else {
return Position::str(start, m_position);
}
}(),
start.line, start.col));
const auto text = Position::str(start, m_position);
if (text == "true") {
m_match_stack.push_back(make_node<eval::Constant_AST_Node>(text, start.line, start.col, const_var(true)));
} else if (text == "false") {
m_match_stack.push_back(make_node<eval::Constant_AST_Node>(text, start.line, start.col, const_var(false)));
} else if (text == "Infinity") {
m_match_stack.push_back(make_node<eval::Constant_AST_Node>(text, start.line, start.col,
const_var(std::numeric_limits<double>::infinity())));
} else if (text == "NaN") {
m_match_stack.push_back(make_node<eval::Constant_AST_Node>(text, start.line, start.col,
const_var(std::numeric_limits<double>::quiet_NaN())));
} else if (text == "_") {
m_match_stack.push_back(make_node<eval::Constant_AST_Node>(text, start.line, start.col,
Boxed_Value(std::make_shared<dispatch::Placeholder_Object>())));
} else {
m_match_stack.push_back(make_node<eval::Id_AST_Node>(
[&]()->std::string{
if (*start == '`') {
// 'escaped' literal, like an operator name
return Position::str(start+1, m_position-1);
} else {
return text;
}
}(),
start.line, start.col));
}
return true;
} else {
return false;