From 9827345213328dff034515565c39953e2ad5d7f5 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Wed, 14 Oct 2009 13:51:35 +0000 Subject: [PATCH] Fix Id Literals so that they are keyed off an Id search. This allows us to add operator overloading on the parse side. --- .../chaiscript/language/chaiscript_parser.hpp | 91 ++++++++----------- unittests/operator_overload.chai | 8 ++ unittests/operator_overload.txt | 1 + 3 files changed, 48 insertions(+), 52 deletions(-) create mode 100644 unittests/operator_overload.chai create mode 100644 unittests/operator_overload.txt diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 25d73f7..49ba2ee 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -278,7 +278,32 @@ namespace chaiscript ++col; } } + else if ((input_pos != input_end) && (*input_pos == '`')) { + retval = true; + ++col; + ++input_pos; + std::string::iterator start = input_pos; + + while ((input_pos != input_end) && (*input_pos != '`')) { + if (Eol()) { + throw Eval_Error("Carriage return in identifier literal", File_Position(line, col), filename); + } + else { + ++input_pos; + ++col; + } + } + if (start == input_pos) { + throw Eval_Error("Missing contents of identifier literal", File_Position(line, col), filename); + } + else if (input_pos == input_end) { + throw Eval_Error("Incomplete identifier literal", File_Position(line, col), filename); + } + + ++col; + ++input_pos; + } return retval; } @@ -296,10 +321,19 @@ namespace chaiscript int prev_col = col; int prev_line = line; if (Id_()) { - std::string match(start, input_pos); - TokenPtr t(new Token(match, Token_Type::Id, filename, prev_line, prev_col, line, col)); - match_stack.push_back(t); - return true; + if (*start == '`') { + //Id Literal + std::string match(start+1, input_pos-1); + TokenPtr t(new Token(match, Token_Type::Id, filename, prev_line, prev_col, line, col)); + match_stack.push_back(t); + return true; + } + else { + std::string match(start, input_pos); + TokenPtr t(new Token(match, Token_Type::Id, filename, prev_line, prev_col, line, col)); + match_stack.push_back(t); + return true; + } } else { return false; @@ -1263,7 +1297,7 @@ namespace chaiscript std::string::iterator prev_pos = input_pos; unsigned int prev_stack_top = match_stack.size(); - if (Id(true) || Id_Literal()) { + if (Id(true)) { retval = true; bool has_more = true; @@ -1383,53 +1417,6 @@ namespace chaiscript return retval; } - /** - * Reads an identifier literal of the special form `` from input - */ - bool Id_Literal() { - bool retval = false; - - SkipWS(); - - if ((input_pos != input_end) && (*input_pos == '`')) { - retval = true; - - int prev_col = col; - int prev_line = line; - - ++col; - ++input_pos; - - std::string::iterator start = input_pos; - - while ((input_pos != input_end) && (*input_pos != '`')) { - if (Eol()) { - throw Eval_Error("Carriage return in identifier literal", File_Position(line, col), filename); - } - else { - ++input_pos; - ++col; - } - } - - if (start == input_pos) { - throw Eval_Error("Missing contents of identifier literal", File_Position(line, col), filename); - } - else if (input_pos == input_end) { - throw Eval_Error("Incomplete identifier literal", File_Position(line, col), filename); - } - - ++col; - std::string match(start, input_pos); - TokenPtr t(new Token(match, Token_Type::Id, filename, prev_line, prev_col, line, col)); - match_stack.push_back(t); - ++input_pos; - - } - - return retval; - } - /** * Reads a unary prefixed expression from input */ diff --git a/unittests/operator_overload.chai b/unittests/operator_overload.chai new file mode 100644 index 0000000..1f3f8cb --- /dev/null +++ b/unittests/operator_overload.chai @@ -0,0 +1,8 @@ +def Bob::`+`(y) { this.x + y.x } +def Bob::Bob() { } +attr Bob::x +var b = Bob() +var c = Bob() +b.x = 4 +c.x = 5 +print(b+c) diff --git a/unittests/operator_overload.txt b/unittests/operator_overload.txt new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/unittests/operator_overload.txt @@ -0,0 +1 @@ +9