// Author: Marek K. /* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Dieses Programm ist Freie Software: Sie koennen es unter den Bedingungen der GNU General Public License, wie von der Free Software Foundation, Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren veroeffentlichten Version, weiter verteilen und/oder modifizieren. Dieses Programm wird in der Hoffnung bereitgestellt, dass es nuetzlich sein wird, jedoch OHNE JEDE GEWAEHR,; sogar ohne die implizite Gewaehr der MARKTFAEHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK. Siehe die GNU General Public License fuer weitere Einzelheiten. Sie sollten eine Kopie der GNU General Public License zusammen mit diesem Programm erhalten haben. Wenn nicht, siehe . */ %{ #include #include #include #include #include #include #include #include #include "mminilang.hpp" int yylex(YP_ARG_1, YP_ARG_2, YP_ARG_3); void yyerror(YP_ARG_1, YP_ARG_2, YP_ARG_3, char const *); double ycall(char *, std::ostream &); double ycall(char *, double, std::ostream &); double ycall(char *, double, double, std::ostream &); void ysetvar(char *, double); double ygetvar(char *); double ygetarg(double, YP_ARG_3); std::map variables; %} %union { double flo; char name[255]; } %parse-param { YP_ARG_1 sti } %parse-param { YP_ARG_2 sto } %parse-param { YP_ARG_3 args } %lex-param { YP_ARG_1 sti } %lex-param { YP_ARG_2 sto } %lex-param { YP_ARG_3 args } %token FLO %token NAME %right '=' %left '+' '-' %left '*' '/' '%' %left '&' '|' 'x' %left NEG NOT LOGIC_NOT %left '^' %left ARG %type exp %% language: /* nothing */ | language statment ; statment: '\n' | exp '\n' | exp ';' | error { yyerrok; } ; exp: FLO { $$ = $1; } | NAME '(' ')' { $$ = ycall($1, sto); } | NAME '(' exp ')' { $$ = ycall($1, $3, sto); } | NAME '(' exp ',' exp ')' { $$ = ycall($1, $3, $5, sto); } | NAME '=' exp { $$ = 1; ysetvar($1, $3); } | NAME { $$ = ygetvar($1); } | '_' exp %prec ARG { $$ = ygetarg($2, args); } | '(' exp ')' { $$ = $2; } /* 1 Operand */ | '-' exp %prec NEG { $$ = - $2; } | '~' exp %prec NOT { $$ = ~ static_cast($2); } | '!' exp %prec LOGIC_NOT { $$ = ! static_cast($2); } /* 2 Operands */ | exp '+' exp { $$ = $1 + $3; } | exp '-' exp { $$ = $1 - $3; } | exp '*' exp { $$ = $1 * $3; } | exp '/' exp { $$ = $1 / $3; } | exp '%' exp { $$ = static_cast($1) % static_cast($3); } | exp '^' exp { $$ = pow($1, $3); } | exp '&' exp { $$ = static_cast($1) & static_cast($3); } | exp '|' exp { $$ = static_cast($1) | static_cast($3); } | exp 'x' exp { $$ = static_cast($1) ^ static_cast($3); } ; %% int yylex(YP_ARG_1 sti, YP_ARG_2, YP_ARG_3) { char c = static_cast(0); while(c == ' ' || c == '\t' || c == static_cast(0)) c = sti.get(); if(isdigit(c) || c == '.') { sti.putback(c); sti >> yylval.flo; return FLO; } else if(isalpha(c) && c != 'x') { size_t i; sti.putback(c); //sti.get(yylval.name, 255, '('); for(i = 0; i < 254 && isalpha(c); i++) { yylval.name[i] = c = sti.get(); } sti.putback(yylval.name[i - 1]); yylval.name[i - 1] = static_cast(0); return NAME; } else if(c == EOF) return 0; else return c; } void yyerror(YP_ARG_1, YP_ARG_2, YP_ARG_3, char const * str) { std::cerr << "Error: " << str << std::endl; exit(EXIT_FAILURE); } double ycall(char * func, std::ostream &) { if(strcmp(func, "exit") == 0) { exit(EXIT_SUCCESS); return 1; } else return 0; } double ycall(char * func, double a, std::ostream & sto) { if(strcmp(func, "sqrt") == 0) return sqrt(a); else if(strcmp(func, "print") == 0) { sto << a << std::endl; return 1; } else if(strcmp(func, "exit") == 0) { exit(static_cast(a)); return 1; } else return 0; } double ycall(char * func, double a, double b, std::ostream &) { if(strcmp(func, "gcd") == 0) return static_cast(gcd(static_cast(a), static_cast(b))); else if(strcmp(func, "lcm") == 0) return static_cast(lcm(static_cast(a), static_cast(b))); else if(strcmp(func, "root") == 0) return pow(a, 1.0/b); else return 0; } inline void ysetvar(char * name, double val) { variables[std::string(name)] = val; } inline double ygetvar(char * name) { return variables[std::string(name)]; } double ygetarg(double key, YP_ARG_3 args) { size_t ind = static_cast(key); if(ind > args.size()) return 0; else return args[ind - 1]; }