/* * * ProLinga-Calc * * Copyright (C) 2002-2008 Xobas Software * All rights reserved. * * This file is part of ProLinga-Calc. * * ProLinga-Calc 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. * * ProLinga-Calc 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 ProLinga-Calc. If not, see . * * More information is available at the following addresses: * * Website : http://www.prolinga.org * * Email : prolinga-list@prolinga.org * * */ #ifndef __CALC_LIB_HPP #define __CALC_LIB_HPP 1 class symbol; enum t_value { INT, FLOAT }; class value { public: t_value tag; symbol* var; int pos; union { int ival; double fval; }; double get() { return tag == INT ? (double)ival : fval; } int get_int() { return tag == INT ? ival : (int)fval; } }; enum t_operator { BEGIN, OPERAND, ERROR, END, LPAR, RPAR, FUNC, POSTINC, POSTDEC, PREINC, PREDEC, PLUS, MINUS, NOT, COM, POW, MUL, DIV, MOD, ADD, SUB, ASL, ASR, LSR, GT, GE, LT, LE, EQ, NE, AND, XOR, OR, SET, SETADD, SETSUB, SETMUL, SETDIV, SETMOD, SETASL, SETASR, SETLSR, SETAND, SETXOR, SETOR, SETPOW, COMMA, TERMINALS }; #define BINARY(opd) (opd >= POW) enum t_symbol { VARIABLE, IFUNC1, FFUNC1, FFUNC2 }; #define CALC_MAX_STACK_SIZE 256 #define CALC_MAX_EXP_LEN 1024 #define CALC_HASH_TABLE_SIZE 1013 class symbol { public: t_symbol tag; void* func; value val; char* name; symbol* next; static symbol* hash_table[CALC_HASH_TABLE_SIZE]; static symbol* add(t_symbol tag, const char* name, void* func = NULL); }; class calculator { public: value v_stack[CALC_MAX_STACK_SIZE]; int v_sp; int o_stack[CALC_MAX_STACK_SIZE]; int o_sp; char* buf; int pos; int tmp_var_count; void evaluate(char* expr); t_operator scan(bool operand); void calcerror(int pos, const char* msg); void calcerror(const char* msg) { calcerror(pos-1, msg); } bool assign(); calculator() { tmp_var_count = 0; } }; #endif /* __CALC_LIB_HPP */