Fix parser to handle shortform equations (fix bad operator parsing). Add back lines to prelude for line numbers

This commit is contained in:
Jonathan Turner 2009-09-09 14:11:03 +00:00
parent daee00da95
commit f23f0edc70
4 changed files with 258 additions and 251 deletions

View File

@ -591,7 +591,7 @@ namespace chaiscript
/** /**
* Reads (and potentially captures) a symbol group from input if it matches the parameter * Reads (and potentially captures) a symbol group from input if it matches the parameter
*/ */
bool Symbol(const char *s, bool capture = false) { bool Symbol(const char *s, bool capture = false, bool disallow_prevention=false) {
SkipWS(); SkipWS();
if (!capture) { if (!capture) {
@ -601,7 +601,7 @@ namespace chaiscript
bool retval = Symbol_(s); bool retval = Symbol_(s);
if (retval) { if (retval) {
//todo: fix this. Hacky workaround for preventing substring matches //todo: fix this. Hacky workaround for preventing substring matches
if ((input_pos != input_end) && ((*input_pos == '+') || (*input_pos == '-') || (*input_pos == '*') || (*input_pos == '/') || (*input_pos == '=') || (*input_pos == '.'))) { if ((input_pos != input_end) && (disallow_prevention == false) && ((*input_pos == '+') || (*input_pos == '-') || (*input_pos == '*') || (*input_pos == '/') || (*input_pos == '=') || (*input_pos == '.'))) {
input_pos = start; input_pos = start;
col = prev_col; col = prev_col;
line = prev_line; line = prev_line;
@ -619,7 +619,7 @@ namespace chaiscript
int prev_line = line; int prev_line = line;
if (Symbol_(s)) { if (Symbol_(s)) {
//todo: fix this. Hacky workaround for preventing substring matches //todo: fix this. Hacky workaround for preventing substring matches
if ((input_pos != input_end) && ((*input_pos == '+') || (*input_pos == '-') || (*input_pos == '*') || (*input_pos == '/') || (*input_pos == '=') || (*input_pos == '.'))) { if ((input_pos != input_end) && (disallow_prevention == false) && ((*input_pos == '+') || (*input_pos == '-') || (*input_pos == '*') || (*input_pos == '/') || (*input_pos == '=') || (*input_pos == '.'))) {
input_pos = start; input_pos = start;
col = prev_col; col = prev_col;
line = prev_line; line = prev_line;
@ -1185,24 +1185,6 @@ namespace chaiscript
int prev_stack_top = match_stack.size(); int prev_stack_top = match_stack.size();
if (Symbol("-")) {
retval = true;
if (!Dot_Access()) {
throw Eval_Error("Incomplete negation expression", File_Position(line, col), filename);
}
build_match(Token_Type::Negate, prev_stack_top);
}
else if (Symbol("!")) {
retval = true;
if (!Dot_Access()) {
throw Eval_Error("Incomplete '!' expression", File_Position(line, col), filename);
}
build_match(Token_Type::Not, prev_stack_top);
}
if (Symbol("++", true)) { if (Symbol("++", true)) {
retval = true; retval = true;
@ -1221,6 +1203,24 @@ namespace chaiscript
build_match(Token_Type::Prefix, prev_stack_top); build_match(Token_Type::Prefix, prev_stack_top);
} }
else if (Char('-')) {
retval = true;
if (!Dot_Access()) {
throw Eval_Error("Incomplete negation expression", File_Position(line, col), filename);
}
build_match(Token_Type::Negate, prev_stack_top);
}
else if (Char('!')) {
retval = true;
if (!Dot_Access()) {
throw Eval_Error("Incomplete '!' expression", File_Position(line, col), filename);
}
build_match(Token_Type::Not, prev_stack_top);
}
return retval; return retval;
} }
@ -1423,7 +1423,8 @@ namespace chaiscript
if (Expression()) { if (Expression()) {
retval = true; retval = true;
if (Symbol("=", true) || Symbol(":=", true) || Symbol("+=", true) || Symbol("-=", true) || Symbol("*=", true) || Symbol("/=", true)) { if (Symbol("=", true, true) || Symbol(":=", true, true) || Symbol("+=", true, true) ||
Symbol("-=", true, true) || Symbol("*=", true, true) || Symbol("/=", true, true)) {
if (!Equation()) { if (!Equation()) {
throw Eval_Error("Incomplete equation", match_stack.back()); throw Eval_Error("Incomplete equation", match_stack.back());
} }

View File

@ -12,36 +12,36 @@
#define CODE_STRING(x, y, z) #x ", " #y ", " #z #define CODE_STRING(x, y, z) #x ", " #y ", " #z
#define chaiscript_prelude CODE_STRING(\ #define chaiscript_prelude CODE_STRING(\
def new(x) { eval(type_name(x))(); } \ def new(x) { eval(type_name(x))(); } \n\
def clone(x) : function_exists(type_name(x)) { eval(type_name(x))(x); } \ def clone(x) : function_exists(type_name(x)) { eval(type_name(x))(x); } \n\
# to_string for Pair()\n\ # to_string for Pair()\n\
def to_string(x) : call_exists(first, x) && call_exists(second, x) { \ def to_string(x) : call_exists(first, x) && call_exists(second, x) { \n\
"<" + x.first.to_string() + ", " + x.second.to_string() + ">"; \ "<" + x.first.to_string() + ", " + x.second.to_string() + ">"; \n\
}\ }\n\
# to_string for containers\n\ # to_string for containers\n\
def to_string(x) : call_exists(range, x) && !x.is_type("string"){ \ def to_string(x) : call_exists(range, x) && !x.is_type("string"){ \n\
"[" + x.join(", ") + "]"; \ "[" + x.join(", ") + "]"; \n\
}\ }\n\
# Basic to_string function\n\ # Basic to_string function\n\
def to_string(x) { \ def to_string(x) { \n\
internal_to_string(x); \ internal_to_string(x); \n\
}\ }\n\
# Prints to console with no carriage return\n\ # Prints to console with no carriage return\n\
def puts(x) { \ def puts(x) { \n\
print_string(x.to_string()); \ print_string(x.to_string()); \n\
} \ } \n\
# Prints to console with carriage return\n\ # Prints to console with carriage return\n\
def print(x) { \ def print(x) { \n\
println_string(x.to_string()); \ println_string(x.to_string()); \n\
} \ } \n\
# Returns the maximum value of two numbers\n\ # Returns the maximum value of two numbers\n\
def max(a, b) { if (a>b) { a } else { b } } \ def max(a, b) { if (a>b) { a } else { b } } \n\
# Returns the minimum value of two numbers\n\ # Returns the minimum value of two numbers\n\
def min(a, b) { if (a<b) { a } else { b } } \ def min(a, b) { if (a<b) { a } else { b } } \n\
# Returns true if the value is odd\n\ # Returns true if the value is odd\n\
def odd(x) { if (x % 2 == 1) { true } else { false } } \ def odd(x) { if (x % 2 == 1) { true } else { false } } \n\
# Returns true if the value is even\n\ # Returns true if the value is even\n\
def even(x) { if (x % 2 == 0) { true } else { false } } \ def even(x) { if (x % 2 == 0) { true } else { false } } \n\
# Pushes the second value onto the container first value while making a clone of the value\n\ # Pushes the second value onto the container first value while making a clone of the value\n\
def push_back(container, x) : call_exists(push_back_ref, container, x) { container.push_back_ref(clone(x)) } \n\ def push_back(container, x) : call_exists(push_back_ref, container, x) { container.push_back_ref(clone(x)) } \n\
# Pushes the second value onto the front of the container first value while making a clone of the value\n\ # Pushes the second value onto the front of the container first value while making a clone of the value\n\
@ -50,14 +50,14 @@ def push_front(container, x) : call_exists(push_front_ref, container, x) { conta
# while making a clone. \n\ # while making a clone. \n\
def insert_at(container, pos, x) { container.insert_ref_at(pos, clone(x)); } \n\ def insert_at(container, pos, x) { container.insert_ref_at(pos, clone(x)); } \n\
# Returns the reverse of the given container\n\ # Returns the reverse of the given container\n\
def reverse(container) {\ def reverse(container) {\n\
var retval = new(container); \ var retval = new(container); \n\
var r = range(container); \ var r = range(container); \n\
while (!r.empty()) { \ while (!r.empty()) { \n\
retval.push_back(r.back()); \ retval.push_back(r.back()); \n\
r.pop_back(); \ r.pop_back(); \n\
} \ } \n\
retval; \ retval; \n\
} \ } \
# Returns true if the object is a retro object\n\ # Returns true if the object is a retro object\n\
def is_retro(r) { call_exists(count, r, "data_type") && r.count("data_type") == 1 && r["data_type"] == "retro" }\n\ def is_retro(r) { call_exists(count, r, "data_type") && r.count("data_type") == 1 && r["data_type"] == "retro" }\n\
@ -74,228 +74,228 @@ def pop_back(r) : is_retro(r) { pop_front(r["data"]) }\n\
# Moves the front iterator of a retro towards the back by one \n\ # Moves the front iterator of a retro towards the back by one \n\
def pop_front(r) : is_retro(r) { pop_back(r["data"]) } \n\ def pop_front(r) : is_retro(r) { pop_back(r["data"]) } \n\
# Performs the second value function over the container first value\n\ # Performs the second value function over the container first value\n\
def for_each(container, func) : call_exists(range, container) { \ def for_each(container, func) : call_exists(range, container) { \n\
var range = range(container); \ var range = range(container); \n\
while (!range.empty()) { \ while (!range.empty()) { \n\
func(range.front()); \ func(range.front()); \n\
range.pop_front(); \ range.pop_front(); \n\
} \ } \n\
} \ } \n\
def back_inserter(container) { \ def back_inserter(container) { \n\
bind(push_back, container, _); \ bind(push_back, container, _); \n\
}\ }\n\
\ \n\
def map(container, func, inserter) : call_exists(range, container) { \ def map(container, func, inserter) : call_exists(range, container) { \n\
var range = range(container); \ var range = range(container); \n\
while (!range.empty()) { \ while (!range.empty()) { \n\
inserter(func(range.front())); \ inserter(func(range.front())); \n\
range.pop_front(); \ range.pop_front(); \n\
} \ } \n\
} \ } \n\
# Performs the second value function over the container first value. Creates a new container with the results\n\ # Performs the second value function over the container first value. Creates a new container with the results\n\
def map(container, func) { \ def map(container, func) { \n\
var retval = new(container); \ var retval = new(container); \n\
map(container, func, back_inserter(retval));\ map(container, func, back_inserter(retval));\n\
retval;\ retval;\n\
}\ }\n\
# Performs the second value function over the container first value. Starts with initial and continues with each element.\n\ # Performs the second value function over the container first value. Starts with initial and continues with each element.\n\
def foldl(container, func, initial) : call_exists(range, container){ \ def foldl(container, func, initial) : call_exists(range, container){ \n\
var retval = initial; \ var retval = initial; \n\
var range = range(container); \ var range = range(container); \n\
while (!range.empty()) { \ while (!range.empty()) { \n\
retval = (func(range.front(), retval)); \ retval = (func(range.front(), retval)); \n\
range.pop_front(); \ range.pop_front(); \n\
} \ } \n\
retval; \ retval; \n\
} \ } \n\
# Returns the sum of the elements of the given value\n\ # Returns the sum of the elements of the given value\n\
def sum(container) { foldl(container, `+`, 0.0) } \ def sum(container) { foldl(container, `+`, 0.0) } \n\
# Returns the product of the elements of the given value\n\ # Returns the product of the elements of the given value\n\
def product(container) { foldl(container, `*`, 1.0) } \ def product(container) { foldl(container, `*`, 1.0) } \n\
# Returns a new container with the elements of the first value concatenated with the elements of the second value\n\ # Returns a new container with the elements of the first value concatenated with the elements of the second value\n\
def concat(x, y) : call_exists(clone, x) { \ def concat(x, y) : call_exists(clone, x) { \n\
var retval = x; \ var retval = x; \n\
var len = y.size(); \ var len = y.size(); \n\
var i = 0; \ var i = 0; \n\
while (i < len) { \ while (i < len) { \n\
retval.push_back(y[i]); \ retval.push_back(y[i]); \n\
++i; \ ++i; \n\
} \ } \n\
retval; \ retval; \n\
} \ } \n\
def take(container, num, inserter) : call_exists(range, container) { \ def take(container, num, inserter) : call_exists(range, container) { \n\
var r = range(container); \ var r = range(container); \n\
var i = num; \ var i = num; \n\
while ((i > 0) && (!r.empty())) { \ while ((i > 0) && (!r.empty())) { \n\
inserter(r.front()); \ inserter(r.front()); \n\
r.pop_front(); \ r.pop_front(); \n\
--i; \ --i; \n\
} \ } \n\
} \ } \n\
# Returns a new container with the given number of elements taken from the container\n\ # Returns a new container with the given number of elements taken from the container\n\
def take(container, num) {\ def take(container, num) {\n\
var retval = new(container); \ var retval = new(container); \n\
take(container, num, back_inserter(retval)); \ take(container, num, back_inserter(retval)); \n\
retval; \ retval; \n\
}\ }\n\
def take_while(container, f, inserter) : call_exists(range, container) { \ def take_while(container, f, inserter) : call_exists(range, container) { \n\
var r = range(container); \ var r = range(container); \n\
while ((!r.empty()) && f(r.front())) { \ while ((!r.empty()) && f(r.front())) { \n\
inserter(r.front()); \ inserter(r.front()); \n\
r.pop_front(); \ r.pop_front(); \n\
} \ } \n\
} \ } \n\
# Returns a new container with the given elements match the second value function\n\ # Returns a new container with the given elements match the second value function\n\
def take_while(container, f) {\ def take_while(container, f) {\n\
var retval = new(container); \ var retval = new(container); \n\
take_while(container, f, back_inserter(retval)); \ take_while(container, f, back_inserter(retval)); \n\
retval;\ retval;\n\
}\ }\n\
def drop(container, num, inserter) : call_exists(range, container) { \ def drop(container, num, inserter) : call_exists(range, container) { \n\
var r = range(container); \ var r = range(container); \n\
var i = num; \ var i = num; \n\
while ((i > 0) && (!r.empty())) { \ while ((i > 0) && (!r.empty())) { \n\
r.pop_front(); \ r.pop_front(); \n\
--i; \ --i; \n\
} \ } \n\
while (!r.empty()) { \ while (!r.empty()) { \n\
inserter(r.front()); \ inserter(r.front()); \n\
r.pop_front(); \ r.pop_front(); \n\
} \ } \n\
} \ } \n\
# Returns a new container with the given number of elements dropped from the given container \n\ # Returns a new container with the given number of elements dropped from the given container \n\
def drop(container, num) {\ def drop(container, num) {\n\
var retval = new(container); \ var retval = new(container); \n\
drop(container, num, back_inserter(retval)); \ drop(container, num, back_inserter(retval)); \n\
retval; \ retval; \n\
}\ }\n\
def drop_while(container, f, inserter) : call_exists(range, container) { \ def drop_while(container, f, inserter) : call_exists(range, container) { \n\
var r = range(container); \ var r = range(container); \n\
while ((!r.empty())&& f(r.front())) { \ while ((!r.empty())&& f(r.front())) { \n\
r.pop_front(); \ r.pop_front(); \n\
} \ } \n\
while (!r.empty()) { \ while (!r.empty()) { \n\
inserter(r.front()); \ inserter(r.front()); \n\
r.pop_front(); \ r.pop_front(); \n\
} \ } \n\
} \ } \n\
# Returns a new container with the given elements dropped that match the second value function\n\ # Returns a new container with the given elements dropped that match the second value function\n\
def drop_while(container, f) {\ def drop_while(container, f) {\n\
var retval = new(container); \ var retval = new(container); \n\
drop_while(container, f, back_inserter(retval)); \ drop_while(container, f, back_inserter(retval)); \n\
retval; \ retval; \n\
}\ }\n\
# Applies the second value function to the container. Starts with the first two elements. Expects at least 2 elements.\n\ # Applies the second value function to the container. Starts with the first two elements. Expects at least 2 elements.\n\
def reduce(container, func) : container.size() >= 2 && call_exists(range, container) { \ def reduce(container, func) : container.size() >= 2 && call_exists(range, container) { \n\
var r = range(container); \ var r = range(container); \n\
var retval = r.front(); \ var retval = r.front(); \n\
r.pop_front(); \ r.pop_front(); \n\
retval = func(retval, r.front()); \ retval = func(retval, r.front()); \n\
r.pop_front(); \ r.pop_front(); \n\
while (!r.empty()) { \ while (!r.empty()) { \n\
retval = func(retval, r.front()); \ retval = func(retval, r.front()); \n\
r.pop_front(); \ r.pop_front(); \n\
} \ } \n\
retval; \ retval; \n\
} \ } \n\
# Returns a string of the elements in container delimited by the second value string\n\ # Returns a string of the elements in container delimited by the second value string\n\
def join(container, delim) { \ def join(container, delim) { \n\
var retval = ""; \ var retval = ""; \n\
var range = range(container); \ var range = range(container); \n\
if (!range.empty()) { \ if (!range.empty()) { \n\
retval += to_string(range.front()); \ retval += to_string(range.front()); \n\
range.pop_front(); \ range.pop_front(); \n\
while (!range.empty()) { \ while (!range.empty()) { \n\
retval += delim; \ retval += delim; \n\
retval += to_string(range.front()); \ retval += to_string(range.front()); \n\
range.pop_front(); \ range.pop_front(); \n\
} \ } \n\
} \ } \n\
retval; \ retval; \n\
} \ } \n\
def filter(container, f, inserter) : call_exists(range, container) { \ def filter(container, f, inserter) : call_exists(range, container) { \n\
var r = range(container); \ var r = range(container); \n\
while (!r.empty()) { \ while (!r.empty()) { \n\
if (f(r.front())) { \ if (f(r.front())) { \n\
inserter(r.front()); \ inserter(r.front()); \n\
} \ } \n\
r.pop_front(); \ r.pop_front(); \n\
} \ } \n\
} \ } \n\
# Returns a new Vector which match the second value function\n\ # Returns a new Vector which match the second value function\n\
def filter(container, f) { \ def filter(container, f) { \n\
var retval = new(container); \ var retval = new(container); \n\
filter(container, f, back_inserter(retval));\ filter(container, f, back_inserter(retval));\n\
retval;\ retval;\n\
}\ }\n\
def generate_range(x, y, inserter) { \ def generate_range(x, y, inserter) { \n\
var i = x; \ var i = x; \n\
while (i <= y) { \ while (i <= y) { \n\
inserter(i); \ inserter(i); \n\
++i; \ ++i; \n\
} \ } \n\
} \ } \n\
# Returns a new Vector which represents the range from the first value to the second value\n\ # Returns a new Vector which represents the range from the first value to the second value\n\
def generate_range(x, y) { \ def generate_range(x, y) { \n\
var retval = Vector(); \ var retval = Vector(); \n\
generate_range(x,y,back_inserter(retval)); \ generate_range(x,y,back_inserter(retval)); \n\
retval; \ retval; \n\
}\ }\n\
# Returns a new Vector with the first value to the second value as its elements\n\ # Returns a new Vector with the first value to the second value as its elements\n\
def collate(x, y) { \ def collate(x, y) { \n\
[x, y]; \ [x, y]; \n\
} \ } \n\
def zip_with(f, x, y, inserter) : call_exists(range, x) && call_exists(range, y) { \ def zip_with(f, x, y, inserter) : call_exists(range, x) && call_exists(range, y) { \n\
var r_x = range(x); \ var r_x = range(x); \n\
var r_y = range(y); \ var r_y = range(y); \n\
while (!r_x.empty() && !r_y.empty()) { \ while (!r_x.empty() && !r_y.empty()) { \n\
inserter(f(r_x.front(), r_y.front())); \ inserter(f(r_x.front(), r_y.front())); \n\
r_x.pop_front(); \ r_x.pop_front(); \n\
r_y.pop_front(); \ r_y.pop_front(); \n\
} \ } \n\
} \ } \n\
# Returns a new Vector which joins matching elements of the second and third value with the first value function\n\ # Returns a new Vector which joins matching elements of the second and third value with the first value function\n\
def zip_with(f, x, y) { \ def zip_with(f, x, y) { \n\
var retval = Vector(); \ var retval = Vector(); \n\
zip_with(f,x,y,back_inserter(retval)); \ zip_with(f,x,y,back_inserter(retval)); \n\
retval;\ retval;\n\
}\ }\n\
# Returns a new Vector which joins matching elements of the first and second\n\ # Returns a new Vector which joins matching elements of the first and second\n\
def zip(x, y) { \ def zip(x, y) { \n\
zip_with(collate, x, y); \ zip_with(collate, x, y); \n\
}\ }\n\
# Returns the position of the second value string in the first value string\n\ # Returns the position of the second value string in the first value string\n\
def find(str, substr) { \ def find(str, substr) { \n\
int(find(str, substr, size_t(0))); \ int(find(str, substr, size_t(0))); \n\
} \ } \n\
# Returns the position of last match of the second value string in the first value string\n\ # Returns the position of last match of the second value string in the first value string\n\
def rfind(str, substr) { \ def rfind(str, substr) { \n\
int(rfind(str, substr, size_t(-1))); \ int(rfind(str, substr, size_t(-1))); \n\
} \ } \n\
# Returns the position of the first match of elements in the second value string in the first value string\n\ # Returns the position of the first match of elements in the second value string in the first value string\n\
def find_first_of(str, list) { \ def find_first_of(str, list) { \n\
int(find_first_of(str, list, size_t(0))); \ int(find_first_of(str, list, size_t(0))); \n\
} \ } \n\
# Returns the position of the last match of elements in the second value string in the first value string\n\ # Returns the position of the last match of elements in the second value string in the first value string\n\
def find_last_of(str, list) { \ def find_last_of(str, list) { \n\
int(find_last_of(str, list, size_t(-1))); \ int(find_last_of(str, list, size_t(-1))); \n\
} \ } \n\
# Returns the position of the first non-matching element in the second value string in the first value string\n\ # Returns the position of the first non-matching element in the second value string in the first value string\n\
def find_first_not_of(str, list) { \ def find_first_not_of(str, list) { \n\
int(find_first_not_of(str, list, size_t(0))); \ int(find_first_not_of(str, list, size_t(0))); \n\
} \ } \n\
# Returns the position of the last non-matching element in the second value string in the first value string\n\ # Returns the position of the last non-matching element in the second value string in the first value string\n\
def find_last_not_of(str, list) { \ def find_last_not_of(str, list) { \n\
int(find_last_not_of(str, list, size_t(-1))); \ int(find_last_not_of(str, list, size_t(-1))); \n\
} \ } \n\
def ltrim(str) { \ def ltrim(str) { \n\
drop_while(str, fun(x) { x == ' ' || x == '\t' }); \ drop_while(str, fun(x) { x == ' ' || x == '\t' }); \n\
} \ } \n\
def rtrim(str) { \ def rtrim(str) { \n\
reverse(drop_while(reverse(str), fun(x) { x == ' ' || x == '\t' })); \ reverse(drop_while(reverse(str), fun(x) { x == ' ' || x == '\t' })); \n\
} \ } \n\
def trim(str) { \ def trim(str) { \n\
ltrim(rtrim(str)); \ ltrim(rtrim(str)); \n\
} \ } \
) )
#endif /* CHAISCRIPT_PRELUDE_HPP_ */ #endif /* CHAISCRIPT_PRELUDE_HPP_ */

View File

@ -0,0 +1,4 @@
var x=.5
print(x)
var y=-.5
print(y)

View File

@ -0,0 +1,2 @@
0.5
-0.5