diff options
-rw-r--r-- | src/lib/eval/eval_context.cc | 39 | ||||
-rw-r--r-- | src/lib/eval/eval_context.h | 18 | ||||
-rw-r--r-- | src/lib/eval/lexer.ll | 36 | ||||
-rw-r--r-- | src/lib/eval/parser.yy | 10 |
4 files changed, 58 insertions, 45 deletions
diff --git a/src/lib/eval/eval_context.cc b/src/lib/eval/eval_context.cc index 94338b9511..2093f99e5a 100644 --- a/src/lib/eval/eval_context.cc +++ b/src/lib/eval/eval_context.cc @@ -1,38 +1,51 @@ -#line 11104 "./doc/bison.texi" #include "eval_context.h" #include "parser.h" -calcxx_driver::calcxx_driver () +#include <fstream> + +EvalContext::EvalContext () : trace_scanning (false), trace_parsing (false) { variables["one"] = 1; variables["two"] = 2; } -calcxx_driver::~calcxx_driver () +EvalContext::~EvalContext () { } int -calcxx_driver::parse (const std::string &f) +EvalContext::parseFile(const std::string &filename) { - file = f; - scan_begin (); - yy::calcxx_parser parser (*this); - parser.set_debug_level (trace_parsing); - int res = parser.parse (); - scan_end (); - return res; + file = filename; + scan_begin(); + yy::EvalParser parser (*this); + parser.set_debug_level(trace_parsing); + int res = parser.parse(); + scan_end(); + return res; +} + +int +EvalContext::parseString(const std::string& str) +{ + remove("/tmp/eval"); + std::fstream f("/tmp/eval", std::ios::trunc); + + f << str; + f.close(); + + return (parseFile("/tmp/eval")); } void -calcxx_driver::error (const yy::location& l, const std::string& m) +EvalContext::error (const yy::location& l, const std::string& m) { std::cerr << l << ": " << m << std::endl; } void -calcxx_driver::error (const std::string& m) +EvalContext::error (const std::string& m) { std::cerr << m << std::endl; } diff --git a/src/lib/eval/eval_context.h b/src/lib/eval/eval_context.h index 1d26f04faa..9960d77f95 100644 --- a/src/lib/eval/eval_context.h +++ b/src/lib/eval/eval_context.h @@ -1,22 +1,22 @@ -#ifndef CALCXX_DRIVER_HH -# define CALCXX_DRIVER_HH +#ifndef EVAL_CONTEXT_H +#define EVAL_CONTEXT_H # include <string> # include <map> # include "parser.h" // Tell Flex the lexer's prototype ... # define YY_DECL \ - yy::calcxx_parser::symbol_type yylex (calcxx_driver& driver) + yy::EvalParser::symbol_type yylex (EvalContext& driver) // ... and declare it for the parser's sake. YY_DECL; // Conducting the whole scanning and parsing of Calc++. -class calcxx_driver +class EvalContext { public: - calcxx_driver (); - virtual ~calcxx_driver (); + EvalContext (); + virtual ~EvalContext (); std::map<std::string, int> variables; @@ -29,7 +29,9 @@ public: // Run the parser on file F. // Return 0 on success. - int parse (const std::string& f); + int parseFile(const std::string& filename); + + int parseString(const std::string& str); // The name of the file being parsed. // Used later to pass the file name to the location tracker. @@ -42,4 +44,4 @@ public: void error (const yy::location& l, const std::string& m); void error (const std::string& m); }; -#endif // ! CALCXX_DRIVER_HH +#endif // ! EVALCONTEXT_HH diff --git a/src/lib/eval/lexer.ll b/src/lib/eval/lexer.ll index f48e8161cd..386e16dfec 100644 --- a/src/lib/eval/lexer.ll +++ b/src/lib/eval/lexer.ll @@ -22,26 +22,26 @@ int [0-9]+ blank [ \t] %{ - // Code run each time a pattern is matched. - # define YY_USER_ACTION loc.columns (yyleng); + // Code run each time a pattern is matched. +#define YY_USER_ACTION loc.columns(yyleng); %} %% %{ - // Code run each time yylex is called. - loc.step (); + // Code run each time yylex is called. + loc.step(); %} {blank}+ loc.step (); [\n]+ loc.lines (yyleng); loc.step (); -"-" return yy::calcxx_parser::make_MINUS(loc); -"+" return yy::calcxx_parser::make_PLUS(loc); -"*" return yy::calcxx_parser::make_STAR(loc); -"/" return yy::calcxx_parser::make_SLASH(loc); -"(" return yy::calcxx_parser::make_LPAREN(loc); -")" return yy::calcxx_parser::make_RPAREN(loc); -":=" return yy::calcxx_parser::make_ASSIGN(loc); +"-" return yy::EvalParser::make_MINUS(loc); +"+" return yy::EvalParser::make_PLUS(loc); +"*" return yy::EvalParser::make_STAR(loc); +"/" return yy::EvalParser::make_SLASH(loc); +"(" return yy::EvalParser::make_LPAREN(loc); +")" return yy::EvalParser::make_RPAREN(loc); +":=" return yy::EvalParser::make_ASSIGN(loc); {int} { @@ -49,31 +49,29 @@ blank [ \t] long n = strtol (yytext, NULL, 10); if (! (INT_MIN <= n && n <= INT_MAX && errno != ERANGE)) driver.error (loc, "integer is out of range"); - return yy::calcxx_parser::make_NUMBER(n, loc); + return yy::EvalParser::make_NUMBER(n, loc); } -{id} return yy::calcxx_parser::make_IDENTIFIER(yytext, loc); +{id} return yy::EvalParser::make_IDENTIFIER(yytext, loc); . driver.error (loc, "invalid character"); -<<EOF>> return yy::calcxx_parser::make_END(loc); +<<EOF>> return yy::EvalParser::make_END(loc); %% void -calcxx_driver::scan_begin () +EvalContext::scan_begin() { yy_flex_debug = trace_scanning; if (file.empty () || file == "-") yyin = stdin; - else if (!(yyin = fopen (file.c_str (), "r"))) + else if (!(yyin = fopen(file.c_str (), "r"))) { error ("cannot open " + file + ": " + strerror(errno)); exit (EXIT_FAILURE); } } - - void -calcxx_driver::scan_end () +EvalContext::scan_end() { fclose (yyin); } diff --git a/src/lib/eval/parser.yy b/src/lib/eval/parser.yy index f13542c7d6..88862a21b2 100644 --- a/src/lib/eval/parser.yy +++ b/src/lib/eval/parser.yy @@ -1,17 +1,17 @@ %skeleton "lalr1.cc" /* -*- C++ -*- */ %require "3.0.2" %defines -%define parser_class_name {calcxx_parser} +%define parser_class_name {EvalParser} %define api.token.constructor %define api.value.type variant %define parse.assert %code requires { # include <string> -class calcxx_driver; +class EvalContext; } // The parsing context. -%param { calcxx_driver& driver } +%param { EvalContext& driver } %locations %initial-action { @@ -62,8 +62,8 @@ exp: | "number" { std::swap ($$, $1); }; %% void -yy::calcxx_parser::error (const location_type& l, - const std::string& m) +yy::EvalParser::error(const location_type& l, + const std::string& m) { driver.error (l, m); } |