From 61dfcb00c0f3f8b3ec5ad4fec3137dcd1506e45e Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 10 Apr 2016 17:19:48 -0600 Subject: [PATCH] Move int/float into Constant --- .../chaiscript/language/chaiscript_common.hpp | 8 +- .../chaiscript/language/chaiscript_eval.hpp | 76 ++++--------------- .../chaiscript/language/chaiscript_parser.hpp | 49 ++++++++---- 3 files changed, 54 insertions(+), 79 deletions(-) diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index a36bc1a..09e16d2 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -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(ast_node_type)]; } diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index a4ac5fe..61413a7 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -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::infinity()); - } else if (t_text == "NaN") { - return const_var(std::numeric_limits::quiet_NaN()); - } else if (t_text == "_") { - return Boxed_Value(std::make_shared()); - } 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 t_children) : diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 2c61d83..ee745b7 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -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(std::move(match), start.line, start.col, std::move(bv))); + m_match_stack.emplace_back(make_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(std::move(match), start.line, start.col, std::move(bv))); + m_match_stack.push_back(make_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(std::move(match), start.line, start.col, std::move(bv))); + m_match_stack.push_back(make_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(std::move(match), start.line, start.col, std::move(bv))); + m_match_stack.push_back(make_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(std::move(match), start.line, start.col, std::move(bv))); + m_match_stack.push_back(make_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( - [&]()->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(text, start.line, start.col, const_var(true))); + } else if (text == "false") { + m_match_stack.push_back(make_node(text, start.line, start.col, const_var(false))); + } else if (text == "Infinity") { + m_match_stack.push_back(make_node(text, start.line, start.col, + const_var(std::numeric_limits::infinity()))); + } else if (text == "NaN") { + m_match_stack.push_back(make_node(text, start.line, start.col, + const_var(std::numeric_limits::quiet_NaN()))); + } else if (text == "_") { + m_match_stack.push_back(make_node(text, start.line, start.col, + Boxed_Value(std::make_shared()))); + } else { + m_match_stack.push_back(make_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;