Fix negative number syntax. Add a unary '-' operator.

Closes #63.
This commit is contained in:
Stephen Dolan committed 2013-01-03 12:53:23 +00:00
1 parent c013b557a2
commit 925ec3751f
4 files changed
+28 -1

No files matched your search

+10
View File
@@ -65,6 +65,15 @@ static jv f_plus(jv input, jv a, jv b) {
} }
} }
static jv f_negate(jv input) {
if (jv_get_kind(input) != JV_KIND_NUMBER) {
return type_error(input, "cannot be negated");
}
jv ret = jv_number(-jv_number_value(input));
jv_free(input);
return ret;
}
static jv f_minus(jv input, jv a, jv b) { static jv f_minus(jv input, jv a, jv b) {
jv_free(input); jv_free(input);
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) { if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
@@ -462,6 +471,7 @@ static jv f_error(jv input, jv msg) {
static struct cfunction function_list[] = { static struct cfunction function_list[] = {
{(cfunction_ptr)f_plus, "_plus", 3}, {(cfunction_ptr)f_plus, "_plus", 3},
{(cfunction_ptr)f_negate, "_negate", 1},
{(cfunction_ptr)f_minus, "_minus", 3}, {(cfunction_ptr)f_minus, "_minus", 3},
{(cfunction_ptr)f_multiply, "_multiply", 3}, {(cfunction_ptr)f_multiply, "_multiply", 3},
{(cfunction_ptr)f_divide, "_divide", 3}, {(cfunction_ptr)f_divide, "_divide", 3},
+1 -1
View File
@@ -73,7 +73,7 @@ struct lexer_param;
yylval->literal = jv_string_sized(yytext + 1, yyleng - 1); return FORMAT; yylval->literal = jv_string_sized(yytext + 1, yyleng - 1); return FORMAT;
} }
-?[0-9.]+([eE][+-]?[0-9]+)? { [0-9.]+([eE][+-]?[0-9]+)? {
yylval->literal = jv_parse_sized(yytext, yyleng); return LITERAL; yylval->literal = jv_parse_sized(yytext, yyleng); return LITERAL;
} }
+4
View File
@@ -260,6 +260,10 @@ Exp "+=" Exp {
$$ = gen_update($1, $3, '+'); $$ = gen_update($1, $3, '+');
} | } |
'-' Exp {
$$ = BLOCK($2, gen_call("_negate", gen_noop()));
} |
Exp '-' Exp { Exp '-' Exp {
$$ = gen_binop($1, $3, '-'); $$ = gen_binop($1, $3, '-');
} | } |
+13
View File
@@ -21,6 +21,11 @@ null
null null
1 1
-1
null
-1
# FIXME: much more number testing needed # FIXME: much more number testing needed
{} {}
@@ -197,6 +202,14 @@ null
"wtasdf" "wtasdf"
2.0 2.0
2-1
null
1
2-(-1)
null
3
1e+0+0.001e3 1e+0+0.001e3
"I wonder what this will be?" "I wonder what this will be?"
20e-1 20e-1