"Fossies" - the Fresh Open Source Software Archive

Member "ivtools-ivtools-2.0.4/src/ComTerp/assignfunc.c" (9 Oct 2020, 11441 Bytes) of package /linux/misc/ivtools-ivtools-2.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "assignfunc.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (c) 2000 IET Inc.
    3  * Copyright (c) 1997,1999 Vectaport Inc.
    4  *
    5  * Permission to use, copy, modify, distribute, and sell this software and
    6  * its documentation for any purpose is hereby granted without fee, provided
    7  * that the above copyright notice appear in all copies and that both that
    8  * copyright notice and this permission notice appear in supporting
    9  * documentation, and that the names of the copyright holders not be used in
   10  * advertising or publicity pertaining to distribution of the software
   11  * without specific, written prior permission.  The copyright holders make
   12  * no representations about the suitability of this software for any purpose.
   13  * It is provided "as is" without express or implied warranty.
   14  *
   15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
   16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
   17  * IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL,
   18  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
   19  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
   20  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
   21  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   22  * 
   23  */
   24 
   25 #include <ComTerp/assignfunc.h>
   26 #include <ComTerp/comvalue.h>
   27 #include <ComTerp/comterp.h>
   28 #include <Attribute/attrlist.h>
   29 #include <Attribute/attribute.h>
   30 #include <InterViews/resource.h>
   31 
   32 #include <fstream>
   33 #include <iostream>
   34 using std::cout;
   35 using std::cerr;
   36 
   37 #define TITLE "AssignFunc"
   38 
   39 /*****************************************************************************/
   40 
   41 int AssignFunc::_symid = -1;
   42 
   43 AssignFunc::AssignFunc(ComTerp* comterp) : ComFunc(comterp) {
   44 }
   45 
   46 
   47 void AssignFunc::execute() {
   48     ComValue operand1(stack_arg(0, true));
   49     if (operand1.is_command() && stack_arg_post_eval_size(0)==1) {
   50         cout << "WARNING:  assignment to command \"" << operand1.command_name() << "\" without args not allowed -- line " << funcstate()->linenum() << "\n";
   51     reset_stack();
   52     push_stack(ComValue::nullval());
   53     return;
   54     }
   55     
   56     if (operand1.type() != ComValue::SymbolType) {
   57       operand1.assignval(stack_arg_post_eval(0, true /* no symbol or attribute lookup */));
   58     }
   59     ComValue* operand2 = new ComValue(stack_arg_post_eval(1, true /* no symbol or attribute lookup */));
   60 #ifdef POSTEVAL_EXPERIMENT
   61     if (operand2->is_attribute() || operand2->is_symbol()) lookup_symval(*operand2);
   62 #else
   63     if (operand2->is_attribute()) lookup_symval(*operand2);
   64 #endif
   65     if (operand1.type() == ComValue::SymbolType) {
   66         AttributeList* attrlist = comterp()->get_attributes();
   67     if (attrlist) {
   68         Resource::ref(attrlist);
   69         Attribute* attr = new Attribute(operand1.symbol_val(), 
   70                         operand2);
   71         attrlist->add_attribute(attr);
   72         Unref(attrlist);
   73     } else if (operand1.global_flag()) {
   74         AttributeValue* oldval = comterp()->lookup_symval(&operand1);
   75         if (oldval) {
   76           comterp()->globaltable()->remove(operand1.symbol_val());
   77           delete (ComValue*)oldval;
   78         }
   79         comterp()->globaltable()->insert(operand1.symbol_val(), operand2);
   80     }
   81     else {
   82         AttributeValue* oldval = comterp()->lookup_symval(&operand1);
   83         if (oldval) {
   84           comterp()->localtable()->remove(operand1.symbol_val());
   85           delete (ComValue*)oldval;
   86         }
   87             comterp()->localtable()->insert(operand1.symbol_val(), operand2);
   88     }
   89     } else if (operand1.is_object(Attribute::class_symid())) {
   90       Attribute* attr = (Attribute*)operand1.obj_val();
   91       attr->Value(operand2);
   92     } else {
   93         cout << "WARNING:  assignment to something other than a symbol or attribute (" <<
   94           symbol_pntr(operand1.type_symid()) << ") ignored -- line " << funcstate()->linenum() << "\n";
   95     cout << "comterp stack:  ";
   96         print_stack_arg_post_eval(0);
   97     delete operand2;
   98     }
   99     reset_stack();
  100     push_stack(*operand2);
  101 }
  102 
  103 ModAssignFunc::ModAssignFunc(ComTerp* comterp) : AssignFunc(comterp) {
  104 }
  105 
  106 
  107 void ModAssignFunc::execute() {
  108     ComValue operand1(stack_arg(0, true));
  109     if (operand1.type() != ComValue::SymbolType) {
  110       operand1.assignval(stack_arg_post_eval(0, true /* no symbol lookup */));
  111     }
  112     ComValue operand2(stack_arg_post_eval(1, true /* no symbol lookup */));
  113     if (operand2.is_attribute()) lookup_symval(operand2);
  114     reset_stack();
  115     if (operand1.type() == ComValue::SymbolType) {
  116         AttributeValue* op1val = comterp()->lookup_symval(&operand1);
  117         if (!op1val) {
  118         push_stack(ComValue::nullval());
  119         return;
  120     }
  121     push_stack(*(ComValue*)op1val);
  122     delete (ComValue*)op1val;
  123     push_stack(operand2);
  124     ModFunc modfunc(comterp());
  125     modfunc.exec(2,0);
  126     ComValue result(pop_stack());
  127         *(ComValue*)op1val = result;
  128     push_stack(result);
  129     }
  130 
  131 }
  132 
  133 MpyAssignFunc::MpyAssignFunc(ComTerp* comterp) : AssignFunc(comterp) {
  134 }
  135 
  136 
  137 void MpyAssignFunc::execute() {
  138     ComValue operand1(stack_arg(0, true));
  139     if (operand1.type() != ComValue::SymbolType) {
  140       operand1.assignval(stack_arg_post_eval(0, true /* no symbol lookup */));
  141     }
  142     ComValue operand2(stack_arg_post_eval(1, true /* no symbol lookup */));
  143     if (operand2.is_attribute()) lookup_symval(operand2);
  144     reset_stack();
  145     if (operand1.type() == ComValue::SymbolType) {
  146         AttributeValue* op1val = comterp()->lookup_symval(&operand1);
  147     if (!op1val) {
  148         push_stack(ComValue::nullval());
  149         return;
  150     }
  151     push_stack(*(ComValue*)op1val);
  152     push_stack(operand2);
  153     MpyFunc mpyfunc(comterp());
  154     mpyfunc.exec(2,0);
  155     ComValue result(pop_stack());
  156         *(ComValue*)op1val = result;
  157     push_stack(result);
  158     }
  159 
  160 }
  161 
  162 AddAssignFunc::AddAssignFunc(ComTerp* comterp) : AssignFunc(comterp) {
  163 }
  164 
  165 void AddAssignFunc::execute() {
  166     ComValue operand1(stack_arg(0, true));
  167     if (operand1.type() != ComValue::SymbolType) {
  168       operand1.assignval(stack_arg_post_eval(0, true /* no symbol lookup */));
  169     }
  170     ComValue operand2(stack_arg_post_eval(1, true /* no symbol lookup */));
  171     if (operand2.is_attribute()) lookup_symval(operand2);
  172     reset_stack();
  173     if (operand1.type() == ComValue::SymbolType) {
  174         AttributeValue* op1val = comterp()->lookup_symval(&operand1);
  175         if (!op1val) {
  176         push_stack(ComValue::nullval());
  177         return;
  178     }
  179     push_stack(*(ComValue*)op1val);
  180     push_stack(operand2);
  181     AddFunc addfunc(comterp());
  182     addfunc.exec(2,0);
  183     ComValue result(pop_stack());
  184         *(ComValue*)op1val = result;
  185     push_stack(result);
  186     }
  187 
  188 }
  189 
  190 SubAssignFunc::SubAssignFunc(ComTerp* comterp) : AssignFunc(comterp) {
  191 }
  192 
  193 
  194 void SubAssignFunc::execute() {
  195     ComValue operand1(stack_arg(0, true));
  196     if (operand1.type() != ComValue::SymbolType) {
  197       operand1.assignval(stack_arg_post_eval(0, true /* no symbol lookup */));
  198     }
  199     ComValue operand2(stack_arg_post_eval(1, true /* no symbol lookup */));
  200     if (operand2.is_attribute()) lookup_symval(operand2);
  201     reset_stack();
  202     if (operand1.type() == ComValue::SymbolType) {
  203         AttributeValue* op1val = comterp()->lookup_symval(&operand1);
  204         if (!op1val) {
  205         push_stack(ComValue::nullval());
  206         return;
  207     }
  208     push_stack(*(ComValue*)op1val);
  209     push_stack(operand2);
  210     SubFunc subfunc(comterp());
  211     subfunc.exec(2,0);
  212     ComValue result(pop_stack());
  213         *(ComValue*)op1val = result;
  214     push_stack(result);
  215     }
  216 
  217 }
  218 
  219 DivAssignFunc::DivAssignFunc(ComTerp* comterp) : AssignFunc(comterp) {
  220 }
  221 
  222 
  223 void DivAssignFunc::execute() {
  224     ComValue operand1(stack_arg(0, true));
  225     if (operand1.type() != ComValue::SymbolType) {
  226       operand1.assignval(stack_arg_post_eval(0, true /* no symbol lookup */));
  227     }
  228     ComValue operand2(stack_arg_post_eval(1, true /* no symbol lookup */));
  229     if (operand2.is_attribute()) lookup_symval(operand2);
  230     reset_stack();
  231     if (operand1.type() == ComValue::SymbolType) {
  232     AttributeValue* op1val = comterp()->lookup_symval(&operand1);
  233     if (!op1val) {
  234         push_stack(ComValue::nullval());
  235         return;
  236     }
  237     push_stack(*(ComValue*)op1val);
  238     push_stack(operand2);
  239     DivFunc divfunc(comterp());
  240     divfunc.exec(2,0);
  241     ComValue result(pop_stack());
  242         *(ComValue*)op1val = result;
  243     push_stack(result);
  244     }
  245 
  246 }
  247 
  248 IncrFunc::IncrFunc(ComTerp* comterp) : AssignFunc(comterp) {
  249 }
  250 
  251 void IncrFunc::execute() {
  252     ComValue operand1(stack_arg(0, true));
  253     if (operand1.type() != ComValue::SymbolType) {
  254       operand1.assignval(stack_arg_post_eval(0, true /* no symbol lookup */));
  255     }
  256     reset_stack();
  257     if (operand1.type() == ComValue::SymbolType) {
  258         AttributeValue* op1val = comterp()->lookup_symval(&operand1);
  259     if (!op1val) 
  260         push_stack(ComValue::nullval());
  261     else {
  262         push_stack(*(ComValue*)op1val);
  263         ComValue one;
  264         one.type(ComValue::IntType);
  265         one.int_ref() = 1;
  266         push_stack(one);
  267         AddFunc addfunc(comterp());
  268         addfunc.exec(2,0);
  269         ComValue result(pop_stack());
  270             *(ComValue*)op1val = result;
  271         push_stack(result);
  272     }
  273     } else 
  274         push_stack(ComValue::nullval());
  275 
  276 }
  277 
  278 IncrAfterFunc::IncrAfterFunc(ComTerp* comterp) : AssignFunc(comterp) {
  279 }
  280 
  281 void IncrAfterFunc::execute() {
  282     ComValue operand1(stack_arg(0, true));
  283 
  284     if (operand1.type() != ComValue::SymbolType) {
  285       operand1.assignval(stack_arg_post_eval(0, true /* no symbol lookup */));
  286     }
  287     reset_stack();
  288     if (operand1.type() == ComValue::SymbolType) {
  289         AttributeValue* op1val = comterp()->lookup_symval(&operand1);
  290     if (!op1val)
  291         push_stack(ComValue::nullval());
  292     else {
  293         push_stack(*(ComValue*)op1val);
  294         ComValue one;
  295         one.type(ComValue::IntType);
  296         one.int_ref() = 1;
  297         push_stack(one);
  298         AddFunc addfunc(comterp());
  299         addfunc.exec(2,0);
  300         ComValue result(pop_stack());
  301         push_stack(*(ComValue*)op1val);
  302             *(ComValue*)op1val = result;
  303     }
  304     } else 
  305         push_stack(ComValue::nullval());
  306 }
  307 
  308 DecrFunc::DecrFunc(ComTerp* comterp) : AssignFunc(comterp) {
  309 }
  310 
  311 void DecrFunc::execute() {
  312     ComValue operand1(stack_arg(0,true));
  313     if (operand1.type() != ComValue::SymbolType) {
  314       operand1.assignval(stack_arg_post_eval(0, true /* no symbol lookup */));
  315     }
  316     reset_stack();
  317     if (operand1.type() == ComValue::SymbolType) {
  318         AttributeValue* op1val = comterp()->lookup_symval(&operand1);
  319     if (!op1val)
  320         push_stack(ComValue::nullval());
  321     else {
  322         push_stack(*(ComValue*)op1val);
  323         ComValue one;
  324         one.type(ComValue::IntType);
  325         one.int_ref() = 1;
  326         push_stack(one);
  327         SubFunc subfunc(comterp());
  328         subfunc.exec(2,0);
  329         ComValue result(pop_stack());
  330             *(ComValue*)op1val = result;
  331         push_stack(result);
  332     }
  333     } else 
  334         push_stack(ComValue::nullval());
  335 
  336 }
  337 
  338 DecrAfterFunc::DecrAfterFunc(ComTerp* comterp) : AssignFunc(comterp) {
  339 }
  340 
  341 void DecrAfterFunc::execute() {
  342     ComValue operand1(stack_arg(0,true));
  343     if (operand1.type() != ComValue::SymbolType) {
  344       operand1.assignval(stack_arg_post_eval(0, true /* no symbol lookup */));
  345     }
  346     reset_stack();
  347     if (operand1.type() == ComValue::SymbolType) {
  348         AttributeValue* op1val = comterp()->lookup_symval(&operand1);
  349     if (!op1val)
  350         push_stack(ComValue::nullval());
  351     else {
  352         push_stack(*(ComValue*)op1val);
  353         ComValue one;
  354         one.type(ComValue::IntType);
  355         one.int_ref() = 1;
  356         push_stack(one);
  357         SubFunc subfunc(comterp());
  358         subfunc.exec(2,0);
  359         ComValue result(pop_stack());
  360         push_stack(*(ComValue*)op1val);
  361             *(ComValue*)op1val = result;
  362     }
  363     } else 
  364         push_stack(ComValue::nullval());
  365 
  366 }