source: trunk/Compiler/ModelicaCBackEnd/templates/ceval_external_template.c @ 11746

Last change on this file since 11746 was 11746, checked in by Christian Andersson, 14 months ago

Made the test TestAssertEqu3 more robust. Related to ticket:5690

File size: 5.0 KB
Line 
1/*
2    Copyright (C) 2015-2018 Modelon AB
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the Common Public License as published by
6    IBM, version 1.0 of the License.
7
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY. See the Common Public License for more details.
10
11    You should have received a copy of the Common Public License
12    along with this program. If not, see
13    <http://www.ibm.com/developerworks/library/os-cpl.html/>.
14*/
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include "jmi.h"
20#include "jmi_dyn_mem.h"
21#include "ModelicaUtilities.h"
22#include <fcntl.h>
23
24$ECE_external_includes$
25 
26/* Manual debugging */
27#define JMCEVAL_DEBUG 0
28#define JMCEVAL_DBGP(x) if (JMCEVAL_DEBUG) { printf(x); fflush(stdout);}
29
30/* Format specifier when printing jmi_real_t */
31#define JMCEVAL_realFormat "%.16f"
32
33/* Used record definitions */
34$ECE_record_definitions$
35
36/* Parses ND dimensions into dimension buffer d*/
37#define JMCEVAL_parseArrayDims(ND) \
38    for (di = 0; di < ND; di++) { scanf("%d",&d[di]); }
39
40/* Parse/print basic types */
41double JMCEVAL_parseReal() {
42    /* Char buffer when reading jmi_real_t. This is necessary
43       since "%lf" is not allowed in c89. */
44    char buff[32];
45    JMCEVAL_DBGP("Parse number: "); 
46    scanf("%s",buff);
47    return strtod(buff, 0);
48}
49
50void JMCEVAL_printReal(double x) {
51    printf(JMCEVAL_realFormat, x); \
52    printf("\n"); \
53    fflush(stdout); \
54}
55
56char* JMCEVAL_parseString() {
57    int d[1];
58    char* str;
59    size_t si,di;
60    JMCEVAL_parseArrayDims(1);
61    getchar();
62    str = ModelicaAllocateString(d[0]);
63    JMCEVAL_DBGP("Parse string: ");
64    for (si = 0; si < d[0]; si++) str[si] = getchar();
65    str[d[0]] = '\0';
66    return str;
67}
68
69void JMCEVAL_printString(const char* str) {
70    printf("%u\n%s\n", (unsigned)strlen(str), str);
71    fflush(stdout);
72}
73
74#define JMCEVAL_parseInteger()  JMCEVAL_parseReal()
75#define JMCEVAL_parseBoolean()  JMCEVAL_parseInteger()
76#define JMCEVAL_parseEnum()     JMCEVAL_parseInteger()
77#define JMCEVAL_printInteger(X) JMCEVAL_printReal(X)
78#define JMCEVAL_printBoolean(X) JMCEVAL_printInteger(X)
79#define JMCEVAL_printEnum(X)    JMCEVAL_printInteger(X)
80#define JMCEVAL_parse(TYPE, X)  X = JMCEVAL_parse##TYPE()
81#define JMCEVAL_print(TYPE, X)  JMCEVAL_print##TYPE(X)
82
83/* Parse/print arrays */
84#define JMCEVAL_parseArray(TYPE,ARR) for (vi = 1; vi <= ARR->num_elems; vi++) { JMCEVAL_parse(TYPE, jmi_array_ref_1(ARR,vi)); }
85#define JMCEVAL_printArray(TYPE,ARR) for (vi = 1; vi <= ARR->num_elems; vi++) { JMCEVAL_print(TYPE, jmi_array_val_1(ARR,vi)); }
86
87/* Used by ModelicaUtilities */
88void jmi_global_log(int warning, const char* name, const char* fmt, const char* value)
89{
90    printf("LOG\n");
91    JMCEVAL_printInteger((double)warning);
92    JMCEVAL_printString(name);
93    JMCEVAL_printString(fmt);
94    JMCEVAL_printString(value);
95}
96
97jmp_buf jmceval_try_location;
98
99#define JMCEVAL_try() (setjmp(jmceval_try_location) == 0)
100
101void jmi_throw()
102{
103    longjmp(jmceval_try_location, 1);
104}
105
106jmi_dynamic_function_memory_t* dyn_fcn_mem = NULL;
107
108jmi_dynamic_function_memory_t* jmi_dynamic_function_memory() {
109    if (dyn_fcn_mem == NULL) { dyn_fcn_mem = jmi_dynamic_function_pool_create(1024*1024); }
110    return dyn_fcn_mem;
111}
112
113void* jmi_global_calloc(size_t n, size_t s)
114{
115    return jmi_dynamic_function_pool_direct_alloc(dyn_fcn_mem, n*s, 1);
116}
117
118void JMCEVAL_setup() {
119#ifdef _WIN32
120    /* Prevent win from translating \n to \r\n */
121    _setmode(fileno(stdout), _O_BINARY);
122#endif
123}
124
125int JMCEVAL_cont(const char* word) {
126    char l[10];
127    char* s = fgets(l, 10, stdin);
128    if (strlen(s) == 1) {
129        s = fgets(l, 10, stdin); /* Extra call to fix stray newline */
130    }
131    if (s == NULL) {
132        exit(2);
133    }
134    if (strlen(s) == strlen(word)) {
135        return strncmp(l, word, strlen(word)) == 0;
136    }
137    return 0;
138}
139
140void JMCEVAL_check(const char* str) {
141    printf("%s\n",str);
142    fflush(stdout);
143}
144
145void JMCEVAL_failed() {
146    JMCEVAL_check("ABORT");
147}
148
149/* Main */
150int main(int argc, const char* argv[])
151{
152    /* Size buffer for reading array dimensions */
153    int d[25];
154   
155    /* Indices for parsing/printing vars, dimensions */
156    size_t vi,di;
157   
158$ECE_decl$
159$ECE_setup_decl$
160
161    JMCEVAL_setup(); /* This needs to happen first */
162
163    JMCEVAL_check("START");
164    if (JMCEVAL_try()) {
165        JMI_DYNAMIC_INIT()
166        /* Init phase */
167$ECE_setup_init$
168        JMI_DYNAMIC_FREE()
169    } else {
170        JMCEVAL_failed();
171    }
172   
173    JMCEVAL_check("READY");
174    while (JMCEVAL_cont("EVAL\n")) {
175        JMI_DYNAMIC_INIT()
176$ECE_calc_decl$
177$ECE_calc_init$
178        JMCEVAL_check("CALC");
179        if (JMCEVAL_try()) {
180            /* Calc phase */
181$ECE_calc$
182        } else {
183            JMCEVAL_failed();
184        }
185$ECE_calc_free$
186        JMI_DYNAMIC_FREE()
187        JMCEVAL_check("READY");
188    }
189
190    if (JMCEVAL_try()) {
191        /* End phase */
192$ECE_free$
193$ECE_setup_free$
194    } else {
195        JMCEVAL_failed();
196    }
197    jmi_dynamic_function_pool_destroy(dyn_fcn_mem);
198    JMCEVAL_check("END");
199    return 0;
200}
201
Note: See TracBrowser for help on using the repository browser.