That way, we don't need to carry the string along.
---
software/libfpvm/ast.h | 1 +
software/libfpvm/parser.y | 2 +-
software/libfpvm/parser_helper.c | 5 ++++-
software/libfpvm/scanner.h | 2 ++
software/libfpvm/scanner.re | 21 +++++++++++++++++++++
5 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/software/libfpvm/ast.h b/software/libfpvm/ast.h
index da0650f..d93e97f 100644
--- a/software/libfpvm/ast.h
+++ b/software/libfpvm/ast.h
@@ -25,6 +25,7 @@
struct id {
int token;
const char *label;
+ float constant;
};
/* maximum supported arity is 3 */
diff --git a/software/libfpvm/parser.y b/software/libfpvm/parser.y
index 42dfd1e..0c9bee9 100644
--- a/software/libfpvm/parser.y
+++ b/software/libfpvm/parser.y
@@ -58,7 +58,7 @@ start(S) ::= node(N). {
node(N) ::= TOK_CONSTANT(C). {
N = node(TOK_CONSTANT, "", NULL, NULL, NULL);
- N->contents.constant = atof(C->label);
+ N->contents.constant = C->constant;
}
node(N) ::= ident(I). {
diff --git a/software/libfpvm/parser_helper.c b/software/libfpvm/parser_helper.c
index 8b6be1e..665e769 100644
--- a/software/libfpvm/parser_helper.c
+++ b/software/libfpvm/parser_helper.c
@@ -37,7 +37,10 @@ struct ast_node *fpvm_parse(const char *expr)
while(tok != TOK_EOF) {
identifier = malloc(sizeof(struct id));
identifier->token = tok;
- identifier->label = get_token(s);
+ if(tok == TOK_CONSTANT)
+ identifier->constant = get_constant(s);
+ else
+ identifier->label = get_token(s);
Parse(p, tok, identifier, &ast);
if(tok == TOK_ERROR) {
printf("FPVM: scan error\n");
diff --git a/software/libfpvm/scanner.h b/software/libfpvm/scanner.h
index 0ccee3a..627364a 100644
--- a/software/libfpvm/scanner.h
+++ b/software/libfpvm/scanner.h
@@ -40,5 +40,7 @@ int scan(struct scanner *s);
*/
const char *get_token(struct scanner *s);
+float get_constant(struct scanner *s);
+
#endif /* __SCANNER_H */
diff --git a/software/libfpvm/scanner.re b/software/libfpvm/scanner.re
index 86cba0b..1362ab4 100644
--- a/software/libfpvm/scanner.re
+++ b/software/libfpvm/scanner.re
@@ -99,3 +99,24 @@ const char *get_token(struct scanner *s)
return unique_n((const char *) s->old_cursor,
s->cursor - s->old_cursor);
}
+
+float get_constant(struct scanner *s)
+{
+ const unsigned char *p;
+ float v = 0;
+ float m = 1;
+
+ for(p = s->old_cursor; p != s->cursor; p++) {
+ if(*p == '.')
+ goto dot;
+ v = v*10+(*p-'0');
+ }
+ return v;
+
+dot:
+ for(p++; p != s->cursor; p++) {
+ m /= 10;
+ v += m*(*p-'0');
+ }
+ return v;
+}
--
1.7.1
_______________________________________________
http://lists.milkymist.org/listinfo.cgi/devel-milkymist.org
IRC: #milkymist@Freenode