Statistiques
| Révision :

root / sollyaIntegration-4.0 / src / createObj-01.c @ 34

Historique | Voir | Annoter | Télécharger (4,93 ko)

1
/** @file createObj-01.c
2
 * CreateObj-01: create a function for an expression given on the command line.
3
 *
4
 * @author S.T.
5
 * @date 2012-11-21
6
 *
7
 */
8
/******************************************************************************/
9
/* Headers, applying the "particular to general" convention.*/
10

    
11
#include "pobyso4/pobyso.h"
12

    
13
/* includes of local headers */
14

    
15
/* includes of project headers */
16

    
17
/* includes of system headers */
18
#include <gmp.h>
19
#include <sollya.h>
20
#include <setjmp.h>
21
#include <string.h>
22
/* Other declarations */
23

    
24
/* Internal prototypes */
25

    
26
/* Types, constants and macros definitions */
27
#define NUM_ARGS 3
28
#define VARIABLE_NAME_STRING "x"
29
#define RADIX 10
30
/* Global variables */
31

    
32
/* Functions */
33
int
34
main(int argc, char** argv)
35
{
36
  //jmp_buf recover;
37
  sollya_obj_t functionObj      = NULL;
38
  sollya_obj_t curVerbLevel     = NULL;
39
  char *freeVariableName        = NULL;
40
  char *functionString          = NULL;
41
  char *initialFreeVariableName = NULL;
42
  char buffer[20];
43

    
44

    
45
  /* check the command line arguments */
46
  if (argc < NUM_ARGS)
47
  {
48
    fprintf(stderr,
49
            "\n\nusage: %s expressionString freeVariableName\n\n",
50
            argv[0]);
51
    fprintf(stderr,
52
            "\texpressionString: the expression to create a node from.\n");
53
    fprintf(stderr,
54
            "\t\tmust be in quotes (e. g. \"cos(x)\");\n");
55
    fprintf(stderr,
56
            "\tfreeVariableName: the name of the free variable;\n");
57
    fprintf(stderr,
58
            "\t\t(e. g. x (no quotes needed));\n");
59
    fprintf(stderr,"\n\n");
60
    return(1);
61
  }
62

    
63
  buffer[0] ='\0';
64

    
65
  functionString    = argv[1];
66
  freeVariableName  = argv[2];
67

    
68
  /* Debug printing. */
69
  fprintf(stdout,
70
          "%s: function: %s\n", argv[0], functionString);
71
  fprintf(stdout,
72
          "%s: variable name: %s\n", argv[0], freeVariableName);
73
  fprintf(stdout, "\n");
74
  /* Initialize Sollya. */
75
  sollya_lib_init();
76

    
77
  initialFreeVariableName = sollya_lib_get_free_variable_name();
78
  if (initialFreeVariableName == NULL)
79
  {
80
    fprintf(stderr, "%s: could not get the name of the free variable. Aborting!\n",
81
                    argv[0]);
82
    sollya_lib_close();
83
    return(1);
84

    
85
  }
86

    
87
  fprintf(stderr, "%s: name of variable (before any assignment): %s\n",
88
                  argv[0],
89
                  initialFreeVariableName);
90

    
91
  /* Set the free variable name. */
92
  sollya_lib_name_free_variable(freeVariableName);
93
  if (strcmp(sollya_lib_get_free_variable_name(), freeVariableName))
94
  {
95
    fprintf(stderr, "Could not set \"%s\" as the variable name. Aborting!\n",
96
                      freeVariableName);
97
    sollya_lib_close();
98
    return(1);
99
  }
100
  /* Parse the function string. */
101
  pobyso_set_verbosity_off(&curVerbLevel);
102
  functionObj = pobyso_parse_string(functionString);
103
  if (sollya_lib_obj_is_error(functionObj))
104
  {
105
    fprintf(stderr, "%s: could not parse \"%s\". Aborting!\n", argv[0], functionString);
106
    sollya_lib_close();
107
    return(1);
108
  }
109
  sollya_lib_fprintf(stdout, "%s: function: %b\n", argv[0], functionObj );
110
  pobyso_autoprint(functionObj, NULL);
111
  sollya_lib_clear_obj(functionObj);
112

    
113
  if (sollya_lib_obj_is_function(curVerbLevel))
114
  {
115
    pobyso_autoprint(curVerbLevel, NULL);
116
    fprintf(stdout, " is a function.\n");
117
  }
118
  pobyso_autoprint(curVerbLevel, NULL);
119
  pobyso_set_verbosity_to(curVerbLevel);
120
  sollya_lib_clear_obj(curVerbLevel);
121

    
122
  strncat(buffer, functionString, strlen(functionString) - 1);
123
  functionObj = pobyso_parse_string(buffer);
124
  if (sollya_lib_obj_is_error(functionObj))
125
  {
126
    fprintf(stderr, "%s: could not parse \"%s\". Aborting!\n", argv[0], functionString);
127
    sollya_lib_close();
128
    return(1);
129
  }
130
  /* Attic from previous versions. */
131
#if 0
132
  fprintf(stderr, "Name of variable: %s\n", getNameOfVariable());
133
  if (functionNode->nodeType == VARIABLE)
134
  {
135
    fprintf(stderr, "Node type is VARIABLE.\n");
136
    if (getNameOfVariable != NULL)
137
    {
138
      if (strcmp(freeVariableName, getNameOfVariable()))
139
      {
140
        fprintf(stderr, "Could not parse %s. Aborting!\n", functionString);
141
        finishTool();
142
        return(1);
143
      }
144
    }
145
  }
146

147
  variableNode      = makeVariable();
148

149
  powNode = makePow(variableNode, makeConstantDouble(3.0));
150
  functionDiffNode  = differentiate(functionNode);
151
  fprintf(stderr, "\tfunctionNode tree...\n");
152
  fprintTree(stderr, functionNode);
153
  fprintf(stderr, "\n");
154
  fprintf(stderr, "\t...end functionNode tree.\n");
155
  fprintf(stderr, "\n");
156
  fprintf(stderr, "\tfunctionDiffNode tree...\n");
157
  fprintTree(stderr,functionDiffNode);
158
  fprintf(stderr, "\n");
159
  fprintf(stderr, "\t...end functionDiffNode tree.\n");
160
  fprintf(stderr, "\n\tvariableNode tree...\n");
161
  fprintTree(stderr, variableNode);
162
  fprintf(stderr, "\n");
163
  fprintf(stderr, "\t...end variableNode tree.\n");
164
  fprintf(stderr, "\n\tpowNode tree...\n");
165
  fprintTree(stderr, powNode);
166
  fprintf(stderr, "\n");
167
  fprintf(stderr, "\t...end powNode tree.\n");
168

169
  free_memory(variableNode);
170
  free_memory(functionNode);
171
  free_memory(functionDiffNode);
172
#endif
173

    
174
  sollya_lib_close();
175
  return(0);
176
} /* End main */