Fix Id Literals so that they are keyed off an Id search. This allows us to add operator overloading on the parse side.
This commit is contained in:
@@ -278,7 +278,32 @@ namespace chaiscript
|
|||||||
++col;
|
++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;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,10 +321,19 @@ namespace chaiscript
|
|||||||
int prev_col = col;
|
int prev_col = col;
|
||||||
int prev_line = line;
|
int prev_line = line;
|
||||||
if (Id_()) {
|
if (Id_()) {
|
||||||
std::string match(start, input_pos);
|
if (*start == '`') {
|
||||||
TokenPtr t(new Token(match, Token_Type::Id, filename, prev_line, prev_col, line, col));
|
//Id Literal
|
||||||
match_stack.push_back(t);
|
std::string match(start+1, input_pos-1);
|
||||||
return true;
|
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 {
|
else {
|
||||||
return false;
|
return false;
|
||||||
@@ -1263,7 +1297,7 @@ namespace chaiscript
|
|||||||
std::string::iterator prev_pos = input_pos;
|
std::string::iterator prev_pos = input_pos;
|
||||||
|
|
||||||
unsigned int prev_stack_top = match_stack.size();
|
unsigned int prev_stack_top = match_stack.size();
|
||||||
if (Id(true) || Id_Literal()) {
|
if (Id(true)) {
|
||||||
retval = true;
|
retval = true;
|
||||||
bool has_more = true;
|
bool has_more = true;
|
||||||
|
|
||||||
@@ -1383,53 +1417,6 @@ namespace chaiscript
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads an identifier literal of the special form `<name>` 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
|
* Reads a unary prefixed expression from input
|
||||||
*/
|
*/
|
||||||
|
8
unittests/operator_overload.chai
Normal file
8
unittests/operator_overload.chai
Normal file
@@ -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)
|
1
unittests/operator_overload.txt
Normal file
1
unittests/operator_overload.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
9
|
Reference in New Issue
Block a user