home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.68.zip / src / asm / expr.c < prev    next >
C/C++ Source or Header  |  2008-04-01  |  4KB  |  143 lines

  1. /*
  2.  * Copyright (c) 2005 Magnus Lind.
  3.  *
  4.  * This software is provided 'as-is', without any express or implied warranty.
  5.  * In no event will the authors be held liable for any damages arising from
  6.  * the use of this software.
  7.  *
  8.  * Permission is granted to anyone to use this software, alter it and re-
  9.  * distribute it freely for any non-commercial, non-profit purpose subject to
  10.  * the following restrictions:
  11.  *
  12.  *   1. The origin of this software must not be misrepresented; you must not
  13.  *   claim that you wrote the original software. If you use this software in a
  14.  *   product, an acknowledgment in the product documentation would be
  15.  *   appreciated but is not required.
  16.  *
  17.  *   2. Altered source versions must be plainly marked as such, and must not
  18.  *   be misrepresented as being the original software.
  19.  *
  20.  *   3. This notice may not be removed or altered from any distribution.
  21.  *
  22.  *   4. The names of this software and/or it's copyright holders may not be
  23.  *   used to endorse or promote products derived from this software without
  24.  *   specific prior written permission.
  25.  *
  26.  */
  27.  
  28. #include "expr.h"
  29. #include "chnkpool.h"
  30. #include "log.h"
  31.  
  32. #include <stdlib.h>
  33.  
  34.  
  35. static struct chunkpool s_expr_pool[1];
  36.  
  37. void expr_init()
  38. {
  39.     chunkpool_init(s_expr_pool, sizeof(struct expr));
  40. }
  41.  
  42. void expr_free()
  43. {
  44.     chunkpool_free(s_expr_pool);
  45. }
  46.  
  47. void expr_dump(int level, struct expr *e)
  48. {
  49.     switch(e->expr_op)
  50.     {
  51.     case SYMBOL:
  52.         LOG(level, ("expr 0x%08X symref %s\n", (u32)e, e->type.symref));
  53.         break;
  54.     case NUMBER:
  55.         LOG(level, ("expr 0x%08X number %d\n", (u32)e, e->type.number));
  56.         break;
  57.     case vNEG:
  58.         LOG(level, ("expr 0x%08X unary op %d, referring to 0x%08X\n",
  59.                     (u32)e, e->expr_op, (u32)e->type.arg1));
  60.     case LNOT:
  61.         LOG(level, ("expr 0x%08X unary op %d, referring to 0x%08X\n",
  62.                     (u32)e, e->expr_op, (u32)e->type.arg1));
  63.         break;
  64.     default:
  65.         LOG(level, ("expr 0x%08X binary op %d, arg1 0x%08X, arg2 0x%08X\n",
  66.                     (u32)e, e->expr_op, (u32)e->type.arg1, (u32)e->expr_arg2));
  67.  
  68.     }
  69. }
  70.  
  71. struct expr *new_expr_op1(i16 op, struct expr *arg)
  72. {
  73.     struct expr *val;
  74.  
  75.     if(op != vNEG && op != LNOT)
  76.     {
  77.         /* error, invalid unary operator  */
  78.         LOG(LOG_ERROR, ("%d not allowed as unary operator\n", op));
  79.         exit(1);
  80.     }
  81.  
  82.     val = chunkpool_malloc(s_expr_pool);
  83.     val->expr_op = op;
  84.     val->type.arg1 = arg;
  85.  
  86.     expr_dump(LOG_DEBUG, val);
  87.  
  88.     return val;
  89. }
  90.  
  91. struct expr *new_expr_op2(i16 op, struct expr *arg1, struct expr *arg2)
  92. {
  93.     struct expr *val;
  94.  
  95.     if(op == vNEG ||
  96.        op == LNOT ||
  97.        op == NUMBER ||
  98.        op == SYMBOL)
  99.     {
  100.         /* error, invalid binary operator  */
  101.         printf("op %d, vNEG %d, NUMBER %d, SYMBOL %d\n", op, vNEG, NUMBER, SYMBOL);
  102.         LOG(LOG_ERROR, ("%d not allowed as binary operator\n", op));
  103.         exit(1);
  104.     }
  105.  
  106.     val = chunkpool_malloc(s_expr_pool);
  107.     val->expr_op = op;
  108.     val->type.arg1 = arg1;
  109.     val->expr_arg2 = arg2;
  110.  
  111.     expr_dump(LOG_DEBUG, val);
  112.  
  113.     return val;
  114. }
  115.  
  116. struct expr *new_expr_symref(const char *symbol)
  117. {
  118.     struct expr *val;
  119.  
  120.     val = chunkpool_malloc(s_expr_pool);
  121.     val->expr_op = SYMBOL;
  122.     val->type.symref = symbol;
  123.  
  124.     expr_dump(LOG_DEBUG, val);
  125.  
  126.     return val;
  127. }
  128.  
  129. struct expr *new_expr_number(i32 number)
  130. {
  131.     struct expr *val;
  132.  
  133.     LOG(LOG_DEBUG, ("creating new number %d\n", number));
  134.  
  135.     val = chunkpool_malloc(s_expr_pool);
  136.     val->expr_op = NUMBER;
  137.     val->type.number = number;
  138.  
  139.     expr_dump(LOG_DEBUG, val);
  140.  
  141.     return val;
  142. }
  143.