Move int/float into Constant
This commit is contained in:
@@ -32,11 +32,11 @@ namespace chaiscript
|
|||||||
|
|
||||||
|
|
||||||
/// Types of AST nodes available to the parser and eval
|
/// 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,
|
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,
|
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,
|
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
|
namespace
|
||||||
@@ -44,11 +44,11 @@ namespace chaiscript
|
|||||||
|
|
||||||
/// Helper lookup to get the name of each node type
|
/// Helper lookup to get the name of each node type
|
||||||
const char *ast_node_type_to_string(AST_Node_Type ast_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",
|
"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",
|
"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",
|
"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)];
|
return ast_node_types[static_cast<int>(ast_node_type)];
|
||||||
}
|
}
|
||||||
|
@@ -129,72 +129,38 @@ namespace chaiscript
|
|||||||
mutable std::atomic_uint_fast32_t m_loc;
|
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:
|
struct Constant_AST_Node final : AST_Node {
|
||||||
Boxed_Value m_value;
|
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 {
|
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &) const override {
|
||||||
Float_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, Boxed_Value t_bv) :
|
return m_value;
|
||||||
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 m_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Id_AST_Node final : AST_Node {
|
struct Id_AST_Node final : AST_Node {
|
||||||
Id_AST_Node(const std::string &t_ast_node_text, Parse_Location t_loc) :
|
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)),
|
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 {
|
Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override {
|
||||||
if (!m_value.is_undef())
|
try {
|
||||||
{
|
return t_ss.get_object(this->text, m_loc);
|
||||||
return m_value;
|
}
|
||||||
} else {
|
catch (std::exception &) {
|
||||||
try {
|
throw exception::eval_error("Can not find object: " + this->text);
|
||||||
return t_ss.get_object(this->text, m_loc);
|
|
||||||
}
|
|
||||||
catch (std::exception &) {
|
|
||||||
throw exception::eval_error("Can not find object: " + this->text);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
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;
|
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)) { }
|
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 {
|
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) :
|
Fun_Call_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector<AST_NodePtr> t_children) :
|
||||||
|
@@ -741,20 +741,20 @@ namespace chaiscript
|
|||||||
if (Hex_()) {
|
if (Hex_()) {
|
||||||
auto match = Position::str(start, m_position);
|
auto match = Position::str(start, m_position);
|
||||||
auto bv = buildInt(16, match, true);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Binary_()) {
|
if (Binary_()) {
|
||||||
auto match = Position::str(start, m_position);
|
auto match = Position::str(start, m_position);
|
||||||
auto bv = buildInt(2, match, true);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
if (Float_()) {
|
if (Float_()) {
|
||||||
auto match = Position::str(start, m_position);
|
auto match = Position::str(start, m_position);
|
||||||
auto bv = buildFloat(match);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -762,11 +762,11 @@ namespace chaiscript
|
|||||||
auto match = Position::str(start, m_position);
|
auto match = Position::str(start, m_position);
|
||||||
if (!match.empty() && (match[0] == '0')) {
|
if (!match.empty() && (match[0] == '0')) {
|
||||||
auto bv = buildInt(8, match, false);
|
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()) {
|
else if (!match.empty()) {
|
||||||
auto bv = buildInt(10, match, false);
|
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 {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -824,16 +824,35 @@ namespace chaiscript
|
|||||||
|
|
||||||
const auto start = m_position;
|
const auto start = m_position;
|
||||||
if (Id_()) {
|
if (Id_()) {
|
||||||
m_match_stack.push_back(make_node<eval::Id_AST_Node>(
|
|
||||||
[&]()->std::string{
|
const auto text = Position::str(start, m_position);
|
||||||
if (*start == '`') {
|
if (text == "true") {
|
||||||
//Id Literal
|
m_match_stack.push_back(make_node<eval::Constant_AST_Node>(text, start.line, start.col, const_var(true)));
|
||||||
return Position::str(start+1, m_position-1);
|
} else if (text == "false") {
|
||||||
} else {
|
m_match_stack.push_back(make_node<eval::Constant_AST_Node>(text, start.line, start.col, const_var(false)));
|
||||||
return Position::str(start, m_position);
|
} 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())));
|
||||||
start.line, start.col));
|
} 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;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
Reference in New Issue
Block a user