diff --git a/.github/workflows/fix-indent.yml b/.github/workflows/fix-indent.yml deleted file mode 100644 index 7e2ad76..0000000 --- a/.github/workflows/fix-indent.yml +++ /dev/null @@ -1,24 +0,0 @@ -name: Run clang-format Linter - -on: [push,workflow_dispatch] - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - uses: DoozyX/clang-format-lint-action@v0.18.1 - with: - source: '.' - exclude: './lib' - extensions: 'hpp,cpp,h,c,ino,sc' - clangFormatVersion: 16 - inplace: True - - uses: EndBug/add-and-commit@v9 - with: - # author_name: Clang Robot - # author_email: robot@example.com - message: 'Committing clang-format changes' - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/examples/FastLED_example/FastLED_example.ino b/examples/FastLED_example/FastLED_example.ino index 08b1329..acf04f8 100644 --- a/examples/FastLED_example/FastLED_example.ino +++ b/examples/FastLED_example/FastLED_example.ino @@ -1,13 +1,11 @@ #define USE_FASTLED -#include "FastLED.h" #include "ESPLiveScript.h" +#include "FastLED.h" #define NUM_LEDS 256 #define DATA_PIN 15 CRGB leds[NUM_LEDS]; -void show() { - FastLED.show(); -} +void show() { FastLED.show(); } string script = R"EOF( void main() { diff --git a/examples/executebinary/executebinary.ino b/examples/executebinary/executebinary.ino index 2278ab8..b9a3fd3 100644 --- a/examples/executebinary/executebinary.ino +++ b/examples/executebinary/executebinary.ino @@ -1,30 +1,25 @@ #include "ESPLivescriptRuntime.h" -#include #include "FS.h" +#include -void setup() -{ - // put your setup code here, to run once: - Serial.begin(115200); +void setup() { + // put your setup code here, to run once: + Serial.begin(115200); - Binary bin2; - LittleFS.begin(true); + Binary bin2; + LittleFS.begin(true); - loadBinary("/test.bin", LittleFS, &bin2); - Executable ex; - if (bin2.error.error) - { - Serial.printf("%s\n", bin2.error.error_message.c_str()); - } - else - { - ex.createExecutableFromBinary(&bin2); - if (ex.isExeExists()) - ex.execute("main"); - } + loadBinary("/test.bin", LittleFS, &bin2); + Executable ex; + if (bin2.error.error) { + Serial.printf("%s\n", bin2.error.error_message.c_str()); + } else { + ex.createExecutableFromBinary(&bin2); + if (ex.isExeExists()) + ex.execute("main"); + } } -void loop() -{ - // put your main code here, to run repeatedly: +void loop() { + // put your main code here, to run repeatedly: } diff --git a/examples/externalvariables/externalvariables.ino b/examples/externalvariables/externalvariables.ino index c2b87bd..02f430c 100644 --- a/examples/externalvariables/externalvariables.ino +++ b/examples/externalvariables/externalvariables.ino @@ -20,8 +20,7 @@ void main() int variable = 0; uint16_t _array[10]; -void setup() -{ +void setup() { // put your setup code here, to run once: Serial.begin(115200); @@ -29,8 +28,7 @@ void setup() addExternalVariable("value", "int", "", (void *)&variable); addExternalVariable("array", "int *", "", (void *)_array); Executable exec = p.parseScript(&script); - if (exec.isExeExists()) - { + if (exec.isExeExists()) { variable = 5; exec.execute("main"); @@ -41,14 +39,12 @@ void setup() exec.execute("change"); printf("new value:%d\n", variable); exec.execute("fillArray"); - for (int i = 0; i < 10; i++) - { + for (int i = 0; i < 10; i++) { printf("%d:%d\n", i, _array[i]); } } } -void loop() -{ +void loop() { // put your main code here, to run repeatedly: } diff --git a/examples/factorial/factorial.ino b/examples/factorial/factorial.ino index 29f39f6..2f9e12f 100644 --- a/examples/factorial/factorial.ino +++ b/examples/factorial/factorial.ino @@ -16,28 +16,25 @@ void main(int g) } )EOF"; -void setup() -{ - // put your setup code here, to run once: - Serial.begin(115200); +void setup() { + // put your setup code here, to run once: + Serial.begin(115200); - Parser p; - Executable exec = p.parseScript(&script); - if (exec.isExeExists()) - { - Arguments args; - args.add(5); - exec.execute("main", args); - args.clear(); - args.add(6); - exec.execute("main", args); - args.clear(); - args.add(7); - exec.execute("main", args); - } + Parser p; + Executable exec = p.parseScript(&script); + if (exec.isExeExists()) { + Arguments args; + args.add(5); + exec.execute("main", args); + args.clear(); + args.add(6); + exec.execute("main", args); + args.clear(); + args.add(7); + exec.execute("main", args); + } } -void loop() -{ - // put your main code here, to run repeatedly: +void loop() { + // put your main code here, to run repeatedly: } diff --git a/examples/firstlight/firstlight.ino b/examples/firstlight/firstlight.ino index 1e4c538..d2cd0dc 100644 --- a/examples/firstlight/firstlight.ino +++ b/examples/firstlight/firstlight.ino @@ -9,20 +9,17 @@ for(int i=0;i<20;i++) } } )EOF"; -void setup() -{ +void setup() { // put your setup code here, to run once: Serial.begin(115200); Parser p; Executable exec = p.parseScript(&script); - if (exec.isExeExists()) - { + if (exec.isExeExists()) { exec.execute("main"); } } -void loop() -{ +void loop() { // put your main code here, to run repeatedly: } diff --git a/examples/jsonexample/jsonexample.ino b/examples/jsonexample/jsonexample.ino index 60548c1..23cd0a7 100644 --- a/examples/jsonexample/jsonexample.ino +++ b/examples/jsonexample/jsonexample.ino @@ -22,27 +22,22 @@ void main() } )EOF"; -void setup() -{ - // put your setup code here, to run once: - Serial.begin(115200); +void setup() { + // put your setup code here, to run once: + Serial.begin(115200); - Parser p; - Executable exec = p.parseScript(&script); - printf("copmùpile done\n"); - if (exec.isExeExists()) - { + Parser p; + Executable exec = p.parseScript(&script); + printf("copmùpile done\n"); + if (exec.isExeExists()) { - exec.execute("main", "{\"dp\":{\"value\":6,\"test\":12},\"dp2\":4.5}"); - exec.execute("main", "{\"dp\":{\"value\":10,\"test\":20},\"dp2\":7.5854}"); - } - else - { - printf("%s\n", exec.error.error_message.c_str()); - } + exec.execute("main", "{\"dp\":{\"value\":6,\"test\":12},\"dp2\":4.5}"); + exec.execute("main", "{\"dp\":{\"value\":10,\"test\":20},\"dp2\":7.5854}"); + } else { + printf("%s\n", exec.error.error_message.c_str()); + } } -void loop() -{ - // put your main code here, to run repeatedly: +void loop() { + // put your main code here, to run repeatedly: } diff --git a/examples/pininterrupt/pininterrupt.ino b/examples/pininterrupt/pininterrupt.ino index df40f67..6c04c16 100644 --- a/examples/pininterrupt/pininterrupt.ino +++ b/examples/pininterrupt/pininterrupt.ino @@ -1,8 +1,7 @@ #include "ESPLiveScript.h" #include "driver/gpio.h" -typedef struct -{ +typedef struct { Executable *ptr; char *name; uint32_t last; @@ -10,24 +9,23 @@ typedef struct } call_isr; call_isr isr_struct; - static void IRAM_ATTR gpio_isr_handler(void *args) { call_isr *ll = (call_isr *)args; unsigned long currentTime = millis(); if ((currentTime - ll->last) > 100) { ll->ptr->executeOnly(string(ll->name)); - ll->last = currentTime; // Update the last interrupt time + ll->last = currentTime; // Update the last interrupt time } } void setup_gpio_interrupt(Executable *ptr, char *str, int pin_num) { printf("Setting up the interrupt\n"); gpio_config_t io_conf = { - .pin_bit_mask = (1ULL << pin_num), - .mode = GPIO_MODE_INPUT, - .pull_up_en = GPIO_PULLUP_ENABLE, - .pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down - .intr_type = GPIO_INTR_NEGEDGE, + .pin_bit_mask = (1ULL << pin_num), + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down + .intr_type = GPIO_INTR_NEGEDGE, }; gpio_config(&io_conf); @@ -58,14 +56,14 @@ void main() void setup() { Serial.begin(115200); - addExternalFunction("pinInterrupt", "void", "uint32_t,char *,int", (void *)setup_gpio_interrupt); + addExternalFunction("pinInterrupt", "void", "uint32_t,char *,int", + (void *)setup_gpio_interrupt); Parser p; Executable exec = p.parseScript(&script); if (exec.isExeExists()) { exec.execute("main"); - } - else { - printf("Error:%s\n",exec.error.error_message.c_str()); + } else { + printf("Error:%s\n", exec.error.error_message.c_str()); } // put your setup code here, to run once: } diff --git a/examples/savebinary/savebinary.ino b/examples/savebinary/savebinary.ino index 95d4cd9..98b4973 100644 --- a/examples/savebinary/savebinary.ino +++ b/examples/savebinary/savebinary.ino @@ -1,6 +1,6 @@ #include "ESPLiveScript.h" -#include #include "FS.h" +#include string script = R"EOF( void main() { @@ -20,20 +20,19 @@ void setup() { Binary bin2; LittleFS.begin(true); if (!bin.error.error) { - //display the content of the bin - // displayBinary(&bin); - //save the binary + // display the content of the bin + // displayBinary(&bin); + // save the binary saveBinary("/test.bin", LittleFS, &bin); freeBinary(&bin); loadBinary("/test.bin", LittleFS, &bin2); Executable ex; - + ex.createExecutableFromBinary(&bin2); if (ex.isExeExists()) ex.execute("main"); - } else { printf("%s\n\r", bin.error.error_message.c_str()); } diff --git a/examples/structure/structure.ino b/examples/structure/structure.ino index a9858aa..0490428 100644 --- a/examples/structure/structure.ino +++ b/examples/structure/structure.ino @@ -25,20 +25,17 @@ void main() } )EOF"; -void setup() -{ +void setup() { // put your setup code here, to run once: Serial.begin(115200); Parser p; Executable exec = p.parseScript(&script); - if (exec.isExeExists()) - { + if (exec.isExeExists()) { exec.execute("main"); } } -void loop() -{ +void loop() { // put your main code here, to run repeatedly: } diff --git a/sc_examples/Madel.sc b/sc_examples/Madel.sc index 52fba15..64aa113 100644 --- a/sc_examples/Madel.sc +++ b/sc_examples/Madel.sc @@ -1,49 +1,39 @@ -#define maxIterations 50 -#define width 128 -#define height 96 +#define maxIterations 50 +#define width 128 +#define height 96 -external CRGB leds[height,width]; -void render2D(int x1, int y1) -{ - float x0 = (x1*2.7)/ width-2.1 ; - float y0 = (y1*2.2)/height - 1.1; - float x = 0 ; - float y = 0; - float x2=0; - float y2=0; - int iter=0; - while (iter < maxIterations && x2+y2<=4) - { +external CRGB leds[height, width]; +void render2D(int x1, int y1) { + float x0 = (x1 * 2.7) / width - 2.1; + float y0 = (y1 * 2.2) / height - 1.1; + float x = 0; + float y = 0; + float x2 = 0; + float y2 = 0; + int iter = 0; + while (iter < maxIterations && x2 + y2 <= 4) { - float fX = x2 - y2 + x0; - y = 2 * x * y + y0; - x = fX; - x2 = x^2; - y2 = y^2; - iter++; - - } - if (iter < maxIterations) - { - leds[y1*width+ x1] = hsv((iter*255 / maxIterations) , 255, 255); - } - else - { - leds[y1*width+x1] = CRGB(0, 0, 0); - } + float fX = x2 - y2 + x0; + y = 2 * x * y + y0; + x = fX; + x2 = x ^ 2; + y2 = y ^ 2; + iter++; + } + if (iter < maxIterations) { + leds[y1 * width + x1] = hsv((iter * 255 / maxIterations), 255, 255); + } else { + leds[y1 * width + x1] = CRGB(0, 0, 0); + } } -void main() -{ - while (true) - { - for (int i = 0; i < width; i++) - { - for (int j = 0; j < height; j++) - { - render2D(i, j); - } +void main() { + while (true) { + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + render2D(i, j); } - show(); - } + } + show(); + } } \ No newline at end of file diff --git a/sc_examples/animwle.sc b/sc_examples/animwle.sc index 04016ac..df56ab6 100644 --- a/sc_examples/animwle.sc +++ b/sc_examples/animwle.sc @@ -5,31 +5,29 @@ // Version Author Date // 1.0.0 JEM(ZRanger1) 12/08/2020 - -#define maxIterations 15 -#define width 128 -#define height 96 +#define maxIterations 15 +#define width 128 +#define height 96 #define scale 0.5 uint32_t __deltamillis[1]; uint32_t __baseTime[1]; -__ASM__ uint32_t millis() -{ - "entry a1,32" - "l32r a5,@___baseTime" - "l32i a14,a5,0" - "rsr a13,234" - "l32r a4,@___deltamillis" - "s32i a13,a4,0" - "sub a13,a13,a14" - "movi a14,240" - "quou a13,a13,a14" - "movi a14,1000" - "quou a2,a13,a14" - // "l32r a4,@_stackr" - //"s32i a13,a4,0" - "retw.n" +__ASM__ uint32_t millis() { + "entry a1,32" + "l32r a5,@___baseTime" + "l32i a14,a5,0" + "rsr a13,234" + "l32r a4,@___deltamillis" + "s32i a13,a4,0" + "sub a13,a13,a14" + "movi a14,240" + "quou a13,a13,a14" + "movi a14,1000" + "quou a2,a13,a14" + // "l32r a4,@_stackr" + //"s32i a13,a4,0" + "retw.n" } float cR = -0.94299; @@ -40,80 +38,66 @@ float cX, cY, fX, fY; // timers used to animate movement and color float t1, t2; -float fmod(float a, float b) -{ - int tmp = a / b; - return a - b * tmp; +float fmod(float a, float b) { + int tmp = a / b; + return a - b * tmp; } -float triangle(float h) -{ - float res = fabs(fmod(2 * h, 2.0) - 1.0); - return 1 - res; +float triangle(float h) { + float res = fabs(fmod(2 * h, 2.0) - 1.0); + return 1 - res; } -float time(float par1) -{ - float myVal = millis(); - myVal = myVal / 10000 / par1; - myVal = fmod(myVal, 1.0); - return myVal; +float time(float par1) { + float myVal = millis(); + myVal = myVal / 10000 / par1; + myVal = fmod(myVal, 1.0); + return myVal; } -void beforeRender() -{ - t1 = (triangle(time(0.2)) - 0.5) * 2.4; - t2 = time(0.05); - cX = cR + t1; - cY = cI + (t1 / 2.5); +void beforeRender() { + t1 = (triangle(time(0.2)) - 0.5) * 2.4; + t2 = time(0.05); + cX = cR + t1; + cY = cI + (t1 / 2.5); } -void render2D(int x1, int y1) -{ - float x = (x1 / width - 0.5) / scale; - float y = (y1 / height - 0.5) / scale; - int iter; - for (iter = 0; iter < maxIterations; iter++) - { - float x2 = x ^ 2; - float y2 = y ^ 2; - if ((int)(x2 + y2) >= 4) - { - break; - } - fX = x2 - y2 + cX; - fY = 2 * x * y + cY; - x = fX; - y = fY; - } +void render2D(int x1, int y1) { + float x = (x1 / width - 0.5) / scale; + float y = (y1 / height - 0.5) / scale; + int iter; + for (iter = 0; iter < maxIterations; iter++) { + float x2 = x ^ 2; + float y2 = y ^ 2; + if ((int)(x2 + y2) >= 4) { + break; + } + fX = x2 - y2 + cX; + fY = 2 * x * y + cY; + x = fX; + y = fY; + } - if (iter < maxIterations) - { - leds[width * y1 + x1] = hsv((t2 + iter / maxIterations) * 255, 255, 255); - } - else - { - leds[width * y1 + x1] = CRGB(0, 0, 0); - } + if (iter < maxIterations) { + leds[width * y1 + x1] = hsv((t2 + iter / maxIterations) * 255, 255, 255); + } else { + leds[width * y1 + x1] = CRGB(0, 0, 0); + } } -void main() -{ - clear(); - resetStat(); +void main() { + clear(); + resetStat(); - int h = 1; - while (true) - { - beforeRender(); - for (int i = 0; i < width; i++) - { - for (int j = 0; j < height; j++) - { - render2D(i, j); - } + int h = 1; + while (true) { + beforeRender(); + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + render2D(i, j); } - // show(); - sync(); - } + } + // show(); + sync(); + } } \ No newline at end of file diff --git a/sc_examples/balls.sc b/sc_examples/balls.sc index 817a729..641b8bb 100644 --- a/sc_examples/balls.sc +++ b/sc_examples/balls.sc @@ -1,103 +1,87 @@ -import rand -#define max_nb_balls 200 -#define rmax 8 -#define rmin 8 -#define width 128 -#define height 96 -#define panel_width 128 -#define panel_height 96 +import rand +#define max_nb_balls 200 +#define rmax 8 +#define rmin 8 +#define width 128 +#define height 96 +#define panel_width 128 +#define panel_height 96 - -struct ball -{ - float vx, vy, xc, yc, r; - int color; - void drawBall() - { - int startx = xc - r; - int r2 = r * r; - float r4 = r ^ 4; - int starty = yc - r; - int _xc = xc; - int _yc = yc; - for (int i = startx; i <= _xc; i++) - for (int j = starty; j <= _yc; j++) - { - int v; - int distance = (i - xc) ^ 2 + (j - yc) ^ 2; - if (distance <= r2) - { - v = 255 * (1 - distance ^ 2 / (r4)); - CRGB cc = hsv(color, 255, v); - leds[j*panel_width +i] = cc; - leds[j*panel_width+(int)(2 * xc - i)] = cc; - leds[(int)(2 * xc - i) + (int)(2 * yc - j) * panel_width] = cc; - leds[i + (int)(2 * yc - j) * panel_width] = cc; - } - } - } - void updateBall() - { - xc += vx; - yc += vy; - if (xc >= (width - r - 1)) - { - xc = width - r - 1; - vx = -vx; - } - if (xc < (r + 1)) - { - xc = r + 1; - vx = -vx; - } - if (yc >= height - r - 1) - { - yc = height - r - 1; - vy = -vy; - } - if (yc < r + 1) - { - yc = r + 1; - vy = -vy; + struct ball{float vx, vy, xc, yc, r; +int color; +void drawBall() { + int startx = xc - r; + int r2 = r * r; + float r4 = r ^ 4; + int starty = yc - r; + int _xc = xc; + int _yc = yc; + for (int i = startx; i <= _xc; i++) + for (int j = starty; j <= _yc; j++) { + int v; + int distance = (i - xc) ^ 2 + (j - yc) ^ 2; + if (distance <= r2) { + v = 255 * (1 - distance ^ 2 / (r4)); + CRGB cc = hsv(color, 255, v); + leds[j * panel_width + i] = cc; + leds[j * panel_width + (int)(2 * xc - i)] = cc; + leds[(int)(2 * xc - i) + (int)(2 * yc - j) * panel_width] = cc; + leds[i + (int)(2 * yc - j) * panel_width] = cc; } + } +} +void updateBall() { + xc += vx; + yc += vy; + if (xc >= (width - r - 1)) { + xc = width - r - 1; + vx = -vx; + } + if (xc < (r + 1)) { + xc = r + 1; + vx = -vx; + } + if (yc >= height - r - 1) { + yc = height - r - 1; + vy = -vy; + } + if (yc < r + 1) { + yc = r + 1; + vy = -vy; + } - drawBall(); - } - ball() - { - vx = rand(300) / 255 + 0.7; - vy = rand(280) / 255 + 0.5; - r = (rmax - rmin) * (rand(280) / 180) + rmin; - xc = width / 2 * (rand(280) / 255 + 0.3) + 15; - yc = height / 2 * (rand(280) / 255 + 0.3) + 15; - color = rand(255); - } + drawBall(); +} +ball() { + vx = rand(300) / 255 + 0.7; + vy = rand(280) / 255 + 0.5; + r = (rmax - rmin) * (rand(280) / 180) + rmin; + xc = width / 2 * (rand(280) / 255 + 0.3) + 15; + yc = height / 2 * (rand(280) / 255 + 0.3) + 15; + color = rand(255); +} } ball Balls[max_nb_balls]; -void main(int num) -{ - if (num > max_nb_balls) - { - num = max_nb_balls; - } - if (num <= 0) - { - num = 1; - } - printfln("numberof balls:%d",num); - resetStat(); - uint32_t h; - while (true) - { - for (int i = 0; i < width; i++) - for (int j = 0; j < height; j++) - leds[j*panel_width+ i] = hsv(i + h + j, 255, 180); +void main(int num) { + if (num > max_nb_balls) { + num = max_nb_balls; + } + if (num <= 0) { + num = 1; + } + printfln("numberof balls:%d", num); + resetStat(); + uint32_t h; + while (true) { + for (int i = 0; i < width; i++) + for (int j = 0; j < height; j++) + leds[j * panel_width + i] = hsv(i + h + j, 255, 180); - for (int i = 0; i < num; i++) - Balls[i].updateBall(); - h++; - sync(); - } + for (int i = 0; i < num; i++) + Balls[i].updateBall(); + h++; + sync(); + } } \ No newline at end of file diff --git a/sc_examples/circles.sc b/sc_examples/circles.sc index 64e92fa..feed5a5 100644 --- a/sc_examples/circles.sc +++ b/sc_examples/circles.sc @@ -1,83 +1,68 @@ -//external CRGB *leds; -//external CRGB *ope; +// external CRGB *leds; +// external CRGB *ope; -//external void clear(); -//external void show(); -//external CRGB hsv(uint8_t h, uint8_t s, uint8_t v); +// external void clear(); +// external void show(); +// external CRGB hsv(uint8_t h, uint8_t s, uint8_t v); -void PixelOn(int x,int y,int color) -{ -if(x<0) -{ - return; -} -if(y<0) -{ - return; -} -leds[(y%96)*128+(x%128)]=hsv(color,255,255); -} -void BresenhamCircle(int xc,int yc,int r) -{ - - int x; - int y; - int p; - if (r<0) - { - return; - } - x=0; - y=r; - - - PixelOn(xc+x,yc-y,xc); - PixelOn(xc-x,yc-y,xc); - PixelOn(xc+x,yc+y,xc); - PixelOn(xc-x,yc+y,xc); - PixelOn(xc+y,yc-x,xc); - PixelOn(xc-y,yc-x,xc); - PixelOn(xc+y,yc+x,xc); - PixelOn(xc-y,yc+x,xc); - - p=3-(2*r); - - for(x=0;x<=y;x++) - { - if (p<0) - { - p=(p+(4*x)+6); - } - else - { - y=y-1; - p=p+((4*(x-y)+10)); - } - - PixelOn(xc+x,yc-y,xc); - PixelOn(xc-x,yc-y,xc); - PixelOn(xc+x,yc+y,xc); - PixelOn(xc-x,yc+y,xc); - PixelOn(xc+y,yc-x,xc); - PixelOn(xc-y,yc-x,xc); - PixelOn(xc+y,yc+x,xc); - PixelOn(xc-y,yc+x,xc); - } - +void PixelOn(int x, int y, int color) { + if (x < 0) { + return; + } + if (y < 0) { + return; + } + leds[(y % 96) * 128 + (x % 128)] = hsv(color, 255, 255); } +void BresenhamCircle(int xc, int yc, int r) { + int x; + int y; + int p; + if (r < 0) { + return; + } + x = 0; + y = r; -void main() -{ + PixelOn(xc + x, yc - y, xc); + PixelOn(xc - x, yc - y, xc); + PixelOn(xc + x, yc + y, xc); + PixelOn(xc - x, yc + y, xc); + PixelOn(xc + y, yc - x, xc); + PixelOn(xc - y, yc - x, xc); + PixelOn(xc + y, yc + x, xc); + PixelOn(xc - y, yc + x, xc); -int i=0; -while(true) -{ -clear(); -printfln("i:%d",i); -BresenhamCircle(i,48,40); -show(); -i++; + p = 3 - (2 * r); + + for (x = 0; x <= y; x++) { + if (p < 0) { + p = (p + (4 * x) + 6); + } else { + y = y - 1; + p = p + ((4 * (x - y) + 10)); + } + + PixelOn(xc + x, yc - y, xc); + PixelOn(xc - x, yc - y, xc); + PixelOn(xc + x, yc + y, xc); + PixelOn(xc - x, yc + y, xc); + PixelOn(xc + y, yc - x, xc); + PixelOn(xc - y, yc - x, xc); + PixelOn(xc + y, yc + x, xc); + PixelOn(xc - y, yc + x, xc); + } } - + +void main() { + + int i = 0; + while (true) { + clear(); + printfln("i:%d", i); + BresenhamCircle(i, 48, 40); + show(); + i++; + } } \ No newline at end of file diff --git a/sc_examples/dsiplayicon.sc b/sc_examples/dsiplayicon.sc index 5349e59..e92a0ea 100644 --- a/sc_examples/dsiplayicon.sc +++ b/sc_examples/dsiplayicon.sc @@ -1,95 +1,82 @@ -uint8_t ghostp[72]={ - 1,9,9,9,1,9,9,9,1, - 1,1,9,1,1,1,9,1,1, - 1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1, - 1,3,2,1,1,3,2,1,1, - 1,2,2,1,1,2,2,1,9, - 9,1,1,1,1,1,1,1,9, - 9,9,1,1,1,1,1,9,9}; +uint8_t ghostp[72] = {1, 9, 9, 9, 1, 9, 9, 9, 1, 1, 1, 9, 1, 1, 1, 9, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 3, 2, 1, 1, 3, 2, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 9, + 9, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 1, 1, 1, 1, 1, 9, 9}; +CRGB palghost[4] = {CRGB(0, 0, 0), CRGB(255, 0, 0), CRGB(255, 255, 255), + CRGB(0, 0, 255)}; +CRGB palghost2[4] = {CRGB(0, 0, 0), CRGB(255, 0, 255), CRGB(255, 255, 255), + CRGB(0, 0, 255)}; -CRGB palghost[4] = {CRGB(0,0,0), CRGB(255,0,0), CRGB(255,255,255), CRGB(0,0,255)}; -CRGB palghost2[4] = {CRGB(0,0,0), CRGB(255,0,255), CRGB(255,255,255), CRGB(0,0,255)}; - -uint8_t mario[238]={ - 100,3,3,100,100,100,100,100,100,100,100,100,100,100,100,100,100, - 100,3,3,3,2,2,2,2,2,2,100,100,100,100,100,100,100, - 100,100,3,3,2,2,2,2,2,2,2,2,2,2,3,3,100, - 100,100,100,100,2,2,2,2,4,2,2,2,2,2,3,3,100, - 100,5,5,1,1,1,1,2,2,2,2,4,2,2,3,3,100, - 5,5,1,1,1,1,1,2,1,1,1,2,100,100,3,3,100, - 5,5,1,1,1,1,2,1,1,1,2,1,1,100,100,3,100, - 100,100,100,100,100,5,5,5,5,5,5,5,1,1,100,100,100, - 100,100,100,3,3,5,5,5,5,6,6,6,6,1,100,100,100, - 100,100,100,3,5,3,3,5,5,5,6,5,5,5,1,100,100, - 100,100,100,3,5,3,5,5,5,6,5,5,5,1,1,100,100, - 100,100,100,100,3,3,3,5,5,6,5,100,1,1,1,100,100, - 100,100,100,100,1,1,1,1,1,1,1,1,1,5,5,100,100, - 100,100,100,100,100,1,1,1,1,1,100,100,5,5,5,100,100 -}; +uint8_t mario[238] = { + 100, 3, 3, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, + 100, 100, 100, 3, 3, 3, 2, 2, 2, 2, 2, 2, 100, 100, 100, + 100, 100, 100, 100, 100, 100, 3, 3, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 3, 3, 100, 100, 100, 100, 100, 2, 2, 2, 2, 4, + 2, 2, 2, 2, 2, 3, 3, 100, 100, 5, 5, 1, 1, 1, 1, + 2, 2, 2, 2, 4, 2, 2, 3, 3, 100, 5, 5, 1, 1, 1, + 1, 1, 2, 1, 1, 1, 2, 100, 100, 3, 3, 100, 5, 5, 1, + 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 100, 100, 3, 100, 100, + 100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 1, 1, 100, 100, + 100, 100, 100, 100, 3, 3, 5, 5, 5, 5, 6, 6, 6, 6, 1, + 100, 100, 100, 100, 100, 100, 3, 5, 3, 3, 5, 5, 5, 6, 5, + 5, 5, 1, 100, 100, 100, 100, 100, 3, 5, 3, 5, 5, 5, 6, + 5, 5, 5, 1, 1, 100, 100, 100, 100, 100, 100, 3, 3, 3, 5, + 5, 6, 5, 100, 1, 1, 1, 100, 100, 100, 100, 100, 100, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 5, 5, 100, 100, 100, 100, 100, 100, + 100, 1, 1, 1, 1, 1, 100, 100, 5, 5, 5, 100, 100}; uint8_t cerise[196] = { - 100, 100, 100, 4, 4, 4, 4, 100, 100, 100, 100, 100, 100, 100, - 100, 100, 4, 1, 1, 1, 1, 4, 100, 100, 100, 100, 100, 100, - 100, 4, 1, 1, 2, 2, 1, 1, 4, 4, 4, 4, 100, 100, - 100, 4, 1, 1, 1, 1, 2, 1, 4, 1, 1, 1, 4, 100, - 100, 4, 1, 1, 1, 1, 1, 1, 4, 2, 2, 1, 1, 4, - 100, 4, 1, 1, 1, 1, 1, 1, 4, 1, 1, 2, 1, 4, - 100, 100, 4, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 4, - 100, 100, 100, 4, 4, 3, 4, 1, 1, 1, 1, 1, 1, 4, - 100, 100, 100, 4, 3, 4, 100, 0, 1, 1, 1, 1, 4, 100, - 100, 100, 4, 3, 4, 4, 4, 3, 4, 4, 4, 4, 100, 100, - 100, 4, 4, 3, 4, 3, 3, 4, 100, 100, 100, 100, 100, 100, - 4, 3, 3, 3, 3, 4, 4, 100, 100, 100, 100, 100, 100, 100, - 4, 3, 3, 4, 4, 100, 100, 100, 100, 100, 100, 100, 100, 100, - 100, 4, 4, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 -}; - + 100, 100, 100, 4, 4, 4, 4, 100, 100, 100, 100, 100, 100, 100, + 100, 100, 4, 1, 1, 1, 1, 4, 100, 100, 100, 100, 100, 100, + 100, 4, 1, 1, 2, 2, 1, 1, 4, 4, 4, 4, 100, 100, + 100, 4, 1, 1, 1, 1, 2, 1, 4, 1, 1, 1, 4, 100, + 100, 4, 1, 1, 1, 1, 1, 1, 4, 2, 2, 1, 1, 4, + 100, 4, 1, 1, 1, 1, 1, 1, 4, 1, 1, 2, 1, 4, + 100, 100, 4, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 4, + 100, 100, 100, 4, 4, 3, 4, 1, 1, 1, 1, 1, 1, 4, + 100, 100, 100, 4, 3, 4, 100, 0, 1, 1, 1, 1, 4, 100, + 100, 100, 4, 3, 4, 4, 4, 3, 4, 4, 4, 4, 100, 100, + 100, 4, 4, 3, 4, 3, 3, 4, 100, 100, 100, 100, 100, 100, + 4, 3, 3, 3, 3, 4, 4, 100, 100, 100, 100, 100, 100, 100, + 4, 3, 3, 4, 4, 100, 100, 100, 100, 100, 100, 100, 100, 100, + 100, 4, 4, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}; -CRGB palm[7]={CRGB(255,0,0),CRGB(255,0,0), CRGB(0,0,255),CRGB(98,65,7),CRGB(255,255,0),CRGB(238,151,64),CRGB(0,0,0)}; -CRGB palec[5] = {CRGB(0,0,0), CRGB(255,0,0), CRGB(255,255,255), CRGB(0,255,0),CRGB(0,0,0)}; +CRGB palm[7] = {CRGB(255, 0, 0), CRGB(255, 0, 0), CRGB(0, 0, 255), + CRGB(98, 65, 7), CRGB(255, 255, 0), CRGB(238, 151, 64), + CRGB(0, 0, 0)}; +CRGB palec[5] = {CRGB(0, 0, 0), CRGB(255, 0, 0), CRGB(255, 255, 255), + CRGB(0, 255, 0), CRGB(0, 0, 0)}; -void displaypic(int x, int y,char * pic,CRGB *palette, int h, int w) -{ -//printf("x:%d y:%d\n",x,y); +void displaypic(int x, int y, char *pic, CRGB *palette, int h, int w) { + // printf("x:%d y:%d\n",x,y); -for (int i=0;i2) - - return fib(h-1)+fib(h-2); - return 1; -} - +int fib(int h) { + if (h > 2) + return fib(h - 1) + fib(h - 2); + return 1; +} -void main() -{ -int num=40; - int op=45; - uint32_t t1=millis(); - int k=fib(num); - uint32_t t2=millis(); - float t3=(t2-t1); - printfln("fib %d=%d duration %u %dms",num,k,t2-t1,(int)(t3)); +void main() { + int num = 40; + int op = 45; + uint32_t t1 = millis(); + int k = fib(num); + uint32_t t2 = millis(); + float t3 = (t2 - t1); + printfln("fib %d=%d duration %u %dms", num, k, t2 - t1, (int)(t3)); } \ No newline at end of file diff --git a/sc_examples/gameoflife.sc b/sc_examples/gameoflife.sc index 89bef25..6e58e3c 100644 --- a/sc_examples/gameoflife.sc +++ b/sc_examples/gameoflife.sc @@ -1,67 +1,53 @@ - -#define width 128 + +#define width 128 #define height 128 -external CRGB leds[height,width]; -uint8_t copy[height,width]; +external CRGB leds[height, width]; +uint8_t copy[height, width]; uint8_t line1[width]; uint8_t linec[width]; -void create_leds(int o) -{ - CRGB color=hsv(o,255,255); - for (int j = 1; j < height - 1; j++) - { - for (int h = 0; h < width; h++) - { - line1[h] = linec[h]; - linec[h]=copy[j,h]; - } - for (int i = 1; i < width - 1; i++) - { - - int res = linec[i - 1] + linec[i + 1]; - res = res + line1[i - 1] + line1[i + 1] + line1[i]; - res=res+copy[j+1,i-1]+copy[j+1,i+1]+copy[j+1,i]; - bool alive = false; - int l = copy[j,i]; +void create_leds(int o) { + CRGB color = hsv(o, 255, 255); + for (int j = 1; j < height - 1; j++) { + for (int h = 0; h < width; h++) { + line1[h] = linec[h]; + linec[h] = copy[j, h]; + } + for (int i = 1; i < width - 1; i++) { -if(res==3) - alive=true; -else - alive=(l==1 && res==2)?true:false; - if (alive) - { - leds[j,i] = color; - copy[j,i] = 1; - } - else - { - leds[j,i] = CRGB(0, 0, 0); - copy[j,i] = 0; - } - } - } + int res = linec[i - 1] + linec[i + 1]; + res = res + line1[i - 1] + line1[i + 1] + line1[i]; + res = res + copy[j + 1, i - 1] + copy[j + 1, i + 1] + copy[j + 1, i]; + bool alive = false; + int l = copy[j, i]; + if (res == 3) + alive = true; + else + alive = (l == 1 && res == 2) ? true : false; + if (alive) { + leds[j, i] = color; + copy[j, i] = 1; + } else { + leds[j, i] = CRGB(0, 0, 0); + copy[j, i] = 0; + } + } + } } - -int main() -{ - for (int h = 0; h < width * height; h++) - { - copy[h]=0; - } - for (int h = 0; h < height; h++) - { - copy[h,width/2]=1; - - } - clear(); - show(); - for (int i = 0; i <50; i++) - { - create_leds(i); - show(); - } +int main() { + for (int h = 0; h < width * height; h++) { + copy[h] = 0; + } + for (int h = 0; h < height; h++) { + copy[h, width / 2] = 1; + } + clear(); + show(); + for (int i = 0; i < 50; i++) { + create_leds(i); + show(); + } } \ No newline at end of file diff --git a/sc_examples/octo.sc b/sc_examples/octo.sc index c5c04c8..288945c 100644 --- a/sc_examples/octo.sc +++ b/sc_examples/octo.sc @@ -1,57 +1,48 @@ -save_reg -external CRGB *leds; +save_reg external CRGB *leds; external void show(); external CRGB hsv(uint8_t h, uint8_t s, uint8_t v); external uint8_t sin8(uint8_t a); external float hypot(float x, float y); external float atan2(float x, float y); external void resetStat(); -define LED_COLS 128 -define LED_ROWS 96 -define NUM_LEDS 12288 -define PI 3.1415926535 -define panel_width 128 -define speed 1 -define nb_branches 5 -uint8_t C_X, C_Y, mapp; +define LED_COLS 128 define LED_ROWS 96 define NUM_LEDS 12288 define + PI 3.1415926535 define panel_width 128 define speed 1 define + nb_branches 5 uint8_t C_X, + C_Y, mapp; uint8_t rMapRadius[NUM_LEDS]; uint8_t rMapAngle[NUM_LEDS]; -void init() -{ - C_X = LED_COLS / 2; - C_Y = LED_ROWS / 2; - mapp = 255 / LED_COLS; - for (int x = -C_X; x < C_X + (LED_COLS % 2); x++) - { - for (int y = -C_Y; y < C_Y + (LED_ROWS % 2); y++) - { +void init() { + C_X = LED_COLS / 2; + C_Y = LED_ROWS / 2; + mapp = 255 / LED_COLS; + for (int x = -C_X; x < C_X + (LED_COLS % 2); x++) { + for (int y = -C_Y; y < C_Y + (LED_ROWS % 2); y++) { - float h = 128 * (atan2(y, x) / PI); - rMapAngle[(x + C_X) * LED_ROWS + y + C_Y] = h; - h = hypot(x, y) * mapp; - rMapRadius[(x + C_X) * LED_ROWS + y + C_Y] = h; // thanks Sutaburosu - } - } + float h = 128 * (atan2(y, x) / PI); + rMapAngle[(x + C_X) * LED_ROWS + y + C_Y] = h; + h = hypot(x, y) * mapp; + rMapRadius[(x + C_X) * LED_ROWS + y + C_Y] = h; // thanks Sutaburosu + } + } } -void main() -{ - resetStat(); - init(); - uint32_t t; - while (true) - { - for (uint8_t x = 0; x < LED_COLS; x++) - { - for (uint8_t y = 0; y < LED_ROWS; y++) - { - uint8_t angle = rMapAngle[x * LED_ROWS + y]; - uint8_t radius = rMapRadius[x * LED_ROWS + y]; - leds[y * panel_width + x] = hsv(t + radius, 255, sin8(sin8((angle * 4 - radius * mapp) / 4 + t) + angle * nb_branches - 2 * t)); - } +void main() { + resetStat(); + init(); + uint32_t t; + while (true) { + for (uint8_t x = 0; x < LED_COLS; x++) { + for (uint8_t y = 0; y < LED_ROWS; y++) { + uint8_t angle = rMapAngle[x * LED_ROWS + y]; + uint8_t radius = rMapRadius[x * LED_ROWS + y]; + leds[y * panel_width + x] = + hsv(t + radius, 255, + sin8(sin8((angle * 4 - radius * mapp) / 4 + t) + + angle * nb_branches - 2 * t)); } - sync(); - t = t + speed; - } + } + sync(); + t = t + speed; + } } \ No newline at end of file diff --git a/sc_examples/octo2.sc b/sc_examples/octo2.sc index 8d91e15..dd98966 100644 --- a/sc_examples/octo2.sc +++ b/sc_examples/octo2.sc @@ -1,72 +1,66 @@ -//import memset +// import memset save_reg -//safe_mode -//external CRGB *leds; -//external void show(); -//external CRGB hsv(uint8_t h,uint8_t s,uint8_t v); -//external uint8_t sin8(uint8_t a); -//external float hypot(float x,float y); -//external float atan2(float x,float y); -//external void resetStat(); -define LED_COLS 128 -define LED_ROWS 96 -define NUM_LEDS 12288 -define PI 3.1415926535 -define panel_width 128 -define speed 1 -define nb_branches 5 -uint8_t C_X,C_Y,mapp ; -//uint8_t C_Y; -//uint8_t mapp; + // safe_mode + // external CRGB *leds; + // external void show(); + // external CRGB hsv(uint8_t h,uint8_t s,uint8_t v); + // external uint8_t sin8(uint8_t a); + // external float hypot(float x,float y); + // external float atan2(float x,float y); + // external void resetStat(); + define LED_COLS 128 define LED_ROWS 96 define NUM_LEDS 12288 define + PI 3.1415926535 define panel_width 128 define speed 1 define + nb_branches 5 uint8_t C_X, + C_Y, mapp; +// uint8_t C_Y; +// uint8_t mapp; uint8_t rMapRadius[NUM_LEDS]; uint8_t rMapAngle[NUM_LEDS]; - -void init() -{ +void init() { C_X = LED_COLS / 2; - C_Y = LED_ROWS / 2; - mapp = 255 / LED_COLS; -for (int x = -C_X; x < C_X + (LED_COLS % 2); x++) { - for (int y = -C_Y; y < C_Y + (LED_ROWS % 2); y++) { + C_Y = LED_ROWS / 2; + mapp = 255 / LED_COLS; + for (int x = -C_X; x < C_X + (LED_COLS % 2); x++) { + for (int y = -C_Y; y < C_Y + (LED_ROWS % 2); y++) { - float h=128*(atan2(y, x)/PI); - rMapAngle[(x + C_X) *LED_ROWS+y + C_Y]= h; - h=hypot(x,y)*mapp; - rMapRadius[(x + C_X)*LED_ROWS +y + C_Y] = h; //thanks Sutaburosu - } + float h = 128 * (atan2(y, x) / PI); + rMapAngle[(x + C_X) * LED_ROWS + y + C_Y] = h; + h = hypot(x, y) * mapp; + rMapRadius[(x + C_X) * LED_ROWS + y + C_Y] = h; // thanks Sutaburosu } + } } - void main() { -resetStat(); + resetStat(); init(); - uint32_t t; - //t = speed; - while(true) - { - - // memset(leds,0,4096*3); - for (uint8_t x = 0; x < LED_COLS; x++) - for (uint8_t y = 0; y < LED_ROWS; y++) { - uint8_t angle = rMapAngle[x*LED_ROWS+y]; - uint8_t radius = rMapRadius[x*LED_ROWS+y]; -// leds[y*panel_width+x] = hsv(t + radius, 255, h); -// int j=t*4-radius; - // j=sin8(j); + uint32_t t; + // t = speed; + while (true) { -// int h=j+angle*nb_branches+t*4; - // h=sin8(h); - leds[y*panel_width+x] = hsv(t + radius, 255, sin8(t*4+sin8(t * 4 - radius)+angle*nb_branches)); - -// leds[y*panel_width+x] = hsv(t + radius, 255, h); - } - - sync(); - t=t+speed; + // memset(leds,0,4096*3); + for (uint8_t x = 0; x < LED_COLS; x++) + for (uint8_t y = 0; y < LED_ROWS; y++) { + uint8_t angle = rMapAngle[x * LED_ROWS + y]; + uint8_t radius = rMapRadius[x * LED_ROWS + y]; + // leds[y*panel_width+x] = hsv(t + radius, 255, h); + // int j=t*4-radius; + // j=sin8(j); + + // int h=j+angle*nb_branches+t*4; + // h=sin8(h); + leds[y * panel_width + x] = + hsv(t + radius, 255, + sin8(t * 4 + sin8(t * 4 - radius) + angle * nb_branches)); + + // leds[y*panel_width+x] = hsv(t + radius, 255, h); + } + + sync(); + t = t + speed; } - //delay(16); + // delay(16); } \ No newline at end of file diff --git a/sc_examples/scroll.sc b/sc_examples/scroll.sc index 80faedc..c472244 100644 --- a/sc_examples/scroll.sc +++ b/sc_examples/scroll.sc @@ -1,88 +1,75 @@ -//safe_mode -//external CRGB leds[12288]; -//external void clear(); -//external void show(); -//external uint8_t * font; -//external void display(int h); +// safe_mode +// external CRGB leds[12288]; +// external void clear(); +// external void show(); +// external uint8_t * font; +// external void display(int h); define led_width 128 -//external CRGB hsv(int h,int s,int v); -char str[]="HERE IS A SCROLLING ...."; + // external CRGB hsv(int h,int s,int v); + char str[] = "HERE IS A SCROLLING ...."; -void displayChar(int posx,int posy, int charpos) -{ -//display(999); -//display(posx); -//display(888); -//display(charpos); -//int posy=20; - // int h=0; - int h=str[charpos]-32; -//display(h); - h=h*8; - if(posx<0) - { - return; - } - if(posx>led_width) - { - return; - } - for(int y=0;y<8;y++) - { - uint8_t d=font[h+y]; -// d=255; -// display(d); - for (int x=0;x<8;x++) - { - if(posx+x 0 ) - { - int _h=posx+x+led_width*(posy+8-y); -// display(_h); - leds[_h]=CRGB(255,255,0); - } - } - } +void displayChar(int posx, int posy, int charpos) { + // display(999); + // display(posx); + // display(888); + // display(charpos); + // int posy=20; + // int h=0; + int h = str[charpos] - 32; + // display(h); + h = h * 8; + if (posx < 0) { + return; + } + if (posx > led_width) { + return; + } + for (int y = 0; y < 8; y++) { + uint8_t d = font[h + y]; + // d=255; + // display(d); + for (int x = 0; x < 8; x++) { + if (posx + x < led_width) { + + // display(d and (1<<(8-x))); + if ((d and (1 << (8 - x))) > 0) { + int _h = posx + x + led_width * (posy + 8 - y); + // display(_h); + leds[_h] = CRGB(255, 255, 0); } + } + } + } } -void main() -{ -printfln("dd %d",-1); - clear(); -// for( int i=0;i<128;i++) -// { -// for(int j=0;j<96;j++) -// { -// leds[i+j*128]=hsv(i+j+k,255,255); -// } -// } - int k=0; - while(k>=0) - { - // k++; - for( int i=0;i<128;i++) - { - for(int j=0;j<96;j++) - { - leds[i+j*128]=hsv(i+j+k,255,100); +void main() { + printfln("dd %d", -1); + clear(); + // for( int i=0;i<128;i++) + // { + // for(int j=0;j<96;j++) + // { + // leds[i+j*128]=hsv(i+j+k,255,255); + // } + // } + int k = 0; + while (k >= 0) { + // k++; + for (int i = 0; i < 128; i++) { + for (int j = 0; j < 96; j++) { + leds[i + j * 128] = hsv(i + j + k, 255, 100); } - } - -// clear(); - int h=0; - while(str[h]>0) - { - int gg=led_width+8*h-k%250; - displayChar(gg,20,h); - h++; + } + + // clear(); + int h = 0; + while (str[h] > 0) { + int gg = led_width + 8 * h - k % 250; + displayChar(gg, 20, h); + h++; } k++; - k=k%300; + k = k % 300; show(); - } - + } } \ No newline at end of file diff --git a/sc_examples/squaresani.sc b/sc_examples/squaresani.sc index 371c99d..96fede6 100644 --- a/sc_examples/squaresani.sc +++ b/sc_examples/squaresani.sc @@ -1,5 +1,5 @@ -//save_reg -//safe_mode +// save_reg +// safe_mode external void show(); external CRGB *leds; external CRGB hsv(int h, int s, int v); @@ -9,125 +9,99 @@ external void resetStat(); external void clear(); external float sin(float h); external float log(float h); -external float atan2(float x,float y ); +external float atan2(float x, float y); -define width 128 -define height 96 -define scale 1 -define speed 5 -define nSquares 4 +define width 128 define height 96 define scale 1 define speed 5 define + nSquares 4 -define cosT 0.999998 -define sinT 0.0001745 -float t1; + define cosT 0.999998 define sinT 0.0001745 float t1; float t2; float timebase; -//var cosT = cos(0.1), sinT = sin(0.1); +// var cosT = cos(0.1), sinT = sin(0.1); uint32_t __baseTime[1]; -__ASM__ void setTime() -{ - "entry a1,32" - "rsr a14,234" - "l32r a5,@___baseTime" - "s32i a14,a5,0" - "retw.n" -} -uint32_t __deltamillis[1]; -__ASM__ uint32_t millis() -{ - "entry a1,32" - "l32r a5,@___baseTime" - "l32i a14,a5,0" - "rsr a13,234" - "l32r a4,@___deltamillis" - "s32i a13,a4,0" - "sub a13,a13,a14" - "movi a14,240" - "quou a13,a13,a14" - "movi a14,1000" - "quou a2,a13,a14" - // "l32r a4,@_stackr" - // "s32i a13,a4,0" - "retw.n" -} -__ASM__ uint32_t elapseMillis() -{ - "entry a1,32" - "l32r a5,@___deltamillis" - "l32i a14,a5,0" - "rsr a13,234" - "sub a13,a13,a14" - "movi a14,240" - "quou a13,a13,a14" - "movi a14,1000" - "quou a2,a13,a14" - // "l32r a4,@_stackr" -// "s32i a13,a4,0" - "retw.n" +__ASM__ void setTime(){"entry a1,32" + "rsr a14,234" + "l32r a5,@___baseTime" + "s32i a14,a5,0" + "retw.n"} uint32_t __deltamillis[1]; +__ASM__ uint32_t millis(){"entry a1,32" + "l32r a5,@___baseTime" + "l32i a14,a5,0" + "rsr a13,234" + "l32r a4,@___deltamillis" + "s32i a13,a4,0" + "sub a13,a13,a14" + "movi a14,240" + "quou a13,a13,a14" + "movi a14,1000" + "quou a2,a13,a14" + // "l32r a4,@_stackr" + // "s32i a13,a4,0" + "retw.n"} __ASM__ uint32_t elapseMillis() { + "entry a1,32" + "l32r a5,@___deltamillis" + "l32i a14,a5,0" + "rsr a13,234" + "sub a13,a13,a14" + "movi a14,240" + "quou a13,a13,a14" + "movi a14,1000" + "quou a2,a13,a14" + // "l32r a4,@_stackr" + // "s32i a13,a4,0" + "retw.n" } -float fmod(float a, float b) -{ +float fmod(float a, float b) { int tmp = (float)(a / b); return a - b * tmp; } -float time(float par1) -{ +float time(float par1) { float myVal = millis(); - myVal = myVal / 65535 / par1; - myVal = fmod(myVal, 1.0); + myVal = myVal / 65535 / par1; + myVal = fmod(myVal, 1.0); return myVal; } - float signum(float a) { -a=a*1000; -if((int)(a)>=0) -{ - return 1.0;//abs(sin(time(0.3))); -} -else -{ - return -1.0; -} - + a = a * 1000; + if ((int)(a) >= 0) { + return 1.0; // abs(sin(time(0.3))); + } else { + return -1.0; + } } - - void beforeRender() { -uint32_t delta=elapseMillis(); - timebase = (timebase + delta/1000) % 3600; + uint32_t delta = elapseMillis(); + timebase = (timebase + delta / 1000) % 3600; t2 = time(0.08); t1 = speed * timebase; } -// squared spiral expression adapted from https://www.shadertoy.com/view/4tlfRB -void render2D(int x,int y) { -float x0=x/width/scale -0.5; -float y0=y/height/scale -0.5; - float x1 = signum(x0); +// squared spiral expression adapted from https://www.shadertoy.com/view/4tlfRB +void render2D(int x, int y) { + float x0 = x / width / scale - 0.5; + float y0 = y / height / scale - 0.5; + float x1 = signum(x0); float y1 = signum(y0); float sx = x1 * cosT + y1 * sinT; - float sy = y1 * cosT - x1 * sinT; + float sy = y1 * cosT - x1 * sinT; - float dx = abs(sin(nSquares*log(x0 * sx + y0 * sy) + atan2(y0,x0) - t1)); -leds[128 * y + x]=hsv((t2 + x0*sx + y0*sy)*255, 255,(dx * dx * dx)*255); + float dx = abs(sin(nSquares * log(x0 * sx + y0 * sy) + atan2(y0, x0) - t1)); + leds[128 * y + x] = + hsv((t2 + x0 * sx + y0 * sy) * 255, 255, (dx * dx * dx) * 255); } -void main() -{ -setTime(); -resetStat(); - int h = 1 ; - while (h > 0) - { +void main() { + setTime(); + resetStat(); + int h = 1; + while (h > 0) { beforeRender(); - for (int i = 0; i < width; i++) - { - for (int j = 0; j < height; j++) - { + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { render2D(i, j); } } diff --git a/sc_examples/tetris.sc b/sc_examples/tetris.sc index 3eba9e8..35a0fde 100644 --- a/sc_examples/tetris.sc +++ b/sc_examples/tetris.sc @@ -1,12 +1,11 @@ external CRGB leds[96, 128]; -//external void printfln(char * s,Args args); -//external CRGB hsv(int h, int k, int v); -//external void clear(); -//external void delay(uint32_t d); -define WIDTH 10 -define HEIGHT 20 +// external void printfln(char * s,Args args); +// external CRGB hsv(int h, int k, int v); +// external void clear(); +// external void delay(uint32_t d); +define WIDTH 10 define HEIGHT 20 -import rand uint8_t board[HEIGHT][WIDTH]; + import rand uint8_t board[HEIGHT][WIDTH]; uint8_t temp[4][4]; CRGB palette[28]; @@ -15,263 +14,217 @@ int currentX = 0; int currentY = 0; uint8_t shapes[7, 4, 4] = { - 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 3, 3, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 4, 4, 0, 4, 4, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 5, 5, 0, - 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 6, 6, 0, - 0, 0, 0, 0, 0, 7, 7, 7, 0, 0, 7, 0, 0, 0, 0, 0}; - -void createPalette() -{ - palette[0] = CRGB(0, 0, 0); - palette[1] = CRGB(0, 0, 0); - palette[2] = CRGB(0, 0, 0); - for (int i = 1; i <= 7; i++) - { - int o = 255 * (i - 1) / 7; - palette[i * 3] = hsv(o, 255, 255); - palette[i * 3 + 1] = hsv(o, 255, 190); - palette[i * 3 + 2] = hsv(o, 255, 128); - } - palette[24] = CRGB(255, 255, 255); - palette[25] = CRGB(50, 50, 50); - palette[26] = CRGB(255, 255, 255); + 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, + 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 3, 3, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 5, 0, 0, 0, 5, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 6, 0, + 0, 6, 6, 0, 0, 0, 0, 0, 0, 7, 7, 7, 0, 0, 7, 0, 0, 0, 0, 0}; + +void createPalette() { + palette[0] = CRGB(0, 0, 0); + palette[1] = CRGB(0, 0, 0); + palette[2] = CRGB(0, 0, 0); + for (int i = 1; i <= 7; i++) { + int o = 255 * (i - 1) / 7; + palette[i * 3] = hsv(o, 255, 255); + palette[i * 3 + 1] = hsv(o, 255, 190); + palette[i * 3 + 2] = hsv(o, 255, 128); + } + palette[24] = CRGB(255, 255, 255); + palette[25] = CRGB(50, 50, 50); + palette[26] = CRGB(255, 255, 255); } -void displayTile(int x, int y, int pa) -{ -if(x<-1) -return; - leds[83 - 4 * y, 4 * x + 4] = palette[3 * pa]; - leds[83 - 4 * y, 4 * x + 5] = palette[3 * pa]; - leds[83 - 4 * y, 4 * x + 6] = palette[3 * pa]; - leds[83 - 4 * y, 4 * x + 7] = palette[3 * pa]; - - leds[83 - 4 * y - 1, 4 * x + 4] = palette[3 * pa]; - leds[83 - 4 * y - 1, 4 * x + 5] = palette[3 * pa + 1]; - leds[83 - 4 * y - 1, 4 * x + 6] = palette[3 * pa + 1]; - leds[83 - 4 * y - 1, 4 * x + 7] = palette[3 * pa + 2]; - - leds[83 - 4 * y - 2, 4 * x + 4] = palette[3 * pa]; - leds[83 - 4 * y - 2, 4 * x + 5] = palette[3 * pa + 1]; - leds[83 - 4 * y - 2, 4 * x + 6] = palette[3 * pa + 1]; - leds[83 - 4 * y - 2, 4 * x + 7] = palette[3 * pa + 2]; - - leds[83 - 4 * y - 3, 4 * x + 4] = palette[3 * pa]; - leds[83 - 4 * y - 3, 4 * x + 5] = palette[3 * pa + 2]; - leds[83 - 4 * y - 3, 4 * x + 6] = palette[3 * pa + 2]; - leds[83 - 4 * y - 3, 4 * x + 7] = palette[3 * pa + 2]; +void displayTile(int x, int y, int pa) { + if (x < -1) + return; + leds[83 - 4 * y, 4 * x + 4] = palette[3 * pa]; + leds[83 - 4 * y, 4 * x + 5] = palette[3 * pa]; + leds[83 - 4 * y, 4 * x + 6] = palette[3 * pa]; + leds[83 - 4 * y, 4 * x + 7] = palette[3 * pa]; + + leds[83 - 4 * y - 1, 4 * x + 4] = palette[3 * pa]; + leds[83 - 4 * y - 1, 4 * x + 5] = palette[3 * pa + 1]; + leds[83 - 4 * y - 1, 4 * x + 6] = palette[3 * pa + 1]; + leds[83 - 4 * y - 1, 4 * x + 7] = palette[3 * pa + 2]; + + leds[83 - 4 * y - 2, 4 * x + 4] = palette[3 * pa]; + leds[83 - 4 * y - 2, 4 * x + 5] = palette[3 * pa + 1]; + leds[83 - 4 * y - 2, 4 * x + 6] = palette[3 * pa + 1]; + leds[83 - 4 * y - 2, 4 * x + 7] = palette[3 * pa + 2]; + + leds[83 - 4 * y - 3, 4 * x + 4] = palette[3 * pa]; + leds[83 - 4 * y - 3, 4 * x + 5] = palette[3 * pa + 2]; + leds[83 - 4 * y - 3, 4 * x + 6] = palette[3 * pa + 2]; + leds[83 - 4 * y - 3, 4 * x + 7] = palette[3 * pa + 2]; } -void init() -{ - for (int i = 0; i < WIDTH * HEIGHT; i++) - board[i] = 0; - createPalette(); - // on dessine les bords - for (int i = 0; i <= WIDTH + 1; i++) - { - displayTile(i - 1, HEIGHT, 8); - } - for (int i = 0; i < HEIGHT; i++) - { - displayTile(-1, i, 8); - displayTile(WIDTH, i, 8); - } +void init() { + for (int i = 0; i < WIDTH * HEIGHT; i++) + board[i] = 0; + createPalette(); + // on dessine les bords + for (int i = 0; i <= WIDTH + 1; i++) { + displayTile(i - 1, HEIGHT, 8); + } + for (int i = 0; i < HEIGHT; i++) { + displayTile(-1, i, 8); + displayTile(WIDTH, i, 8); + } } -void mergeShapeToBoard() -{ - for (int y = 0; y < 4; y++) - { - for (int x = 0; x < 4; x++) - { - if (currentShape[y][x]) - { - board[currentY + y][currentX + x] = currentShape[y][x]; - } +void mergeShapeToBoard() { + for (int y = 0; y < 4; y++) { + for (int x = 0; x < 4; x++) { + if (currentShape[y][x]) { + board[currentY + y][currentX + x] = currentShape[y][x]; } - } + } + } } -bool checkCollision(int dx, int dy) -{ - for (int y = 0; y < 4; y++) - { - for (int x = 0; x < 4; x++) - { - if (currentShape[y][x] > 0) - { - int newX = currentX + x + dx; - //printfln("x:%d",newX); - int newY = currentY + y + dy; - //if (newX < 0 || newX>=WIDTH) - // return true; - //if (newY >= HEIGHT) - // return true; - if (newX <0 || newX>=WIDTH || newY>=HEIGHT || board[newY, newX] ) - { - return true; - } - } +bool checkCollision(int dx, int dy) { + for (int y = 0; y < 4; y++) { + for (int x = 0; x < 4; x++) { + if (currentShape[y][x] > 0) { + int newX = currentX + x + dx; + // printfln("x:%d",newX); + int newY = currentY + y + dy; + // if (newX < 0 || newX>=WIDTH) + // return true; + // if (newY >= HEIGHT) + // return true; + if (newX < 0 || newX >= WIDTH || newY >= HEIGHT || board[newY, newX]) { + return true; + } } - } - return false; + } + } + return false; } -void rotateShape() -{ +void rotateShape() { - for (int y = 0; y < 4; y++) - { - for (int x = 0; x < 4; x++) - { - temp[y][x] = currentShape[3 - x][y]; - } - } - for (int i = 0; i < 16; i++) - currentShape[i] = temp[i]; + for (int y = 0; y < 4; y++) { + for (int x = 0; x < 4; x++) { + temp[y][x] = currentShape[3 - x][y]; + } + } + for (int i = 0; i < 16; i++) + currentShape[i] = temp[i]; } bool update; -void left() -{ -if(update) -return; - if (!checkCollision(-1, 0)) - { - currentX--; - update = true; - } +void left() { + if (update) + return; + if (!checkCollision(-1, 0)) { + currentX--; + update = true; + } } -void right() -{ -if(update) -return; - if (!checkCollision(1, 0)) - { - currentX++; - update = true; - } +void right() { + if (update) + return; + if (!checkCollision(1, 0)) { + currentX++; + update = true; + } } -void rotate() -{ -if(update) -return; - rotateShape(); - if (checkCollision(0, 0)) - { - rotateShape(); - // update = true; - } - - else - { - update=true; - } +void rotate() { + if (update) + return; + rotateShape(); + if (checkCollision(0, 0)) { + rotateShape(); + // update = true; + } + + else { + update = true; + } } -void spawnShape() -{ - int shapeIndex = rand(7); - for (int i = 0; i < 16; i++) - currentShape[i] = shapes[shapeIndex * 16 + i]; - currentX = WIDTH / 2 - 2; - currentY = 0; +void spawnShape() { + int shapeIndex = rand(7); + for (int i = 0; i < 16; i++) + currentShape[i] = shapes[shapeIndex * 16 + i]; + currentX = WIDTH / 2 - 2; + currentY = 0; } -//void putTetro(int x, int y, int tetro) +// void putTetro(int x, int y, int tetro) //{ -// for (int i = 0; i < 4; i++) -// for (int j = 0; j < 4; j++) -// board[y + j, x + i] = shapes[tetro, j, i]; -//} -void drawBoard() -{ - for (int i = 0; i < WIDTH; i++) - for (int j = 0; j < HEIGHT; j++) - displayTile(i, j, board[j, i]); - - for (int i = 0; i < 4; i++) - for (int j = 0; j < 4; j++) - { - - if (currentShape[j, i]) - displayTile(i + currentX, j + currentY, currentShape[j, i]); - } +// for (int i = 0; i < 4; i++) +// for (int j = 0; j < 4; j++) +// board[y + j, x + i] = shapes[tetro, j, i]; +// } +void drawBoard() { + for (int i = 0; i < WIDTH; i++) + for (int j = 0; j < HEIGHT; j++) + displayTile(i, j, board[j, i]); + + for (int i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) { + + if (currentShape[j, i]) + displayTile(i + currentX, j + currentY, currentShape[j, i]); + } } int count = 0; -void clearFullLines() -{ - for (int y = 0; y < HEIGHT; y++) - { - bool full = true; - for (int x = 0; x < WIDTH; x++) - { - if (board[y][x] == 0) - { - full = false; - break; - } +void clearFullLines() { + for (int y = 0; y < HEIGHT; y++) { + bool full = true; + for (int x = 0; x < WIDTH; x++) { + if (board[y][x] == 0) { + full = false; + break; } - if (full) - { - for (int ty = y; ty > 0; ty--) - { - for (int i = 0; i < WIDTH; i++) - { - board[ty, i] = board[ty - 1, i]; - } - // memcpy(board[ty], board[ty - 1], sizeof(board[ty])); - } - for (int i = 0; i < WIDTH; i++) - { - board[0, i] = 0; - } + } + if (full) { + for (int ty = y; ty > 0; ty--) { + for (int i = 0; i < WIDTH; i++) { + board[ty, i] = board[ty - 1, i]; + } + // memcpy(board[ty], board[ty - 1], sizeof(board[ty])); } - } -} - -int main() -{ - pinInterrupt(_execaddr_, "left", 23); - pinInterrupt(_execaddr_, "right", 2); - pinInterrupt(_execaddr_, "rotate", 15); - clear(); - show(); - init(); - spawnShape(); - while (true) - { - if (update) - { - - drawBoard(); - show(); - update = false; + for (int i = 0; i < WIDTH; i++) { + board[0, i] = 0; } - if (count == 0) - { + } + } +} - if (!checkCollision(0, 1)) - { - // printfln("currnt:%d", currentY); - currentY++; - } - else - { - mergeShapeToBoard(); - spawnShape(); - clearFullLines(); - if (checkCollision(0, 0)) - { -// printfln("lost"); - break; - } - } - update = true; +int main() { + pinInterrupt(_execaddr_, "left", 23); + pinInterrupt(_execaddr_, "right", 2); + pinInterrupt(_execaddr_, "rotate", 15); + clear(); + show(); + init(); + spawnShape(); + while (true) { + if (update) { + + drawBoard(); + show(); + update = false; + } + if (count == 0) { + + if (!checkCollision(0, 1)) { + // printfln("currnt:%d", currentY); + currentY++; + } else { + mergeShapeToBoard(); + spawnShape(); + clearFullLines(); + if (checkCollision(0, 0)) { + // printfln("lost"); + break; + } } - count = (count + 1) % 300; - delay(1); - } + update = true; + } + count = (count + 1) % 300; + delay(1); + } } \ No newline at end of file diff --git a/src/ESPLiveScript.h b/src/ESPLiveScript.h index 839c90c..072cc54 100644 --- a/src/ESPLiveScript.h +++ b/src/ESPLiveScript.h @@ -12,8 +12,8 @@ using namespace std; #endif #ifndef __TEST_DEBUG -#include "soc/timer_group_struct.h" #include "soc/timer_group_reg.h" +#include "soc/timer_group_struct.h" // #include "soc/rtc_wdt.h" #endif @@ -32,3877 +32,3519 @@ Token __t; #include "asm_parser.h" #include "execute.h" #ifdef __TEST_DEBUG -void prettyPrint(NodeToken *_nd, string ident) -{ - NodeToken nd = NodeToken(*_nd); - if (_nd == NULL) - { - printf("mlqskdlmqskdlomqksdlmfdkqsdlmfk"); - } - // printf("%s%d\n", ident.c_str(), 1); - - printf("%s%s\tisPointer:%d\tasPointer:%d\t", ident.c_str(), nodeTypeNames[nd._nodetype].c_str(), nd.isPointer, nd.asPointer); //, tokenNames[nd._token.type].c_str()); - - printf("text:%s\ttokenType:%s\t", nd.getTokenText(), tokenNames[nd.type].c_str()); - // printf("\n"); - if (nd.getVarType() != NULL) - { - - if (nd.type == (int)TokenUserDefinedVariable) - printf("var name:%s\t total size:%d\tstackpos:%d\t", _userDefinedTypes[nd._vartype].varName.c_str(), nd._total_size, nd.stack_pos); - else +void prettyPrint(NodeToken *_nd, string ident) { + NodeToken nd = NodeToken(*_nd); + if (_nd == NULL) { + printf("mlqskdlmqskdlomqksdlmfdkqsdlmfk"); + } + // printf("%s%d\n", ident.c_str(), 1); + + printf("%s%s\tisPointer:%d\tasPointer:%d\t", ident.c_str(), + nodeTypeNames[nd._nodetype].c_str(), nd.isPointer, + nd.asPointer); //, tokenNames[nd._token.type].c_str()); + + printf("text:%s\ttokenType:%s\t", nd.getTokenText(), + tokenNames[nd.type].c_str()); + // printf("\n"); + if (nd.getVarType() != NULL) { + + if (nd.type == (int)TokenUserDefinedVariable) + printf("var name:%s\t total size:%d\tstackpos:%d\t", + _userDefinedTypes[nd._vartype].varName.c_str(), nd._total_size, + nd.stack_pos); + else - printf("var type:%s\ttotal size:%d\tstackpos:%d\t", varTypeEnumNames[nd._vartype].c_str(), nd._total_size, nd.stack_pos); - } + printf("var type:%s\ttotal size:%d\tstackpos:%d\t", + varTypeEnumNames[nd._vartype].c_str(), nd._total_size, + nd.stack_pos); + } - printf("target :%s", nd.getTargetText()); - printf("\n"); + printf("target :%s", nd.getTargetText()); + printf("\n"); - ident += "|--"; + ident += "|--"; - int i = 0; - if (_nd->children_size() > 0) - { - for (NodeToken *t : *_nd->children) - { - // printf("child:%d\n",i); - prettyPrint(t, ident); - i++; - // printf("on finit child:%d\n",i); - } + int i = 0; + if (_nd->children_size() > 0) { + for (NodeToken *t : *_nd->children) { + // printf("child:%d\n",i); + prettyPrint(t, ident); + i++; + // printf("on finit child:%d\n",i); } - // printf("we go back\n"); + } + // printf("we go back\n"); } #endif -class Parser -{ +class Parser { public: - NodeToken root; - Parser() {} - - string linepos() - { - string f = string_format(" at line:%d position:%d", current()->line, current()->pos); - Error.line = current()->line; - Error.pos = current()->pos; - return f; - } - int size() - { - return _tks->size(); - } - Token *getTokenAtPos(int pos) - { - return _tks->getTokenAtPos(pos); - } - Token *current() - { - return _tks->current(); - } - Token *next() - { - return _tks->next(); - } - Token *prev() - { - return _tks->prev(); + NodeToken root; + Parser() {} + + string linepos() { + string f = string_format(" at line:%d position:%d", current()->line, + current()->pos); + Error.line = current()->line; + Error.pos = current()->pos; + return f; + } + int size() { return _tks->size(); } + Token *getTokenAtPos(int pos) { return _tks->getTokenAtPos(pos); } + Token *current() { return _tks->current(); } + Token *next() { return _tks->next(); } + Token *prev() { return _tks->prev(); } + Token *peek(int index) { return _tks->peek(index); } + bool Match(tokenType tt) { return _tks->Match(tt); } + bool Match(tokenType tt, int index) { return _tks->Match(tt, index); } + + void parse() { + + addfloatdivision = false; + point_regnum = 4; + point_regnum = 4; + stack_size = 0; + for_if_num = 0; + block_statement_num = 0; + local_var_num = 0; + nb_argument = 0; + + safeMode = false; + saveReg = false; + saveRegAbs = false; + _asPointer = false; + isPointer = false; + isASM = false; + current_node = &program; + sigs.clear(); + _userDefinedTypes.clear(); + _compare.clear(); + _compare.shrink_to_fit(); + nodeTokenList.clear(); + program.clearAll(); + sav_t.clear(); + sav_t.shrink_to_fit(); + main_context.clear(); + targetList.clear(); + sav_token.clear(); + _node_token_stack.clear(); + _functions.clear(); + _functions.shrink_to_fit(); + all_text.clear(); + content.clear(); + header.clear(); + footer.clear(); + jsonVar.clear(); + main_script.init(); + initMem(); + _tks = &__tks; + _tks->tokenize(&main_script, true, true, 1); + Error.error = 0; + Error.error_message = ""; + Error.line = 0; + Error.pos = 0; + nbofextern = 0; + extvariablestore = ""; + boolextern = false; + parseProgram(); + } + + Binary compileBinary() { + Binary bin; + bin.error.error = 0; + _sav_token_line = 1; + parse(); + bin.error = Error; + if (Error.error) { + pushToConsole(Error.error_message.c_str(), true); + + return bin; } - Token *peek(int index) - { - return _tks->peek(index); + pushToConsole("***********PARSING DONE*********"); + updateMem(); + buildParents(&program); +#ifdef __TEST_DEBUG + pushToConsole("***********dispalying DONE*********"); + prettyPrint(&program, ""); +#endif + program.visitNode(); + pushToConsole("***********COMPILING DONE*********"); + updateMem(); + displayStat(); + sigs.clear(); + sigs.shrink_to_fit(); + main_script.clear(); + _userDefinedTypes.clear(); + _userDefinedTypes.shrink_to_fit(); + nodeTokenList.clear(); + program.clearAll(); + sav_t.clear(); + sav_t.shrink_to_fit(); + main_context.clear(); + targetList.clear(); + change_type.clear(); + sav_token.clear(); + _node_token_stack.clear(); + _compare.clear(); + _compare.shrink_to_fit(); + for (NodeToken *h : _functions) { + + h->clearAll(); + free(h); } - bool Match(tokenType tt) - { - return _tks->Match(tt); + _functions.clear(); + _functions.shrink_to_fit(); + all_text.clear(); + all_targets.clear(); + + pushToConsole("***********AFTER CLEAN*********"); + + updateMem(); + displayStat("afterclen"); + + pushToConsole("***********CREATE BINARY*********"); + bin = createBinary(&footer, &header, &content, __parser_debug); +#ifndef __TEST_DEBUG + updateMem(); + displayStat(); + content.clear(); + updateMem(); + displayStat(); + header.clear(); + updateMem(); + displayStat(); + footer.clear(); + updateMem(); + displayStat(); + change_type.clear(); + _asm_parsed.clear(); + all_text.clear(); + jsonVar.clear(); + updateMem(); + displayStat(); + + if (bin.error.error == 1) { + // pushToConsole("WTF", true); + pushToConsole(bin.error.error_message.c_str(), true); + freeBinary(&bin); } - bool Match(tokenType tt, int index) - { - return _tks->Match(tt, index); +#endif + return bin; + } + Executable compile() { + + Executable results; + + Binary bin = compileBinary(); + // bin.error.error=1; + if (bin.error.error == 0) { + + pushToConsole("***********CREATE EXECUTABLE*********"); + + executable _executecmd = createExectutable(&bin); + // freeBinary(&bin); + results.setExecutable(_executecmd); + results.error = _executecmd.error; + updateMem(); + displayStat(); + if (_executecmd.error.error == 1) { + // exeExist = false; + // Serial.printf(termColor.Red); + + pushToConsole(_executecmd.error.error_message.c_str(), true); + } + } else { + // pushToConsole(bin.error.error_message.c_str(), true); + // freeBinary(&bin); + // updateMem(); + results.error = bin.error; + // displayStat(); } - void parse() - { + return results; + } - addfloatdivision = false; - point_regnum = 4; - point_regnum = 4; - stack_size = 0; - for_if_num = 0; - block_statement_num = 0; - local_var_num = 0; - nb_argument = 0; - - safeMode = false; - saveReg = false; - saveRegAbs = false; - _asPointer = false; - isPointer = false; - isASM = false; - current_node = &program; - sigs.clear(); - _userDefinedTypes.clear(); - _compare.clear(); - _compare.shrink_to_fit(); - nodeTokenList.clear(); - program.clearAll(); - sav_t.clear(); - sav_t.shrink_to_fit(); - main_context.clear(); - targetList.clear(); - sav_token.clear(); - _node_token_stack.clear(); - _functions.clear(); - _functions.shrink_to_fit(); - all_text.clear(); - content.clear(); - header.clear(); - footer.clear(); - jsonVar.clear(); - main_script.init(); - initMem(); - _tks = &__tks; - _tks->tokenize(&main_script, true, true, 1); - Error.error = 0; - Error.error_message = ""; - Error.line = 0; - Error.pos = 0; - nbofextern=0; - extvariablestore=""; - boolextern=false; - parseProgram(); - } + Executable parseScript(string *str) { - Binary compileBinary() - { - Binary bin; - bin.error.error = 0; - _sav_token_line = 1; - parse(); - bin.error = Error; - if (Error.error) - { - pushToConsole(Error.error_message.c_str(), true); - - return bin; - } - pushToConsole("***********PARSING DONE*********"); - updateMem(); - buildParents(&program); -#ifdef __TEST_DEBUG - pushToConsole("***********dispalying DONE*********"); - prettyPrint(&program, ""); + main_script.clear(); + main_script.addContent((char *)_sync.c_str()); + // main_script.addContent((char *)division.c_str()); + main_script.addContent((char *)base_ext_functions.c_str()); + main_script.addContent((char *)str->c_str()); + return compile(); + } + + Binary parseScriptBinary(string *str) { + + main_script.clear(); + main_script.addContent((char *)_sync.c_str()); + // main_script.addContent((char *)division.c_str()); + main_script.addContent((char *)base_ext_functions.c_str()); + main_script.addContent((char *)str->c_str()); + return compileBinary(); + } +#ifdef __CONSOLE_ESP32 + Executable parse_c(list *_script) { + main_script.clear(); + main_script.addContent((char *)_sync.c_str()); + // main_script.addContent((char *)division.c_str()); + main_script.addContent((char *)base_ext_functions.c_str()); + string sc = ""; + for (string s : *_script) { + sc = sc + "\n" + s; + } + main_script.addContent((char *)sc.c_str()); + return compile(); + } + Binary parse_cBinary(list *_script) { + main_script.clear(); + main_script.addContent((char *)_sync.c_str()); + // main_script.addContent((char *)division.c_str()); + main_script.addContent((char *)base_ext_functions.c_str()); + string sc = ""; + for (string s : *_script) { + sc = sc + "\n" + s; + } + main_script.addContent((char *)sc.c_str()); + return compileBinary(); + } #endif - program.visitNode(); - pushToConsole("***********COMPILING DONE*********"); - updateMem(); - displayStat(); - sigs.clear(); - sigs.shrink_to_fit(); - main_script.clear(); - _userDefinedTypes.clear(); - _userDefinedTypes.shrink_to_fit(); - nodeTokenList.clear(); - program.clearAll(); - sav_t.clear(); - sav_t.shrink_to_fit(); - main_context.clear(); - targetList.clear(); - change_type.clear(); - sav_token.clear(); - _node_token_stack.clear(); - _compare.clear(); - _compare.shrink_to_fit(); - for (NodeToken *h : _functions) - { + void getVariable(bool isStore) { + + current_cntx->findVariable(current(), false); // false + // NodeToken *nd = search_result; + if (search_result == NULL) { + + found = false; + for (int i = 0; i < external_links.size(); i++) { + if (strcmp(current()->getText(), external_links[i].name.c_str()) == 0) { + sav_t.push_back(*current()); + found = true; + _node_token_stack.push_back(current_node); + current_node = &program; + // string toinsert = external_links[i].name; + extra_script.clear(); + _extra_tks.clear(); + // printf("on iserset %s\n", external_links[i].signature.c_str()); + extra_script.addContent( + (char *)(external_links[i].signature.c_str())); + extra_script.init(); + __isBlockComment = false; + _tks = &_extra_tks; + for (int i = 0; i < __DEPTH; i++) { + _tks->push(Token()); + } + insecond = true; + _tks->tokenizelow(&extra_script, true, true, 20); + insecond = false; + + parseType(); + if (Error.error) { - h->clearAll(); - free(h); - } - _functions.clear(); - _functions.shrink_to_fit(); - all_text.clear(); - all_targets.clear(); + return; + } + parseVariableForCreation(); + if (Error.error) { - pushToConsole("***********AFTER CLEAN*********"); + return; + } + NodeToken nd = nodeTokenList.pop(); + NodeToken _t = nodeTokenList.pop(); + if (isExternal) { + nd._nodetype = (int)defExtGlobalVariableNode; - updateMem(); - displayStat("afterclen"); + isExternal = false; + } else { + nd._nodetype = (int)defGlobalVariableNode; + } + copyPrty(&_t, &nd); - pushToConsole("***********CREATE BINARY*********"); - bin = createBinary(&footer, &header, &content, __parser_debug); -#ifndef __TEST_DEBUG - updateMem(); - displayStat(); - content.clear(); - updateMem(); - displayStat(); - header.clear(); - updateMem(); - displayStat(); - footer.clear(); - updateMem(); - displayStat(); - change_type.clear(); - _asm_parsed.clear(); - all_text.clear(); - jsonVar.clear(); - updateMem(); - displayStat(); + current_node = program.addChild(nd); - if (bin.error.error == 1) - { - // pushToConsole("WTF", true); - pushToConsole(bin.error.error_message.c_str(), true); - freeBinary(&bin); + main_context.addVariable(nd); + current_node = _node_token_stack.back(); + _node_token_stack.pop_back(); + + _tks = &__tks; + extra_script.clear(); + _extra_tks.clear(); + current_cntx->findVariable(&sav_t.back(), false); + sav_t.pop_back(); + break; } -#endif - return bin; - } - Executable compile() - { + } + if (!found) - Executable results; + { + Error.error = 1; + Error.error_message = + string_format("impossible to find declaraiton for %s %s", + current()->getText(), linepos().c_str()); + next(); + return; + } + } - Binary bin = compileBinary(); - // bin.error.error=1; - if (bin.error.error == 0) - { + // token *vartoken = current(); + // auto var = + // current_node = current_node->addChild( + createNodeVariable(current(), isStore); + + next(); + if (Match(TokenOpenBracket)) { + // on parse + next(); + + // NodeToken nd; + NodeToken nd = NodeToken(changeTypeNode); + nd._nodetype = changeTypeNode; + nd.type = TokenKeywordVarType; + nd._vartype = __none__; + current_node = current_node->addChild(nd); + change_type.push_back(current_node); + parseExpr(); + if (Error.error) { + next(); + return; + } + current_node = current_node->parent; + change_type.pop_back(); + if (Match(TokenCloseBracket)) { - pushToConsole("***********CREATE EXECUTABLE*********"); + // Error.error = 0; + // current_node = current_node->parent; - executable _executecmd = createExectutable(&bin); - // freeBinary(&bin); - results.setExecutable(_executecmd); - results.error = _executecmd.error; - updateMem(); - displayStat(); - if (_executecmd.error.error == 1) - { - // exeExist = false; - // Serial.printf(termColor.Red); + next(); + // return; + } else if (Match(TokenComma)) { + while (Match(TokenComma)) { + next(); + + // nb_argument++; + // NodeToken nd; + NodeToken nd = NodeToken(changeTypeNode); + nd._nodetype = changeTypeNode; + nd.type = TokenKeywordVarType; + nd._vartype = __none__; + current_node = current_node->addChild(nd); + change_type.push_back(current_node); + parseExpr(); + if (Error.error) { + return; + } + current_node = current_node->parent; + change_type.pop_back(); + // arg.addChild(res._nd); + } + if (Match(TokenCloseBracket)) { + + next(); + vector tile; + int _s = current_node->children_size(); + int nb = 0; + string sd = string(current_node->getTargetText()); + if (sd.compare(0, 1, "@") == 0) { + tile = split(sd, " "); + + sscanf(tile[0].c_str(), "@%d", &nb); + } + if (nb < _s) { + Error.error = 1; + Error.error_message = string_format( + "too many arguments expected less than %d got %d at %s", nb, _s, + linepos().c_str()); + next(); + return; + } + } else { - pushToConsole(_executecmd.error.error_message.c_str(), true); - } - } - else - { - // pushToConsole(bin.error.error_message.c_str(), true); - // freeBinary(&bin); - // updateMem(); - results.error = bin.error; - // displayStat(); + Error.error = 1; + Error.error_message = + string_format("expecting ] or , %s at %s", current()->getText(), + linepos().c_str()); + next(); + return; } + } else { - return results; + Error.error = 1; + Error.error_message = + string_format("expecting ] or , %s at %s", current()->getText(), + linepos().c_str()); + next(); + return; + } } + // else + // { + + if (Match(TokenMember) && Match(TokenIdentifier, 1) && + !Match(TokenOpenParenthesis, 2)) { + next(); + int i = 0; + varType *v = NULL; + + if (current_node->_vartype == __CRGB__ or + current_node->_vartype == __CRGBW__) { + i = findMember(current_node->getVarType(), + string(current()->getText())); + v = current_node->getVarType(); + if (i < 0) { + Error.error = 1; + Error.error_message = + string_format("Member %s of struct %s does not exists", + current()->getText(), v->varName.c_str()); + next(); + return; + } + } else { + i = findMember(current_node->_vartype, string(current()->getText())); + // int pos = 0; + v = &_userDefinedTypes[current_node->_vartype]; + + if (i < 0) { + Error.error = 1; + Error.error_message = + string_format("Member %s of struct %s does not exists", + current()->getText(), v->varName.c_str()); + next(); + return; + } + } + // next(); + // current_node->addTargetText(string(current()->getText())); + current_node->type = TokenUserDefinedVariableMember; + current_node->_vartype = v->types[i]; + if (!_asPointer) + current_node->stack_pos = current_node->stack_pos + v->starts[i]; + else + current_node->stack_pos = current_node->stack_pos + 1000 * v->starts[i]; + if (current_node->isPointer) { + // current_node->_total_size =1000* current_node->_total_size + // +v->sizes[i]; + } else { + current_node->_total_size = v->sizes[i]; + } + next(); + } else if (Match(TokenMember, 0) && Match(TokenIdentifier, 1) && + Match(TokenOpenParenthesis, 2)) { + + current_cntx->findVariable(current_node->getTokenText(), false); + if (search_result == NULL) { - Executable parseScript(string *str) - { - - main_script.clear(); - main_script.addContent((char *)_sync.c_str()); - // main_script.addContent((char *)division.c_str()); - main_script.addContent((char *)base_ext_functions.c_str()); - main_script.addContent((char *)str->c_str()); - return compile(); + Error.error = 1; + Error.error_message = + string_format("impossible to find declaraiton for %s %s", + current()->getText(), linepos().c_str()); + next(); + return; + } + // next(); + next(); + + current()->addText( + string_format("%s.%s", search_result->getVarType()->varName.c_str(), + current()->getText())); + // nd = *search_result; //30/12 + NodeToken nd = NodeToken(*search_result); + // nd.copyChildren(search_result); + if (search_result->_nodetype == defGlobalVariableNode) + nd._nodetype = globalVariableNode; + else if (search_result->_nodetype == defLocalVariableNodeAsRegister) { + nd._nodetype = localVariableNodeAsRegister; + nd.target = search_result->target; + } else + nd._nodetype = localVariableNode; + + nd.type = TokenUserDefinedVariableMemberFunction; + nd.isPointer = true; + nd._total_size = search_result->getVarType() + ->total_size; // nd.copyChildren(current_node); + /* + for(int i=0;ichildren_size();i++) + { + nd.addChild(*current_node->getChildAtPos(i)); + } + + NodeToken *par=current_node->parent; + current_node->parent->children->pop_back(); + current_node=par; + */ + current_node->_nodetype = UnknownNode; + // NodeToken *par = current_node; + _node_token_stack.push_back(current_node); + current_node = current_node->parent; + + nodeTokenList.push(nd); + isStructFunction = true; + // printf("her\n"); + parseFunctionCall(); + + if (Error.error) { + return; + } + current_node->getChildAtPos(current_node->children_size() - 1) + ->getChildAtPos(0) + ->getChildAtPos(0) + ->copyChildren(_node_token_stack.back()); + _node_token_stack.pop_back(); + isStructFunction = false; + Error.error = 0; + return; } + Error.error = 0; + tmp_sav = current_node; + current_node = current_node->parent; - Binary parseScriptBinary(string *str) - { + return; - main_script.clear(); - main_script.addContent((char *)_sync.c_str()); - // main_script.addContent((char *)division.c_str()); - main_script.addContent((char *)base_ext_functions.c_str()); - main_script.addContent((char *)str->c_str()); - return compileBinary(); + //} + } + void parseArguments() { +#ifdef PARSER_DEBUG + updateMem(); +#endif + // resParse result; + // signature="("; + sigs.push_back("("); + nb_argument = 0; + nb_args.push_back(0); + // NodeInputArguments arg; + current_node = current_node->addChild(NodeToken(inputArgumentsNode)); + if (isStructFunction) { + + if (struct_name.size() == 0) { + current_node->addChild(nodeTokenList.pop()); + nb_args.pop_back(); + nb_args.push_back(1); + } else { + NodeToken nd = NodeToken(); + nd.setTargetText("pointer"); + nd.isPointer = true; + nd.type = TokenUserDefinedVariable; + nd._nodetype = localVariableNode; + nd.stack_pos = _STACK_SIZE; + current_node->addChild(nd); + nb_args.pop_back(); + nb_args.push_back(1); + } } -#ifdef __CONSOLE_ESP32 - Executable parse_c(list *_script) - { - main_script.clear(); - main_script.addContent((char *)_sync.c_str()); - // main_script.addContent((char *)division.c_str()); - main_script.addContent((char *)base_ext_functions.c_str()); - string sc = ""; - for (string s : *_script) - { - sc = sc + "\n" + s; - } - main_script.addContent((char *)sc.c_str()); - return compile(); + if (Match(TokenCloseParenthesis)) { + // resParse result; + Error.error = 0; + // result._nd = arg; + // printf("on retourne with argh ide\n"); + current_node = current_node->parent; + string _signature = sigs.back() + ")"; + sigs.pop_back(); + sigs.push_back(_signature); + next(); + return; } - Binary parse_cBinary(list *_script) - { - main_script.clear(); - main_script.addContent((char *)_sync.c_str()); - // main_script.addContent((char *)division.c_str()); - main_script.addContent((char *)base_ext_functions.c_str()); - string sc = ""; - for (string s : *_script) - { - sc = sc + "\n" + s; - } - main_script.addContent((char *)sc.c_str()); - return compileBinary(); + nb_args.pop_back(); + if (isStructFunction) { + nb_args.push_back(2); + } else { + nb_args.push_back(1); + } + // nb_argument = 1; + // Serial.printf("lkklqdqsdksmdkqsd\r\n"); + // NodeToken nd; + NodeToken nd = NodeToken(changeTypeNode); + nd._nodetype = changeTypeNode; + nd.type = TokenKeywordVarType; + nd._vartype = __none__; + current_node = current_node->addChild(nd); + change_type.push_back(current_node); + // printf("lkklqdqsdksm excut dkqsd\r\n"); + parseExpr(); + // printf("lkklqdqsdksm excut dkqsd\r\n"); + if (Error.error) { + return; + } + string _signature = sigs.back() + current_node->getVarType()->varName; + sigs.pop_back(); + sigs.push_back(_signature); + if (current_node->isPointer) { + + string _signature = sigs.back() + "*"; + sigs.pop_back(); + sigs.push_back(_signature); + } + current_node = current_node->parent; + change_type.pop_back(); + // arg.addChild(res._nd); + while (Match(TokenComma)) { + next(); + __sav_arg = nb_args.back(); + nb_args.pop_back(); + nb_args.push_back(__sav_arg + 1); + // nb_argument++; + // NodeToken nd; + NodeToken nd = NodeToken(changeTypeNode); + nd._nodetype = changeTypeNode; + nd.type = TokenKeywordVarType; + nd._vartype = __none__; + current_node = current_node->addChild(nd); + change_type.push_back(current_node); + parseExpr(); + if (Error.error) { + return; + } + + string _signature = + sigs.back() + "|" + current_node->getVarType()->varName; + sigs.pop_back(); + sigs.push_back(_signature); + if (current_node->isPointer) { + + string _signature = sigs.back() + "*"; + sigs.pop_back(); + sigs.push_back(_signature); + } + current_node = current_node->parent; + change_type.pop_back(); + // arg.addChild(res._nd); + } + if (!Match(TokenCloseParenthesis)) { + + Error.error = 1; + Error.error_message = string_format("Expected ) %s", linepos().c_str()); + next(); + return; } + next(); + Error.error = 0; + // result._nd = arg; + _signature = sigs.back() + ")"; + sigs.pop_back(); + sigs.push_back(_signature); + current_node = current_node->parent; + return; + } + void parseFunctionCall() { +#ifdef PARSER_DEBUG + pushToConsole(string_format("functions:%s", __FUNCTION__)); + updateMem(); #endif - void getVariable(bool isStore) - { + // printf("calling function yves %s\r\n", current()->getText()); + + sav_t.push_back(*current()); + next(); + next(); + + main_context.findCandidate(sav_t.back().getText()); + if (!main_context.findCandidate(sav_t.back().getText())) { + if (struct_name.size() > 0) { + string v_tmp = + string_format("%s.%s", struct_name.c_str(), sav_t.back().getText()); + if (main_context.findCandidate((char *)v_tmp.c_str())) + isStructFunction = true; + } + } + // printf("calling function suite\r\n"); + parseArguments(); + // printf("calling function suite\r\n"); + if (Error.error) { + return; + } - current_cntx->findVariable(current(), false); // false - // NodeToken *nd = search_result; - if (search_result == NULL) - { - - found = false; - for (int i = 0; i < external_links.size(); i++) - { - if (strcmp(current()->getText(), external_links[i].name.c_str()) == 0) - { - sav_t.push_back(*current()); - found = true; - _node_token_stack.push_back(current_node); - current_node = &program; - // string toinsert = external_links[i].name; - extra_script.clear(); - _extra_tks.clear(); - // printf("on iserset %s\n", external_links[i].signature.c_str()); - extra_script.addContent((char *)(external_links[i].signature.c_str())); - extra_script.init(); - __isBlockComment = false; - _tks = &_extra_tks; - for (int i = 0; i < __DEPTH; i++) - { - _tks->push(Token()); - } - insecond = true; - _tks->tokenizelow(&extra_script, true, true, 20); - insecond = false; - - parseType(); - if (Error.error) - { - - return; - } - parseVariableForCreation(); - if (Error.error) - { - - return; - } - NodeToken nd = nodeTokenList.pop(); - NodeToken _t = nodeTokenList.pop(); - if (isExternal) - { - nd._nodetype = (int)defExtGlobalVariableNode; - - isExternal = false; - } - else - { - nd._nodetype = (int)defGlobalVariableNode; - } - copyPrty(&_t, &nd); - - current_node = program.addChild(nd); - - main_context.addVariable(nd); - current_node = _node_token_stack.back(); - _node_token_stack.pop_back(); - - _tks = &__tks; - extra_script.clear(); - _extra_tks.clear(); - current_cntx->findVariable(&sav_t.back(), false); - sav_t.pop_back(); - break; - } + _node_token_stack.push_back(current_node->children->back()); + // NodeToken d = current_node->children->back(); + current_node->children->pop_back(); + + (&sav_t.back()) + ->addText( + string_format("%s%s", sav_t.back().getText(), sigs.back().c_str())); + sigs.pop_back(); + main_context.findFunction(&sav_t.back()); + // NodeToken *t =search_result; + if (search_result == NULL) { + + if (struct_name.size() > 0) { + sav_t.push_back(sav_t.back()); + (&sav_t.back()) + ->addText(string_format("%s.%s", struct_name.c_str(), + sav_t.back().getText())); + main_context.findFunction(&sav_t.back()); + isStructFunction = true; + sav_t.pop_back(); + // sav_t.pop_back(); + } + if (search_result == NULL) { + + for (int i = 0; i < external_links.size(); i++) { + // printf("comparing %s ,%s \n\r", + // external_links[i].signature.c_str(), + // external_links[i].signature.c_str()); + // bool + found = false; + + if (strstr(external_links[i].signature.c_str(), "Args") != NULL) { + int l = strstr(external_links[i].signature.c_str(), "Args") - + external_links[i].signature.c_str(); + if (l > 0) + l--; + if (strncmp(external_links[i].signature.c_str(), + sav_t.back().getText(), l) == 0) { + found = true; + } + } else if (external_links[i].signature.compare( + string(sav_t.back().getText())) == 0) { + found = true; + } + if (found) { + // printf("her\n\r"); + savestacksize = stack_size; + _node_token_stack.push_back(current_node); + current_node = &program; + // string toinsert = external_links[i].name; //"external " + + // external_links[i].out + " " + external_links[i].name + + // "("+external_links[i].in + ");"; printf("on inseet %s\n", + // external_links[i].name.c_str()); + // main_script.previousChar(); + extra_script.clear(); + + extra_script.addContent((char *)(external_links[i].name.c_str())); + extra_script.init(); + _tks = &_extra_tks; + // extra_script.nextChar(); + + _tks->clear(); + + // next(); + // prev(); + pos_in_line = 0; + insecond = true; + _tks->tokenizelow(&extra_script, true, true, 20); + insecond = false; + // printf("%s \n\r",next()->getText()); + // _for_display=false; + + // printf("%s \n\r",next()->getText()); + // printf("%s \n\r",next()->getText()); + + // prev(); + sav_b = isStructFunction; + isStructFunction = false; + parseType(); + + if (Error.error) { + // printf("ice\n\r"); + return; } - if (!found) - { - Error.error = 1; - Error.error_message = string_format("impossible to find declaraiton for %s %s", current()->getText(), linepos().c_str()); - next(); - return; + parseDefFunction(); + if (Error.error) { + // printf("cold\n\r"); + return; } + current_node = _node_token_stack.back(); + _node_token_stack.pop_back(); + isStructFunction = sav_b; + break; + // return; + } } + _tks = &__tks; + extra_script.clear(); + _extra_tks.clear(); + stack_size = savestacksize; + // printf("looking for %s\n\r", sav_t.back().getText()); + main_context.findFunction(&sav_t.back()); + if (search_result == NULL) { + Error.error = 1; + Error.error_message = + string_format("function %s not found %s", sav_t.back().getText(), + linepos().c_str()); + return; + } + // current()->type=TokenSemicolon; + } + } - // token *vartoken = current(); - // auto var = - // current_node = current_node->addChild( - createNodeVariable(current(), isStore); + // NodeToken *res=search_result; - next(); - if (Match(TokenOpenBracket)) - { - // on parse - next(); + NodeToken _nd = NodeToken(*search_result); + if (_nd._nodetype == (int)defExtFunctionNode) { - // NodeToken nd; - NodeToken nd = NodeToken(changeTypeNode); - nd._nodetype = changeTypeNode; - nd.type = TokenKeywordVarType; - nd._vartype = __none__; - current_node = current_node->addChild(nd); - change_type.push_back(current_node); - parseExpr(); - if (Error.error) - { - next(); - return; - } - current_node = current_node->parent; - change_type.pop_back(); - if (Match(TokenCloseBracket)) - { + _nd._nodetype = extCallFunctionNode; + } else // if (_nd._nodetype == (int)defFunctionNode) + { + _nd._nodetype = callFunctionNode; + } - // Error.error = 0; - // current_node = current_node->parent; + // NodeExtCallFunction function = NodeExtCallFunction(t); + _nd.target = search_result_index; + current_node = current_node->addChild(_nd); + // current_node->copyChildren(search_result); + // current_node->addChild(NodeToken(search_result->getChildAtPos(0))); + + // if (search_result->getChildAtPos(0)->_vartype == __float__ and + // change_type.size() > 0) + // change_type.back()->_vartype = __float__; + + if (change_type.size() > 0) { + if (search_result->getChildAtPos(0)->children_size() == 0 && + !search_result->getChildAtPos(0)->asPointer) + change_type.back()->isPointer = + search_result->getChildAtPos(0)->isPointer; // n,ew modif here + if (change_type.back()->_vartype != __float__) { + if (search_result->getChildAtPos(0)->_vartype == __float__ || + search_result->getChildAtPos(0)->_vartype == __uint32_t__) { + change_type.back()->_vartype = + search_result->getChildAtPos(0)->_vartype; + } + } + } + // NodeToken *o = + // current_node->addChild(NodeToken(search_result->getChildAtPos(1))); + // o->copyChildren(search_result->getChildAtPos(1)); - next(); - // return; - } - else if (Match(TokenComma)) - { - while (Match(TokenComma)) - { - next(); - - // nb_argument++; - // NodeToken nd; - NodeToken nd = NodeToken(changeTypeNode); - nd._nodetype = changeTypeNode; - nd.type = TokenKeywordVarType; - nd._vartype = __none__; - current_node = current_node->addChild(nd); - change_type.push_back(current_node); - parseExpr(); - if (Error.error) - { - return; - } - current_node = current_node->parent; - change_type.pop_back(); - // arg.addChild(res._nd); - } - if (Match(TokenCloseBracket)) - { - - next(); - vector tile; - int _s = current_node->children_size(); - int nb = 0; - string sd = string(current_node->getTargetText()); - if (sd.compare(0, 1, "@") == 0) - { - tile = split(sd, " "); - - sscanf(tile[0].c_str(), "@%d", &nb); - } - if (nb < _s) - { - Error.error = 1; - Error.error_message = string_format("too many arguments expected less than %d got %d at %s", nb, _s, linepos().c_str()); - next(); - return; - } - } - else - { + // sav_nb_arg = function._link->getChildAtPos(1)->children_size(); - Error.error = 1; - Error.error_message = string_format("expecting ] or , %s at %s", current()->getText(), linepos().c_str()); - next(); - return; - } - } - else - { + nb_sav_args.push_back(search_result->getChildAtPos(1)->children_size()); + if (isStructFunction) { + // nb_sav_args.push_back( nb_sav_args.back()-1); + isStructFunction = false; + } + for (int i = 0; i < search_result->getChildAtPos(1)->children_size(); i++) { + if (search_result->getChildAtPos(1)->getChildAtPos(i)->_vartype == + __Args__) { - Error.error = 1; - Error.error_message = string_format("expecting ] or , %s at %s", current()->getText(), linepos().c_str()); - next(); - return; - } - } - // else - // { + nb_sav_args.pop_back(); + nb_sav_args.push_back(999); + } + } - if (Match(TokenMember) && Match(TokenIdentifier, 1) && !Match(TokenOpenParenthesis, 2)) - { - next(); - int i = 0; - varType *v = NULL; - - if (current_node->_vartype == __CRGB__ or current_node->_vartype == __CRGBW__) - { - i = findMember(current_node->getVarType(), string(current()->getText())); - v = current_node->getVarType(); - if (i < 0) - { - Error.error = 1; - Error.error_message = string_format("Member %s of struct %s does not exists", current()->getText(), v->varName.c_str()); - next(); - return; - } - } - else - { - i = findMember(current_node->_vartype, string(current()->getText())); - // int pos = 0; - v = &_userDefinedTypes[current_node->_vartype]; - - if (i < 0) - { - Error.error = 1; - Error.error_message = string_format("Member %s of struct %s does not exists", current()->getText(), v->varName.c_str()); - next(); - return; - } - } - // next(); - // current_node->addTargetText(string(current()->getText())); - current_node->type = TokenUserDefinedVariableMember; - current_node->_vartype = v->types[i]; - if (!_asPointer) - current_node->stack_pos = current_node->stack_pos + v->starts[i]; - else - current_node->stack_pos = current_node->stack_pos + 1000 * v->starts[i]; - if (current_node->isPointer) - { - // current_node->_total_size =1000* current_node->_total_size +v->sizes[i]; - } - else - { - current_node->_total_size = v->sizes[i]; - } - next(); - } - else if (Match(TokenMember, 0) && Match(TokenIdentifier, 1) && Match(TokenOpenParenthesis, 2)) - { + current_node->_vartype = search_result->getChildAtPos(0)->_vartype; - current_cntx->findVariable(current_node->getTokenText(), false); - if (search_result == NULL) - { + current_node->addChild(_node_token_stack.back()); + _node_token_stack.pop_back(); - Error.error = 1; - Error.error_message = string_format("impossible to find declaraiton for %s %s", current()->getText(), linepos().c_str()); - next(); - return; - } - // next(); - next(); + if (nb_sav_args.back() != nb_args.back() and + nb_sav_args.back() != 999) // if (sav_nb_arg != nb_args.back()) + { + Error.error = 1; - current()->addText(string_format("%s.%s", search_result->getVarType()->varName.c_str(), current()->getText())); - // nd = *search_result; //30/12 - NodeToken nd = NodeToken(*search_result); - // nd.copyChildren(search_result); - if (search_result->_nodetype == defGlobalVariableNode) - nd._nodetype = globalVariableNode; - else if (search_result->_nodetype == defLocalVariableNodeAsRegister) - { - nd._nodetype = localVariableNodeAsRegister; - nd.target = search_result->target; - } - else - nd._nodetype = localVariableNode; - - nd.type = TokenUserDefinedVariableMemberFunction; - nd.isPointer = true; - nd._total_size = search_result->getVarType()->total_size; // nd.copyChildren(current_node); - /* - for(int i=0;ichildren_size();i++) - { - nd.addChild(*current_node->getChildAtPos(i)); - } - - NodeToken *par=current_node->parent; - current_node->parent->children->pop_back(); - current_node=par; - */ - current_node->_nodetype = UnknownNode; - // NodeToken *par = current_node; - _node_token_stack.push_back(current_node); - current_node = current_node->parent; + Error.error_message = + string_format("Expected %d arguments got %d %s", nb_sav_args.back(), + nb_args.back(), linepos().c_str()); + return; + } + nb_args.pop_back(); + nb_sav_args.pop_back(); + sav_t.pop_back(); + Error.error = 0; + current_node = current_node->parent; - nodeTokenList.push(nd); - isStructFunction = true; - // printf("her\n"); - parseFunctionCall(); + return; + } - if (Error.error) - { - return; - } - current_node->getChildAtPos(current_node->children_size() - 1)->getChildAtPos(0)->getChildAtPos(0)->copyChildren(_node_token_stack.back()); - _node_token_stack.pop_back(); - isStructFunction = false; - Error.error = 0; - return; - } - Error.error = 0; - tmp_sav = current_node; - current_node = current_node->parent; + void parseComparaison() { +#ifdef PARSER_DEBUG + updateMem(); +#endif - return; + Error.error = 0; - //} + current_node = current_node->addChild(NodeToken(current(), comparatorNode)); + + current_node->setTargetText(targetList.pop()); + parseExprAndOr(); + // parseExprConditionnal(); + if (Error.error) { + return; } - void parseArguments() - { + + next(); + Error.error = 0; + current_node = current_node->parent; + + return; + } + void parseStatement() { #ifdef PARSER_DEBUG - updateMem(); + updateMem(); #endif - // resParse result; - // signature="("; - sigs.push_back("("); - nb_argument = 0; - nb_args.push_back(0); - // NodeInputArguments arg; - current_node = current_node->addChild(NodeToken(inputArgumentsNode)); - if (isStructFunction) - { - - if (struct_name.size() == 0) - { - current_node->addChild(nodeTokenList.pop()); - nb_args.pop_back(); - nb_args.push_back(1); - } - else - { - NodeToken nd = NodeToken(); - nd.setTargetText("pointer"); - nd.isPointer = true; - nd.type = TokenUserDefinedVariable; - nd._nodetype = localVariableNode; - nd.stack_pos = _STACK_SIZE; - current_node->addChild(nd); - nb_args.pop_back(); - nb_args.push_back(1); - } - } - if (Match(TokenCloseParenthesis)) - { - // resParse result; - Error.error = 0; - // result._nd = arg; - // printf("on retourne with argh ide\n"); - current_node = current_node->parent; - string _signature = sigs.back() + ")"; - sigs.pop_back(); - sigs.push_back(_signature); - next(); - return; - } - nb_args.pop_back(); - if (isStructFunction) - { - nb_args.push_back(2); - } - else - { - nb_args.push_back(1); - } - // nb_argument = 1; - // Serial.printf("lkklqdqsdksmdkqsd\r\n"); - // NodeToken nd; + + isPointer = false; + sav_token.clear(); + Error.error = 0; + current_node->addChild(NodeToken(statementNode)); + updateMem(); + if (Match(TokenString)) { + current_node->addChild(NodeToken(current(), stringNode)); + next(); + return; + } else if (Match(TokenKeywordBreak)) { + string c = findForWhile(); + if (c.compare("__") == 0) { + Error.error = 1; + Error.error_message = string_format( + "nor For ar while found for break %s", linepos().c_str()); + return; + } + next(); + if (Match(TokenSemicolon)) { + current_node->addChild(NodeToken(c, breakNode)); + next(); + return; + } else { + Error.error = 1; + Error.error_message = + string_format("d Expected ; %s", linepos().c_str()); + return; + } + } else if (Match(TokenKeywordContinue)) { + string c = findForWhile(); + if (c.compare("__") == 0) { + Error.error = 1; + Error.error_message = string_format( + "nor For ar while found for continue %s", linepos().c_str()); + return; + } + next(); + if (Match(TokenSemicolon)) { + current_node->addChild(NodeToken(c, continueNode)); + next(); + return; + } else { + Error.error = 1; + Error.error_message = + string_format("d Expected ; %s", linepos().c_str()); + return; + } + } else if (Match(TokenKeywordReturn)) { + next(); + if (Match(TokenSemicolon)) { + current_node->addChild(NodeToken(returnNode)); + next(); + return; + } else { + current_node = current_node->addChild(NodeToken(returnNode)); + NodeToken nd = NodeToken(changeTypeNode); nd._nodetype = changeTypeNode; nd.type = TokenKeywordVarType; + nd.setTokenText("yevbs"); nd._vartype = __none__; + if (lasttype != NULL) { + nd._vartype = lasttype->_vartype; + /* + if (lasttype->_vartype == __float__) + { + nd._vartype = __float__; + } + */ + } else { + Error.error = 1; + Error.error_message = + string_format("issue with return %s", linepos().c_str()); + return; + } + current_node = current_node->addChild(nd); change_type.push_back(current_node); - // printf("lkklqdqsdksm excut dkqsd\r\n"); parseExpr(); - // printf("lkklqdqsdksm excut dkqsd\r\n"); - if (Error.error) - { - return; - } - string _signature = sigs.back() + current_node->getVarType()->varName; - sigs.pop_back(); - sigs.push_back(_signature); - if (current_node->isPointer) - { + if (Error.error) { + return; + } + if (Match(TokenSemicolon)) { + Error.error = 0; + current_node = current_node->parent; + // res._nd = var; + + current_node = current_node->parent; + change_type.pop_back(); + next(); + return; + } else { + Error.error = 1; + Error.error_message = + string_format("d Expected ; %s", linepos().c_str()); + return; + } + } + } - string _signature = sigs.back() + "*"; - sigs.pop_back(); - sigs.push_back(_signature); - } - current_node = current_node->parent; - change_type.pop_back(); - // arg.addChild(res._nd); - while (Match(TokenComma)) - { - next(); - __sav_arg = nb_args.back(); - nb_args.pop_back(); - nb_args.push_back(__sav_arg + 1); - // nb_argument++; - // NodeToken nd; - NodeToken nd = NodeToken(changeTypeNode); - nd._nodetype = changeTypeNode; - nd.type = TokenKeywordVarType; - nd._vartype = __none__; - current_node = current_node->addChild(nd); - change_type.push_back(current_node); - parseExpr(); - if (Error.error) - { - return; - } + else if (Match(TokenIdentifier) && Match(TokenOpenParenthesis, 1)) { + sav_b = isStructFunction; + isStructFunction = false; + parseFunctionCall(); - string _signature = sigs.back() + "|" + current_node->getVarType()->varName; - sigs.pop_back(); - sigs.push_back(_signature); - if (current_node->isPointer) - { + isStructFunction = sav_b; + if (Error.error) { + return; + } else { + if (Match(TokenSemicolon)) { + Error.error = 0; + // current_node->addChild(res._nd); + // current_node=current_node->parent; + // result._nd = res._nd; + next(); + return; + } else { + Error.error = 1; + Error.error_message = + string_format("Expected ; %s", linepos().c_str()); + next(); + return; + } + } + } + if (Match(TokenIdentifier) && + (Match(TokenPlusPlus, 1) or Match(TokenMinusMinus, 1))) { + // NodeAssignement d = NodeAssignement(); + // printf("on est ici %s\r\n",current()->text.c_str()); + current_node = current_node->addChild(NodeToken(assignementNode)); + getVariable(true); + if (Error.error) { + return; + } + // NodeUnitary g = NodeUnitary(); + current_node = current_node->addChild(NodeToken(unitaryOpNode)); + prev(); + // printf("on est ici %s\r\n",current()->text.c_str()); + getVariable(false); + if (Error.error) { + return; + } + current_node->type = current()->type; + // current_node->addChild(NodeToken(current(), operatorNode)); + next(); + current_node = current_node->parent; + current_node = current_node->parent; + if (!Match(TokenSemicolon) && !Match(TokenCloseParenthesis)) { + Error.error = 1; + Error.error_message = string_format("Expected ; %s", linepos().c_str()); + next(); + return; + } + Error.error = 0; + next(); + return; + } else if (Match(TokenStar) && Match(TokenIdentifier, 1)) { + _asPointer = true; + next(); + + Error.error = 0; + // return; + } else if (Match(TokenIdentifier)) { + // NodeAssignement nd; + + current_node = current_node->addChild(NodeToken(assignementNode)); + getVariable(true); + if (Error.error) { + return; + } + NodeToken d = NodeToken(current_node->getChildAtPos(0)); - string _signature = sigs.back() + "*"; - sigs.pop_back(); - sigs.push_back(_signature); - } - current_node = current_node->parent; - change_type.pop_back(); - // arg.addChild(res._nd); - } - if (!Match(TokenCloseParenthesis)) - { + nodeTokenList.push(d); + _asPointer = false; + isPointer = false; + if (Error.error) { + return; + } - Error.error = 1; - Error.error_message = string_format("Expected ) %s", linepos().c_str()); - next(); - return; - } - next(); + if (Match(TokenSemicolon)) { Error.error = 0; - // result._nd = arg; - _signature = sigs.back() + ")"; - sigs.pop_back(); - sigs.push_back(_signature); + // result._nd = nd; + // current_node=current_node->parent; current_node = current_node->parent; - return; - } - void parseFunctionCall() - { -#ifdef PARSER_DEBUG - pushToConsole(string_format("functions:%s", __FUNCTION__)); - updateMem(); -#endif - // printf("calling function yves %s\r\n", current()->getText()); - - sav_t.push_back(*current()); next(); + nodeTokenList.pop(); + return; + } + if (Match(TokenEqual)) { + nodeTokenList.pop(); + // NodeToken nd; + NodeToken nd = NodeToken(changeTypeNode); + nd._nodetype = changeTypeNode; + nd.type = TokenKeywordVarType; + nd.setTokenText("yves"); + nd._vartype = tmp_sav->_vartype; + /* + if (tmp_sav->_vartype == __float__) + nd._vartype = __float__; + else + nd._vartype = __none__; + */ + current_node = current_node->addChild(nd); + change_type.push_back(current_node); next(); - - main_context.findCandidate(sav_t.back().getText()); - if (!main_context.findCandidate(sav_t.back().getText())) - { - if (struct_name.size() > 0) - { - string v_tmp = string_format("%s.%s", struct_name.c_str(), sav_t.back().getText()); - if (main_context.findCandidate((char *)v_tmp.c_str())) - isStructFunction = true; - } - } - // printf("calling function suite\r\n"); - parseArguments(); - // printf("calling function suite\r\n"); - if (Error.error) - { - return; - } - - _node_token_stack.push_back(current_node->children->back()); - // NodeToken d = current_node->children->back(); - current_node->children->pop_back(); - - (&sav_t.back())->addText(string_format("%s%s", sav_t.back().getText(), sigs.back().c_str())); - sigs.pop_back(); - main_context.findFunction(&sav_t.back()); - // NodeToken *t =search_result; - if (search_result == NULL) - { - - if (struct_name.size() > 0) - { - sav_t.push_back(sav_t.back()); - (&sav_t.back())->addText(string_format("%s.%s", struct_name.c_str(), sav_t.back().getText())); - main_context.findFunction(&sav_t.back()); - isStructFunction = true; - sav_t.pop_back(); - // sav_t.pop_back(); - } - if (search_result == NULL) - { - - for (int i = 0; i < external_links.size(); i++) - { - // printf("comparing %s ,%s \n\r", external_links[i].signature.c_str(), external_links[i].signature.c_str()); - // bool - found = false; - - if (strstr(external_links[i].signature.c_str(), "Args") != NULL) - { - int l = strstr(external_links[i].signature.c_str(), "Args") - external_links[i].signature.c_str(); - if (l > 0) - l--; - if (strncmp(external_links[i].signature.c_str(), sav_t.back().getText(), l) == 0) - { - found = true; - } - } - else if (external_links[i].signature.compare(string(sav_t.back().getText())) == 0) - { - found = true; - } - if (found) - { - // printf("her\n\r"); - savestacksize = stack_size; - _node_token_stack.push_back(current_node); - current_node = &program; - // string toinsert = external_links[i].name; //"external " + external_links[i].out + " " + external_links[i].name + "("+external_links[i].in + ");"; - // printf("on inseet %s\n", external_links[i].name.c_str()); - // main_script.previousChar(); - extra_script.clear(); - - extra_script.addContent((char *)(external_links[i].name.c_str())); - extra_script.init(); - _tks = &_extra_tks; - // extra_script.nextChar(); - - _tks->clear(); - - // next(); - // prev(); - pos_in_line = 0; - insecond = true; - _tks->tokenizelow(&extra_script, true, true, 20); - insecond = false; - // printf("%s \n\r",next()->getText()); - // _for_display=false; - - // printf("%s \n\r",next()->getText()); - // printf("%s \n\r",next()->getText()); - - // prev(); - sav_b = isStructFunction; - isStructFunction = false; - parseType(); - - if (Error.error) - { - // printf("ice\n\r"); - return; - } - - parseDefFunction(); - if (Error.error) - { - // printf("cold\n\r"); - return; - } - current_node = _node_token_stack.back(); - _node_token_stack.pop_back(); - isStructFunction = sav_b; - break; - // return; - } - } - _tks = &__tks; - extra_script.clear(); - _extra_tks.clear(); - stack_size = savestacksize; - // printf("looking for %s\n\r", sav_t.back().getText()); - main_context.findFunction(&sav_t.back()); - if (search_result == NULL) - { - Error.error = 1; - Error.error_message = string_format("function %s not found %s", sav_t.back().getText(), linepos().c_str()); - return; - } - // current()->type=TokenSemicolon; - } - } - - // NodeToken *res=search_result; - - NodeToken _nd = NodeToken(*search_result); - if (_nd._nodetype == (int)defExtFunctionNode) - { - - _nd._nodetype = extCallFunctionNode; - } - else // if (_nd._nodetype == (int)defFunctionNode) - { - _nd._nodetype = callFunctionNode; - } - - // NodeExtCallFunction function = NodeExtCallFunction(t); - _nd.target = search_result_index; - current_node = current_node->addChild(_nd); - // current_node->copyChildren(search_result); - // current_node->addChild(NodeToken(search_result->getChildAtPos(0))); - - // if (search_result->getChildAtPos(0)->_vartype == __float__ and change_type.size() > 0) - // change_type.back()->_vartype = __float__; - - if (change_type.size() > 0) - { - if (search_result->getChildAtPos(0)->children_size() == 0 && !search_result->getChildAtPos(0)->asPointer) - change_type.back()->isPointer = search_result->getChildAtPos(0)->isPointer; // n,ew modif here - if (change_type.back()->_vartype != __float__) - { - if (search_result->getChildAtPos(0)->_vartype == __float__ || search_result->getChildAtPos(0)->_vartype == __uint32_t__) - { - change_type.back()->_vartype = search_result->getChildAtPos(0)->_vartype; - } - } - } - // NodeToken *o = current_node->addChild(NodeToken(search_result->getChildAtPos(1))); - // o->copyChildren(search_result->getChildAtPos(1)); - - // sav_nb_arg = function._link->getChildAtPos(1)->children_size(); - - nb_sav_args.push_back(search_result->getChildAtPos(1)->children_size()); - if (isStructFunction) - { - // nb_sav_args.push_back( nb_sav_args.back()-1); - isStructFunction = false; - } - for (int i = 0; i < search_result->getChildAtPos(1)->children_size(); i++) - { - if (search_result->getChildAtPos(1)->getChildAtPos(i)->_vartype == __Args__) - { - - nb_sav_args.pop_back(); - nb_sav_args.push_back(999); - } + parseExpr(); + if (Error.error) { + return; } - current_node->_vartype = search_result->getChildAtPos(0)->_vartype; - - current_node->addChild(_node_token_stack.back()); - _node_token_stack.pop_back(); - - if (nb_sav_args.back() != nb_args.back() and nb_sav_args.back() != 999) // if (sav_nb_arg != nb_args.back()) - { - Error.error = 1; - - Error.error_message = string_format("Expected %d arguments got %d %s", nb_sav_args.back(), nb_args.back(), linepos().c_str()); - return; + if (!Match(TokenSemicolon) && !Match(TokenCloseParenthesis)) { + Error.error = 1; + Error.error_message = + string_format("Expected ici ; %s", linepos().c_str()); + // next(); + return; } - nb_args.pop_back(); - nb_sav_args.pop_back(); - sav_t.pop_back(); + // current_node->addChild(left._nd); + // current_node->addChild(right._nd); Error.error = 0; + // result._nd = nd; current_node = current_node->parent; + current_node = current_node->parent; // expr + change_type.pop_back(); + next(); return; - } - - void parseComparaison() - { -#ifdef PARSER_DEBUG - updateMem(); -#endif - - Error.error = 0; - - current_node = current_node->addChild(NodeToken(current(), comparatorNode)); - - current_node->setTargetText(targetList.pop()); - parseExprAndOr(); - // parseExprConditionnal(); - if (Error.error) - { - return; - } + } else if (Match(TokenPlusEqual) || Match(TokenMinusEqual) || + Match(TokenStarEqual) || Match(TokenSlashEqual)) { + sav_t.push_back(*current()); + // NodeToken nd; + NodeToken nd = NodeToken(changeTypeNode); + nd._nodetype = changeTypeNode; + nd.type = TokenKeywordVarType; + nd.setTokenText("yves"); + nd._vartype = tmp_sav->_vartype; + current_node = current_node->addChild(nd); + change_type.push_back(current_node); next(); - Error.error = 0; - current_node = current_node->parent; + current_node = current_node->addChild(NodeToken(binOpNode)); + NodeToken *_d = current_node->addChild(nodeTokenList.pop()); + switch (_d->_nodetype) { + case storeExtGlocalVariableNode: + _d->_nodetype = extGlobalVariableNode; + break; + case storeGlobalVariableNode: + _d->_nodetype = globalVariableNode; + break; + case storeLocalVariableNode: + _d->_nodetype = localVariableNode; + break; + case storeLocalVariableNodeAsRegister: + _d->_nodetype = localVariableNodeAsRegister; + break; + } + __t = sav_t.back(); + switch (__t.type) { + case TokenPlusEqual: + __t.type = TokenAddition; + break; + case TokenMinusEqual: + __t.type = TokenSubstraction; + break; + case TokenStarEqual: + __t.type = TokenStar; + break; + case TokenSlashEqual: + __t.type = TokenSlash; + break; + } + // current_node->addChild(NodeToken(&t,operatorNode)); + current_node->type = __t.type; + sav_t.pop_back(); + parseExpr(); + if (Error.error) { + return; + } + if (Match(TokenSemicolon)) { + Error.error = 0; + // result._nd = nd; + current_node = current_node->parent; + current_node = current_node->parent; + current_node = current_node->parent; + next(); + // nodeTokenList.pop(); + return; + } else { + Error.error = 1; + Error.error_message = + string_format("Expected ; %s", linepos().c_str()); + return; + } + } + + else { + Error.error = 1; + Error.error_message = string_format("Expected = %s", linepos().c_str()); return; + } } - void parseStatement() - { -#ifdef PARSER_DEBUG - updateMem(); -#endif - isPointer = false; - sav_token.clear(); - Error.error = 0; - current_node->addChild(NodeToken(statementNode)); - updateMem(); - if (Match(TokenString)) - { - current_node->addChild(NodeToken(current(), stringNode)); - next(); - return; - } - else if (Match(TokenKeywordBreak)) - { - string c = findForWhile(); - if (c.compare("__") == 0) - { - Error.error = 1; - Error.error_message = string_format("nor For ar while found for break %s", linepos().c_str()); - return; - } - next(); - if (Match(TokenSemicolon)) - { - current_node->addChild(NodeToken(c, breakNode)); - next(); - return; - } - else - { - Error.error = 1; - Error.error_message = string_format("d Expected ; %s", linepos().c_str()); - return; - } - } - else if (Match(TokenKeywordContinue)) - { - string c = findForWhile(); - if (c.compare("__") == 0) - { - Error.error = 1; - Error.error_message = string_format("nor For ar while found for continue %s", linepos().c_str()); - return; - } - next(); - if (Match(TokenSemicolon)) - { - current_node->addChild(NodeToken(c, continueNode)); - next(); - return; - } - else - { - Error.error = 1; - Error.error_message = string_format("d Expected ; %s", linepos().c_str()); - return; - } - } - else if (Match(TokenKeywordReturn)) - { - next(); - if (Match(TokenSemicolon)) - { - current_node->addChild(NodeToken(returnNode)); - next(); - return; - } - else - { - current_node = current_node->addChild(NodeToken(returnNode)); - - NodeToken nd = NodeToken(changeTypeNode); - nd._nodetype = changeTypeNode; - nd.type = TokenKeywordVarType; - nd.setTokenText("yevbs"); - nd._vartype = __none__; - if (lasttype != NULL) - { - nd._vartype = lasttype->_vartype; - /* - if (lasttype->_vartype == __float__) - { - nd._vartype = __float__; - } - */ - } - else - { - Error.error = 1; - Error.error_message = string_format("issue with return %s", linepos().c_str()); - return; - } + else if (Match(TokenKeywordElse)) { + // on tente le for(){} + // token *fort = current(); + // Context cntx; + // cntx.name = current()->text; + // //printf("entering f %d %s %s %x\n", + // current_cntx->_global->children_size(), + // current_cntx->_global->name.c_str(), current()->text.c_str(), + // (uint64_t)current_cntx->_global); current_cntx = + // (*(current_cntx)).addChild(cntx); + current_cntx = current_cntx->addChild(Context()); + // current_cntx = k; + // string target =string_format("label_%d%s",for_if_num,k->name.c_str()); + targetList.push(string_format("label_%d", for_if_num)); + //=target; + for_if_num++; + + // NodeElse ndf = NodeElse(fort); + // ndf.target = targetList.pop(); + // current_node = current_node->addChild(ndf); + current_node = current_node->addChild( + NodeToken(current(), elseNode, targetList.pop())); + next(); + + if (Match(TokenOpenCurlyBracket)) { + parseBlockStatement(); + if (Error.error) { + return; + } + } else { + // next(); + parseStatement(); + if (Error.error) { + return; + } + } + + // current_node->target=target; + + // resParse result; + + Error.error = 0; + // result._nd = ndf; + // next(); + current_cntx = current_cntx->parent; + current_node = current_node->parent; + // current_node=current_node->parent; + return; + } else if (Match(TokenKeywordWhile)) { + // on tente le for(){} + sav_t.push_back(*current()); + // Context cntx; + // cntx.name = current()->text; + // //printf("entering f %d %s %s %x\n", + // current_cntx->_global->children_size(), + // current_cntx->_global->name.c_str(), current()->text.c_str(), + // (uint64_t)current_cntx->_global); + // Context *k = (*(current_cntx)).addChild(cntx); + // current_cntx = (*(current_cntx)).addChild(cntx);; + current_cntx = current_cntx->addChild(Context()); + targetList.push(string_format("label_%d", for_if_num)); + //=target; + for_if_num++; + next(); + if (Match(TokenOpenParenthesis)) { + // NodeWhile ndf = NodeWhile(fort); + // ndf.target = target; + // current_node = current_node->addChild(ndf); + current_node = current_node->addChild( + NodeToken(&sav_t.back(), whileNode, targetList.get())); + next(); - current_node = current_node->addChild(nd); - change_type.push_back(current_node); - parseExpr(); - if (Error.error) - { - return; - } - if (Match(TokenSemicolon)) - { - Error.error = 0; - current_node = current_node->parent; - // res._nd = var; - - current_node = current_node->parent; - change_type.pop_back(); - next(); - return; - } - else - { - Error.error = 1; - Error.error_message = string_format("d Expected ; %s", linepos().c_str()); - return; - } - } + // printf(" *************** on parse comp/n"); + parseComparaison(); + // parseExpr(); + if (Error.error) { + return; } - - else if (Match(TokenIdentifier) && Match(TokenOpenParenthesis, 1)) - { - sav_b = isStructFunction; - isStructFunction = false; - parseFunctionCall(); - - isStructFunction = sav_b; - if (Error.error) - { - return; - } - else - { - if (Match(TokenSemicolon)) - { - Error.error = 0; - // current_node->addChild(res._nd); - // current_node=current_node->parent; - // result._nd = res._nd; - next(); - return; - } - else - { - Error.error = 1; - Error.error_message = string_format("Expected ; %s", linepos().c_str()); - next(); - return; - } - } - } - if (Match(TokenIdentifier) && (Match(TokenPlusPlus, 1) or Match(TokenMinusMinus, 1))) - { - // NodeAssignement d = NodeAssignement(); - // printf("on est ici %s\r\n",current()->text.c_str()); - current_node = current_node->addChild(NodeToken(assignementNode)); - getVariable(true); - if (Error.error) - { - return; - } - // NodeUnitary g = NodeUnitary(); - current_node = current_node->addChild(NodeToken(unitaryOpNode)); - prev(); - // printf("on est ici %s\r\n",current()->text.c_str()); - getVariable(false); - if (Error.error) - { - return; - } - current_node->type = current()->type; - // current_node->addChild(NodeToken(current(), operatorNode)); - next(); - current_node = current_node->parent; - current_node = current_node->parent; - if (!Match(TokenSemicolon) && !Match(TokenCloseParenthesis)) - { - Error.error = 1; - Error.error_message = string_format("Expected ; %s", linepos().c_str()); - next(); - return; - } - Error.error = 0; - next(); + // targetList.pop(); + ////printf("on a parse %s\n",comparator._nd._token->text.c_str()); + // printf(" *************** on parse inc/n"); + // next(); + if (Match(TokenOpenCurlyBracket)) { + parseBlockStatement(); + if (Error.error) { return; + } + } else { + // next(); + parseStatement(); + if (Error.error) { + return; + } } - else if (Match(TokenStar) && Match(TokenIdentifier, 1)) - { - _asPointer = true; - next(); - - Error.error = 0; - // return; - } - else if (Match(TokenIdentifier)) - { - // NodeAssignement nd; - - current_node = current_node->addChild(NodeToken(assignementNode)); - getVariable(true); - if (Error.error) - { - return; - } - NodeToken d = NodeToken(current_node->getChildAtPos(0)); - nodeTokenList.push(d); - _asPointer = false; - isPointer = false; - if (Error.error) - { - return; - } + // current_node->target=target; - if (Match(TokenSemicolon)) - { - Error.error = 0; - // result._nd = nd; - // current_node=current_node->parent; - current_node = current_node->parent; - next(); - nodeTokenList.pop(); - return; - } - if (Match(TokenEqual)) - { - nodeTokenList.pop(); - // NodeToken nd; - NodeToken nd = NodeToken(changeTypeNode); - nd._nodetype = changeTypeNode; - nd.type = TokenKeywordVarType; - nd.setTokenText("yves"); - nd._vartype = tmp_sav->_vartype; - /* - if (tmp_sav->_vartype == __float__) - nd._vartype = __float__; - else - nd._vartype = __none__; - */ - current_node = current_node->addChild(nd); - change_type.push_back(current_node); - next(); - parseExpr(); - if (Error.error) - { - return; - } - - if (!Match(TokenSemicolon) && !Match(TokenCloseParenthesis)) - { - Error.error = 1; - Error.error_message = string_format("Expected ici ; %s", linepos().c_str()); - // next(); - return; - } - // current_node->addChild(left._nd); - // current_node->addChild(right._nd); - Error.error = 0; - // result._nd = nd; - current_node = current_node->parent; - current_node = current_node->parent; // expr + // resParse result; - change_type.pop_back(); - next(); - return; - } - else if (Match(TokenPlusEqual) || Match(TokenMinusEqual) || Match(TokenStarEqual) || Match(TokenSlashEqual)) - { - sav_t.push_back(*current()); - // NodeToken nd; - NodeToken nd = NodeToken(changeTypeNode); - nd._nodetype = changeTypeNode; - nd.type = TokenKeywordVarType; - nd.setTokenText("yves"); - nd._vartype = tmp_sav->_vartype; - - current_node = current_node->addChild(nd); - change_type.push_back(current_node); - next(); - current_node = current_node->addChild(NodeToken(binOpNode)); - NodeToken *_d = current_node->addChild(nodeTokenList.pop()); - switch (_d->_nodetype) - { - case storeExtGlocalVariableNode: - _d->_nodetype = extGlobalVariableNode; - break; - case storeGlobalVariableNode: - _d->_nodetype = globalVariableNode; - break; - case storeLocalVariableNode: - _d->_nodetype = localVariableNode; - break; - case storeLocalVariableNodeAsRegister: - _d->_nodetype = localVariableNodeAsRegister; - break; - } - __t = sav_t.back(); - switch (__t.type) - { - case TokenPlusEqual: - __t.type = TokenAddition; - break; - case TokenMinusEqual: - __t.type = TokenSubstraction; - break; - case TokenStarEqual: - __t.type = TokenStar; - break; - case TokenSlashEqual: - __t.type = TokenSlash; - break; - } - // current_node->addChild(NodeToken(&t,operatorNode)); - current_node->type = __t.type; - sav_t.pop_back(); - - parseExpr(); - if (Error.error) - { - return; - } - if (Match(TokenSemicolon)) - { - Error.error = 0; - // result._nd = nd; - current_node = current_node->parent; - current_node = current_node->parent; - current_node = current_node->parent; - next(); - // nodeTokenList.pop(); - return; - } - else - { - Error.error = 1; - Error.error_message = string_format("Expected ; %s", linepos().c_str()); - return; - } - } + Error.error = 0; + // result._nd = ndf; + // next(); + current_cntx = current_cntx->parent; + current_node = current_node->parent; + sav_t.pop_back(); + // current_node=current_node->parent; + return; + } else { + // resParse res; + Error.error = 1; + Error.error_message = + string_format("expecting ( %s", linepos().c_str()); + next(); + return; + } + } else if (Match(TokenKeywordIf)) { + // on tente le for(){} + // token *fort=current(); + // Context cntx; + // cntx.name = current()->text; + // //printf("entering f %d %s %s %x\n", + // current_cntx->_global->children_size(), + // current_cntx->_global->name.c_str(), current()->text.c_str(), + // (uint64_t)current_cntx->_global); Context *k = + // (*(current_cntx)).addChild(cntx); + current_cntx = current_cntx->addChild(Context()); + // string target =string_format("label_%d%s",for_if_num,k->name.c_str()); + targetList.push(string_format("label_%d", for_if_num)); + //=target; + for_if_num++; + // next(); + if (Match(TokenOpenParenthesis, 1)) { + // NodeIf ndf = NodeIf(current()); + // ndf.target = targetList.get(); + + // current_node = current_node->addChild(ndf); + current_node = current_node->addChild( + NodeToken(current(), ifNode, targetList.get())); + next(); + next(); - else - { - Error.error = 1; - Error.error_message = string_format("Expected = %s", linepos().c_str()); - return; - } + // printf(" *************** on parse comp/n"); + parseComparaison(); + if (Error.error) { + return; } - - else if (Match(TokenKeywordElse)) - { - // on tente le for(){} - // token *fort = current(); - // Context cntx; - // cntx.name = current()->text; - // //printf("entering f %d %s %s %x\n", current_cntx->_global->children_size(), current_cntx->_global->name.c_str(), current()->text.c_str(), (uint64_t)current_cntx->_global); - // current_cntx = (*(current_cntx)).addChild(cntx); - current_cntx = current_cntx->addChild(Context()); - // current_cntx = k; - // string target =string_format("label_%d%s",for_if_num,k->name.c_str()); - targetList.push(string_format("label_%d", for_if_num)); - //=target; - for_if_num++; - - // NodeElse ndf = NodeElse(fort); - // ndf.target = targetList.pop(); - // current_node = current_node->addChild(ndf); - current_node = current_node->addChild(NodeToken(current(), elseNode, targetList.pop())); - next(); - - if (Match(TokenOpenCurlyBracket)) - { - parseBlockStatement(); - if (Error.error) - { - return; - } - } - else - { - // next(); - parseStatement(); - if (Error.error) - { - return; - } - } - - // current_node->target=target; - - // resParse result; - - Error.error = 0; - // result._nd = ndf; - // next(); - current_cntx = current_cntx->parent; - current_node = current_node->parent; - // current_node=current_node->parent; + ////printf("on a parse %s\n",comparator._nd._token->text.c_str()); + // printf(" *************** on parse inc/n"); + if (Match(TokenOpenCurlyBracket)) { + parseBlockStatement(); + if (Error.error) { + return; + } + } else { + // next(); + parseStatement(); + if (Error.error) { return; + } } - else if (Match(TokenKeywordWhile)) - { - // on tente le for(){} - sav_t.push_back(*current()); - // Context cntx; - // cntx.name = current()->text; - // //printf("entering f %d %s %s %x\n", current_cntx->_global->children_size(), current_cntx->_global->name.c_str(), current()->text.c_str(), (uint64_t)current_cntx->_global); - // Context *k = (*(current_cntx)).addChild(cntx); - // current_cntx = (*(current_cntx)).addChild(cntx);; - current_cntx = current_cntx->addChild(Context()); - targetList.push(string_format("label_%d", for_if_num)); - //=target; - for_if_num++; - next(); - if (Match(TokenOpenParenthesis)) - { - // NodeWhile ndf = NodeWhile(fort); - // ndf.target = target; - // current_node = current_node->addChild(ndf); - current_node = current_node->addChild(NodeToken(&sav_t.back(), whileNode, targetList.get())); - next(); - - // printf(" *************** on parse comp/n"); - parseComparaison(); - // parseExpr(); - if (Error.error) - { - return; - } - // targetList.pop(); - ////printf("on a parse %s\n",comparator._nd._token->text.c_str()); - // printf(" *************** on parse inc/n"); - // next(); - if (Match(TokenOpenCurlyBracket)) - { - parseBlockStatement(); - if (Error.error) - { - return; - } - } - else - { - // next(); - parseStatement(); - if (Error.error) - { - return; - } - } + // current_node->target=target; - // current_node->target=target; + // resParse result; - // resParse result; + Error.error = 0; + // result._nd = ndf; + // next(); + current_cntx = current_cntx->parent; + current_node = current_node->parent; + // current_node=current_node->parent; + return; + } else { + // resParse res; + Error.error = 1; + Error.error_message = + string_format("expecting ( %s", linepos().c_str()); + next(); + return; + } + } else if (Match(TokenKeywordFor)) { + // on tente le for(){} + // token *fort=current(); + // Context cntx; + // cntx.name = current()->text; + // //printf("entering f %d %s %s %x\n", + // current_cntx->_global->children_size(), + // current_cntx->_global->name.c_str(), current()->text.c_str(), + // (uint64_t)current_cntx->_global); current_cntx = + // current_cntx->addChild(cntx); + current_cntx = current_cntx->addChild(Context()); + + // current_cntx = k; + // string target + // =string_format("label_%d%s",for_if_num,current_cntx->name.c_str()); + targetList.push(string_format("label_%d", for_if_num)); + //=target; + for_if_num++; + // next(); + if (Match(TokenOpenParenthesis, 1)) { + // NodeFor ndf = NodeFor(current()); + // ndf.target = targetList.get(); + next(); + // current_node = current_node->addChild(ndf); + current_node = current_node->addChild( + NodeToken(current(), forNode, targetList.get())); + next(); + current_node = current_node->addChild(NodeToken(statementNode)); + // __current.push( current()); + _is_variable_as_register.set(false); + _for_num++; - Error.error = 0; - // result._nd = ndf; - // next(); - current_cntx = current_cntx->parent; - current_node = current_node->parent; - sav_t.pop_back(); - // current_node=current_node->parent; - return; - } - else - { - // resParse res; - Error.error = 1; - Error.error_message = string_format("expecting ( %s", linepos().c_str()); - next(); - return; - } + // printf("in for :%d\n",_for_depth_reg.get()); + if (_for_depth_reg.get() <= _MAX_FOR_DEPTH_REG) { + _is_variable_as_register.set(true); } - else if (Match(TokenKeywordIf)) - { - // on tente le for(){} - // token *fort=current(); - // Context cntx; - // cntx.name = current()->text; - // //printf("entering f %d %s %s %x\n", current_cntx->_global->children_size(), current_cntx->_global->name.c_str(), current()->text.c_str(), (uint64_t)current_cntx->_global); - // Context *k = (*(current_cntx)).addChild(cntx); - current_cntx = current_cntx->addChild(Context()); - // string target =string_format("label_%d%s",for_if_num,k->name.c_str()); - targetList.push(string_format("label_%d", for_if_num)); - //=target; - for_if_num++; - // next(); - if (Match(TokenOpenParenthesis, 1)) - { - // NodeIf ndf = NodeIf(current()); - // ndf.target = targetList.get(); - - // current_node = current_node->addChild(ndf); - current_node = current_node->addChild(NodeToken(current(), ifNode, targetList.get())); - next(); - next(); - // printf(" *************** on parse comp/n"); - parseComparaison(); - if (Error.error) - { - return; - } - ////printf("on a parse %s\n",comparator._nd._token->text.c_str()); - // printf(" *************** on parse inc/n"); - if (Match(TokenOpenCurlyBracket)) - { - parseBlockStatement(); - if (Error.error) - { - return; - } - } - else - { - // next(); - parseStatement(); - if (Error.error) - { - return; - } - } - // current_node->target=target; - - // resParse result; - - Error.error = 0; - // result._nd = ndf; - // next(); - current_cntx = current_cntx->parent; - current_node = current_node->parent; - // current_node=current_node->parent; - return; - } - else - { - // resParse res; - Error.error = 1; - Error.error_message = string_format("expecting ( %s", linepos().c_str()); - next(); - return; - } + parseStatement(); + // _for_depth_reg++; + _is_variable_as_register.set(false); + // __sav_pos = _tks->position; + // deleteNotNeededToken(__current.pop(), current()); + // _tks->position = __sav_pos; + // parseStatement(); + if (Error.error) { + return; } - else if (Match(TokenKeywordFor)) - { - // on tente le for(){} - // token *fort=current(); - // Context cntx; - // cntx.name = current()->text; - // //printf("entering f %d %s %s %x\n", current_cntx->_global->children_size(), current_cntx->_global->name.c_str(), current()->text.c_str(), (uint64_t)current_cntx->_global); - // current_cntx = current_cntx->addChild(cntx); - current_cntx = current_cntx->addChild(Context()); - - // current_cntx = k; - // string target =string_format("label_%d%s",for_if_num,current_cntx->name.c_str()); - targetList.push(string_format("label_%d", for_if_num)); - //=target; - for_if_num++; - // next(); - if (Match(TokenOpenParenthesis, 1)) - { - // NodeFor ndf = NodeFor(current()); - // ndf.target = targetList.get(); - next(); - // current_node = current_node->addChild(ndf); - current_node = current_node->addChild(NodeToken(current(), forNode, targetList.get())); - next(); - current_node = current_node->addChild(NodeToken(statementNode)); - // __current.push( current()); - _is_variable_as_register.set(false); - _for_num++; - - // printf("in for :%d\n",_for_depth_reg.get()); - if (_for_depth_reg.get() <= _MAX_FOR_DEPTH_REG) - { - _is_variable_as_register.set(true); - } - - parseStatement(); - // _for_depth_reg++; - _is_variable_as_register.set(false); - // __sav_pos = _tks->position; - // deleteNotNeededToken(__current.pop(), current()); - // _tks->position = __sav_pos; - // parseStatement(); - if (Error.error) - { - return; - } - current_node = current_node->parent; - // printf(" *************** on parse comp/n"); - parseComparaison(); - if (Error.error) - { - return; - } - ////printf("on a parse %s\n",comparator._nd._token->text.c_str()); - // printf(" *************** on parse inc/n"); - // __current.push( current()); - current_node = current_node->addChild(NodeToken(statementNode)); - parseStatement(); - // __sav_pos = _tks->position; - // deleteNotNeededToken(__current.pop(), current()); - // _tks->position = __sav_pos; - // parseStatement(); - if (Error.error) - { - return; - } - current_node = current_node->parent; - if (Match(TokenOpenCurlyBracket)) - { - parseBlockStatement(); - if (Error.error) - { - return; - } - } - else - { - // next(); - parseStatement(); - if (Error.error) - { - return; - } - } - - // current_node->target=target; - - // resParse result; - - Error.error = 0; - // result._nd = ndf; - // next(); - _for_num--; - if(_for_num==0) - { - if(nbofextern==1) - { - NodeToken nd=NodeToken(onlyNode); - nd.addTargetText(extvariablestore); - current_node->addChild(nd); - } - nbofextern=0; - extvariablestore=""; - } - current_cntx = current_cntx->parent; - current_node = current_node->parent; - // current_node=current_node->parent; - // printf("end of for :%d\n", _for_depth_reg.get()); - _for_depth_reg.decrease(); - return; - } - else - { - // resParse res; - Error.error = 1; - Error.error_message = string_format("expecting ( %s", linepos().c_str()); - next(); - return; - } + current_node = current_node->parent; + // printf(" *************** on parse comp/n"); + parseComparaison(); + if (Error.error) { + return; + } + ////printf("on a parse %s\n",comparator._nd._token->text.c_str()); + // printf(" *************** on parse inc/n"); + // __current.push( current()); + current_node = current_node->addChild(NodeToken(statementNode)); + parseStatement(); + // __sav_pos = _tks->position; + // deleteNotNeededToken(__current.pop(), current()); + // _tks->position = __sav_pos; + // parseStatement(); + if (Error.error) { + return; } - - else if (Match(TokenKeywordVarType) or Match(TokenUserDefinedVariable)) - { - // printf("trying to create %s\n", current()->getText()); - parseType(); - varTypeEnum d = nodeTokenList.get().getVarType()->_varType; - if (Error.error) - { - Error.error = 1; - - return; - } - nodeTokenList.push(nodeTokenList.get()); - parseVariableForCreation(); - if (Error.error) - { - - return; - } - if (_for_depth_reg.get() <= _MAX_FOR_DEPTH_REG_2 and (d == __float__ or d == __int__ or d == __s_int__ or d == __uint8_t__ or d == __uint32_t__ or d == __uint16_t__)) - { - _is_variable_as_register.set(true); - } - // else - // _is_variable_as_register.set(false)); - - nodeTokenList.push(createNodeLocalVariableForCreation(nodeTokenList.pop(), nodeTokenList.pop())); - // if(_for_depth_reg<=_MAX_FOR_DEPTH_REG_2) - //_for_depth_reg++; - // YBA 27/02 - - // NodeToken _nd=nodeTokenList.get(); - _is_variable_as_register.set(false); - - current_cntx->addVariable(nodeTokenList.get()); // 27/05 - if (Match(TokenComma)) - { - while (Match(TokenComma)) - { - next(); - if (nodeTokenList.get()._nodetype == defLocalVariableNodeAsRegister) - { - NodeToken _nd = nodeTokenList.pop(); - _nd._nodetype = defLocalVariableNode; - nodeTokenList.pop(); - nodeTokenList.push(_nd); - } - nodeTokenList.push(nodeTokenList.get()); - parseVariableForCreation(); - if (Error.error) - { - - return; - } - nodeTokenList.push(createNodeLocalVariableForCreation(nodeTokenList.pop(), nodeTokenList.pop())); - current_cntx->addVariable(nodeTokenList.get()); - } - if (!Match(TokenSemicolon)) - { - Error.error = 1; - Error.error_message = string_format("Expected ; %s", linepos().c_str()); - next(); - return; - } - else - { - next(); - nodeTokenList.pop(); - return; - } - } - // nodeTokenList.pop(); - if (Match(TokenSemicolon)) - { - Error.error = 0; - // result._nd = var; - // _uniquesave= - if (nodeTokenList.get().type == TokenUserDefinedVariable) - { - - // printf("check if coinstructo\r\n"); - current()->addText(string_format("%s._@%s()", nodeTokenList.get().getVarType()->varName.c_str(), nodeTokenList.get().getVarType()->varName.c_str())); - main_context.findFunction(current()); - - if (search_result != NULL) - { - - // printf(" printf("we have contructore\r\n"); if coinstructo\r\n"); - NodeToken nd = nodeTokenList.get(); - nd._nodetype = callConstructorNode; - current_node->addChild(nodeTokenList.pop()); - current_node->addChild(nd); - nodeTokenList.pop(); - next(); - return; - } - else - { - current_node->addChild(nodeTokenList.pop()); - nodeTokenList.pop(); - next(); - return; - } - } - current_node->addChild(nodeTokenList.pop()); - nodeTokenList.pop(); - // current_cntx->variables.back().text; - // current_node = current_node->parent; - // _uniquesave->text=current_cntx->variables.back().text; - next(); - return; - } - - if (Match(TokenEqual)) - { - // NodeStatement ndsmt; - tmp_sav = current_node->addChild(nodeTokenList.get()); - - if (nodeTokenList.get().type == TokenUserDefinedVariable) - { - - current_cntx->findVariable(nodeTokenList.get().getTokenText(), false); - if (search_result == NULL) - { - - Error.error = 1; - Error.error_message = string_format("impossible to find declaraiton for %s %s", current()->getText(), linepos().c_str()); - next(); - return; - } - - next(); - current()->addText(string_format("%s._@%s", search_result->getVarType()->varName.c_str(), current()->getText())); - NodeToken nd = NodeToken(*search_result); - // nd.copyChildren(search_result);//30/12 - - if (search_result->_nodetype == defGlobalVariableNode) - nd._nodetype = globalVariableNode; - else - { - if (search_result->_nodetype == defLocalVariableNodeAsRegister) - { - nd._nodetype = localVariableNodeAsRegister; - nd.target = search_result->target; - } - else - nd._nodetype = localVariableNode; // globalVariableNode; - } - nd.type = TokenUserDefinedVariableMemberFunction; - nd.isPointer = true; - nd._total_size = search_result->getVarType()->total_size; - - nodeTokenList.push(nd); - isStructFunction = true; - - parseFunctionCall(); - - if (Error.error) - { - return; - } - if (!Match(TokenSemicolon)) - { - Error.error = 1; - Error.error_message = string_format("Missing ; %s", linepos().c_str()); - return; - } - next(); - // current_node->getChildAtPos(current_node->children_size() - 1)->getChildAtPos(2)->getChildAtPos(0)->copyChildren(par); - isStructFunction = false; - Error.error = 0; - // current_node = current_node->parent; - return; - } - // NodeAssignement nd; - current_node = current_node->addChild(NodeToken(assignementNode)); - next(); - // auto left = createNodeLocalVariableForStore(var); - // copyPrty(type._nd, &var); - // current_node->addChild(left); - // _uniquesave= - NodeToken _uniquesave = nodeTokenList.pop(); - if (_uniquesave.getNodeTokenType() == defLocalVariableNode) - { - _uniquesave._nodetype = (int)storeLocalVariableNode; - } - else if (_uniquesave.getNodeTokenType() == defLocalVariableNodeAsRegister) - { - _uniquesave._nodetype = (int)storeLocalVariableNodeAsRegister; - } - else - { - _uniquesave._nodetype = (int)storeGlobalVariableNode; - } - current_node->addChild(_uniquesave); // createNodeLocalVariableForStore(nodeTokenList.pop())); - // _uniquesave->text=current_cntx->variables.back().text; - // NodeToken nd; - NodeToken nd = NodeToken(changeTypeNode); - nd._nodetype = changeTypeNode; - nd.type = TokenKeywordVarType; - nd.setTokenText("yevbs"); - nd._vartype = tmp_sav->_vartype; - /* - if (tmp_sav->_vartype == __float__) - nd._vartype = __float__; - else - nd._vartype = __none__; - */ - current_node = current_node->addChild(nd); - change_type.push_back(current_node); - parseExpr(); - - if (Error.error) - { - - return; - } - - if (!Match(TokenSemicolon)) - { - Error.error = 1; - Error.error_message = string_format("Expected ; %s", linepos().c_str()); - next(); - return; - } - // nd.addChild(left); - // nd.addChild(right._nd); - // ndsmt.addChild(nd); - Error.error = 0; - // result._nd = ndsmt; - // current_node->addChild(ndsmt); - current_node = current_node->parent; - - current_node = current_node->parent; // expr - change_type.pop_back(); - next(); - return; - } - - else - { - Error.error = 1; - Error.error_message = string_format(" expecting ; ou = %s", linepos().c_str()); - return; - } - + current_node = current_node->parent; + if (Match(TokenOpenCurlyBracket)) { + parseBlockStatement(); + if (Error.error) { return; - } - else - { - parseExpr(); - - if (Error.error) - { - return; - } - - if (!Match(TokenSemicolon) && !Match(TokenCloseParenthesis)) - { - Error.error = 1; - Error.error_message = string_format("Expected ici ; %s", linepos().c_str()); - // next(); - return; - } - current_node = current_node->parent; // expr - - change_type.pop_back(); - next(); + } + } else { + // next(); + parseStatement(); + if (Error.error) { return; + } } - } - void parseBlockStatement() - { -#ifdef PARSER_DEBUG - updateMem(); -#endif + // current_node->target=target; - updateMem(); - current_cntx = current_cntx->addChild(Context()); - block_statement_num++; + // resParse result; - current_node = current_node->addChild(NodeToken(current(), blockStatementNode)); - next(); - while (!Match(TokenCloseCurlyBracket) && !Match(TokenEndOfFile)) - { - // printf("on tente aouter un stamt\n"); - //.push( current()); - - parseStatement(); - // __sav_pos = _tks->position; - // deleteNotNeededToken(__current.pop(), current()); - // _tks->position = __sav_pos; - if (Error.error) - { - return; - } - } - if (Match(TokenEndOfFile)) - { - Error.error = 1; - Error.error_message = string_format("expected } %s", linepos().c_str()); - next(); - return; - } Error.error = 0; - // result._nd = nbStmnt; + // result._nd = ndf; + // next(); + _for_num--; + if (_for_num == 0) { + if (nbofextern == 1) { + NodeToken nd = NodeToken(onlyNode); + nd.addTargetText(extvariablestore); + current_node->addChild(nd); + } + nbofextern = 0; + extvariablestore = ""; + } current_cntx = current_cntx->parent; current_node = current_node->parent; + // current_node=current_node->parent; + // printf("end of for :%d\n", _for_depth_reg.get()); + _for_depth_reg.decrease(); + return; + } else { + // resParse res; + Error.error = 1; + Error.error_message = + string_format("expecting ( %s", linepos().c_str()); next(); return; + } } - void parseCreateArguments() - { -#ifdef PARSER_DEBUG - updateMem(); -#endif - Error.error = 0; - signature = "("; - - current_node = current_node->addChild(NodeToken(defInputArgumentsNode)); - if (isStructFunction) - { - NodeToken nd = NodeToken(); - nd.setTargetText("pointer"); - nd.isPointer = true; - nd.type = TokenUserDefinedVariable; - nd._nodetype = defLocalVariableNode; - nd.stack_pos = _STACK_SIZE; - current_node->addChild(nd); - current_cntx->addVariable(nd); - } - if (Match(TokenCloseParenthesis)) - { - // resParse result; - Error.error = 0; - // result._nd = arg; - current_node = current_node->parent; - signature = signature + ")"; - - // printf("on retourne with argh ide\n"); - return; - } - parseType(); - if (Error.error) - { - return; - } - signature = signature + nodeTokenList.get().getVarType()->varName; - if (nodeTokenList.get().isPointer) - signature = signature + "*"; - - parseVariableForCreation(); - if (Error.error) - { - return; - } - NodeToken _nd = nodeTokenList.pop(); - //_nd._nodetype=(int)defLocalVariableNode; - NodeToken _t = nodeTokenList.pop(); - - copyPrty(&_t, &_nd); - // if(_is_variable_as_register) - // _nd = NodeToken(_nd, defLocalVariableNodeAsRegister); - // else - _is_variable_as_register.set(false); - - if (_for_depth_reg.get() <= _MAX_FOR_DEPTH_REG_2) - { - _is_variable_as_register.set(true); - } - if (_is_variable_as_register.get()) - { - _nd = NodeToken(_nd, defLocalVariableNodeAsRegister); - _nd.target = _for_depth_reg.get(); - _for_depth_reg.increase(); - } - else - _nd = NodeToken(_nd, defLocalVariableNode); + else if (Match(TokenKeywordVarType) or Match(TokenUserDefinedVariable)) { + // printf("trying to create %s\n", current()->getText()); + parseType(); + varTypeEnum d = nodeTokenList.get().getVarType()->_varType; + if (Error.error) { + Error.error = 1; - _is_variable_as_register.set(false); + return; + } + nodeTokenList.push(nodeTokenList.get()); + parseVariableForCreation(); + if (Error.error) { - // NodeDefLocalVariable var = NodeDefLocalVariable(_nd); - // _nd._nodetype=(int)defLocalVariableNode; - // copyPrty(type._nd,&var); - current_node->addChild(_nd); - current_cntx->addVariable(_nd); - //_uniquesave->text=current_cntx->variables.back().text; - // arg.addChild(nd); - // next(); - // printf("current %s\n", tokenNames[current()->type].c_str()); - while (Match(TokenComma)) - { - next(); - parseType(); - if (Error.error) - { - return; - } - signature = signature + "|" + nodeTokenList.get().getVarType()->varName; - if (nodeTokenList.get().isPointer) - signature = signature + "*"; - parseVariableForCreation(); - if (Error.error) - { - return; - } - // NodeToken _nd = nodeTokenList.pop(); + return; + } + if (_for_depth_reg.get() <= _MAX_FOR_DEPTH_REG_2 and + (d == __float__ or d == __int__ or d == __s_int__ or + d == __uint8_t__ or d == __uint32_t__ or d == __uint16_t__)) { + _is_variable_as_register.set(true); + } + // else + // _is_variable_as_register.set(false)); + + nodeTokenList.push(createNodeLocalVariableForCreation( + nodeTokenList.pop(), nodeTokenList.pop())); + // if(_for_depth_reg<=_MAX_FOR_DEPTH_REG_2) + //_for_depth_reg++; + // YBA 27/02 + + // NodeToken _nd=nodeTokenList.get(); + _is_variable_as_register.set(false); + + current_cntx->addVariable(nodeTokenList.get()); // 27/05 + if (Match(TokenComma)) { + while (Match(TokenComma)) { + next(); + if (nodeTokenList.get()._nodetype == defLocalVariableNodeAsRegister) { NodeToken _nd = nodeTokenList.pop(); - //_nd._nodetype=(int)defLocalVariableNode; - NodeToken _t = nodeTokenList.pop(); + _nd._nodetype = defLocalVariableNode; + nodeTokenList.pop(); + nodeTokenList.push(_nd); + } + nodeTokenList.push(nodeTokenList.get()); + parseVariableForCreation(); + if (Error.error) { - copyPrty(&_t, &_nd); - if (_is_variable_as_register.get()) - { - _nd = NodeToken(_nd, defLocalVariableNodeAsRegister); - _nd.target = _for_depth_reg.get(); - } - else - _nd = NodeToken(_nd, defLocalVariableNode); - // NodeDefLocalVariable var = NodeDefLocalVariable(_nd); - // _nd._nodetype=(int)defLocalVariableNode; - // arg.addChild(var); - current_node->addChild(_nd); - current_cntx->addVariable(_nd); - //_uniquesave->text=current_cntx->variables.back().text; - // next(); - } - // prev(); - // resParse result; + return; + } + nodeTokenList.push(createNodeLocalVariableForCreation( + nodeTokenList.pop(), nodeTokenList.pop())); + current_cntx->addVariable(nodeTokenList.get()); + } + if (!Match(TokenSemicolon)) { + Error.error = 1; + Error.error_message = + string_format("Expected ; %s", linepos().c_str()); + next(); + return; + } else { + next(); + nodeTokenList.pop(); + return; + } + } + // nodeTokenList.pop(); + if (Match(TokenSemicolon)) { Error.error = 0; - signature = signature + ")"; - - current_node = current_node->parent; + // result._nd = var; + // _uniquesave= + if (nodeTokenList.get().type == TokenUserDefinedVariable) { + + // printf("check if coinstructo\r\n"); + current()->addText(string_format( + "%s._@%s()", nodeTokenList.get().getVarType()->varName.c_str(), + nodeTokenList.get().getVarType()->varName.c_str())); + main_context.findFunction(current()); + + if (search_result != NULL) { + + // printf(" printf("we have contructore\r\n"); if + // coinstructo\r\n"); + NodeToken nd = nodeTokenList.get(); + nd._nodetype = callConstructorNode; + current_node->addChild(nodeTokenList.pop()); + current_node->addChild(nd); + nodeTokenList.pop(); + next(); + return; + } else { + current_node->addChild(nodeTokenList.pop()); + nodeTokenList.pop(); + next(); + return; + } + } + current_node->addChild(nodeTokenList.pop()); + nodeTokenList.pop(); + // current_cntx->variables.back().text; + // current_node = current_node->parent; + // _uniquesave->text=current_cntx->variables.back().text; + next(); return; - } + } - void parseDefFunction() - { -#ifdef PARSER_DEBUG - updateMem(); -#endif - // printf("set up\n"); - _for_depth_reg.push(2); - _is_variable_as_register.push(false); - if (isStructFunction) - _for_depth_reg.increase(); - Error.error = 0; - bool ext_function = false; - bool is_asm = false; - // printf("entering function %s \r\n", current()->getText()); - if (isExternal) - { - ext_function = true; - isExternal = false; - } - if (isASM) - { - isASM = false; - is_asm = true; - } - // resParse result; - // Token func = *current(); - NodeToken nd; - main_context.findFunction(current()); - bool isdeclaration = false; - if (search_result != NULL) // if (current_cntx->findFunction(current()) != NULL) - { + if (Match(TokenEqual)) { + // NodeStatement ndsmt; + tmp_sav = current_node->addChild(nodeTokenList.get()); - if (search_result->_nodetype != declarationFunctionNode) - { + if (nodeTokenList.get().type == TokenUserDefinedVariable) { - Error.error = 1; - Error.error_message = string_format("function already %s declared in the scope for %s", current()->getText(), linepos().c_str()); - next(); - return; - } - else - { - isdeclaration = true; - } - } - if (ext_function) - { + current_cntx->findVariable(nodeTokenList.get().getTokenText(), false); + if (search_result == NULL) { - nd = NodeToken(current(), defExtFunctionNode); - // function.addChild( nodeTokenList.pop()); - // function.addChild(arguments._nd); + Error.error = 1; + Error.error_message = + string_format("impossible to find declaraiton for %s %s", + current()->getText(), linepos().c_str()); + next(); + return; + } - current_node = current_node->addChild(nd); - lasttype = current_node->addChild(nodeTokenList.pop()); + next(); + current()->addText(string_format( + "%s._@%s", search_result->getVarType()->varName.c_str(), + current()->getText())); + NodeToken nd = NodeToken(*search_result); + // nd.copyChildren(search_result);//30/12 - // lasttype = current_node; //modif 30/12 - } - else if (is_asm) - { - nd = NodeToken(current(), defAsmFunctionNode); - // function.addChild( nodeTokenList.pop()); - // function.addChild(arguments._nd); - - current_node = current_node->addChild(nd); - lasttype = current_node->addChild(nodeTokenList.pop()); - } + if (search_result->_nodetype == defGlobalVariableNode) + nd._nodetype = globalVariableNode; + else { + if (search_result->_nodetype == defLocalVariableNodeAsRegister) { + nd._nodetype = localVariableNodeAsRegister; + nd.target = search_result->target; + } else + nd._nodetype = localVariableNode; // globalVariableNode; + } + nd.type = TokenUserDefinedVariableMemberFunction; + nd.isPointer = true; + nd._total_size = search_result->getVarType()->total_size; + + nodeTokenList.push(nd); + isStructFunction = true; + + parseFunctionCall(); + + if (Error.error) { + return; + } + if (!Match(TokenSemicolon)) { + Error.error = 1; + Error.error_message = + string_format("Missing ; %s", linepos().c_str()); + return; + } + next(); + // current_node->getChildAtPos(current_node->children_size() - + // 1)->getChildAtPos(2)->getChildAtPos(0)->copyChildren(par); + isStructFunction = false; + Error.error = 0; + // current_node = current_node->parent; + return; + } + // NodeAssignement nd; + current_node = current_node->addChild(NodeToken(assignementNode)); + next(); + // auto left = createNodeLocalVariableForStore(var); + // copyPrty(type._nd, &var); + // current_node->addChild(left); + // _uniquesave= + NodeToken _uniquesave = nodeTokenList.pop(); + if (_uniquesave.getNodeTokenType() == defLocalVariableNode) { + _uniquesave._nodetype = (int)storeLocalVariableNode; + } else if (_uniquesave.getNodeTokenType() == + defLocalVariableNodeAsRegister) { + _uniquesave._nodetype = (int)storeLocalVariableNodeAsRegister; + } else { + _uniquesave._nodetype = (int)storeGlobalVariableNode; + } + current_node->addChild( + _uniquesave); // createNodeLocalVariableForStore(nodeTokenList.pop())); + // _uniquesave->text=current_cntx->variables.back().text; + // NodeToken nd; + NodeToken nd = NodeToken(changeTypeNode); + nd._nodetype = changeTypeNode; + nd.type = TokenKeywordVarType; + nd.setTokenText("yevbs"); + nd._vartype = tmp_sav->_vartype; + /* + if (tmp_sav->_vartype == __float__) + nd._vartype = __float__; else - { - nd = NodeToken(current(), defFunctionNode); - // function.addChild( nodeTokenList.pop()); - // function.addChild(arguments._nd); + nd._vartype = __none__; + */ + current_node = current_node->addChild(nd); + change_type.push_back(current_node); + parseExpr(); - current_node = current_node->addChild(nd); - lasttype = current_node->addChild(nodeTokenList.pop()); - } - // on ajoute un nouveau contexte + if (Error.error) { - current_cntx = current_cntx->addChild(Context()); - // current_cntx = k; - if (isStructFunction) - { - stack_size = _STACK_SIZE + 4; - current_node->type = TokenUserDefinedVariableMemberFunction; - } - else - { - stack_size = _STACK_SIZE; - } - block_statement_num = 0; - next(); - next(); - parseCreateArguments(); - if (Error.error) - { - return; - } - // printf("signature %s%s\r\n", current_node->getTokenText(), signature.c_str()); - current_node->setTokenText(string_format("%s%s", current_node->getTokenText(), signature.c_str())); - if (!isdeclaration) - { - main_context.addFunction(current_node); + return; } - if (!Match(TokenCloseParenthesis)) - { - Error.error = 1; - Error.error_message = string_format("Expected ) %s", linepos().c_str()); - next(); - return; + if (!Match(TokenSemicolon)) { + Error.error = 1; + Error.error_message = + string_format("Expected ; %s", linepos().c_str()); + next(); + return; } + // nd.addChild(left); + // nd.addChild(right._nd); + // ndsmt.addChild(nd); + Error.error = 0; + // result._nd = ndsmt; + // current_node->addChild(ndsmt); + current_node = current_node->parent; + + current_node = current_node->parent; // expr + change_type.pop_back(); next(); - if (ext_function) - { - if (Match(TokenSemicolon)) - { + return; + } - Error.error = 0; - current_cntx = current_cntx->parent; - current_node = current_node->parent; - next(); - _is_variable_as_register.pop(); - _for_depth_reg.pop(); - return; - } - else - { - Error.error = 1; - Error.error_message = string_format("Impossible to redifine external function expected ; %s", linepos().c_str()); - next(); - return; - } - } - else if (Match(TokenSemicolon)) - { - (_functions.back())->_nodetype = declarationFunctionNode; - Error.error = 0; - current_cntx = current_cntx->parent; - current_node = current_node->parent; - next(); - _is_variable_as_register.pop(); - _for_depth_reg.pop(); - return; - } - else - { - if (Match(TokenOpenCurlyBracket)) - { - - parseBlockStatement(); - if (Error.error) - { - return; - } + else { + Error.error = 1; + Error.error_message = + string_format(" expecting ; ou = %s", linepos().c_str()); + return; + } - // current_node->addChild(blocsmt._nd); - // current_node = current_node->parent; - current_node->stack_pos = stack_size; - // result._nd = function; - Error.error = 0; + return; + } else { + parseExpr(); - point_regnum = 4; + if (Error.error) { + return; + } -#ifndef __MEM_PARSER - buildParents(current_node); - - current_node->visitNode(); - current_node->clear(); - current_cntx->clear(); - _node_token_stack.clear(); - sav_token.clear(); - change_type.clear(); - // printf("after clean function %s\n",current_node->getTokenText()); - updateMem(); + if (!Match(TokenSemicolon) && !Match(TokenCloseParenthesis)) { + Error.error = 1; + Error.error_message = + string_format("Expected ici ; %s", linepos().c_str()); + // next(); + return; + } + current_node = current_node->parent; // expr + + change_type.pop_back(); + next(); + return; + } + } + + void parseBlockStatement() { +#ifdef PARSER_DEBUG + updateMem(); #endif - /* - #ifndef __MEM_PARSER - printf("on compile %s\r\n",current_node->text.c_str()); - __sav_pos = _tks->position; - buildParents(current_node); - - current_node->visitNode(current_node); - clearContext(tobedeted); - _tks->position = __sav_pos; - #endif - */ - // printf("on a visité\r\n"); - current_cntx = current_cntx->parent; - current_node = current_node->parent; - _is_variable_as_register.pop(); - _for_depth_reg.pop(); - return; - } - else - { - Error.error = 1; - Error.error_message = string_format("Expected { %s", linepos().c_str()); - next(); - return; - } - } - _is_variable_as_register.pop(); - _for_depth_reg.pop(); + updateMem(); + current_cntx = current_cntx->addChild(Context()); + block_statement_num++; + + current_node = + current_node->addChild(NodeToken(current(), blockStatementNode)); + next(); + while (!Match(TokenCloseCurlyBracket) && !Match(TokenEndOfFile)) { + // printf("on tente aouter un stamt\n"); + //.push( current()); + + parseStatement(); + // __sav_pos = _tks->position; + // deleteNotNeededToken(__current.pop(), current()); + // _tks->position = __sav_pos; + if (Error.error) { return; + } } - void parseTerm() - { + if (Match(TokenEndOfFile)) { + Error.error = 1; + Error.error_message = string_format("expected } %s", linepos().c_str()); + next(); + return; + } + Error.error = 0; + // result._nd = nbStmnt; + current_cntx = current_cntx->parent; + current_node = current_node->parent; + next(); + return; + } + + void parseCreateArguments() { #ifdef PARSER_DEBUG - updateMem(); + updateMem(); #endif - sav_token.push_back(current_node); - parseFactor(); - if (Error.error) - { - return; - } - if (Match(TokenQuestionMark)) - { - next(); - _node_token_stack.push_back(current_node->children->back()); - // NodeToken d = current_node->children->back(); - current_node->children->pop_back(); - current_node = current_node->addChild(NodeToken(ternaryIfNode)); - current_node->addChild(_node_token_stack.back()); - _node_token_stack.pop_back(); - current_node->addTargetText(string_format("label_tern_%d", for_if_num)); - for_if_num++; - parseExpr(); - if (Error.error) - { - return; - } - if (Match(TokenColon)) - { - next(); - - parseExpr(); - if (Error.error) - { - return; - } - } - else - { - Error.error = 1; - Error.error_message = string_format("Expected : %s", linepos().c_str()); - } - current_node = current_node->parent; - } + Error.error = 0; + signature = "("; + + current_node = current_node->addChild(NodeToken(defInputArgumentsNode)); + if (isStructFunction) { + NodeToken nd = NodeToken(); + nd.setTargetText("pointer"); + nd.isPointer = true; + nd.type = TokenUserDefinedVariable; + nd._nodetype = defLocalVariableNode; + nd.stack_pos = _STACK_SIZE; + current_node->addChild(nd); + current_cntx->addVariable(nd); + } + if (Match(TokenCloseParenthesis)) { + // resParse result; + Error.error = 0; + // result._nd = arg; + current_node = current_node->parent; + signature = signature + ")"; + + // printf("on retourne with argh ide\n"); + return; + } + parseType(); + if (Error.error) { + return; + } + signature = signature + nodeTokenList.get().getVarType()->varName; + if (nodeTokenList.get().isPointer) + signature = signature + "*"; - while (Match(TokenStar) || Match(TokenSlash) || Match(TokenModulo) || Match(TokenKeywordOr) || Match(TokenKeywordAnd) || Match(TokenPower)) - { - // token *op = current(); - sav_t.push_back(*current()); - next(); - // NodeBinOperator nodeopt; - _node_token_stack.push_back(current_node->children->back()); - // NodeToken d = current_node->children->back(); - current_node->children->pop_back(); - // current_node->children->erase( --current_node->children->end()); - current_node = current_node->addChild(NodeToken(binOpNode)); - current_node->addChild(_node_token_stack.back()); - _node_token_stack.pop_back(); - // current_node->parent->children->remove(current_node->parent->children->back()); - // NodeOperator opt = NodeOperator(op); - // current_node->addChild(NodeToken(&sav_t.back(), operatorNode)); - current_node->type = sav_t.back().type; - sav_t.pop_back(); - parseFactor(); - if (Error.error == 1) - { - return; - } - current_node = current_node->parent; - // left._nd = NodeBinOperator(left._nd, opt, right._nd); - } - // next(); - current_node = sav_token.back(); - sav_token.pop_back(); - // current_node = sav_pa; - // printf("exit term\n"); + parseVariableForCreation(); + if (Error.error) { + return; + } + NodeToken _nd = nodeTokenList.pop(); + //_nd._nodetype=(int)defLocalVariableNode; + NodeToken _t = nodeTokenList.pop(); + + copyPrty(&_t, &_nd); + // if(_is_variable_as_register) + // _nd = NodeToken(_nd, defLocalVariableNodeAsRegister); + // else + _is_variable_as_register.set(false); + + if (_for_depth_reg.get() <= _MAX_FOR_DEPTH_REG_2) { + _is_variable_as_register.set(true); + } + if (_is_variable_as_register.get()) { + _nd = NodeToken(_nd, defLocalVariableNodeAsRegister); + _nd.target = _for_depth_reg.get(); + _for_depth_reg.increase(); + } else + _nd = NodeToken(_nd, defLocalVariableNode); + + _is_variable_as_register.set(false); + + // NodeDefLocalVariable var = NodeDefLocalVariable(_nd); + // _nd._nodetype=(int)defLocalVariableNode; + // copyPrty(type._nd,&var); + current_node->addChild(_nd); + current_cntx->addVariable(_nd); + //_uniquesave->text=current_cntx->variables.back().text; + // arg.addChild(nd); + // next(); + // printf("current %s\n", tokenNames[current()->type].c_str()); + while (Match(TokenComma)) { + next(); + parseType(); + if (Error.error) { return; + } + signature = signature + "|" + nodeTokenList.get().getVarType()->varName; + if (nodeTokenList.get().isPointer) + signature = signature + "*"; + parseVariableForCreation(); + if (Error.error) { + return; + } + // NodeToken _nd = nodeTokenList.pop(); + NodeToken _nd = nodeTokenList.pop(); + //_nd._nodetype=(int)defLocalVariableNode; + NodeToken _t = nodeTokenList.pop(); + + copyPrty(&_t, &_nd); + if (_is_variable_as_register.get()) { + _nd = NodeToken(_nd, defLocalVariableNodeAsRegister); + _nd.target = _for_depth_reg.get(); + } else + _nd = NodeToken(_nd, defLocalVariableNode); + // NodeDefLocalVariable var = NodeDefLocalVariable(_nd); + // _nd._nodetype=(int)defLocalVariableNode; + // arg.addChild(var); + current_node->addChild(_nd); + current_cntx->addVariable(_nd); + //_uniquesave->text=current_cntx->variables.back().text; + // next(); } + // prev(); + // resParse result; + Error.error = 0; + signature = signature + ")"; - void parseExprAndOr() - { + current_node = current_node->parent; + return; + } + + void parseDefFunction() { #ifdef PARSER_DEBUG - updateMem(); + updateMem(); #endif + // printf("set up\n"); + _for_depth_reg.push(2); + _is_variable_as_register.push(false); + if (isStructFunction) + _for_depth_reg.increase(); + Error.error = 0; + bool ext_function = false; + bool is_asm = false; + // printf("entering function %s \r\n", current()->getText()); + if (isExternal) { + ext_function = true; + isExternal = false; + } + if (isASM) { + isASM = false; + is_asm = true; + } + // resParse result; + // Token func = *current(); + NodeToken nd; + main_context.findFunction(current()); + bool isdeclaration = false; + if (search_result != + NULL) // if (current_cntx->findFunction(current()) != NULL) + { - sav_token.push_back(current_node); - - parseExprConditionnal(); + if (search_result->_nodetype != declarationFunctionNode) { - if (Error.error == 1) - { - return; - } - while (Match(TokenDoubleUppersand) || Match(TokenDoubleOr)) - { + Error.error = 1; + Error.error_message = + string_format("function already %s declared in the scope for %s", + current()->getText(), linepos().c_str()); + next(); + return; + } else { + isdeclaration = true; + } + } + if (ext_function) { + + nd = NodeToken(current(), defExtFunctionNode); + // function.addChild( nodeTokenList.pop()); + // function.addChild(arguments._nd); + + current_node = current_node->addChild(nd); + lasttype = current_node->addChild(nodeTokenList.pop()); + + // lasttype = current_node; //modif 30/12 + } else if (is_asm) { + nd = NodeToken(current(), defAsmFunctionNode); + // function.addChild( nodeTokenList.pop()); + // function.addChild(arguments._nd); + + current_node = current_node->addChild(nd); + lasttype = current_node->addChild(nodeTokenList.pop()); + } else { + nd = NodeToken(current(), defFunctionNode); + // function.addChild( nodeTokenList.pop()); + // function.addChild(arguments._nd); + + current_node = current_node->addChild(nd); + lasttype = current_node->addChild(nodeTokenList.pop()); + } + // on ajoute un nouveau contexte + + current_cntx = current_cntx->addChild(Context()); + // current_cntx = k; + if (isStructFunction) { + stack_size = _STACK_SIZE + 4; + current_node->type = TokenUserDefinedVariableMemberFunction; + } else { + stack_size = _STACK_SIZE; + } + block_statement_num = 0; + next(); + next(); + parseCreateArguments(); + if (Error.error) { + return; + } + // printf("signature %s%s\r\n", current_node->getTokenText(), + // signature.c_str()); + current_node->setTokenText( + string_format("%s%s", current_node->getTokenText(), signature.c_str())); + if (!isdeclaration) { + main_context.addFunction(current_node); + } - sav_t.push_back(*current()); - next(); + if (!Match(TokenCloseParenthesis)) { + Error.error = 1; + Error.error_message = string_format("Expected ) %s", linepos().c_str()); + next(); + return; + } + next(); + if (ext_function) { + if (Match(TokenSemicolon)) { - _node_token_stack.push_back(current_node->children->back()); - // NodeToken d = current_node->children->back(); - current_node->children->pop_back(); - current_node = current_node->addChild(NodeToken(binOpNode)); - current_node->addChild(_node_token_stack.back()); - _node_token_stack.pop_back(); - // current_node->parent->children->remove(current_node->parent->children->back()); - if ((&sav_t.back())->type == TokenDoubleUppersand) - (&sav_t.back())->type = TokenKeywordAnd; - else - (&sav_t.back())->type = TokenKeywordOr; - // current_node->addChild(NodeToken(&sav_t.back(), operatorNode)); - current_node->type = sav_t.back().type; - sav_t.pop_back(); - parseExprConditionnal(); - if (Error.error == 1) - { - return; - } - current_node = current_node->parent; - // left._nd = NodeBinOperator(left._nd, opt, right._nd); - } - // next(); - current_node = sav_token.back(); - sav_token.pop_back(); - // lasttype=change_type.back(); - // printf("last type:%d\n",lasttype->_vartype); - // change_type.pop_back(); - // current_node = sav_pa; - // printf("exit expr"); Error.error = 0; + current_cntx = current_cntx->parent; + current_node = current_node->parent; + next(); + _is_variable_as_register.pop(); + _for_depth_reg.pop(); return; - } - void parseExprConditionnal() - { -#ifdef PARSER_DEBUG - updateMem(); -#endif - sav_token.push_back(current_node); - parseExpr(); - if (Error.error == 1) - { - return; - } - while (Match(TokenDoubleEqual) || Match(TokenNotEqual) ||Match(TokenLessOrEqualThan) || Match(TokenLessThan) || Match(TokenMoreOrEqualThan) || Match(TokenMoreThan)) - { - - // token *op = current(); - targetList.push(string_format("label_%d", for_if_num)); - //=target; - for_if_num++; - sav_t.push_back(*current()); - next(); - _node_token_stack.push_back(current_node->children->back()); - - current_node->children->pop_back(); - current_node = current_node->addChild(NodeToken(testNode)); - // current_node->addChild(NodeToken(&sav_t.back(), operatorNode)); - current_node->type = sav_t.back().type; - current_node->setTargetText(targetList.pop()); - // NodeToken nd; - NodeToken nd = NodeToken(changeTypeNode); - // nd._nodetype = changeTypeNode; - nd.type = TokenKeywordVarType; - nd._vartype = findfloat(_node_token_stack.back()); - if (nd._vartype != __float__) - { - nd._vartype = finduint32_t(_node_token_stack.back()); - } - current_node = current_node->addChild(nd); - change_type.push_back(current_node); - current_node->addChild(_node_token_stack.back()); - _node_token_stack.pop_back(); - current_node = current_node->parent; - NodeToken nd2 = NodeToken(changeTypeNode); - // nd._nodetype = changeTypeNode; - nd2.type = TokenKeywordVarType; - nd2._vartype = __none__; - current_node = current_node->addChild(nd); - // current_node->type=sav_t.back().type; - change_type.push_back(current_node); - sav_t.pop_back(); - parseExpr(); - if (Error.error == 1) - { - return; - } + } else { + Error.error = 1; + Error.error_message = string_format( + "Impossible to redifine external function expected ; %s", + linepos().c_str()); + next(); + return; + } + } else if (Match(TokenSemicolon)) { + (_functions.back())->_nodetype = declarationFunctionNode; + Error.error = 0; + current_cntx = current_cntx->parent; + current_node = current_node->parent; + next(); + _is_variable_as_register.pop(); + _for_depth_reg.pop(); + return; + } else { + if (Match(TokenOpenCurlyBracket)) { + + parseBlockStatement(); + if (Error.error) { + return; + } + + // current_node->addChild(blocsmt._nd); + // current_node = current_node->parent; + current_node->stack_pos = stack_size; + // result._nd = function; + Error.error = 0; - current_node = current_node->parent; - current_node = current_node->parent; - change_type.pop_back(); - } + point_regnum = 4; + +#ifndef __MEM_PARSER + buildParents(current_node); - current_node = sav_token.back(); - sav_token.pop_back(); + current_node->visitNode(); + current_node->clear(); + current_cntx->clear(); + _node_token_stack.clear(); + sav_token.clear(); + change_type.clear(); + // printf("after clean function %s\n",current_node->getTokenText()); + updateMem(); +#endif - Error.error = 0; + /* + #ifndef __MEM_PARSER + printf("on compile %s\r\n",current_node->text.c_str()); + __sav_pos = _tks->position; + buildParents(current_node); + + current_node->visitNode(current_node); + clearContext(tobedeted); + _tks->position = __sav_pos; + #endif + */ + // printf("on a visité\r\n"); + current_cntx = current_cntx->parent; + current_node = current_node->parent; + _is_variable_as_register.pop(); + _for_depth_reg.pop(); return; + } else { + Error.error = 1; + Error.error_message = string_format("Expected { %s", linepos().c_str()); + next(); + return; + } } - void parseExpr() - { + _is_variable_as_register.pop(); + _for_depth_reg.pop(); + return; + } + void parseTerm() { #ifdef PARSER_DEBUG - updateMem(); + updateMem(); #endif - sav_token.push_back(current_node); + sav_token.push_back(current_node); + parseFactor(); + if (Error.error) { + return; + } + if (Match(TokenQuestionMark)) { + next(); + _node_token_stack.push_back(current_node->children->back()); + // NodeToken d = current_node->children->back(); + current_node->children->pop_back(); + current_node = current_node->addChild(NodeToken(ternaryIfNode)); + current_node->addChild(_node_token_stack.back()); + _node_token_stack.pop_back(); + current_node->addTargetText(string_format("label_tern_%d", for_if_num)); + for_if_num++; + parseExpr(); + if (Error.error) { + return; + } + if (Match(TokenColon)) { + next(); - parseTerm(); - if (Error.error == 1) - { - return; + parseExpr(); + if (Error.error) { + return; } - while (Match(TokenAddition) || Match(TokenSubstraction) || Match(TokenShiftLeft) || Match(TokenShiftRight)) - { + } else { + Error.error = 1; + Error.error_message = string_format("Expected : %s", linepos().c_str()); + } + current_node = current_node->parent; + } - // token *op = current(); - sav_t.push_back(*current()); - next(); - _node_token_stack.push_back(current_node->children->back()); + while (Match(TokenStar) || Match(TokenSlash) || Match(TokenModulo) || + Match(TokenKeywordOr) || Match(TokenKeywordAnd) || + Match(TokenPower)) { + // token *op = current(); + sav_t.push_back(*current()); + next(); + // NodeBinOperator nodeopt; + _node_token_stack.push_back(current_node->children->back()); + // NodeToken d = current_node->children->back(); + current_node->children->pop_back(); + // current_node->children->erase( --current_node->children->end()); + current_node = current_node->addChild(NodeToken(binOpNode)); + current_node->addChild(_node_token_stack.back()); + _node_token_stack.pop_back(); + // current_node->parent->children->remove(current_node->parent->children->back()); + // NodeOperator opt = NodeOperator(op); + // current_node->addChild(NodeToken(&sav_t.back(), operatorNode)); + current_node->type = sav_t.back().type; + sav_t.pop_back(); + parseFactor(); + if (Error.error == 1) { + return; + } + current_node = current_node->parent; + // left._nd = NodeBinOperator(left._nd, opt, right._nd); + } + // next(); + current_node = sav_token.back(); + sav_token.pop_back(); + // current_node = sav_pa; + // printf("exit term\n"); + return; + } + + void parseExprAndOr() { +#ifdef PARSER_DEBUG + updateMem(); +#endif - current_node->children->pop_back(); - current_node = current_node->addChild(NodeToken(binOpNode)); - current_node->addChild(_node_token_stack.back()); - _node_token_stack.pop_back(); - // current_node->addChild(NodeToken(&sav_t.back(), operatorNode)); - current_node->type = sav_t.back().type; - sav_t.pop_back(); - parseTerm(); - if (Error.error == 1) - { - return; - } - current_node = current_node->parent; - } + sav_token.push_back(current_node); - current_node = sav_token.back(); - sav_token.pop_back(); + parseExprConditionnal(); - Error.error = 0; + if (Error.error == 1) { + return; + } + while (Match(TokenDoubleUppersand) || Match(TokenDoubleOr)) { + + sav_t.push_back(*current()); + next(); + + _node_token_stack.push_back(current_node->children->back()); + // NodeToken d = current_node->children->back(); + current_node->children->pop_back(); + current_node = current_node->addChild(NodeToken(binOpNode)); + current_node->addChild(_node_token_stack.back()); + _node_token_stack.pop_back(); + // current_node->parent->children->remove(current_node->parent->children->back()); + if ((&sav_t.back())->type == TokenDoubleUppersand) + (&sav_t.back())->type = TokenKeywordAnd; + else + (&sav_t.back())->type = TokenKeywordOr; + // current_node->addChild(NodeToken(&sav_t.back(), operatorNode)); + current_node->type = sav_t.back().type; + sav_t.pop_back(); + parseExprConditionnal(); + if (Error.error == 1) { return; + } + current_node = current_node->parent; + // left._nd = NodeBinOperator(left._nd, opt, right._nd); + } + // next(); + current_node = sav_token.back(); + sav_token.pop_back(); + // lasttype=change_type.back(); + // printf("last type:%d\n",lasttype->_vartype); + // change_type.pop_back(); + // current_node = sav_pa; + // printf("exit expr"); + Error.error = 0; + return; + } + void parseExprConditionnal() { +#ifdef PARSER_DEBUG + updateMem(); +#endif + sav_token.push_back(current_node); + parseExpr(); + if (Error.error == 1) { + return; } + while (Match(TokenDoubleEqual) || Match(TokenNotEqual) || + Match(TokenLessOrEqualThan) || Match(TokenLessThan) || + Match(TokenMoreOrEqualThan) || Match(TokenMoreThan)) { + + // token *op = current(); + targetList.push(string_format("label_%d", for_if_num)); + //=target; + for_if_num++; + sav_t.push_back(*current()); + next(); + _node_token_stack.push_back(current_node->children->back()); + + current_node->children->pop_back(); + current_node = current_node->addChild(NodeToken(testNode)); + // current_node->addChild(NodeToken(&sav_t.back(), operatorNode)); + current_node->type = sav_t.back().type; + current_node->setTargetText(targetList.pop()); + // NodeToken nd; + NodeToken nd = NodeToken(changeTypeNode); + // nd._nodetype = changeTypeNode; + nd.type = TokenKeywordVarType; + nd._vartype = findfloat(_node_token_stack.back()); + if (nd._vartype != __float__) { + nd._vartype = finduint32_t(_node_token_stack.back()); + } + current_node = current_node->addChild(nd); + change_type.push_back(current_node); + current_node->addChild(_node_token_stack.back()); + _node_token_stack.pop_back(); + current_node = current_node->parent; + NodeToken nd2 = NodeToken(changeTypeNode); + // nd._nodetype = changeTypeNode; + nd2.type = TokenKeywordVarType; + nd2._vartype = __none__; + current_node = current_node->addChild(nd); + // current_node->type=sav_t.back().type; + change_type.push_back(current_node); + sav_t.pop_back(); + parseExpr(); + if (Error.error == 1) { + return; + } - void parseFactor() - { + current_node = current_node->parent; + current_node = current_node->parent; + change_type.pop_back(); + } + + current_node = sav_token.back(); + sav_token.pop_back(); + + Error.error = 0; + return; + } + void parseExpr() { #ifdef PARSER_DEBUG - updateMem(); + updateMem(); #endif + sav_token.push_back(current_node); - Error.error = 0; + parseTerm(); + if (Error.error == 1) { + return; + } + while (Match(TokenAddition) || Match(TokenSubstraction) || + Match(TokenShiftLeft) || Match(TokenShiftRight)) { + + // token *op = current(); + sav_t.push_back(*current()); + next(); + _node_token_stack.push_back(current_node->children->back()); + + current_node->children->pop_back(); + current_node = current_node->addChild(NodeToken(binOpNode)); + current_node->addChild(_node_token_stack.back()); + _node_token_stack.pop_back(); + // current_node->addChild(NodeToken(&sav_t.back(), operatorNode)); + current_node->type = sav_t.back().type; + sav_t.pop_back(); + parseTerm(); + if (Error.error == 1) { + return; + } + current_node = current_node->parent; + } - if (Match(TokenStar) && Match(TokenIdentifier, 1)) - { - _asPointer = true; + current_node = sav_token.back(); + sav_token.pop_back(); - next(); - // return; - } - if (Match(TokenUppersand) && Match(TokenIdentifier, 1)) - { - isPointer = true; - // printf("qsldkqsld\n"); - next(); - // return; - } - if (current()->getType() == TokenEndOfFile) - { + Error.error = 0; + return; + } - next(); - return; - } + void parseFactor() { +#ifdef PARSER_DEBUG + updateMem(); +#endif - else if (Match(TokenNumber)) - { - - // NodeNumber g = NodeNumber(current()); - current_node->addChild(NodeToken(current(), numberNode)); - if (change_type.size() > 0) - { - if (change_type.back()->_vartype != __float__) - { - if (current()->_vartype == __float__ || current()->_vartype == __uint32_t__) - { - change_type.back()->_vartype = current()->_vartype; - } - } - /* - if (current()->_vartype == __float__) - change_type.back()->_vartype = current()->_vartype; - */ - } - next(); + Error.error = 0; - Error.error = 0; - // result._nd = g; - // printf("exit factor\n"); + if (Match(TokenStar) && Match(TokenIdentifier, 1)) { + _asPointer = true; - return; - } + next(); + // return; + } + if (Match(TokenUppersand) && Match(TokenIdentifier, 1)) { + isPointer = true; + // printf("qsldkqsld\n"); + next(); + // return; + } + if (current()->getType() == TokenEndOfFile) { - else if (Match(TokenNot) || Match(TokenAddition) || Match(TokenSubstraction) || Match(TokenUppersand) || Match(TokenKeywordFabs) || Match(TokenKeywordAbs)) - { - // token *t = current(); - // NodeUnitary g = NodeUnitary(); - current_node = current_node->addChild(NodeToken(unitaryOpNode)); - sav_t.push_back(*current()); + next(); + return; + } - next(); + else if (Match(TokenNumber)) { - parseFactor(); - if (Error.error == 1) - { - return; - } - // NodeUnitary g = NodeUnitary(NodeOperator(t), res._nd); - // current_node->addChild(NodeToken(&sav_t.back(), operatorNode)); - current_node->type = sav_t.back().type; - sav_t.pop_back(); - current_node = current_node->parent; - Error.error = 0; - return; + // NodeNumber g = NodeNumber(current()); + current_node->addChild(NodeToken(current(), numberNode)); + if (change_type.size() > 0) { + if (change_type.back()->_vartype != __float__) { + if (current()->_vartype == __float__ || + current()->_vartype == __uint32_t__) { + change_type.back()->_vartype = current()->_vartype; + } } + /* + if (current()->_vartype == __float__) + change_type.back()->_vartype = current()->_vartype; + */ + } + next(); - else if (Match(TokenOpenParenthesis) && Match(TokenKeywordVarType, 1) && Match(TokenCloseParenthesis, 2) && Match(TokenOpenParenthesis, 3)) - { - // on a (float) ....; - next(); - // NodeChangeType d=NodeChangeType(current()); + Error.error = 0; + // result._nd = g; + // printf("exit factor\n"); - current_node = current_node->addChild(NodeToken(current(), changeTypeNode)); + return; + } - // change_type.push_back(current_node); + else if (Match(TokenNot) || Match(TokenAddition) || + Match(TokenSubstraction) || Match(TokenUppersand) || + Match(TokenKeywordFabs) || Match(TokenKeywordAbs)) { + // token *t = current(); + // NodeUnitary g = NodeUnitary(); + current_node = current_node->addChild(NodeToken(unitaryOpNode)); + sav_t.push_back(*current()); - // if(change_type.size()>0) - // change_type.back()->_vartype=current()->_vartype; - next(); //) - next(); //( - next(); - // NodeToken nd; - NodeToken nd = NodeToken(changeTypeNode); - nd._nodetype = changeTypeNode; - nd.type = TokenKeywordVarType; - nd._vartype = current_node->_vartype; - /* - if (current_node->_vartype == __float__) - nd._vartype = __float__; - else - nd._vartype = __none__; - */ - current_node = current_node->addChild(nd); - change_type.push_back(current_node); - - parseExpr(); - if (Error.error == 1) - { - return; - } - if (Match(TokenCloseParenthesis)) - { - next(); - Error.error = 0; - // current_node = current_node->parent; + next(); - current_node = current_node->parent; - current_node = current_node->parent; + parseFactor(); + if (Error.error == 1) { + return; + } + // NodeUnitary g = NodeUnitary(NodeOperator(t), res._nd); + // current_node->addChild(NodeToken(&sav_t.back(), operatorNode)); + current_node->type = sav_t.back().type; + sav_t.pop_back(); + current_node = current_node->parent; + Error.error = 0; + return; + } - // current_node=current_node->parent; - change_type.pop_back(); - return; - } - else - { - Error.error = 1; - Error.error_message = string_format("expected ) at %s", linepos().c_str()); - return; - } - } - else if (Match(TokenOpenParenthesis)) - { - next(); - // csprintf("one est icic\n\r"); - parseExprAndOr(); - // if(lasttype->_vartype==__float__) - // { - // change_type.back()->_vartype=__float__; - // } - - if (Error.error == 1) - { - return; - } - // NodeToken d=parseExpr(); - if (Match(TokenCloseParenthesis)) - { - next(); - Error.error = 0; - return; - } - else - { - Error.error = 1; - Error.error_message = string_format("expected ) at %s", linepos().c_str()); - return; - } - } + else if (Match(TokenOpenParenthesis) && Match(TokenKeywordVarType, 1) && + Match(TokenCloseParenthesis, 2) && + Match(TokenOpenParenthesis, 3)) { + // on a (float) ....; + next(); + // NodeChangeType d=NodeChangeType(current()); + + current_node = + current_node->addChild(NodeToken(current(), changeTypeNode)); + + // change_type.push_back(current_node); + + // if(change_type.size()>0) + // change_type.back()->_vartype=current()->_vartype; + next(); //) + next(); //( + next(); + // NodeToken nd; + NodeToken nd = NodeToken(changeTypeNode); + nd._nodetype = changeTypeNode; + nd.type = TokenKeywordVarType; + nd._vartype = current_node->_vartype; + /* + if (current_node->_vartype == __float__) + nd._vartype = __float__; + else + nd._vartype = __none__; + */ + current_node = current_node->addChild(nd); + change_type.push_back(current_node); + + parseExpr(); + if (Error.error == 1) { + return; + } + if (Match(TokenCloseParenthesis)) { + next(); + Error.error = 0; + // current_node = current_node->parent; - else if (Match(TokenIdentifier) && !Match(TokenOpenParenthesis, 1)) - { - getVariable(false); - if (Error.error) - { - return; - } - // if(current_node->getChildAtPos(current_node->children_size()-1)->_vartype==__float__) - // change_type.back()->_vartype=__float__; - if (change_type.size() > 0) - { - - if (tmp_sav->children_size() == 0 && !(tmp_sav->asPointer)) - change_type.back()->isPointer = tmp_sav->isPointer; - if (change_type.back()->_vartype != __float__) - { - if (tmp_sav->_vartype == __float__ || tmp_sav->_vartype == __uint32_t__) - { - change_type.back()->_vartype = tmp_sav->_vartype; - } - } + current_node = current_node->parent; + current_node = current_node->parent; - /* - if (tmp_sav->_vartype == __float__) - change_type.back()->_vartype = __float__; - */ - } + // current_node=current_node->parent; + change_type.pop_back(); + return; + } else { + Error.error = 1; + Error.error_message = + string_format("expected ) at %s", linepos().c_str()); + return; + } + } else if (Match(TokenOpenParenthesis)) { + next(); + // csprintf("one est icic\n\r"); + parseExprAndOr(); + // if(lasttype->_vartype==__float__) + // { + // change_type.back()->_vartype=__float__; + // } + + if (Error.error == 1) { + return; + } + // NodeToken d=parseExpr(); + if (Match(TokenCloseParenthesis)) { + next(); + Error.error = 0; + return; + } else { + Error.error = 1; + Error.error_message = + string_format("expected ) at %s", linepos().c_str()); + return; + } + } - if (Error.error == 1) - { - // next(); - return; - } + else if (Match(TokenIdentifier) && !Match(TokenOpenParenthesis, 1)) { + getVariable(false); + if (Error.error) { + return; + } + // if(current_node->getChildAtPos(current_node->children_size()-1)->_vartype==__float__) + // change_type.back()->_vartype=__float__; + if (change_type.size() > 0) { - return; + if (tmp_sav->children_size() == 0 && !(tmp_sav->asPointer)) + change_type.back()->isPointer = tmp_sav->isPointer; + if (change_type.back()->_vartype != __float__) { + if (tmp_sav->_vartype == __float__ || + tmp_sav->_vartype == __uint32_t__) { + change_type.back()->_vartype = tmp_sav->_vartype; + } } - else if (Match(TokenUserDefinedVariable) && Match(TokenOpenParenthesis, 1)) - { - sav_b = isStructFunction; - isStructFunction = true; - NodeToken d = NodeToken(current_node->parent->getChildAtPos(0)); - // NodeToken *par = current_node->parent->getChildAtPos(0); - _node_token_stack.push_back(current_node->parent->getChildAtPos(0)); - // printf("num of cheiold:%d\n",current_node->parent->getChildAtPos(0)->children_size()); - // nd=NodeToken(*current_node->parent->getChildAtPos(0)->getChildAtPos(0)->getChildAtPos(0)); - // d.addChild(NodeToken(current_node->parent->getChildAtPos(0)->getChildAtPos(0))); - - current_node = current_node->parent; - - if (d._nodetype == storeGlobalVariableNode) - d._nodetype = globalVariableNode; - else if (d._nodetype == storeLocalVariableNodeAsRegister) - d._nodetype = localVariableNodeAsRegister; - else - d._nodetype = localVariableNode; - d.type = TokenUserDefinedVariableMemberFunction; - current_node->children->pop_back(); - current_node->children->pop_back(); - nodeTokenList.push(d); - - // current_node->children->clear(); - current()->addText(string_format("%s._@%s", current()->getText(), current()->getText())); - parseFunctionCall(); - if (Error.error) - { - return; - } - current_node->getChildAtPos(current_node->children_size() - 1)->getChildAtPos(2)->getChildAtPos(0)->copyChildren(_node_token_stack.back()); - _node_token_stack.pop_back(); - // if(current_node->getChildAtPos(current_node->children_size()-1)->getChildAtPos(0)->_vartype==__float__) - // change_type.back()->_vartype=__float__; - isStructFunction = sav_b; - return; - } - else if (Match(TokenIdentifier) && Match(TokenOpenParenthesis, 1)) - { - sav_b = isStructFunction; - isStructFunction = false; - parseFunctionCall(); - if (Error.error) - { - return; - } - // if(current_node->getChildAtPos(current_node->children_size()-1)->getChildAtPos(0)->_vartype==__float__) - // change_type.back()->_vartype=__float__; - isStructFunction = sav_b; - return; - } + /* + if (tmp_sav->_vartype == __float__) + change_type.back()->_vartype = __float__; + */ + } - else if (Match(TokenKeywordVarType) && Match(TokenOpenParenthesis, 1)) - { - // on tente CRGB() - // token *typeVar = current(); - sav_t.push_back(*current()); - // NodeNumber num = NodeNumber( current()); - current_node = current_node->addChild(NodeToken(current(), numberNode)); - next(); - if (Match(TokenOpenParenthesis)) - { - for (int i = 0; i < sav_t.back().getVarType()->size; i++) - { - next(); - parseExpr(); - if (Error.error) - { - next(); - return; - } - // num.addChild(res._nd); - if (i == sav_t.back().getVarType()->size - 1) - { - if (!Match(TokenCloseParenthesis)) - { - // resParse res; - Error.error = 1; - Error.error_message = string_format(" ecpected ) %s", linepos().c_str()); - next(); - return; - } - } - else - { - if (!Match(TokenComma)) - { - // resParse res; - Error.error = 1; - Error.error_message = string_format(" ecpected , %s", linepos().c_str()); - next(); - return; - } - } - } - next(); - current_node = current_node->parent; - current_node->_vartype = __CRGB__; - // resParse result; - Error.error = 0; + if (Error.error == 1) { + // next(); + return; + } - // result._nd=num; - sav_t.pop_back(); - return; - } - else - { - // resParse res; - Error.error = 1; - Error.error_message = string_format(" expected ( %s", linepos().c_str()); - next(); - return; - } - } - else if (Match(TokenString)) - { - // printf("in a stirn\n"); - // NodeToken nd; //=NodeToken(); - NodeToken nd = NodeToken(defGlobalVariableNode); - nd._nodetype = defGlobalVariableNode; - nd.type = TokenKeywordVarType; - - nd._vartype = __char__; - nd.isPointer = true; - nd.textref = all_text.addText(string_format("local_string_%d", for_if_num)); - for_if_num++; - - // nd.addChild(NodeToken(current(), stringNode)); - current_cntx->addVariable(nd); - program.addChildFront(nd)->addChild(NodeToken(current(), stringNode)); - // printf("in a stirn end\n"); - // NodeToken *f = program.addChildFront(nd)->addChild(NodeToken(current(), stringNode)); - // f->addChild(NodeToken(current(), stringNode)); - nd._nodetype = globalVariableNode; - // nd.children->clear(); - if (current_node->_nodetype == changeTypeNode) - { - current_node->_vartype = __char__; - current_node->isPointer = true; - } - current_node->addChild(nd); + return; + } + + else if (Match(TokenUserDefinedVariable) && + Match(TokenOpenParenthesis, 1)) { + sav_b = isStructFunction; + isStructFunction = true; + NodeToken d = NodeToken(current_node->parent->getChildAtPos(0)); + // NodeToken *par = current_node->parent->getChildAtPos(0); + _node_token_stack.push_back(current_node->parent->getChildAtPos(0)); + // printf("num of + // cheiold:%d\n",current_node->parent->getChildAtPos(0)->children_size()); + // nd=NodeToken(*current_node->parent->getChildAtPos(0)->getChildAtPos(0)->getChildAtPos(0)); + // d.addChild(NodeToken(current_node->parent->getChildAtPos(0)->getChildAtPos(0))); + + current_node = current_node->parent; + + if (d._nodetype == storeGlobalVariableNode) + d._nodetype = globalVariableNode; + else if (d._nodetype == storeLocalVariableNodeAsRegister) + d._nodetype = localVariableNodeAsRegister; + else + d._nodetype = localVariableNode; + d.type = TokenUserDefinedVariableMemberFunction; + current_node->children->pop_back(); + current_node->children->pop_back(); + nodeTokenList.push(d); + + // current_node->children->clear(); + current()->addText( + string_format("%s._@%s", current()->getText(), current()->getText())); + parseFunctionCall(); + if (Error.error) { + return; + } + current_node->getChildAtPos(current_node->children_size() - 1) + ->getChildAtPos(2) + ->getChildAtPos(0) + ->copyChildren(_node_token_stack.back()); + _node_token_stack.pop_back(); + // if(current_node->getChildAtPos(current_node->children_size()-1)->getChildAtPos(0)->_vartype==__float__) + // change_type.back()->_vartype=__float__; + isStructFunction = sav_b; + return; + } else if (Match(TokenIdentifier) && Match(TokenOpenParenthesis, 1)) { + sav_b = isStructFunction; + isStructFunction = false; + parseFunctionCall(); + if (Error.error) { + return; + } + // if(current_node->getChildAtPos(current_node->children_size()-1)->getChildAtPos(0)->_vartype==__float__) + // change_type.back()->_vartype=__float__; + isStructFunction = sav_b; + return; + } + + else if (Match(TokenKeywordVarType) && Match(TokenOpenParenthesis, 1)) { + // on tente CRGB() + // token *typeVar = current(); + sav_t.push_back(*current()); + // NodeNumber num = NodeNumber( current()); + current_node = current_node->addChild(NodeToken(current(), numberNode)); + next(); + if (Match(TokenOpenParenthesis)) { + for (int i = 0; i < sav_t.back().getVarType()->size; i++) { + next(); + parseExpr(); + if (Error.error) { next(); - // printf("in a stirn end\n"); return; + } + // num.addChild(res._nd); + if (i == sav_t.back().getVarType()->size - 1) { + if (!Match(TokenCloseParenthesis)) { + // resParse res; + Error.error = 1; + Error.error_message = + string_format(" ecpected ) %s", linepos().c_str()); + next(); + return; + } + } else { + if (!Match(TokenComma)) { + // resParse res; + Error.error = 1; + Error.error_message = + string_format(" ecpected , %s", linepos().c_str()); + next(); + return; + } + } } + next(); + current_node = current_node->parent; + current_node->_vartype = __CRGB__; + // resParse result; + Error.error = 0; + + // result._nd=num; + sav_t.pop_back(); + return; + } else { + // resParse res; Error.error = 1; - Error.error_message = string_format(" impossible to find Token %d %s %s", current()->getType(), current()->getText(), linepos().c_str()); + Error.error_message = + string_format(" expected ( %s", linepos().c_str()); next(); return; + } + } else if (Match(TokenString)) { + // printf("in a stirn\n"); + // NodeToken nd; //=NodeToken(); + NodeToken nd = NodeToken(defGlobalVariableNode); + nd._nodetype = defGlobalVariableNode; + nd.type = TokenKeywordVarType; + + nd._vartype = __char__; + nd.isPointer = true; + nd.textref = + all_text.addText(string_format("local_string_%d", for_if_num)); + for_if_num++; + + // nd.addChild(NodeToken(current(), stringNode)); + current_cntx->addVariable(nd); + program.addChildFront(nd)->addChild(NodeToken(current(), stringNode)); + // printf("in a stirn end\n"); + // NodeToken *f = program.addChildFront(nd)->addChild(NodeToken(current(), + // stringNode)); + // f->addChild(NodeToken(current(), stringNode)); + nd._nodetype = globalVariableNode; + // nd.children->clear(); + if (current_node->_nodetype == changeTypeNode) { + current_node->_vartype = __char__; + current_node->isPointer = true; + } + current_node->addChild(nd); + next(); + // printf("in a stirn end\n"); + return; } - void parseType() - { + Error.error = 1; + Error.error_message = string_format( + " impossible to find Token %d %s %s", current()->getType(), + current()->getText(), linepos().c_str()); + next(); + return; + } + void parseType() { #ifdef PARSER_DEBUG - updateMem(); + updateMem(); #endif - // resParse res; - NodeToken _nd = NodeToken(UnknownNode); - if (Match(TokenExternal)) - { - isExternal = true; - next(); - } - else if (Match(TokenKeywordASM)) - { - isASM = true; - next(); - } + // resParse res; + NodeToken _nd = NodeToken(UnknownNode); + if (Match(TokenExternal)) { + isExternal = true; + next(); + } else if (Match(TokenKeywordASM)) { + isASM = true; + next(); + } - if (Match(TokenKeywordVarType) or Match(TokenUserDefinedVariable)) - { + if (Match(TokenKeywordVarType) or Match(TokenUserDefinedVariable)) { - _nd._nodetype = typeNode; - _nd.type = current()->type; - _nd._vartype = current()->_vartype; - _nd.textref = current()->textref; + _nd._nodetype = typeNode; + _nd.type = current()->type; + _nd._vartype = current()->_vartype; + _nd.textref = current()->textref; - // printf("pqoidpoqsidpoqisdopiqsopdiqsd %s\r\n",current()->text.c_str()); - int k = findStruct(current()->getText()); - if (k > -1) - { - _nd._vartype = k; - _nd._total_size = _userDefinedTypes[k]._varSize; - } + // printf("pqoidpoqsidpoqisdopiqsopdiqsd %s\r\n",current()->text.c_str()); + int k = findStruct(current()->getText()); + if (k > -1) { + _nd._vartype = k; + _nd._total_size = _userDefinedTypes[k]._varSize; + } - next(); - if (Match(TokenStar)) - { - _nd.isPointer = true; - next(); - } - else - { - _nd.isPointer = false; - } - } - else if (Match(TokenSemicolon)) - { - next(); - } - else - { - Error.error = 1; - Error.error_message = string_format("expecting external, __ASM__ or variable type %s %d %s", linepos().c_str(), current()->type, current()->getText()); - next(); - return; - } - Error.error = 0; - nodeTokenList.push(_nd); - return; + next(); + if (Match(TokenStar)) { + _nd.isPointer = true; + next(); + } else { + _nd.isPointer = false; + } + } else if (Match(TokenSemicolon)) { + next(); + } else { + Error.error = 1; + Error.error_message = string_format( + "expecting external, __ASM__ or variable type %s %d %s", + linepos().c_str(), current()->type, current()->getText()); + next(); + return; } - void parseVariableForCreation() - { + Error.error = 0; + nodeTokenList.push(_nd); + return; + } + void parseVariableForCreation() { #ifdef PARSER_DEBUG - updateMem(); + updateMem(); #endif - current_cntx->findVariable(current(), true); - if (search_result != NULL) - { + current_cntx->findVariable(current(), true); + if (search_result != NULL) { + // resParse res; + Error.error = 1; + Error.error_message = + string_format("variable %s already declared in the scope for %s", + current()->getText(), linepos().c_str()); + next(); + return; + } + if (Match(TokenOpenBracket, 1)) { + // we are in the case led[]; + string sizestr = ""; + int j = 0; + NodeToken var = NodeToken(current()); + next(); + next(); + var._total_size = 1; + if (Match(TokenNumber)) { + j++; + if (current()->getVarType()->_varType == __int__) { + var._total_size *= stringToInt(current()->getText()); + sizestr = sizestr + " " + string(current()->getText()); + next(); + } else { + // resParse res; + Error.error = 1; + Error.error_message = + string_format("expecting an integer %s", linepos().c_str()); + next(); + return; + } + while (Match(TokenComma)) { + next(); + if (current()->getVarType()->_varType == __int__) { + j++; + var._total_size *= stringToInt(current()->getText()); + sizestr = sizestr + " " + string(current()->getText()); + next(); + } else { // resParse res; Error.error = 1; - Error.error_message = string_format("variable %s already declared in the scope for %s", current()->getText(), linepos().c_str()); + Error.error_message = + string_format("expecting an integer %s", linepos().c_str()); next(); return; - } - if (Match(TokenOpenBracket, 1)) - { - // we are in the case led[]; - string sizestr = ""; - int j = 0; - NodeToken var = NodeToken(current()); - next(); - next(); - var._total_size = 1; - if (Match(TokenNumber)) - { - j++; - if (current()->getVarType()->_varType == __int__) - { - var._total_size *= stringToInt(current()->getText()); - sizestr = sizestr + " " + string(current()->getText()); - next(); - } - else - { - // resParse res; - Error.error = 1; - Error.error_message = string_format("expecting an integer %s", linepos().c_str()); - next(); - return; - } - while (Match(TokenComma)) - { - next(); - if (current()->getVarType()->_varType == __int__) - { - j++; - var._total_size *= stringToInt(current()->getText()); - sizestr = sizestr + " " + string(current()->getText()); - next(); - } - else - { - // resParse res; - Error.error = 1; - Error.error_message = string_format("expecting an integer %s", linepos().c_str()); - next(); - return; - } - } - if (Match(TokenCloseBracket)) - { - var.isPointer = true; - var._nodetype = defGlobalVariableNode; // we can't have arrays in the stack - // var._total_size = stringToInt(num.getText()); - sizestr = string_format("@%d%s", j, sizestr.c_str()); - var.addTargetText(sizestr); - next(); - // resParse result; - Error.error = 0; - nodeTokenList.push(var); - return; - } - else - { - Error.error = 1; - Error.error_message = string_format("expecting ] %s", linepos().c_str()); - next(); - return; - } - } - else if (Match(TokenCloseBracket)) - { - var.isPointer = true; - var._nodetype = defGlobalVariableNode; // we can't have arrays in the stack - // var._total_size = stringToInt(num->text); - next(); + } + } + if (Match(TokenCloseBracket)) { + var.isPointer = true; + var._nodetype = defGlobalVariableNode; // we can't have arrays in the + // stack var._total_size = + // stringToInt(num.getText()); + sizestr = string_format("@%d%s", j, sizestr.c_str()); + var.addTargetText(sizestr); + next(); + // resParse result; + Error.error = 0; + nodeTokenList.push(var); + return; + } else { + Error.error = 1; + Error.error_message = + string_format("expecting ] %s", linepos().c_str()); + next(); + return; + } + } else if (Match(TokenCloseBracket)) { + var.isPointer = true; + var._nodetype = + defGlobalVariableNode; // we can't have arrays in the stack + // var._total_size = stringToInt(num->text); + next(); - Error.error = 0; - nodeTokenList.push(var); - return; - } - else - { + Error.error = 0; + nodeTokenList.push(var); + return; + } else { + Error.error = 1; + Error.error_message = + string_format("expecting an integer %s or ]", linepos().c_str()); + next(); + return; + } + } else { + NodeToken nd = NodeToken(current()); + Error.error = 0; + nodeTokenList.push(nd); + next(); + return; + } + } + void parseProgram() { + int memberpos = 0; + int _start = 0; + int _pos = 0; + struct_name = ""; + // int _totalsize = 0; + + current_cntx = &main_context; + current_node = &program; + Error.error = 0; + while (Match(TokenEndOfFile) == false) { + isStructFunction = false; + updateMem(); + if (Match(TokenUnknown)) { + Error.error = 1; + Error.error_message = + string_format("Unkown Token %s", linepos().c_str()); + return; + } + if (Match(TokenKeywordStruct)) { + + oneFunction = false; + current_cntx = current_cntx->addChild(Context()); + next(); + if (Match(TokenUserDefinedName)) { + + usded._varType = __userDefined__; + memberpos = 0; + _start = 0; + _pos = 0; + // _totalsize = 0; + usded.varName = current()->getText(); + struct_name = usded.varName; + + next(); //{ + + next(); // int + while (!Match(TokenCloseCurlyBracket) and !Match(TokenEndOfFile)) { + + if (Match(TokenUserDefinedVariable) and + Match(TokenOpenParenthesis, 1)) { + isStructFunction = true; + NodeToken _nd = NodeToken(UnknownNode); + _nd._nodetype = typeNode; + _nd.type = TokenKeywordVarType; + _nd._vartype = __void__; + _nd.textref = EOF_TEXT; + + nodeTokenList.push(_nd); + + current()->addText(string_format("%s._@%s", usded.varName.c_str(), + current()->getText())); + parseDefFunction(); + + if (Error.error) { + return; + } + isStructFunction = false; + } else if (Match(TokenKeywordVarType) and + Match(TokenIdentifier, 1) and + Match(TokenOpenParenthesis, 2)) { + isStructFunction = true; + parseType(); + if (Error.error) { + + return; + } + + current()->addText(string_format("%s.%s", usded.varName.c_str(), + current()->getText())); + parseDefFunction(); + + if (Error.error) { + return; + } + isStructFunction = false; + } else if (Match(TokenKeywordVarType) and + Match(TokenIdentifier, 1) and + !Match(TokenOpenParenthesis, 2)) { + usded.starts[memberpos] = _start; + + __v = *current()->getVarType(); + usded.types[memberpos] = __v._varType; + usded.memberSize[memberpos] = __v.size; + _start += __v.total_size; + for (int _var = 0; _var < __v.size; _var++) { + usded.load[_pos] = __v.load[_var]; + usded.store[_pos] = __v.store[_var]; + usded.sizes[_pos] = __v.sizes[_var]; + _pos++; + } + next(); // name + NodeToken nd = NodeToken(current(), defLocalVariableNode); + nd.type = TokenUserDefinedVariableMember; + nd._vartype = __v._varType; + nd.stack_pos = 1000 * (_start - __v.total_size) + _STACK_SIZE; + nd._total_size = __v.size; + nd.asPointer = true; + nd.isPointer = true; + current_cntx->addVariable(nd); + + usded.membersNames[memberpos] = current()->getText(); + // printf(" addinr + // %s.%s\r\n",usded.varName.c_str(),current()->text.c_str()); + next(); // ; + while (Match(TokenComma)) { + // next(); + memberpos++; + usded.starts[memberpos] = _start; + + usded.types[memberpos] = __v._varType; + usded.memberSize[memberpos] = __v.size; + _start += __v.total_size; + for (int _var = 0; _var < __v.size; _var++) { + usded.load[_pos] = __v.load[_var]; + usded.store[_pos] = __v.store[_var]; + usded.sizes[_pos] = __v.sizes[_var]; + _pos++; + } + next(); // name + NodeToken nd = NodeToken(current(), defLocalVariableNode); + nd.type = TokenUserDefinedVariableMember; + nd._vartype = __v._varType; + nd.stack_pos = 1000 * (_start - __v.total_size) + _STACK_SIZE; + nd._total_size = __v.size; + nd.asPointer = true; + nd.isPointer = true; + current_cntx->addVariable(nd); + + usded.membersNames[memberpos] = current()->getText(); + // printf(" addinr + // %s.%s\r\n",usded.varName.c_str(),current()->text.c_str()); + next(); // ; + } + if (!Match(TokenSemicolon)) { Error.error = 1; - Error.error_message = string_format("expecting an integer %s or ]", linepos().c_str()); - next(); - return; - } + Error.error_message = + string_format("missing ; at line %s", linepos().c_str()); + return; + } + next(); + memberpos++; + } else { + Error.error = 1; + Error.error_message = + string_format("Invalid statement line %s", linepos().c_str()); + return; + } + } + usded.size = _pos; + usded.total_size = _start; + usded._varSize = _start; + _userDefinedTypes.push_back(usded); + next(); + } + // if (!oneFunction) + //{ + current_cntx = current_cntx->parent; + struct_name = ""; + isStructFunction = false; + // } + } else if (Match(TokenKeywordSafeMode)) { + safeMode = true; + next(); + } else if (Match(TokenKeywordSaveReg)) { + saveReg = true; + next(); + } else if (Match(TokenKeywordSaveRegAbs)) { + saveRegAbs = true; + next(); + } else if (Match(TokenJson)) { + // printf("looking fo json:\n"); + next(); + jsonStruct j; + j.json = string(current()->getText()); + + next(); + if (!Match(TokenAs)) { + Error.error = 1; + Error.error_message = + string_format("Missing As in Json %s", linepos().c_str()); + return; } - else - { - NodeToken nd = NodeToken(current()); - Error.error = 0; - nodeTokenList.push(nd); - next(); - return; + next(); + + parseType(); + + if (Error.error) { + + return; } - } - void parseProgram() - { - int memberpos = 0; - int _start = 0; - int _pos = 0; - struct_name = ""; - // int _totalsize = 0; - current_cntx = &main_context; - current_node = &program; - Error.error = 0; - while (Match(TokenEndOfFile) == false) - { - isStructFunction = false; - updateMem(); - if (Match(TokenUnknown)) - { - Error.error = 1; - Error.error_message = string_format("Unkown Token %s", linepos().c_str()); - return; + prev(); + j.type = current()->getVarType()->_varType; + // next(); + next(); + j.variable = string(current()->getText()); + prev(); + // prev(); + + // printf("ff %s %s\n",varTypeEnumNames[j.type].c_str(), + // j.variable.c_str()); + + jsonVar.push_back(j); + } else { + parseType(); + if (Error.error) { + + return; + } + if (Match(TokenIdentifier)) { + if (Match(TokenOpenParenthesis, 1)) { + + parseDefFunction(); + + if (Error.error) { + return; } - if (Match(TokenKeywordStruct)) - { + } else { + nodeTokenList.push(nodeTokenList.get()); + parseVariableForCreation(); + if (Error.error) { - oneFunction = false; - current_cntx = current_cntx->addChild(Context()); - next(); - if (Match(TokenUserDefinedName)) - { - - usded._varType = __userDefined__; - memberpos = 0; - _start = 0; - _pos = 0; - // _totalsize = 0; - usded.varName = current()->getText(); - struct_name = usded.varName; - - next(); //{ - - next(); // int - while (!Match(TokenCloseCurlyBracket) and !Match(TokenEndOfFile)) - { - - if (Match(TokenUserDefinedVariable) and Match(TokenOpenParenthesis, 1)) - { - isStructFunction = true; - NodeToken _nd = NodeToken(UnknownNode); - _nd._nodetype = typeNode; - _nd.type = TokenKeywordVarType; - _nd._vartype = __void__; - _nd.textref = EOF_TEXT; - - nodeTokenList.push(_nd); - - current()->addText(string_format("%s._@%s", usded.varName.c_str(), current()->getText())); - parseDefFunction(); - - if (Error.error) - { - return; - } - isStructFunction = false; - } - else if (Match(TokenKeywordVarType) and Match(TokenIdentifier, 1) and Match(TokenOpenParenthesis, 2)) - { - isStructFunction = true; - parseType(); - if (Error.error) - { - - return; - } - - current()->addText(string_format("%s.%s", usded.varName.c_str(), current()->getText())); - parseDefFunction(); - - if (Error.error) - { - return; - } - isStructFunction = false; - } - else if (Match(TokenKeywordVarType) and Match(TokenIdentifier, 1) and !Match(TokenOpenParenthesis, 2)) - { - usded.starts[memberpos] = _start; - - __v = *current()->getVarType(); - usded.types[memberpos] = __v._varType; - usded.memberSize[memberpos] = __v.size; - _start += __v.total_size; - for (int _var = 0; _var < __v.size; _var++) - { - usded.load[_pos] = __v.load[_var]; - usded.store[_pos] = __v.store[_var]; - usded.sizes[_pos] = __v.sizes[_var]; - _pos++; - } - next(); // name - NodeToken nd = NodeToken(current(), defLocalVariableNode); - nd.type = TokenUserDefinedVariableMember; - nd._vartype = __v._varType; - nd.stack_pos = 1000 * (_start - __v.total_size) + _STACK_SIZE; - nd._total_size = __v.size; - nd.asPointer = true; - nd.isPointer = true; - current_cntx->addVariable(nd); - - usded.membersNames[memberpos] = current()->getText(); - // printf(" addinr %s.%s\r\n",usded.varName.c_str(),current()->text.c_str()); - next(); // ; - while (Match(TokenComma)) - { - // next(); - memberpos++; - usded.starts[memberpos] = _start; - - usded.types[memberpos] = __v._varType; - usded.memberSize[memberpos] = __v.size; - _start += __v.total_size; - for (int _var = 0; _var < __v.size; _var++) - { - usded.load[_pos] = __v.load[_var]; - usded.store[_pos] = __v.store[_var]; - usded.sizes[_pos] = __v.sizes[_var]; - _pos++; - } - next(); // name - NodeToken nd = NodeToken(current(), defLocalVariableNode); - nd.type = TokenUserDefinedVariableMember; - nd._vartype = __v._varType; - nd.stack_pos = 1000 * (_start - __v.total_size) + _STACK_SIZE; - nd._total_size = __v.size; - nd.asPointer = true; - nd.isPointer = true; - current_cntx->addVariable(nd); - - usded.membersNames[memberpos] = current()->getText(); - // printf(" addinr %s.%s\r\n",usded.varName.c_str(),current()->text.c_str()); - next(); // ; - } - if (!Match(TokenSemicolon)) - { - Error.error = 1; - Error.error_message = string_format("missing ; at line %s", linepos().c_str()); - return; - } - next(); - memberpos++; - } - else - { - Error.error = 1; - Error.error_message = string_format("Invalid statement line %s", linepos().c_str()); - return; - } - } - usded.size = _pos; - usded.total_size = _start; - usded._varSize = _start; - _userDefinedTypes.push_back(usded); - next(); - } - // if (!oneFunction) - //{ - current_cntx = current_cntx->parent; - struct_name = ""; - isStructFunction = false; - // } + return; } - else if (Match(TokenKeywordSafeMode)) - { - safeMode = true; - next(); + NodeToken nd = nodeTokenList.pop(); + NodeToken _t = nodeTokenList.pop(); + if (isExternal) { + nd._nodetype = (int)defExtGlobalVariableNode; + + isExternal = false; + } else { + nd._nodetype = (int)defGlobalVariableNode; } - else if (Match(TokenKeywordSaveReg)) - { - saveReg = true; + copyPrty(&_t, &nd); + + current_node = program.addChild(nd); + tmp_sav = current_node; + current_cntx->addVariable(nd); + if (Match(TokenComma)) { + while (Match(TokenComma)) { next(); - } - else if (Match(TokenKeywordSaveRegAbs)) - { - saveRegAbs = true; + nodeTokenList.push(nodeTokenList.get()); + parseVariableForCreation(); + if (Error.error) { + + return; + } + NodeToken nd = nodeTokenList.pop(); + NodeToken _t = nodeTokenList.pop(); + if (isExternal) { + nd._nodetype = (int)defExtGlobalVariableNode; + + isExternal = false; + } else { + nd._nodetype = (int)defGlobalVariableNode; + } + copyPrty(&_t, &nd); + + current_node = program.addChild(nd); + tmp_sav = current_node; + current_cntx->addVariable(nd); + } + if (!Match(TokenSemicolon)) { + Error.error = 1; + Error.error_message = + string_format("Expected ; %s", linepos().c_str()); next(); - } - else if (Match(TokenJson)) - { - // printf("looking fo json:\n"); + return; + } else { + next(); + nodeTokenList.pop(); + current_node = current_node->parent; + // return; + } + } else if (Match(TokenSemicolon)) { + nodeTokenList.pop(); + if (current_node->type == TokenUserDefinedVariable) { + current()->addText(string_format( + "%s._@%s()", current_node->getVarType()->varName.c_str(), + current_node->getVarType()->varName.c_str())); + main_context.findFunction(current()); + + if (search_result != NULL) { + NodeToken d = NodeToken(*current_node); + d._nodetype = callConstructorNode; + current_node = current_node->parent; + current_node = current_node->addChild(d); + } + } + + current_node = current_node->parent; + next(); + } else if (Match(TokenEqual) && Match(TokenString, 1)) { + nodeTokenList.pop(); + next(); + current_node->addChild(NodeToken(current(), stringNode)); + next(); + if (!Match(TokenSemicolon)) { + + Error.error = 1; + Error.error_message = + string_format("Expected ; %s", linepos().c_str()); next(); - jsonStruct j; - j.json = string(current()->getText()); + return; + } + next(); + Error.error = 0; + current_node = current_node->parent; + } else if (Match(TokenEqual) && Match(TokenOpenCurlyBracket, 1)) { + nodeTokenList.pop(); + next(); + next(); + parseFactor(); + if (Error.error) { + return; + } + while (Match(TokenComma)) { next(); - if (!Match(TokenAs)) - { - Error.error = 1; - Error.error_message = string_format("Missing As in Json %s", linepos().c_str()); - return; + parseFactor(); + if (Error.error) { + return; } + } + if (!Match(TokenCloseCurlyBracket)) { + + Error.error = 1; + Error.error_message = + string_format("Expected } %s", linepos().c_str()); next(); + return; + } + next(); + if (!Match(TokenSemicolon)) { - parseType(); + Error.error = 1; + Error.error_message = + string_format("Expected ; %s", linepos().c_str()); + next(); + return; + } + next(); + Error.error = 0; + // __sav_pos = _tks->position; - if (Error.error) - { + // _tks->position = __sav_pos; + current_node = current_node->parent; + } - return; - } + else if (Match(TokenEqual) and + Match(TokenUserDefinedVariable, 1) and + Match(TokenOpenParenthesis, 2) and + current_node->type == TokenUserDefinedVariable) { - prev(); - j.type = current()->getVarType()->_varType; - // next(); - next(); - j.variable = string(current()->getText()); - prev(); - // prev(); + nodeTokenList.pop(); + next(); + current()->addText(string_format( + "%s._@%s", current_node->getVarType()->varName.c_str(), + current()->getText())); + NodeToken nd = NodeToken(*current_node); + if (current_node->_nodetype == defGlobalVariableNode) + nd._nodetype = globalVariableNode; + else + nd._nodetype = localVariableNode; // globalVariableNode; + nd.type = TokenUserDefinedVariableMemberFunction; + nd.isPointer = true; + nd._total_size = current_node->getVarType()->total_size; - // printf("ff %s %s\n",varTypeEnumNames[j.type].c_str(), j.variable.c_str()); + nodeTokenList.push(nd); + isStructFunction = true; + current_node = current_node->parent; + parseFunctionCall(); - jsonVar.push_back(j); - } - else - { - parseType(); - if (Error.error) - { + if (Error.error) { + return; + } + if (!Match(TokenSemicolon)) { + Error.error = 1; + Error.error_message = + string_format("Missing ; %s", linepos().c_str()); + return; + } + next(); + // current_node->getChildAtPos(current_node->children_size() + // - + // 1)->getChildAtPos(2)->getChildAtPos(0)->copyChildren(par); + isStructFunction = false; + Error.error = 0; + // current_node = current_node->parent; + // return; + } else if (Match(TokenEqual)) { + nodeTokenList.pop(); + next(); + NodeToken nd = NodeToken(*current_node); + nd._nodetype = storeGlobalVariableNode; + current_node = current_node->parent; + current_node = current_node->addChild(NodeToken(assignementNode)); + current_node->addChild(nd); + + parseExpr(); + if (Error.error) { + return; + } + if (!Match(TokenSemicolon)) { + Error.error = 1; + Error.error_message = + string_format("Missing ; %s", linepos().c_str()); + return; + } - return; - } - if (Match(TokenIdentifier)) - { - if (Match(TokenOpenParenthesis, 1)) - { - - parseDefFunction(); - - if (Error.error) - { - return; - } - } - else - { - nodeTokenList.push(nodeTokenList.get()); - parseVariableForCreation(); - if (Error.error) - { - - return; - } - NodeToken nd = nodeTokenList.pop(); - NodeToken _t = nodeTokenList.pop(); - if (isExternal) - { - nd._nodetype = (int)defExtGlobalVariableNode; - - isExternal = false; - } - else - { - nd._nodetype = (int)defGlobalVariableNode; - } - copyPrty(&_t, &nd); - - current_node = program.addChild(nd); - tmp_sav = current_node; - current_cntx->addVariable(nd); - if (Match(TokenComma)) - { - while (Match(TokenComma)) - { - next(); - nodeTokenList.push(nodeTokenList.get()); - parseVariableForCreation(); - if (Error.error) - { - - return; - } - NodeToken nd = nodeTokenList.pop(); - NodeToken _t = nodeTokenList.pop(); - if (isExternal) - { - nd._nodetype = (int)defExtGlobalVariableNode; - - isExternal = false; - } - else - { - nd._nodetype = (int)defGlobalVariableNode; - } - copyPrty(&_t, &nd); - - current_node = program.addChild(nd); - tmp_sav = current_node; - current_cntx->addVariable(nd); - } - if (!Match(TokenSemicolon)) - { - Error.error = 1; - Error.error_message = string_format("Expected ; %s", linepos().c_str()); - next(); - return; - } - else - { - next(); - nodeTokenList.pop(); - current_node = current_node->parent; - // return; - } - } - else if (Match(TokenSemicolon)) - { - nodeTokenList.pop(); - if (current_node->type == TokenUserDefinedVariable) - { - current()->addText(string_format("%s._@%s()", current_node->getVarType()->varName.c_str(), current_node->getVarType()->varName.c_str())); - main_context.findFunction(current()); - - if (search_result != NULL) - { - NodeToken d = NodeToken(*current_node); - d._nodetype = callConstructorNode; - current_node = current_node->parent; - current_node = current_node->addChild(d); - } - } - - current_node = current_node->parent; - next(); - } - else if (Match(TokenEqual) && Match(TokenString, 1)) - { - nodeTokenList.pop(); - next(); - current_node->addChild(NodeToken(current(), stringNode)); - next(); - if (!Match(TokenSemicolon)) - { - - Error.error = 1; - Error.error_message = string_format("Expected ; %s", linepos().c_str()); - next(); - return; - } - next(); - Error.error = 0; - - current_node = current_node->parent; - } - else if (Match(TokenEqual) && Match(TokenOpenCurlyBracket, 1)) - { - nodeTokenList.pop(); - next(); - next(); - parseFactor(); - if (Error.error) - { - return; - } - while (Match(TokenComma)) - { - next(); - parseFactor(); - if (Error.error) - { - return; - } - } - if (!Match(TokenCloseCurlyBracket)) - { - - Error.error = 1; - Error.error_message = string_format("Expected } %s", linepos().c_str()); - next(); - return; - } - next(); - if (!Match(TokenSemicolon)) - { - - Error.error = 1; - Error.error_message = string_format("Expected ; %s", linepos().c_str()); - next(); - return; - } - next(); - Error.error = 0; - // __sav_pos = _tks->position; - - // _tks->position = __sav_pos; - current_node = current_node->parent; - } - - else if (Match(TokenEqual) and Match(TokenUserDefinedVariable, 1) and Match(TokenOpenParenthesis, 2) and current_node->type == TokenUserDefinedVariable) - { - - nodeTokenList.pop(); - next(); - current()->addText(string_format("%s._@%s", current_node->getVarType()->varName.c_str(), current()->getText())); - NodeToken nd = NodeToken(*current_node); - if (current_node->_nodetype == defGlobalVariableNode) - nd._nodetype = globalVariableNode; - else - nd._nodetype = localVariableNode; // globalVariableNode; - nd.type = TokenUserDefinedVariableMemberFunction; - nd.isPointer = true; - nd._total_size = current_node->getVarType()->total_size; - - nodeTokenList.push(nd); - isStructFunction = true; - current_node = current_node->parent; - parseFunctionCall(); - - if (Error.error) - { - return; - } - if (!Match(TokenSemicolon)) - { - Error.error = 1; - Error.error_message = string_format("Missing ; %s", linepos().c_str()); - return; - } - next(); - // current_node->getChildAtPos(current_node->children_size() - 1)->getChildAtPos(2)->getChildAtPos(0)->copyChildren(par); - isStructFunction = false; - Error.error = 0; - // current_node = current_node->parent; - // return; - } - else if (Match(TokenEqual)) - { - nodeTokenList.pop(); - next(); - NodeToken nd = NodeToken(*current_node); - nd._nodetype = storeGlobalVariableNode; - current_node = current_node->parent; - current_node = current_node->addChild(NodeToken(assignementNode)); - current_node->addChild(nd); - - parseExpr(); - if (Error.error) - { - return; - } - if (!Match(TokenSemicolon)) - { - Error.error = 1; - Error.error_message = string_format("Missing ; %s", linepos().c_str()); - return; - } - - next(); - current_node = current_node->parent; - // current_node=current_node->parent; - } - } - } - else - { - Error.error = 1; - Error.error_message = string_format("expected identifier %s", linepos().c_str()); - next(); - return; - } + next(); + current_node = current_node->parent; + // current_node=current_node->parent; } + } + } else { + Error.error = 1; + Error.error_message = + string_format("expected identifier %s", linepos().c_str()); + next(); + return; } - Error.error = 0; - - return; + } } + Error.error = 0; + + return; + } }; #ifdef __CONSOLE_ESP32 Parser p = Parser(); -void kill(Console *cons, vector args) -{ - if (args.size() == 0) - { - if (SCExecutable.isRunning()) - { +void kill(Console *cons, vector args) { + if (args.size() == 0) { + if (SCExecutable.isRunning()) { - SCExecutable.kill(); - } - else - { - pushToConsole("Nothing is currently running.", true); - } - } - else - { - scriptRuntime.kill(args[0]); + SCExecutable.kill(); + } else { + pushToConsole("Nothing is currently running.", true); } + } else { + scriptRuntime.kill(args[0]); + } } -Arguments parseInputArgs(string variables) -{ - Arguments _args; - vector ar = split(variables, ","); - for (int i = 0; i < ar.size(); i++) - { - // printf("var %d :%s\n", i, ar[i].c_str()); - if (ar[i].find(".") != -1) - { - float j = 0; - sscanf(ar[i].c_str(), "%f", &j); - _args.add(j); - } - else - { - int j = 0; - sscanf(ar[i].c_str(), "%d", &j); - _args.add(j); - } +Arguments parseInputArgs(string variables) { + Arguments _args; + vector ar = split(variables, ","); + for (int i = 0; i < ar.size(); i++) { + // printf("var %d :%s\n", i, ar[i].c_str()); + if (ar[i].find(".") != -1) { + float j = 0; + sscanf(ar[i].c_str(), "%f", &j); + _args.add(j); + } else { + int j = 0; + sscanf(ar[i].c_str(), "%d", &j); + _args.add(j); } - return _args; + } + return _args; } -void run(Console *cons, vector args) -{ - Arguments _args; - string progToRun = ""; - if (args.size() > 0) - { - if (args[0].find("(") != -1) - { - string variables = args[0].substr(1, args[0].size() - 2); - _args = parseInputArgs(variables); - } - else - { - progToRun = args[0]; - } - if (args.size() > 1) - { - string variables = args[1].substr(1, args[1].size() - 2); - _args = parseInputArgs(variables); - } - // printf("%s\n\r",args[0].c_str()); - } - if (progToRun.size() == 0) - { - if (SCExecutable.isRunning()) - { - LedOS.pushToConsole("Something Already running kill it first ..."); - kill(cons, args); - } - SCExecutable.executeAsTask("main", _args); - } - else - { - scriptRuntime.executeAsTask(progToRun, _args); +void run(Console *cons, vector args) { + Arguments _args; + string progToRun = ""; + if (args.size() > 0) { + if (args[0].find("(") != -1) { + string variables = args[0].substr(1, args[0].size() - 2); + _args = parseInputArgs(variables); + } else { + progToRun = args[0]; } -} -void kill_cEsc(Console *cons) -{ - LedOS.displayf = false; - vector f; - if (cons->cmode == edit) - { - LedOS.storeCurrentLine(); + if (args.size() > 1) { + string variables = args[1].substr(1, args[1].size() - 2); + _args = parseInputArgs(variables); } - kill(cons, f); - if (cons->cmode == keyword) - { - _push(config.ENDLINE); - _push(cons->prompt(cons).c_str()); + // printf("%s\n\r",args[0].c_str()); + } + if (progToRun.size() == 0) { + if (SCExecutable.isRunning()) { + LedOS.pushToConsole("Something Already running kill it first ..."); + kill(cons, args); } + SCExecutable.executeAsTask("main", _args); + } else { + scriptRuntime.executeAsTask(progToRun, _args); + } } -void parseasm(Console *cons, vector args) -{ -} -void compile_c(Console *cons, vector args) -{ - pushToConsole("Compiling ...", true); - Executable _scExec = p.parse_c(&cons->script); - _scExec.name = cons->filename; - scriptRuntime.addExe(_scExec); - if (_scExec.exeExist) - { - - pushToConsole(string_format("Compiling done. Handle number:%d", scriptRuntime._scExecutables.size()), true); - } - else - { - pushToConsole(_scExec.error.error_message); - } +void kill_cEsc(Console *cons) { + LedOS.displayf = false; + vector f; + if (cons->cmode == edit) { + LedOS.storeCurrentLine(); + } + kill(cons, f); + if (cons->cmode == keyword) { + _push(config.ENDLINE); + _push(cons->prompt(cons).c_str()); + } } -void free(Console *cons, vector args) -{ - if (args.size() == 0) - { - if (SCExecutable.isRunning()) - { - SCExecutable.kill(); - } - SCExecutable.free(); - } - else - { - scriptRuntime.deleteExe(args[0]); - } +void parseasm(Console *cons, vector args) {} +void compile_c(Console *cons, vector args) { + pushToConsole("Compiling ...", true); + Executable _scExec = p.parse_c(&cons->script); + _scExec.name = cons->filename; + scriptRuntime.addExe(_scExec); + if (_scExec.exeExist) { + + pushToConsole(string_format("Compiling done. Handle number:%d", + scriptRuntime._scExecutables.size()), + true); + } else { + pushToConsole(_scExec.error.error_message); + } } -void saveBin(Console *cons, vector args) -{ - Binary bin; - if (args.size() > 0) - { - LedOS.pushToConsole("Compiling ...", true); - __parser_debug = false; - bin = p.parse_cBinary(&cons->script); - saveBinary((char *)(fileSystem.current_path + args[0]).c_str(), *fileSystem.current_mount->fs, &bin); - freeBinary(&bin); +void free(Console *cons, vector args) { + if (args.size() == 0) { + if (SCExecutable.isRunning()) { + SCExecutable.kill(); } - else - LedOS.pushToConsole("filename missing ...", true); + SCExecutable.free(); + } else { + scriptRuntime.deleteExe(args[0]); + } } -void displaybin(Console *cons, vector args) -{ - Binary bin; - if (args.size() > 0) - { - loadBinary((char *)(fileSystem.current_path + args[0]).c_str(), *fileSystem.current_mount->fs, &bin); - displayBinary(&bin); - freeBinary(&bin); - } +void saveBin(Console *cons, vector args) { + Binary bin; + if (args.size() > 0) { + LedOS.pushToConsole("Compiling ...", true); + __parser_debug = false; + bin = p.parse_cBinary(&cons->script); + saveBinary((char *)(fileSystem.current_path + args[0]).c_str(), + *fileSystem.current_mount->fs, &bin); + freeBinary(&bin); + } else + LedOS.pushToConsole("filename missing ...", true); } -void binload(Console *cons, vector args) -{ - Binary bin; - // printf("we are here\n"); - Arguments _args; - if (args.size() > 0) - { - if (SCExecutable.isRunning()) - { - LedOS.pushToConsole("Something Already running kill it first ..."); - vector k; - kill(cons, k); - } - bool othercore = false; - - SCExecutable.free(); - // bin = p.parse_cBinary(&cons->script); - loadBinary((char *)(fileSystem.current_path + args[0]).c_str(), *fileSystem.current_mount->fs, &bin); - if (bin.error.error) - { - // Serial.printf("oioioioe ZZZ"); - pushToConsole(bin.error.error_message.c_str(), true); - return; - } - if (args.size() > 1) - { - string variables = args[1].substr(1, args[1].size() - 2); - _args = parseInputArgs(variables); - } - executable _executecmd = createExectutable(&bin); - SCExecutable.setExecutable(_executecmd); - SCExecutable.error = _executecmd.error; - if (_executecmd.error.error == 1) - { - // exeExist = false; - Serial.printf("oioioioe"); - - pushToConsole(_executecmd.error.error_message.c_str(), true); - } - else - { - // if (SCExecutable.exeExist) - //{ - - exeExist = true; - if (true) - { - // vector d; - // d.push_back("main"); - LedOS.pushToConsole("***********START RUN *********"); - SCExecutable.execute("main", _args); - // run(cons, d); - - if (cons->cmode == keyword) - { - _push(config.ENDLINE); - _push(cons->prompt(cons).c_str()); - } - } - else - { - LedOS.pushToConsole("Start program", true); - SCExecutable.execute("main"); - // executeBinary("main", executecmd); - LedOS.pushToConsole("Execution done.", true); - } - // } - } - } - else - - LedOS.pushToConsole("filename missing ...", true); +void displaybin(Console *cons, vector args) { + Binary bin; + if (args.size() > 0) { + loadBinary((char *)(fileSystem.current_path + args[0]).c_str(), + *fileSystem.current_mount->fs, &bin); + displayBinary(&bin); + freeBinary(&bin); + } } - -void parse_c(Console *cons, vector args) -{ - if (SCExecutable.isRunning()) - { - LedOS.pushToConsole("Something Already running kill it first ..."); - vector k; - kill(cons, k); +void binload(Console *cons, vector args) { + Binary bin; + // printf("we are here\n"); + Arguments _args; + if (args.size() > 0) { + if (SCExecutable.isRunning()) { + LedOS.pushToConsole("Something Already running kill it first ..."); + vector k; + kill(cons, k); } bool othercore = false; SCExecutable.free(); - LedOS.pushToConsole("Compiling ...", true); - if (args.size() > 0) - { - if (args[0].compare("&") != 0) - __parser_debug = true; - if (args[args.size() - 1].compare("&") == 0) - othercore = true; + // bin = p.parse_cBinary(&cons->script); + loadBinary((char *)(fileSystem.current_path + args[0]).c_str(), + *fileSystem.current_mount->fs, &bin); + if (bin.error.error) { + // Serial.printf("oioioioe ZZZ"); + pushToConsole(bin.error.error_message.c_str(), true); + return; } - SCExecutable = p.parse_c(&cons->script); - // if (p.parse_c(&cons->script)) - - if (SCExecutable.exeExist) - { - - exeExist = true; - if (othercore) - { - vector d; - // d.push_back("main"); - LedOS.pushToConsole("***********START RUN *********"); - run(cons, d); - - if (cons->cmode == keyword) - { - _push(config.ENDLINE); - _push(cons->prompt(cons).c_str()); - } - } - else - { - LedOS.pushToConsole("Start program", true); - SCExecutable.execute("main"); - // executeBinary("main", executecmd); - LedOS.pushToConsole("Execution done.", true); - } + if (args.size() > 1) { + string variables = args[1].substr(1, args[1].size() - 2); + _args = parseInputArgs(variables); } - else - { - LedOS.pushToConsole(SCExecutable.error.error_message, true); + executable _executecmd = createExectutable(&bin); + SCExecutable.setExecutable(_executecmd); + SCExecutable.error = _executecmd.error; + if (_executecmd.error.error == 1) { + // exeExist = false; + Serial.printf("oioioioe"); + + pushToConsole(_executecmd.error.error_message.c_str(), true); + } else { + // if (SCExecutable.exeExist) + //{ + + exeExist = true; + if (true) { + // vector d; + // d.push_back("main"); + LedOS.pushToConsole("***********START RUN *********"); + SCExecutable.execute("main", _args); + // run(cons, d); + + if (cons->cmode == keyword) { + _push(config.ENDLINE); + _push(cons->prompt(cons).c_str()); + } + } else { + LedOS.pushToConsole("Start program", true); + SCExecutable.execute("main"); + // executeBinary("main", executecmd); + LedOS.pushToConsole("Execution done.", true); + } + // } } - __parser_debug = false; + } else + + LedOS.pushToConsole("filename missing ...", true); } -void parsec_cEsc(Console *cons) -{ - LedOS.displayf = false; - vector f; - f.push_back("&"); - if (cons->cmode == edit) - { - LedOS.storeCurrentLine(); - } - parse_c(cons, f); - if (cons->cmode == keyword) - { +void parse_c(Console *cons, vector args) { + if (SCExecutable.isRunning()) { + LedOS.pushToConsole("Something Already running kill it first ..."); + vector k; + kill(cons, k); + } + bool othercore = false; + + SCExecutable.free(); + LedOS.pushToConsole("Compiling ...", true); + if (args.size() > 0) { + if (args[0].compare("&") != 0) + __parser_debug = true; + if (args[args.size() - 1].compare("&") == 0) + othercore = true; + } + SCExecutable = p.parse_c(&cons->script); + // if (p.parse_c(&cons->script)) + + if (SCExecutable.exeExist) { + + exeExist = true; + if (othercore) { + vector d; + // d.push_back("main"); + LedOS.pushToConsole("***********START RUN *********"); + run(cons, d); + + if (cons->cmode == keyword) { _push(config.ENDLINE); _push(cons->prompt(cons).c_str()); + } + } else { + LedOS.pushToConsole("Start program", true); + SCExecutable.execute("main"); + // executeBinary("main", executecmd); + LedOS.pushToConsole("Execution done.", true); } + } else { + LedOS.pushToConsole(SCExecutable.error.error_message, true); + } + __parser_debug = false; } -void listExec(Console *cons, vector args) -{ - scriptRuntime.listExec(); +void parsec_cEsc(Console *cons) { + LedOS.displayf = false; + vector f; + f.push_back("&"); + if (cons->cmode == edit) { + LedOS.storeCurrentLine(); + } + parse_c(cons, f); + if (cons->cmode == keyword) { + _push(config.ENDLINE); + _push(cons->prompt(cons).c_str()); + } } -class __INIT_PARSER -{ +void listExec(Console *cons, vector args) { scriptRuntime.listExec(); } + +class __INIT_PARSER { public: - __INIT_PARSER() - { - // __run_handle = NULL; - LedOS.addKeywordCommand("compile", parse_c, "Compile and run a program add '&' for run on the second core"); - LedOS.addKeywordCommand("comp", compile_c, "Compile a program"); - LedOS.addKeywordCommand("createbin", saveBin, "Compile a program into binary"); - LedOS.addKeywordCommand("executebin", binload, "executebin"); - LedOS.addKeywordCommand("displaybin", displaybin, "displaybin"); - LedOS.addKeywordCommand("./", binload, "executebin"); - LedOS.addKeywordCommand("list", listExec, "list the compiled programs"); - LedOS.addKeywordCommand("free", free, "free the binary free x will free the program with handle x"); - LedOS.addKeywordCommand("run", run, "Run an already compiled program (always second Core) run x run program with handle x"); - LedOS.addKeywordCommand("kill", kill, "Stop a running program kill x kill program with handle x"); - LedOS.addKeywordCommand("parseasm", parseasm, "Parse assembly program"); - LedOS.addEscCommand(18, parsec_cEsc, "Compile and execute a program (always second Core)"); - LedOS.addEscCommand(11, kill_cEsc, "Stop a running program"); - // addExternal("__feed", externalType::function, (void *)feedTheDog); - } + __INIT_PARSER() { + // __run_handle = NULL; + LedOS.addKeywordCommand( + "compile", parse_c, + "Compile and run a program add '&' for run on the second core"); + LedOS.addKeywordCommand("comp", compile_c, "Compile a program"); + LedOS.addKeywordCommand("createbin", saveBin, + "Compile a program into binary"); + LedOS.addKeywordCommand("executebin", binload, "executebin"); + LedOS.addKeywordCommand("displaybin", displaybin, "displaybin"); + LedOS.addKeywordCommand("./", binload, "executebin"); + LedOS.addKeywordCommand("list", listExec, "list the compiled programs"); + LedOS.addKeywordCommand( + "free", free, + "free the binary free x will free the program with handle x"); + LedOS.addKeywordCommand("run", run, + "Run an already compiled program (always second " + "Core) run x run program with handle x"); + LedOS.addKeywordCommand( + "kill", kill, + "Stop a running program kill x kill program with handle x"); + LedOS.addKeywordCommand("parseasm", parseasm, "Parse assembly program"); + LedOS.addEscCommand(18, parsec_cEsc, + "Compile and execute a program (always second Core)"); + LedOS.addEscCommand(11, kill_cEsc, "Stop a running program"); + // addExternal("__feed", externalType::function, (void *)feedTheDog); + } }; __INIT_PARSER _init_parser; @@ -3921,175 +3563,141 @@ const char *_colors[_NB_COLORS] = { }; -void formatInit() -{ - _parenthesis.clear(); - _curlybracket.clear(); - _bracket.clear(); - _prevparenthesis = 0; - _prevcurlybracket = 0; - _prevbracket = 0; +void formatInit() { + _parenthesis.clear(); + _curlybracket.clear(); + _bracket.clear(); + _prevparenthesis = 0; + _prevcurlybracket = 0; + _prevbracket = 0; } -void formatLine() -{ - _prevparenthesis = 0; - _prevcurlybracket = 0; - _prevbracket = 0; +void formatLine() { + _prevparenthesis = 0; + _prevcurlybracket = 0; + _prevbracket = 0; } -string formatLine(string str) -{ - // Serial.printf("streing:%s\r\n",str.c_str()); - if (str.size() < 2) - return str; - string sd; - sd = ""; - sd = sd + str; - // _parent.clear(); - _for_display = true; - // Script s(&str); - main_script.clear(); - _tks = &__tks; - all_text.clear(); - main_script.addContent((char *)sd.c_str()); - main_script.init(); - // Serial.printf("streing:%s\r\n",str.c_str()); - _tks->tokenize(&main_script, true, true, 1); - // _tks->init(); - // Serial.printf("streing:%s\r\n",str.c_str()); - string res = ""; - - _parenthesis.clear(); - _bracket.clear(); - _parenthesis.clear(); - _prevparenthesis = 0; - _prevcurlybracket = 0; - _prevbracket = 0; - - while (_tks->current()->getType() != TokenEndOfFile) // for (int i = 0; i < _tks->size(); i++) - { - Token tk = *_tks->current(); - // Serial.printf("token %s\r\n",tk.getText()); - /* if (tk.type == TokenOpenCurlyBracket) - { - - // char *color= (char *)_colors[_curlybracket.size()%_NB_COLORS]; - res = res + string_format("%s%s", _colors[_curlybracket.size() % _NB_COLORS], tk.text.c_str()); - _curlybracket.push_back(_colors[_curlybracket.size() % _NB_COLORS]); - _prevcurlybracket++; - } - else if (tk.type == TokenCloseCurlyBracket) - { - - if (_curlybracket.size() == 0) - { - res = res + string_format("%s%s", "\u001b[38;5;196m", tk.text.c_str()); - } - else - { - // char * color=_curlybracket.back(); - _prevcurlybracket--; - res = res + string_format("%s%s", _curlybracket.back(), tk.text.c_str()); - _curlybracket.pop_back(); - } - } */ - if (tk.getType() == TokenOpenParenthesis) - { - _prevparenthesis++; - res = - res + string_format("%s%s", - _colors[(_parenthesis.size() + - 2) % - _NB_COLORS], - tk.getText()); - _parenthesis.push_back(_colors - [(_parenthesis.size() + 2) % _NB_COLORS]); - } - else if (tk.getType() == TokenCloseParenthesis) - { - - if (_parenthesis.size() == 0) - { - res = - res + string_format("%s%s", "\u001b[38;5;196m", - tk.getText()); - } - else - { - _prevparenthesis--; - res = - res + string_format("%s%s", _parenthesis.back(), - tk.getText()); - _parenthesis.pop_back(); - } - } - else if (tk.getType() == TokenOpenBracket) - { - _prevbracket++; - res = - res + string_format("%s%s", - _colors[_bracket.size() % _NB_COLORS], - tk.getText()); - _bracket.push_back(_colors[_bracket.size() % _NB_COLORS]); - } - else if (tk.getType() == TokenCloseBracket) - { - - if (_bracket.size() == 0) - { - res = - res + string_format("%s%s", "\u001b[38;5;196m", - tk.getText()); - } - else - { - _prevbracket--; - res = - res + string_format("%s%s", _bracket.back(), tk.getText()); - _bracket.pop_back(); - } - } - /* - else if (tk.type == TokenKeyword) - { - res = res + string_format("%s%s", KeywordTypeFormat[tk._keyword], tk.text.c_str()); - } */ - else - { - Token tkn = *_tks->peek(1); - if (tk.getType() == TokenIdentifier && tkn.getType() == TokenOpenParenthesis) - { - res = - res + string_format("%s%s", tokenFormat[TokenFunction], - tk.getText()); - } - else - { - - res = - res + string_format("%s%s", tokenFormat[tk.type], - tk.getText()); - } - // _tks->prev(); - } - _tks->next(); +string formatLine(string str) { + // Serial.printf("streing:%s\r\n",str.c_str()); + if (str.size() < 2) + return str; + string sd; + sd = ""; + sd = sd + str; + // _parent.clear(); + _for_display = true; + // Script s(&str); + main_script.clear(); + _tks = &__tks; + all_text.clear(); + main_script.addContent((char *)sd.c_str()); + main_script.init(); + // Serial.printf("streing:%s\r\n",str.c_str()); + _tks->tokenize(&main_script, true, true, 1); + // _tks->init(); + // Serial.printf("streing:%s\r\n",str.c_str()); + string res = ""; + + _parenthesis.clear(); + _bracket.clear(); + _parenthesis.clear(); + _prevparenthesis = 0; + _prevcurlybracket = 0; + _prevbracket = 0; + + while (_tks->current()->getType() != + TokenEndOfFile) // for (int i = 0; i < _tks->size(); i++) + { + Token tk = *_tks->current(); + // Serial.printf("token %s\r\n",tk.getText()); + /* if (tk.type == TokenOpenCurlyBracket) + { + + // char *color= (char *)_colors[_curlybracket.size()%_NB_COLORS]; + res = res + string_format("%s%s", _colors[_curlybracket.size() % + _NB_COLORS], tk.text.c_str()); + _curlybracket.push_back(_colors[_curlybracket.size() % _NB_COLORS]); + _prevcurlybracket++; + } + else if (tk.type == TokenCloseCurlyBracket) + { + + if (_curlybracket.size() == 0) + { + res = res + string_format("%s%s", "\u001b[38;5;196m", tk.text.c_str()); + } + else + { + // char * color=_curlybracket.back(); + _prevcurlybracket--; + res = res + string_format("%s%s", _curlybracket.back(), tk.text.c_str()); + _curlybracket.pop_back(); + } + } */ + if (tk.getType() == TokenOpenParenthesis) { + _prevparenthesis++; + res = res + string_format("%s%s", + _colors[(_parenthesis.size() + 2) % _NB_COLORS], + tk.getText()); + _parenthesis.push_back(_colors[(_parenthesis.size() + 2) % _NB_COLORS]); + } else if (tk.getType() == TokenCloseParenthesis) { + + if (_parenthesis.size() == 0) { + res = res + string_format("%s%s", "\u001b[38;5;196m", tk.getText()); + } else { + _prevparenthesis--; + res = res + string_format("%s%s", _parenthesis.back(), tk.getText()); + _parenthesis.pop_back(); + } + } else if (tk.getType() == TokenOpenBracket) { + _prevbracket++; + res = res + string_format("%s%s", _colors[_bracket.size() % _NB_COLORS], + tk.getText()); + _bracket.push_back(_colors[_bracket.size() % _NB_COLORS]); + } else if (tk.getType() == TokenCloseBracket) { + + if (_bracket.size() == 0) { + res = res + string_format("%s%s", "\u001b[38;5;196m", tk.getText()); + } else { + _prevbracket--; + res = res + string_format("%s%s", _bracket.back(), tk.getText()); + _bracket.pop_back(); + } + } + /* + else if (tk.type == TokenKeyword) + { + res = res + string_format("%s%s", KeywordTypeFormat[tk._keyword], + tk.text.c_str()); + } */ + else { + Token tkn = *_tks->peek(1); + if (tk.getType() == TokenIdentifier && + tkn.getType() == TokenOpenParenthesis) { + res = res + + string_format("%s%s", tokenFormat[TokenFunction], tk.getText()); + } else { + + res = res + string_format("%s%s", tokenFormat[tk.type], tk.getText()); + } + // _tks->prev(); } + _tks->next(); + } - _tks->clear(); - // _parent.clear(); + _tks->clear(); + // _parent.clear(); - _for_display = false; - return res; + _for_display = false; + return res; } -class __INIT_TOKEN -{ +class __INIT_TOKEN { public: - __INIT_TOKEN() - { - LedOS.addHightLightinf("sc", formatLine, formatInit, formatLine); - } + __INIT_TOKEN() { + LedOS.addHightLightinf("sc", formatLine, formatInit, formatLine); + } }; __INIT_TOKEN _init_token; #endif diff --git a/src/ESPLivescriptRuntime.h b/src/ESPLivescriptRuntime.h index 8d4a339..d94c5ea 100644 --- a/src/ESPLivescriptRuntime.h +++ b/src/ESPLivescriptRuntime.h @@ -4,23 +4,18 @@ #include using namespace std; - #include "string_function.h" - void pushToConsole(string str, bool force) -{ +void pushToConsole(string str, bool force) { #ifdef __CONSOLE_ESP32 - LedOS.pushToConsole(str, force); + LedOS.pushToConsole(str, force); #else - ESP_LOGD("ESPLiveScript","%s\r\n", str.c_str()); + ESP_LOGD("ESPLiveScript", "%s\r\n", str.c_str()); #endif } -void pushToConsole(string str) -{ - pushToConsole(str, false); -} +void pushToConsole(string str) { pushToConsole(str, false); } uint8_t *binary_data = NULL; uint8_t *tmp_binary_data = NULL; uint8_t *tmp_exec = NULL; @@ -33,57 +28,51 @@ uint16_t tmp_instr_size = 0; uint16_t _data_size = 0; uint16_t tmp_data_size = 0; uint16_t binary_header_size = 0; -enum varTypeEnum -{ - __none__, - __uint8_t__, - __uint16_t__, - __uint32_t__, - __int__, - __s_int__, - __float__, - __void__, - __CRGB__, - __CRGBW__, - __char__, - __Args__, - __bool__, - __userDefined__, - __unknown__ +enum varTypeEnum { + __none__, + __uint8_t__, + __uint16_t__, + __uint32_t__, + __int__, + __s_int__, + __float__, + __void__, + __CRGB__, + __CRGBW__, + __char__, + __Args__, + __bool__, + __userDefined__, + __unknown__ }; -enum class externalType -{ +enum class externalType { function, value, }; -typedef struct -{ - void *ptr; +typedef struct { + void *ptr; externalType type; string name; string signature; string shortname; - + int offset; } asm_external; -typedef struct -{ +typedef struct { string opcde; string operandes; int error; } line; -struct error_message_struct -{ +struct error_message_struct { string error_message; int error; uint16_t line; uint16_t pos; }; -typedef struct -{ +typedef struct { string name; uint32_t address; string variables; @@ -92,14 +81,13 @@ typedef struct } globalcall; typedef struct { - - string json; - uint8_t type; - uint32_t address; + + string json; + uint8_t type; + uint32_t address; } jsonVariable; -typedef struct -{ +typedef struct { error_message_struct error; vector functions; vector jsonVars; @@ -112,72 +100,50 @@ typedef struct } executable; -typedef struct -{ - error_message_struct error; - uint8_t *binary_data; - uint8_t *function_data; - uint16_t instruction_size; - uint16_t tmp_instruction_size; - uint16_t function_size; - uint16_t data_size; - +typedef struct { + error_message_struct error; + uint8_t *binary_data; + uint8_t *function_data; + uint16_t instruction_size; + uint16_t tmp_instruction_size; + uint16_t function_size; + uint16_t data_size; } Binary; -class _arguments -{ +class _arguments { public: - _arguments() - { - vartype = __unknown__; - } - _arguments(int val) - { - vartype = __int__; - intval = val; - } - _arguments(float val) - { - vartype = __float__; - floatval = val; - } - - varTypeEnum vartype; - int intval; - float floatval; + _arguments() { vartype = __unknown__; } + _arguments(int val) { + vartype = __int__; + intval = val; + } + _arguments(float val) { + vartype = __float__; + floatval = val; + } + + varTypeEnum vartype; + int intval; + float floatval; }; -class Arguments -{ +class Arguments { public: - Arguments() {} - void add(int val) - { - _args.push_back(_arguments(val)); - } - void add(float val) - { - _args.push_back(_arguments(val)); - } - void clear() - { - _args.clear(); - _args.shrink_to_fit(); - } - void add(_arguments a) - { - _args.push_back(a); - } - int size() - { - return _args.size(); - } - vector<_arguments> _args; + Arguments() {} + void add(int val) { _args.push_back(_arguments(val)); } + void add(float val) { _args.push_back(_arguments(val)); } + void clear() { + _args.clear(); + _args.shrink_to_fit(); + } + void add(_arguments a) { _args.push_back(a); } + int size() { return _args.size(); } + vector<_arguments> _args; }; #endif #include "FS.h" -#include "asm_external.h" -#include "execute_asm.h" -#include "execute.h" \ No newline at end of file +#include "asm_external.h" +#include "execute.h" +#include "execute_asm.h" \ No newline at end of file diff --git a/src/NodeToken.h b/src/NodeToken.h index 98ddaab..77ee1bf 100644 --- a/src/NodeToken.h +++ b/src/NodeToken.h @@ -9,33 +9,27 @@ #endif using namespace std; +#include "asm_external.h" #include "string_function.h" - #include "tokenizer.h" -#include "asm_external.h" #define _MAX_FOR_DEPTH_REG 4 #define _MAX_FOR_DEPTH_REG_2 2 // int _for_depth_reg = 0; // bool _is_variable_as_register = false; -void pushToConsole(string str, bool force) -{ +void pushToConsole(string str, bool force) { #ifdef __CONSOLE_ESP32 - LedOS.pushToConsole(str, force); + LedOS.pushToConsole(str, force); #else #ifndef __TEST_DEBUG - ESP_LOGD("ESPLiveScript", "%s\r\n", str.c_str()); + ESP_LOGD("ESPLiveScript", "%s\r\n", str.c_str()); #else - printf("%s\r\n", str.c_str()); + printf("%s\r\n", str.c_str()); #endif - #endif } -void pushToConsole(string str) -{ - pushToConsole(str, false); -} +void pushToConsole(string str) { pushToConsole(str, false); } #define _START_2 32 #define _STACK_SIZE (_START_2 + 6 * 4) @@ -68,11 +62,10 @@ string struct_name = ""; vector nb_args; vector sigs; vector nb_sav_args; -typedef struct -{ - string json; - string variable; - varTypeEnum type; +typedef struct { + string json; + string variable; + varTypeEnum type; } jsonStruct; @@ -121,187 +114,186 @@ vector _compare; // token *__current; int __sav_pos; -void initMem() -{ +void initMem() { #ifndef __COMPILER_TEST - __startmem = esp_get_free_heap_size(); - TaskHandle_t loopTaskHandle = xTaskGetCurrentTaskHandle(); - - // Get the minimum stack size left for the loop task - UBaseType_t stackHighWaterMark = uxTaskGetStackHighWaterMark(loopTaskHandle); - __startStackMemory = stackHighWaterMark * sizeof(StackType_t); - __maxMemUsage = 0; - __MaxStackMemory = 0; - __starttime = ESP.getCycleCount(); - __exe_size = 0; - // printf("We satrt with: %ld free and stack:%ld \n", __startmem, __startStackMemory); + __startmem = esp_get_free_heap_size(); + TaskHandle_t loopTaskHandle = xTaskGetCurrentTaskHandle(); + + // Get the minimum stack size left for the loop task + UBaseType_t stackHighWaterMark = uxTaskGetStackHighWaterMark(loopTaskHandle); + __startStackMemory = stackHighWaterMark * sizeof(StackType_t); + __maxMemUsage = 0; + __MaxStackMemory = 0; + __starttime = ESP.getCycleCount(); + __exe_size = 0; + // printf("We satrt with: %ld free and stack:%ld \n", __startmem, + // __startStackMemory); #endif } -void displayStat(char *text) -{ - pushToConsole(string_format(" %s :max used memory: %ld maxstack:%ld started %d free mem:%ld consumed %ld time:%dms", text, __maxMemUsage, __MaxStackMemory, __startmem, esp_get_free_heap_size(), __startmem - esp_get_free_heap_size(), (__endtime - __starttime) / 240000)); -} -void displayStat() -{ - displayStat(""); +void displayStat(char *text) { + pushToConsole(string_format(" %s :max used memory: %ld maxstack:%ld started " + "%d free mem:%ld consumed %ld time:%dms", + text, __maxMemUsage, __MaxStackMemory, __startmem, + esp_get_free_heap_size(), + __startmem - esp_get_free_heap_size(), + (__endtime - __starttime) / 240000)); } -void updateMem() -{ +void displayStat() { displayStat(""); } +void updateMem() { #ifndef __COMPILER_TEST - if (esp_get_free_heap_size() > __startmem) - __startmem = esp_get_free_heap_size(); - uint32_t newdelta = __startmem - esp_get_free_heap_size(); - if (newdelta > __maxMemUsage) - __maxMemUsage = newdelta; - TaskHandle_t loopTaskHandle = xTaskGetCurrentTaskHandle(); + if (esp_get_free_heap_size() > __startmem) + __startmem = esp_get_free_heap_size(); + uint32_t newdelta = __startmem - esp_get_free_heap_size(); + if (newdelta > __maxMemUsage) + __maxMemUsage = newdelta; + TaskHandle_t loopTaskHandle = xTaskGetCurrentTaskHandle(); - // Get the minimum stack size left for the loop task - UBaseType_t stackHighWaterMark = uxTaskGetStackHighWaterMark(loopTaskHandle); + // Get the minimum stack size left for the loop task + UBaseType_t stackHighWaterMark = uxTaskGetStackHighWaterMark(loopTaskHandle); - // Print the remaining stack size in bytes - if (__startStackMemory < stackHighWaterMark * sizeof(StackType_t)) - __startStackMemory = stackHighWaterMark * sizeof(StackType_t); + // Print the remaining stack size in bytes + if (__startStackMemory < stackHighWaterMark * sizeof(StackType_t)) + __startStackMemory = stackHighWaterMark * sizeof(StackType_t); - newdelta = __startStackMemory - stackHighWaterMark * sizeof(StackType_t); - if (newdelta > __MaxStackMemory) - __MaxStackMemory = newdelta; + newdelta = __startStackMemory - stackHighWaterMark * sizeof(StackType_t); + if (newdelta > __MaxStackMemory) + __MaxStackMemory = newdelta; - __endtime = ESP.getCycleCount(); + __endtime = ESP.getCycleCount(); #if PARSER_DEBUG == 1 - displayStat(); + displayStat(); #endif - // printf("max memory: %ld mem and stack:%ld free mem:%ld\n", __maxMemUsage, __MaxStackMemory, esp_get_free_heap_size()); + // printf("max memory: %ld mem and stack:%ld free mem:%ld\n", __maxMemUsage, + // __MaxStackMemory, esp_get_free_heap_size()); #endif } -string _numToBytes(uint32_t __num) -{ - string val = ".bytes 4"; - uint8_t c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - c = __num & 0xff; - val = val + " " + string_format("%02x", c); - return val; +string _numToBytes(uint32_t __num) { + string val = ".bytes 4"; + uint8_t c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + c = __num & 0xff; + val = val + " " + string_format("%02x", c); + return val; } enum nodeType { - typeNode, - numberNode, - binOpNode, - unitaryOpNode, - operatorNode, - globalVariableNode, - localVariableNode, - localVariableNodeAsRegister, - blockStatementNode, - defFunctionNode, - statementNode, - programNode, - assignementNode, - comparatorNode, - callFunctionNode, - forNode, - argumentNode, - extGlobalVariableNode, - extDefFunctionNode, - extCallFunctionNode, - returnArgumentNode, - variableDeclarationNode, - defExtFunctionNode, - inputArgumentsNode, - defExtGlobalVariableNode, - defGlobalVariableNode, - defLocalVariableNode, - defLocalVariableNodeAsRegister, - storeLocalVariableNode, - storeLocalVariableNodeAsRegister, - storeGlobalVariableNode, - storeExtGlocalVariableNode, - ifNode, - elseNode, - whileNode, - returnNode, - defAsmFunctionNode, - stringNode, - changeTypeNode, - importNode, - continueNode, - breakNode, - testNode, - ternaryIfNode, - callConstructorNode, - defInputArgumentsNode, - declarationFunctionNode, - UnknownNode, - onlyNode + typeNode, + numberNode, + binOpNode, + unitaryOpNode, + operatorNode, + globalVariableNode, + localVariableNode, + localVariableNodeAsRegister, + blockStatementNode, + defFunctionNode, + statementNode, + programNode, + assignementNode, + comparatorNode, + callFunctionNode, + forNode, + argumentNode, + extGlobalVariableNode, + extDefFunctionNode, + extCallFunctionNode, + returnArgumentNode, + variableDeclarationNode, + defExtFunctionNode, + inputArgumentsNode, + defExtGlobalVariableNode, + defGlobalVariableNode, + defLocalVariableNode, + defLocalVariableNodeAsRegister, + storeLocalVariableNode, + storeLocalVariableNodeAsRegister, + storeGlobalVariableNode, + storeExtGlocalVariableNode, + ifNode, + elseNode, + whileNode, + returnNode, + defAsmFunctionNode, + stringNode, + changeTypeNode, + importNode, + continueNode, + breakNode, + testNode, + ternaryIfNode, + callConstructorNode, + defInputArgumentsNode, + declarationFunctionNode, + UnknownNode, + onlyNode }; -string nodeTypeNames[] = - { +string nodeTypeNames[] = { #ifdef __TEST_DEBUG - "typeNode", - "numberNode", - "binOpNode", - "unitaryOpNode", - "operatorNode", - "globalVariableNode", - "localVariableNode", - "localVariableNodeAsRegister", - "blockStatementNode", - "defFunctionNode", - "statementNode", - "programNode", - "assignementNode", - "comparatorNode", - "callFunctionNode", - "forNode", - "argumentNode", - "extGlobalVariableNode", - "extDefFunctionNode", - "extCallFunctionNode", - "returnArgumentNode", - "variableDeclarationNode", - "defExtFunctionNode", - "inputArgumentsNode", - "defExtGlobalVariableNode", - "defGlobalVariableNode", - "defLocalVariableNode", - "defLocalVariableNodeAsRegister", - "storeLocalVariableNode", - "storeLocalVariableNodeAsRegister", - "storeGlobalVariableNode", - "storeExtGlocalVariableNode", - "ifNode", - "elseNode", - "whileNode", - "returnNode", - "defAsmFunctionNode", - "stringNode", - "changeTypeNode", - "importNode", - "continueNode", - "breakNode", - "testNode", - "ternaryIfNode", - "callConstructorNode", - "defInputArgumentsNode", - "declarationFunctionNode", - "UnknownNode" - "OnlyNode" + "typeNode", + "numberNode", + "binOpNode", + "unitaryOpNode", + "operatorNode", + "globalVariableNode", + "localVariableNode", + "localVariableNodeAsRegister", + "blockStatementNode", + "defFunctionNode", + "statementNode", + "programNode", + "assignementNode", + "comparatorNode", + "callFunctionNode", + "forNode", + "argumentNode", + "extGlobalVariableNode", + "extDefFunctionNode", + "extCallFunctionNode", + "returnArgumentNode", + "variableDeclarationNode", + "defExtFunctionNode", + "inputArgumentsNode", + "defExtGlobalVariableNode", + "defGlobalVariableNode", + "defLocalVariableNode", + "defLocalVariableNodeAsRegister", + "storeLocalVariableNode", + "storeLocalVariableNodeAsRegister", + "storeGlobalVariableNode", + "storeExtGlocalVariableNode", + "ifNode", + "elseNode", + "whileNode", + "returnNode", + "defAsmFunctionNode", + "stringNode", + "changeTypeNode", + "importNode", + "continueNode", + "breakNode", + "testNode", + "ternaryIfNode", + "callConstructorNode", + "defInputArgumentsNode", + "declarationFunctionNode", + "UnknownNode" + "OnlyNode" #endif @@ -310,64 +302,56 @@ string nodeTypeNames[] = typedef struct { - int error; - string error_message; + int error; + string error_message; } f_error_struct; -int findMember(varType *v, string member) -{ - // printf("zerk %d %s\n", v->size, v->varName.c_str()); - for (int i = 0; i < v->size; i++) - { - // printf("look for %s %s\n", member.c_str(), v->membersNames[i].c_str()); - if (v->membersNames[i].compare(member) == 0) - { - return i; - } +int findMember(varType *v, string member) { + // printf("zerk %d %s\n", v->size, v->varName.c_str()); + for (int i = 0; i < v->size; i++) { + // printf("look for %s %s\n", member.c_str(), + // v->membersNames[i].c_str()); + if (v->membersNames[i].compare(member) == 0) { + return i; } - return -1; + } + return -1; } -int findMember(uint8_t _v, string member) -{ - - varType *v = &_userDefinedTypes[_v]; - // printf("zerk dfin %d %s\n",v->size,v->varName.c_str()); - for (int i = 0; i < v->size; i++) - { - // printf("look for %s %s\n",member.c_str(),v->membersNames[i].c_str()); - if (v->membersNames[i].compare(member) == 0) - { - return i; - } - } - return -1; +int findMember(uint8_t _v, string member) { + + varType *v = &_userDefinedTypes[_v]; + // printf("zerk dfin %d %s\n",v->size,v->varName.c_str()); + for (int i = 0; i < v->size; i++) { + // printf("look for %s + // %s\n",member.c_str(),v->membersNames[i].c_str()); + if (v->membersNames[i].compare(member) == 0) { + return i; + } + } + return -1; } -int findStruct(char *structName) -{ - for (int i = 0; i < _userDefinedTypes.size(); i++) - { - if (strcmp(_userDefinedTypes[i].varName.c_str(), structName) == 0) - { - return i; - } +int findStruct(char *structName) { + for (int i = 0; i < _userDefinedTypes.size(); i++) { + if (strcmp(_userDefinedTypes[i].varName.c_str(), structName) == 0) { + return i; } - return -1; + } + return -1; } // class Context; uint16_t stringToInt(char *str) { - uint16_t res = 0; - int i = 0; - while (str[i] != 0) - { - res = 10 * res + (str[i] - 48); - i++; - } - return res; + uint16_t res = 0; + int i = 0; + while (str[i] != 0) { + res = 10 * res + (str[i] - 48); + i++; + } + return res; } Text all_targets; @@ -428,587 +412,511 @@ void *__objectbuff; NodeToken *__tmpToken; vector *__vtmp; // NodeToken *tmpToken; -class NodeToken -{ +class NodeToken { public: - NodeToken() - { + NodeToken() { + + children = NULL; + + parent = NULL; + + _total_size = 1; + target = EOF_TEXTARRAY; + textref = EOF_TEXTARRAY; + stack_pos = 0; + isPointer = false; + asPointer = false; + _nodetype = (int)UnknownNode; + + type = (int)TokenUnknown; + _vartype = EOF_VARTYPE; + } + NodeToken(nodeType tt) { + _nodetype = tt; + children = NULL; + } + NodeToken(Token *t, nodeType tt) { + type = t->type; + textref = t->textref; + _vartype = t->_vartype; + _nodetype = tt; + children = NULL; + } + NodeToken(Token *t) { + type = t->type; + textref = t->textref; + _vartype = t->_vartype; + children = NULL; + } + NodeToken(Token t, nodeType tt) { + type = t.type; + textref = t.textref; + _vartype = t._vartype; + _nodetype = tt; + children = NULL; + } + NodeToken(NodeToken nd, nodeType tt) { + + type = nd.type; + textref = nd.textref; + _vartype = nd._vartype; + _nodetype = tt; + isPointer = nd.isPointer; + _total_size = nd._total_size; + target = nd.target; + children = NULL; + + if (tt == defLocalVariableNode) { + isPointer = nd.isPointer; + _total_size = nd._total_size; + // visitNode=visitNodeDefLocalVariable; + int delta = 0; + if (nd.isPointer) { + if (stack_size % 4 != 0) + delta = 4 - stack_size % 4; + } + if (nd.getVarType()->_varType == __uint32_t__ || + nd.getVarType()->_varType == __float__ || + nd.getVarType()->_varType == __CRGB__ || + nd.getVarType()->_varType == __int__ || + nd.getVarType()->_varType == + __userDefined__) //|| nd.getVarType()->_varType == __CRGBW__) + { + if (stack_size % 4 != 0) { + if (nd.getVarType()->_varSize % 2 == 0) + delta = nd.getVarType()->_varSize - stack_size % 4; + else + delta = nd.getVarType()->_varSize - stack_size % 4 + 1; + } + } else if (nd.getVarType()->_varType == __uint16_t__ || + nd.getVarType()->_varType == __s_int__) { + if (stack_size % 2 != 0) { + delta = 1; + } + } + stack_size += delta; + nd.stack_pos = stack_size; + if (nd.isPointer) { + stack_size += 4; + } else { + stack_size += nd.getVarType()->_varSize; + } + + stack_pos = nd.stack_pos; + } + } + NodeToken(NodeToken *nd, nodeType tt) { + + type = nd->type; + textref = nd->textref; + _vartype = nd->_vartype; + _nodetype = tt; + isPointer = nd->isPointer; + asPointer = nd->asPointer; + _total_size = nd->_total_size; + stack_pos = nd->stack_pos; + target = nd->target; + children = NULL; + } + NodeToken(NodeToken *nd) { + + type = nd->type; + textref = nd->textref; + _vartype = nd->_vartype; + _nodetype = nd->_nodetype; + isPointer = nd->isPointer; + asPointer = nd->asPointer; + _total_size = nd->_total_size; + stack_pos = nd->stack_pos; + target = nd->target; + children = NULL; + } + NodeToken(string _target, nodeType tt) { + _nodetype = tt; + addTargetText(_target); + children = NULL; + } + NodeToken(NodeToken nd, nodeType tt, string _target) { + + type = nd.type; + textref = nd.textref; + _vartype = nd._vartype; + isPointer = nd.isPointer; + _total_size = nd._total_size; + _nodetype = tt; + stack_pos = nd.stack_pos; + addTargetText(_target); + children = NULL; + } + NodeToken *addChild(NodeToken *j) { + j->parent = this; + if (children == NULL) { + children = new vector(); + } + children->push_back(j); + return j; + } + NodeToken *addChildFront(NodeToken *j) { + j->parent = this; + if (children == NULL) { + children = new vector(); + } + // children->push_front(j); + children->insert(children->begin(), j); + return j; + } + void copyChildren(NodeToken *k) { + if (k->children_size() > 0) { + for (NodeToken *lm : *k->children) { + NodeToken *hj = addChild(*lm); + if (lm->children_size() > 0) { + for (NodeToken *kl : *lm->children) { + hj->addChild(*kl); + } + } + } + } + } + NodeToken *addChild(NodeToken nd) { + // nd.parent = this; + // nd.children->clear(); + + // printf("ok pour crear %s\n",nodeTypeNames[j._nodetype].c_str()); + __tmpToken = new NodeToken(); + __tmpToken->type = nd.type; + __tmpToken->textref = nd.textref; + __tmpToken->_vartype = nd._vartype; + __tmpToken->_nodetype = nd._nodetype; + __tmpToken->isPointer = nd.isPointer; + __tmpToken->asPointer = nd.asPointer; + __tmpToken->_total_size = nd._total_size; + __tmpToken->stack_pos = nd.stack_pos; + __tmpToken->target = nd.target; + __tmpToken->parent = this; + __tmpToken->children = NULL; + if (children == NULL) { + children = new vector(); + } + children->push_back(__tmpToken); + return __tmpToken; + } + NodeToken *addChildFront(NodeToken nd) { + nd.parent = this; + // nd.children->clear(); + __tmpToken = new NodeToken(); + __tmpToken->type = nd.type; + __tmpToken->textref = nd.textref; + __tmpToken->_vartype = nd._vartype; + __tmpToken->_nodetype = nd._nodetype; + __tmpToken->isPointer = nd.isPointer; + __tmpToken->asPointer = nd.asPointer; + __tmpToken->_total_size = nd._total_size; + __tmpToken->stack_pos = nd.stack_pos; + __tmpToken->target = nd.target; + __tmpToken->parent = this; + __tmpToken->children = NULL; + + __tmpToken->children = NULL; + if (children == NULL) { + children = new vector(); + + // children = __vtmp; + } + children->insert(children->begin(), __tmpToken); + return __tmpToken; + } + void clear(bool all) { + // printf("on efface %s\n",getTokenText()); + if (children != NULL) { + // printf("clearing nodetype %d %d\n",_nodetype,children->size()); + for (int i = 0; i < children->size(); i++) { + // printf("child :%i\n",i); + NodeToken *child = children->at(i); + // printf(" on efface %s\n",child->getTokenText()); + if (child != NULL) { + child->clearAll(); + child->parent = NULL; + free(child); + child = NULL; + } + } + + children->clear(); + children->shrink_to_fit(); + free(children); + children = NULL; + } + } + void clear() { clear(false); } + void clearAll() { clear(true); } + + void replaceTargetText(string t) { all_targets.replaceText(textref, t); } + NodeToken *getChildAtPos(int pos) { + if (children == NULL) + return NULL; + if (pos >= 0 and pos < children_size()) + return (*children)[pos]; + else + return NULL; + } + char *getTokenText() { return all_text.getText(textref); } + void setTokenText(string t) { textref = all_text.addText(t); } + void addTargetText(string t) { target = all_targets.addText(t); } + char *getTargetText() { return all_targets.getText(target); } + void setTargetText(string t) { target = all_targets.addText(t); } + + varType *getVarType() { + if (_vartype == EOF_VARTYPE) + + return NULL; + + if (type == TokenUserDefinedVariable) { + /// int k = findStruct(getTokenText()); + // if (k > -1) + // { + if (target != EOF_TEXTARRAY) { + if (string(getTargetText()).compare(0, 1, "@") == 0) { + return &_userDefinedTypes[_vartype]; + } + int i = findMember(_vartype, string(getTargetText())); + if (i > -1) { + return &_varTypes[(_userDefinedTypes[_vartype]).types[i]]; + } else { + printf("member %s not foudn in %s\n", getTargetText(), + _userDefinedTypes[_vartype].varName.c_str()); + return NULL; + } + } + + return &_userDefinedTypes[_vartype]; + // } + // else + // return &_varTypes[_vartype]; + } else + return &_varTypes[_vartype]; + } + nodeType getNodeTokenType() { return (nodeType)_nodetype; } + int children_size() { + if (children == NULL) + return 0; + else + return children->size(); + } + int findMaxArgumentSize() { + int cur_size = 0; + if (_nodetype == extCallFunctionNode or _nodetype == callFunctionNode) { + cur_size = getChildAtPos(1)->children_size(); + for (int i = 0; i < getChildAtPos(2)->children_size(); i++) { + int cmp = getChildAtPos(2)->getChildAtPos(i)->findMaxArgumentSize(); + if (cmp > cur_size) + cur_size = cmp; + } + } else { + if (children_size() > 0) { + for (int i = 0; i < children_size(); i++) { + int cmp = getChildAtPos(i)->findMaxArgumentSize(); + if (cmp > cur_size) + cur_size = cmp; + } + } + } + return cur_size; + } + void visitNode() { + switch ((nodeType)_nodetype) { + case typeNode: + _visittypeNode(this); + break; + case numberNode: + _visitnumberNode(this); + break; + + case binOpNode: + _visitbinOpNode(this); + break; + + case unitaryOpNode: + _visitunitaryOpNode(this); + break; + + case operatorNode: + _visitoperatorNode(this); + break; - children = NULL; + case globalVariableNode: + _visitglobalVariableNode(this); + break; - parent = NULL; + case localVariableNode: + _visitlocalVariableNode(this); + break; - _total_size = 1; - target = EOF_TEXTARRAY; - textref = EOF_TEXTARRAY; - stack_pos = 0; - isPointer = false; - asPointer = false; - _nodetype = (int)UnknownNode; + case blockStatementNode: + _visitblockStatementNode(this); + break; - type = (int)TokenUnknown; - _vartype = EOF_VARTYPE; - } - NodeToken(nodeType tt) - { - _nodetype = tt; - children = NULL; - } - NodeToken(Token *t, nodeType tt) - { - type = t->type; - textref = t->textref; - _vartype = t->_vartype; - _nodetype = tt; - children = NULL; - } - NodeToken(Token *t) - { - type = t->type; - textref = t->textref; - _vartype = t->_vartype; - children = NULL; - } - NodeToken(Token t, nodeType tt) - { - type = t.type; - textref = t.textref; - _vartype = t._vartype; - _nodetype = tt; - children = NULL; - } - NodeToken(NodeToken nd, nodeType tt) - { + case defFunctionNode: + _visitdefFunctionNode(this); + break; - type = nd.type; - textref = nd.textref; - _vartype = nd._vartype; - _nodetype = tt; - isPointer = nd.isPointer; - _total_size = nd._total_size; - target = nd.target; - children = NULL; + case statementNode: + _visitstatementNode(this); + break; - if (tt == defLocalVariableNode) - { - isPointer = nd.isPointer; - _total_size = nd._total_size; - // visitNode=visitNodeDefLocalVariable; - int delta = 0; - if (nd.isPointer) - { - if (stack_size % 4 != 0) - delta = 4 - stack_size % 4; - } - if (nd.getVarType()->_varType == __uint32_t__ || nd.getVarType()->_varType == __float__ || nd.getVarType()->_varType == __CRGB__ || nd.getVarType()->_varType == __int__ || nd.getVarType()->_varType == __userDefined__) //|| nd.getVarType()->_varType == __CRGBW__) - { - if (stack_size % 4 != 0) - { - if (nd.getVarType()->_varSize % 2 == 0) - delta = nd.getVarType()->_varSize - stack_size % 4; - else - delta = nd.getVarType()->_varSize - stack_size % 4 + 1; - } - } - else if (nd.getVarType()->_varType == __uint16_t__ || nd.getVarType()->_varType == __s_int__) - { - if (stack_size % 2 != 0) - { - delta = 1; - } - } - stack_size += delta; - nd.stack_pos = stack_size; - if (nd.isPointer) - { - stack_size += 4; - } - else - { - stack_size += nd.getVarType()->_varSize; - } + case programNode: + _visitprogramNode(this); + break; - stack_pos = nd.stack_pos; - } - } - NodeToken(NodeToken *nd, nodeType tt) - { + case assignementNode: + _visitassignementNode(this); + break; - type = nd->type; - textref = nd->textref; - _vartype = nd->_vartype; - _nodetype = tt; - isPointer = nd->isPointer; - asPointer = nd->asPointer; - _total_size = nd->_total_size; - stack_pos = nd->stack_pos; - target = nd->target; - children = NULL; - } - NodeToken(NodeToken *nd) - { + case comparatorNode: + _visitcomparatorNode(this); + break; - type = nd->type; - textref = nd->textref; - _vartype = nd->_vartype; - _nodetype = nd->_nodetype; - isPointer = nd->isPointer; - asPointer = nd->asPointer; - _total_size = nd->_total_size; - stack_pos = nd->stack_pos; - target = nd->target; - children = NULL; - } - NodeToken(string _target, nodeType tt) - { - _nodetype = tt; - addTargetText(_target); - children = NULL; - } - NodeToken(NodeToken nd, nodeType tt, string _target) - { + case callFunctionNode: + _visitcallFunctionNode(this); + break; - type = nd.type; - textref = nd.textref; - _vartype = nd._vartype; - isPointer = nd.isPointer; - _total_size = nd._total_size; - _nodetype = tt; - stack_pos = nd.stack_pos; - addTargetText(_target); - children = NULL; - } - NodeToken *addChild(NodeToken *j) - { - j->parent = this; - if (children == NULL) - { - children = new vector(); - } - children->push_back(j); - return j; - } - NodeToken *addChildFront(NodeToken *j) - { - j->parent = this; - if (children == NULL) - { - children = new vector(); - } - // children->push_front(j); - children->insert(children->begin(), j); - return j; - } - void copyChildren(NodeToken *k) - { - if (k->children_size() > 0) - { - for (NodeToken *lm : *k->children) - { - NodeToken *hj = addChild(*lm); - if (lm->children_size() > 0) - { - for (NodeToken *kl : *lm->children) - { - hj->addChild(*kl); - } - } - } - } - } - NodeToken *addChild(NodeToken nd) - { - // nd.parent = this; - // nd.children->clear(); - - // printf("ok pour crear %s\n",nodeTypeNames[j._nodetype].c_str()); - __tmpToken = new NodeToken(); - __tmpToken->type = nd.type; - __tmpToken->textref = nd.textref; - __tmpToken->_vartype = nd._vartype; - __tmpToken->_nodetype = nd._nodetype; - __tmpToken->isPointer = nd.isPointer; - __tmpToken->asPointer = nd.asPointer; - __tmpToken->_total_size = nd._total_size; - __tmpToken->stack_pos = nd.stack_pos; - __tmpToken->target = nd.target; - __tmpToken->parent = this; - __tmpToken->children = NULL; - if (children == NULL) - { - children = new vector(); - } - children->push_back(__tmpToken); - return __tmpToken; - } - NodeToken *addChildFront(NodeToken nd) - { - nd.parent = this; - // nd.children->clear(); - __tmpToken = new NodeToken(); - __tmpToken->type = nd.type; - __tmpToken->textref = nd.textref; - __tmpToken->_vartype = nd._vartype; - __tmpToken->_nodetype = nd._nodetype; - __tmpToken->isPointer = nd.isPointer; - __tmpToken->asPointer = nd.asPointer; - __tmpToken->_total_size = nd._total_size; - __tmpToken->stack_pos = nd.stack_pos; - __tmpToken->target = nd.target; - __tmpToken->parent = this; - __tmpToken->children = NULL; - - __tmpToken->children = NULL; - if (children == NULL) - { - children = new vector(); + case forNode: + _visitforNode(this); + break; - // children = __vtmp; - } - children->insert(children->begin(), __tmpToken); - return __tmpToken; - } - void clear(bool all) - { - // printf("on efface %s\n",getTokenText()); - if (children != NULL) - { - // printf("clearing nodetype %d %d\n",_nodetype,children->size()); - for (int i = 0; i < children->size(); i++) - { - // printf("child :%i\n",i); - NodeToken *child = children->at(i); - // printf(" on efface %s\n",child->getTokenText()); - if (child != NULL) - { - child->clearAll(); - child->parent = NULL; - free(child); - child = NULL; - } - } + case argumentNode: + _visitargumentNode(this); + break; - children->clear(); - children->shrink_to_fit(); - free(children); - children = NULL; - } - } - void clear() - { - clear(false); - } - void clearAll() - { - clear(true); - } + case extGlobalVariableNode: + _visitextGlobalVariableNode(this); + break; + /* + case extDefFunctionNode: + _visitextDefFunctionNode(this); + break; + */ + case extCallFunctionNode: + _visitextCallFunctionNode(this); + break; + + case returnArgumentNode: + _visitreturnArgumentNode(this); + break; + + case variableDeclarationNode: + _visitvariableDeclarationNode(this); + break; + + case defExtFunctionNode: + _visitdefExtFunctionNode(this); + break; + + case inputArgumentsNode: + _visitinputArgumentsNode(this); + break; + + case defInputArgumentsNode: + _visitdefInputArgumentsNode(this); + break; - void replaceTargetText(string t) - { - all_targets.replaceText(textref, t); - } - NodeToken *getChildAtPos(int pos) - { - if (children == NULL) - return NULL; - if (pos >= 0 and pos < children_size()) - return (*children)[pos]; - else - return NULL; - } - char *getTokenText() - { - return all_text.getText(textref); - } - void setTokenText(string t) - { - textref = all_text.addText(t); - } - void addTargetText(string t) - { - target = all_targets.addText(t); - } - char *getTargetText() - { - return all_targets.getText(target); - } - void setTargetText(string t) - { - target = all_targets.addText(t); - } + case defExtGlobalVariableNode: + _visitdefExtGlobalVariableNode(this); + break; - varType *getVarType() - { - if (_vartype == EOF_VARTYPE) + case defGlobalVariableNode: + _visitdefGlobalVariableNode(this); + break; - return NULL; + case defLocalVariableNode: + _visitdefLocalVariableNode(this); + break; - if (type == TokenUserDefinedVariable) - { - /// int k = findStruct(getTokenText()); - // if (k > -1) - // { - if (target != EOF_TEXTARRAY) - { - if (string(getTargetText()).compare(0, 1, "@") == 0) - { - return &_userDefinedTypes[_vartype]; - } - int i = findMember(_vartype, string(getTargetText())); - if (i > -1) - { - return &_varTypes[(_userDefinedTypes[_vartype]).types[i]]; - } - else - { - printf("member %s not foudn in %s\n", getTargetText(), _userDefinedTypes[_vartype].varName.c_str()); - return NULL; - } - } + case storeLocalVariableNode: + _visitstoreLocalVariableNode(this); + break; - return &_userDefinedTypes[_vartype]; - // } - // else - // return &_varTypes[_vartype]; - } - else - return &_varTypes[_vartype]; - } - nodeType getNodeTokenType() - { - return (nodeType)_nodetype; - } - int children_size() - { - if (children == NULL) - return 0; - else - return children->size(); - } - int findMaxArgumentSize() - { - int cur_size = 0; - if (_nodetype == extCallFunctionNode or _nodetype == callFunctionNode) - { - cur_size = getChildAtPos(1)->children_size(); - for (int i = 0; i < getChildAtPos(2)->children_size(); i++) - { - int cmp = getChildAtPos(2)->getChildAtPos(i)->findMaxArgumentSize(); - if (cmp > cur_size) - cur_size = cmp; - } - } - else - { - if (children_size() > 0) - { - for (int i = 0; i < children_size(); i++) - { - int cmp = getChildAtPos(i)->findMaxArgumentSize(); - if (cmp > cur_size) - cur_size = cmp; - } - } - } - return cur_size; - } - void visitNode() - { - switch ((nodeType)_nodetype) - { - case typeNode: - _visittypeNode(this); - break; - case numberNode: - _visitnumberNode(this); - break; - - case binOpNode: - _visitbinOpNode(this); - break; - - case unitaryOpNode: - _visitunitaryOpNode(this); - break; - - case operatorNode: - _visitoperatorNode(this); - break; - - case globalVariableNode: - _visitglobalVariableNode(this); - break; - - case localVariableNode: - _visitlocalVariableNode(this); - break; - - case blockStatementNode: - _visitblockStatementNode(this); - break; - - case defFunctionNode: - _visitdefFunctionNode(this); - break; - - case statementNode: - _visitstatementNode(this); - break; - - case programNode: - _visitprogramNode(this); - break; - - case assignementNode: - _visitassignementNode(this); - break; - - case comparatorNode: - _visitcomparatorNode(this); - break; - - case callFunctionNode: - _visitcallFunctionNode(this); - break; - - case forNode: - _visitforNode(this); - break; - - case argumentNode: - _visitargumentNode(this); - break; - - case extGlobalVariableNode: - _visitextGlobalVariableNode(this); - break; - /* - case extDefFunctionNode: - _visitextDefFunctionNode(this); - break; - */ - case extCallFunctionNode: - _visitextCallFunctionNode(this); - break; - - case returnArgumentNode: - _visitreturnArgumentNode(this); - break; - - case variableDeclarationNode: - _visitvariableDeclarationNode(this); - break; - - case defExtFunctionNode: - _visitdefExtFunctionNode(this); - break; - - case inputArgumentsNode: - _visitinputArgumentsNode(this); - break; - - case defInputArgumentsNode: - _visitdefInputArgumentsNode(this); - break; - - case defExtGlobalVariableNode: - _visitdefExtGlobalVariableNode(this); - break; - - case defGlobalVariableNode: - _visitdefGlobalVariableNode(this); - break; - - case defLocalVariableNode: - _visitdefLocalVariableNode(this); - break; - - case storeLocalVariableNode: - _visitstoreLocalVariableNode(this); - break; - - case storeGlobalVariableNode: - _visitstoreGlobalVariableNode(this); - break; - - case storeExtGlocalVariableNode: - _visitstoreExtGlocalVariableNode(this); - break; - - case ifNode: - _visitifNode(this); - break; - case localVariableNodeAsRegister: - _visitlocalVariableNodeAsRegister(this); - break; - case storeLocalVariableNodeAsRegister: - _visitstoreLocalVariableNodeAsRegister(this); - break; - case elseNode: - _visitelseNode(this); - break; - - case whileNode: - _visitwhileNode(this); - break; - - case returnNode: - _visitreturnNode(this); - break; - - case defAsmFunctionNode: - _visitdefAsmFunctionNode(this); - break; - - case stringNode: - _visitstringNode(this); - break; - - case changeTypeNode: - _visitchangeTypeNode(this); - break; - - case importNode: - _visitimportNode(this); - break; - - case continueNode: - _visitcontinueNode(this); - break; - - case breakNode: - _visitbreakNode(this); - break; - case testNode: - _visittestNode(this); - break; - - case ternaryIfNode: - _visitternaryIfNode(this); - break; - case callConstructorNode: - _visitcallConstructorNode(this); - break; - case UnknownNode: - _visitUnknownNode(this); - break; - default: - break; - } - } - vector *children = NULL; - NodeToken *parent = NULL; - uint16_t _total_size = 1; - uint16_t target = EOF_TEXTARRAY; - uint16_t textref = EOF_TEXTARRAY; - uint16_t stack_pos = 0; - bool isPointer = false; - bool asPointer = false; - uint8_t _nodetype = (int)UnknownNode; - - uint8_t type = (int)TokenUnknown; - uint8_t _vartype = EOF_VARTYPE; + case storeGlobalVariableNode: + _visitstoreGlobalVariableNode(this); + break; + + case storeExtGlocalVariableNode: + _visitstoreExtGlocalVariableNode(this); + break; + + case ifNode: + _visitifNode(this); + break; + case localVariableNodeAsRegister: + _visitlocalVariableNodeAsRegister(this); + break; + case storeLocalVariableNodeAsRegister: + _visitstoreLocalVariableNodeAsRegister(this); + break; + case elseNode: + _visitelseNode(this); + break; + + case whileNode: + _visitwhileNode(this); + break; + + case returnNode: + _visitreturnNode(this); + break; + + case defAsmFunctionNode: + _visitdefAsmFunctionNode(this); + break; + + case stringNode: + _visitstringNode(this); + break; + + case changeTypeNode: + _visitchangeTypeNode(this); + break; + + case importNode: + _visitimportNode(this); + break; + + case continueNode: + _visitcontinueNode(this); + break; + + case breakNode: + _visitbreakNode(this); + break; + case testNode: + _visittestNode(this); + break; + + case ternaryIfNode: + _visitternaryIfNode(this); + break; + case callConstructorNode: + _visitcallConstructorNode(this); + break; + case UnknownNode: + _visitUnknownNode(this); + break; + default: + break; + } + } + vector *children = NULL; + NodeToken *parent = NULL; + uint16_t _total_size = 1; + uint16_t target = EOF_TEXTARRAY; + uint16_t textref = EOF_TEXTARRAY; + uint16_t stack_pos = 0; + bool isPointer = false; + bool asPointer = false; + uint8_t _nodetype = (int)UnknownNode; + + uint8_t type = (int)TokenUnknown; + uint8_t _vartype = EOF_VARTYPE; }; Script main_script; @@ -1019,236 +927,208 @@ int search_result_index = -1; NodeToken *search_result; NodeToken *tmp_sav; vector _functions; -class Context -{ +class Context { public: - Context() - { - children = NULL; - variables = NULL; - } - void clear() - { - if (variables != NULL) - { - for (NodeToken *nd : *variables) - { - free(nd); - } - variables->clear(); - variables->shrink_to_fit(); - free(variables); - variables = NULL; - } - if (children != NULL) - { - for (int i = 0; i < children->size(); i++) - { - Context *child = children->at(i); - child->clear(); - free(child); - child = NULL; - } - children->clear(); - children->shrink_to_fit(); - free(children); - children = NULL; - } - } - Context *addChild(Context cntx) - { - - cntx.parent = this; - - Context *tmpcntx = new Context(); - memcpy((void *)tmpcntx, (void *)&cntx, sizeof(Context)); - - tmpcntx->children = NULL; - - if (children == NULL) - { - // void *vecttmp = malloc(sizeof(vector)); - children = new vector(); - - // children = vtmp; - } - children->push_back(tmpcntx); - - return tmpcntx; - } - Context *addChild() - { - // cntx.parent = this; - return addChild(Context()); + Context() { + children = NULL; + variables = NULL; + } + void clear() { + if (variables != NULL) { + for (NodeToken *nd : *variables) { + free(nd); + } + variables->clear(); + variables->shrink_to_fit(); + free(variables); + variables = NULL; + } + if (children != NULL) { + for (int i = 0; i < children->size(); i++) { + Context *child = children->at(i); + child->clear(); + free(child); + child = NULL; + } + children->clear(); + children->shrink_to_fit(); + free(children); + children = NULL; + } + } + Context *addChild(Context cntx) { + + cntx.parent = this; + + Context *tmpcntx = new Context(); + memcpy((void *)tmpcntx, (void *)&cntx, sizeof(Context)); + + tmpcntx->children = NULL; + + if (children == NULL) { + // void *vecttmp = malloc(sizeof(vector)); + children = new vector(); + + // children = vtmp; + } + children->push_back(tmpcntx); + + return tmpcntx; + } + Context *addChild() { + // cntx.parent = this; + return addChild(Context()); + } + void addFunction(NodeToken *nd) { + NodeToken *__tmpToken = new NodeToken(); + __tmpToken->type = nd->type; + __tmpToken->textref = nd->textref; + __tmpToken->_vartype = nd->_vartype; + __tmpToken->_nodetype = nd->_nodetype; + __tmpToken->isPointer = nd->isPointer; + __tmpToken->asPointer = nd->asPointer; + __tmpToken->_total_size = nd->_total_size; + __tmpToken->stack_pos = nd->stack_pos; + __tmpToken->target = nd->target; + __tmpToken->parent = NULL; + __tmpToken->children = NULL; + // printf("add funcion %s\n", nd->getTokenText()); + _functions.push_back(__tmpToken); + // printf("duplicate add funcion %s\n", nd->getTokenText()); + (_functions.back())->copyChildren(nd); + // printf("end add funcion %s\n", nd->getTokenText()); + } + void addVariable(NodeToken nd) { + __tmpToken = new NodeToken(); + __tmpToken->type = nd.type; + __tmpToken->textref = nd.textref; + __tmpToken->_vartype = nd._vartype; + __tmpToken->_nodetype = nd._nodetype; + __tmpToken->isPointer = nd.isPointer; + __tmpToken->asPointer = nd.asPointer; + __tmpToken->_total_size = nd._total_size; + __tmpToken->stack_pos = nd.stack_pos; + __tmpToken->target = nd.target; + //__tmpToken->parent=this; + __tmpToken->children = NULL; + if (variables == NULL) { + variables = new vector(); + } + variables->push_back(__tmpToken); + // return tmpToken; + } + bool findCandidate(char *str) { + // NodeTo + + search_result = NULL; + char *tocmp; + if (str == NULL) + return false; + + for (vector::iterator it = _functions.begin(); + it != _functions.end(); ++it) { + + if (strstr((*it)->getTokenText(), str) == (*it)->getTokenText()) { + return true; + } } - void addFunction(NodeToken *nd) - { - NodeToken *__tmpToken = new NodeToken(); - __tmpToken->type = nd->type; - __tmpToken->textref = nd->textref; - __tmpToken->_vartype = nd->_vartype; - __tmpToken->_nodetype = nd->_nodetype; - __tmpToken->isPointer = nd->isPointer; - __tmpToken->asPointer = nd->asPointer; - __tmpToken->_total_size = nd->_total_size; - __tmpToken->stack_pos = nd->stack_pos; - __tmpToken->target = nd->target; - __tmpToken->parent = NULL; - __tmpToken->children = NULL; - // printf("add funcion %s\n", nd->getTokenText()); - _functions.push_back(__tmpToken); - // printf("duplicate add funcion %s\n", nd->getTokenText()); - (_functions.back())->copyChildren(nd); - // printf("end add funcion %s\n", nd->getTokenText()); - } - void addVariable(NodeToken nd) - { - __tmpToken = new NodeToken(); - __tmpToken->type = nd.type; - __tmpToken->textref = nd.textref; - __tmpToken->_vartype = nd._vartype; - __tmpToken->_nodetype = nd._nodetype; - __tmpToken->isPointer = nd.isPointer; - __tmpToken->asPointer = nd.asPointer; - __tmpToken->_total_size = nd._total_size; - __tmpToken->stack_pos = nd.stack_pos; - __tmpToken->target = nd.target; - //__tmpToken->parent=this; - __tmpToken->children = NULL; - if (variables == NULL) - { - variables = new vector(); - } - variables->push_back(__tmpToken); - // return tmpToken; - } - bool findCandidate(char *str) - { - // NodeTo - - search_result = NULL; - char *tocmp; - if (str == NULL) - return false; - for (vector::iterator it = _functions.begin(); it != _functions.end(); ++it) - { - - if (strstr((*it)->getTokenText(), str) == (*it)->getTokenText()) - { - return true; - } - } + /* + if (parent != NULL) + { + return parent->findFunction(t); + } + */ + // search_result = NULL; + return false; + } + void findFunction(Token *t) { + // NodeTo + search_result_index = -1; + search_result = NULL; + char *tocmp; + if (t->getText() == NULL) + return; + int tmp_index = 0; + for (vector::iterator it = _functions.begin(); + it != _functions.end(); ++it) { + + if (strstr((*it)->getTokenText(), "Args") != NULL) { + int l = strstr((*it)->getTokenText(), "Args") - (*it)->getTokenText(); + if (l > 0) + l--; + if (strncmp((*it)->getTokenText(), t->getText(), l) == 0) { + search_result = *it; + search_result_index = tmp_index; + return; + } + } else { + + if (strcmp((*it)->getTokenText(), t->getText()) == 0) { + search_result = *it; + search_result_index = tmp_index; + return; + } + } + tmp_index++; + } + + // looking in the external + + search_result = NULL; + return; + } + void findVariable(char *t, bool isCreation) { + search_result = NULL; - /* - if (parent != NULL) - { - return parent->findFunction(t); - } - */ - // search_result = NULL; - return false; - } - void findFunction(Token *t) - { - // NodeTo - search_result_index = -1; - search_result = NULL; - char *tocmp; - if (t->getText() == NULL) + if (t == NULL) + return; + // //printf("lokking for variable |%s| dans %s already %d variables defined + // \n", t->text.c_str(),name.c_str(),variables.size()); + if (variables != NULL) { + for (vector::iterator it = variables->begin(); + it != variables->end(); ++it) { + + if ((*it)->getTokenText() != NULL) { + if (strcmp((*it)->getTokenText(), t) == 0) { + search_result = *it; return; - int tmp_index = 0; - for (vector::iterator it = _functions.begin(); it != _functions.end(); ++it) - { - - if (strstr((*it)->getTokenText(), "Args") != NULL) - { - int l = strstr((*it)->getTokenText(), "Args") - (*it)->getTokenText(); - if (l > 0) - l--; - if (strncmp((*it)->getTokenText(), t->getText(), l) == 0) - { - search_result = *it; - search_result_index = tmp_index; - return; - } - } - else - { - - if (strcmp((*it)->getTokenText(), t->getText()) == 0) - { - search_result = *it; - search_result_index = tmp_index; - return; - } + } + } + } + } + if (!isCreation) { + Context *c_cntx = this->parent; + while (c_cntx != NULL) { + // ////printf("lokking for variable |%s| dans %s %d branches\n", + // t->text.c_str(),c_cntx->name.c_str(),c_cntx->variables.size()); + if (c_cntx->variables != NULL) { + for (vector::iterator it = c_cntx->variables->begin(); + it != c_cntx->variables->end(); ++it) { + // ////printf("is equalt to |%s|\n", (*it)._token->text.c_str()); + if ((*it)->getTokenText() != NULL) { + // //////printf("is equalt to |%s|\n", + // (*it)._token->text.c_str()); + if (strcmp((*it)->getTokenText(), t) == 0) { + search_result = *it; + return; + } } - tmp_index++; + } } - - // looking in the external - - search_result = NULL; - return; + c_cntx = c_cntx->parent; + } } - void findVariable(char *t, bool isCreation) - { - search_result = NULL; - - if (t == NULL) - return; - // //printf("lokking for variable |%s| dans %s already %d variables defined \n", t->text.c_str(),name.c_str(),variables.size()); - if (variables != NULL) - { - for (vector::iterator it = variables->begin(); it != variables->end(); ++it) - { - - if ((*it)->getTokenText() != NULL) - { - if (strcmp((*it)->getTokenText(), t) == 0) - { - search_result = *it; - return; - } - } - } - } - if (!isCreation) - { - Context *c_cntx = this->parent; - while (c_cntx != NULL) - { - // ////printf("lokking for variable |%s| dans %s %d branches\n", t->text.c_str(),c_cntx->name.c_str(),c_cntx->variables.size()); - if (c_cntx->variables != NULL) - { - for (vector::iterator it = c_cntx->variables->begin(); it != c_cntx->variables->end(); ++it) - { - // ////printf("is equalt to |%s|\n", (*it)._token->text.c_str()); - if ((*it)->getTokenText() != NULL) - { - // //////printf("is equalt to |%s|\n", (*it)._token->text.c_str()); - if (strcmp((*it)->getTokenText(), t) == 0) - { - search_result = *it; - return; - } - } - } - } - c_cntx = c_cntx->parent; - } - } - search_result = NULL; - return; - } - void findVariable(Token *t, bool isCreation) - { - findVariable(t->getText(), isCreation); - } - Context *parent = NULL; - vector *children; - vector *variables; + search_result = NULL; + return; + } + void findVariable(Token *t, bool isCreation) { + findVariable(t->getText(), isCreation); + } + Context *parent = NULL; + vector *children; + vector *variables; }; Context main_context; Context *current_cntx; @@ -1265,4557 +1145,4354 @@ vector _node_token_stack; void copyPrty(NodeToken *from, NodeToken *to) { - if (!to->isPointer) - to->isPointer = from->isPointer; - to->stack_pos = from->stack_pos; - to->_vartype = from->_vartype; - to->type = from->type; - // to->target=from->target; - to->_total_size = to->_total_size * from->getVarType()->_varSize; + if (!to->isPointer) + to->isPointer = from->isPointer; + to->stack_pos = from->stack_pos; + to->_vartype = from->_vartype; + to->type = from->type; + // to->target=from->target; + to->_total_size = to->_total_size * from->getVarType()->_varSize; +} +varTypeEnum findfloat(NodeToken *nd) { + if (nd->_vartype == __float__) { + return __float__; + } else if (nd->_nodetype == globalVariableNode) { + return __none__; + } else { + if (nd->children_size() > 0) { + for (NodeToken *child : *nd->children) { + if (findfloat(child) == __float__) { + return __float__; + } + } + return __none__; + } else { + return __none__; + } + } +} +varTypeEnum finduint32_t(NodeToken *nd) { + if (nd->_vartype == __uint32_t__) { + return __uint32_t__; + } else { + if (nd->children_size() > 0) { + for (NodeToken *child : *nd->children) { + if (findfloat(child) == __uint32_t__) { + return __uint32_t__; + } + } + return __none__; + } else { + return __none__; + } + } } -varTypeEnum findfloat(NodeToken *nd) +void createNodeVariable(Token *_var, bool isStore) + { - if (nd->_vartype == __float__) - { - return __float__; - } - else if (nd->_nodetype == globalVariableNode) - { - return __none__; - } - else - { - if (nd->children_size() > 0) - { - for (NodeToken *child : *nd->children) - { - if (findfloat(child) == __float__) - { - return __float__; - } - } - return __none__; - } - else - { - return __none__; - } - } + // //printf("***************create cariavbla %d %s\n", isStore + // ,nd->_token->text.c_str()); NodeToken var = NodeToken(_var); + // //printf("%s %d\n",_var->text.c_str(),_asPointer); + + NodeToken v = NodeToken(_var); + switch (search_result->getNodeTokenType()) { + case extGlobalVariableNode: { + if (isStore) { + // NodeStoreExtGlobalVariable v = NodeStoreExtGlobalVariable(var); + // NodeStoreExtGlobalVariable v = NodeStoreExtGlobalVariable(_var); + v._nodetype = (int)storeExtGlocalVariableNode; + if (extvariablestore.size() > 0) { + if (extvariablestore.compare(string(v.getTokenText())) != 0) { + nbofextern++; + } + } else { + extvariablestore = string(v.getTokenText()); + + nbofextern++; + } + + // current_node->asPointer=asPointer; + // return; + } else { + // NodeExtGlobalVariable v = NodeExtGlobalVariable(_var); + v._nodetype = (int)extGlobalVariableNode; + + // current_node->asPointer=asPointer; + // return; + } + } break; + case defExtGlobalVariableNode: { + if (isStore) { + // NodeStoreExtGlobalVariable v = NodeStoreExtGlobalVariable(_var); + v._nodetype = (int)storeExtGlocalVariableNode; + if (extvariablestore.size() > 0) { + if (extvariablestore.compare(string(v.getTokenText())) != 0) { + nbofextern++; + } + } else { + extvariablestore = string(v.getTokenText()); + // printf("%s\n", v.getTokenText()); + nbofextern++; + } + // current_node->asPointer=asPointer; + // return; + } else { + // NodeExtGlobalVariable v = NodeExtGlobalVariable(_var); + v._nodetype = (int)extGlobalVariableNode; + // current_node->asPointer=asPointer; + // return; + } + } break; + case defLocalVariableNodeAsRegister: { + if (isStore == true) { + // NodeStoreLocalVariable v = NodeStoreLocalVariable(_var); + v._nodetype = storeLocalVariableNodeAsRegister; + v.target = search_result->target; + // asPointer=false; + // return; + } else { + // NodeLocalVariable v = NodeLocalVariable(_var); + v._nodetype = (int)localVariableNodeAsRegister; + v.target = search_result->target; + // current_node->asPointer=asPointer; + // return; + } + } break; + case localVariableNodeAsRegister: { + if (isStore == true) { + // NodeStoreLocalVariable v = NodeStoreLocalVariable(_var); + v._nodetype = storeLocalVariableNodeAsRegister; + v.target = search_result->target; + // current_node->asPointer=asPointer; + // asPointer=false; + // return; + } else { + // NodeLocalVariable v = NodeLocalVariable(_var); + v._nodetype = (int)localVariableNodeAsRegister; + v.target = search_result->target; + // current_node->asPointer=asPointer; + // return; + } + } break; + case defLocalVariableNode: { + if (isStore == true) { + // NodeStoreLocalVariable v = NodeStoreLocalVariable(_var); + v._nodetype = (int)storeLocalVariableNode; + // current_node->asPointer=asPointer; + // asPointer=false; + // return; + } else { + // NodeLocalVariable v = NodeLocalVariable(_var); + v._nodetype = localVariableNode; + // current_node->asPointer=asPointer; + // return; + } + } break; + case localVariableNode: { + if (isStore == true) { + // NodeStoreLocalVariable v = NodeStoreLocalVariable(_var); + + v._nodetype = storeLocalVariableNode; + // current_node->asPointer=asPointer; + // return; + } else { + // NodeLocalVariable v = NodeLocalVariable(_var); + v._nodetype = (int)localVariableNode; + // current_node->asPointer=asPointer; + // § return; + } + } break; + case defGlobalVariableNode: { + + if (isStore) { + // NodeStoreGlobalVariable v = NodeStoreGlobalVariable(_var); + v._nodetype = (int)storeGlobalVariableNode; + // v.addTargetText(search_result->getTargetText()); + // current_node->asPointer=asPointer; + // return; + } else { + // NodeGlobalVariable v = NodeGlobalVariable(_var); + v._nodetype = (int)globalVariableNode; + // current_node->asPointer=asPointer; + // return; + } + } break; + case globalVariableNode: { + if (isStore) { + // NodeStoreGlobalVariable v = NodeStoreGlobalVariable(_var); + v._nodetype = (int)storeGlobalVariableNode; + // v.addTargetText(search_result->getTargetText()); + // current_node->asPointer=asPointer; + // return; + } else { + // NodeGlobalVariable v = NodeGlobalVariable(_var); + v._nodetype = (int)globalVariableNode; + // current_node->asPointer=asPointer; + // return; + } + } break; + default: { + if (isStore == true) { + // NodeStoreLocalVariable v = NodeStoreLocalVariable(_var); + v._nodetype = storeLocalVariableNode; + // current_node->asPointer=asPointer; + // return; + } else { + // NodeLocalVariable v = NodeLocalVariable(_var); + v._nodetype = localVariableNode; + // current_node->asPointer=asPointer; + // § return; + } + } break; + } + if (search_result->getTargetText()[0] == '@') { + v.addTargetText(search_result->getTargetText()); + } + copyPrty(search_result, &v); + v.asPointer = _asPointer; + if (search_result->asPointer) { + v.asPointer = true; + } + if (isPointer) + v.isPointer = isPointer; + current_node = current_node->addChild(v); } -varTypeEnum finduint32_t(NodeToken *nd) + +NodeToken createNodeLocalVariableForCreation(NodeToken var, NodeToken nd) + { - if (nd->_vartype == __uint32_t__) - { - return __uint32_t__; - } - else - { - if (nd->children_size() > 0) - { - for (NodeToken *child : *nd->children) - { - if (findfloat(child) == __uint32_t__) - { - return __uint32_t__; - } - } - return __none__; - } - else - { - return __none__; - } - } + switch (var._nodetype) { + case defGlobalVariableNode: { + NodeToken v = NodeToken(var, defGlobalVariableNode); + copyPrty(&nd, &v); + return v; + } break; + case defLocalVariableNode: { + + NodeToken v = NodeToken(var, defLocalVariableNode); + if (_is_variable_as_register.get()) { + v._nodetype = defLocalVariableNodeAsRegister; + v.target = _for_depth_reg.get(); + _for_depth_reg.increase(); + _is_variable_as_register.set(false); + } + copyPrty(&nd, &v); + return v; + } break; + break; + default: { + + copyPrty(&nd, &var); + NodeToken v = NodeToken(var, defLocalVariableNode); + if (_is_variable_as_register.get()) { + v._nodetype = defLocalVariableNodeAsRegister; + v.target = _for_depth_reg.get(); + _for_depth_reg.increase(); + _is_variable_as_register.set(false); + } + // copyPrty(&nd, &v); + // NodeDefLocalVariable v = NodeDefLocalVariable(var); + + return v; + } break; + } } -void createNodeVariable(Token *_var, bool isStore) -{ - // //printf("***************create cariavbla %d %s\n", isStore ,nd->_token->text.c_str()); - // NodeToken var = NodeToken(_var); - // //printf("%s %d\n",_var->text.c_str(),_asPointer); +string findForWhile() { + string res = "__"; + NodeToken *p = current_node; + while (p->_nodetype != forNode and p->_nodetype != whileNode) { + p = p->parent; + if (p == NULL) + break; + } + if (p != NULL) { + // //printf("jkd\n"); + res = string(p->getTargetText()); + } + return res; +} - NodeToken v = NodeToken(_var); - switch (search_result->getNodeTokenType()) - { - case extGlobalVariableNode: - { - if (isStore) - { - // NodeStoreExtGlobalVariable v = NodeStoreExtGlobalVariable(var); - // NodeStoreExtGlobalVariable v = NodeStoreExtGlobalVariable(_var); - v._nodetype = (int)storeExtGlocalVariableNode; - if (extvariablestore.size() > 0) - { - if (extvariablestore.compare(string(v.getTokenText())) != 0) - { - nbofextern++; - } - } - else - { - extvariablestore = string(v.getTokenText()); +void buildParents(NodeToken *__nd) - nbofextern++; - } +{ - // current_node->asPointer=asPointer; - // return; - } - else - { - // NodeExtGlobalVariable v = NodeExtGlobalVariable(_var); - v._nodetype = (int)extGlobalVariableNode; + // return; //new + // printf("klkkmkml %s\r\n",__nd->getTokenText()); + if (__nd->children_size() > 0) { + for (vector::iterator it = __nd->children->begin(); + it != __nd->children->end(); it++) { + + if ((*it)->_nodetype == (int)UnknownNode) { + __nd->children->erase(it); + // printf("on supppirm\r\n"); + } else { + (*it)->parent = __nd; + buildParents(*it); + } + } + } +} - // current_node->asPointer=asPointer; - // return; - } - } +void translateType(varTypeEnum to, varTypeEnum from, int regnum) + +{ + // printf("to:%s from: + // %s\n",varTypeEnumNames[to].c_str(),varTypeEnumNames[from].c_str()); + // if(to==__none__) + // to=__int__; + if (to == __none__ or from == to) + return; + switch (to) { + case __float__: + // bufferText->sp.pop(); + bufferText->addAfterNoDouble( + string_format("float.s f%d,a%d,0", regnum, regnum)); + // bufferText->sp.push(bufferText->get()); break; - case defExtGlobalVariableNode: - { - if (isStore) - { - // NodeStoreExtGlobalVariable v = NodeStoreExtGlobalVariable(_var); - v._nodetype = (int)storeExtGlocalVariableNode; - if (extvariablestore.size() > 0) - { - if (extvariablestore.compare(string(v.getTokenText())) != 0) - { - nbofextern++; - } - } - else - { - extvariablestore = string(v.getTokenText()); - //printf("%s\n", v.getTokenText()); - nbofextern++; - } - // current_node->asPointer=asPointer; - // return; - } - else - { - // NodeExtGlobalVariable v = NodeExtGlobalVariable(_var); - v._nodetype = (int)extGlobalVariableNode; - // current_node->asPointer=asPointer; - // return; - } + case __int__: + switch (from) { + case __float__: + // bufferText->sp.pop(); + bufferText->addAfterNoDouble( + string_format("trunc.s a%d,f%d,0", regnum, regnum)); + // bufferText->sp.push(bufferText->get()); + break; + default: + break; } break; - case defLocalVariableNodeAsRegister: - { - if (isStore == true) - { - // NodeStoreLocalVariable v = NodeStoreLocalVariable(_var); - v._nodetype = storeLocalVariableNodeAsRegister; - v.target = search_result->target; - // asPointer=false; - // return; - } - else - { - // NodeLocalVariable v = NodeLocalVariable(_var); - v._nodetype = (int)localVariableNodeAsRegister; - v.target = search_result->target; - // current_node->asPointer=asPointer; - // return; - } + case __uint8_t__: + switch (from) { + case __float__: + // bufferText->sp.pop(); + bufferText->addAfterNoDouble( + string_format("trunc.s a%d,f%d,0", regnum, regnum)); + // bufferText->sp.push(bufferText->get()); + break; + default: + break; } break; - case localVariableNodeAsRegister: - { - if (isStore == true) - { - // NodeStoreLocalVariable v = NodeStoreLocalVariable(_var); - v._nodetype = storeLocalVariableNodeAsRegister; - v.target = search_result->target; - // current_node->asPointer=asPointer; - // asPointer=false; - // return; - } - else - { - // NodeLocalVariable v = NodeLocalVariable(_var); - v._nodetype = (int)localVariableNodeAsRegister; - v.target = search_result->target; - // current_node->asPointer=asPointer; - // return; - } + + default: + switch (from) { + case __float__: + // bufferText->sp.pop(); + bufferText->addAfterNoDouble( + string_format("trunc.s a%d,f%d,0", regnum, regnum)); + // bufferText->sp.push(bufferText->get()); + break; + default: + break; } break; - case defLocalVariableNode: - { - if (isStore == true) - { - // NodeStoreLocalVariable v = NodeStoreLocalVariable(_var); - v._nodetype = (int)storeLocalVariableNode; - // current_node->asPointer=asPointer; - // asPointer=false; - // return; - } - else - { - // NodeLocalVariable v = NodeLocalVariable(_var); - v._nodetype = localVariableNode; - // current_node->asPointer=asPointer; - // return; - } - } break; - case localVariableNode: - { - if (isStore == true) - { - // NodeStoreLocalVariable v = NodeStoreLocalVariable(_var); + } +} - v._nodetype = storeLocalVariableNode; - // current_node->asPointer=asPointer; - // return; - } - else - { - // NodeLocalVariable v = NodeLocalVariable(_var); - v._nodetype = (int)localVariableNode; - // current_node->asPointer=asPointer; - // § return; - } +string _data_sav; +Stack globalType = Stack(__int__); + +void _visittypeNode(NodeToken *nd) {} +void _visitstoreLocalVariableNodeAsRegister(NodeToken *nd) { + if (nd->getVarType()->_varType == __float__) { + bufferText->addAfter( + bufferText->sp.pop(), + string_format("rfr a%d,f%d", nd->target, register_numl.get())); + } else + bufferText->addAfter( + bufferText->sp.pop(), + string_format("mov a%d,a%d", nd->target, register_numl.get())); +} +void _visitlocalVariableNodeAsRegister(NodeToken *nd) { + if (nd->getVarType()->_varType == __float__) + bufferText->addAfter( + string_format("wfr f%d,a%d", register_numl.get(), nd->target)); + else + bufferText->addAfter( + string_format("movr a%d,a%d", register_numl.get(), nd->target)); + bufferText->sp.push(bufferText->get()); + register_numl.decrease(); +} +void _visitnumberNode(NodeToken *nd) { + // //printf("in number\n"); + + // register_numl.duplicate(); + if (nd->children_size() > 0) { + for (int i = 0; i < nd->children_size(); i++) { + register_numl.duplicate(); + nd->getChildAtPos(i)->visitNode(); + // register_numl.pop(); + // bufferText->sp.push(bufferText->get()); + register_numl.pop(); + } + } else { + if (nd->getVarType()->_varType == __float__) { + float __f = 0; + sscanf(nd->getTokenText(), "%f", &__f); + header.addAfter(string_format("@_%s_%d:", "local_var", local_var_num)); + // local_var_num++; + string val = ".bytes 4"; + uint32_t __num = (uint32_t)(*((uint32_t *)&__f)); + // Serial.//printf(" on a float %4x\r\n",__num); + uint8_t c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + header.addAfter(val); + // point_regnum++; + bufferText->addAfter(string_format("l32r a%d,@_%s_%d", 9, "local_var", + local_var_num)); // YBA 25-02-2025 + bufferText->addAfter(string_format("lsi f%d,a%d,0", register_numl.get(), + 9)); // YBA 25-02-2025 + bufferText->sp.push(bufferText->get()); + // point_regnum--; + local_var_num++; + register_numl.decrease(); + } else if (string(nd->getTokenText()).find("x") != string::npos) { + unsigned int __num = 0; + + sscanf(nd->getTokenText(), "%x", &__num); + + if (__num >= 2048) { + header.addAfter(string_format("@_%s_%d:", "local_var", local_var_num)); + string val = ".bytes 4"; + uint8_t c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + header.addAfter(val); + // point_regnum++; + bufferText->addAfter(string_format("l32r a%d,@_%s_%d", 9, "local_var", + local_var_num)); // YBA 25-02-2025 + bufferText->addAfter(string_format( + "l32i a%d,a%d,0", register_numl.get(), 9)); // YBA 25-02-2025 + bufferText->sp.push(bufferText->get()); + // point_regnum--; + local_var_num++; + register_numl.decrease(); + } else { + bufferText->addAfter( + string_format("movi a%d,%d", register_numl.get(), + __num)); // nd->_token->text.c_str())); + bufferText->sp.push(bufferText->get()); + register_numl.decrease(); + } + } else { + int __num = 0; + + sscanf(nd->getTokenText(), "%d", &__num); + + if (__num >= 2048 or __num <= -2047) { + header.addAfter(string_format("@_%s_%d:", "local_var", local_var_num)); + string val = ".bytes 4"; + uint8_t c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + c = __num & 0xff; + val = val + " " + string_format("%02x", c); + // val=val+'A'; + header.addAfter(val); + // point_regnum++; + bufferText->addAfter(string_format("l32r a%d,@_%s_%d", 9, "local_var", + local_var_num)); // YBA 25-02-1975 + bufferText->addAfter(string_format( + "l32i a%d,a%d,0", register_numl.get(), 9)); // YBA 25-02-1975 + bufferText->sp.push(bufferText->get()); + // point_regnum--; + local_var_num++; + register_numl.decrease(); + } else { + // printf("Pour %s\n",bufferText->current().c_str()); + bufferText->addAfter(string_format( + "movi a%d,%d", register_numl.get(), + __num)); // nd->_token->text.c_str())); + // printf("on a %s\n",bufferText->current().c_str()); + bufferText->sp.push(bufferText->get()); + register_numl.decrease(); + } } + } +} + +void _visitbinOpNode(NodeToken *nd) { + + // printf("bin operator\n"); + // register_numl.displaystack(); + register_numl.duplicate(); + // register_numr.duplicate(); + // if (nd->getChildAtPos(0)->visitNode != NULL) + nd->getChildAtPos(0)->visitNode(); + // printf("ddd %s\n",bufferText->back().c_str()); + // register_numl.displaystack(); + register_numl.duplicate(); + // register_numr.duplicate(); + if (nd->type != TokenPower) { + // if (nd->getChildAtPos(2)->visitNode != NULL) + nd->getChildAtPos(1)->visitNode(); + } else { + bufferText->addAfter(" "); + bufferText->sp.push(bufferText->get()); + } + // register_numr.pop(); + register_numl.swap(); + register_numr.push(register_numl.pop()); + register_numl.swap(); + // register_numl.displaystack(); + // if (nd->getChildAtPos(1)->visitNode != NULL) + // nd->getChildAtPos(1)->visitNode(); + _visitoperatorNode(nd); + register_numl.pop(); + if (nd->type == TokenAddition || nd->type == TokenSubstraction) { + register_numl.increase(); + } + // nex + if (nd->type == TokenSlash || nd->type == TokenStar) { + register_numl.pop(); + register_numl.push(register_numr.get()); + } + // end new + bufferText->sp.pop(); + bufferText->sp.pop(); + bufferText->sp.push(bufferText->get()); + // register_numl.pop(); + register_numr.pop(); +} +void _visitunitaryOpNode(NodeToken *nd) { + // register_numl.displaystack(); + register_numl.duplicate(); + // register_numr.duplicate(); + if (nd->type == TokenUppersand) { + // //printf("node UNitary operator2\n"); + // nd->getChildAtPos(0)->asPointer = true; + // addTokenSup(nd); + // nd->_token->_varType = __none__; + nd->_vartype = (int)__none__; + nd->isPointer = true; + nd->getChildAtPos(0)->visitNode(); + register_numl.pop(); + // bufferText->sp.pop(); + bufferText->sp.push(bufferText->get()); + return; + } + if (nd->type == TokenNot) { + + nd->getChildAtPos(0)->visitNode(); + register_numl.pop(); + bufferText->addAfter("movi a12,0"); + bufferText->addAfter(string_format("bnez a%d,label_not_%d", + register_numl.get(), for_if_num)); + bufferText->addAfter("movi a12,1"); + bufferText->addAfter(string_format("label_not_%d:", for_if_num)); + bufferText->addAfter(string_format("mov a%d,a12", register_numl.get())); + bufferText->sp.push(bufferText->get()); + register_numl.decrease(); + return; + } else if (nd->type == TokenSubstraction) { + nd->type = TokenNegation; + + nd->getChildAtPos(0)->visitNode(); + // register_numl.displaystack(); + register_numl.pop(); + // bufferText->sp.push(bufferText->get()); + + // nd->getChildAtPos(1)->visitNode(); + _visitoperatorNode(nd); + // register_numl.pop(); + register_numl.decrease(); + } else { + // if (nd->getChildAtPos(0)->visitNode != NULL) + nd->getChildAtPos(0)->visitNode(); + // register_numl.displaystack(); + register_numl.pop(); + _visitoperatorNode(nd); + // nd->getChildAtPos(1)->visitNode(); + register_numl.decrease(); + bufferText->sp.push(bufferText->get()); + } +} + +void _visitoperatorNode(NodeToken *nd) { + // printf("operator %s\n",tokenNames[nd->type].c_str()); + // register_numl.pop(); + // register_numl.displaystack(); + varTypeEnum l = __none__; + varTypeEnum r = __none__; + // //printf("kk\n"); + + if (nd->getChildAtPos(0)->getVarType() != NULL) { + l = nd->getChildAtPos(0)->getVarType()->_varType; + } + + // //printf("kk2 :%d\n",nd->parent->children_size()); + if (nd->children_size() >= 2) { + if (nd->getChildAtPos(1) == NULL) { + // printf("WFT %d + // %s\n",nd->parent->children_size(),nodeTypeNames[nd->parent->_nodetype].c_str()); + } + + if (nd->getChildAtPos(1)->getVarType() != NULL) { + // //printf("kk32 %s\n",nodeTypeNames[nd->parent->_nodetype].c_str()); + r = nd->getChildAtPos(1)->getVarType()->_varType; + } + } + // //printf("kk3\n"); + bool ff = false; + if (nd->getVarType() == NULL) { + // addTokenSup(nd->parent); + if (globalType.get() == __float__) { + // nd->parent->_token->_varType = __float__; + nd->_vartype = (int)__float__; + } else { + // nd->parent->_token->_varType = __none__; + nd->_vartype = __none__; + } + } + + // //printf("kk4\n"); + if (globalType.get() == __float__) { + ff = true; + nd->_vartype = __float__; + } + // //printf("kk5\n"); + asmInstruction asmInstr; + if (nd->children_size() >= 2) + translateType(globalType.get(), r, register_numr.get()); + translateType(globalType.get(), l, register_numl.get()); + switch (nd->type) { + case TokenAddition: { + if (ff) { + asmInstr = adds; + } else { + asmInstr = add; + } + string new_line = string_format( + "%s %s%d,%s%d,%s%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), register_numl.get(), + getRegType(asmInstr, 2).c_str(), register_numr.get()); + + string _last = bufferText->current(); + ; // bufferText->back(); + string tocmp = string_format("movi a%d,", register_numr.get()); + // printf("to found %s\r\n",_last,tocmp); + if (_last.compare(0, tocmp.size(), tocmp) == 0) { + // printf("to found %s\r\n",_last,tocmp); + int a, b; + sscanf(_last.c_str(), "movi a%d,%d", &a, &b); + if (b >= -128 and b <= 127) { + // bufferText->pop(); + bufferText->blankCurrent(); + bufferText->addAfter(string_format( + "addi a%d,a%d,%d", register_numl.get(), register_numl.get(), b)); + } else { + bufferText->addAfter(new_line); + } + } else { + bufferText->addAfter(new_line); + } + + // return; + } break; + case TokenShiftLeft: + // bufferText->addAfter("movi a8,32"); + // bufferText->addAfter(string_format("sub + // a%d,a8,a%d",register_numr.get(),register_numr.get()).c_str()); + bufferText->addAfter(string_format("ssl a%d", register_numr.get()).c_str()); + bufferText->addAfter( + string_format("sll a%d,a%d", register_numl.get(), register_numl.get()) + .c_str()); break; - case defGlobalVariableNode: - { + case TokenShiftRight: + // bufferText->addAfter("movi a8,32"); + // bufferText->addAfter(string_format("sub + // a%d,a8,a%d",register_numr.get(),register_numr.get()).c_str()); + bufferText->addAfter( + string_format("wsr a%d,3", register_numr.get()).c_str()); + bufferText->addAfter( + string_format("srl a%d,a%d", register_numl.get(), register_numl.get()) + .c_str()); + break; + case TokenSubstraction: { + if (ff) { + asmInstr = subs; + } else { + asmInstr = sub; + } + string new_line = string_format( + "%s %s%d,%s%d,%s%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), register_numl.get(), + getRegType(asmInstr, 2).c_str(), register_numr.get()); + + string _last = bufferText->current(); + string tocmp = string_format("movi a%d,", register_numr.get()); + // printf("to found %s %s\r\n", bufferText->current().c_str(), + // tocmp.c_str()); + if (_last.compare(0, tocmp.size(), tocmp) == 0) { + // printf("to found %s\r\n",_last,tocmp); + int a, b; + sscanf(_last.c_str(), "movi a%d,%d", &a, &b); + if (b >= -128 and b <= 127) { + // bufferText->pop(); + bufferText->blankCurrent(); + bufferText->addAfter(string_format( + "addi a%d,a%d,-%d", register_numl.get(), register_numl.get(), b)); + } else { + bufferText->addAfter(new_line); + } + } else { + bufferText->addAfter(new_line); + } + // bufferText->addAfter(string_format("sub a%d,a%d,a%d", + // register_numl.get(), register_numl.get(), register_numr.get())); return; + } break; + case TokenSlash: + // bufferText->addAfter(string_format("quou a%d,a%d,a%d", + // register_numl.get(), register_numl.get(), register_numr.get())); + if (ff) { + bufferText->addAfter(string_format("mov.s f1,f%d", register_numl.get())); + bufferText->addAfter(string_format("mov.s f2,f%d", register_numr.get())); + bufferText->addAfter("call8 @___div(d|d)"); + bufferText->addAfter(string_format("mov.s f%d,f0", register_numl.get())); + addfloatdivision = true; + } else { + // asmInstr = quou; + if (l == __uint32_t__ || r == __uint32_t__) + asmInstr = quou; + else + asmInstr = quos; + bufferText->addAfter(string_format( + "%s %s%d,%s%d,%s%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), register_numl.get(), + getRegType(asmInstr, 2).c_str(), register_numr.get())); + } + // return; + break; + case TokenStar: + if (ff) { + asmInstr = muls; + } else { + asmInstr = mull; + } + bufferText->addAfter(string_format( + "%s %s%d,%s%d,%s%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), register_numl.get(), + getRegType(asmInstr, 2).c_str(), register_numr.get())); + // bufferText->addAfter(string_format("mull a%d,a%d,a%d", + // register_numl.get(), register_numl.get(), register_numr.get())); return; + break; + case TokenPlusPlus: + if (nd->getChildAtPos(0)->isPointer && + nd->getChildAtPos(0)->children_size() == 0) { + bufferText->addAfter(string_format( + "addi a%d,a%d,%d", register_numl.get(), register_numl.get(), + nd->getChildAtPos(0)->getVarType()->total_size)); + } else { + bufferText->addAfter(string_format("addi a%d,a%d,1", register_numl.get(), + register_numl.get())); + } + // return; + break; + case TokenMinusMinus: + if (nd->getChildAtPos(0)->isPointer && + nd->getChildAtPos(0)->children_size() == 0) { + bufferText->addAfter(string_format( + "addi a%d,a%d,-%d", register_numl.get(), register_numl.get(), + nd->getChildAtPos(0)->getVarType()->total_size)); + } else { + bufferText->addAfter(string_format("addi a%d,a%d,-1", register_numl.get(), + register_numl.get())); + } + // return; + break; + case TokenModulo: + bufferText->addAfter(string_format("remu a%d,a%d,a%d", register_numl.get(), + register_numl.get(), + register_numr.get())); + // return; + break; + case TokenPower: { + // comment supprimer ce qu'il y a avant + int __num = 0; + if (nd->getChildAtPos(1)->_nodetype == numberNode) { + + sscanf(nd->getChildAtPos(1)->getTokenText(), "%d", &__num); + if (__num > 2) { + if (ff) { + bufferText->blankCurrent(); + bufferText->addAfter( + string_format("mov.s f10,f%d", register_numl.get())); + asmInstr = muls; + } else { + bufferText->addAfter( + string_format("mov a10,a%d", register_numl.get())); + asmInstr = mull; + } + for (int k = 1; k < __num; k++) { + bufferText->addAfter(string_format( + "%s %s%d,%s%d,%s10", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), register_numl.get(), + getRegType(asmInstr, 2).c_str())); + } + } else { + if (ff) { + bufferText->blankCurrent(); + asmInstr = muls; + } else { + asmInstr = mull; + } + bufferText->addAfter(string_format( + "%s %s%d,%s%d,%s%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), register_numl.get(), + getRegType(asmInstr, 2).c_str(), register_numl.get())); + } + } + } break; + case TokenKeywordAnd: + bufferText->addAfter(string_format("and a%d,a%d,a%d", register_numl.get(), + register_numl.get(), + register_numr.get())); + break; + case TokenKeywordOr: + bufferText->addAfter(string_format("or a%d,a%d,a%d", register_numl.get(), + register_numl.get(), + register_numr.get())); + break; + case TokenKeywordFabs: - if (isStore) - { - // NodeStoreGlobalVariable v = NodeStoreGlobalVariable(_var); - v._nodetype = (int)storeGlobalVariableNode; - // v.addTargetText(search_result->getTargetText()); - // current_node->asPointer=asPointer; - // return; - } - else - { - // NodeGlobalVariable v = NodeGlobalVariable(_var); - v._nodetype = (int)globalVariableNode; - // current_node->asPointer=asPointer; - // return; - } - } + bufferText->addAfter(string_format("abs.s f%d,f%d", register_numl.get(), + register_numl.get())); break; - case globalVariableNode: - { - if (isStore) - { - // NodeStoreGlobalVariable v = NodeStoreGlobalVariable(_var); - v._nodetype = (int)storeGlobalVariableNode; - // v.addTargetText(search_result->getTargetText()); - // current_node->asPointer=asPointer; - // return; - } - else - { - // NodeGlobalVariable v = NodeGlobalVariable(_var); - v._nodetype = (int)globalVariableNode; - // current_node->asPointer=asPointer; - // return; - } + case TokenKeywordAbs: + if (ff) { + bufferText->addAfter(string_format("abs.s f%d,f%d", register_numl.get(), + register_numl.get())); + } else { + bufferText->addAfter(string_format("abs a%d,a%d", register_numl.get(), + register_numl.get())); } break; - default: - { - if (isStore == true) - { - // NodeStoreLocalVariable v = NodeStoreLocalVariable(_var); - v._nodetype = storeLocalVariableNode; - // current_node->asPointer=asPointer; - // return; - } - else - { - // NodeLocalVariable v = NodeLocalVariable(_var); - v._nodetype = localVariableNode; - // current_node->asPointer=asPointer; - // § return; - } - } + case TokenNegation: + if (ff) { + asmInstr = negs; + } else { + asmInstr = neg; + } + bufferText->addAfter( + string_format("%s %s%d,%s%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), register_numl.get())); + bufferText->sp.pop(); + bufferText->sp.push(bufferText->get()); + // return; + default: + // return; break; - } - if (search_result->getTargetText()[0] == '@') - { - v.addTargetText(search_result->getTargetText()); - } - copyPrty(search_result, &v); - v.asPointer = _asPointer; - if (search_result->asPointer) - { - v.asPointer = true; - } - if (isPointer) - v.isPointer = isPointer; - current_node = current_node->addChild(v); + } } +// void _visitProgram(NodeToken *nd) {} +void _visitglobalVariableNode(NodeToken *nd) { -NodeToken createNodeLocalVariableForCreation(NodeToken var, NodeToken nd) - -{ - switch (var._nodetype) - { - case defGlobalVariableNode: - { - NodeToken v = NodeToken(var, defGlobalVariableNode); - copyPrty(&nd, &v); - return v; + // printf("comopiline glmobalvar %s\n",nd->getTokenText()); + // int r_size = 0; + isInFunction = false; + register_numl.duplicate(); + if (nd->children_size() > 0) { + // int r_size = 0; + vector tile; + int nb = 0; + string sd = string(nd->getTargetText()); + if (sd.compare(0, 1, "@") == 0) { + tile = split(sd, " "); + + sscanf(tile[0].c_str(), "@%d", &nb); + // r_size = stringToInt((char *)tile[1].c_str()); + } + if (nb > 1) { + bufferText->addAfter("movi a10,0"); + } + + for (int i = 0; i < nd->children_size(); i++) { + // globalType.push(__int__); + register_numl.duplicate(); + nd->getChildAtPos(i)->visitNode(); + register_numl.pop(); + translateType(__int__, nd->getChildAtPos(i)->getVarType()->_varType, + register_numl.get()); + if (nd->children_size() > 1) { + if (i < nd->children_size() - 1) { + + for (int h = 1; h < nd->children_size() - i; h++) { + bufferText->addAfter(string_format( + "movi a11,%d", stringToInt((char *)tile[i + 1 + h].c_str()))); + // bufferText->addAfter(string_format("mull a11,a10,a11")); + bufferText->addAfter(string_format( + "mull a%d,a%d,a11", register_numl.get(), register_numl.get())); + } + // if(i>0) + bufferText->addAfter( + string_format("add a10,a10,a%d", register_numl.get())); + } else { + bufferText->addAfter(string_format( + "add a%d,a10,a%d", register_numl.get(), register_numl.get())); + } + } + + // globalType.pop(); + } + } + varType *v = nd->getVarType(); + int start = nd->stack_pos; + // uint8_t regnum = 1; + /* YBA 25-02-2025 + if (nd->asPointer or (nd->isPointer)) // && nd->children_size() == 0)) + point_regnum++; + /*/ + if (nd->isPointer) { + // start = nd->stack_pos; + // regnum = point_regnum; + } + // string body = ""; + // register_numl++; + // bufferText->addAfter(string_format("l32r a%d,%s", point_regnum, + // nd->_token->text.c_str())); + if (nd->isPointer && nd->children_size() > 0) // leds[g]; + { + // f=f+number.f; + if (nd->type == TokenUserDefinedVariableMember or + nd->type == TokenUserDefinedVariableMemberFunction) { + bufferText->addAfter( + string_format("movi a%d,%d", point_regnum, nd->_total_size)); + bufferText->addAfter(string_format("mull a%d,a%d,a%d", + register_numl.get(), + register_numl.get(), point_regnum)); + bufferText->addAfter( + string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); + bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, + point_regnum, register_numl.get())); + } else if (v->total_size > 4) { + // string tmp=content.l->back(); + // content.l->pop_back(); + bufferText->addAfter( + string_format("movi a%d,%d", point_regnum, v->total_size)); + bufferText->addAfter(string_format("mull a%d,a%d,a%d", + register_numl.get(), + register_numl.get(), point_regnum)); + bufferText->addAfter( + string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); + bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, + point_regnum, register_numl.get())); + } else { + bufferText->addAfter( + string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); + for (int i = 0; i < v->total_size; i++) { + bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, + point_regnum, register_numl.get())); + } + } + } else { + bufferText->addAfter( + string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); + } + bufferText->sp.push(bufferText->get()); + if (nd->asPointer) //(&d) + { + bufferText->addAfter( + string_format("mov a%d,a%d", register_numl.get(), point_regnum)); + bufferText->sp.push(bufferText->get()); + } else if ((nd->children_size() > 0 or !nd->isPointer) && + nd->type != + TokenUserDefinedVariableMemberFunction) // leds[h] or h h being + // global) + { + // if (nd->target == EOF_TEXTARRAY) + // { + for (int i = 0; i < v->size; i++) { + // bufferText->addAfter(string_format("%s %s%d,%s%d,%d", + // v->load[i].c_str(), v->reg_name.c_str(), register_numl.get(), + // v->reg_name.c_str(), point_regnum, start)); + asmInstruction asmInstr = v->load[i]; + bufferText->addAfter(string_format( + "%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), point_regnum, start)); + // register_numl--; + start += v->sizes[i]; + bufferText->sp.push(bufferText->get()); } - break; - case defLocalVariableNode: - { + // } + /* + else + { + + int i = findMember(nd->_vartype, string(nd->getTargetText())); + int pos = 0; + v = &_userDefinedTypes[nd->_vartype]; + // printf(" we try to find %s %d\r\n", nd->getTargetText(), i); + if (i > -1) + { + // nd->getVarType() = &_varTypes[v->types[i]]; + // nd->_vartype=(uint8_t)v->types[i]; + start = nd->stack_pos + v->starts[i]; + for (int h = 0; h < i; h++) + { + pos += v->memberSize[h]; + // //printf("pos %d\r\n", pos); + } - NodeToken v = NodeToken(var, defLocalVariableNode); - if (_is_variable_as_register.get()) - { - v._nodetype = defLocalVariableNodeAsRegister; - v.target = _for_depth_reg.get(); - _for_depth_reg.increase(); - _is_variable_as_register.set(false); - } - copyPrty(&nd, &v); - return v; - } - break; - break; - default: - { + for (int l = pos; l <= v->memberSize[i] - 1 + pos; l++) + { + // //printf("start gloabl %d\r\n", start); + bufferText->addAfter(string_format("%s %s%d,%s%d,%d", + asmInstructionsName[v->load[l]].c_str(), getRegType(v->load[l], 0).c_str(), + register_numl.get(), getRegType(v->load[l], 1).c_str(), point_regnum, + start)); start += v->sizes[l]; bufferText->sp.push(bufferText->get()); + } + } + else + { + return; + } + } + */ + // if(v->size==1) + // bufferText->sp.pop(); + } else // s(leds) + { + bufferText->addAfter( + string_format("mov a%d,a%d", register_numl.get(), point_regnum)); + bufferText->sp.push(bufferText->get()); + } + // res.f = f; + // res.header = number.header + h; + // point_regnum++; + register_numl.pop(); + // res.register_numl=register_numl; + // res.register_numr=register_numr; + register_numl.decrease(); + /* YBA 25-02-2025 + if (nd->asPointer or (nd->isPointer)) // && nd->children_size() == 0)) + point_regnum--; + */ + return; +} - copyPrty(&nd, &var); - NodeToken v = NodeToken(var, defLocalVariableNode); - if (_is_variable_as_register.get()) - { - v._nodetype = defLocalVariableNodeAsRegister; - v.target = _for_depth_reg.get(); - _for_depth_reg.increase(); - _is_variable_as_register.set(false); - } - // copyPrty(&nd, &v); - // NodeDefLocalVariable v = NodeDefLocalVariable(var); +void _visitcallConstructorNode(NodeToken *nd) { + int size = nd->_total_size / nd->getVarType()->total_size; + + if (nd->stack_pos > 0) { + bufferText->addAfter(string_format("addi a5,a1,%d", nd->stack_pos)); + } else { + bufferText->addAfter(string_format("l32r a5,@_%s", nd->getTokenText())); + } + if (size > 1) { + bufferText->addAfter("movi a6,0"); + bufferText->addAfter(string_format("loop_label_%d:", for_if_num)); + } + bufferText->addAfter("mov a10,a5"); + // bufferText->addAfter("mov a10,a2"); + bufferText->addAfter(string_format("call8 @_%s._@%s()", + nd->getVarType()->varName.c_str(), + nd->getVarType()->varName.c_str())); + if (size > 1) { + bufferText->addAfter( + string_format("addi a5,a5,%d", nd->getVarType()->total_size)); + bufferText->addAfter("addi a6,a6,1"); + bufferText->addAfter(string_format("movi a7,%d", size)); + + bufferText->addAfter(string_format("bne a7,a6,loop_label_%d", for_if_num)); + for_if_num++; + } +} - return v; +void _visitlocalVariableNode(NodeToken *nd) { + // printf("in lcoall\n"); + + if (nd->asPointer) { + register_numl.duplicate(); + varType *v = nd->getVarType(); + int start = nd->stack_pos; + // //printf("kzlekmze\n"); + // bufferText->addAfter(string_format("l32r a%d,stack", point_regnum)); + + // if( if (nd->isPointer && nd->children_size() > 0)) + + if (nd->type == TokenUserDefinedVariableMember or + nd->type == TokenUserDefinedVariableMemberFunction) { + if (!nd->isPointer) { + bufferText->addAfter(string_format("addi a%d,a1,%d", + register_numl.get(), + start - (int)(start / 1000) * 1000)); + bufferText->addAfter( + string_format("l32i a%d,a%d,%d", register_numl.get(), + register_numl.get(), (start / 1000))); + } else { + // bufferText->addAfter(string_format("l32i a%d,a1,%d", + // register_numl.get(),start-(int)(start/1000)*1000)); + asmInstruction asmInstr = v->load[0]; + // bufferText->addAfter(string_format("%s %s%d,%s%d,%d", + // asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, + // 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), + // register_numl.get(), start/1000)); + bufferText->addAfter(string_format( + "%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), 2, start / 1000)); + translateType(globalType.get(), v->_varType, register_numl.get()); + // bufferText->addAfter(string_format("l16si a%d,a%d,%d", + // register_numl.get(),register_numl.get(),start/1000)); + } + } else { + bufferText->addAfter( + string_format("addi a%d,a1,%d", register_numl.get(), start)); } - break; + register_numl.decrease(); + if (nd->isPointer && nd->children_size() > 0) { + register_numl.duplicate(); + nd->getChildAtPos(0)->visitNode(); + register_numl.pop(); + for (int i = 0; i < v->total_size; i++) { + bufferText->addAfter( + string_format("add a%d,a%d,a%d", register_numl.get() + 1, + register_numl.get() + 1, register_numl.get())); + } + register_numl.increase(); } -} + bufferText->sp.push(bufferText->get()); + register_numl.pop(); + register_numl.decrease(); + // //printf("kzlekmze2\n"); + return; + } + register_numl.duplicate(); + // if (nd->children_size() > 0) + //{ + // number = nd->getChildAtPos(0)->visitNode(nd->getChildAtPos(0), + // register_numl, register_numr); + // } + + if (nd->children_size() > 0) { + globalType.push(__int__); + register_numl.duplicate(); + nd->getChildAtPos(0)->visitNode(); + register_numl.pop(); + globalType.pop(); + } + varType *v = nd->getVarType(); + int start = nd->stack_pos; + uint8_t regnum = 1; + if (nd->asPointer or (nd->isPointer && nd->children_size() == 0)) + point_regnum++; + // uint8_t save_reg; + // point_regnum++; + if (nd->isPointer) { + // start = nd->stack_pos; + regnum = point_regnum; + } + if (nd->isPointer) { + int start = nd->stack_pos; -string findForWhile() -{ - string res = "__"; - NodeToken *p = current_node; - while (p->_nodetype != forNode and p->_nodetype != whileNode) - { - p = p->parent; - if (p == NULL) - break; + // bufferText->addAfter(string_format("addi a%d,a1,%d", point_regnum, + // start)); + + if (nd->children_size() == 0) { + if (nd->type == TokenUserDefinedVariableMemberFunction) + bufferText->addAfter( + string_format("addi a%d,a1,%d", register_numl.get(), start)); + else + bufferText->addAfter( + string_format("l32i a%d,a1,%d", register_numl.get(), start)); + // bufferText->addAfter(string_format("mov a%d,a%d", register_numl.get(), + // point_regnum)); + bufferText->sp.push(bufferText->get()); + } else { + bufferText->addAfter( + string_format("l32i a%d,a1,%d", point_regnum, start)); + start = 0; + for (int i = 0; i < v->total_size; i++) { + bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, + point_regnum, register_numl.get())); + } + bufferText->sp.push(bufferText->get()); + for (int i = 0; i < v->size; i++) { + // bufferText->addAfter(string_format("%s %s%d,%s%d,%d", + // v->load[i].c_str(), v->reg_name.c_str(), register_numl.get(), + // v->reg_name.c_str(), regnum, start)); + asmInstruction asmInstr = v->load[i]; + bufferText->addAfter(string_format( + "%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), point_regnum, start)); + translateType(globalType.get(), v->_varType, register_numl.get()); + // register_numl--; + start += v->sizes[i]; + bufferText->sp.push(bufferText->get()); + } } - if (p != NULL) - { - // //printf("jkd\n"); - res = string(p->getTargetText()); + if (nd->asPointer or (nd->isPointer && nd->children_size() == 0)) + point_regnum--; + register_numl.pop(); + register_numl.decrease(); + } else { + // printf("jj\n"); + for (int i = 0; i < v->size; i++) { + // bufferText->addAfter(string_format("%s %s%d,%s%d,%d", + // v->load[i].c_str(), v->reg_name.c_str(), register_numl.get(), + // v->reg_name.c_str(), regnum, start)); + asmInstruction asmInstr = v->load[i]; + bufferText->addAfter(string_format( + "%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), regnum, start)); + // printf("jj2 :%s\n",string_format("%s %s%d,%s%d,%d", + // asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), + // register_numl.get(), getRegType(asmInstr, 1).c_str(), regnum, + // start).c_str()); + translateType(globalType.get(), v->_varType, register_numl.get()); + // printf("jj3\n"); + // register_numl--; + start += v->sizes[i]; + bufferText->sp.push(bufferText->get()); + // printf("jj4\n"); } - return res; + register_numl.pop(); + // printf("jj5\n"); + register_numl.decrease(); + // printf("jj6\n"); + + return; + } } -void buildParents(NodeToken *__nd) - -{ +void _visitblockStatementNode(NodeToken *nd) { + register_numr.clear(); + register_numl.clear(); + register_numl.push(15); + register_numr.push(15); - // return; //new - // printf("klkkmkml %s\r\n",__nd->getTokenText()); - if (__nd->children_size() > 0) - { - for (vector::iterator it = __nd->children->begin(); it != __nd->children->end(); it++) - { + register_numl.push(15); + register_numr.push(15); + for (int i = 0; i < nd->children_size(); i++) { - if ((*it)->_nodetype == (int)UnknownNode) - { - __nd->children->erase(it); - // printf("on supppirm\r\n"); - } - else - { - (*it)->parent = __nd; - buildParents(*it); - } - } - } + nd->getChildAtPos(i)->visitNode(); + // f = f + g.f; + } + // nd->clear(); } - -void translateType(varTypeEnum to, varTypeEnum from, int regnum) - -{ - // printf("to:%s from: %s\n",varTypeEnumNames[to].c_str(),varTypeEnumNames[from].c_str()); - // if(to==__none__) - // to=__int__; - if (to == __none__ or from == to) - return; - switch (to) - { - case __float__: - // bufferText->sp.pop(); - bufferText->addAfterNoDouble(string_format("float.s f%d,a%d,0", regnum, regnum)); - // bufferText->sp.push(bufferText->get()); - break; - case __int__: - switch (from) - { - case __float__: - // bufferText->sp.pop(); - bufferText->addAfterNoDouble(string_format("trunc.s a%d,f%d,0", regnum, regnum)); - // bufferText->sp.push(bufferText->get()); - break; - default: - break; - } - break; - case __uint8_t__: - switch (from) - { - case __float__: - // bufferText->sp.pop(); - bufferText->addAfterNoDouble(string_format("trunc.s a%d,f%d,0", regnum, regnum)); - // bufferText->sp.push(bufferText->get()); - break; - default: - break; - } - break; - - default: - switch (from) - { - case __float__: - // bufferText->sp.pop(); - bufferText->addAfterNoDouble(string_format("trunc.s a%d,f%d,0", regnum, regnum)); - // bufferText->sp.push(bufferText->get()); - break; - default: - break; - } - break; - break; - } +void _visitdefFunctionNode(NodeToken *nd) { + // printf("compiling %s\n", nd->getTokenText()); + bufferText = &content; + isStructFunction = false; + if (nd->type == TokenUserDefinedVariableMemberFunction) + isStructFunction = true; + header.addAfter(string_format(".global @_%s", nd->getTokenText())); + if (!isStructFunction) + header.addAfter(string_format(".global @__%s", nd->getTokenText())); + // string variables = ""; + if (!isStructFunction) { + string variables = ""; + for (int i = 0; i < nd->getChildAtPos(1)->children_size(); i++) { + variables = string_format( + "%s %d", variables.c_str(), + nd->getChildAtPos(1)->getChildAtPos(i)->getVarType()->total_size); + } + header.addAfter(string_format( + ".var %d%s", nd->getChildAtPos(1)->children_size(), variables.c_str())); + } + if (nd->getChildAtPos(1)->children_size() > -1) { + header.addAfter(string_format("@_stack__%s:", nd->getTokenText())); + header.addAfter(string_format( + ".bytes %d", (nd->getChildAtPos(1)->children_size() + 1) * 4)); + } + if (!isStructFunction) { + bufferText->addAfter(string_format("@__%s:", nd->getTokenText())); + + NodeToken *variaToken = nd->getChildAtPos(1); + if (variaToken->children_size() > 0) { + bufferText->addAfter(string_format( + "entry a1,%d", ((nd->stack_pos) / 8 + 1) * 8 + 16 + + _STACK_SIZE)); // ((nd->stack_pos) / 8 + 1) * 8+20) + bufferText->addAfter( + string_format("l32r a9,@_stack__%s", nd->getTokenText())); + } + for (int k = 0; k < variaToken->children_size(); k++) { + // int start = variaToken->getChildAtPos(k)->stack_pos; + + // printf("ee p\r\n"); + int start = variaToken->getChildAtPos(k)->stack_pos; + if (start < _STACK_SIZE) + start = _STACK_SIZE; + for (int j = 0; j < variaToken->getChildAtPos(k)->getVarType()->size; + j++) { + asmInstruction asmInstr = + variaToken->getChildAtPos(k)->getVarType()->load[0]; + bufferText->addAfter(string_format( + "%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), k + 10, + getRegType(asmInstr, 1).c_str(), 9, + start - + _STACK_SIZE)); // point_regnum + // asmInstruction asmInstr = + // variaToken->getChildAtPos(k)->getVarType()->store[0]; + // bufferText->addAfter(string_format("%s + // %s%d,%s9,%d", + // asmInstructionsName[asmInstr].c_str(), + // getRegType(asmInstr, 0).c_str(), + // k+10,getRegType(asmInstr, 1).c_str(), start)); + start += variaToken->getChildAtPos(k)->getVarType()->sizes[j]; + } + //} + } + if (variaToken->children_size() > 0) { + bufferText->addAfter(string_format("call8 @_%s", nd->getTokenText())); + bufferText->addAfter(string_format("retw.n", nd->getTokenText())); + } + } + + bufferText->addAfter(string_format("@_%s:", nd->getTokenText())); + bufferText->addAfter(string_format( + "entry a1,%d", ((nd->stack_pos) / 8 + 1) * 8 + 16 + + _STACK_SIZE)); // ((nd->stack_pos) / 8 + 1) * 8+20) + int sav = 9; +#if _TRIGGER == 0 + bufferText->addAfterNoDouble( + string_format("l32r a%d,@_stack_%s", sav, nd->getTokenText())); +#endif + if (saveReg) { + bufferText->addAfter("ssi f15,a1,16"); + bufferText->addAfter("ssi f14,a1,20"); + bufferText->addAfter("ssi f13,a1,24"); + } + if (saveRegAbs) { + bufferText->addAfter("s32i a15,a1,16"); + bufferText->addAfter("s32i a14,a1,20"); + bufferText->addAfter("s32i a13,a1,24"); + } + for (int i = 1; i < nd->children_size(); i++) { + + nd->getChildAtPos(i)->visitNode(); + // f = f + g.f; + // h = h + g.header; + } + + if (saveReg) { + bufferText->addAfter("lsi f15,a1,16"); + bufferText->addAfter("lsi f14,a1,20"); + bufferText->addAfter("lsi f13,a1,24"); + } + if (saveRegAbs) { + bufferText->addAfter("l32i a15,a1,16"); + bufferText->addAfter("l32i a14,a1,20"); + bufferText->addAfter("l32i a13,a1,24"); + } + bufferText->addAfter(string_format("retw.n")); + + isStructFunction = false; + bufferText = &footer; } +void _visitstatementNode(NodeToken *nd) { + point_regnum = 5; + // printf("visit statement\n"); + register_numr.clear(); + register_numl.clear(); + register_numl.push(15); + register_numr.push(15); + + register_numl.push(15); + register_numr.push(15); + for (int i = 0; i < nd->children_size(); i++) { -string _data_sav; -Stack globalType = Stack(__int__); - -void _visittypeNode(NodeToken *nd) {} -void _visitstoreLocalVariableNodeAsRegister(NodeToken *nd) -{ - if (nd->getVarType()->_varType == __float__) - { - bufferText->addAfter(bufferText->sp.pop(), string_format("rfr a%d,f%d", nd->target, register_numl.get())); - } - else - bufferText->addAfter(bufferText->sp.pop(), string_format("mov a%d,a%d", nd->target, register_numl.get())); -} -void _visitlocalVariableNodeAsRegister(NodeToken *nd) -{ - if (nd->getVarType()->_varType == __float__) - bufferText->addAfter(string_format("wfr f%d,a%d", register_numl.get(), nd->target)); - else - bufferText->addAfter(string_format("movr a%d,a%d", register_numl.get(), nd->target)); - bufferText->sp.push(bufferText->get()); - register_numl.decrease(); + register_numl.duplicate(); + nd->getChildAtPos(i)->visitNode(); + register_numl.pop(); + } + // nd->clear(); + // printf("end statement\n"); + //_node_token_stack.clear(); } -void _visitnumberNode(NodeToken *nd) -{ - // //printf("in number\n"); - - // register_numl.duplicate(); - if (nd->children_size() > 0) - { - for (int i = 0; i < nd->children_size(); i++) - { - register_numl.duplicate(); - nd->getChildAtPos(i)->visitNode(); - // register_numl.pop(); - // bufferText->sp.push(bufferText->get()); - register_numl.pop(); - } - } - else - { - if (nd->getVarType()->_varType == __float__) - { - float __f = 0; - sscanf(nd->getTokenText(), "%f", &__f); - header.addAfter(string_format("@_%s_%d:", "local_var", local_var_num)); - // local_var_num++; - string val = ".bytes 4"; - uint32_t __num = (uint32_t)(*((uint32_t *)&__f)); - // Serial.//printf(" on a float %4x\r\n",__num); - uint8_t c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - header.addAfter(val); - // point_regnum++; - bufferText->addAfter(string_format("l32r a%d,@_%s_%d", 9, "local_var", local_var_num)); // YBA 25-02-2025 - bufferText->addAfter(string_format("lsi f%d,a%d,0", register_numl.get(), 9)); // YBA 25-02-2025 - bufferText->sp.push(bufferText->get()); - // point_regnum--; - local_var_num++; - register_numl.decrease(); - } - else if (string(nd->getTokenText()).find("x") != string::npos) - { - unsigned int __num = 0; +void _visitprogramNode(NodeToken *nd) { + // + // printf("visit program\n"); + point_regnum = 5; + + // content.clear(); + // header.clear(); + content.begin(); + header.begin(); + footer.begin(); + + // header.addAfter("@_stack:"); + // header.addAfter(".bytes 60"); + header.addBefore("@__handle_:"); + header.addBefore(".bytes 4"); + header.addBefore("@__execaddr_:"); + header.addBefore(".bytes 4"); + header.addBefore("@__sync:"); + header.addBefore(".bytes 4"); + // header.addAfter("@_stackr:"); + // header.addAfter(".bytes 32"); + + footer.addBefore(" "); + // footer.addAfter("@__footer:"); + // footer.addAfter("entry a1,144"); + + // header.addAfter("__basetime:"); + // header.addAfter(".bytes 4"); + register_numr.clear(); + register_numl.clear(); + register_numl.push(15); + register_numr.push(15); + bufferText = &footer; + for (int i = 0; i < nd->children_size(); i++) { - sscanf(nd->getTokenText(), "%x", &__num); +#ifndef __MEM_PARSER + if (nd->getChildAtPos(i)->_nodetype != defFunctionNode && + nd->getChildAtPos(i)->_nodetype != defAsmFunctionNode) { +#endif + nd->getChildAtPos(i)->visitNode(); +#ifndef __MEM_PARSER + } // NEW +#endif + } - if (__num >= 2048) - { - header.addAfter(string_format("@_%s_%d:", "local_var", local_var_num)); - string val = ".bytes 4"; - uint8_t c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - header.addAfter(val); - // point_regnum++; - bufferText->addAfter(string_format("l32r a%d,@_%s_%d", 9, "local_var", local_var_num)); // YBA 25-02-2025 - bufferText->addAfter(string_format("l32i a%d,a%d,0", register_numl.get(), 9)); // YBA 25-02-2025 - bufferText->sp.push(bufferText->get()); - // point_regnum--; - local_var_num++; - register_numl.decrease(); - } - else - { - bufferText->addAfter(string_format("movi a%d,%d", register_numl.get(), __num)); // nd->_token->text.c_str())); - bufferText->sp.push(bufferText->get()); - register_numl.decrease(); - } - } - else - { - int __num = 0; + // footer.addAfter("retw.n"); - sscanf(nd->getTokenText(), "%d", &__num); + if (footer.size() > 1) { + header.addAfter(".global @__footer"); + footer.addAfter("retw.n"); + footer.begin(); - if (__num >= 2048 or __num <= -2047) - { - header.addAfter(string_format("@_%s_%d:", "local_var", local_var_num)); - string val = ".bytes 4"; - uint8_t c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - c = __num & 0xff; - val = val + " " + string_format("%02x", c); - // val=val+'A'; - header.addAfter(val); - // point_regnum++; - bufferText->addAfter(string_format("l32r a%d,@_%s_%d", 9, "local_var", local_var_num)); // YBA 25-02-1975 - bufferText->addAfter(string_format("l32i a%d,a%d,0", register_numl.get(), 9)); // YBA 25-02-1975 - bufferText->sp.push(bufferText->get()); - // point_regnum--; - local_var_num++; - register_numl.decrease(); - } - else - { - // printf("Pour %s\n",bufferText->current().c_str()); - bufferText->addAfter(string_format("movi a%d,%d", register_numl.get(), __num)); // nd->_token->text.c_str())); - // printf("on a %s\n",bufferText->current().c_str()); - bufferText->sp.push(bufferText->get()); - register_numl.decrease(); - } - } - } + footer.addBefore("entry a1,144"); + footer.begin(); + footer.addBefore("@__footer:"); + } + + if (addfloatdivision) { + header.addAfter(" .global @___div(d|d)"); + header.addAfter("@_stack___div(d|d):"); + header.addAfter(".bytes 12"); + content.end(); + for (int i = 0; i < _div_size; i++) { + content.addAfter(string(_div[i])); + } + } } +void _visitassignementNode(NodeToken *nd) { + // printf("entre assignemen\n") ; + point_regnum = 5; // YBA 25-02-2025 + bufferText->sp.clear(); + bufferText->sp.push(bufferText->get()); + register_numl.duplicate(); -void _visitbinOpNode(NodeToken *nd) -{ + if (nd->children_size() > 1) + globalType.push(nd->getChildAtPos(0)->getVarType()->_varType); - // printf("bin operator\n"); - // register_numl.displaystack(); - register_numl.duplicate(); - // register_numr.duplicate(); - // if (nd->getChildAtPos(0)->visitNode != NULL) - nd->getChildAtPos(0)->visitNode(); - // printf("ddd %s\n",bufferText->back().c_str()); - // register_numl.displaystack(); + if (nd->children_size() > 1) { register_numl.duplicate(); - // register_numr.duplicate(); - if (nd->type != TokenPower) - { - // if (nd->getChildAtPos(2)->visitNode != NULL) - nd->getChildAtPos(1)->visitNode(); - } - else - { - bufferText->addAfter(" "); - bufferText->sp.push(bufferText->get()); - } - // register_numr.pop(); - register_numl.swap(); - register_numr.push(register_numl.pop()); - register_numl.swap(); - // register_numl.displaystack(); - // if (nd->getChildAtPos(1)->visitNode != NULL) - // nd->getChildAtPos(1)->visitNode(); - _visitoperatorNode(nd); + nd->getChildAtPos(1)->visitNode(); register_numl.pop(); - if (nd->type == TokenAddition || nd->type == TokenSubstraction) - { - register_numl.increase(); - } - // nex - if (nd->type == TokenSlash || nd->type == TokenStar) - { - register_numl.pop(); - register_numl.push(register_numr.get()); + + if (nd->getChildAtPos(1)->getVarType() != NULL) { + + translateType(globalType.get(), + nd->getChildAtPos(1)->getVarType()->_varType, + register_numl.get()); + // printf("retour on push\n") ; + // } } - // end new - bufferText->sp.pop(); + bufferText->sp.pop(); bufferText->sp.push(bufferText->get()); - // register_numl.pop(); - register_numr.pop(); -} -void _visitunitaryOpNode(NodeToken *nd) -{ - // register_numl.displaystack(); - register_numl.duplicate(); - // register_numr.duplicate(); - if (nd->type == TokenUppersand) - { - // //printf("node UNitary operator2\n"); - // nd->getChildAtPos(0)->asPointer = true; - // addTokenSup(nd); - // nd->_token->_varType = __none__; - nd->_vartype = (int)__none__; - nd->isPointer = true; - nd->getChildAtPos(0)->visitNode(); - register_numl.pop(); - // bufferText->sp.pop(); - bufferText->sp.push(bufferText->get()); - return; - } - if (nd->type == TokenNot) - { - nd->getChildAtPos(0)->visitNode(); - register_numl.pop(); - bufferText->addAfter("movi a12,0"); - bufferText->addAfter(string_format("bnez a%d,label_not_%d", register_numl.get(), for_if_num)); - bufferText->addAfter("movi a12,1"); - bufferText->addAfter(string_format("label_not_%d:", for_if_num)); - bufferText->addAfter(string_format("mov a%d,a12", register_numl.get())); - bufferText->sp.push(bufferText->get()); - register_numl.decrease(); - return; - } - else if (nd->type == TokenSubstraction) - { - nd->type = TokenNegation; + point_regnum++; + } + register_numl.duplicate(); + nd->getChildAtPos(0)->visitNode(); + register_numl.pop(); + + register_numl.pop(); + bufferText->sp.pop(); + globalType.pop(); + // clearNodeToken(nd); // new + // nd->clear(); + register_numl.clear(); + register_numl.push(15); + register_numl.push(15); + + register_numr.clear(); + register_numr.push(15); + register_numr.push(15); +} +void _visitternaryIfNode(NodeToken *nd) { + + register_numl.duplicate(); + nd->getChildAtPos(0)->visitNode(); + register_numl.pop(); + bufferText->addAfter( + string_format("beqz a%d,%s", register_numl.get(), nd->getTargetText())); + /* + register_numr.clear(); + register_numl.clear(); + register_numl.push(15); + register_numr.push(15); + + register_numl.push(15); + register_numr.push(15); - nd->getChildAtPos(0)->visitNode(); - // register_numl.displaystack(); - register_numl.pop(); - // bufferText->sp.push(bufferText->get()); +*/ + register_numl.duplicate(); + nd->getChildAtPos(1)->visitNode(); + register_numl.pop(); + bufferText->addAfter(string_format("j %s_end", nd->getTargetText())); + bufferText->addAfter(string_format("%s:", nd->getTargetText())); + /* + register_numr.clear(); + register_numl.clear(); + register_numl.push(15); + register_numr.push(15); + + register_numl.push(15); + register_numr.push(15); + */ + register_numl.duplicate(); + nd->getChildAtPos(2)->visitNode(); + register_numl.pop(); + bufferText->addAfter(string_format("%s_end:", nd->getTargetText())); +} - // nd->getChildAtPos(1)->visitNode(); - _visitoperatorNode(nd); - // register_numl.pop(); - register_numl.decrease(); +bool isBranchImmediate(int val, bool inverse) { + if (!inverse) { + switch (val) { + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + case 8: + case 10: + case 12: + case 16: + case 32: + case 64: + case 128: + case 256: + return true; + break; + default: + return false; + break; } - else - { - // if (nd->getChildAtPos(0)->visitNode != NULL) - nd->getChildAtPos(0)->visitNode(); - // register_numl.displaystack(); - register_numl.pop(); - _visitoperatorNode(nd); - // nd->getChildAtPos(1)->visitNode(); - register_numl.decrease(); - bufferText->sp.push(bufferText->get()); + } else { + switch (val) { + // case 1: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + case 9: + case 11: + case 15: + case 31: + case 63: + case 127: + case 255: + return true; + break; + default: + return false; + break; } + } } -void _visitoperatorNode(NodeToken *nd) -{ - // printf("operator %s\n",tokenNames[nd->type].c_str()); - // register_numl.pop(); - // register_numl.displaystack(); - varTypeEnum l = __none__; - varTypeEnum r = __none__; - // //printf("kk\n"); +int valBranchImmediate(int val) { + switch (val) { + case 1: + return 1; + break; + case 2: + return 2; + break; + case 3: + return 3; + break; + case 4: + return 4; + break; + case 5: + return 5; + break; + case 6: + return 6; + break; + case 7: + return 7; + break; + case 8: + return 8; + break; + case 10: + return 9; + break; + case 12: + return 10; + break; + case 16: + return 11; + break; + case 32: + return 12; + break; + case 64: + return 13; + break; + case 128: + return 14; + break; + case 256: + return 14; + break; + default: + return 88; + break; + } +} - if (nd->getChildAtPos(0)->getVarType() != NULL) - { - l = nd->getChildAtPos(0)->getVarType()->_varType; - } +void _visittestNode(NodeToken *nd) { + + int numl = register_numl.get(); + + if (nd->getChildAtPos(0)->_vartype == __float__) + nd->getChildAtPos(1)->_vartype = __float__; + if (nd->getChildAtPos(1)->_vartype == __float__) + nd->getChildAtPos(0)->_vartype = __float__; + string _add = ""; + if (nd->getChildAtPos(0)->_vartype == __uint32_t__ || + nd->getChildAtPos(1)->_vartype == __uint32_t__) + _add = "u"; + // register_numl.duplicate(); + nd->getChildAtPos(0)->visitNode(); + // register_numl.pop(); + + int leftl = register_numl.get(); + + // register_numl.duplicate(); + nd->getChildAtPos(1)->visitNode(); + // register_numl.pop(); + + //////printf("compare %s %s\n",tokenNames[nd->_token->type + ///].c_str(),nd->_token->text.c_str()); + string compop = ""; + string compo2 = ""; + // to compose + int h = 999; + + if (nd->getChildAtPos(1)->_vartype == __float__) { + switch (nd->type) { + case TokenLessThan: + h = numl; + compop = "olt.s"; // greater or equal + // bufferText->addAfter( string_format("%s_end:\n",nd->target.c_str())); + compo2 = "bf"; + break; + case TokenDoubleEqual: + h = numl; + compop = "oeq.s"; // not equal + compo2 = "bf"; + break; + case TokenNotEqual: + h = numl; + compop = "oeq.s"; // equal + compo2 = "bt"; + break; + case TokenMoreOrEqualThan: + compop = "ole.s"; // less then + h = numl; + numl = leftl; + leftl = h; + compo2 = "bf"; + break; + case TokenMoreThan: + compop = "olt.s"; // not equal + h = numl; + numl = leftl; + leftl = h; + compo2 = "bf"; + break; + case TokenLessOrEqualThan: + h = numl; + compop = "ole.s"; // not equal + compo2 = "bf"; - // //printf("kk2 :%d\n",nd->parent->children_size()); - if (nd->children_size() >= 2) - { - if (nd->getChildAtPos(1) == NULL) - { - // printf("WFT %d %s\n",nd->parent->children_size(),nodeTypeNames[nd->parent->_nodetype].c_str()); - } + // compo2="bt"; + break; + default: + break; + } + bufferText->addAfter( + string_format("%s b0,f%d,f%d", compop.c_str(), numl, leftl)); + bufferText->addAfter( + string_format("%s b0,%s_end", compo2.c_str(), nd->getTargetText())); + bufferText->addAfter(string_format("movi a%d,1", h)); + bufferText->addAfter(string_format("j %s_end_", nd->getTargetText())); + bufferText->addAfter(string_format("%s_end:", nd->getTargetText())); + bufferText->addAfter(string_format("movi a%d,0", h)); + bufferText->addAfter(string_format("%s_end_:", nd->getTargetText())); + register_numl.increase(); + } else { - if (nd->getChildAtPos(1)->getVarType() != NULL) - { - // //printf("kk32 %s\n",nodeTypeNames[nd->parent->_nodetype].c_str()); - r = nd->getChildAtPos(1)->getVarType()->_varType; - } - } - // //printf("kk3\n"); - bool ff = false; - if (nd->getVarType() == NULL) - { - // addTokenSup(nd->parent); - if (globalType.get() == __float__) - { - // nd->parent->_token->_varType = __float__; - nd->_vartype = (int)__float__; - } - else - { - // nd->parent->_token->_varType = __none__; - nd->_vartype = __none__; - } + switch (nd->type) { + case TokenLessThan: + h = numl; + compop = "bge"; // greater or equal blt + // bufferText->addAfter( string_format("%s_end:\n",nd->target.c_str())); + break; + case TokenDoubleEqual: + h = numl; + compop = "bne"; // not equal beq + break; + case TokenNotEqual: + h = numl; + compop = "beq"; // equal + break; + case TokenMoreOrEqualThan: + h = numl; + compop = "blt"; // less then + break; + case TokenMoreThan: + compop = "bge"; // not equal + h = numl; + numl = leftl; + leftl = h; + break; + case TokenLessOrEqualThan: + compop = "blt"; // not equal + h = numl; + numl = leftl; + leftl = h; + break; + default: + break; } - // //printf("kk4\n"); - if (globalType.get() == __float__) - { - ff = true; - nd->_vartype = __float__; - } - // //printf("kk5\n"); - asmInstruction asmInstr; - if (nd->children_size() >= 2) - translateType(globalType.get(), r, register_numr.get()); - translateType(globalType.get(), l, register_numl.get()); - switch (nd->type) - { - case TokenAddition: - { - if (ff) - { - asmInstr = adds; - } - else - { - asmInstr = add; - } - string new_line = string_format("%s %s%d,%s%d,%s%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), register_numl.get(), getRegType(asmInstr, 2).c_str(), register_numr.get()); + bufferText->addAfter(string_format("%s%s a%d,a%d,%s_end", compop.c_str(), + _add.c_str(), numl, leftl, + nd->getTargetText())); + bufferText->addAfter(string_format("movi a%d,1", h)); + bufferText->addAfter(string_format("j %s_end_", nd->getTargetText())); + bufferText->addAfter(string_format("%s_end:", nd->getTargetText())); + bufferText->addAfter(string_format("movi a%d,0", h)); + bufferText->addAfter(string_format("%s_end_:", nd->getTargetText())); + register_numl.increase(); + } + // f = f + g.f; +} - string _last = bufferText->current(); - ; // bufferText->back(); - string tocmp = string_format("movi a%d,", register_numr.get()); - // printf("to found %s\r\n",_last,tocmp); - if (_last.compare(0, tocmp.size(), tocmp) == 0) - { - // printf("to found %s\r\n",_last,tocmp); - int a, b; - sscanf(_last.c_str(), "movi a%d,%d", &a, &b); - if (b >= -128 and b <= 127) - { - // bufferText->pop(); - bufferText->blankCurrent(); - bufferText->addAfter(string_format("addi a%d,a%d,%d", register_numl.get(), register_numl.get(), b)); - } - else - { - bufferText->addAfter(new_line); - } - } - else - { - bufferText->addAfter(new_line); - } +void _visitcomparatorNode(NodeToken *nd) { + // printf("in comparator\n"); + int numl = register_numl.get(); - // return; + if (nd->getChildAtPos(0)->_nodetype != testNode) { + // on va tester si on est >0; + nd->getChildAtPos(0)->visitNode(); + if (nd->_total_size > 116) { + bufferText->addAfter( + string_format("bnez a%d,%s_if", numl, nd->parent->getTargetText())); + bufferText->addAfter( + string_format("j %s_end", nd->parent->getTargetText())); + bufferText->addAfter( + string_format("%s_if:", nd->parent->getTargetText())); + register_numl.increase(); + } else { + bufferText->addAfter( + string_format("beqz a%d,%s_end", numl, nd->parent->getTargetText())); + // bufferText->addAfter(_compare.back()+1,string_format("j %s_end", + // nd->target.c_str())); + // bufferText->addAfter(_compare.back()+2,string_format("%s_if:", + // nd->target.c_str())); + register_numl.increase(); } - break; - case TokenShiftLeft: - // bufferText->addAfter("movi a8,32"); - // bufferText->addAfter(string_format("sub a%d,a8,a%d",register_numr.get(),register_numr.get()).c_str()); - bufferText->addAfter(string_format("ssl a%d", register_numr.get()).c_str()); - bufferText->addAfter(string_format("sll a%d,a%d", register_numl.get(), register_numl.get()).c_str()); + + return; + } + // printf("test node\n\r"); + // nd->getChildAtPos(0)->visitNode(); + nd = nd->getChildAtPos(0); + nd->_total_size = nd->parent->_total_size; + nd->setTargetText(string(nd->parent->getTargetText())); + if (nd->getChildAtPos(0)->_vartype == __float__) + nd->getChildAtPos(1)->_vartype = __float__; + if (nd->getChildAtPos(1)->_vartype == __float__) + nd->getChildAtPos(0)->_vartype = __float__; + // register_numl.duplicate(); + string _add = ""; + if (nd->getChildAtPos(0)->_vartype == __uint32_t__ || + nd->getChildAtPos(1)->_vartype == __uint32_t__) + _add = "u"; + nd->getChildAtPos(0)->visitNode(); + // register_numl.pop(); + + int leftl = register_numl.get(); + + // register_numl.duplicate(); + nd->getChildAtPos(1)->visitNode(); + // register_numl.pop(); + + //////printf("compare %s %s\n",tokenNames[nd->_token->type + ///].c_str(),nd->_token->text.c_str()); + string compop = ""; + string compo2 = ""; + // to compose + int h; + + if (nd->getChildAtPos(1)->_vartype == __float__) { + + if (nd->_total_size > 116) { + switch (nd->type) { + case TokenLessThan: + compop = "olt.s"; // greater or equal + // h = numl; + // numl = leftl; + // leftl = h; + // bufferText->addAfter( + // string_format("%s_end:\n",nd->target.c_str())); + compo2 = "bt"; break; - case TokenShiftRight: - // bufferText->addAfter("movi a8,32"); - // bufferText->addAfter(string_format("sub a%d,a8,a%d",register_numr.get(),register_numr.get()).c_str()); - bufferText->addAfter(string_format("wsr a%d,3", register_numr.get()).c_str()); - bufferText->addAfter(string_format("srl a%d,a%d", register_numl.get(), register_numl.get()).c_str()); + case TokenDoubleEqual: + compop = "oeq.s"; // not equal + compo2 = "bt"; break; - case TokenSubstraction: - { - if (ff) - { - asmInstr = subs; - } - else - { - asmInstr = sub; - } - string new_line = string_format("%s %s%d,%s%d,%s%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), register_numl.get(), getRegType(asmInstr, 2).c_str(), register_numr.get()); + case TokenNotEqual: + compop = "oeq.s"; // equal + compo2 = "bf"; + break; + case TokenMoreOrEqualThan: + compop = "ole.s"; // less then + h = numl; + numl = leftl; + leftl = h; + compo2 = "bt"; - string _last = bufferText->current(); - string tocmp = string_format("movi a%d,", register_numr.get()); - // printf("to found %s %s\r\n", bufferText->current().c_str(), tocmp.c_str()); - if (_last.compare(0, tocmp.size(), tocmp) == 0) - { - // printf("to found %s\r\n",_last,tocmp); - int a, b; - sscanf(_last.c_str(), "movi a%d,%d", &a, &b); - if (b >= -128 and b <= 127) - { - // bufferText->pop(); - bufferText->blankCurrent(); - bufferText->addAfter(string_format("addi a%d,a%d,-%d", register_numl.get(), register_numl.get(), b)); - } - else - { - bufferText->addAfter(new_line); - } - } - else - { - bufferText->addAfter(new_line); - } - // bufferText->addAfter(string_format("sub a%d,a%d,a%d", register_numl.get(), register_numl.get(), register_numr.get())); - // return; - } - break; - case TokenSlash: - // bufferText->addAfter(string_format("quou a%d,a%d,a%d", register_numl.get(), register_numl.get(), register_numr.get())); - if (ff) - { - bufferText->addAfter(string_format("mov.s f1,f%d", register_numl.get())); - bufferText->addAfter(string_format("mov.s f2,f%d", register_numr.get())); - bufferText->addAfter("call8 @___div(d|d)"); - bufferText->addAfter(string_format("mov.s f%d,f0", register_numl.get())); - addfloatdivision = true; - } - else - { - // asmInstr = quou; - if (l == __uint32_t__ || r == __uint32_t__) - asmInstr = quou; - else - asmInstr = quos; - bufferText->addAfter(string_format("%s %s%d,%s%d,%s%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), register_numl.get(), getRegType(asmInstr, 2).c_str(), register_numr.get())); - } - // return; - break; - case TokenStar: - if (ff) - { - asmInstr = muls; - } - else - { - asmInstr = mull; - } - bufferText->addAfter(string_format("%s %s%d,%s%d,%s%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), register_numl.get(), getRegType(asmInstr, 2).c_str(), register_numr.get())); - // bufferText->addAfter(string_format("mull a%d,a%d,a%d", register_numl.get(), register_numl.get(), register_numr.get())); - // return; break; - case TokenPlusPlus: - if (nd->getChildAtPos(0)->isPointer && nd->getChildAtPos(0)->children_size() == 0) - { - bufferText->addAfter(string_format("addi a%d,a%d,%d", register_numl.get(), register_numl.get(), nd->getChildAtPos(0)->getVarType()->total_size)); - } - else - { - bufferText->addAfter(string_format("addi a%d,a%d,1", register_numl.get(), register_numl.get())); - } - // return; + case TokenMoreThan: + compop = "olt.s"; // not equal + h = numl; + numl = leftl; + leftl = h; + compo2 = "bt"; break; - case TokenMinusMinus: - if (nd->getChildAtPos(0)->isPointer && nd->getChildAtPos(0)->children_size() == 0) - { - bufferText->addAfter(string_format("addi a%d,a%d,-%d", register_numl.get(), register_numl.get(), nd->getChildAtPos(0)->getVarType()->total_size)); - } - else - { - bufferText->addAfter(string_format("addi a%d,a%d,-1", register_numl.get(), register_numl.get())); - } - // return; + case TokenLessOrEqualThan: + compop = "ole.s"; // not equal + compo2 = "bt"; break; - case TokenModulo: - bufferText->addAfter(string_format("remu a%d,a%d,a%d", register_numl.get(), register_numl.get(), register_numr.get())); - // return; + default: break; - case TokenPower: - { - // comment supprimer ce qu'il y a avant - int __num = 0; - if (nd->getChildAtPos(1)->_nodetype == numberNode) - { - - sscanf(nd->getChildAtPos(1)->getTokenText(), "%d", &__num); - if (__num > 2) - { - if (ff) - { - bufferText->blankCurrent(); - bufferText->addAfter(string_format("mov.s f10,f%d", register_numl.get())); - asmInstr = muls; - } - else - { - bufferText->addAfter(string_format("mov a10,a%d", register_numl.get())); - asmInstr = mull; - } - for (int k = 1; k < __num; k++) - { - bufferText->addAfter(string_format("%s %s%d,%s%d,%s10", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), register_numl.get(), getRegType(asmInstr, 2).c_str())); - } - } - else - { - if (ff) - { - bufferText->blankCurrent(); - asmInstr = muls; - } - else - { - asmInstr = mull; - } - bufferText->addAfter(string_format("%s %s%d,%s%d,%s%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), register_numl.get(), getRegType(asmInstr, 2).c_str(), register_numl.get())); - } - } - } - break; - case TokenKeywordAnd: - bufferText->addAfter(string_format("and a%d,a%d,a%d", register_numl.get(), register_numl.get(), register_numr.get())); + } + bufferText->addAfter( + string_format("%s b0,f%d,f%d", compop.c_str(), numl, leftl)); + bufferText->addAfter( + string_format("%s b0,%s_if", compo2.c_str(), nd->getTargetText())); + bufferText->addAfter(string_format("j %s_end", nd->getTargetText())); + bufferText->addAfter(string_format("%s_if:", nd->getTargetText())); + register_numl.increase(); + } else { + switch (nd->type) { + case TokenLessThan: + compop = "olt.s"; // greater or equal + // bufferText->addAfter( + // string_format("%s_end:\n",nd->target.c_str())); + compo2 = "bf"; break; - case TokenKeywordOr: - bufferText->addAfter(string_format("or a%d,a%d,a%d", register_numl.get(), register_numl.get(), register_numr.get())); + case TokenDoubleEqual: + compop = "oeq.s"; // not equal + compo2 = "bf"; break; - case TokenKeywordFabs: - - bufferText->addAfter(string_format("abs.s f%d,f%d", register_numl.get(), register_numl.get())); + case TokenNotEqual: + compop = "oeq.s"; // equal + compo2 = "bt"; break; - case TokenKeywordAbs: - if (ff) - { - bufferText->addAfter(string_format("abs.s f%d,f%d", register_numl.get(), register_numl.get())); - } - else - { - bufferText->addAfter(string_format("abs a%d,a%d", register_numl.get(), register_numl.get())); - } + case TokenMoreOrEqualThan: + compop = "ole.s"; // less then + h = numl; + numl = leftl; + leftl = h; + compo2 = "bf"; break; - case TokenNegation: - if (ff) - { - asmInstr = negs; - } - else - { - asmInstr = neg; - } - bufferText->addAfter(string_format("%s %s%d,%s%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), register_numl.get())); - bufferText->sp.pop(); - bufferText->sp.push(bufferText->get()); - // return; - default: - // return; + case TokenMoreThan: + compop = "olt.s"; // not equal + h = numl; + numl = leftl; + leftl = h; + compo2 = "bf"; break; - } -} -// void _visitProgram(NodeToken *nd) {} -void _visitglobalVariableNode(NodeToken *nd) -{ - - // printf("comopiline glmobalvar %s\n",nd->getTokenText()); - // int r_size = 0; - isInFunction = false; - register_numl.duplicate(); - if (nd->children_size() > 0) - { - // int r_size = 0; - vector tile; - int nb = 0; - string sd = string(nd->getTargetText()); - if (sd.compare(0, 1, "@") == 0) - { - tile = split(sd, " "); + case TokenLessOrEqualThan: + compop = "ole.s"; // not equal + compo2 = "bf"; - sscanf(tile[0].c_str(), "@%d", &nb); - // r_size = stringToInt((char *)tile[1].c_str()); - } - if (nb > 1) - { - bufferText->addAfter("movi a10,0"); + // compo2="bt"; + break; + default: + break; + } + bufferText->addAfter( + string_format("%s b0,f%d,f%d", compop.c_str(), numl, leftl)); + bufferText->addAfter( + string_format("%s b0,%s_end", compo2.c_str(), nd->getTargetText())); + // bufferText->addAfter(string_format("%s a%d,a%d,%s_end", compop.c_str(), + // numl, leftl, nd->getTargetText())); + + // bufferText->addAfter(_compare.back()+1,string_format("j %s_end", + // nd->target.c_str())); + // bufferText->addAfter(_compare.back()+2,string_format("%s_if:", + // nd->target.c_str())); + register_numl.increase(); + } + } else { + if (nd->_total_size > 127) // 127 + { + int f; + bool regular = true; + + if (nd->getChildAtPos(1)->getChildAtPos(0)->_nodetype == numberNode) { + + f = stringToInt(nd->getChildAtPos(1)->getChildAtPos(0)->getTokenText()); + switch (nd->type) { + case TokenLessThan: + if (isBranchImmediate(f, false)) { + + compop = "blti"; // not equal + leftl = numl; + // f++; + regular = false; + } + break; + case TokenDoubleEqual: + if (isBranchImmediate(f, false)) { + compop = "bnei"; // equal + regular = false; + leftl = numl; + } + break; + case TokenNotEqual: + if (isBranchImmediate(f, false)) { + compop = "beqi"; // equal + regular = false; + leftl = numl; + } + break; + case TokenMoreOrEqualThan: + if (isBranchImmediate(f, false)) { + compop = "bgei"; // not equal + leftl = numl; + // f++; + regular = false; + } + break; + case TokenMoreThan: + if (isBranchImmediate(f, true)) { + compop = "bgei"; // not equal + leftl = numl; + f++; + regular = false; + } + break; + case TokenLessOrEqualThan: + if (isBranchImmediate(f, true)) { + compop = "blti"; // not equal + leftl = numl; + f++; + regular = false; + } + + break; + default: + break; } + } + if (regular) { + switch (nd->type) { + case TokenLessThan: + compop = "blt"; // greater or equal + // bufferText->addAfter( + // string_format("%s_end:\n",nd->target.c_str())); + break; + case TokenDoubleEqual: + compop = "beq"; // not equal + break; + case TokenNotEqual: + compop = "bne"; // equal + break; + case TokenMoreOrEqualThan: + compop = "bge"; // less then + break; + case TokenMoreThan: + compop = "blt"; // not equal + h = numl; + numl = leftl; + leftl = h; + break; + case TokenLessOrEqualThan: + compop = "bge"; // not equal + h = numl; + numl = leftl; + leftl = h; + break; + default: + break; + } + } + if (regular) { + bufferText->addAfter(string_format("%s%s a%d,a%d,%s_if", compop.c_str(), + _add.c_str(), numl, leftl, + nd->getTargetText())); + } else { + bufferText->blankCurrent(); + + f = valBranchImmediate(f); + bufferText->addAfter(string_format("%s%s a%d,%d,%s_if", compop.c_str(), + _add.c_str(), leftl, f, + nd->getTargetText())); + } + bufferText->addAfter(string_format("j %s_end", nd->getTargetText())); + bufferText->addAfter(string_format("%s_if:", nd->getTargetText())); + register_numl.increase(); + } else { + int f; + bool regular = true; + + if (nd->getChildAtPos(1)->getChildAtPos(0)->_nodetype == numberNode) { + + f = stringToInt(nd->getChildAtPos(1)->getChildAtPos(0)->getTokenText()); + switch (nd->type) { + case TokenLessThan: + if (isBranchImmediate(f, false)) { + printf("on est ici doublec\n"); + compop = "bgei"; // not equal + h = numl; + numl = leftl; + leftl = h; + // f++; + regular = false; + } + break; + case TokenDoubleEqual: + if (isBranchImmediate(f, false)) { + compop = "bnei"; // equal + regular = false; + leftl = numl; + } + break; + case TokenNotEqual: + if (isBranchImmediate(f, false)) { + compop = "beqi"; // equal + regular = false; + } + break; + case TokenMoreOrEqualThan: + if (isBranchImmediate(f, false)) { + compop = "blti"; // not equal + h = numl; + numl = leftl; + leftl = h; + // f++; + regular = false; + } + break; + case TokenMoreThan: + if (isBranchImmediate(f, true)) { + compop = "blti"; // not equal + h = numl; + numl = leftl; + leftl = h; + f++; + regular = false; + } + break; + case TokenLessOrEqualThan: + if (isBranchImmediate(f, true)) { + compop = "bgei"; // not equal + h = numl; + numl = leftl; + leftl = h; + f++; + regular = false; + } - for (int i = 0; i < nd->children_size(); i++) - { - // globalType.push(__int__); - register_numl.duplicate(); - nd->getChildAtPos(i)->visitNode(); - register_numl.pop(); - translateType(__int__, nd->getChildAtPos(i)->getVarType()->_varType, register_numl.get()); - if (nd->children_size() > 1) - { - if (i < nd->children_size() - 1) - { - - for (int h = 1; h < nd->children_size() - i; h++) - { - bufferText->addAfter(string_format("movi a11,%d", stringToInt((char *)tile[i + 1 + h].c_str()))); - // bufferText->addAfter(string_format("mull a11,a10,a11")); - bufferText->addAfter(string_format("mull a%d,a%d,a11", register_numl.get(), register_numl.get())); - } - // if(i>0) - bufferText->addAfter(string_format("add a10,a10,a%d", register_numl.get())); - } - else - { - bufferText->addAfter(string_format("add a%d,a10,a%d", register_numl.get(), register_numl.get())); - } - } - - // globalType.pop(); - } - } - varType *v = nd->getVarType(); - int start = nd->stack_pos; - // uint8_t regnum = 1; - /* YBA 25-02-2025 - if (nd->asPointer or (nd->isPointer)) // && nd->children_size() == 0)) - point_regnum++; - /*/ - if (nd->isPointer) - { - // start = nd->stack_pos; - // regnum = point_regnum; - } - // string body = ""; - // register_numl++; - // bufferText->addAfter(string_format("l32r a%d,%s", point_regnum, nd->_token->text.c_str())); - if (nd->isPointer && nd->children_size() > 0) // leds[g]; - { - // f=f+number.f; - if (nd->type == TokenUserDefinedVariableMember or nd->type == TokenUserDefinedVariableMemberFunction) - { - bufferText->addAfter(string_format("movi a%d,%d", point_regnum, nd->_total_size)); - bufferText->addAfter(string_format("mull a%d,a%d,a%d", register_numl.get(), register_numl.get(), point_regnum)); - bufferText->addAfter(string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); - bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, point_regnum, register_numl.get())); - } - else if (v->total_size > 4) - { - // string tmp=content.l->back(); - // content.l->pop_back(); - bufferText->addAfter(string_format("movi a%d,%d", point_regnum, v->total_size)); - bufferText->addAfter(string_format("mull a%d,a%d,a%d", register_numl.get(), register_numl.get(), point_regnum)); - bufferText->addAfter(string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); - bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, point_regnum, register_numl.get())); - } - else - { - bufferText->addAfter(string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); - for (int i = 0; i < v->total_size; i++) - { - bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, point_regnum, register_numl.get())); - } - } - } - else - { - bufferText->addAfter(string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); - } - bufferText->sp.push(bufferText->get()); - if (nd->asPointer) //(&d) - { - bufferText->addAfter(string_format("mov a%d,a%d", register_numl.get(), point_regnum)); - bufferText->sp.push(bufferText->get()); - } - else if ((nd->children_size() > 0 or !nd->isPointer) && nd->type != TokenUserDefinedVariableMemberFunction) // leds[h] or h h being global) - { - // if (nd->target == EOF_TEXTARRAY) - // { - for (int i = 0; i < v->size; i++) - { - // bufferText->addAfter(string_format("%s %s%d,%s%d,%d", v->load[i].c_str(), v->reg_name.c_str(), register_numl.get(), v->reg_name.c_str(), point_regnum, start)); - asmInstruction asmInstr = v->load[i]; - bufferText->addAfter(string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), point_regnum, start)); - // register_numl--; - start += v->sizes[i]; - bufferText->sp.push(bufferText->get()); + break; + default: + break; } - // } - /* - else - { + } - int i = findMember(nd->_vartype, string(nd->getTargetText())); - int pos = 0; - v = &_userDefinedTypes[nd->_vartype]; - // printf(" we try to find %s %d\r\n", nd->getTargetText(), i); - if (i > -1) - { - // nd->getVarType() = &_varTypes[v->types[i]]; - // nd->_vartype=(uint8_t)v->types[i]; - start = nd->stack_pos + v->starts[i]; - for (int h = 0; h < i; h++) - { - pos += v->memberSize[h]; - // //printf("pos %d\r\n", pos); - } - - for (int l = pos; l <= v->memberSize[i] - 1 + pos; l++) - { - // //printf("start gloabl %d\r\n", start); - bufferText->addAfter(string_format("%s %s%d,%s%d,%d", asmInstructionsName[v->load[l]].c_str(), getRegType(v->load[l], 0).c_str(), register_numl.get(), getRegType(v->load[l], 1).c_str(), point_regnum, start)); - start += v->sizes[l]; - bufferText->sp.push(bufferText->get()); - } - } - else - { - return; - } - } - */ - // if(v->size==1) - // bufferText->sp.pop(); - } - else // s(leds) - { - bufferText->addAfter(string_format("mov a%d,a%d", register_numl.get(), point_regnum)); - bufferText->sp.push(bufferText->get()); - } - // res.f = f; - // res.header = number.header + h; - // point_regnum++; - register_numl.pop(); - // res.register_numl=register_numl; - // res.register_numr=register_numr; - register_numl.decrease(); - /* YBA 25-02-2025 - if (nd->asPointer or (nd->isPointer)) // && nd->children_size() == 0)) - point_regnum--; - */ - return; + if (regular) { + switch (nd->type) { + case TokenLessThan: + compop = "bge"; // greater or equal blt + // bufferText->addAfter( + // string_format("%s_end:\n",nd->target.c_str())); + break; + case TokenDoubleEqual: + compop = "bne"; // not equal beq + break; + case TokenNotEqual: + compop = "beq"; // equal + break; + case TokenMoreOrEqualThan: + compop = "blt"; // less then + break; + case TokenMoreThan: + compop = "bge"; // not equal + h = numl; + numl = leftl; + leftl = h; + break; + case TokenLessOrEqualThan: + compop = "blt"; // not equal + h = numl; + numl = leftl; + leftl = h; + break; + default: + break; + } + } + if (regular) + bufferText->addAfter(string_format("%s%s a%d,a%d,%s_end", + compop.c_str(), _add.c_str(), numl, + leftl, nd->getTargetText())); + else { + bufferText->blankCurrent(); + f = valBranchImmediate(f); + bufferText->addAfter(string_format("%s%s a%d,%d,%s_end", compop.c_str(), + _add.c_str(), leftl, f, + nd->getTargetText())); + } + // bufferText->addAfter(_compare.back()+1,string_format("j %s_end", + // nd->target.c_str())); + // bufferText->addAfter(_compare.back()+2,string_format("%s_if:", + // nd->target.c_str())); + register_numl.increase(); + } + } + // printf("out comparator\n"); } -void _visitcallConstructorNode(NodeToken *nd) -{ - int size = nd->_total_size / nd->getVarType()->total_size; +void _visitCallFunctionTemplate(NodeToken *nd, int regbase, bool isExtCall) { - if (nd->stack_pos > 0) - { - bufferText->addAfter(string_format("addi a5,a1,%d", nd->stack_pos)); - } + int staack_offset = (nd->getChildAtPos(0)->children_size() - 7) * 4; + bool convert = true; + bool isArg = false; + int nbfloat = 0; + if (nd == NULL) { + return; + } + bool saveinstack[20]; + for (int i = 0; i < 20; i++) { + if (isExtCall) + saveinstack[i] = false; else - { - bufferText->addAfter(string_format("l32r a5,@_%s", nd->getTokenText())); - } - if (size > 1) - { - bufferText->addAfter("movi a6,0"); - bufferText->addAfter(string_format("loop_label_%d:", for_if_num)); - } - bufferText->addAfter("mov a10,a5"); - // bufferText->addAfter("mov a10,a2"); - bufferText->addAfter(string_format("call8 @_%s._@%s()", nd->getVarType()->varName.c_str(), nd->getVarType()->varName.c_str())); - if (size > 1) - { - bufferText->addAfter(string_format("addi a5,a5,%d", nd->getVarType()->total_size)); - bufferText->addAfter("addi a6,a6,1"); - bufferText->addAfter(string_format("movi a7,%d", size)); - - bufferText->addAfter(string_format("bne a7,a6,loop_label_%d", for_if_num)); - for_if_num++; - } -} - -void _visitlocalVariableNode(NodeToken *nd) -{ - // printf("in lcoall\n"); + saveinstack[i] = false; + if (i >= 4) + saveinstack[i] = true; + } + NodeToken *func = _functions[nd->target]; + + NodeToken *t = nd; // cntx.findFunction(nd->_token); + if (t == NULL) { + // globalType.pop(); + return; + } + // printf(" %s %d %d\n",nd->_token->text.c_str(), + // t->children_size(),t->getChildAtPos(1)->children_size()); for (int i = 0; i + // < t->getChildAtPos(1)->children_size(); i++) + + // printf("number of arg %s %d\r\n", nd->getTokenText(), + // nd->findMaxArgumentSize()); + for (int i = t->getChildAtPos(0)->children_size() - 1; i >= 0; i--) { + // printf("***number of arg %d %d\r\n", i, + // nd->getChildAtPos(2)->getChildAtPos(i)->findMaxArgumentSize()); + bool save_in_stack = false; + for (int j = 0; j < i; j++) { + if (nd->getChildAtPos(0)->getChildAtPos(j)->findMaxArgumentSize() - 1 >= + i) { + save_in_stack = true; + } + } + if (isExtCall) { + if (i == 0) + save_in_stack = false; + } else { + if (i < 2) + save_in_stack = false; + } + saveinstack[i] = save_in_stack; + if (i >= 4) { + save_in_stack = true; + saveinstack[i] = true; + } + if (i >= 6) { + save_in_stack = false; + saveinstack[i] = false; + } + if (t->getChildAtPos(0)->getChildAtPos(i)->isPointer) { + register_numl.duplicate(); + nd->getChildAtPos(0)->getChildAtPos(i)->visitNode(); + register_numl.pop(); + if (save_in_stack == true) { + bufferText->addAfter(string_format( + "s32i a%d,a1,%d", register_numl.get(), i * 4 + _START_2)); + } else { + bufferText->addAfter( + string_format("mov a%d,a%d", regbase + i, register_numl.get())); + } + } else + + { + if (i < func->getChildAtPos(1)->children_size()) { + if (func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->_varType == + __Args__) + convert = false; - if (nd->asPointer) - { register_numl.duplicate(); - varType *v = nd->getVarType(); - int start = nd->stack_pos; - // //printf("kzlekmze\n"); - // bufferText->addAfter(string_format("l32r a%d,stack", point_regnum)); - - // if( if (nd->isPointer && nd->children_size() > 0)) - - if (nd->type == TokenUserDefinedVariableMember or nd->type == TokenUserDefinedVariableMemberFunction) - { - if (!nd->isPointer) - { - bufferText->addAfter(string_format("addi a%d,a1,%d", register_numl.get(), start - (int)(start / 1000) * 1000)); - bufferText->addAfter(string_format("l32i a%d,a%d,%d", register_numl.get(), register_numl.get(), (start / 1000))); + globalType.push( + func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->_varType); + nd->getChildAtPos(0)->getChildAtPos(i)->visitNode(); + register_numl.pop(); + } else { + register_numl.duplicate(); + // globalType.push(t->getChildAtPos(2)->getChildAtPos(i)->getVarType()->_varType); + nd->getChildAtPos(0)->getChildAtPos(i)->visitNode(); + register_numl.pop(); + } + if (nd->getChildAtPos(0)->getChildAtPos(i)->getVarType() != NULL and + convert) + translateType( + globalType.get(), + nd->getChildAtPos(0)->getChildAtPos(i)->getVarType()->_varType, + register_numl.get()); + varTypeEnum _vartype; + NodeToken *l; + if (i < func->getChildAtPos(1)->children_size()) { + l = func->getChildAtPos(1); + _vartype = + func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->_varType; + if (_vartype == __Args__) { + _vartype = + t->getChildAtPos(0)->getChildAtPos(i)->getVarType()->_varType; + isArg = true; + /* + bufferText->pop(); + bufferText->addAfter(string_format("l32i a12,a8")); + bufferText->addAfter(string_format("mov a12,a8")); + bufferText->addAfter(string_format("addi a8,a8,4")); + bufferText->addAfter(string_format("mov a13,a8")); + */ + } + } else { + l = t->getChildAtPos(0); + _vartype = + t->getChildAtPos(0)->getChildAtPos(i)->getVarType()->_varType; + } + + if (_vartype == __float__) { + + if (isArg) { + } else { + if (save_in_stack == true) { + bufferText->addAfter(string_format( + "ssi f%d,a1,%d", register_numl.get(), i * 4 + _START_2)); + } else { + bufferText->addAfter( + string_format("rfr a%d,f%d", regbase + i, register_numl.get())); + } + } + } else if (l->getChildAtPos(i)->getVarType()->_varType == __CRGB__ or + l->getChildAtPos(i)->getVarType()->_varType == __CRGBW__) { + // bufferText->addAfter( bufferText->sp.pop(),string_format("mov + // a%d,a%d", 10 + i, register_numl.get())); + if (t->getChildAtPos(0) + ->getChildAtPos(i) + ->getChildAtPos(0) + ->_nodetype == numberNode) { + + if (save_in_stack == true) { + for (int k = 0; k < 3; k++) { + bufferText->addAfter(bufferText->sp.pop(), + string_format("s8i a%d,a1,%d", + register_numl.get(), + i * 4 + _START_2 + k)); } - else - { - // bufferText->addAfter(string_format("l32i a%d,a1,%d", register_numl.get(),start-(int)(start/1000)*1000)); - asmInstruction asmInstr = v->load[0]; - // bufferText->addAfter(string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), register_numl.get(), start/1000)); - bufferText->addAfter(string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), 2, start / 1000)); - translateType(globalType.get(), v->_varType, register_numl.get()); - // bufferText->addAfter(string_format("l16si a%d,a%d,%d", register_numl.get(),register_numl.get(),start/1000)); + // bufferText->addAfter(string_format("l32i a%d,a1,%d",10+i,i * 4 + + // _START_2)); + } + + else { + for (int k = 2; k >= 0; k--) { + bufferText->addAfter(bufferText->sp.pop(), + string_format("s8i a%d,a1,%d", + register_numl.get(), + i * 4 + _START_2 + k)); } - } + bufferText->addAfter( + string_format("l32i a%d,a1,%d", regbase + i, i * 4 + _START_2)); + } + } else { + + for (int k = 0; k < t->getChildAtPos(0) + ->getChildAtPos(i) + ->getChildAtPos(0) + ->getVarType() + ->size; + k++) { + // bufferText->addAfter(string_format("mov a15,a10")); + // bufferText->addAfter(bufferText->sp.pop(),string_format("slli + // a%d,a%d,%d", 10+i,register_numl.get(), k* 8)); register_numl--; + bufferText->pop(); + } + if (t->getChildAtPos(0) + ->getChildAtPos(i) + ->getChildAtPos(0) + ->_nodetype == callFunctionNode) { + if (save_in_stack == true) { + bufferText->addAfter(bufferText->sp.pop(), + string_format("l32i a%d,a8,0", regbase + i)); + bufferText->addAfter(string_format("s32i a%d,a1,%d", regbase + i, + i * 4 + _START_2)); + } else { + bufferText->addAfter(bufferText->sp.pop(), + string_format("l32i a%d,a8,0", 10 + i)); + } + } + if (t->getChildAtPos(0) + ->getChildAtPos(i) + ->getChildAtPos(0) + ->_nodetype == extCallFunctionNode) { + if (save_in_stack == true) { + // bufferText->addAfter(bufferText->sp.pop(), string_format("mov + // a%d,a10", 10 + i)); + bufferText->addAfter( + string_format("s32i a10,a1,%d", i * 4 + _START_2)); + } else { + bufferText->addAfter(bufferText->sp.pop(), + string_format("mov a%d,a10", regbase + i)); + } + } else if (t->getChildAtPos(0) + ->getChildAtPos(i) + ->getChildAtPos(0) + ->_nodetype == localVariableNode) { + if (save_in_stack == true) { + bufferText->addAfter(bufferText->sp.pop(), + string_format("l32i a%d,a1,%d", regbase + i, + t->getChildAtPos(0) + ->getChildAtPos(i) + ->getChildAtPos(0) + ->stack_pos)); + bufferText->addAfter(string_format("s32i a%d,a1,%d", regbase + i, + i * 4 + _START_2)); + } else { + bufferText->addAfter(bufferText->sp.pop(), + string_format("l32i a%d,a1,%d", regbase + i, + t->getChildAtPos(0) + ->getChildAtPos(i) + ->getChildAtPos(0) + ->stack_pos)); + } + } else if (t->getChildAtPos(2) + ->getChildAtPos(i) + ->getChildAtPos(0) + ->_nodetype == globalVariableNode) { + // tobe done + if (save_in_stack == true) { + bufferText->addAfter(bufferText->sp.pop(), + string_format("l32i a%d,a5,0", regbase + i)); + bufferText->addAfter(string_format("s32i a%d,a1,%d", regbase + i, + i * 4 + _START_2)); + } else { + bufferText->addAfter(bufferText->sp.pop(), + string_format("l32i a%d,a5,0", regbase + i)); + } + } + } + } + + else { + if (save_in_stack == true) { + bufferText->addAfter(string_format( + "s32i a%d,a1,%d", register_numl.get(), i * 4 + _START_2)); + } else { + // to change + if (i >= 6) { + bufferText->addAfter(string_format( + "s32i a%d,a1,%d", register_numl.get(), staack_offset)); + staack_offset -= 4; + } else { + bufferText->addAfter( + string_format("mov a%d,a%d", regbase + i, register_numl.get())); + } + } + } + + globalType.pop(); + } + } + + // bufferText->end(); + for (int i = 0; i < nd->getChildAtPos(0)->children_size(); i++) { + if (i < 7) { + if (saveinstack[i] == true) { + varType *v; + if (i < func->getChildAtPos(1)->children_size()) + v = func->getChildAtPos(1)->getChildAtPos(i)->getVarType(); else - { - bufferText->addAfter(string_format("addi a%d,a1,%d", register_numl.get(), start)); + v = nd->getChildAtPos(0)->getChildAtPos(i)->getVarType(); + if (v->_varType == __none__) { + v = &_varTypes[__uint32_t__]; } - register_numl.decrease(); - if (nd->isPointer && nd->children_size() > 0) - { - register_numl.duplicate(); - nd->getChildAtPos(0)->visitNode(); - register_numl.pop(); - for (int i = 0; i < v->total_size; i++) - { - bufferText->addAfter(string_format("add a%d,a%d,a%d", register_numl.get() + 1, register_numl.get() + 1, register_numl.get())); - } - register_numl.increase(); + + if (v->_varType == __float__) { + bufferText->addAfter( + string_format("l32i a%d,a1,%d", regbase + i, i * 4 + _START_2)); + } else { + bufferText->addAfter(string_format( + "%s %s%d,%s1,%d", asmInstructionsName[v->load[0]].c_str(), + getRegType(v->load[0], 0).c_str(), regbase + i, + getRegType(v->load[0], 1).c_str(), i * 4 + _START_2)); } - bufferText->sp.push(bufferText->get()); - register_numl.pop(); - register_numl.decrease(); - // //printf("kzlekmze2\n"); - return; + } } - register_numl.duplicate(); - // if (nd->children_size() > 0) - //{ - // number = nd->getChildAtPos(0)->visitNode(nd->getChildAtPos(0), register_numl, register_numr); - // } + } - if (nd->children_size() > 0) - { - globalType.push(__int__); - register_numl.duplicate(); - nd->getChildAtPos(0)->visitNode(); - register_numl.pop(); - globalType.pop(); - } - varType *v = nd->getVarType(); - int start = nd->stack_pos; - uint8_t regnum = 1; - if (nd->asPointer or (nd->isPointer && nd->children_size() == 0)) - point_regnum++; - // uint8_t save_reg; - // point_regnum++; - if (nd->isPointer) - { - // start = nd->stack_pos; - regnum = point_regnum; - } - if (nd->isPointer) - { - int start = nd->stack_pos; + if (isExtCall) { + bufferText->addAfter(string_format("callExt a8,@_%s", nd->getTokenText())); + } else { + // bufferText->addAfter(string_format("mov a10,a2")); + bufferText->addAfter(string_format("call8 @_%s", nd->getTokenText())); + } + bufferText->sp.push(bufferText->get()); - // bufferText->addAfter(string_format("addi a%d,a1,%d", point_regnum, start)); + return; +} - if (nd->children_size() == 0) - { - if (nd->type == TokenUserDefinedVariableMemberFunction) - bufferText->addAfter(string_format("addi a%d,a1,%d", register_numl.get(), start)); - else - bufferText->addAfter(string_format("l32i a%d,a1,%d", register_numl.get(), start)); - // bufferText->addAfter(string_format("mov a%d,a%d", register_numl.get(), point_regnum)); +void _visitcallFunctionNode(NodeToken *nd) { + // printf("compiling call function %s\n", nd->getTokenText()); + NodeToken *t = nd; // cntx.findFunction(nd->_token); + NodeToken *func = _functions[nd->target]; + if (func->getChildAtPos(1)->children_size() >= + _TRIGGER) { // printf("token type + // %d\r\n",nd->_link->getChildAtPos(0)->_token->_vartype->_varType); + // printf("we ar ehere\n"); + if (t == NULL) { + return; + } + + // if(t->getChildAtPos(1)->children_size()<1) + // return;point_regnum + int save = 9; // point_regnum; + // bufferText->addAfterNoDouble(string_format("l32r + // a%d,@_stack_%s", save,nd->getTokenText())); // point_regnum + if (func->getChildAtPos(1)->children_size() == 0 and + t->type == TokenUserDefinedVariableMemberFunction) { + bufferText->addAfter(bufferText->sp.pop(), string_format("mov a10,a2")); + } + for (int i = 0; i < func->getChildAtPos(1)->children_size(); i++) { + // printf("calll \r\n"); + // isPointer = false; + if (func->getChildAtPos(1)->getChildAtPos(i)->isPointer) { + // printf("calll p\r\n"); + // isPointer = true; + register_numl.duplicate(); + nd->getChildAtPos(0)->getChildAtPos(i)->visitNode(); + register_numl.pop(); + int start = + func->getChildAtPos(1)->getChildAtPos(i)->stack_pos - _STACK_SIZE; + + if (t->type == TokenUserDefinedVariableMemberFunction and i == 0) { + // printf("*******onr en ocoi******\r\n"); + if (!isStructFunction) { + bufferText->addAfter( + bufferText->sp.pop(), + string_format("mov a10,a%d", register_numl.get())); + } else { + // bufferText->pop(); + bufferText->addAfter(bufferText->sp.pop(), + string_format("mov a10,a2")); + } + } else { + bufferText->addAfter(bufferText->sp.pop(), + string_format("s32i a%d,a%d,%d", + register_numl.get(), save, + start)); // point_regnum + bufferText->addBefore(string_format( + "l32r a%d,@_stack_%s", save, nd->getTokenText())); // point_regnum + } // isPointer=false; + } else { + + globalType.push( + func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->_varType); + register_numl.duplicate(); + // printf("in callf:"); + nd->getChildAtPos(0)->getChildAtPos(i)->visitNode(); + // printf("\n"); + register_numl.pop(); + int start = + func->getChildAtPos(1)->getChildAtPos(i)->stack_pos - _STACK_SIZE + + func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->total_size; + int tot = + func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->size - 1; + for (int j = 0; + j < func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->size; + j++) { + + if (nd->getChildAtPos(0)->getChildAtPos(i)->getVarType() != NULL) { + translateType( + globalType.get(), + nd->getChildAtPos(0)->getChildAtPos(i)->getVarType()->_varType, + register_numl.get()); + bufferText->sp.pop(); bufferText->sp.push(bufferText->get()); - } - else - { - bufferText->addAfter(string_format("l32i a%d,a1,%d", point_regnum, start)); - start = 0; - for (int i = 0; i < v->total_size; i++) - { - bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, point_regnum, register_numl.get())); + } else { + // translateType(globalType.get(), + // nd->getChildAtPos(0)->getChildAtPos(i)->_token->_varType, + // register_numl.get()); + } + + start -= func->getChildAtPos(1) + ->getChildAtPos(i) + ->getVarType() + ->sizes[tot - j]; + asmInstruction asmInstr = func->getChildAtPos(1) + ->getChildAtPos(i) + ->getVarType() + ->store[tot - j]; + // bufferText->addAfter(bufferText->sp.pop(), string_format("%s + // %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), + // getRegType(asmInstr, 0).c_str(), register_numl.get(), + // getRegType(asmInstr, 1).c_str(), point_regnum, start)); + int sav; + // if (j == t->getChildAtPos(1)->getChildAtPos(i)->getVarType()->size + // - 1) + if (!intest) { + sav = bufferText->get(); + bufferText->addAfter( + bufferText->sp.pop(), + string_format( + "%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), save, start)); + if (j == + func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->size - + 1) { + // bufferText->sp.push(bufferText->get()); + + bufferText->addAfter( + sav, + string_format("l32r a%d,@_stack_%s", save, + nd->getTokenText())); // point_regnum + // bufferText->addAfter(sav, + // string_format("l32r + // a%d,@_stack",save)); } - bufferText->sp.push(bufferText->get()); - for (int i = 0; i < v->size; i++) - { - // bufferText->addAfter(string_format("%s %s%d,%s%d,%d", v->load[i].c_str(), v->reg_name.c_str(), register_numl.get(), v->reg_name.c_str(), regnum, start)); - asmInstruction asmInstr = v->load[i]; - bufferText->addAfter(string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), point_regnum, start)); - translateType(globalType.get(), v->_varType, register_numl.get()); - // register_numl--; - start += v->sizes[i]; - bufferText->sp.push(bufferText->get()); + } else { + // sav = bufferText->get(); + bufferText->addAfter(string_format( + "%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), save, start)); + if (j == + func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->size - + 1) { + // bufferText->sp.push(bufferText->get()); + + bufferText->addBefore(string_format( + "l32r a%d,@_stack_%s", save, + nd->getTokenText())); // point_regnum + // bufferText->putIteratorAtPos(bufferText->get()+1); + // // bufferText->addAfter(sav, + // string_format("l32r + // a%d,@_stack",save)); } + } + // start+=t->getChildAtPos(1)->getChildAtPos(i)->_token->_vartype->sizes[j]; } - if (nd->asPointer or (nd->isPointer && nd->children_size() == 0)) - point_regnum--; - register_numl.pop(); - register_numl.decrease(); + globalType.pop(); + } + // bufferText->addAfter(string_format("addi + // a%d,a1,%d",11+i,t->getChildAtPos(1)->getChildAtPos(i)->stack_pos)); + } + // bufferText->addAfter(string_format("mov a10,a2")); // neded to find the + // external variables !!!!!! + bufferText->addAfter(string_format("call8 @_%s", nd->getTokenText())); + } else { + _visitCallFunctionTemplate(nd, 10, false); + } + int start = nd->stack_pos; + // printf("ini\r\n"); + varType *v = func->getChildAtPos(0)->getVarType(); + // printf("ini\r\n"); + if (v == NULL) { + printf("nodeToken %d\r\n", func->getChildAtPos(0)->type); + printf("NULL\r\n"); + } + + if (v->size > 1) { + // printf("ini size\r\n"); + bufferText->addAfter(string_format("l32r a%d,@_stackr", 8)); // point_regnum + for (int i = 0; i < v->size; i++) { + // bufferText->addAfter(string_format("mov a15,a10")); + // bufferText->addAfter(string_format("%s %s%d,%s%d,%d", + // v->load[i].c_str(), v->reg_name.c_str(), register_numl.get(), + // v->reg_name.c_str(), point_regnum, start)); + asmInstruction asmInstr = v->load[i]; + // printf("tryin to get %d %d\r\n",i,asmInstr); + bufferText->addAfter(string_format( + "%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), 8, start)); // point_regnum + // register_numl--; + start += v->sizes[i]; + // bufferText->sp.push(bufferText->get()); } - else - { - // printf("jj\n"); - for (int i = 0; i < v->size; i++) - { - // bufferText->addAfter(string_format("%s %s%d,%s%d,%d", v->load[i].c_str(), v->reg_name.c_str(), register_numl.get(), v->reg_name.c_str(), regnum, start)); - asmInstruction asmInstr = v->load[i]; - bufferText->addAfter(string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), regnum, start)); - // printf("jj2 :%s\n",string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), regnum, start).c_str()); - translateType(globalType.get(), v->_varType, register_numl.get()); - // printf("jj3\n"); - // register_numl--; - start += v->sizes[i]; - bufferText->sp.push(bufferText->get()); - // printf("jj4\n"); - } - register_numl.pop(); - // printf("jj5\n"); - register_numl.decrease(); - // printf("jj6\n"); - - return; + register_numl.decrease(); + } else if (v->size > 0) { + if (v->_varType == __float__) { + bufferText->addAfter(string_format("mov.s f%d,f2", register_numl.get())); + } else { + bufferText->addAfter(string_format("mov a%d,a10", register_numl.get())); } + // bufferText->sp.push(bufferText->get()); + bufferText->sp.push(bufferText->get()); + register_numl.decrease(); + } } - -void _visitblockStatementNode(NodeToken *nd) -{ - register_numr.clear(); - register_numl.clear(); - register_numl.push(15); - register_numr.push(15); - - register_numl.push(15); - register_numr.push(15); - for (int i = 0; i < nd->children_size(); i++) - { - - nd->getChildAtPos(i)->visitNode(); - // f = f + g.f; - } - // nd->clear(); -} -void _visitdefFunctionNode(NodeToken *nd) -{ - // printf("compiling %s\n", nd->getTokenText()); - bufferText = &content; - isStructFunction = false; - if (nd->type == TokenUserDefinedVariableMemberFunction) - isStructFunction = true; - header.addAfter(string_format(".global @_%s", nd->getTokenText())); - if (!isStructFunction) - header.addAfter(string_format(".global @__%s", nd->getTokenText())); - // string variables = ""; - if (!isStructFunction) - { - string variables = ""; - for (int i = 0; i < nd->getChildAtPos(1)->children_size(); i++) - { - variables = string_format("%s %d", variables.c_str(), nd->getChildAtPos(1)->getChildAtPos(i)->getVarType()->total_size); - } - header.addAfter(string_format(".var %d%s", nd->getChildAtPos(1)->children_size(), variables.c_str())); - } - if (nd->getChildAtPos(1)->children_size() > -1) - { - header.addAfter(string_format("@_stack__%s:", nd->getTokenText())); - header.addAfter(string_format(".bytes %d", (nd->getChildAtPos(1)->children_size() + 1) * 4)); - } - if (!isStructFunction) - { - bufferText->addAfter(string_format("@__%s:", nd->getTokenText())); - - NodeToken *variaToken = nd->getChildAtPos(1); - if (variaToken->children_size() > 0) - { - bufferText->addAfter(string_format("entry a1,%d", ((nd->stack_pos) / 8 + 1) * 8 + 16 + _STACK_SIZE)); // ((nd->stack_pos) / 8 + 1) * 8+20) - bufferText->addAfter(string_format("l32r a9,@_stack__%s", nd->getTokenText())); - } - for (int k = 0; k < variaToken->children_size(); k++) - { - // int start = variaToken->getChildAtPos(k)->stack_pos; - - // printf("ee p\r\n"); - int start = variaToken->getChildAtPos(k)->stack_pos; - if (start < _STACK_SIZE) - start = _STACK_SIZE; - for (int j = 0; j < variaToken->getChildAtPos(k)->getVarType()->size; j++) - { - asmInstruction asmInstr = variaToken->getChildAtPos(k)->getVarType()->load[0]; - bufferText->addAfter(string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), k + 10, getRegType(asmInstr, 1).c_str(), 9, start - _STACK_SIZE)); // point_regnum - // asmInstruction asmInstr = variaToken->getChildAtPos(k)->getVarType()->store[0]; - // bufferText->addAfter(string_format("%s %s%d,%s9,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), k+10,getRegType(asmInstr, 1).c_str(), start)); - start += variaToken->getChildAtPos(k)->getVarType()->sizes[j]; - } - //} - } - if (variaToken->children_size() > 0) - { - bufferText->addAfter(string_format("call8 @_%s", nd->getTokenText())); - bufferText->addAfter(string_format("retw.n", nd->getTokenText())); - } - } - - bufferText->addAfter(string_format("@_%s:", nd->getTokenText())); - bufferText->addAfter(string_format("entry a1,%d", ((nd->stack_pos) / 8 + 1) * 8 + 16 + _STACK_SIZE)); // ((nd->stack_pos) / 8 + 1) * 8+20) - int sav = 9; -#if _TRIGGER == 0 - bufferText->addAfterNoDouble(string_format("l32r a%d,@_stack_%s", sav, nd->getTokenText())); -#endif - if (saveReg) - { - bufferText->addAfter("ssi f15,a1,16"); - bufferText->addAfter("ssi f14,a1,20"); - bufferText->addAfter("ssi f13,a1,24"); - } - if (saveRegAbs) - { - bufferText->addAfter("s32i a15,a1,16"); - bufferText->addAfter("s32i a14,a1,20"); - bufferText->addAfter("s32i a13,a1,24"); - } - for (int i = 1; i < nd->children_size(); i++) - { - - nd->getChildAtPos(i)->visitNode(); - // f = f + g.f; - // h = h + g.header; - } - - if (saveReg) - { - bufferText->addAfter("lsi f15,a1,16"); - bufferText->addAfter("lsi f14,a1,20"); - bufferText->addAfter("lsi f13,a1,24"); - } - if (saveRegAbs) - { - bufferText->addAfter("l32i a15,a1,16"); - bufferText->addAfter("l32i a14,a1,20"); - bufferText->addAfter("l32i a13,a1,24"); - } - bufferText->addAfter(string_format("retw.n")); - - isStructFunction = false; - bufferText = &footer; -} -void _visitstatementNode(NodeToken *nd) -{ - point_regnum = 5; - // printf("visit statement\n"); - register_numr.clear(); - register_numl.clear(); - register_numl.push(15); - register_numr.push(15); - - register_numl.push(15); - register_numr.push(15); - for (int i = 0; i < nd->children_size(); i++) - { - - register_numl.duplicate(); - nd->getChildAtPos(i)->visitNode(); - register_numl.pop(); - } - // nd->clear(); - // printf("end statement\n"); - //_node_token_stack.clear(); -} -void _visitprogramNode(NodeToken *nd) -{ - // - // printf("visit program\n"); - point_regnum = 5; - - // content.clear(); - // header.clear(); - content.begin(); - header.begin(); - footer.begin(); - - // header.addAfter("@_stack:"); - // header.addAfter(".bytes 60"); - header.addBefore("@__handle_:"); - header.addBefore(".bytes 4"); - header.addBefore("@__execaddr_:"); - header.addBefore(".bytes 4"); - header.addBefore("@__sync:"); - header.addBefore(".bytes 4"); - // header.addAfter("@_stackr:"); - // header.addAfter(".bytes 32"); - - footer.addBefore(" "); - // footer.addAfter("@__footer:"); - // footer.addAfter("entry a1,144"); - - // header.addAfter("__basetime:"); - // header.addAfter(".bytes 4"); - register_numr.clear(); - register_numl.clear(); - register_numl.push(15); - register_numr.push(15); - bufferText = &footer; - for (int i = 0; i < nd->children_size(); i++) - { - -#ifndef __MEM_PARSER - if (nd->getChildAtPos(i)->_nodetype != defFunctionNode && nd->getChildAtPos(i)->_nodetype != defAsmFunctionNode) - { -#endif - nd->getChildAtPos(i)->visitNode(); -#ifndef __MEM_PARSER - } // NEW -#endif - } - - // footer.addAfter("retw.n"); - - if (footer.size() > 1) - { - header.addAfter(".global @__footer"); - footer.addAfter("retw.n"); - footer.begin(); - - footer.addBefore("entry a1,144"); - footer.begin(); - footer.addBefore("@__footer:"); - } - - if (addfloatdivision) - { - header.addAfter(" .global @___div(d|d)"); - header.addAfter("@_stack___div(d|d):"); - header.addAfter(".bytes 12"); - content.end(); - for (int i = 0; i < _div_size; i++) - { - content.addAfter(string(_div[i])); - } - } -} -void _visitassignementNode(NodeToken *nd) -{ - // printf("entre assignemen\n") ; - point_regnum = 5; // YBA 25-02-2025 - bufferText->sp.clear(); - bufferText->sp.push(bufferText->get()); - register_numl.duplicate(); - - if (nd->children_size() > 1) - globalType.push(nd->getChildAtPos(0)->getVarType()->_varType); - - if (nd->children_size() > 1) - { - register_numl.duplicate(); - nd->getChildAtPos(1)->visitNode(); - register_numl.pop(); - - if (nd->getChildAtPos(1)->getVarType() != NULL) - { - - translateType(globalType.get(), nd->getChildAtPos(1)->getVarType()->_varType, register_numl.get()); - // printf("retour on push\n") ; - // } - } - - bufferText->sp.pop(); - bufferText->sp.push(bufferText->get()); - - point_regnum++; - } - register_numl.duplicate(); - nd->getChildAtPos(0)->visitNode(); - register_numl.pop(); - - register_numl.pop(); - bufferText->sp.pop(); - globalType.pop(); - // clearNodeToken(nd); // new - // nd->clear(); - register_numl.clear(); - register_numl.push(15); - register_numl.push(15); - - register_numr.clear(); - register_numr.push(15); - register_numr.push(15); -} -void _visitternaryIfNode(NodeToken *nd) -{ - - register_numl.duplicate(); - nd->getChildAtPos(0)->visitNode(); - register_numl.pop(); - bufferText->addAfter(string_format("beqz a%d,%s", register_numl.get(), nd->getTargetText())); - /* - register_numr.clear(); - register_numl.clear(); - register_numl.push(15); - register_numr.push(15); - - register_numl.push(15); - register_numr.push(15); - -*/ - register_numl.duplicate(); - nd->getChildAtPos(1)->visitNode(); - register_numl.pop(); - bufferText->addAfter(string_format("j %s_end", nd->getTargetText())); - bufferText->addAfter(string_format("%s:", nd->getTargetText())); - /* - register_numr.clear(); - register_numl.clear(); - register_numl.push(15); - register_numr.push(15); - - register_numl.push(15); - register_numr.push(15); - */ - register_numl.duplicate(); - nd->getChildAtPos(2)->visitNode(); - register_numl.pop(); - bufferText->addAfter(string_format("%s_end:", nd->getTargetText())); -} - -bool isBranchImmediate(int val, bool inverse) -{ - if (!inverse) - { - switch (val) - { - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - case 8: - case 10: - case 12: - case 16: - case 32: - case 64: - case 128: - case 256: - return true; - break; - default: - return false; - break; - } - } - else - { - switch (val) - { - // case 1: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - case 9: - case 11: - case 15: - case 31: - case 63: - case 127: - case 255: - return true; - break; - default: - return false; - break; - } - } -} - -int valBranchImmediate(int val) -{ - switch (val) - { - case 1: - return 1; - break; - case 2: - return 2; - break; - case 3: - return 3; - break; - case 4: - return 4; - break; - case 5: - return 5; - break; - case 6: - return 6; - break; - case 7: - return 7; - break; - case 8: - return 8; - break; - case 10: - return 9; - break; - case 12: - return 10; - break; - case 16: - return 11; - break; - case 32: - return 12; - break; - case 64: - return 13; - break; - case 128: - return 14; - break; - case 256: - return 14; - break; - default: - return 88; - break; - } -} - -void _visittestNode(NodeToken *nd) -{ - - int numl = register_numl.get(); - - if (nd->getChildAtPos(0)->_vartype == __float__) - nd->getChildAtPos(1)->_vartype = __float__; - if (nd->getChildAtPos(1)->_vartype == __float__) - nd->getChildAtPos(0)->_vartype = __float__; - string _add = ""; - if (nd->getChildAtPos(0)->_vartype == __uint32_t__ || nd->getChildAtPos(1)->_vartype == __uint32_t__) - _add = "u"; - // register_numl.duplicate(); - nd->getChildAtPos(0)->visitNode(); - // register_numl.pop(); - - int leftl = register_numl.get(); - - // register_numl.duplicate(); - nd->getChildAtPos(1)->visitNode(); - // register_numl.pop(); - - //////printf("compare %s %s\n",tokenNames[nd->_token->type ].c_str(),nd->_token->text.c_str()); - string compop = ""; - string compo2 = ""; - // to compose - int h = 999; - - if (nd->getChildAtPos(1)->_vartype == __float__) - { - switch (nd->type) - { - case TokenLessThan: - h = numl; - compop = "olt.s"; // greater or equal - // bufferText->addAfter( string_format("%s_end:\n",nd->target.c_str())); - compo2 = "bf"; - break; - case TokenDoubleEqual: - h = numl; - compop = "oeq.s"; // not equal - compo2 = "bf"; - break; - case TokenNotEqual: - h = numl; - compop = "oeq.s"; // equal - compo2 = "bt"; - break; - case TokenMoreOrEqualThan: - compop = "ole.s"; // less then - h = numl; - numl = leftl; - leftl = h; - compo2 = "bf"; - break; - case TokenMoreThan: - compop = "olt.s"; // not equal - h = numl; - numl = leftl; - leftl = h; - compo2 = "bf"; - break; - case TokenLessOrEqualThan: - h = numl; - compop = "ole.s"; // not equal - compo2 = "bf"; - - // compo2="bt"; - break; - default: - break; - } - bufferText->addAfter(string_format("%s b0,f%d,f%d", compop.c_str(), numl, leftl)); - bufferText->addAfter(string_format("%s b0,%s_end", compo2.c_str(), nd->getTargetText())); - bufferText->addAfter(string_format("movi a%d,1", h)); - bufferText->addAfter(string_format("j %s_end_", nd->getTargetText())); - bufferText->addAfter(string_format("%s_end:", nd->getTargetText())); - bufferText->addAfter(string_format("movi a%d,0", h)); - bufferText->addAfter(string_format("%s_end_:", nd->getTargetText())); - register_numl.increase(); - } - else - { - - switch (nd->type) - { - case TokenLessThan: - h = numl; - compop = "bge"; // greater or equal blt - // bufferText->addAfter( string_format("%s_end:\n",nd->target.c_str())); - break; - case TokenDoubleEqual: - h = numl; - compop = "bne"; // not equal beq - break; - case TokenNotEqual: - h = numl; - compop = "beq"; // equal - break; - case TokenMoreOrEqualThan: - h = numl; - compop = "blt"; // less then - break; - case TokenMoreThan: - compop = "bge"; // not equal - h = numl; - numl = leftl; - leftl = h; - break; - case TokenLessOrEqualThan: - compop = "blt"; // not equal - h = numl; - numl = leftl; - leftl = h; - break; - default: - break; - } - - bufferText->addAfter(string_format("%s%s a%d,a%d,%s_end", compop.c_str(), _add.c_str(), numl, leftl, nd->getTargetText())); - bufferText->addAfter(string_format("movi a%d,1", h)); - bufferText->addAfter(string_format("j %s_end_", nd->getTargetText())); - bufferText->addAfter(string_format("%s_end:", nd->getTargetText())); - bufferText->addAfter(string_format("movi a%d,0", h)); - bufferText->addAfter(string_format("%s_end_:", nd->getTargetText())); - register_numl.increase(); - } - // f = f + g.f; -} - -void _visitcomparatorNode(NodeToken *nd) -{ - // printf("in comparator\n"); - int numl = register_numl.get(); - - if (nd->getChildAtPos(0)->_nodetype != testNode) - { - // on va tester si on est >0; - nd->getChildAtPos(0)->visitNode(); - if (nd->_total_size > 116) - { - bufferText->addAfter(string_format("bnez a%d,%s_if", numl, nd->parent->getTargetText())); - bufferText->addAfter(string_format("j %s_end", nd->parent->getTargetText())); - bufferText->addAfter(string_format("%s_if:", nd->parent->getTargetText())); - register_numl.increase(); - } - else - { - bufferText->addAfter(string_format("beqz a%d,%s_end", numl, nd->parent->getTargetText())); - // bufferText->addAfter(_compare.back()+1,string_format("j %s_end", nd->target.c_str())); - // bufferText->addAfter(_compare.back()+2,string_format("%s_if:", nd->target.c_str())); - register_numl.increase(); - } - - return; - } - // printf("test node\n\r"); - // nd->getChildAtPos(0)->visitNode(); - nd = nd->getChildAtPos(0); - nd->_total_size = nd->parent->_total_size; - nd->setTargetText(string(nd->parent->getTargetText())); - if (nd->getChildAtPos(0)->_vartype == __float__) - nd->getChildAtPos(1)->_vartype = __float__; - if (nd->getChildAtPos(1)->_vartype == __float__) - nd->getChildAtPos(0)->_vartype = __float__; - // register_numl.duplicate(); - string _add = ""; - if (nd->getChildAtPos(0)->_vartype == __uint32_t__ || nd->getChildAtPos(1)->_vartype == __uint32_t__) - _add = "u"; - nd->getChildAtPos(0)->visitNode(); - // register_numl.pop(); - - int leftl = register_numl.get(); - - // register_numl.duplicate(); - nd->getChildAtPos(1)->visitNode(); - // register_numl.pop(); - - //////printf("compare %s %s\n",tokenNames[nd->_token->type ].c_str(),nd->_token->text.c_str()); - string compop = ""; - string compo2 = ""; - // to compose - int h; - - if (nd->getChildAtPos(1)->_vartype == __float__) - { - - if (nd->_total_size > 116) - { - switch (nd->type) - { - case TokenLessThan: - compop = "olt.s"; // greater or equal - // h = numl; - // numl = leftl; - // leftl = h; - // bufferText->addAfter( string_format("%s_end:\n",nd->target.c_str())); - compo2 = "bt"; - break; - case TokenDoubleEqual: - compop = "oeq.s"; // not equal - compo2 = "bt"; - break; - case TokenNotEqual: - compop = "oeq.s"; // equal - compo2 = "bf"; - break; - case TokenMoreOrEqualThan: - compop = "ole.s"; // less then - h = numl; - numl = leftl; - leftl = h; - compo2 = "bt"; - - break; - case TokenMoreThan: - compop = "olt.s"; // not equal - h = numl; - numl = leftl; - leftl = h; - compo2 = "bt"; - break; - case TokenLessOrEqualThan: - compop = "ole.s"; // not equal - compo2 = "bt"; - break; - default: - break; - } - bufferText->addAfter(string_format("%s b0,f%d,f%d", compop.c_str(), numl, leftl)); - bufferText->addAfter(string_format("%s b0,%s_if", compo2.c_str(), nd->getTargetText())); - bufferText->addAfter(string_format("j %s_end", nd->getTargetText())); - bufferText->addAfter(string_format("%s_if:", nd->getTargetText())); - register_numl.increase(); - } - else - { - switch (nd->type) - { - case TokenLessThan: - compop = "olt.s"; // greater or equal - // bufferText->addAfter( string_format("%s_end:\n",nd->target.c_str())); - compo2 = "bf"; - break; - case TokenDoubleEqual: - compop = "oeq.s"; // not equal - compo2 = "bf"; - break; - case TokenNotEqual: - compop = "oeq.s"; // equal - compo2 = "bt"; - break; - case TokenMoreOrEqualThan: - compop = "ole.s"; // less then - h = numl; - numl = leftl; - leftl = h; - compo2 = "bf"; - break; - case TokenMoreThan: - compop = "olt.s"; // not equal - h = numl; - numl = leftl; - leftl = h; - compo2 = "bf"; - break; - case TokenLessOrEqualThan: - compop = "ole.s"; // not equal - compo2 = "bf"; - - // compo2="bt"; - break; - default: - break; - } - bufferText->addAfter(string_format("%s b0,f%d,f%d", compop.c_str(), numl, leftl)); - bufferText->addAfter(string_format("%s b0,%s_end", compo2.c_str(), nd->getTargetText())); - // bufferText->addAfter(string_format("%s a%d,a%d,%s_end", compop.c_str(), numl, leftl, nd->getTargetText())); - - // bufferText->addAfter(_compare.back()+1,string_format("j %s_end", nd->target.c_str())); - // bufferText->addAfter(_compare.back()+2,string_format("%s_if:", nd->target.c_str())); - register_numl.increase(); - } - } - else - { - if (nd->_total_size > 127) // 127 - { - int f; - bool regular = true; - - if (nd->getChildAtPos(1)->getChildAtPos(0)->_nodetype == numberNode) - { - - f = stringToInt(nd->getChildAtPos(1)->getChildAtPos(0)->getTokenText()); - switch (nd->type) - { - case TokenLessThan: - if (isBranchImmediate(f, false)) - { - - compop = "blti"; // not equal - leftl = numl; - // f++; - regular = false; - } - break; - case TokenDoubleEqual: - if (isBranchImmediate(f, false)) - { - compop = "bnei"; // equal - regular = false; - leftl = numl; - } - break; - case TokenNotEqual: - if (isBranchImmediate(f, false)) - { - compop = "beqi"; // equal - regular = false; - leftl = numl; - } - break; - case TokenMoreOrEqualThan: - if (isBranchImmediate(f, false)) - { - compop = "bgei"; // not equal - leftl = numl; - // f++; - regular = false; - } - break; - case TokenMoreThan: - if (isBranchImmediate(f, true)) - { - compop = "bgei"; // not equal - leftl = numl; - f++; - regular = false; - } - break; - case TokenLessOrEqualThan: - if (isBranchImmediate(f, true)) - { - compop = "blti"; // not equal - leftl = numl; - f++; - regular = false; - } - - break; - default: - break; - } - } - if (regular) - { - switch (nd->type) - { - case TokenLessThan: - compop = "blt"; // greater or equal - // bufferText->addAfter( string_format("%s_end:\n",nd->target.c_str())); - break; - case TokenDoubleEqual: - compop = "beq"; // not equal - break; - case TokenNotEqual: - compop = "bne"; // equal - break; - case TokenMoreOrEqualThan: - compop = "bge"; // less then - break; - case TokenMoreThan: - compop = "blt"; // not equal - h = numl; - numl = leftl; - leftl = h; - break; - case TokenLessOrEqualThan: - compop = "bge"; // not equal - h = numl; - numl = leftl; - leftl = h; - break; - default: - break; - } - } - if (regular) - { - bufferText->addAfter(string_format("%s%s a%d,a%d,%s_if", compop.c_str(), _add.c_str(), numl, leftl, nd->getTargetText())); - } - else - { - bufferText->blankCurrent(); - - f = valBranchImmediate(f); - bufferText->addAfter(string_format("%s%s a%d,%d,%s_if", compop.c_str(), _add.c_str(), leftl, f, nd->getTargetText())); - } - bufferText->addAfter(string_format("j %s_end", nd->getTargetText())); - bufferText->addAfter(string_format("%s_if:", nd->getTargetText())); - register_numl.increase(); - } - else - { - int f; - bool regular = true; - - if (nd->getChildAtPos(1)->getChildAtPos(0)->_nodetype == numberNode) - { - - f = stringToInt(nd->getChildAtPos(1)->getChildAtPos(0)->getTokenText()); - switch (nd->type) - { - case TokenLessThan: - if (isBranchImmediate(f, false)) - { - printf("on est ici doublec\n"); - compop = "bgei"; // not equal - h = numl; - numl = leftl; - leftl = h; - // f++; - regular = false; - } - break; - case TokenDoubleEqual: - if (isBranchImmediate(f, false)) - { - compop = "bnei"; // equal - regular = false; - leftl = numl; - } - break; - case TokenNotEqual: - if (isBranchImmediate(f, false)) - { - compop = "beqi"; // equal - regular = false; - } - break; - case TokenMoreOrEqualThan: - if (isBranchImmediate(f, false)) - { - compop = "blti"; // not equal - h = numl; - numl = leftl; - leftl = h; - // f++; - regular = false; - } - break; - case TokenMoreThan: - if (isBranchImmediate(f, true)) - { - compop = "blti"; // not equal - h = numl; - numl = leftl; - leftl = h; - f++; - regular = false; - } - break; - case TokenLessOrEqualThan: - if (isBranchImmediate(f, true)) - { - compop = "bgei"; // not equal - h = numl; - numl = leftl; - leftl = h; - f++; - regular = false; - } - - break; - default: - break; - } - } - - if (regular) - { - switch (nd->type) - { - case TokenLessThan: - compop = "bge"; // greater or equal blt - // bufferText->addAfter( string_format("%s_end:\n",nd->target.c_str())); - break; - case TokenDoubleEqual: - compop = "bne"; // not equal beq - break; - case TokenNotEqual: - compop = "beq"; // equal - break; - case TokenMoreOrEqualThan: - compop = "blt"; // less then - break; - case TokenMoreThan: - compop = "bge"; // not equal - h = numl; - numl = leftl; - leftl = h; - break; - case TokenLessOrEqualThan: - compop = "blt"; // not equal - h = numl; - numl = leftl; - leftl = h; - break; - default: - break; - } - } - if (regular) - bufferText->addAfter(string_format("%s%s a%d,a%d,%s_end", compop.c_str(), _add.c_str(), numl, leftl, nd->getTargetText())); - else - { - bufferText->blankCurrent(); - f = valBranchImmediate(f); - bufferText->addAfter(string_format("%s%s a%d,%d,%s_end", compop.c_str(), _add.c_str(), leftl, f, nd->getTargetText())); - } - // bufferText->addAfter(_compare.back()+1,string_format("j %s_end", nd->target.c_str())); - // bufferText->addAfter(_compare.back()+2,string_format("%s_if:", nd->target.c_str())); - register_numl.increase(); - } - } - // printf("out comparator\n"); -} - -void _visitCallFunctionTemplate(NodeToken *nd, int regbase, bool isExtCall) -{ - - int staack_offset = (nd->getChildAtPos(0)->children_size() - 7) * 4; - bool convert = true; - bool isArg = false; - int nbfloat = 0; - if (nd == NULL) - { - return; - } - bool saveinstack[20]; - for (int i = 0; i < 20; i++) - { - if (isExtCall) - saveinstack[i] = false; - else - saveinstack[i] = false; - if (i >= 4) - saveinstack[i] = true; - } - NodeToken *func = _functions[nd->target]; - - NodeToken *t = nd; // cntx.findFunction(nd->_token); - if (t == NULL) - { - // globalType.pop(); - return; - } - // printf(" %s %d %d\n",nd->_token->text.c_str(), t->children_size(),t->getChildAtPos(1)->children_size()); - // for (int i = 0; i < t->getChildAtPos(1)->children_size(); i++) - - // printf("number of arg %s %d\r\n", nd->getTokenText(), nd->findMaxArgumentSize()); - for (int i = t->getChildAtPos(0)->children_size() - 1; i >= 0; i--) - { - // printf("***number of arg %d %d\r\n", i, nd->getChildAtPos(2)->getChildAtPos(i)->findMaxArgumentSize()); - bool save_in_stack = false; - for (int j = 0; j < i; j++) - { - if (nd->getChildAtPos(0)->getChildAtPos(j)->findMaxArgumentSize() - 1 >= i) - { - save_in_stack = true; - } - } - if (isExtCall) - { - if (i == 0) - save_in_stack = false; - } - else - { - if (i < 2) - save_in_stack = false; - } - saveinstack[i] = save_in_stack; - if (i >= 4) - { - save_in_stack = true; - saveinstack[i] = true; - } - if (i >= 6) - { - save_in_stack = false; - saveinstack[i] = false; - } - if (t->getChildAtPos(0)->getChildAtPos(i)->isPointer) - { - register_numl.duplicate(); - nd->getChildAtPos(0)->getChildAtPos(i)->visitNode(); - register_numl.pop(); - if (save_in_stack == true) - { - bufferText->addAfter(string_format("s32i a%d,a1,%d", register_numl.get(), i * 4 + _START_2)); - } - else - { - bufferText->addAfter(string_format("mov a%d,a%d", regbase + i, register_numl.get())); - } - } - else - - { - if (i < func->getChildAtPos(1)->children_size()) - { - if (func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->_varType == __Args__) - convert = false; - - register_numl.duplicate(); - globalType.push(func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->_varType); - nd->getChildAtPos(0)->getChildAtPos(i)->visitNode(); - register_numl.pop(); - } - else - { - register_numl.duplicate(); - // globalType.push(t->getChildAtPos(2)->getChildAtPos(i)->getVarType()->_varType); - nd->getChildAtPos(0)->getChildAtPos(i)->visitNode(); - register_numl.pop(); - } - if (nd->getChildAtPos(0)->getChildAtPos(i)->getVarType() != NULL and convert) - translateType(globalType.get(), nd->getChildAtPos(0)->getChildAtPos(i)->getVarType()->_varType, register_numl.get()); - varTypeEnum _vartype; - NodeToken *l; - if (i < func->getChildAtPos(1)->children_size()) - { - l = func->getChildAtPos(1); - _vartype = func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->_varType; - if (_vartype == __Args__) - { - _vartype = t->getChildAtPos(0)->getChildAtPos(i)->getVarType()->_varType; - isArg = true; - /* - bufferText->pop(); - bufferText->addAfter(string_format("l32i a12,a8")); - bufferText->addAfter(string_format("mov a12,a8")); - bufferText->addAfter(string_format("addi a8,a8,4")); - bufferText->addAfter(string_format("mov a13,a8")); - */ - } - } - else - { - l = t->getChildAtPos(0); - _vartype = t->getChildAtPos(0)->getChildAtPos(i)->getVarType()->_varType; - } - - if (_vartype == __float__) - { - - if (isArg) - { - } - else - { - if (save_in_stack == true) - { - bufferText->addAfter(string_format("ssi f%d,a1,%d", register_numl.get(), i * 4 + _START_2)); - } - else - { - bufferText->addAfter(string_format("rfr a%d,f%d", regbase + i, register_numl.get())); - } - } - } - else if (l->getChildAtPos(i)->getVarType()->_varType == __CRGB__ or l->getChildAtPos(i)->getVarType()->_varType == __CRGBW__) - { - // bufferText->addAfter( bufferText->sp.pop(),string_format("mov a%d,a%d", 10 + i, register_numl.get())); - if (t->getChildAtPos(0)->getChildAtPos(i)->getChildAtPos(0)->_nodetype == numberNode) - { - - if (save_in_stack == true) - { - for (int k = 0; k < 3; k++) - { - bufferText->addAfter(bufferText->sp.pop(), string_format("s8i a%d,a1,%d", register_numl.get(), i * 4 + _START_2 + k)); - } - // bufferText->addAfter(string_format("l32i a%d,a1,%d",10+i,i * 4 + _START_2)); - } - - else - { - for (int k = 2; k >= 0; k--) - { - bufferText->addAfter(bufferText->sp.pop(), string_format("s8i a%d,a1,%d", register_numl.get(), i * 4 + _START_2 + k)); - } - bufferText->addAfter(string_format("l32i a%d,a1,%d", regbase + i, i * 4 + _START_2)); - } - } - else - { - - for (int k = 0; k < t->getChildAtPos(0)->getChildAtPos(i)->getChildAtPos(0)->getVarType()->size; k++) - { - // bufferText->addAfter(string_format("mov a15,a10")); - // bufferText->addAfter(bufferText->sp.pop(),string_format("slli a%d,a%d,%d", 10+i,register_numl.get(), k* 8)); - // register_numl--; - bufferText->pop(); - } - if (t->getChildAtPos(0)->getChildAtPos(i)->getChildAtPos(0)->_nodetype == callFunctionNode) - { - if (save_in_stack == true) - { - bufferText->addAfter(bufferText->sp.pop(), string_format("l32i a%d,a8,0", regbase + i)); - bufferText->addAfter(string_format("s32i a%d,a1,%d", regbase + i, i * 4 + _START_2)); - } - else - { - bufferText->addAfter(bufferText->sp.pop(), string_format("l32i a%d,a8,0", 10 + i)); - } - } - if (t->getChildAtPos(0)->getChildAtPos(i)->getChildAtPos(0)->_nodetype == extCallFunctionNode) - { - if (save_in_stack == true) - { - // bufferText->addAfter(bufferText->sp.pop(), string_format("mov a%d,a10", 10 + i)); - bufferText->addAfter(string_format("s32i a10,a1,%d", i * 4 + _START_2)); - } - else - { - bufferText->addAfter(bufferText->sp.pop(), string_format("mov a%d,a10", regbase + i)); - } - } - else if (t->getChildAtPos(0)->getChildAtPos(i)->getChildAtPos(0)->_nodetype == localVariableNode) - { - if (save_in_stack == true) - { - bufferText->addAfter(bufferText->sp.pop(), string_format("l32i a%d,a1,%d", regbase + i, t->getChildAtPos(0)->getChildAtPos(i)->getChildAtPos(0)->stack_pos)); - bufferText->addAfter(string_format("s32i a%d,a1,%d", regbase + i, i * 4 + _START_2)); - } - else - { - bufferText->addAfter(bufferText->sp.pop(), string_format("l32i a%d,a1,%d", regbase + i, t->getChildAtPos(0)->getChildAtPos(i)->getChildAtPos(0)->stack_pos)); - } - } - else if (t->getChildAtPos(2)->getChildAtPos(i)->getChildAtPos(0)->_nodetype == globalVariableNode) - { - // tobe done - if (save_in_stack == true) - { - bufferText->addAfter(bufferText->sp.pop(), string_format("l32i a%d,a5,0", regbase + i)); - bufferText->addAfter(string_format("s32i a%d,a1,%d", regbase + i, i * 4 + _START_2)); - } - else - { - bufferText->addAfter(bufferText->sp.pop(), string_format("l32i a%d,a5,0", regbase + i)); - } - } - } - } - - else - { - if (save_in_stack == true) - { - bufferText->addAfter(string_format("s32i a%d,a1,%d", register_numl.get(), i * 4 + _START_2)); - } - else - { - // to change - if (i >= 6) - { - bufferText->addAfter(string_format("s32i a%d,a1,%d", register_numl.get(), staack_offset)); - staack_offset -= 4; - } - else - { - bufferText->addAfter(string_format("mov a%d,a%d", regbase + i, register_numl.get())); - } - } - } - - globalType.pop(); - } - } - - // bufferText->end(); - for (int i = 0; i < nd->getChildAtPos(0)->children_size(); i++) - { - if (i < 7) - { - if (saveinstack[i] == true) - { - varType *v; - if (i < func->getChildAtPos(1)->children_size()) - v = func->getChildAtPos(1)->getChildAtPos(i)->getVarType(); - else - v = nd->getChildAtPos(0)->getChildAtPos(i)->getVarType(); - if (v->_varType == __none__) - { - v = &_varTypes[__uint32_t__]; - } - - if (v->_varType == __float__) - { - bufferText->addAfter(string_format("l32i a%d,a1,%d", regbase + i, i * 4 + _START_2)); - } - else - { - bufferText->addAfter(string_format("%s %s%d,%s1,%d", asmInstructionsName[v->load[0]].c_str(), getRegType(v->load[0], 0).c_str(), regbase + i, getRegType(v->load[0], 1).c_str(), i * 4 + _START_2)); - } - } - } - } - - if (isExtCall) - { - bufferText->addAfter(string_format("callExt a8,@_%s", nd->getTokenText())); - } - else - { - // bufferText->addAfter(string_format("mov a10,a2")); - bufferText->addAfter(string_format("call8 @_%s", nd->getTokenText())); - } - bufferText->sp.push(bufferText->get()); - - return; -} - -void _visitcallFunctionNode(NodeToken *nd) -{ - // printf("compiling call function %s\n", nd->getTokenText()); - NodeToken *t = nd; // cntx.findFunction(nd->_token); - NodeToken *func = _functions[nd->target]; - if (func->getChildAtPos(1)->children_size() >= _TRIGGER) - { // printf("token type %d\r\n",nd->_link->getChildAtPos(0)->_token->_vartype->_varType); - // printf("we ar ehere\n"); - if (t == NULL) - { - return; - } - - // if(t->getChildAtPos(1)->children_size()<1) - // return;point_regnum - int save = 9; // point_regnum; - // bufferText->addAfterNoDouble(string_format("l32r a%d,@_stack_%s", save,nd->getTokenText())); // point_regnum - if (func->getChildAtPos(1)->children_size() == 0 and t->type == TokenUserDefinedVariableMemberFunction) - { - bufferText->addAfter(bufferText->sp.pop(), string_format("mov a10,a2")); - } - for (int i = 0; i < func->getChildAtPos(1)->children_size(); i++) - { - // printf("calll \r\n"); - // isPointer = false; - if (func->getChildAtPos(1)->getChildAtPos(i)->isPointer) - { - // printf("calll p\r\n"); - // isPointer = true; - register_numl.duplicate(); - nd->getChildAtPos(0)->getChildAtPos(i)->visitNode(); - register_numl.pop(); - int start = func->getChildAtPos(1)->getChildAtPos(i)->stack_pos - _STACK_SIZE; - - if (t->type == TokenUserDefinedVariableMemberFunction and i == 0) - { - // printf("*******onr en ocoi******\r\n"); - if (!isStructFunction) - { - bufferText->addAfter(bufferText->sp.pop(), string_format("mov a10,a%d", register_numl.get())); - } - else - { - // bufferText->pop(); - bufferText->addAfter(bufferText->sp.pop(), string_format("mov a10,a2")); - } - } - else - { - bufferText->addAfter(bufferText->sp.pop(), string_format("s32i a%d,a%d,%d", register_numl.get(), save, start)); // point_regnum - bufferText->addBefore(string_format("l32r a%d,@_stack_%s", save, nd->getTokenText())); // point_regnum - } // isPointer=false; - } - else - { - - globalType.push(func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->_varType); - register_numl.duplicate(); - // printf("in callf:"); - nd->getChildAtPos(0)->getChildAtPos(i)->visitNode(); - // printf("\n"); - register_numl.pop(); - int start = func->getChildAtPos(1)->getChildAtPos(i)->stack_pos - _STACK_SIZE + func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->total_size; - int tot = func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->size - 1; - for (int j = 0; j < func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->size; j++) - { - - if (nd->getChildAtPos(0)->getChildAtPos(i)->getVarType() != NULL) - { - translateType(globalType.get(), nd->getChildAtPos(0)->getChildAtPos(i)->getVarType()->_varType, register_numl.get()); - bufferText->sp.pop(); - bufferText->sp.push(bufferText->get()); - } - else - { - // translateType(globalType.get(), nd->getChildAtPos(0)->getChildAtPos(i)->_token->_varType, register_numl.get()); - } - - start -= func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->sizes[tot - j]; - asmInstruction asmInstr = func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->store[tot - j]; - // bufferText->addAfter(bufferText->sp.pop(), string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), point_regnum, start)); - int sav; - // if (j == t->getChildAtPos(1)->getChildAtPos(i)->getVarType()->size - 1) - if (!intest) - { - sav = bufferText->get(); - bufferText->addAfter(bufferText->sp.pop(), string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), save, start)); - if (j == func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->size - 1) - { - // bufferText->sp.push(bufferText->get()); - - bufferText->addAfter(sav, string_format("l32r a%d,@_stack_%s", save, nd->getTokenText())); // point_regnum - // bufferText->addAfter(sav, string_format("l32r a%d,@_stack",save)); - } - } - else - { - // sav = bufferText->get(); - bufferText->addAfter(string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), save, start)); - if (j == func->getChildAtPos(1)->getChildAtPos(i)->getVarType()->size - 1) - { - // bufferText->sp.push(bufferText->get()); - - bufferText->addBefore(string_format("l32r a%d,@_stack_%s", save, nd->getTokenText())); // point_regnum - // bufferText->putIteratorAtPos(bufferText->get()+1); // bufferText->addAfter(sav, string_format("l32r a%d,@_stack",save)); - } - } - // start+=t->getChildAtPos(1)->getChildAtPos(i)->_token->_vartype->sizes[j]; - } - globalType.pop(); - } - // bufferText->addAfter(string_format("addi a%d,a1,%d",11+i,t->getChildAtPos(1)->getChildAtPos(i)->stack_pos)); - } - // bufferText->addAfter(string_format("mov a10,a2")); // neded to find the external variables !!!!!! - bufferText->addAfter(string_format("call8 @_%s", nd->getTokenText())); - } - else - { - _visitCallFunctionTemplate(nd, 10, false); - } - int start = nd->stack_pos; - // printf("ini\r\n"); - varType *v = func->getChildAtPos(0)->getVarType(); - // printf("ini\r\n"); - if (v == NULL) - { - printf("nodeToken %d\r\n", func->getChildAtPos(0)->type); - printf("NULL\r\n"); - } - - if (v->size > 1) - { - // printf("ini size\r\n"); - bufferText->addAfter(string_format("l32r a%d,@_stackr", 8)); // point_regnum - for (int i = 0; i < v->size; i++) - { - // bufferText->addAfter(string_format("mov a15,a10")); - // bufferText->addAfter(string_format("%s %s%d,%s%d,%d", v->load[i].c_str(), v->reg_name.c_str(), register_numl.get(), v->reg_name.c_str(), point_regnum, start)); - asmInstruction asmInstr = v->load[i]; - // printf("tryin to get %d %d\r\n",i,asmInstr); - bufferText->addAfter(string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), 8, start)); // point_regnum - // register_numl--; - start += v->sizes[i]; - // bufferText->sp.push(bufferText->get()); - } - register_numl.decrease(); - } - else if (v->size > 0) - { - if (v->_varType == __float__) - { - bufferText->addAfter(string_format("mov.s f%d,f2", register_numl.get())); - } - else - { - bufferText->addAfter(string_format("mov a%d,a10", register_numl.get())); - } - // bufferText->sp.push(bufferText->get()); - bufferText->sp.push(bufferText->get()); - register_numl.decrease(); - } -} -void _visitforNode(NodeToken *nd) -{ - // printf("ente for\n") ; - point_regnum = 5; - - register_numl.duplicate(); - nd->getChildAtPos(0)->visitNode(); - register_numl.pop(); - if (nd->getChildAtPos(nd->children_size() - 1)->_nodetype == onlyNode) - { - bufferText->addAfter(string_format("movExt a7,@_ext_%s", (nd->getChildAtPos(nd->children_size() - 1)->getTargetText()))); - boolextern = true; - } - bufferText->addAfter(string_format("j test_%s", nd->getTargetText())); - bufferText->addAfter(string_format("%s_end:", nd->getTargetText())); - _compare.push_back(bufferText->get()); - - register_numl.duplicate(); - - nd->getChildAtPos(3)->visitNode(); - if (nd->children_size() > 4) - { - if (nd->getChildAtPos(4)->_nodetype != onlyNode) - { - nd->getChildAtPos(4)->visitNode(); - } - } - if (nd->children_size() > 5) - { - - nd->getChildAtPos(5)->visitNode(); - } - register_numl.pop(); - - register_numl.duplicate(); - nd->getChildAtPos(2)->visitNode(); - register_numl.pop(); - switch (nd->getChildAtPos(1)->getChildAtPos(0)->type) - { - case TokenLessThan: - // printf("on est ici doublecdd\n"); - nd->getChildAtPos(1)->getChildAtPos(0)->type = TokenMoreOrEqualThan; // greater or equal - // bufferText->addAfter( string_format("%s_end:\n",nd->target.c_str())); - break; - case TokenDoubleEqual: - nd->getChildAtPos(1)->getChildAtPos(0)->type = TokenNotEqual; - break; - case TokenNotEqual: - nd->getChildAtPos(1)->getChildAtPos(0)->type = TokenDoubleEqual; - break; - case TokenMoreOrEqualThan: - nd->getChildAtPos(1)->getChildAtPos(0)->type = TokenLessThan; - break; - case TokenMoreThan: - nd->getChildAtPos(1)->getChildAtPos(0)->type = TokenLessOrEqualThan; - break; - case TokenLessOrEqualThan: - nd->getChildAtPos(1)->getChildAtPos(0)->type = TokenMoreThan; - break; - default: - break; - } - int jumpsize = (bufferText->get() - _compare.back()) * 3; - nd->getChildAtPos(1)->_total_size = jumpsize; - // bufferText->putIteratorAtPos(_compare.back()); - bufferText->addAfter(string_format("test_%s:", nd->getTargetText())); - - register_numl.duplicate(); - nd->getChildAtPos(1)->visitNode(); - register_numl.pop(); - - _compare.pop_back(); - - bufferText->addAfter(string_format("%s_continue:", nd->getTargetText())); - bufferText->addAfter(string_format("%s_break:", nd->getTargetText())); - // bufferText->putIteratorAtPos(bufferText->get()); - // bufferText->addAfter(string_format("j %s", nd->getTargetText())); - // bufferText->addAfter(string_format("%s:", nd->getTargetText())); - // clearNodeToken(nd); - if (nd->getChildAtPos(nd->children_size() - 1)->_nodetype == onlyNode) - { - - boolextern = false; - } - return; -} -void _visitargumentNode(NodeToken *nd) {} -void _visitextGlobalVariableNode(NodeToken *nd) -{ - register_numl.duplicate(); - if (nd->children_size() > 0) - { - register_numl.duplicate(); - nd->getChildAtPos(0)->visitNode(); - register_numl.pop(); - } - varType *v = nd->getVarType(); - int start = nd->stack_pos; - // uint8_t regnum = 1; - if (nd->isPointer) - { - // start = nd->stack_pos; - // regnum = point_regnum; - } - // string body = ""; - // register_numl++; - - if (!nd->isPointer) - { - bufferText->addAfter(string_format("movExt a%d,@_ext_%s", point_regnum, nd->getTokenText())); - } - else - { - if (nd->children_size() > 0) - { - - if (nd->children_size() > 1) - { - /* - for (int par = 0; par < nd->children_size(); par++) - { - register_numl.duplicate(); - nd->getChildAtPos(par)->visitNode(); - register_numl.pop(); - bufferText->addAfter(string_format("mov a%d,a%d", 10 + par, register_numl.get())); - } - bufferText->addAfter(string_format("callExt a%d,map%dD", register_numl.get(), nd->children_size())); - bufferText->addAfter(string_format("mov a%d,a10", register_numl.get())); - */ - vector tile; - int nb = 0; - string sd = string(nd->getTargetText()); - if (sd.compare(0, 1, "@") == 0) - { - tile = split(sd, " "); - - sscanf(tile[0].c_str(), "@%d", &nb); - } - if (nb > 1 and sd.compare(0, 1, "@") == 0) - { - bufferText->addAfter("movi a10,0"); - } - for (int par = 0; par < nd->children_size(); par++) - { - // globalType.push(__int__); - if (par > 0) - { - register_numl.duplicate(); - nd->getChildAtPos(par)->visitNode(); - register_numl.pop(); - } - if (nd->getChildAtPos(par)->getVarType() != NULL) - { - translateType(__int__, nd->getChildAtPos(par)->getVarType()->_varType, register_numl.get()); - } - if (nb > 1) - { - if (par < nd->children_size() - 1) - { - - for (int h = 1; h < nd->children_size() - par; h++) - { - bufferText->addAfter(string_format("movi a11,%d", stringToInt((char *)tile[par + 1 + h].c_str()))); - // bufferText->addAfter(string_format("mull a11,a10,a11")); - bufferText->addAfter(string_format("mull a%d,a%d,a11", register_numl.get(), register_numl.get())); - } - bufferText->addAfter(string_format("add a10,a10,a%d", register_numl.get())); - } - else - { - bufferText->addAfter(string_format("add a%d,a10,a%d", register_numl.get(), register_numl.get())); - } - } - - // globalType.pop(); - } - } - else - { - translateType(__int__, nd->getChildAtPos(0)->getVarType()->_varType, register_numl.get()); - } - - bufferText->addAfter(string_format("movExt a%d,@_ext_%s", point_regnum, nd->getTokenText())); - // f=f+number.f; - for (int i = 0; i < v->total_size; i++) - { - bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, point_regnum, register_numl.get())); - } - } - else - { - bufferText->addAfter(string_format("movExt a%d,@_ext_%s", register_numl.get(), nd->getTokenText())); - bufferText->sp.push(bufferText->get()); - bufferText->sp.push(bufferText->get()); - register_numl.pop(); - register_numl.decrease(); - return; - } - } - bufferText->sp.push(bufferText->get()); - for (int i = 0; i < v->size; i++) - { - // bufferText->addAfter(string_format("%s %s%d,%s%d,%d", v->load[i].c_str(), v->reg_name.c_str(), register_numl.get(), v->reg_name.c_str(), point_regnum, start)); - asmInstruction asmInstr = v->load[i]; - bufferText->addAfter(string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), point_regnum, start)); - // register_numl--; - start += v->sizes[i]; - bufferText->sp.push(bufferText->get()); - } - // res.f = f; - // res.header = number.header + h; - // point_regnum++; - register_numl.pop(); - register_numl.decrease(); - // res.register_numl=register_numl; - // res.register_numr=register_numr; - return; -} -// void _visitextDefFunctionNode(NodeToken *nd){} - -void _visitextCallFunctionNode(NodeToken *nd) -{ - NodeToken *t = nd; - _visitCallFunctionTemplate(nd, 10, true); - int start = nd->stack_pos; - varType *v = _functions[nd->target]->getChildAtPos(0)->getVarType(); - if (v->_varType == __float__) - { - bufferText->addAfter(string_format("wfr f%d,a%d", register_numl.get(), 10)); - - bufferText->sp.push(bufferText->get()); - // globalType.push(__float__); - } - else - { - if (v->size > 1) - { - for (int i = 0; i < v->size; i++) - { - // bufferText->addAfter(string_format("mov a15,a10")); - if(i>-1) - bufferText->addAfter(string_format("extui a%d,a%d,%d,%d", register_numl.get(), 10, start * 8, v->sizes[i] * 8)); - else - bufferText->addAfter(string_format("mov a%d,a10", register_numl.get())); - // register_numl--; - start += v->sizes[i]; - bufferText->sp.push(bufferText->get()); - } - } - else - { - if (v->_varType != __void__) - { - bufferText->addAfter(string_format("mov a%d,a10", register_numl.get())); - } - bufferText->sp.push(bufferText->get()); - } - } - register_numl.decrease(); -} -void _visitreturnArgumentNode(NodeToken *nd) {} -void _visitvariableDeclarationNode(NodeToken *nd) {} -void _visitdefExtFunctionNode(NodeToken *nd) -{ - header.addAfter(string_format("@_%s:", nd->getTokenText())); - header.addAfter(".bytes 4"); -} -void _visitinputArgumentsNode(NodeToken *nd) -{ -} -void _visitdefInputArgumentsNode(NodeToken *nd) -{ - - if (nd->children_size() < 1) - return; - int sav = 9; - if (nd->children_size() >= _TRIGGER) - { // point_regnum; - // 17/01 // bufferText->addAfterNoDouble(string_format("l32r a%d,@_stack_%s", sav, nd->parent->getTokenText())); // point_regnum - // bufferText->addAfterNoDouble(string_format("l32r a%d,@_stack", sav)); - for (int i = 0; i < nd->children_size(); i++) - { - // printf("ee\r\n"); - int start = nd->getChildAtPos(i)->stack_pos; - if (nd->getChildAtPos(i)->isPointer) - { - // printf("ee p\r\n"); - int start = nd->getChildAtPos(i)->stack_pos; - bufferText->addAfter(string_format("l32i a15,a%d,%d", sav, start - _STACK_SIZE)); // point reg_bnum - bufferText->addAfter(string_format("s32i a15,a1,%d", start)); - } - else - { - // printf("ee j\r\n"); - for (int j = 0; j < nd->getChildAtPos(i)->getVarType()->size; j++) - { - asmInstruction asmInstr = nd->getChildAtPos(i)->getVarType()->load[0]; - bufferText->addAfter(string_format("%s %s15,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), getRegType(asmInstr, 1).c_str(), sav, start - _STACK_SIZE)); // point_regnum - asmInstr = nd->getChildAtPos(i)->getVarType()->store[0]; - bufferText->addAfter(string_format("%s %s15,%s1,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), getRegType(asmInstr, 1).c_str(), start)); - start += nd->getChildAtPos(i)->getVarType()->sizes[j]; - } - } - } - } - else - { - int reg_num = 2; - int stek = ((nd->parent->stack_pos) / 8 + 1) * 8 + 16 + _STACK_SIZE; - for (int i = 0; i < nd->children_size(); i++) - { - int start = nd->getChildAtPos(i)->stack_pos; - if (start >= _STACK_SIZE) - { - // printf("ee\r\n"); - if (reg_num <= 7) - { - - // if (nd->getChildAtPos(i)->isPointer) - //{ - // printf("ee p\r\n"); - // int start = nd->getChildAtPos(i)->stack_pos; - // bufferText->addAfter(string_format("l32i a15,a%d,%d", sav, start - _STACK_SIZE)); // point reg_bnum - // - if (nd->getChildAtPos(i)->isPointer) - { - bufferText->addAfter(string_format("s32i a%d,a1,%d", reg_num, start)); - } - else if (nd->getChildAtPos(i)->getVarType()->_varType == __float__) - { - bufferText->addAfter(string_format("s32i a%d,a1,%d", reg_num, start)); - // bufferText->addAfter(string_format("%s %s15,%s1,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), getRegType(asmInstr, 1).c_str(), start)); - } - else - { - asmInstruction asmInstr = nd->getChildAtPos(i)->getVarType()->store[0]; - bufferText->addAfter(string_format("%s a%d,a1,%d", asmInstructionsName[asmInstr].c_str(), reg_num, start)); - } - // reg_num++; - } - else - { - asmInstruction asmInstr = nd->getChildAtPos(i)->getVarType()->load[0]; - bufferText->addAfter(string_format("%s a15,a1,%d", asmInstructionsName[asmInstr].c_str(), stek + start - nd->getChildAtPos(6)->stack_pos)); - asmInstr = nd->getChildAtPos(i)->getVarType()->store[0]; - bufferText->addAfter(string_format("%s a15,a1,%d", asmInstructionsName[asmInstr].c_str(), start)); - } - } - else if (nd->getChildAtPos(i)->getVarType()->_varType == __float__) - { - } - reg_num++; - /* - } - else - { - // printf("ee j\r\n"); - for (int j = 0; j < nd->getChildAtPos(i)->getVarType()->size; j++) - { - asmInstruction asmInstr = nd->getChildAtPos(i)->getVarType()->load[0]; - bufferText->addAfter(string_format("%s %s15,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), getRegType(asmInstr, 1).c_str(), sav, start - _STACK_SIZE)); // point_regnum - asmInstr = nd->getChildAtPos(i)->getVarType()->store[0]; - bufferText->addAfter(string_format("%s %s15,%s1,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), getRegType(asmInstr, 1).c_str(), start)); - start += nd->getChildAtPos(i)->getVarType()->sizes[j]; - } - } - */ - } - } -} -void _visitdefExtGlobalVariableNode(NodeToken *nd) -{ - if (safeMode) - { - if (nd->isPointer) - { - header.addAfter(string_format("@_size_%s:", nd->getTokenText())); - header.addAfter(_numToBytes(nd->_total_size / nd->getVarType()->total_size).c_str()); - } - } - header.addAfter(string_format("@_ext_%s:", nd->getTokenText())); - header.addAfter(".bytes 4"); -} -void _visitdefGlobalVariableNode(NodeToken *nd) -{ - if (strcmp(nd->getTokenText(), "_handle_") == 0 || strcmp(nd->getTokenText(), "_execaddr_") == 0) - return; - if (safeMode) - { - if (nd->isPointer) - { - header.addAfter(string_format("@_size_%s:", nd->getTokenText())); - header.addAfter(_numToBytes(nd->_total_size / nd->getVarType()->total_size).c_str()); - } - } - header.addAfter(string_format("@_%s:", nd->getTokenText())); - if (nd->children_size() == 0) - { - header.addAfter(string_format(".bytes %d", nd->_total_size)); - } - else - { - _data_sav = ""; - if (nd->getVarType()->_varType == __CRGB__ or nd->getVarType()->_varType == __CRGBW__) - { - if (nd->children_size() > 0) - { - for (NodeToken *ndt : *nd->children) - { - if (ndt->children_size()) - { - for (NodeToken *ndtc : *ndt->children) - { - int __num = 0; - sscanf(ndtc->getTokenText(), "%d", &__num); - _data_sav = _data_sav + " " + string_format("%02x", __num); - } - } - } - } - } - else if (nd->getVarType()->_varType == __char__) - { - string str = string(nd->getChildAtPos(0)->getTokenText()); - nd->_total_size = str.size() - 1; // we remove the size taken by " " adn we add 0 fo \0 - for (int i = 1; i < str.size() - 1; i++) - { - _data_sav = _data_sav + " " + string_format("%02x", str.c_str()[i]); - } - _data_sav = _data_sav + " 00"; - } - else - { - uint32_t __num; - __num = 0; - uint8_t c; - if (nd->children_size() > 0) - { - for (NodeToken *ndt : *nd->children) - { - if (nd->getVarType()->_varType == __float__) - { - float __f = 0; - sscanf(ndt->getTokenText(), "%f", &__f); - __num = (uint32_t)(*((uint32_t *)&__f)); - } - else - { - __num = 0; - sscanf(ndt->getTokenText(), "%lu", &__num); - } - for (int i = 0; i < nd->getVarType()->total_size; i++) - { - c = __num & 0xff; - _data_sav = _data_sav + " " + string_format("%02x", c); - // val=val+'A'; - __num = __num / 256; - } - } - } - } - header.addAfter(string_format(".bytes %d%s", nd->_total_size, _data_sav.c_str())); - } -} -void _visitdefLocalVariableNode(NodeToken *nd) {} - -void _visitstoreLocalVariableNode(NodeToken *nd) -{ - // //printf("jjjkkj\n"); - varType *v = nd->getVarType(); - // globalType.push(__float__);//v->_varType); - // //printf("ona stocké:%d: %s\n",globalType.get(),varTypeEnumNames[globalType.get()].c_str()); - uint8_t regnum = 1; - if (nd->asPointer or (nd->isPointer && nd->children_size() == 0)) - point_regnum++; - if (nd->isPointer) - { - // start = nd->stack_pos; - regnum = point_regnum; - } - int start = nd->stack_pos; - if (nd->isPointer) - { - start = 0; - } - int numl = register_numl.get(); - for (int h = 0; h < v->size - 1; h++) - { - start += v->sizes[h]; - } - if (nd->children_size() > 0 or !nd->isPointer or nd->asPointer) - { - for (int i = v->size - 1; i >= 0; i--) - { - // //printf("jjjkkj: %d\n",i); - // list::iterator l=bufferText->sp.pop(); - // bufferText->addAfter( bufferText->sp.pop(), string_format("%s %s%d,%s%d,%d", v->store[i].c_str(), v->reg_name.c_str(), numl, v->reg_name.c_str(), regnum, start)); - bufferText->addAfter(bufferText->sp.pop(), string_format("%s %s%d,%s%d,%d", asmInstructionsName[v->store[i]].c_str(), getRegType(v->store[i], 0).c_str(), numl, getRegType(v->store[i], 1).c_str(), regnum, start)); - // numl++; - start -= v->sizes[i]; - } - } - else - { - bufferText->addAfter(bufferText->sp.pop(), string_format("s32i a%d,a%d,%d", numl, regnum, 0)); // start - // bufferText->addAfter(bufferText->sp.pop(), string_format("s32i a%d,a%d,%d", numl, 1,nd->stack_pos)); - } - if (nd->isPointer) - { - register_numl.duplicate(); - bufferText->sp.push(bufferText->get()); - bufferText->sp.swap(); - bufferText->putIteratorAtPos(bufferText->sp.get()); - // bufferText->sp.displaystack("PILE"); - if (nd->children_size() > 0) - { - globalType.push(__int__); - register_numl.duplicate(); - nd->getChildAtPos(0)->visitNode(); - register_numl.pop(); - globalType.pop(); - } - if (nd->children_size() > 0) - { - bufferText->addAfter(string_format("l32i a%d,a1,%d", point_regnum, nd->stack_pos)); - for (int i = 0; i < v->total_size; i++) - { - bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, point_regnum, register_numl.get())); - } - } - else if (nd->asPointer) - { - if (nd->type == TokenUserDefinedVariableMember or nd->type == TokenUserDefinedVariableMemberFunction) - { - - if (isStructFunction) - { - bufferText->addAfter(string_format("addi a%d,a%d,%d", point_regnum, 2, (int)(nd->stack_pos / 1000))); - } - else - { - bufferText->addAfter(string_format("l32i a%d,a1,%d", point_regnum, nd->stack_pos - (int)(nd->stack_pos / 1000) * 1000)); - bufferText->addAfter(string_format("addi a%d,a%d,%d", point_regnum, point_regnum, (int)(nd->stack_pos / 1000))); - } - } - else - - bufferText->addAfter(string_format("l32i a%d,a1,%d", point_regnum, nd->stack_pos)); - } - else - { - // a remettre - bufferText->addAfter(string_format("addi a%d,a1,%d", point_regnum, nd->stack_pos)); - } - bufferText->end(); - bufferText->sp.pop(); - // point_regnum++; - register_numl.pop(); - // point_regnum--; - if (nd->asPointer or (nd->isPointer && nd->children_size() == 0)) - point_regnum--; - } -} -void _visitstoreGlobalVariableNode(NodeToken *nd) -{ - - register_numl.duplicate(); - varType *v = nd->getVarType(); - int start = nd->stack_pos; - // uint8_t regnum = 1; - // int savreg_num=point_regnum; - if (nd->asPointer or (nd->isPointer)) // && nd->children_size() == 0)) - point_regnum++; - int savreg_num = point_regnum; - point_regnum = 6; // YBA 25-02-20252 - if (nd->isPointer) - { - // start = nd->stack_pos; - // regnum = point_regnum; - } - // string body = ""; - // register_numl++; - for (int h = 0; h < v->size - 1; h++) - { - start += v->sizes[h]; - } - int j = bufferText->sp.get(); - if ((nd->children_size() > 0 or !nd->isPointer)) - { - // if (nd->target == EOF_TEXTARRAY) - // { - - for (int i = v->size - 1; i >= 0; i--) - { - // printf("here in store %d\r\n",i); - // bufferText->addAfter(bufferText->sp.pop(), string_format("%s %s%d,%s%d,%d", v->store[i].c_str(), v->reg_name.c_str(), register_numl.get(), v->reg_name.c_str(), point_regnum, start)); - bufferText->addAfter(bufferText->sp.pop(), string_format("%s %s%d,%s%d,%d", asmInstructionsName[v->store[i]].c_str(), getRegType(v->store[i], 0).c_str(), register_numl.get(), getRegType(v->store[i], 1).c_str(), point_regnum, start)); - // register_numl--; - // printf("here in store %d\r\n",i); - start -= v->sizes[i - 1]; - // bufferText->sp.push(bufferText->get()); - } - } - - else - { - bufferText->addAfter(bufferText->sp.pop(), string_format("s32i a%d,a%d,%d", register_numl.get(), point_regnum, start)); - } - // res.f = f; - // res.header = number.header + h; - bufferText->sp.push(bufferText->get()); - bufferText->sp.swap(); - if (v->size > 0) - bufferText->putIteratorAtPos(bufferText->sp.get()); - else - bufferText->putIteratorAtPos(j); - if (nd->children_size() > 0) - { - vector tile; - int nb = 0; - string sd = string(nd->getTargetText()); - if (sd.compare(0, 1, "@") == 0) - { - tile = split(sd, " "); - - sscanf(tile[0].c_str(), "@%d", &nb); - } - if (nb > 1 and sd.compare(0, 1, "@") == 0) - { - bufferText->addAfter("movi a10,0"); - } - for (int i = 0; i < nd->children_size(); i++) - { - globalType.push(__int__); - register_numl.duplicate(); - nd->getChildAtPos(i)->visitNode(); - register_numl.pop(); - translateType(__int__, nd->getChildAtPos(i)->getVarType()->_varType, register_numl.get()); - if (nb > 1) - { - if (i < nd->children_size() - 1) - { - - for (int h = 1; h < nd->children_size() - i; h++) - { - bufferText->addAfter(string_format("movi a11,%d", stringToInt((char *)tile[i + 1 + h].c_str()))); - // bufferText->addAfter(string_format("mull a11,a10,a11")); - bufferText->addAfter(string_format("mull a%d,a%d,a11", register_numl.get(), register_numl.get())); - } - bufferText->addAfter(string_format("add a10,a10,a%d", register_numl.get())); - } - else - { - bufferText->addAfter(string_format("add a%d,a10,a%d", register_numl.get(), register_numl.get())); - } - } - - globalType.pop(); - } - } - - if (safeMode && nd->isPointer) - { - bufferText->addAfter(string_format("l32r a%d,@_size_%s", point_regnum, nd->getTokenText())); - bufferText->addAfter(string_format("l32i a%d,a%d,0", point_regnum, point_regnum)); - bufferText->addAfter(string_format("bge a%d,a%d,__test_safe_%d", point_regnum, register_numl.get(), for_if_num)); - bufferText->addAfter(string_format("movi a10,%d", 0)); - bufferText->addAfter(string_format("mov a11,a%d", point_regnum)); - bufferText->addAfter(string_format("mov a12,a%d", register_numl.get())); - bufferText->addAfter("callExt a8,error"); - bufferText->addAfter("retw.n"); - bufferText->addAfter(string_format("__test_safe_%d:", for_if_num)); - for_if_num++; - } - - if (nd->isPointer && nd->children_size() > 0) - { - - if (nd->type == TokenUserDefinedVariableMember or nd->type == TokenUserDefinedVariableMemberFunction) - { - bufferText->addAfter(string_format("movi a%d,%d", point_regnum, nd->_total_size)); - bufferText->addAfter(string_format("mull a%d,a%d,a%d", register_numl.get(), register_numl.get(), point_regnum)); - bufferText->addAfter(string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); - bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, point_regnum, register_numl.get())); - } - else if (v->total_size > 4) - { - - // string tmp=content.l->back(); - // content.l->pop_back(); - bufferText->addAfter(string_format("movi a%d,%d", point_regnum, v->total_size)); - bufferText->addAfter(string_format("mull a%d,a%d,a%d", register_numl.get(), register_numl.get(), point_regnum)); - bufferText->addAfter(string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); - bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, point_regnum, register_numl.get())); - } - else - { - bufferText->addAfter(string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); - for (int i = 0; i < v->total_size; i++) - { - bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, point_regnum, register_numl.get())); - } - } - } - else - { - bufferText->addAfter(string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); - } - bufferText->end(); - if (isStructFunction) - { - bufferText->addAfter(string_format("s32i a2,a1,%d", _STACK_SIZE)); - } - bufferText->sp.pop(); - // if(!nd->isPointer) - // point_regnum++; - register_numl.pop(); - point_regnum = savreg_num; - if (nd->asPointer or (nd->isPointer)) // && nd->children_size() == 0)) - point_regnum--; - // point_regnum--; - // res.register_numl=register_numl; - // res.register_numr=register_numr; - - return; +void _visitforNode(NodeToken *nd) { + // printf("ente for\n") ; + point_regnum = 5; + + register_numl.duplicate(); + nd->getChildAtPos(0)->visitNode(); + register_numl.pop(); + if (nd->getChildAtPos(nd->children_size() - 1)->_nodetype == onlyNode) { + bufferText->addAfter(string_format( + "movExt a7,@_ext_%s", + (nd->getChildAtPos(nd->children_size() - 1)->getTargetText()))); + boolextern = true; + } + bufferText->addAfter(string_format("j test_%s", nd->getTargetText())); + bufferText->addAfter(string_format("%s_end:", nd->getTargetText())); + _compare.push_back(bufferText->get()); + + register_numl.duplicate(); + + nd->getChildAtPos(3)->visitNode(); + if (nd->children_size() > 4) { + if (nd->getChildAtPos(4)->_nodetype != onlyNode) { + nd->getChildAtPos(4)->visitNode(); + } + } + if (nd->children_size() > 5) { + + nd->getChildAtPos(5)->visitNode(); + } + register_numl.pop(); + + register_numl.duplicate(); + nd->getChildAtPos(2)->visitNode(); + register_numl.pop(); + switch (nd->getChildAtPos(1)->getChildAtPos(0)->type) { + case TokenLessThan: + // printf("on est ici doublecdd\n"); + nd->getChildAtPos(1)->getChildAtPos(0)->type = + TokenMoreOrEqualThan; // greater or equal + // bufferText->addAfter( string_format("%s_end:\n",nd->target.c_str())); + break; + case TokenDoubleEqual: + nd->getChildAtPos(1)->getChildAtPos(0)->type = TokenNotEqual; + break; + case TokenNotEqual: + nd->getChildAtPos(1)->getChildAtPos(0)->type = TokenDoubleEqual; + break; + case TokenMoreOrEqualThan: + nd->getChildAtPos(1)->getChildAtPos(0)->type = TokenLessThan; + break; + case TokenMoreThan: + nd->getChildAtPos(1)->getChildAtPos(0)->type = TokenLessOrEqualThan; + break; + case TokenLessOrEqualThan: + nd->getChildAtPos(1)->getChildAtPos(0)->type = TokenMoreThan; + break; + default: + break; + } + int jumpsize = (bufferText->get() - _compare.back()) * 3; + nd->getChildAtPos(1)->_total_size = jumpsize; + // bufferText->putIteratorAtPos(_compare.back()); + bufferText->addAfter(string_format("test_%s:", nd->getTargetText())); + + register_numl.duplicate(); + nd->getChildAtPos(1)->visitNode(); + register_numl.pop(); + + _compare.pop_back(); + + bufferText->addAfter(string_format("%s_continue:", nd->getTargetText())); + bufferText->addAfter(string_format("%s_break:", nd->getTargetText())); + // bufferText->putIteratorAtPos(bufferText->get()); + // bufferText->addAfter(string_format("j %s", nd->getTargetText())); + // bufferText->addAfter(string_format("%s:", nd->getTargetText())); + // clearNodeToken(nd); + if (nd->getChildAtPos(nd->children_size() - 1)->_nodetype == onlyNode) { + + boolextern = false; + } + return; } - -void _visitstoreExtGlocalVariableNode(NodeToken *nd) -{ - - register_numl.duplicate(); - varType *v = nd->getVarType(); - int start = nd->stack_pos; - uint8_t regnum = 1; - if (nd->isPointer) - { - // start = nd->stack_pos; - regnum = point_regnum; - } - // string body = ""; - int savreg_num = point_regnum; - point_regnum = 6; // YBA 25-02-2025 - // register_numl++; - for (int h = 0; h < v->size - 1; h++) - { - start += v->sizes[h]; - } - if (nd->children_size() > 0 or !nd->isPointer or nd->asPointer) - { - for (int i = v->size - 1; i >= 0; i--) - { - bufferText->addAfter(bufferText->sp.pop(), string_format("%s %s%d,%s%d,%d", asmInstructionsName[v->store[i]].c_str(), getRegType(v->store[i], 0).c_str(), register_numl.get(), getRegType(v->store[i], 1).c_str(), point_regnum, start)); - // register_numl--; - start -= v->sizes[i]; - // bufferText->sp.push(bufferText->get()); - } - } - else - { - bufferText->addAfter(bufferText->sp.pop(), string_format("s32i a%d,a%d,%d", register_numl.get(), point_regnum, start)); - } - // res.f = f; - // res.header = number.header + h; - // bufferText->sp.push(bufferText->get()); - // bufferText->sp.swap(); - // bufferText->putIteratorAtPos(bufferText->sp.get()); - // bufferText->sp.displaystack("PILE"); - // bufferText->addAfter(bufferText->sp.l->front(),""); - bufferText->putIteratorAtPos(bufferText->sp.front()); - if (nd->children_size() == 1) - { - register_numl.duplicate(); - nd->getChildAtPos(0)->visitNode(); - register_numl.pop(); - if (nd->getChildAtPos(0)->getVarType() != NULL) - { - translateType(__int__, nd->getChildAtPos(0)->getVarType()->_varType, register_numl.get()); - } - else - { - // translateType(__int__, nd->getChildAtPos(0)->_token->_varType, register_numl.get()); - } - } - else if (nd->children_size() > 1) - { +void _visitargumentNode(NodeToken *nd) {} +void _visitextGlobalVariableNode(NodeToken *nd) { + register_numl.duplicate(); + if (nd->children_size() > 0) { + register_numl.duplicate(); + nd->getChildAtPos(0)->visitNode(); + register_numl.pop(); + } + varType *v = nd->getVarType(); + int start = nd->stack_pos; + // uint8_t regnum = 1; + if (nd->isPointer) { + // start = nd->stack_pos; + // regnum = point_regnum; + } + // string body = ""; + // register_numl++; + + if (!nd->isPointer) { + bufferText->addAfter( + string_format("movExt a%d,@_ext_%s", point_regnum, nd->getTokenText())); + } else { + if (nd->children_size() > 0) { + + if (nd->children_size() > 1) { /* for (int par = 0; par < nd->children_size(); par++) { register_numl.duplicate(); nd->getChildAtPos(par)->visitNode(); register_numl.pop(); - bufferText->addAfter(string_format("mov a%d,a%d", 10 + par, register_numl.get())); + bufferText->addAfter(string_format("mov a%d,a%d", 10 + par, + register_numl.get())); } - bufferText->addAfter(string_format("callExt a%d,map%dD", register_numl.get(), nd->children_size())); + bufferText->addAfter(string_format("callExt a%d,map%dD", + register_numl.get(), nd->children_size())); bufferText->addAfter(string_format("mov a%d,a10", register_numl.get())); */ vector tile; int nb = 0; string sd = string(nd->getTargetText()); - if (sd.compare(0, 1, "@") == 0) - { - tile = split(sd, " "); + if (sd.compare(0, 1, "@") == 0) { + tile = split(sd, " "); - sscanf(tile[0].c_str(), "@%d", &nb); + sscanf(tile[0].c_str(), "@%d", &nb); } - if (nb > 2 and sd.compare(0, 1, "@") == 0) - { - // if(nd->children>2) - bufferText->addAfter("movi a10,0"); + if (nb > 1 and sd.compare(0, 1, "@") == 0) { + bufferText->addAfter("movi a10,0"); } - for (int par = 0; par < nd->children_size(); par++) - { - // globalType.push(__int__); + for (int par = 0; par < nd->children_size(); par++) { + // globalType.push(__int__); + if (par > 0) { register_numl.duplicate(); nd->getChildAtPos(par)->visitNode(); register_numl.pop(); - translateType(__int__, nd->getChildAtPos(par)->getVarType()->_varType, register_numl.get()); - if (nb > 1) - { - if (par < nd->children_size() - 1) - { - - for (int h = 1; h < nd->children_size() - par; h++) - { - bufferText->addAfter(string_format("movi a11,%d", stringToInt((char *)tile[par + 1 + h].c_str()))); - // bufferText->addAfter(string_format("mull a11,a10,a11")); - if (nb > 2) - bufferText->addAfter(string_format("mull a%d,a%d,a11", register_numl.get(), register_numl.get())); - else - bufferText->addAfter(string_format("mull a10,a%d,a11", register_numl.get())); - } - if (nb > 2) - bufferText->addAfter(string_format("add a10,a10,a%d", register_numl.get())); - // else - // bufferText->addAfter("nop"); - } - else - { - - // bufferText->addAfter(string_format("mov a10,a%d", register_numl.get())); - bufferText->addAfter(string_format("add a%d,a10,a%d", register_numl.get(), register_numl.get())); - // else - // bufferText->addAfter(string_format("add a%d,a%d,a%d", register_numl.get(),register_numl.get(), register_numl.get())); - // if(nb<=2) - // bufferText->addAfter("nop"); - } + } + if (nd->getChildAtPos(par)->getVarType() != NULL) { + translateType(__int__, + nd->getChildAtPos(par)->getVarType()->_varType, + register_numl.get()); + } + if (nb > 1) { + if (par < nd->children_size() - 1) { + + for (int h = 1; h < nd->children_size() - par; h++) { + bufferText->addAfter(string_format( + "movi a11,%d", + stringToInt((char *)tile[par + 1 + h].c_str()))); + // bufferText->addAfter(string_format("mull a11,a10,a11")); + bufferText->addAfter(string_format("mull a%d,a%d,a11", + register_numl.get(), + register_numl.get())); + } + bufferText->addAfter( + string_format("add a10,a10,a%d", register_numl.get())); + } else { + bufferText->addAfter(string_format( + "add a%d,a10,a%d", register_numl.get(), register_numl.get())); } + } + + // globalType.pop(); + } + } else { + translateType(__int__, nd->getChildAtPos(0)->getVarType()->_varType, + register_numl.get()); + } + + bufferText->addAfter(string_format("movExt a%d,@_ext_%s", point_regnum, + nd->getTokenText())); + // f=f+number.f; + for (int i = 0; i < v->total_size; i++) { + bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, + point_regnum, register_numl.get())); + } + } else { + bufferText->addAfter(string_format( + "movExt a%d,@_ext_%s", register_numl.get(), nd->getTokenText())); + bufferText->sp.push(bufferText->get()); + bufferText->sp.push(bufferText->get()); + register_numl.pop(); + register_numl.decrease(); + return; + } + } + bufferText->sp.push(bufferText->get()); + for (int i = 0; i < v->size; i++) { + // bufferText->addAfter(string_format("%s %s%d,%s%d,%d", v->load[i].c_str(), + // v->reg_name.c_str(), register_numl.get(), v->reg_name.c_str(), + // point_regnum, start)); + asmInstruction asmInstr = v->load[i]; + bufferText->addAfter( + string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), point_regnum, start)); + // register_numl--; + start += v->sizes[i]; + bufferText->sp.push(bufferText->get()); + } + // res.f = f; + // res.header = number.header + h; + // point_regnum++; + register_numl.pop(); + register_numl.decrease(); + // res.register_numl=register_numl; + // res.register_numr=register_numr; + return; +} +// void _visitextDefFunctionNode(NodeToken *nd){} - // globalType.pop(); - } - } +void _visitextCallFunctionNode(NodeToken *nd) { + NodeToken *t = nd; + _visitCallFunctionTemplate(nd, 10, true); + int start = nd->stack_pos; + varType *v = _functions[nd->target]->getChildAtPos(0)->getVarType(); + if (v->_varType == __float__) { + bufferText->addAfter(string_format("wfr f%d,a%d", register_numl.get(), 10)); - if (safeMode && nd->isPointer) - { - bufferText->addAfter(string_format("l32r a%d,@_size_%s", regnum, nd->getTokenText())); - bufferText->addAfter(string_format("l32i a%d,a%d,0", regnum, regnum)); - bufferText->addAfter(string_format("bge a%d,a%d,__test_safe_%d", regnum, register_numl.get(), for_if_num)); - bufferText->addAfter(string_format("movi a10,%d", 0)); // nd->_token->line - bufferText->addAfter(string_format("mov a11,a%d", regnum)); - bufferText->addAfter(string_format("mov a12,a%d", register_numl.get())); - bufferText->addAfter("callExt a8,error"); - bufferText->addAfter("retw.n"); - bufferText->addAfter(string_format("__test_safe_%d:", for_if_num)); - for_if_num++; + bufferText->sp.push(bufferText->get()); + // globalType.push(__float__); + } else { + if (v->size > 1) { + for (int i = 0; i < v->size; i++) { + // bufferText->addAfter(string_format("mov a15,a10")); + if (i > -1) + bufferText->addAfter(string_format("extui a%d,a%d,%d,%d", + register_numl.get(), 10, start * 8, + v->sizes[i] * 8)); + else + bufferText->addAfter( + string_format("mov a%d,a10", register_numl.get())); + // register_numl--; + start += v->sizes[i]; + bufferText->sp.push(bufferText->get()); + } + } else { + if (v->_varType != __void__) { + bufferText->addAfter(string_format("mov a%d,a10", register_numl.get())); + } + bufferText->sp.push(bufferText->get()); } - if (!boolextern) - bufferText->addAfter(string_format("movExt a%d,@_ext_%s", point_regnum, nd->getTokenText())); - else - bufferText->addAfter(string_format("mov a%d,a7", point_regnum)); - if (nd->isPointer && nd->children_size() > 0) - { - // f=f+number.f; - for (int i = 0; i < v->total_size; i++) - { - bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, point_regnum, register_numl.get())); + } + register_numl.decrease(); +} +void _visitreturnArgumentNode(NodeToken *nd) {} +void _visitvariableDeclarationNode(NodeToken *nd) {} +void _visitdefExtFunctionNode(NodeToken *nd) { + header.addAfter(string_format("@_%s:", nd->getTokenText())); + header.addAfter(".bytes 4"); +} +void _visitinputArgumentsNode(NodeToken *nd) {} +void _visitdefInputArgumentsNode(NodeToken *nd) { + + if (nd->children_size() < 1) + return; + int sav = 9; + if (nd->children_size() >= + _TRIGGER) { // point_regnum; + // 17/01 // bufferText->addAfterNoDouble(string_format("l32r + // a%d,@_stack_%s", sav, nd->parent->getTokenText())); // + // point_regnum + // bufferText->addAfterNoDouble(string_format("l32r + // a%d,@_stack", sav)); + for (int i = 0; i < nd->children_size(); i++) { + // printf("ee\r\n"); + int start = nd->getChildAtPos(i)->stack_pos; + if (nd->getChildAtPos(i)->isPointer) { + // printf("ee p\r\n"); + int start = nd->getChildAtPos(i)->stack_pos; + bufferText->addAfter(string_format( + "l32i a15,a%d,%d", sav, start - _STACK_SIZE)); // point reg_bnum + bufferText->addAfter(string_format("s32i a15,a1,%d", start)); + } else { + // printf("ee j\r\n"); + for (int j = 0; j < nd->getChildAtPos(i)->getVarType()->size; j++) { + asmInstruction asmInstr = nd->getChildAtPos(i)->getVarType()->load[0]; + bufferText->addAfter(string_format( + "%s %s15,%s%d,%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), getRegType(asmInstr, 1).c_str(), + sav, start - _STACK_SIZE)); // point_regnum + asmInstr = nd->getChildAtPos(i)->getVarType()->store[0]; + bufferText->addAfter(string_format( + "%s %s15,%s1,%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), getRegType(asmInstr, 1).c_str(), + start)); + start += nd->getChildAtPos(i)->getVarType()->sizes[j]; + } + } + } + } else { + int reg_num = 2; + int stek = ((nd->parent->stack_pos) / 8 + 1) * 8 + 16 + _STACK_SIZE; + for (int i = 0; i < nd->children_size(); i++) { + int start = nd->getChildAtPos(i)->stack_pos; + if (start >= _STACK_SIZE) { + // printf("ee\r\n"); + if (reg_num <= 7) { + + // if (nd->getChildAtPos(i)->isPointer) + //{ + // printf("ee p\r\n"); + // int start = nd->getChildAtPos(i)->stack_pos; + // bufferText->addAfter(string_format("l32i a15,a%d,%d", sav, start - + // _STACK_SIZE)); // point reg_bnum + // + if (nd->getChildAtPos(i)->isPointer) { + bufferText->addAfter( + string_format("s32i a%d,a1,%d", reg_num, start)); + } else if (nd->getChildAtPos(i)->getVarType()->_varType == + __float__) { + bufferText->addAfter( + string_format("s32i a%d,a1,%d", reg_num, start)); + // bufferText->addAfter(string_format("%s %s15,%s1,%d", + // asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, + // 0).c_str(), getRegType(asmInstr, 1).c_str(), start)); + } else { + asmInstruction asmInstr = + nd->getChildAtPos(i)->getVarType()->store[0]; + bufferText->addAfter(string_format( + "%s a%d,a1,%d", asmInstructionsName[asmInstr].c_str(), reg_num, + start)); + } + // reg_num++; + } else { + asmInstruction asmInstr = nd->getChildAtPos(i)->getVarType()->load[0]; + bufferText->addAfter(string_format( + "%s a15,a1,%d", asmInstructionsName[asmInstr].c_str(), + stek + start - nd->getChildAtPos(6)->stack_pos)); + asmInstr = nd->getChildAtPos(i)->getVarType()->store[0]; + bufferText->addAfter(string_format( + "%s a15,a1,%d", asmInstructionsName[asmInstr].c_str(), start)); + } + } else if (nd->getChildAtPos(i)->getVarType()->_varType == __float__) { + } + reg_num++; + /* + } + else + { + // printf("ee j\r\n"); + for (int j = 0; j < nd->getChildAtPos(i)->getVarType()->size; j++) + { + asmInstruction asmInstr = + nd->getChildAtPos(i)->getVarType()->load[0]; + bufferText->addAfter(string_format("%s %s15,%s%d,%d", + asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), + getRegType(asmInstr, 1).c_str(), sav, start - _STACK_SIZE)); // + point_regnum asmInstr = nd->getChildAtPos(i)->getVarType()->store[0]; + bufferText->addAfter(string_format("%s %s15,%s1,%d", + asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), + getRegType(asmInstr, 1).c_str(), start)); start += + nd->getChildAtPos(i)->getVarType()->sizes[j]; + } + } + */ + } + } +} +void _visitdefExtGlobalVariableNode(NodeToken *nd) { + if (safeMode) { + if (nd->isPointer) { + header.addAfter(string_format("@_size_%s:", nd->getTokenText())); + header.addAfter( + _numToBytes(nd->_total_size / nd->getVarType()->total_size).c_str()); + } + } + header.addAfter(string_format("@_ext_%s:", nd->getTokenText())); + header.addAfter(".bytes 4"); +} +void _visitdefGlobalVariableNode(NodeToken *nd) { + if (strcmp(nd->getTokenText(), "_handle_") == 0 || + strcmp(nd->getTokenText(), "_execaddr_") == 0) + return; + if (safeMode) { + if (nd->isPointer) { + header.addAfter(string_format("@_size_%s:", nd->getTokenText())); + header.addAfter( + _numToBytes(nd->_total_size / nd->getVarType()->total_size).c_str()); + } + } + header.addAfter(string_format("@_%s:", nd->getTokenText())); + if (nd->children_size() == 0) { + header.addAfter(string_format(".bytes %d", nd->_total_size)); + } else { + _data_sav = ""; + if (nd->getVarType()->_varType == __CRGB__ or + nd->getVarType()->_varType == __CRGBW__) { + if (nd->children_size() > 0) { + for (NodeToken *ndt : *nd->children) { + if (ndt->children_size()) { + for (NodeToken *ndtc : *ndt->children) { + int __num = 0; + sscanf(ndtc->getTokenText(), "%d", &__num); + _data_sav = _data_sav + " " + string_format("%02x", __num); + } + } + } + } + } else if (nd->getVarType()->_varType == __char__) { + string str = string(nd->getChildAtPos(0)->getTokenText()); + nd->_total_size = + str.size() - 1; // we remove the size taken by " " adn we add 0 fo \0 + for (int i = 1; i < str.size() - 1; i++) { + _data_sav = _data_sav + " " + string_format("%02x", str.c_str()[i]); + } + _data_sav = _data_sav + " 00"; + } else { + uint32_t __num; + __num = 0; + uint8_t c; + if (nd->children_size() > 0) { + for (NodeToken *ndt : *nd->children) { + if (nd->getVarType()->_varType == __float__) { + float __f = 0; + sscanf(ndt->getTokenText(), "%f", &__f); + __num = (uint32_t)(*((uint32_t *)&__f)); + } else { + __num = 0; + sscanf(ndt->getTokenText(), "%lu", &__num); + } + for (int i = 0; i < nd->getVarType()->total_size; i++) { + c = __num & 0xff; + _data_sav = _data_sav + " " + string_format("%02x", c); + // val=val+'A'; + __num = __num / 256; + } } + } + } + header.addAfter( + string_format(".bytes %d%s", nd->_total_size, _data_sav.c_str())); + } +} +void _visitdefLocalVariableNode(NodeToken *nd) {} + +void _visitstoreLocalVariableNode(NodeToken *nd) { + // //printf("jjjkkj\n"); + varType *v = nd->getVarType(); + // globalType.push(__float__);//v->_varType); + // //printf("ona stocké:%d: + // %s\n",globalType.get(),varTypeEnumNames[globalType.get()].c_str()); + uint8_t regnum = 1; + if (nd->asPointer or (nd->isPointer && nd->children_size() == 0)) + point_regnum++; + if (nd->isPointer) { + // start = nd->stack_pos; + regnum = point_regnum; + } + int start = nd->stack_pos; + if (nd->isPointer) { + start = 0; + } + int numl = register_numl.get(); + for (int h = 0; h < v->size - 1; h++) { + start += v->sizes[h]; + } + if (nd->children_size() > 0 or !nd->isPointer or nd->asPointer) { + for (int i = v->size - 1; i >= 0; i--) { + // //printf("jjjkkj: %d\n",i); + // list::iterator l=bufferText->sp.pop(); + // bufferText->addAfter( bufferText->sp.pop(), string_format("%s + // %s%d,%s%d,%d", v->store[i].c_str(), v->reg_name.c_str(), numl, + // v->reg_name.c_str(), regnum, start)); + bufferText->addAfter( + bufferText->sp.pop(), + string_format("%s %s%d,%s%d,%d", + asmInstructionsName[v->store[i]].c_str(), + getRegType(v->store[i], 0).c_str(), numl, + getRegType(v->store[i], 1).c_str(), regnum, start)); + // numl++; + start -= v->sizes[i]; + } + } else { + bufferText->addAfter( + bufferText->sp.pop(), + string_format("s32i a%d,a%d,%d", numl, regnum, 0)); // start + // bufferText->addAfter(bufferText->sp.pop(), string_format("s32i + // a%d,a%d,%d", numl, 1,nd->stack_pos)); + } + if (nd->isPointer) { + register_numl.duplicate(); + bufferText->sp.push(bufferText->get()); + bufferText->sp.swap(); + bufferText->putIteratorAtPos(bufferText->sp.get()); + // bufferText->sp.displaystack("PILE"); + if (nd->children_size() > 0) { + globalType.push(__int__); + register_numl.duplicate(); + nd->getChildAtPos(0)->visitNode(); + register_numl.pop(); + globalType.pop(); + } + if (nd->children_size() > 0) { + bufferText->addAfter( + string_format("l32i a%d,a1,%d", point_regnum, nd->stack_pos)); + for (int i = 0; i < v->total_size; i++) { + bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, + point_regnum, register_numl.get())); + } + } else if (nd->asPointer) { + if (nd->type == TokenUserDefinedVariableMember or + nd->type == TokenUserDefinedVariableMemberFunction) { + + if (isStructFunction) { + bufferText->addAfter(string_format("addi a%d,a%d,%d", point_regnum, 2, + (int)(nd->stack_pos / 1000))); + } else { + bufferText->addAfter(string_format( + "l32i a%d,a1,%d", point_regnum, + nd->stack_pos - (int)(nd->stack_pos / 1000) * 1000)); + bufferText->addAfter(string_format("addi a%d,a%d,%d", point_regnum, + point_regnum, + (int)(nd->stack_pos / 1000))); + } + } else + + bufferText->addAfter( + string_format("l32i a%d,a1,%d", point_regnum, nd->stack_pos)); + } else { + // a remettre + bufferText->addAfter( + string_format("addi a%d,a1,%d", point_regnum, nd->stack_pos)); } bufferText->end(); bufferText->sp.pop(); - //; + // point_regnum++; register_numl.pop(); - point_regnum = savreg_num; // point_regnum--; - // res.register_numl=register_numl; - // res.register_numr=register_numr; - return; + if (nd->asPointer or (nd->isPointer && nd->children_size() == 0)) + point_regnum--; + } +} +void _visitstoreGlobalVariableNode(NodeToken *nd) { + + register_numl.duplicate(); + varType *v = nd->getVarType(); + int start = nd->stack_pos; + // uint8_t regnum = 1; + // int savreg_num=point_regnum; + if (nd->asPointer or (nd->isPointer)) // && nd->children_size() == 0)) + point_regnum++; + int savreg_num = point_regnum; + point_regnum = 6; // YBA 25-02-20252 + if (nd->isPointer) { + // start = nd->stack_pos; + // regnum = point_regnum; + } + // string body = ""; + // register_numl++; + for (int h = 0; h < v->size - 1; h++) { + start += v->sizes[h]; + } + int j = bufferText->sp.get(); + if ((nd->children_size() > 0 or !nd->isPointer)) { + // if (nd->target == EOF_TEXTARRAY) + // { + + for (int i = v->size - 1; i >= 0; i--) { + // printf("here in store %d\r\n",i); + // bufferText->addAfter(bufferText->sp.pop(), string_format("%s + // %s%d,%s%d,%d", v->store[i].c_str(), v->reg_name.c_str(), + // register_numl.get(), v->reg_name.c_str(), point_regnum, start)); + bufferText->addAfter( + bufferText->sp.pop(), + string_format( + "%s %s%d,%s%d,%d", asmInstructionsName[v->store[i]].c_str(), + getRegType(v->store[i], 0).c_str(), register_numl.get(), + getRegType(v->store[i], 1).c_str(), point_regnum, start)); + // register_numl--; + // printf("here in store %d\r\n",i); + start -= v->sizes[i - 1]; + // bufferText->sp.push(bufferText->get()); + } + } + + else { + bufferText->addAfter(bufferText->sp.pop(), + string_format("s32i a%d,a%d,%d", register_numl.get(), + point_regnum, start)); + } + // res.f = f; + // res.header = number.header + h; + bufferText->sp.push(bufferText->get()); + bufferText->sp.swap(); + if (v->size > 0) + bufferText->putIteratorAtPos(bufferText->sp.get()); + else + bufferText->putIteratorAtPos(j); + if (nd->children_size() > 0) { + vector tile; + int nb = 0; + string sd = string(nd->getTargetText()); + if (sd.compare(0, 1, "@") == 0) { + tile = split(sd, " "); + + sscanf(tile[0].c_str(), "@%d", &nb); + } + if (nb > 1 and sd.compare(0, 1, "@") == 0) { + bufferText->addAfter("movi a10,0"); + } + for (int i = 0; i < nd->children_size(); i++) { + globalType.push(__int__); + register_numl.duplicate(); + nd->getChildAtPos(i)->visitNode(); + register_numl.pop(); + translateType(__int__, nd->getChildAtPos(i)->getVarType()->_varType, + register_numl.get()); + if (nb > 1) { + if (i < nd->children_size() - 1) { + + for (int h = 1; h < nd->children_size() - i; h++) { + bufferText->addAfter(string_format( + "movi a11,%d", stringToInt((char *)tile[i + 1 + h].c_str()))); + // bufferText->addAfter(string_format("mull a11,a10,a11")); + bufferText->addAfter(string_format( + "mull a%d,a%d,a11", register_numl.get(), register_numl.get())); + } + bufferText->addAfter( + string_format("add a10,a10,a%d", register_numl.get())); + } else { + bufferText->addAfter(string_format( + "add a%d,a10,a%d", register_numl.get(), register_numl.get())); + } + } + + globalType.pop(); + } + } + + if (safeMode && nd->isPointer) { + bufferText->addAfter( + string_format("l32r a%d,@_size_%s", point_regnum, nd->getTokenText())); + bufferText->addAfter( + string_format("l32i a%d,a%d,0", point_regnum, point_regnum)); + bufferText->addAfter(string_format("bge a%d,a%d,__test_safe_%d", + point_regnum, register_numl.get(), + for_if_num)); + bufferText->addAfter(string_format("movi a10,%d", 0)); + bufferText->addAfter(string_format("mov a11,a%d", point_regnum)); + bufferText->addAfter(string_format("mov a12,a%d", register_numl.get())); + bufferText->addAfter("callExt a8,error"); + bufferText->addAfter("retw.n"); + bufferText->addAfter(string_format("__test_safe_%d:", for_if_num)); + for_if_num++; + } + + if (nd->isPointer && nd->children_size() > 0) { + + if (nd->type == TokenUserDefinedVariableMember or + nd->type == TokenUserDefinedVariableMemberFunction) { + bufferText->addAfter( + string_format("movi a%d,%d", point_regnum, nd->_total_size)); + bufferText->addAfter(string_format("mull a%d,a%d,a%d", + register_numl.get(), + register_numl.get(), point_regnum)); + bufferText->addAfter( + string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); + bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, + point_regnum, register_numl.get())); + } else if (v->total_size > 4) { + + // string tmp=content.l->back(); + // content.l->pop_back(); + bufferText->addAfter( + string_format("movi a%d,%d", point_regnum, v->total_size)); + bufferText->addAfter(string_format("mull a%d,a%d,a%d", + register_numl.get(), + register_numl.get(), point_regnum)); + bufferText->addAfter( + string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); + bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, + point_regnum, register_numl.get())); + } else { + bufferText->addAfter( + string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); + for (int i = 0; i < v->total_size; i++) { + bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, + point_regnum, register_numl.get())); + } + } + } else { + bufferText->addAfter( + string_format("l32r a%d,@_%s", point_regnum, nd->getTokenText())); + } + bufferText->end(); + if (isStructFunction) { + bufferText->addAfter(string_format("s32i a2,a1,%d", _STACK_SIZE)); + } + bufferText->sp.pop(); + // if(!nd->isPointer) + // point_regnum++; + register_numl.pop(); + point_regnum = savreg_num; + if (nd->asPointer or (nd->isPointer)) // && nd->children_size() == 0)) + point_regnum--; + // point_regnum--; + // res.register_numl=register_numl; + // res.register_numr=register_numr; + + return; } -void _visitifNode(NodeToken *nd) -{ - // printf("oo\n"); - point_regnum = 5; - _compare.push_back(bufferText->get()); - // printf("oo1\n"); - // bufferText->addAfter( string_format("%s:\n",nd->target.c_str())); - +void _visitstoreExtGlocalVariableNode(NodeToken *nd) { + + register_numl.duplicate(); + varType *v = nd->getVarType(); + int start = nd->stack_pos; + uint8_t regnum = 1; + if (nd->isPointer) { + // start = nd->stack_pos; + regnum = point_regnum; + } + // string body = ""; + int savreg_num = point_regnum; + point_regnum = 6; // YBA 25-02-2025 + // register_numl++; + for (int h = 0; h < v->size - 1; h++) { + start += v->sizes[h]; + } + if (nd->children_size() > 0 or !nd->isPointer or nd->asPointer) { + for (int i = v->size - 1; i >= 0; i--) { + bufferText->addAfter( + bufferText->sp.pop(), + string_format( + "%s %s%d,%s%d,%d", asmInstructionsName[v->store[i]].c_str(), + getRegType(v->store[i], 0).c_str(), register_numl.get(), + getRegType(v->store[i], 1).c_str(), point_regnum, start)); + // register_numl--; + start -= v->sizes[i]; + // bufferText->sp.push(bufferText->get()); + } + } else { + bufferText->addAfter(bufferText->sp.pop(), + string_format("s32i a%d,a%d,%d", register_numl.get(), + point_regnum, start)); + } + // res.f = f; + // res.header = number.header + h; + // bufferText->sp.push(bufferText->get()); + // bufferText->sp.swap(); + // bufferText->putIteratorAtPos(bufferText->sp.get()); + // bufferText->sp.displaystack("PILE"); + // bufferText->addAfter(bufferText->sp.l->front(),""); + bufferText->putIteratorAtPos(bufferText->sp.front()); + if (nd->children_size() == 1) { register_numl.duplicate(); - // printf("oo2bis\n"); - - if (nd->children_size() > 2) - { - nd->getChildAtPos(1)->visitNode(); - nd->getChildAtPos(2)->visitNode(); - } - else + nd->getChildAtPos(0)->visitNode(); + register_numl.pop(); + if (nd->getChildAtPos(0)->getVarType() != NULL) { + translateType(__int__, nd->getChildAtPos(0)->getVarType()->_varType, + register_numl.get()); + } else { + // translateType(__int__, nd->getChildAtPos(0)->_token->_varType, + // register_numl.get()); + } + } else if (nd->children_size() > 1) { + /* + for (int par = 0; par < nd->children_size(); par++) { - nd->getChildAtPos(1)->visitNode(); + register_numl.duplicate(); + nd->getChildAtPos(par)->visitNode(); + register_numl.pop(); + bufferText->addAfter(string_format("mov a%d,a%d", 10 + par, + register_numl.get())); } - register_numl.pop(); - // printf("oo2\n"); + bufferText->addAfter(string_format("callExt a%d,map%dD", + register_numl.get(), nd->children_size())); + bufferText->addAfter(string_format("mov a%d,a10", register_numl.get())); +*/ + vector tile; + int nb = 0; + string sd = string(nd->getTargetText()); + if (sd.compare(0, 1, "@") == 0) { + tile = split(sd, " "); + + sscanf(tile[0].c_str(), "@%d", &nb); + } + if (nb > 2 and sd.compare(0, 1, "@") == 0) { + // if(nd->children>2) + bufferText->addAfter("movi a10,0"); + } + for (int par = 0; par < nd->children_size(); par++) { + // globalType.push(__int__); + register_numl.duplicate(); + nd->getChildAtPos(par)->visitNode(); + register_numl.pop(); + translateType(__int__, nd->getChildAtPos(par)->getVarType()->_varType, + register_numl.get()); + if (nb > 1) { + if (par < nd->children_size() - 1) { + + for (int h = 1; h < nd->children_size() - par; h++) { + bufferText->addAfter(string_format( + "movi a11,%d", stringToInt((char *)tile[par + 1 + h].c_str()))); + // bufferText->addAfter(string_format("mull a11,a10,a11")); + if (nb > 2) + bufferText->addAfter(string_format("mull a%d,a%d,a11", + register_numl.get(), + register_numl.get())); + else + bufferText->addAfter( + string_format("mull a10,a%d,a11", register_numl.get())); + } + if (nb > 2) + bufferText->addAfter( + string_format("add a10,a10,a%d", register_numl.get())); + // else + // bufferText->addAfter("nop"); + } else { + + // bufferText->addAfter(string_format("mov a10,a%d", + // register_numl.get())); + bufferText->addAfter(string_format( + "add a%d,a10,a%d", register_numl.get(), register_numl.get())); + // else + // bufferText->addAfter(string_format("add a%d,a%d,a%d", + // register_numl.get(),register_numl.get(), register_numl.get())); + // if(nb<=2) + // bufferText->addAfter("nop"); + } + } + + // globalType.pop(); + } + } + + if (safeMode && nd->isPointer) { + bufferText->addAfter( + string_format("l32r a%d,@_size_%s", regnum, nd->getTokenText())); + bufferText->addAfter(string_format("l32i a%d,a%d,0", regnum, regnum)); + bufferText->addAfter(string_format("bge a%d,a%d,__test_safe_%d", regnum, + register_numl.get(), for_if_num)); + bufferText->addAfter(string_format("movi a10,%d", 0)); // nd->_token->line + bufferText->addAfter(string_format("mov a11,a%d", regnum)); + bufferText->addAfter(string_format("mov a12,a%d", register_numl.get())); + bufferText->addAfter("callExt a8,error"); + bufferText->addAfter("retw.n"); + bufferText->addAfter(string_format("__test_safe_%d:", for_if_num)); + for_if_num++; + } + if (!boolextern) + bufferText->addAfter( + string_format("movExt a%d,@_ext_%s", point_regnum, nd->getTokenText())); + else + bufferText->addAfter(string_format("mov a%d,a7", point_regnum)); + if (nd->isPointer && nd->children_size() > 0) { + // f=f+number.f; + for (int i = 0; i < v->total_size; i++) { + bufferText->addAfter(string_format("add a%d,a%d,a%d", point_regnum, + point_regnum, register_numl.get())); + } + } + bufferText->end(); + bufferText->sp.pop(); + //; + register_numl.pop(); + point_regnum = savreg_num; + // point_regnum--; + // res.register_numl=register_numl; + // res.register_numr=register_numr; + return; +} - nd->getChildAtPos(0)->_total_size = (bufferText->get() - _compare.back()) * 3; // on estime que toutes les instructions ont la meme taille - bufferText->addAfter(string_format("%s_end:", nd->getTargetText())); - // printf("oo3\n"); - bufferText->putIteratorAtPos(_compare.back()); - // bufferText->addAfter(""); - // bufferText->sp.push(bufferText->get()); - // printf("oo4\n"); - register_numl.duplicate(); - intest = true; - nd->getChildAtPos(0)->visitNode(); - intest = false; - register_numl.pop(); - // printf("oo5n"); - bufferText->putIteratorAtPos(bufferText->get()); - _compare.pop_back(); +void _visitifNode(NodeToken *nd) { + // printf("oo\n"); + point_regnum = 5; + _compare.push_back(bufferText->get()); + // printf("oo1\n"); + // bufferText->addAfter( string_format("%s:\n",nd->target.c_str())); + + register_numl.duplicate(); + // printf("oo2bis\n"); + + if (nd->children_size() > 2) { + nd->getChildAtPos(1)->visitNode(); + nd->getChildAtPos(2)->visitNode(); + } else { + nd->getChildAtPos(1)->visitNode(); + } + register_numl.pop(); + // printf("oo2\n"); + + nd->getChildAtPos(0)->_total_size = + (bufferText->get() - _compare.back()) * + 3; // on estime que toutes les instructions ont la meme taille + bufferText->addAfter(string_format("%s_end:", nd->getTargetText())); + // printf("oo3\n"); + bufferText->putIteratorAtPos(_compare.back()); + // bufferText->addAfter(""); + // bufferText->sp.push(bufferText->get()); + // printf("oo4\n"); + register_numl.duplicate(); + intest = true; + nd->getChildAtPos(0)->visitNode(); + intest = false; + register_numl.pop(); + // printf("oo5n"); + bufferText->putIteratorAtPos(bufferText->get()); + _compare.pop_back(); } -void _visitelseNode(NodeToken *nd) -{ - point_regnum = 5; - bufferText->addBefore(string_format("j %s", nd->getTargetText())); - // register_numl.duplicate(); +void _visitelseNode(NodeToken *nd) { + point_regnum = 5; + bufferText->addBefore(string_format("j %s", nd->getTargetText())); + // register_numl.duplicate(); - register_numl.duplicate(); - if (nd->children_size() > 1) - { - nd->getChildAtPos(1)->visitNode(); - } - else - { - nd->getChildAtPos(0)->visitNode(); - } - register_numl.pop(); + register_numl.duplicate(); + if (nd->children_size() > 1) { + nd->getChildAtPos(1)->visitNode(); + } else { + nd->getChildAtPos(0)->visitNode(); + } + register_numl.pop(); - bufferText->addAfter(string_format("%s:", nd->getTargetText())); + bufferText->addAfter(string_format("%s:", nd->getTargetText())); } -void _visitwhileNode(NodeToken *nd) -{ - point_regnum = 5; - bufferText->addAfter(string_format("%s_while:", nd->getTargetText())); - bufferText->addAfter(string_format("%s_continue:", nd->getTargetText())); - _compare.push_back(bufferText->get()); +void _visitwhileNode(NodeToken *nd) { + point_regnum = 5; + bufferText->addAfter(string_format("%s_while:", nd->getTargetText())); + bufferText->addAfter(string_format("%s_continue:", nd->getTargetText())); + _compare.push_back(bufferText->get()); - // bufferText->addAfter( string_format("%s:\n",nd->target.c_str())); + // bufferText->addAfter( string_format("%s:\n",nd->target.c_str())); - register_numl.duplicate(); - if (nd->children_size() > 2) - { - nd->getChildAtPos(1)->visitNode(); - nd->getChildAtPos(2)->visitNode(); - } - else - { - nd->getChildAtPos(1)->visitNode(); - } - register_numl.pop(); + register_numl.duplicate(); + if (nd->children_size() > 2) { + nd->getChildAtPos(1)->visitNode(); + nd->getChildAtPos(2)->visitNode(); + } else { + nd->getChildAtPos(1)->visitNode(); + } + register_numl.pop(); - bufferText->addAfter(string_format("j %s_while", nd->getTargetText())); - nd->getChildAtPos(0)->_total_size = (bufferText->get() - _compare.back()) * 3; // on estime que toutes les instructions ont la meme taille + bufferText->addAfter(string_format("j %s_while", nd->getTargetText())); + nd->getChildAtPos(0)->_total_size = + (bufferText->get() - _compare.back()) * + 3; // on estime que toutes les instructions ont la meme taille - bufferText->addAfter(string_format("%s_end:", nd->getTargetText())); - bufferText->addAfter(string_format("%s_break:", nd->getTargetText())); - if (nd->getChildAtPos(0)->getChildAtPos(0)->_nodetype != numberNode) - { - bufferText->putIteratorAtPos(_compare.back()); - intest = true; - nd->getChildAtPos(0)->visitNode(); - intest = false; - register_numl.pop(); - bufferText->putIteratorAtPos(bufferText->get()); - } - _compare.pop_back(); + bufferText->addAfter(string_format("%s_end:", nd->getTargetText())); + bufferText->addAfter(string_format("%s_break:", nd->getTargetText())); + if (nd->getChildAtPos(0)->getChildAtPos(0)->_nodetype != numberNode) { + bufferText->putIteratorAtPos(_compare.back()); + intest = true; + nd->getChildAtPos(0)->visitNode(); + intest = false; + register_numl.pop(); + bufferText->putIteratorAtPos(bufferText->get()); + } + _compare.pop_back(); } -void _visitreturnNode(NodeToken *nd) -{ - NodeToken *t = nd; - while (t->_nodetype != defFunctionNode and t->parent != NULL) - { - t = t->parent; - } - t = t->getChildAtPos(0); - if (t->getVarType()->size > 1) - { - bufferText->addAfter(string_format("l32r a%d,@_stackr", 9)); // point_regnum - for (int i = 0; i < nd->children_size(); i++) - { - globalType.push(t->getVarType()->_varType); - register_numl.duplicate(); - nd->getChildAtPos(i)->visitNode(); - register_numl.pop(); - int start = nd->stack_pos + t->getVarType()->total_size; - int tot = t->getVarType()->size - 1; - // bufferText->addBefore(bufferText->sp.get(),string_format("l32r a%d,@_stackr", 8)); - for (int j = 0; j < t->getVarType()->size; j++) - { - start -= t->getVarType()->sizes[tot - j]; - asmInstruction asmInstr = t->getVarType()->store[tot - j]; - bufferText->addAfter(bufferText->sp.pop(), string_format("%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), getRegType(asmInstr, 0).c_str(), register_numl.get(), getRegType(asmInstr, 1).c_str(), 9, start)); // point_regnum - } - globalType.pop(); - } - } - else - { - for (int i = 0; i < nd->children_size(); i++) - { - globalType.push(t->getVarType()->_varType); - register_numl.duplicate(); - nd->getChildAtPos(i)->visitNode(); - register_numl.pop(); - translateType(t->getVarType()->_varType, nd->getChildAtPos(i)->getVarType()->_varType, register_numl.get()); - // int start = nd->stack_pos + t->getVarType()->total_size; - // int tot = t->getVarType()->size - 1; - if (t->getVarType()->_varType == __float__) - { - // bufferText->addAfter(bufferText->sp.pop(), string_format("mov.s f2,f%d", register_numl.get())); - bufferText->addAfter(string_format("mov.s f2,f%d", register_numl.get())); - } - else - { - // bufferText->addAfter(bufferText->sp.pop(), string_format("mov a2,a%d", register_numl.get())); - bufferText->addAfter(string_format("mov a2,a%d", register_numl.get())); - } - - globalType.pop(); - } - } - if (saveReg) - { - bufferText->addAfter("lsi f15,a1,16"); - bufferText->addAfter("lsi f14,a1,20"); - bufferText->addAfter("lsi f13,a1,24"); - } - if (saveRegAbs) - { - bufferText->addAfter("l32i a15,a1,16"); - bufferText->addAfter("l32i a14,a1,20"); - bufferText->addAfter("l32i a13,a1,24"); - } - bufferText->addAfter("retw.n"); +void _visitreturnNode(NodeToken *nd) { + NodeToken *t = nd; + while (t->_nodetype != defFunctionNode and t->parent != NULL) { + t = t->parent; + } + t = t->getChildAtPos(0); + if (t->getVarType()->size > 1) { + bufferText->addAfter(string_format("l32r a%d,@_stackr", 9)); // point_regnum + for (int i = 0; i < nd->children_size(); i++) { + globalType.push(t->getVarType()->_varType); + register_numl.duplicate(); + nd->getChildAtPos(i)->visitNode(); + register_numl.pop(); + int start = nd->stack_pos + t->getVarType()->total_size; + int tot = t->getVarType()->size - 1; + // bufferText->addBefore(bufferText->sp.get(),string_format("l32r + // a%d,@_stackr", 8)); + for (int j = 0; j < t->getVarType()->size; j++) { + start -= t->getVarType()->sizes[tot - j]; + asmInstruction asmInstr = t->getVarType()->store[tot - j]; + bufferText->addAfter( + bufferText->sp.pop(), + string_format( + "%s %s%d,%s%d,%d", asmInstructionsName[asmInstr].c_str(), + getRegType(asmInstr, 0).c_str(), register_numl.get(), + getRegType(asmInstr, 1).c_str(), 9, start)); // point_regnum + } + globalType.pop(); + } + } else { + for (int i = 0; i < nd->children_size(); i++) { + globalType.push(t->getVarType()->_varType); + register_numl.duplicate(); + nd->getChildAtPos(i)->visitNode(); + register_numl.pop(); + translateType(t->getVarType()->_varType, + nd->getChildAtPos(i)->getVarType()->_varType, + register_numl.get()); + // int start = nd->stack_pos + t->getVarType()->total_size; + // int tot = t->getVarType()->size - 1; + if (t->getVarType()->_varType == __float__) { + // bufferText->addAfter(bufferText->sp.pop(), string_format("mov.s + // f2,f%d", register_numl.get())); + bufferText->addAfter( + string_format("mov.s f2,f%d", register_numl.get())); + } else { + // bufferText->addAfter(bufferText->sp.pop(), string_format("mov + // a2,a%d", register_numl.get())); + bufferText->addAfter(string_format("mov a2,a%d", register_numl.get())); + } + + globalType.pop(); + } + } + if (saveReg) { + bufferText->addAfter("lsi f15,a1,16"); + bufferText->addAfter("lsi f14,a1,20"); + bufferText->addAfter("lsi f13,a1,24"); + } + if (saveRegAbs) { + bufferText->addAfter("l32i a15,a1,16"); + bufferText->addAfter("l32i a14,a1,20"); + bufferText->addAfter("l32i a13,a1,24"); + } + bufferText->addAfter("retw.n"); } -void _visitdefAsmFunctionNode(NodeToken *nd) -{ - bufferText = &content; - header.addAfter(string_format(".global @_%s", nd->getTokenText())); - bufferText->addAfter(string_format("@_%s:", nd->getTokenText())); - header.addAfter(string_format("@_stack_%s:", nd->getTokenText())); - header.addAfter(string_format(".bytes %d", (nd->getChildAtPos(1)->children_size() + 1) * 4)); - - // bufferText->addAfter(string_format("entry a1,%d", 80)); // ((nd->stack_pos) / 8 + 1) * 8) - if (nd->children_size() >= 3) - { - nd->getChildAtPos(2)->visitNode(); - } // f = f + g.f; - // h = h + g.header; - bufferText = &footer; +void _visitdefAsmFunctionNode(NodeToken *nd) { + bufferText = &content; + header.addAfter(string_format(".global @_%s", nd->getTokenText())); + bufferText->addAfter(string_format("@_%s:", nd->getTokenText())); + header.addAfter(string_format("@_stack_%s:", nd->getTokenText())); + header.addAfter(string_format( + ".bytes %d", (nd->getChildAtPos(1)->children_size() + 1) * 4)); + + // bufferText->addAfter(string_format("entry a1,%d", 80)); // ((nd->stack_pos) + // / 8 + 1) * 8) + if (nd->children_size() >= 3) { + nd->getChildAtPos(2)->visitNode(); + } // f = f + g.f; + // h = h + g.header; + bufferText = &footer; } -void _visitstringNode(NodeToken *nd) -{ - string target = string(nd->getTokenText()); - bufferText->addAfter(string_format("%s", target.substr(1, target.size() - 2).c_str())); +void _visitstringNode(NodeToken *nd) { + string target = string(nd->getTokenText()); + bufferText->addAfter( + string_format("%s", target.substr(1, target.size() - 2).c_str())); } -void _visitchangeTypeNode(NodeToken *nd) -{ +void _visitchangeTypeNode(NodeToken *nd) { - globalType.push(nd->getVarType()->_varType); - for (int i = 0; i < nd->children_size(); i++) - { - - register_numl.duplicate(); - nd->getChildAtPos(i)->visitNode(); + globalType.push(nd->getVarType()->_varType); + for (int i = 0; i < nd->children_size(); i++) { - register_numl.pop(); - // f = f + g.f; - // h = h + g.header; + register_numl.duplicate(); + nd->getChildAtPos(i)->visitNode(); - if (nd->getChildAtPos(i)->getVarType() != NULL) - { - // register_numl.pop(); - if (strlen(nd->getChildAtPos(i)->getTokenText()) > 0) - translateType(globalType.get(), nd->getChildAtPos(i)->getVarType()->_varType, register_numl.get()); - else - translateType(globalType.get(), nd->getChildAtPos(i)->getVarType()->_varType, register_numl.get()); - } - else - { - // translateType(globalType.get(), nd->getChildAtPos(i)->_token->_varType, register_numl.get()); - } - register_numl.decrease(); + register_numl.pop(); + // f = f + g.f; + // h = h + g.header; + + if (nd->getChildAtPos(i)->getVarType() != NULL) { + // register_numl.pop(); + if (strlen(nd->getChildAtPos(i)->getTokenText()) > 0) + translateType(globalType.get(), + nd->getChildAtPos(i)->getVarType()->_varType, + register_numl.get()); + else + translateType(globalType.get(), + nd->getChildAtPos(i)->getVarType()->_varType, + register_numl.get()); + } else { + // translateType(globalType.get(), nd->getChildAtPos(i)->_token->_varType, + // register_numl.get()); } - // varTypeEnum s = globalType.pop(); - globalType.pop(); + register_numl.decrease(); + } + // varTypeEnum s = globalType.pop(); + globalType.pop(); } void _visitimportNode(NodeToken *nd) {} -void _visitcontinueNode(NodeToken *nd) -{ - bufferText->addAfter(string_format("j %s_continue", nd->getTargetText())); +void _visitcontinueNode(NodeToken *nd) { + bufferText->addAfter(string_format("j %s_continue", nd->getTargetText())); } -void _visitbreakNode(NodeToken *nd) -{ +void _visitbreakNode(NodeToken *nd) { - bufferText->addAfter(string_format("j %s_break", nd->getTargetText())); + bufferText->addAfter(string_format("j %s_break", nd->getTargetText())); } void _visitUnknownNode(NodeToken *nd) {} -void optimize(Text *text) -{ - // return; - // int regnum; - for (int regnum = 3; regnum < 11; regnum++) - { - // regnum=9; - string str = "__"; - string registername = string_format("a%d", regnum); - // printf("on test register name %s\r\n", registername.c_str()); - for (int i = 0; i < text->size(); i++) - { - string tmp = string((*text->getChildAtPos(i))); - - if (tmp.size() > 0) - { - if (tmp.compare(0, 2, "@_") == 0) - { - // printf("restart \r\n"); - str = "__"; - } - else if (tmp.find(":") != -1) - { - str = "__"; - } - /* - else if(tmp.find("callExt")!=-1) - { - str="__"; - }*/ - else - { - vector d = split(tmp, " "); - if (d[0].compare("call8") == 0 and regnum >= 8) - { - str = "_"; - } - else if (d.size() > 1) - { - if (d[1].compare(0, registername.size(), registername) == 0) - { - // printf("on a %s %s\r\n", d[1].c_str(), registername.c_str()); - // if (!(str.compare("__")== 0)) - //{ - if (str.compare(tmp) == 0) - { - // printf("%s found \r\n", tmp.c_str()); - text->replaceText(i, " "); - //") - } - //} - else - { - if (d[0].compare("movi") == 0 or d[0].compare("l32i") == 0 or d[0].compare("l32r") == 0 or d[0].compare("l16i") == 0 or d[0].compare("l16ui") == 0 or d[0].compare("l8ui") == 0 or d[0].compare("movExt") == 0) - { - str = tmp; - } - else - { - str = "__"; - } - } - } - } - d.clear(); - d.shrink_to_fit(); - } - } - } - } - - vector d; - vector d2; - vector d1; - string before = " "; - int indexbefore = 0; - - ; - - before = " "; - indexbefore = 0; - // on va dealer abec les floats - string torep = " "; - - for (int i = 0; i < text->size(); i++) - { - - string tmp = string((*text->getChildAtPos(i))); - if (*text->getChildAtPos(i) == NULL) - { - printf("************************** %d \n\r", i); +void optimize(Text *text) { + // return; + // int regnum; + for (int regnum = 3; regnum < 11; regnum++) { + // regnum=9; + string str = "__"; + string registername = string_format("a%d", regnum); + // printf("on test register name %s\r\n", registername.c_str()); + for (int i = 0; i < text->size(); i++) { + string tmp = string((*text->getChildAtPos(i))); + + if (tmp.size() > 0) { + if (tmp.compare(0, 2, "@_") == 0) { + // printf("restart \r\n"); + str = "__"; + } else if (tmp.find(":") != -1) { + str = "__"; } - if (tmp.compare(" ") != 0 && tmp != "") - { - d = split(tmp, " "); - if (d[0].compare("float.s") == 0) - { - if (tmp.compare(before) == 0) - { - text->replaceText(indexbefore, " "); - before = " "; - } - else - { - before = tmp; - indexbefore = i; - d2 = split(d[1], ","); - torep = d2[1]; + /* + else if(tmp.find("callExt")!=-1) + { + str="__"; + }*/ + else { + vector d = split(tmp, " "); + if (d[0].compare("call8") == 0 and regnum >= 8) { + str = "_"; + } else if (d.size() > 1) { + if (d[1].compare(0, registername.size(), registername) == 0) { + // printf("on a %s %s\r\n", d[1].c_str(), registername.c_str()); + // if (!(str.compare("__")== 0)) + //{ + if (str.compare(tmp) == 0) { + // printf("%s found \r\n", tmp.c_str()); + text->replaceText(i, " "); + //") + } + //} + else { + if (d[0].compare("movi") == 0 or d[0].compare("l32i") == 0 or + d[0].compare("l32r") == 0 or d[0].compare("l16i") == 0 or + d[0].compare("l16ui") == 0 or d[0].compare("l8ui") == 0 or + d[0].compare("movExt") == 0) { + str = tmp; + } else { + str = "__"; } + } } - else if (d[0].find_first_of(":") != std::string::npos or d[0].compare("call8") == 0 or d[0].compare("callExt") == 0) - { - before = " "; - torep = " "; + } + d.clear(); + d.shrink_to_fit(); + } + } + } + } + + vector d; + vector d2; + vector d1; + string before = " "; + int indexbefore = 0; + + ; + + before = " "; + indexbefore = 0; + // on va dealer abec les floats + string torep = " "; + + for (int i = 0; i < text->size(); i++) { + + string tmp = string((*text->getChildAtPos(i))); + if (*text->getChildAtPos(i) == NULL) { + printf("************************** %d \n\r", i); + } + if (tmp.compare(" ") != 0 && tmp != "") { + d = split(tmp, " "); + if (d[0].compare("float.s") == 0) { + if (tmp.compare(before) == 0) { + text->replaceText(indexbefore, " "); + before = " "; + } else { + before = tmp; + indexbefore = i; + d2 = split(d[1], ","); + torep = d2[1]; + } + } else if (d[0].find_first_of(":") != std::string::npos or + d[0].compare("call8") == 0 or d[0].compare("callExt") == 0) { + before = " "; + torep = " "; + } else if (d.size() > 1) { + + d2 = split(d[1], ","); + if (d2[0].compare(torep) == 0) { + before = " "; + torep = " "; + } + } + } + } + d.clear(); + d1.clear(); + d2.clear(); + + before = " "; + indexbefore = 0; + + for (int i = 0; i < text->size(); i++) { + if (*text->getChildAtPos(i) != NULL) { + string tmp = string((*text->getChildAtPos(i))); + if (tmp.compare(" ") != 0 && tmp != "") { + vector d2 = split(tmp, " "); + // printf("on regarde %s %s\n\r",before.c_str(),tmp.c_str()); + if (d2[0].compare("mov") == 0) { + // printf("on regarde mov |%s|%s|\n\r",before.c_str(),tmp.c_str()); + vector d = split(before, " "); + + if (d[0].compare("mull") == 0 or d[0].compare("quou") == 0 or + d[0].compare("quos") == 0 or d[0].compare("sub") == 0 or + d[0].compare("add") == 0 or d[0].compare("mov") == 0 or + d[0].compare("movi") == 0 or d[0].compare("l32i") == 0 or + d[0].compare("l32r") == 0 or d[0].compare("l16i") == 0 or + d[0].compare("l16ui") == 0 or d[0].compare("l8ui") == 0 or + d[0].compare("addi") == 0) { + vector p = split(d[1], ","); + vector p2 = split(d2[1], ","); + if (p[0].compare(p2[1]) == 0) { + // prtinf("replace %s\n %s by %s\n\r",before.c_str(),tmp.c_str(),) + text->replaceText(indexbefore, " "); + string newstr = d[0] + " " + p2[0]; + for (int k = 1; k < p.size(); k++) { + newstr = newstr + "," + p[k]; + } + // printf("replace %s\n %s by + // %s\n\r",before.c_str(),tmp.c_str(),newstr.c_str()); + text->replaceText(i, newstr); + tmp = newstr; + } + } + } + + before = tmp; + indexbefore = i; + } + } + } + + vector from; + vector to; + vector index; + + for (int i = 0; i < text->size(); i++) { + if (*text->getChildAtPos(i) != NULL) { + + string tmp = string((*text->getChildAtPos(i))); + if (tmp.compare(" ") != 0 && tmp != "") { + // printf("on est %d %s\n",i,tmp.c_str()); + d = split(tmp, " "); + if (d[0].compare("movr") == 0) { + int ind = -1; + found = false; + d1 = split(d[1], ","); + for (int mp = 0; mp < index.size(); mp++) { + if (d1[1].compare(to[mp]) == 0) { + found = true; + ind = mp; + } + } + if (found) { + vector::iterator nth = from.begin() + ind; + from.erase(nth); + nth = to.begin() + ind; + to.erase(nth); + vector::iterator nth2 = index.begin() + ind; + index.erase(nth2); + } + + found = false; + for (int mp = 0; mp < index.size(); mp++) { + if (d1[0].compare(from[mp]) == 0) { + found = true; + ind = mp; } - else if (d.size() > 1) + } + if (found) { + vector::iterator nth = from.begin() + ind; + from.erase(nth); + nth = to.begin() + ind; + to.erase(nth); + vector::iterator nth2 = index.begin() + ind; + index.erase(nth2); + } + + index.push_back(i); + from.push_back(d1[0]); + to.push_back(d1[1]); + } + + if (strncmp(d[0].c_str(), "b", 1) == 0) { + d2 = split(d[1], ","); + found = false; + int ind = -1; + for (int mp = 0; mp < index.size(); mp++) { + if (d2[0].compare(to[mp]) == 0) { + found = true; + ind = mp; + } + } + if (found) { + vector::iterator nth = from.begin() + ind; + from.erase(nth); + nth = to.begin() + ind; + to.erase(nth); + vector::iterator nth2 = index.begin() + ind; + index.erase(nth2); + } + } + + if (index.size() > 0) { + // printf("ddd :%s\n",*text->getChildAtPos(i)); + if (d[0].compare("bnez") == 0 or d[0].compare("beqz") == 0) { + d2 = split(d[1], ","); + string newstr = d[0] + " "; + int ind = -1; + bool found = false; + for (int mp = 0; mp < index.size(); mp++) { + if (d2[0].compare(from[mp]) == 0) { + newstr = newstr + to[mp] + ","; + text->replaceText(index[mp], " "); + + found = true; + ind = mp; + ; + } + } + if (!found) + newstr = newstr + d2[0] + ","; + newstr = newstr + d2[1]; + text->replaceText(i, newstr); + /* + if (found) { - - d2 = split(d[1], ","); - if (d2[0].compare(torep) == 0) - { - before = " "; - torep = " "; - } + vector::iterator nth = from.begin() + ind; + from.erase(nth); + nth = to.begin() + ind; + to.erase(nth); + vector::iterator nth2 = index.begin() + ind; + index.erase(nth2); } - } - } - d.clear(); - d1.clear(); - d2.clear(); + */ + /* + index.clear(); + index.shrink_to_fit(); + from.clear(); + from.shrink_to_fit(); + to.clear(); + to.shrink_to_fit(); + */ + // printf("new %s\n",newstr.c_str()); + } else if (d[0].compare("bge") == 0 or d[0].compare("blt") == 0 or + d[0].compare("beq") == 0 or d[0].compare("bne") == 0 or + d[0].compare("blti") == 0 or d[0].compare("bgei") == 0 or + d[0].compare("beqi") == 0 or d[0].compare("bnei") == 0) { + + d2 = split(d[1], ","); + string newstr = d[0] + " "; + bool found = false; + int ind = -1; + for (int mp = 0; mp < index.size(); mp++) { + // printf("compare %s:%s\n",from[mp].c_str(),d2[0].c_str()); + if (d2[0].compare(from[mp]) == 0) { + newstr = newstr + to[mp] + ","; + text->replaceText(index[mp], " "); + + found = true; + ind = mp; + ; + } + } + if (!found) + newstr = newstr + d2[0] + ","; + + if (found) { + vector::iterator nth = from.begin() + ind; + from.erase(nth); + nth = to.begin() + ind; + to.erase(nth); + vector::iterator nth2 = index.begin() + ind; + index.erase(nth2); + } + found = false; + for (int mp = 0; mp < index.size(); mp++) { + if (d2[1].compare(from[mp]) == 0) { + newstr = newstr + to[mp] + ","; + text->replaceText(index[mp], " "); + ind = mp; + found = true; + ; + } + } + if (!found) + newstr = newstr + d2[1] + ","; + + newstr = newstr + d2[2]; + text->replaceText(i, newstr); + + if (found) { + vector::iterator nth = from.begin() + ind; + from.erase(nth); + nth = to.begin() + ind; + to.erase(nth); + vector::iterator nth2 = index.begin() + ind; + index.erase(nth2); + } + /* + index.clear(); + index.shrink_to_fit(); + from.clear(); + from.shrink_to_fit(); + to.clear(); + to.shrink_to_fit(); + */ + // printf("new %s\n", newstr.c_str()); + } else if (d[0].compare("movi") == 0 or d[0].compare("rsr") == 0 or + d[0].compare("wsr") == 0 or d[0].compare("ssl") == 0) { + d2 = split(d[1], ","); + int ind; + found = false; + + for (int mp = 0; mp < index.size(); mp++) { + if (d2[0].compare(from[mp]) == 0) { + // newstr = newstr + to[mp]; + // text->replaceText(index[mp], " "); + + found = true; + ind = mp; + } + } + if (found) { + vector::iterator nth = from.begin() + ind; + from.erase(nth); + nth = to.begin() + ind; + to.erase(nth); + vector::iterator nth2 = index.begin() + ind; + index.erase(nth2); + } + } else if (d[0].compare("neg") == 0 or d[0].compare("abs") == 0 or + d[0].compare("mov") == 0 or d[0].compare("sll") == 0 or + d[0].compare("srl") == 0) { + + d2 = split(d[1], ","); + int ind; + string newstr = d[0] + " " + d2[0] + ","; + bool found = false; + for (int mp = 0; mp < index.size(); mp++) { + if (d2[1].compare(from[mp]) == 0) { + newstr = newstr + to[mp]; + text->replaceText(index[mp], " "); + + found = true; + ind = mp; + } + } + if (!found) + newstr = newstr + d2[1]; - before = " "; - indexbefore = 0; + text->replaceText(i, newstr); + found = false; - for (int i = 0; i < text->size(); i++) - { - if (*text->getChildAtPos(i) != NULL) - { - string tmp = string((*text->getChildAtPos(i))); - if (tmp.compare(" ") != 0 && tmp != "") - { - vector d2 = split(tmp, " "); - // printf("on regarde %s %s\n\r",before.c_str(),tmp.c_str()); - if (d2[0].compare("mov") == 0) - { - // printf("on regarde mov |%s|%s|\n\r",before.c_str(),tmp.c_str()); - vector d = split(before, " "); - - if (d[0].compare("mull") == 0 or d[0].compare("quou") == 0 or d[0].compare("quos") == 0 or d[0].compare("sub") == 0 or d[0].compare("add") == 0 or d[0].compare("mov") == 0 or d[0].compare("movi") == 0 or d[0].compare("l32i") == 0 or d[0].compare("l32r") == 0 or d[0].compare("l16i") == 0 or d[0].compare("l16ui") == 0 or d[0].compare("l8ui") == 0 or d[0].compare("addi") == 0) - { - vector p = split(d[1], ","); - vector p2 = split(d2[1], ","); - if (p[0].compare(p2[1]) == 0) - { - // prtinf("replace %s\n %s by %s\n\r",before.c_str(),tmp.c_str(),) - text->replaceText(indexbefore, " "); - string newstr = d[0] + " " + p2[0]; - for (int k = 1; k < p.size(); k++) - { - newstr = newstr + "," + p[k]; - } - // printf("replace %s\n %s by %s\n\r",before.c_str(),tmp.c_str(),newstr.c_str()); - text->replaceText(i, newstr); - tmp = newstr; - } - } - } + for (int mp = 0; mp < index.size(); mp++) { + if (d2[0].compare(from[mp]) == 0) { + // newstr = newstr + to[mp]; + // text->replaceText(index[mp], " "); - before = tmp; - indexbefore = i; + found = true; + ind = mp; + } } - } - } - - vector from; - vector to; - vector index; - - for (int i = 0; i < text->size(); i++) - { - if (*text->getChildAtPos(i) != NULL) - { + if (found) { + vector::iterator nth = from.begin() + ind; + from.erase(nth); + nth = to.begin() + ind; + to.erase(nth); + vector::iterator nth2 = index.begin() + ind; + index.erase(nth2); + } + /* + index.clear(); + index.shrink_to_fit(); + from.clear(); + from.shrink_to_fit(); + to.clear(); + to.shrink_to_fit(); + */ + // printf("new %s\n", newstr.c_str()); + } + + else if (d[0].compare("round.s") == 0 or + d[0].compare("floor.s") == 0 or + d[0].compare("float.s") == 0 or d[0].compare("l32i") == 0 or + d[0].compare("s32i") == 0 or d[0].compare("l16i") == 0 or + d[0].compare("l16si") == 0 or d[0].compare("l16ui") == 0 or + d[0].compare("l8ui") == 0 or d[0].compare("s8i") == 0 or + d[0].compare("s16i") == 0 or d[0].compare("addi") == 0 or + d[0].compare("trunc.s") == 0) { + d2 = split(d[1], ","); + int ind; + string newstr = d[0] + " " + d2[0] + ","; + bool found = false; + for (int mp = 0; mp < index.size(); mp++) { + if (d2[1].compare(from[mp]) == 0) { + newstr = newstr + to[mp] + ","; + text->replaceText(index[mp], " "); + ind = mp; + found = true; + ; + } + } + if (!found) + newstr = newstr + d2[1] + ","; + newstr = newstr + d2[2]; + text->replaceText(i, newstr); + + found = false; + for (int mp = 0; mp < index.size(); mp++) { + if (d2[0].compare(from[mp]) == 0) { + // newstr = newstr + to[mp] + ","; + // text->replaceText(index[mp], " "); + ind = mp; + found = true; + ; + } + } + if (found) { + vector::iterator nth = from.begin() + ind; + from.erase(nth); + nth = to.begin() + ind; + to.erase(nth); + vector::iterator nth2 = index.begin() + ind; + index.erase(nth2); + } + /* + index.clear(); + index.shrink_to_fit(); + from.clear(); + from.shrink_to_fit(); + to.clear(); + to.shrink_to_fit(); + */ + // printf("new %s\n", newstr.c_str()); + } else if (d[0].compare("remu") == 0 or d[0].compare("or") == 0 or + d[0].compare("and") == 0 or d[0].compare("mull") == 0 or + d[0].compare("sub") == 0 or d[0].compare("add") == 0 or + d[0].compare("quou") == 0 or d[0].compare("quos") == 0) { + int ind = -1; + + d2 = split(d[1], ","); + string newstr = d[0] + " " + d2[0] + ","; + bool found = false; + for (int mp = 0; mp < index.size(); mp++) { + if (d2[1].compare(from[mp]) == 0) { + newstr = newstr + to[mp] + ","; + text->replaceText(index[mp], " "); + + found = true; + ind = mp; + ; + } + } + if (!found) + newstr = newstr + d2[1] + ","; - string tmp = string((*text->getChildAtPos(i))); - if (tmp.compare(" ") != 0 && tmp != "") - { - // printf("on est %d %s\n",i,tmp.c_str()); - d = split(tmp, " "); - if (d[0].compare("movr") == 0) - { - int ind = -1; - found = false; - d1 = split(d[1], ","); - for (int mp = 0; mp < index.size(); mp++) - { - if (d1[1].compare(to[mp]) == 0) - { - found = true; - ind = mp; - } - } - if (found) - { - vector::iterator nth = from.begin() + ind; - from.erase(nth); - nth = to.begin() + ind; - to.erase(nth); - vector::iterator nth2 = index.begin() + ind; - index.erase(nth2); - } - - found = false; - for (int mp = 0; mp < index.size(); mp++) - { - if (d1[0].compare(from[mp]) == 0) - { - found = true; - ind = mp; - } - } - if (found) - { - vector::iterator nth = from.begin() + ind; - from.erase(nth); - nth = to.begin() + ind; - to.erase(nth); - vector::iterator nth2 = index.begin() + ind; - index.erase(nth2); - } - - index.push_back(i); - from.push_back(d1[0]); - to.push_back(d1[1]); - } + found = false; + for (int mp = 0; mp < index.size(); mp++) { - if (strncmp(d[0].c_str(), "b", 1) == 0) - { - d2 = split(d[1], ","); - found = false; - int ind = -1; - for (int mp = 0; mp < index.size(); mp++) - { - if (d2[0].compare(to[mp]) == 0) - { - found = true; - ind = mp; - } - } - if (found) - { - vector::iterator nth = from.begin() + ind; - from.erase(nth); - nth = to.begin() + ind; - to.erase(nth); - vector::iterator nth2 = index.begin() + ind; - index.erase(nth2); - } - } + if (d2[2].compare(from[mp]) == 0) { + newstr = newstr + to[mp]; + found = true; + ind = mp; - if (index.size() > 0) - { - // printf("ddd :%s\n",*text->getChildAtPos(i)); - if (d[0].compare("bnez") == 0 or d[0].compare("beqz") == 0) - { - d2 = split(d[1], ","); - string newstr = d[0] + " "; - int ind = -1; - bool found = false; - for (int mp = 0; mp < index.size(); mp++) - { - if (d2[0].compare(from[mp]) == 0) - { - newstr = newstr + to[mp] + ","; - text->replaceText(index[mp], " "); - - found = true; - ind = mp; - ; - } - } - if (!found) - newstr = newstr + d2[0] + ","; - newstr = newstr + d2[1]; - text->replaceText(i, newstr); - /* - if (found) - { - vector::iterator nth = from.begin() + ind; - from.erase(nth); - nth = to.begin() + ind; - to.erase(nth); - vector::iterator nth2 = index.begin() + ind; - index.erase(nth2); - } - */ - /* - index.clear(); - index.shrink_to_fit(); - from.clear(); - from.shrink_to_fit(); - to.clear(); - to.shrink_to_fit(); - */ - // printf("new %s\n",newstr.c_str()); - } - else if (d[0].compare("bge") == 0 or d[0].compare("blt") == 0 or d[0].compare("beq") == 0 or d[0].compare("bne") == 0 or d[0].compare("blti") == 0 or d[0].compare("bgei") == 0 or d[0].compare("beqi") == 0 or d[0].compare("bnei") == 0) - { - - d2 = split(d[1], ","); - string newstr = d[0] + " "; - bool found = false; - int ind = -1; - for (int mp = 0; mp < index.size(); mp++) - { - // printf("compare %s:%s\n",from[mp].c_str(),d2[0].c_str()); - if (d2[0].compare(from[mp]) == 0) - { - newstr = newstr + to[mp] + ","; - text->replaceText(index[mp], " "); - - found = true; - ind = mp; - ; - } - } - if (!found) - newstr = newstr + d2[0] + ","; - - if (found) - { - vector::iterator nth = from.begin() + ind; - from.erase(nth); - nth = to.begin() + ind; - to.erase(nth); - vector::iterator nth2 = index.begin() + ind; - index.erase(nth2); - } - found = false; - for (int mp = 0; mp < index.size(); mp++) - { - if (d2[1].compare(from[mp]) == 0) - { - newstr = newstr + to[mp] + ","; - text->replaceText(index[mp], " "); - ind = mp; - found = true; - ; - } - } - if (!found) - newstr = newstr + d2[1] + ","; - - newstr = newstr + d2[2]; - text->replaceText(i, newstr); - - if (found) - { - vector::iterator nth = from.begin() + ind; - from.erase(nth); - nth = to.begin() + ind; - to.erase(nth); - vector::iterator nth2 = index.begin() + ind; - index.erase(nth2); - } - /* - index.clear(); - index.shrink_to_fit(); - from.clear(); - from.shrink_to_fit(); - to.clear(); - to.shrink_to_fit(); - */ - // printf("new %s\n", newstr.c_str()); - } - else if (d[0].compare("movi") == 0 or d[0].compare("rsr") == 0 or d[0].compare("wsr") == 0 or d[0].compare("ssl") == 0) - { - d2 = split(d[1], ","); - int ind; - found = false; - - for (int mp = 0; mp < index.size(); mp++) - { - if (d2[0].compare(from[mp]) == 0) - { - // newstr = newstr + to[mp]; - // text->replaceText(index[mp], " "); - - found = true; - ind = mp; - } - } - if (found) - { - vector::iterator nth = from.begin() + ind; - from.erase(nth); - nth = to.begin() + ind; - to.erase(nth); - vector::iterator nth2 = index.begin() + ind; - index.erase(nth2); - } - } - else if (d[0].compare("neg") == 0 or d[0].compare("abs") == 0 or d[0].compare("mov") == 0 or d[0].compare("sll") == 0 or d[0].compare("srl") == 0) - { - - d2 = split(d[1], ","); - int ind; - string newstr = d[0] + " " + d2[0] + ","; - bool found = false; - for (int mp = 0; mp < index.size(); mp++) - { - if (d2[1].compare(from[mp]) == 0) - { - newstr = newstr + to[mp]; - text->replaceText(index[mp], " "); - - found = true; - ind = mp; - } - } - if (!found) - newstr = newstr + d2[1]; - - text->replaceText(i, newstr); - found = false; - - for (int mp = 0; mp < index.size(); mp++) - { - if (d2[0].compare(from[mp]) == 0) - { - // newstr = newstr + to[mp]; - // text->replaceText(index[mp], " "); - - found = true; - ind = mp; - } - } - if (found) - { - vector::iterator nth = from.begin() + ind; - from.erase(nth); - nth = to.begin() + ind; - to.erase(nth); - vector::iterator nth2 = index.begin() + ind; - index.erase(nth2); - } - /* - index.clear(); - index.shrink_to_fit(); - from.clear(); - from.shrink_to_fit(); - to.clear(); - to.shrink_to_fit(); - */ - // printf("new %s\n", newstr.c_str()); - } - - else if (d[0].compare("round.s") == 0 or d[0].compare("floor.s") == 0 or d[0].compare("float.s") == 0 or d[0].compare("l32i") == 0 or d[0].compare("s32i") == 0 or d[0].compare("l16i") == 0 or d[0].compare("l16si") == 0 or d[0].compare("l16ui") == 0 or d[0].compare("l8ui") == 0 or d[0].compare("s8i") == 0 or d[0].compare("s16i") == 0 or d[0].compare("addi") == 0 or d[0].compare("trunc.s") == 0) - { - d2 = split(d[1], ","); - int ind; - string newstr = d[0] + " " + d2[0] + ","; - bool found = false; - for (int mp = 0; mp < index.size(); mp++) - { - if (d2[1].compare(from[mp]) == 0) - { - newstr = newstr + to[mp] + ","; - text->replaceText(index[mp], " "); - ind = mp; - found = true; - ; - } - } - if (!found) - newstr = newstr + d2[1] + ","; - newstr = newstr + d2[2]; - text->replaceText(i, newstr); - - found = false; - for (int mp = 0; mp < index.size(); mp++) - { - if (d2[0].compare(from[mp]) == 0) - { - // newstr = newstr + to[mp] + ","; - // text->replaceText(index[mp], " "); - ind = mp; - found = true; - ; - } - } - if (found) - { - vector::iterator nth = from.begin() + ind; - from.erase(nth); - nth = to.begin() + ind; - to.erase(nth); - vector::iterator nth2 = index.begin() + ind; - index.erase(nth2); - } - /* - index.clear(); - index.shrink_to_fit(); - from.clear(); - from.shrink_to_fit(); - to.clear(); - to.shrink_to_fit(); - */ - // printf("new %s\n", newstr.c_str()); - } - else if (d[0].compare("remu") == 0 or d[0].compare("or") == 0 or d[0].compare("and") == 0 or d[0].compare("mull") == 0 or d[0].compare("sub") == 0 or d[0].compare("add") == 0 or d[0].compare("quou") == 0 or d[0].compare("quos") == 0) - { - int ind = -1; - - d2 = split(d[1], ","); - string newstr = d[0] + " " + d2[0] + ","; - bool found = false; - for (int mp = 0; mp < index.size(); mp++) - { - if (d2[1].compare(from[mp]) == 0) - { - newstr = newstr + to[mp] + ","; - text->replaceText(index[mp], " "); - - found = true; - ind = mp; - ; - } - } - if (!found) - newstr = newstr + d2[1] + ","; - - found = false; - for (int mp = 0; mp < index.size(); mp++) - { - - if (d2[2].compare(from[mp]) == 0) - { - newstr = newstr + to[mp]; - found = true; - ind = mp; - - text->replaceText(index[mp], " "); - } - } - if (!found) - newstr = newstr + d2[2]; - - found = false; - for (int mp = 0; mp < index.size(); mp++) - { - if (d2[0].compare(from[mp]) == 0) - { - // newstr = newstr + to[mp] + ","; - // text->replaceText(index[mp], " "); - - found = true; - ind = mp; - ; - } - } - - if (found) - { - vector::iterator nth = from.begin() + ind; - from.erase(nth); - nth = to.begin() + ind; - to.erase(nth); - vector::iterator nth2 = index.begin() + ind; - index.erase(nth2); - } - text->replaceText(i, newstr); - - // printf("new %s\n", newstr.c_str()); - /* - index.clear(); - index.shrink_to_fit(); - from.clear(); - from.shrink_to_fit(); - to.clear(); - to.shrink_to_fit(); - */ - } - else if (d[0].find_first_of(":") != std::string::npos or d[0].compare("call8") == 0 or d[0].compare("callExt") == 0) - { - - index.clear(); - index.shrink_to_fit(); - from.clear(); - from.shrink_to_fit(); - to.clear(); - to.shrink_to_fit(); - } - } + text->replaceText(index[mp], " "); + } } - } - } - for (int i = 0; i < text->size(); i++) - { - string tmp = string((*text->getChildAtPos(i))); - if (*text->getChildAtPos(i) != NULL) - { - if (tmp.compare(" ") != 0 && tmp != "") - { - d = split(tmp, " "); - if (d[0].compare("mov") == 0) - { - d2 = split(d[1], ","); - if (d2[0].compare(d2[1]) == 0) - { - text->replaceText(i, " "); - } - } + if (!found) + newstr = newstr + d2[2]; + + found = false; + for (int mp = 0; mp < index.size(); mp++) { + if (d2[0].compare(from[mp]) == 0) { + // newstr = newstr + to[mp] + ","; + // text->replaceText(index[mp], " "); + + found = true; + ind = mp; + ; + } } - } - } - - for (int i = 0; i < text->size(); i++) - { - string tmp = string((*text->getChildAtPos(i))); - if (*text->getChildAtPos(i) != NULL) - { - if (tmp.compare(" ") != 0 && tmp != "") - { - d = split(tmp, " "); - if (d[0].compare("l32i") == 0) - { - tmp = string((*text->getChildAtPos(i - 1))); - if (*text->getChildAtPos(i) != NULL) - { - if (tmp.compare(" ") != 0 && tmp != "") - { - d2 = split(tmp, " "); - if (d2[0].compare("s32i") == 0) - { - if (d2[1].compare(d[1]) == 0) - text->replaceText(i, " "); - } - } - } - } - if (d[0].compare("l8ui") == 0) - { - tmp = string((*text->getChildAtPos(i - 1))); - if (*text->getChildAtPos(i) != NULL) - { - if (tmp.compare(" ") != 0 && tmp != "") - { - d2 = split(tmp, " "); - if (d2[0].compare("s8i") == 0) - { - if (d2[1].compare(d[1]) == 0) - text->replaceText(i, " "); - } - } - } - } - if (d[0].compare("lsi") == 0) - { - tmp = string((*text->getChildAtPos(i - 1))); - if (*text->getChildAtPos(i) != NULL) - { - if (tmp.compare(" ") != 0 && tmp != "") - { - d2 = split(tmp, " "); - if (d2[0].compare("ssi") == 0) - { - if (d2[1].compare(d[1]) == 0) - text->replaceText(i, " "); - } - } - } - } + if (found) { + vector::iterator nth = from.begin() + ind; + from.erase(nth); + nth = to.begin() + ind; + to.erase(nth); + vector::iterator nth2 = index.begin() + ind; + index.erase(nth2); } - } - } + text->replaceText(i, newstr); - index.shrink_to_fit(); - from.clear(); - from.shrink_to_fit(); - to.clear(); - to.shrink_to_fit(); - d.clear(); - d.shrink_to_fit(); - d1.clear(); - d1.shrink_to_fit(); - d2.clear(); - d2.shrink_to_fit(); + // printf("new %s\n", newstr.c_str()); + /* + index.clear(); + index.shrink_to_fit(); + from.clear(); + from.shrink_to_fit(); + to.clear(); + to.shrink_to_fit(); + */ + } else if (d[0].find_first_of(":") != std::string::npos or + d[0].compare("call8") == 0 or + d[0].compare("callExt") == 0) { + + index.clear(); + index.shrink_to_fit(); + from.clear(); + from.shrink_to_fit(); + to.clear(); + to.shrink_to_fit(); + } + } + } + } + } + for (int i = 0; i < text->size(); i++) { + string tmp = string((*text->getChildAtPos(i))); + if (*text->getChildAtPos(i) != NULL) { + if (tmp.compare(" ") != 0 && tmp != "") { + d = split(tmp, " "); + if (d[0].compare("mov") == 0) { + d2 = split(d[1], ","); + if (d2[0].compare(d2[1]) == 0) { + text->replaceText(i, " "); + } + } + } + } + } + + for (int i = 0; i < text->size(); i++) { + string tmp = string((*text->getChildAtPos(i))); + if (*text->getChildAtPos(i) != NULL) { + if (tmp.compare(" ") != 0 && tmp != "") { + d = split(tmp, " "); + if (d[0].compare("l32i") == 0) { + tmp = string((*text->getChildAtPos(i - 1))); + if (*text->getChildAtPos(i) != NULL) { + if (tmp.compare(" ") != 0 && tmp != "") { + d2 = split(tmp, " "); + if (d2[0].compare("s32i") == 0) { + if (d2[1].compare(d[1]) == 0) + text->replaceText(i, " "); + } + } + } + } + if (d[0].compare("l8ui") == 0) { + tmp = string((*text->getChildAtPos(i - 1))); + if (*text->getChildAtPos(i) != NULL) { + if (tmp.compare(" ") != 0 && tmp != "") { + d2 = split(tmp, " "); + if (d2[0].compare("s8i") == 0) { + if (d2[1].compare(d[1]) == 0) + text->replaceText(i, " "); + } + } + } + } + + if (d[0].compare("lsi") == 0) { + tmp = string((*text->getChildAtPos(i - 1))); + if (*text->getChildAtPos(i) != NULL) { + if (tmp.compare(" ") != 0 && tmp != "") { + d2 = split(tmp, " "); + if (d2[0].compare("ssi") == 0) { + if (d2[1].compare(d[1]) == 0) + text->replaceText(i, " "); + } + } + } + } + } + } + } + + index.shrink_to_fit(); + from.clear(); + from.shrink_to_fit(); + to.clear(); + to.shrink_to_fit(); + d.clear(); + d.shrink_to_fit(); + d1.clear(); + d1.shrink_to_fit(); + d2.clear(); + d2.shrink_to_fit(); } \ No newline at end of file diff --git a/src/asm_external.h b/src/asm_external.h index 4053bb0..a5db40b 100644 --- a/src/asm_external.h +++ b/src/asm_external.h @@ -10,122 +10,101 @@ vector external_links; uint32_t external_links_array[60]; -uint32_t * createExternalLinks() -{ - for(int i=0;i j=split(in,","); - asmex.signature=name+"("; - asmex.name="external "+out+" "+name +"("; - for (int i=0;i j = split(in, ","); + asmex.signature = name + "("; + asmex.name = "external " + out + " " + name + "("; + for (int i = 0; i < j.size(); i++) { + // printf("%s %d\n\r",j[i].c_str(),j[i].find("Args")); + if (j[i].find("Args") == string::npos) + asmex.signature = asmex.signature + "d"; else - asmex.signature= asmex.signature+"Args"; - if(j[i].find("*")!=string::npos) - asmex.signature= asmex.signature+"*"; - asmex.name=string_format("%s%s a%d",asmex.name.c_str(),j[i].c_str(),i); - if (i::iterator it=external_links.begin(); - for(vector::iterator it=external_links.begin();itname.compare(name)==0) - { - it->ptr=ptr; - return; +void replaceExternal(string name, void *ptr) { + // vector::iterator it=external_links.begin(); + for (vector::iterator it = external_links.begin(); + it < external_links.end(); it++) { + if (it->name.compare(name) == 0) { + it->ptr = ptr; + return; } - } + } } -int findLink(string label, externalType op) -{ - if(label.find_first_of("(")!=string::npos) +int findLink(string label, externalType op) { + if (label.find_first_of("(") != string::npos) { + label = label.substr(0, label.find_first_of("(")); + } + for (int i = 0; i < external_links.size(); i++) { + if ((external_links[i].shortname.compare(label) == + 0)) //&& (external_links[i].type==op)) { - label=label.substr(0,label.find_first_of("(")); + return i; } - for(int i=0;i using namespace std; -#include -#include #include +#include +#include #include // #include @@ -16,9 +16,9 @@ using namespace std; #include "FS.h" #include "esp_heap_caps.h" #endif -#include "string_function.h" -#include "asm_parser_LMbin.h" #include "asm_external.h" +#include "asm_parser_LMbin.h" +#include "string_function.h" #define ALIGN_INSTR 4 #define ALIGN_DATA 4 @@ -40,72 +40,65 @@ uint16_t binary_header_size = 0; // vector _asm_parsed; parsedLines _asm_parsed; -void addInstr(result_parse_line operande, parsedLines *asm_parsed) -{ - if (operande.op != opCodeType::data && operande.op != opCodeType::number) - { +void addInstr(result_parse_line operande, parsedLines *asm_parsed) { + if (operande.op != opCodeType::data && operande.op != opCodeType::number) { operande.address = _address_instr; - if (operande.align == true) - { + if (operande.align == true) { int add_size = 0; uint32_t unst_local; int op = (_address_instr % ALIGN_INSTR); - - switch(op) - { - case 2: - add_size=2; - unst_local=0xF03D; - memcpy(tmp_exec + operande.address,&unst_local,2); - break; - case 1: - add_size=3; - unst_local=0x0020F0; - memcpy(tmp_exec + operande.address,&unst_local,3); - break; - case 3: - add_size=5; - unst_local=0x0020F0; - memcpy(tmp_exec + operande.address,&unst_local,3); - unst_local=0xF03D; - memcpy(tmp_exec + operande.address+3,&unst_local,2); - break; - default: - add_size=0; - break; - } - - //if (op > 0) - // add_size = ALIGN_INSTR - op; + + switch (op) { + case 2: + add_size = 2; + unst_local = 0xF03D; + memcpy(tmp_exec + operande.address, &unst_local, 2); + break; + case 1: + add_size = 3; + unst_local = 0x0020F0; + memcpy(tmp_exec + operande.address, &unst_local, 3); + break; + case 3: + add_size = 5; + unst_local = 0x0020F0; + memcpy(tmp_exec + operande.address, &unst_local, 3); + unst_local = 0xF03D; + memcpy(tmp_exec + operande.address + 3, &unst_local, 2); + break; + default: + add_size = 0; + break; + } + + // if (op > 0) + // add_size = ALIGN_INSTR - op; operande.address += add_size; } memcpy(tmp_exec + operande.address, &operande.bincode, operande.size); // printf("parseASM instr %d\r\n",operande.address); _address_instr = operande.size + operande.address; - #ifndef __TEST_DEBUG +#ifndef __TEST_DEBUG if (operande.op != opCodeType::standard) - #endif +#endif asm_parsed->push_back(operande); - } - else - { + } else { int add_size = 0; int op = (_address_data % ALIGN_DATA); - if (op > 0) - { + if (op > 0) { add_size = ALIGN_DATA - op; _address_data += add_size; } asm_parsed->last()->bincode = _address_data; _address_instr += 4; - if (operande.nameref != EOF_TEXTARRAY) - { + if (operande.nameref != EOF_TEXTARRAY) { // printf("on ajoute un data de %d\n\r",operande.size); operande.address = _address_data; - memcpy(tmp_binary_data + _tmp_data_address, operande.getText(), operande.size); + memcpy(tmp_binary_data + _tmp_data_address, operande.getText(), + operande.size); _tmp_data_address += operande.size; asm_parsed->push_back(operande); } @@ -114,17 +107,14 @@ void addInstr(result_parse_line operande, parsedLines *asm_parsed) } } -result_parse_line *getInstrAtPos(int pos) -{ +result_parse_line *getInstrAtPos(int pos) { int i = 0; - if (pos >= _asm_parsed.size() || pos < 0) - { + if (pos >= _asm_parsed.size() || pos < 0) { return (*_asm_parsed.end()); } - for (vector::iterator it = _asm_parsed.begin(); it != _asm_parsed.end(); it++) - { - if (i == pos) - { + for (vector::iterator it = _asm_parsed.begin(); + it != _asm_parsed.end(); it++) { + if (i == pos) { return *it; } @@ -132,17 +122,14 @@ result_parse_line *getInstrAtPos(int pos) } return (*_asm_parsed.end()); } -vector::iterator getIterAtPos(int pos) -{ +vector::iterator getIterAtPos(int pos) { int i = 0; - if (pos >= _asm_parsed.size() || pos < 0) - { + if (pos >= _asm_parsed.size() || pos < 0) { return _asm_parsed.end(); } - for (vector::iterator it = _asm_parsed.begin(); it != _asm_parsed.end(); it++) - { - if (i == pos) - { + for (vector::iterator it = _asm_parsed.begin(); + it != _asm_parsed.end(); it++) { + if (i == pos) { return it; } @@ -150,27 +137,21 @@ vector::iterator getIterAtPos(int pos) } return _asm_parsed.end(); } -class opRegister -{ +class opRegister { public: - static result_parse_operande parse(std::string s) - { + static result_parse_operande parse(std::string s) { error_message_struct error; result_parse_operande res; char *endptr = NULL; error.error = 0; s = trim(s); - if (s.at(0) == 'a') - { + if (s.at(0) == 'a') { string s2 = s.substr(1, s.size()); - if (s2.size() > 0) - { + if (s2.size() > 0) { int value = strtol(s2.c_str(), &endptr, 10); - if (*endptr == 0) - { - if (value >= 0 and value <= 15) - { + if (*endptr == 0) { + if (value >= 0 and value <= 15) { res.value = value; res.error = error; return res; @@ -184,27 +165,21 @@ class opRegister return res; } }; -class opFloatRegister -{ +class opFloatRegister { public: - static result_parse_operande parse(std::string s) - { + static result_parse_operande parse(std::string s) { error_message_struct error; result_parse_operande res; char *endptr = NULL; error.error = 0; s = trim(s); - if (s.at(0) == 'f') - { + if (s.at(0) == 'f') { string s2 = s.substr(1, s.size()); - if (s2.size() > 0) - { + if (s2.size() > 0) { int value = strtol(s2.c_str(), &endptr, 10); - if (*endptr == 0) - { - if (value >= 0 and value <= 15) - { + if (*endptr == 0) { + if (value >= 0 and value <= 15) { res.value = value; res.error = error; return res; @@ -218,27 +193,21 @@ class opFloatRegister return res; } }; -class opBooleanRegister -{ +class opBooleanRegister { public: - static result_parse_operande parse(std::string s) - { + static result_parse_operande parse(std::string s) { error_message_struct error; result_parse_operande res; char *endptr = NULL; error.error = 0; s = trim(s); - if (s.at(0) == 'b') - { + if (s.at(0) == 'b') { string s2 = s.substr(1, s.size()); - if (s2.size() > 0) - { + if (s2.size() > 0) { int value = strtol(s2.c_str(), &endptr, 10); - if (*endptr == 0) - { - if (value >= 0 and value <= 15) - { + if (*endptr == 0) { + if (value >= 0 and value <= 15) { res.value = value; res.error = error; return res; @@ -253,24 +222,20 @@ class opBooleanRegister } }; -class opHex -{ +class opHex { public: - static result_parse_operande parse(std::string s, uint32_t min, uint32_t max) - { + static result_parse_operande parse(std::string s, uint32_t min, + uint32_t max) { error_message_struct error; result_parse_operande res; char *endptr = NULL; error.error = 0; s = trim(s); - if (s.size() > 0) - { + if (s.size() > 0) { int value = strtol(s.c_str(), &endptr, 10); - if (*endptr == 0) - { - if (value >= min and value <= max) - { + if (*endptr == 0) { + if (value >= min and value <= max) { res.value = value; res.error = error; return res; @@ -278,31 +243,28 @@ class opHex } } - error.error_message = string_format("incorrect value %s should be between %d and %d\n", s.c_str(), min, max); + error.error_message = + string_format("incorrect value %s should be between %d and %d\n", + s.c_str(), min, max); error.error = 1; res.error = error; return res; } }; -class opNumber -{ +class opNumber { public: - static result_parse_operande parse(std::string s, int min, int max) - { + static result_parse_operande parse(std::string s, int min, int max) { error_message_struct error; result_parse_operande res; char *endptr = NULL; error.error = 0; s = trim(s); - if (s.size() > 0) - { + if (s.size() > 0) { int value = strtol(s.c_str(), &endptr, 10); - if (*endptr == 0) - { - if (value >= min and value <= max) - { + if (*endptr == 0) { + if (value >= min and value <= max) { res.value = value; res.error = error; return res; @@ -310,26 +272,25 @@ class opNumber } } - error.error_message = string_format("incorrect value %s should be between %d and %d\n", s.c_str(), min, max); + error.error_message = + string_format("incorrect value %s should be between %d and %d\n", + s.c_str(), min, max); error.error = 1; res.error = error; return res; } }; -class oplabel -{ +class oplabel { public: - static result_parse_operande parse(std::string s) - { + static result_parse_operande parse(std::string s) { error_message_struct error; result_parse_operande res; // char *endptr = NULL; error.error = 0; s = trim(s); - if (s.size() > 0) - { + if (s.size() > 0) { res.label = s; res.error = error; return res; @@ -342,113 +303,90 @@ class oplabel } }; -result_parse_operande operandeParse(string s, operandeType optype) -{ +result_parse_operande operandeParse(string s, operandeType optype) { - if (optype == operandeType::registers) - { + if (optype == operandeType::registers) { return opRegister::parse(s); } - if (optype == operandeType::floatregisters) - { + if (optype == operandeType::floatregisters) { return opFloatRegister::parse(s); } - if (optype == operandeType::boolregisters) - { + if (optype == operandeType::boolregisters) { return opBooleanRegister::parse(s); } - if (optype == operandeType::l0_255) - { + if (optype == operandeType::l0_255) { return opNumber::parse(s, 0, 255); } - if (optype == operandeType::lm32_95) - { + if (optype == operandeType::lm32_95) { return opNumber::parse(s, -32, 95); } - if (optype == operandeType::lm2048_2047) - { + if (optype == operandeType::lm2048_2047) { return opNumber::parse(s, -2048, 2047); } - if (optype == operandeType::l0_15) - { + if (optype == operandeType::l0_15) { return opNumber::parse(s, 0, 15); } - if (optype == operandeType::l0_32760) - { + if (optype == operandeType::l0_32760) { return opNumber::parse(s, 0, 32760); } - if (optype == operandeType::l0_240) - { + if (optype == operandeType::l0_240) { return opNumber::parse(s, 0, 240); } - if (optype == operandeType::l0_31) - { + if (optype == operandeType::l0_31) { return opNumber::parse(s, 0, 31); } - if (optype == operandeType::lm128_127) - { + if (optype == operandeType::lm128_127) { return opNumber::parse(s, -128, 127); } - if (optype == operandeType::lm32768_32512) - { + if (optype == operandeType::lm32768_32512) { return opNumber::parse(s, -32768, 35512); } - if (optype == operandeType::l1_31) - { + if (optype == operandeType::l1_31) { return opNumber::parse(s, 1, 31); } - if (optype == operandeType::l7_22) - { + if (optype == operandeType::l7_22) { return opNumber::parse(s, 7, 22); } - if (optype == operandeType::l0_60) - { + if (optype == operandeType::l0_60) { return opNumber::parse(s, 0, 60); } - if (optype == operandeType::lm64_m4) - { + if (optype == operandeType::lm64_m4) { return opNumber::parse(s, -64, -4); } - if (optype == operandeType::l0_510) - { + if (optype == operandeType::l0_510) { return opNumber::parse(s, 0, 510); } - if (optype == operandeType::lm8_7) - { + if (optype == operandeType::lm8_7) { return opNumber::parse(s, -8, 7); } - if (optype == operandeType::l0_1020) - { + if (optype == operandeType::l0_1020) { return opNumber::parse(s, 0, 1020); } - if (optype == operandeType::lm1_15) - { + if (optype == operandeType::lm1_15) { return opNumber::parse(s, -1, 15); } - if (optype == operandeType::l0_FFFFFFFF) - { + if (optype == operandeType::l0_FFFFFFFF) { return opHex::parse(s, 0, 0xFFFFFFFF); } - if (optype == operandeType::label) - { + if (optype == operandeType::label) { return oplabel::parse(s); } result_parse_operande res; @@ -457,8 +395,9 @@ result_parse_operande operandeParse(string s, operandeType optype) return res; } -result_parse_line parseOperandes(string str, int nboperande, operandeType *optypes, int size, uint32_t (*createbin)(uint32_t *val)) -{ +result_parse_line parseOperandes(string str, int nboperande, + operandeType *optypes, int size, + uint32_t (*createbin)(uint32_t *val)) { result_parse_line result; uint32_t values[4]; @@ -466,53 +405,46 @@ result_parse_line parseOperandes(string str, int nboperande, operandeType *optyp result.align = false; result.size = size; vector operandes = split(str, ","); - if (operandes.size() == nboperande) - { - for (int i = 0; i < nboperande; i++) - { + if (operandes.size() == nboperande) { + for (int i = 0; i < nboperande; i++) { result_parse_operande res = operandeParse(operandes[i], optypes[i]); - if (res.error.error) - { + if (res.error.error) { asm_Error.error = res.error.error; - asm_Error.error_message = asm_Error.error_message + res.error.error_message; - } - else - { - if (optypes[i] == operandeType::label) - { + asm_Error.error_message = + asm_Error.error_message + res.error.error_message; + } else { + if (optypes[i] == operandeType::label) { result.addText(res.label); } values[i] = res.value; } } - if (asm_Error.error == 0) - { - if (createbin) - { + if (asm_Error.error == 0) { + if (createbin) { result.bincode = createbin(values); // result.size=size; } } - } - else - { + } else { asm_Error.error = 1; - asm_Error.error_message = string_format("asm_Error:expected %d arguments got %d\n", nboperande, operandes.size()); + asm_Error.error_message = + string_format("asm_Error:expected %d arguments got %d\n", nboperande, + operandes.size()); } result.op = opCodeType::standard; return result; } -int findLabel(string s, parsedLines *asm_parsed) -{ +int findLabel(string s, parsedLines *asm_parsed) { int res = -1; int i = 0; - for (vector::iterator it = asm_parsed->begin(); it != asm_parsed->end(); it++) - { - if ((*it)->op == opCodeType::label || (*it)->op == opCodeType::data_label || (*it)->op == opCodeType::number_label || (*it)->op == opCodeType::external_var_label || (*it)->op == opCodeType::external_call) - { - if (trim(string((*it)->getText())).compare(trim(s)) == 0) - { + for (vector::iterator it = asm_parsed->begin(); + it != asm_parsed->end(); it++) { + if ((*it)->op == opCodeType::label || (*it)->op == opCodeType::data_label || + (*it)->op == opCodeType::number_label || + (*it)->op == opCodeType::external_var_label || + (*it)->op == opCodeType::external_call) { + if (trim(string((*it)->getText())).compare(trim(s)) == 0) { return i; } } @@ -521,17 +453,14 @@ int findLabel(string s, parsedLines *asm_parsed) return res; } -int findFunction(string s, vector *asm_parsed) -{ +int findFunction(string s, vector *asm_parsed) { int res = -1; int i = 0; - for (vector::iterator it = asm_parsed->begin(); it != asm_parsed->end(); it++) - { - if ((*it).op == opCodeType::function_declaration) - { + for (vector::iterator it = asm_parsed->begin(); + it != asm_parsed->end(); it++) { + if ((*it).op == opCodeType::function_declaration) { // if ((*it).name.compare(s) == 0) - if (strcmp((*it).getText(), s.c_str()) == 0) - { + if (strcmp((*it).getText(), s.c_str()) == 0) { return i; } } @@ -540,11 +469,9 @@ int findFunction(string s, vector *asm_parsed) return res; } -result_parse_line parseline(line sp, parsedLines *asm_parsed) -{ +result_parse_line parseline(line sp, parsedLines *asm_parsed) { - if (sp.opcde.find(":") != -1) - { + if (sp.opcde.find(":") != -1) { result_parse_line res; asm_Error.error = 0; res.op = opCodeType::label; @@ -553,79 +480,69 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) res.align = false; res.addText(trim(sp.opcde.substr(0, sp.opcde.find(":")))); // printf("processing label:%s\n",res.getText()); - if (findLabel(string(res.getText()), asm_parsed) != -1) - { + if (findLabel(string(res.getText()), asm_parsed) != -1) { asm_Error.error = 1; - // res.error.error_message = string_format("label %s is already declared in line %d\n", res.name.c_str(), (*asm_parsed)[findLabel(res.name, asm_parsed)].line); - asm_Error.error_message = string_format("label %s is already declare\n", res.getText()); + // res.error.error_message = string_format("label %s is already declared + // in line %d\n", res.name.c_str(), (*asm_parsed)[findLabel(res.name, + // asm_parsed)].line); + asm_Error.error_message = + string_format("label %s is already declare\n", res.getText()); } - if (sp.opcde.substr(0, 2).compare("@_") == 0) - { + if (sp.opcde.substr(0, 2).compare("@_") == 0) { res.align = true; } - // res.align=true; + // res.align=true; return res; } - if (sp.opcde.compare("add") == 0) - { + if (sp.opcde.compare("add") == 0) { return parseOperandes(sp.operandes, 3, op_add, 3, bin_add); } - if (sp.opcde.compare("sub") == 0) - { + if (sp.opcde.compare("sub") == 0) { return parseOperandes(sp.operandes, 3, op_sub, 3, bin_sub); } - if (sp.opcde.compare("quou") == 0) - { + if (sp.opcde.compare("quou") == 0) { return parseOperandes(sp.operandes, 3, op_quou, 3, bin_quou); } - if (sp.opcde.compare("quos") == 0) - { + if (sp.opcde.compare("quos") == 0) { return parseOperandes(sp.operandes, 3, op_quou, 3, bin_quos); } - if (sp.opcde.compare("add.n") == 0) - { + if (sp.opcde.compare("add.n") == 0) { return parseOperandes(sp.operandes, 3, op_add, 2, bin_add_n); } - if (sp.opcde.compare("addi") == 0) - { + if (sp.opcde.compare("addi") == 0) { return parseOperandes(sp.operandes, 3, op_addi, 3, bin_addi); } - if (sp.opcde.compare("addi.n") == 0) - { + if (sp.opcde.compare("addi.n") == 0) { return parseOperandes(sp.operandes, 3, op_addi_n, 2, bin_addi_n); } - if (sp.opcde.compare("and") == 0) - { + if (sp.opcde.compare("and") == 0) { return parseOperandes(sp.operandes, 3, op_and, 3, bin_and); } - if (sp.opcde.compare("salt") == 0) - { + if (sp.opcde.compare("salt") == 0) { return parseOperandes(sp.operandes, 3, op_and, 3, bin_salt); } - if (sp.opcde.compare("or") == 0) - { + if (sp.opcde.compare("or") == 0) { return parseOperandes(sp.operandes, 3, op_and, 3, bin_or); } - if (sp.opcde.compare("bnez") == 0) - { + if (sp.opcde.compare("bnez") == 0) { - result_parse_line ps = parseOperandes(sp.operandes, 2, op_bnez, 3, bin_bnez); + result_parse_line ps = + parseOperandes(sp.operandes, 2, op_bnez, 3, bin_bnez); ps.op = opCodeType::jump; ps.calculateOfssetJump = jump_bnez; return ps; } - if (sp.opcde.compare("beqz") == 0) - { + if (sp.opcde.compare("beqz") == 0) { - result_parse_line ps = parseOperandes(sp.operandes, 2, op_bnez, 3, bin_beqz); + result_parse_line ps = + parseOperandes(sp.operandes, 2, op_bnez, 3, bin_beqz); ps.op = opCodeType::jump; ps.calculateOfssetJump = jump_bnez; return ps; } - if (sp.opcde.compare("j") == 0) - { + if (sp.opcde.compare("j") == 0) { result_parse_line ps = parseOperandes(sp.operandes, 1, op_j, 3, bin_j); ps.op = opCodeType::jump; @@ -633,54 +550,53 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) return ps; } - if (sp.opcde.compare("bt") == 0) - { + if (sp.opcde.compare("bt") == 0) { - result_parse_line ps = parseOperandes(sp.operandes, 2, op_jumpfloat, 3, bin_bt); + result_parse_line ps = + parseOperandes(sp.operandes, 2, op_jumpfloat, 3, bin_bt); ps.op = opCodeType::jump; ps.calculateOfssetJump = jump_blt; return ps; } - if (sp.opcde.compare("bf") == 0) - { + if (sp.opcde.compare("bf") == 0) { - result_parse_line ps = parseOperandes(sp.operandes, 2, op_jumpfloat, 3, bin_bf); + result_parse_line ps = + parseOperandes(sp.operandes, 2, op_jumpfloat, 3, bin_bf); ps.op = opCodeType::jump; ps.calculateOfssetJump = jump_blt; return ps; } - if (sp.opcde.compare("olt.s") == 0) - { + if (sp.opcde.compare("olt.s") == 0) { - result_parse_line ps = parseOperandes(sp.operandes, 3, op_floatco, 3, bin_olts); + result_parse_line ps = + parseOperandes(sp.operandes, 3, op_floatco, 3, bin_olts); // ps.op = opCodeType::jump; // ps.calculateOfssetJump = jump_bge; return ps; } - if (sp.opcde.compare("oeq.s") == 0) - { + if (sp.opcde.compare("oeq.s") == 0) { - result_parse_line ps = parseOperandes(sp.operandes, 3, op_floatco, 3, bin_oeqs); + result_parse_line ps = + parseOperandes(sp.operandes, 3, op_floatco, 3, bin_oeqs); // ps.op = opCodeType::jump; // ps.calculateOfssetJump = jump_bge; return ps; } - if (sp.opcde.compare("ole.s") == 0) - { + if (sp.opcde.compare("ole.s") == 0) { - result_parse_line ps = parseOperandes(sp.operandes, 3, op_floatco, 3, bin_oles); + result_parse_line ps = + parseOperandes(sp.operandes, 3, op_floatco, 3, bin_oles); // ps.op = opCodeType::jump; // ps.calculateOfssetJump = jump_bge; return ps; } - if (sp.opcde.compare("blt") == 0) - { + if (sp.opcde.compare("blt") == 0) { result_parse_line ps = parseOperandes(sp.operandes, 3, op_blt, 3, bin_blt); ps.op = opCodeType::jump; @@ -688,44 +604,43 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) return ps; } - if (sp.opcde.compare("blti") == 0) - { + if (sp.opcde.compare("blti") == 0) { - result_parse_line ps = parseOperandes(sp.operandes, 3, op_blti, 3, bin_blti); + result_parse_line ps = + parseOperandes(sp.operandes, 3, op_blti, 3, bin_blti); ps.op = opCodeType::jump; ps.calculateOfssetJump = jump_blt; return ps; } - if (sp.opcde.compare("bgei") == 0) - { + if (sp.opcde.compare("bgei") == 0) { - result_parse_line ps = parseOperandes(sp.operandes, 3, op_blti, 3, bin_bgei); + result_parse_line ps = + parseOperandes(sp.operandes, 3, op_blti, 3, bin_bgei); ps.op = opCodeType::jump; ps.calculateOfssetJump = jump_blt; return ps; } - if (sp.opcde.compare("bnei") == 0) - { + if (sp.opcde.compare("bnei") == 0) { - result_parse_line ps = parseOperandes(sp.operandes, 3, op_blti, 3, bin_bnei); + result_parse_line ps = + parseOperandes(sp.operandes, 3, op_blti, 3, bin_bnei); ps.op = opCodeType::jump; ps.calculateOfssetJump = jump_blt; return ps; } - if (sp.opcde.compare("beqi") == 0) - { + if (sp.opcde.compare("beqi") == 0) { - result_parse_line ps = parseOperandes(sp.operandes, 3, op_blti, 3, bin_beqi); + result_parse_line ps = + parseOperandes(sp.operandes, 3, op_blti, 3, bin_beqi); ps.op = opCodeType::jump; ps.calculateOfssetJump = jump_blt; return ps; } - if (sp.opcde.compare("bltu") == 0) - { + if (sp.opcde.compare("bltu") == 0) { result_parse_line ps = parseOperandes(sp.operandes, 3, op_blt, 3, bin_bltu); ps.op = opCodeType::jump; @@ -733,8 +648,7 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) return ps; } - if (sp.opcde.compare("bge") == 0) - { + if (sp.opcde.compare("bge") == 0) { result_parse_line ps = parseOperandes(sp.operandes, 3, op_bge, 3, bin_bge); ps.op = opCodeType::jump; @@ -742,8 +656,7 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) return ps; } - if (sp.opcde.compare("bgeu") == 0) - { + if (sp.opcde.compare("bgeu") == 0) { result_parse_line ps = parseOperandes(sp.operandes, 3, op_bge, 3, bin_bgeu); ps.op = opCodeType::jump; @@ -752,8 +665,7 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) return ps; } - if (sp.opcde.compare("beq") == 0) - { + if (sp.opcde.compare("beq") == 0) { result_parse_line ps = parseOperandes(sp.operandes, 3, op_beq, 3, bin_beq); ps.op = opCodeType::jump; @@ -761,8 +673,7 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) return ps; } - if (sp.opcde.compare("bne") == 0) - { + if (sp.opcde.compare("bne") == 0) { result_parse_line ps = parseOperandes(sp.operandes, 3, op_bne, 3, bin_bne); ps.op = opCodeType::jump; @@ -770,8 +681,7 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) return ps; } - if (sp.opcde.compare("bequ") == 0) - { + if (sp.opcde.compare("bequ") == 0) { result_parse_line ps = parseOperandes(sp.operandes, 3, op_beq, 3, bin_beq); ps.op = opCodeType::jump; @@ -779,8 +689,7 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) return ps; } - if (sp.opcde.compare("bneu") == 0) - { + if (sp.opcde.compare("bneu") == 0) { result_parse_line ps = parseOperandes(sp.operandes, 3, op_bne, 3, bin_bne); ps.op = opCodeType::jump; @@ -789,181 +698,148 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) return ps; } - if (sp.opcde.compare("l32i") == 0) - { + if (sp.opcde.compare("l32i") == 0) { return parseOperandes(sp.operandes, 3, op_l32i, 3, bin_l32i); } - if (sp.opcde.compare("l32i.n") == 0) - { + if (sp.opcde.compare("l32i.n") == 0) { return parseOperandes(sp.operandes, 3, op_l32i_n, 2, bin_l32i_n); } - if (sp.opcde.compare("l32r") == 0) - { - result_parse_line ps = parseOperandes(sp.operandes, 2, op_l32r, 3, bin_l32r); + if (sp.opcde.compare("l32r") == 0) { + result_parse_line ps = + parseOperandes(sp.operandes, 2, op_l32r, 3, bin_l32r); ps.op = opCodeType::jump_32aligned; ps.calculateOfssetJump = jump_l32r; return ps; } - if (sp.opcde.compare("call8") == 0) - { - result_parse_line ps = parseOperandes(sp.operandes, 1, op_call8, 3, bin_call8); + if (sp.opcde.compare("call8") == 0) { + result_parse_line ps = + parseOperandes(sp.operandes, 1, op_call8, 3, bin_call8); ps.op = opCodeType::jump_32aligned; ps.calculateOfssetJump = jump_call8; return ps; } - if (sp.opcde.compare("call12") == 0) - { - result_parse_line ps = parseOperandes(sp.operandes, 1, op_call8, 3, bin_call12); + if (sp.opcde.compare("call12") == 0) { + result_parse_line ps = + parseOperandes(sp.operandes, 1, op_call8, 3, bin_call12); ps.op = opCodeType::jump_32aligned; ps.calculateOfssetJump = jump_call8; return ps; } - if (sp.opcde.compare("rsr") == 0) - { + if (sp.opcde.compare("rsr") == 0) { return parseOperandes(sp.operandes, 2, op_rsr, 3, bin_rsr); } - if (sp.opcde.compare("wsr") == 0) - { + if (sp.opcde.compare("wsr") == 0) { return parseOperandes(sp.operandes, 2, op_rsr, 3, bin_wsr); } - if (sp.opcde.compare("mov") == 0) - { + if (sp.opcde.compare("mov") == 0) { return parseOperandes(sp.operandes, 2, op_mov, 3, bin_mov); } - if (sp.opcde.compare("movr") == 0) - { + if (sp.opcde.compare("movr") == 0) { return parseOperandes(sp.operandes, 2, op_mov, 3, bin_mov); } - if (sp.opcde.compare("abs") == 0) - { + if (sp.opcde.compare("abs") == 0) { return parseOperandes(sp.operandes, 2, op_mov, 3, bin_abs); } - if (sp.opcde.compare("sll") == 0) - { + if (sp.opcde.compare("sll") == 0) { return parseOperandes(sp.operandes, 2, op_mov, 3, bin_sll); } - if (sp.opcde.compare("ssl") == 0) - { + if (sp.opcde.compare("ssl") == 0) { return parseOperandes(sp.operandes, 1, op_ssl, 3, bin_ssl); } - if (sp.opcde.compare("srl") == 0) - { + if (sp.opcde.compare("srl") == 0) { return parseOperandes(sp.operandes, 2, op_mov, 3, bin_srl); } - if (sp.opcde.compare("mull") == 0) - { + if (sp.opcde.compare("mull") == 0) { return parseOperandes(sp.operandes, 3, op_add, 3, bin_mull); } - if (sp.opcde.compare("mov.n") == 0) - { + if (sp.opcde.compare("mov.n") == 0) { return parseOperandes(sp.operandes, 2, op_mov_n, 2, bin_mov_n); } - if (sp.opcde.compare("neg") == 0) - { + if (sp.opcde.compare("neg") == 0) { return parseOperandes(sp.operandes, 2, op_neg, 3, bin_neg); } - if (sp.opcde.compare("movi") == 0) - { + if (sp.opcde.compare("movi") == 0) { return parseOperandes(sp.operandes, 2, op_movi, 3, bin_movi); } - if (sp.opcde.compare("movi.n") == 0) - { + if (sp.opcde.compare("movi.n") == 0) { return parseOperandes(sp.operandes, 2, op_movi_n, 2, bin_movi_n); } - if (sp.opcde.compare("s32i") == 0) - { + if (sp.opcde.compare("s32i") == 0) { return parseOperandes(sp.operandes, 3, op_s32i, 3, bin_s32i); } - if (sp.opcde.compare("s32i.n") == 0) - { + if (sp.opcde.compare("s32i.n") == 0) { return parseOperandes(sp.operandes, 3, op_s32i_n, 2, bin_s32i_n); } - if (sp.opcde.compare("s8i") == 0) - { + if (sp.opcde.compare("s8i") == 0) { return parseOperandes(sp.operandes, 3, op_s8i, 3, bin_s8i); } - if (sp.opcde.compare("l8ui") == 0) - { + if (sp.opcde.compare("l8ui") == 0) { return parseOperandes(sp.operandes, 3, op_l8ui, 3, bin_l8ui); } - if (sp.opcde.compare("s16i") == 0) - { + if (sp.opcde.compare("s16i") == 0) { return parseOperandes(sp.operandes, 3, op_s16i, 3, bin_s16i); } - if (sp.opcde.compare("l16si") == 0) - { + if (sp.opcde.compare("l16si") == 0) { return parseOperandes(sp.operandes, 3, op_l16si, 3, bin_l16si); } - if (sp.opcde.compare("l16ui") == 0) - { + if (sp.opcde.compare("l16ui") == 0) { return parseOperandes(sp.operandes, 3, op_l16ui, 3, bin_l16ui); } - if (sp.opcde.compare("slli") == 0) - { + if (sp.opcde.compare("slli") == 0) { return parseOperandes(sp.operandes, 3, op_slli, 3, bin_slli); } - if (sp.opcde.compare("remu") == 0) - { + if (sp.opcde.compare("remu") == 0) { return parseOperandes(sp.operandes, 3, op_remu, 3, bin_remu); } - if (sp.opcde.compare("entry") == 0) - { + if (sp.opcde.compare("entry") == 0) { return parseOperandes(sp.operandes, 2, op_entry, 3, bin_entry); } - if (sp.opcde.compare("nop.n") == 0) - { + if (sp.opcde.compare("nop.n") == 0) { return parseOperandes(sp.operandes, 0, NULL, 2, bin_nop_n); } - if (sp.opcde.compare("nop") == 0) - { + if (sp.opcde.compare("nop") == 0) { return parseOperandes(sp.operandes, 0, NULL, 3, bin_nop); } - if (sp.opcde.compare("callx8") == 0) - { + if (sp.opcde.compare("callx8") == 0) { return parseOperandes(sp.operandes, 1, op_callx8, 3, bin_callx8); } - if (sp.opcde.compare("extui") == 0) - { + if (sp.opcde.compare("extui") == 0) { return parseOperandes(sp.operandes, 4, op_extui, 3, bin_extui); } - if (sp.opcde.compare("retw.n") == 0) - { + if (sp.opcde.compare("retw.n") == 0) { return parseOperandes(sp.operandes, 0, NULL, 2, bin_retw_n); } - if (sp.opcde.compare(".bytes") == 0) - { + if (sp.opcde.compare(".bytes") == 0) { char *endptr = NULL; // vector sf=split(sp.operandes," "); uint16_t value; string suite; string depart = trim(sp.operandes); - if (depart.find_first_of(" ") < depart.size()) - { + if (depart.find_first_of(" ") < depart.size()) { suite = depart.substr(depart.find_first_of(" ") + 1, depart.size()); - value = strtol(depart.substr(0, depart.find_first_of(" ")).c_str(), &endptr, 10); - } - else - { + value = strtol(depart.substr(0, depart.find_first_of(" ")).c_str(), + &endptr, 10); + } else { suite = ""; // depart; value = strtol(depart.c_str(), &endptr, 10); } // value = strtol(sp.operandes.c_str(), &endptr, 10); - // int value = strtol(sp.operandes.substr(0, sp.operandes.find_first_of(" ")).c_str(), &endptr, 10); - // printf("first of %d\n",sp.operandes.find_first_of(" ")); - // printf("on a /%s/%s/%d\n",depart.c_str(), suite.c_str(),value); + // int value = strtol(sp.operandes.substr(0, sp.operandes.find_first_of(" + // ")).c_str(), &endptr, 10); printf("first of + // %d\n",sp.operandes.find_first_of(" ")); printf("on a + // /%s/%s/%d\n",depart.c_str(), suite.c_str(),value); - if (*endptr == 0) - { + if (*endptr == 0) { result_parse_line ps; ps.op = opCodeType::data; - result_parse_line *ps1 = getInstrAtPos((*asm_parsed).size() - 1); //&(*asm_parsed)[(*asm_parsed).size() - 1]; - if (ps1->op == opCodeType::label) - { + result_parse_line *ps1 = getInstrAtPos( + (*asm_parsed).size() - 1); //&(*asm_parsed)[(*asm_parsed).size() - 1]; + if (ps1->op == opCodeType::label) { ps1->size = 4; ps1->op = opCodeType::data_label; ps1->align = true; @@ -971,8 +847,7 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) ps.size = (value / 4) * 4 + 4; vector sf = split(suite, " "); string name = ""; - for (int i = 0; i < sf.size(); i++) - { + for (int i = 0; i < sf.size(); i++) { unsigned int __num = 0; sscanf(sf[i].c_str(), "%x", &__num); // printf("%s \r\n",sf[i].c_str()); @@ -984,30 +859,25 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) ps.nameref = EOF_TEXTARRAY; sf.clear(); return ps; - } - else - { + } else { // result_parse_line ps; asm_Error.error = 1; asm_Error.error_message = "Prior instruction is not a label"; return ps; } - } - else - { + } else { result_parse_line ps; asm_Error.error = 1; asm_Error.error_message = "Not Valid size for bytes"; return ps; } } - if (sp.opcde.compare(".word") == 0) - { + if (sp.opcde.compare(".word") == 0) { result_parse_line ps; ps.op = opCodeType::number; - result_parse_line *ps1 = getInstrAtPos((*asm_parsed).size() - 1); // &(*asm_parsed)[(*asm_parsed).size() - 1]; - if (ps1->op == opCodeType::label) - { + result_parse_line *ps1 = getInstrAtPos( + (*asm_parsed).size() - 1); // &(*asm_parsed)[(*asm_parsed).size() - 1]; + if (ps1->op == opCodeType::label) { ps1->size = 4; ps1->op = opCodeType::number_label; ps1->align = true; @@ -1021,8 +891,7 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) // return parseOperandes(sp.operandes, 1, op_word, 4, bin_word); } - if (sp.opcde.compare(".align") == 0) - { + if (sp.opcde.compare(".align") == 0) { result_parse_line ps; asm_Error.error = 0; ps.align = false; @@ -1031,43 +900,37 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) return ps; } - if (sp.opcde.compare(".global") == 0) - { + if (sp.opcde.compare(".global") == 0) { result_parse_line ps; ps = parseOperandes(sp.operandes, 1, op_global, 0, NULL); // printf("operandes %s\r\n",ps.getText()); ps.op = opCodeType::function_declaration; return ps; } - if (sp.opcde.compare(".globalExt") == 0) - { + if (sp.opcde.compare(".globalExt") == 0) { result_parse_line ps; ps = parseOperandes(sp.operandes, 1, op_global, 0, NULL); // printf("operandes %s\r\n",ps.getText()); ps.op = opCodeType::ext_function_declaration; return ps; } - if (sp.opcde.compare(".var") == 0) - { + if (sp.opcde.compare(".var") == 0) { result_parse_line ps; ps = parseOperandes(sp.operandes, 1, op_global, 0, NULL); // printf("operandes %s\r\n",ps.getText()); ps.op = opCodeType::variable; return ps; } - if (sp.opcde.compare("callExt") == 0) - { + if (sp.opcde.compare("callExt") == 0) { /* - result_parse_line ps = parseOperandes(sp.operandes, 1, op_call8, 3, bin_call8); - ps.op = opCodeType::external_call; - ps.calculateOfssetJump = jump_call8; - return ps; + result_parse_line ps = parseOperandes(sp.operandes, 1, op_call8, 3, + bin_call8); ps.op = opCodeType::external_call; ps.calculateOfssetJump = + jump_call8; return ps; */ result_parse_line ps; ps = parseOperandes(sp.operandes, 2, op_callExt, 0, bin_movExt); - if (asm_Error.error != 0) - { + if (asm_Error.error != 0) { return ps; } @@ -1075,12 +938,12 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) // { // string debugsav=ps.debugtxt; int savbin = ps.bincode; - ps = parseOperandes(string_format("a%d,%s", ps.bincode, ps.getText()), 2, op_l32r, 3, bin_l32r); + ps = parseOperandes(string_format("a%d,%s", ps.bincode, ps.getText()), 2, + op_l32r, 3, bin_l32r); ps.op = opCodeType::jump_32aligned; ps.calculateOfssetJump = jump_l32r; int index = findLabel(string(ps.getText()), asm_parsed); - if (index > -1) - { + if (index > -1) { result_parse_line *ps1 = getInstrAtPos(index); ps1->op = opCodeType::external_call; ps1->align = true; @@ -1089,7 +952,8 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) // ps.op = opCodeType::external_call; //(*asm_parsed).push_back(ps); addInstr(ps, asm_parsed); - ps = parseOperandes(string_format("a%d", savbin), 1, op_callx8, 3, bin_callx8); + ps = parseOperandes(string_format("a%d", savbin), 1, op_callx8, 3, + bin_callx8); // ps.debugtxt=debugsav; // ps.op = opCodeType::external_call; @@ -1098,16 +962,14 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) return ps; } - if (sp.opcde.compare("movExt") == 0) - { + if (sp.opcde.compare("movExt") == 0) { result_parse_line ps; ps = parseOperandes(sp.operandes, 2, op_l32r, 3, bin_l32r); ps.op = opCodeType::jump_32aligned; - //ps.align=true; + // ps.align=true; ps.calculateOfssetJump = jump_l32r; int index = findLabel(string(ps.getText()), asm_parsed); - if (index > -1) - { + if (index > -1) { result_parse_line *ps1 = getInstrAtPos(index); ps1->op = opCodeType::external_var_label; } @@ -1117,30 +979,30 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) if (i == -1) { asm_Error.error = 1; - asm_Error.error_message = string_format("External variable %s not found\n", ps.getText()); + asm_Error.error_message = string_format("External variable %s not + found\n", ps.getText()); } else { // string debugsav=ps.debugtxt; - ps = parseOperandes(string_format("a%d,a2,%d", ps.bincode, i * 4), 3, op_l32i, 3, bin_l32i); + ps = parseOperandes(string_format("a%d,a2,%d", ps.bincode, i * 4), 3, + op_l32i, 3, bin_l32i); // ps.debugtxt=debugsav; }*/ return ps; } - if (sp.opcde.compare(".text") == 0) - { + if (sp.opcde.compare(".text") == 0) { result_parse_line ps; sp.operandes = trim(sp.operandes); - if (sp.operandes.size() > 0) - { + if (sp.operandes.size() > 0) { ps.op = opCodeType::data; - result_parse_line *ps1 = getInstrAtPos((*asm_parsed).size() - 1); //&(*asm_parsed)[(*asm_parsed).size() - 1]; - if (ps1->op == opCodeType::label) - { + result_parse_line *ps1 = getInstrAtPos( + (*asm_parsed).size() - 1); //&(*asm_parsed)[(*asm_parsed).size() - 1]; + if (ps1->op == opCodeType::label) { ps1->size = 4; ps1->op = opCodeType::data_label; ps1->align = true; @@ -1149,16 +1011,13 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) // ps.name = sp.operandes; ps.addText(sp.operandes + '\n' + '\0'); } - } - else - { + } else { asm_Error.error = 1; asm_Error.error_message = "no valuable string"; } return ps; } - if (sp.opcde.compare(".type") == 0) - { + if (sp.opcde.compare(".type") == 0) { result_parse_line ps; ps.op = opCodeType::not_known; asm_Error.error = 0; @@ -1168,96 +1027,74 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) } /************float operator********/ - if (sp.opcde.compare("lsi") == 0) - { + if (sp.opcde.compare("lsi") == 0) { return parseOperandes(sp.operandes, 3, op_lsi, 3, bin_lsi); } - if (sp.opcde.compare("ssi") == 0) - { + if (sp.opcde.compare("ssi") == 0) { return parseOperandes(sp.operandes, 3, op_ssi, 3, bin_ssi); } - if (sp.opcde.compare("rfr") == 0) - { + if (sp.opcde.compare("rfr") == 0) { return parseOperandes(sp.operandes, 2, op_rfr, 3, bin_rfr); } - if (sp.opcde.compare("wfr") == 0) - { + if (sp.opcde.compare("wfr") == 0) { return parseOperandes(sp.operandes, 2, op_wfr, 3, bin_wfr); } - if (sp.opcde.compare("add.s") == 0) - { + if (sp.opcde.compare("add.s") == 0) { return parseOperandes(sp.operandes, 3, op_adds, 3, bin_adds); } - if (sp.opcde.compare("sub.s") == 0) - { + if (sp.opcde.compare("sub.s") == 0) { return parseOperandes(sp.operandes, 3, op_subs, 3, bin_subs); } - if (sp.opcde.compare("float.s") == 0) - { + if (sp.opcde.compare("float.s") == 0) { return parseOperandes(sp.operandes, 3, op_floats, 3, bin_floats); } - if (sp.opcde.compare("floor.s") == 0) - { + if (sp.opcde.compare("floor.s") == 0) { return parseOperandes(sp.operandes, 3, op_floors, 3, bin_floors); } - if (sp.opcde.compare("mul.s") == 0) - { + if (sp.opcde.compare("mul.s") == 0) { return parseOperandes(sp.operandes, 3, op_muls, 3, bin_muls); } - if (sp.opcde.compare("trunc.s") == 0) - { + if (sp.opcde.compare("trunc.s") == 0) { return parseOperandes(sp.operandes, 3, op_truncs, 3, bin_truncs); } - if (sp.opcde.compare("round.s") == 0) - { + if (sp.opcde.compare("round.s") == 0) { return parseOperandes(sp.operandes, 3, op_truncs, 3, bin_rounds); } - if (sp.opcde.compare("div0.s") == 0) - { + if (sp.opcde.compare("div0.s") == 0) { return parseOperandes(sp.operandes, 2, op_div0s, 3, bin_div0s); } - if (sp.opcde.compare("divn.s") == 0) - { + if (sp.opcde.compare("divn.s") == 0) { return parseOperandes(sp.operandes, 3, op_divns, 3, bin_divns); } - if (sp.opcde.compare("const.s") == 0) - { + if (sp.opcde.compare("const.s") == 0) { return parseOperandes(sp.operandes, 2, op_consts, 3, bin_consts); } - if (sp.opcde.compare("mov.s") == 0) - { + if (sp.opcde.compare("mov.s") == 0) { return parseOperandes(sp.operandes, 2, op_movs, 3, bin_movs); } - if (sp.opcde.compare("abs.s") == 0) - { + if (sp.opcde.compare("abs.s") == 0) { return parseOperandes(sp.operandes, 2, op_movs, 3, bin_abss); } - if (sp.opcde.compare("maddn.s") == 0) - { + if (sp.opcde.compare("maddn.s") == 0) { return parseOperandes(sp.operandes, 3, op_maddns, 3, bin_maddns); } - if (sp.opcde.compare("nexp01.s") == 0) - { + if (sp.opcde.compare("nexp01.s") == 0) { return parseOperandes(sp.operandes, 2, op_nexp01s, 3, bin_nexp01s); } - if (sp.opcde.compare("neg.s") == 0) - { + if (sp.opcde.compare("neg.s") == 0) { return parseOperandes(sp.operandes, 2, op_negs, 3, bin_negs); } - if (sp.opcde.compare("addexpm.s") == 0) - { + if (sp.opcde.compare("addexpm.s") == 0) { return parseOperandes(sp.operandes, 2, op_addexpms, 3, bin_addexpms); } - if (sp.opcde.compare("addexp.s") == 0) - { + if (sp.opcde.compare("addexp.s") == 0) { return parseOperandes(sp.operandes, 2, op_addexps, 3, bin_addexps); } - if (sp.opcde.compare("mkdadj.s") == 0) - { + if (sp.opcde.compare("mkdadj.s") == 0) { return parseOperandes(sp.operandes, 2, op_mkdadjs, 3, bin_mkdadjs); } result_parse_line res; @@ -1268,13 +1105,11 @@ result_parse_line parseline(line sp, parsedLines *asm_parsed) return res; } -line splitOpcodeOperande(string s) -{ +line splitOpcodeOperande(string s) { line res; s = trim(s); - if (s.size() < 2) - { + if (s.size() < 2) { res.error = 1; return res; } @@ -1282,12 +1117,9 @@ line splitOpcodeOperande(string s) // string s=trim(s2); string opcode = trim(s.substr(0, s.find_first_of(" "))); string operandes; - if (s.find_first_of(" ") < s.size()) - { + if (s.find_first_of(" ") < s.size()) { operandes = s.substr(s.find_first_of(" "), s.size()); - } - else - { + } else { operandes = ""; } res.opcde = opcode; @@ -1295,14 +1127,14 @@ line splitOpcodeOperande(string s) return res; } void printparsdAsm(uint32_t start_address, parsedLines *asm_parsed); -error_message_struct parseASM(Text *_footer, Text *_header, Text *_content, parsedLines *asm_parsed) -{ +error_message_struct parseASM(Text *_footer, Text *_header, Text *_content, + parsedLines *asm_parsed) { // list lines = *_lines; error_message_struct main_error; main_error.error = 0; main_error.error_message = ""; optimize(_content); - + #ifdef __TEST_DEBUG _header->display(); // content.display(); @@ -1323,18 +1155,15 @@ error_message_struct parseASM(Text *_footer, Text *_header, Text *_content, pars int _nb_data = 0; int _size = 0; tmp_data_size = 0; - for (int i = 0; i < _header->size(); i++) - { + for (int i = 0; i < _header->size(); i++) { string str = _header->textAt(i); - if (str.compare(0, 6, ".bytes") == 0) - { + if (str.compare(0, 6, ".bytes") == 0) { int h = 0; _nb_data++; vector __v = split(trim(str), " "); sscanf(__v[1].c_str(), "%d", &h); h = (h / 4) * 4 + 4; - if (__v.size() > 2) - { + if (__v.size() > 2) { // printf("%s \n\r",str.c_str()); tmp_data_size += h; } @@ -1346,44 +1175,38 @@ error_message_struct parseASM(Text *_footer, Text *_header, Text *_content, pars // printf("taille %d\r\n",_size); int _nb_align_label = 0; int _nb_not_aligned_label = 0; - for (int i = 0; i < _content->size(); i++) - { + for (int i = 0; i < _content->size(); i++) { string str = _header->textAt(i); - if (str.compare(0, 2, "@_") == 0) - { + if (str.compare(0, 2, "@_") == 0) { _nb_align_label++; - } - else - { - if (str.find(":") != -1) - { + } else { + if (str.find(":") != -1) { _nb_not_aligned_label++; } } } - for (int i = 0; i < _footer->size(); i++) - { + for (int i = 0; i < _footer->size(); i++) { string str = _footer->textAt(i); - if (str.compare(0, 2, "@_") == 0) - { + if (str.compare(0, 2, "@_") == 0) { _nb_align_label++; - } - else - { - if (str.find(":") != -1) - { + } else { + if (str.find(":") != -1) { _nb_not_aligned_label++; } } } - _instr_size = (_nb_align_label + 1) * ALIGN_INSTR + (_content->size() + _footer->size() - _nb_not_aligned_label) * 3; + _instr_size = + (_nb_align_label + 1) * ALIGN_INSTR + + (_content->size() + _footer->size() - _nb_not_aligned_label) * 3; // printf("taille instr %d\r\n",_instr_size); _instr_size = (_instr_size / 8) * 8 + 8; tmp_instr_size = _instr_size + (tmp_data_size / 4) * 4 + 4; - string _d = string_format("Creation of an %d bytes binary and %d bytes data %d", _instr_size, _size, (tmp_data_size / 4) * 4 + 4); + string _d = + string_format("Creation of an %d bytes binary and %d bytes data %d", + _instr_size, _size, (tmp_data_size / 4) * 4 + 4); pushToConsole(_d); updateMem(); @@ -1392,46 +1215,41 @@ error_message_struct parseASM(Text *_footer, Text *_header, Text *_content, pars tmp_binary_data = tmp_exec + _instr_size; updateMem(); displayStat("after mem"); - if (_size > 0) - { + if (_size > 0) { //_size += ALIGN_DATA * _nb_data; // binary_data = (uint8_t *)malloc((_size / 4) * 4 + 4); _data_size = (_size / 4) * 4 + 4; } // printf("her:\r\n"); - string d = string_format("Parsing %d assembly lines ... ", _header->size() + _content->size() + _footer->size()); + string d = + string_format("Parsing %d assembly lines ... ", + _header->size() + _content->size() + _footer->size()); pushToConsole(d); int size = _header->size(); int tmp_size = size; int tmp_size2 = _content->size() + _header->size(); - for (int i = 0; i < size; i++) - { + for (int i = 0; i < size; i++) { - if (__parser_debug) - { + if (__parser_debug) { printf("on parse line: %d : %s\r\n", i, _header->front().c_str()); } // printf("on parse line: %d : %s\r\n",i,_header->front().c_str()); line res = splitOpcodeOperande(_header->front()); - if (!res.error) - { + if (!res.error) { result_parse_line re_sparse = parseline(res, asm_parsed); - if (__parser_debug) - { + if (__parser_debug) { // re_sparse.debugtxt = _lines->front(); } re_sparse.line = i + 1; // printf("%d %s %d\r\n",i+1,_lines->front().c_str(),sizeof(re_sparse)); - if (asm_Error.error) - { + if (asm_Error.error) { main_error.error = 1; - main_error.error_message += string_format("line:%d %s\r\n", i, asm_Error.error_message.c_str()); - } - else - { + main_error.error_message += + string_format("line:%d %s\r\n", i, asm_Error.error_message.c_str()); + } else { // printf("befoire line:%d mem:%u\r\n",i, esp_get_free_heap_size()); // asm_parsed->push_back(re_sparse); addInstr(re_sparse, asm_parsed); @@ -1439,45 +1257,39 @@ error_message_struct parseASM(Text *_footer, Text *_header, Text *_content, pars // printf("afetr line:%d mem:%u\r\n",i, esp_get_free_heap_size()); } } - // #ifndef __TEST_DEBUG + // #ifndef __TEST_DEBUG _header->pop_front(); - // #endif + // #endif } size = _content->size(); - for (int i = tmp_size; i < size + tmp_size; i++) - { - if ((_content->_texts[0]) != NULL) - { - if (__parser_debug) - { + for (int i = tmp_size; i < size + tmp_size; i++) { + if ((_content->_texts[0]) != NULL) { + if (__parser_debug) { printf("on parse line: %d : %s\r\n", i, _content->front().c_str()); } - if (_content->front().compare(" ") != 0) - { + if (_content->front().compare(" ") != 0) { // printf("on parse line: %d : %s\r\n",i,_lines->front().c_str()); line res = splitOpcodeOperande(_content->front()); - if (!res.error) - { + if (!res.error) { result_parse_line re_sparse = parseline(res, asm_parsed); - #ifdef __TEST_DEBUG +#ifdef __TEST_DEBUG re_sparse.debugtxt = _content->front(); - #endif - if (__parser_debug) - { +#endif + if (__parser_debug) { // re_sparse.debugtxt = _lines->front(); } // printf("on parse line: %d : %s\r\n",i,_content->front().c_str()); re_sparse.line = i + 1; - // printf("%d %s %d\r\n",i+1,_lines->front().c_str(),sizeof(re_sparse)); + // printf("%d %s + // %d\r\n",i+1,_lines->front().c_str(),sizeof(re_sparse)); - if (asm_Error.error) - { + if (asm_Error.error) { main_error.error = 1; - main_error.error_message += string_format("line:%d %s %s\r\n", i, _content->front().c_str(), asm_Error.error_message.c_str()); - } - else - { + main_error.error_message += string_format( + "line:%d %s %s\r\n", i, _content->front().c_str(), + asm_Error.error_message.c_str()); + } else { // printf("befoire line:%d mem:%u\r\n",i, esp_get_free_heap_size()); // asm_parsed->push_back(re_sparse); addInstr(re_sparse, asm_parsed); @@ -1488,42 +1300,35 @@ error_message_struct parseASM(Text *_footer, Text *_header, Text *_content, pars } } // printf("on parse line: %d parseds\r\n", i); - // #ifndef __TEST_DEBUG + // #ifndef __TEST_DEBUG _content->pop_front(); - // #endif + // #endif // printf("on delete line : %d \r\n", i); } size = _footer->size(); - for (int i = tmp_size2; i < size + tmp_size2; i++) - { - if (__parser_debug) - { + for (int i = tmp_size2; i < size + tmp_size2; i++) { + if (__parser_debug) { printf("on parse line: %d : %s\r\n", i, _footer->front().c_str()); } - if (_footer->front().compare(" ") != 0) - { + if (_footer->front().compare(" ") != 0) { // printf("on parse line: %d : %s\r\n",i,_lines->front().c_str()); line res = splitOpcodeOperande(_footer->front()); - if (!res.error) - { + if (!res.error) { result_parse_line re_sparse = parseline(res, asm_parsed); - - if (__parser_debug) - { + + if (__parser_debug) { // re_sparse.debugtxt = _lines->front(); } // printf("on parse line: %d : %s\r\n",i,_content->front().c_str()); re_sparse.line = i + 1; // printf("%d %s %d\r\n",i+1,_lines->front().c_str(),sizeof(re_sparse)); - if (asm_Error.error) - { + if (asm_Error.error) { main_error.error = 1; - main_error.error_message += string_format("line:%d %s\r\n", i, asm_Error.error_message.c_str()); - } - else - { + main_error.error_message += string_format( + "line:%d %s\r\n", i, asm_Error.error_message.c_str()); + } else { // printf("befoire line:%d mem:%u\r\n",i, esp_get_free_heap_size()); // asm_parsed->push_back(re_sparse); addInstr(re_sparse, asm_parsed); @@ -1534,43 +1339,39 @@ error_message_struct parseASM(Text *_footer, Text *_header, Text *_content, pars } // #ifndef __TEST_DEBUG _footer->pop_front(); - // #endif + // #endif } // printf("Done.\r\n"); - if (main_error.error == 1) - { + if (main_error.error == 1) { // free(binary_data); free(tmp_exec); } updateMem(); displayStat(); - #ifdef __TEST_DEBUG -printparsdAsm(0,asm_parsed); - #endif +#ifdef __TEST_DEBUG + printparsdAsm(0, asm_parsed); +#endif return main_error; } -void printparsdAsm(uint32_t start_address, parsedLines *asm_parsed) -{ - for (vector::iterator it = asm_parsed->begin(); it != asm_parsed->end(); it++) - { +void printparsdAsm(uint32_t start_address, parsedLines *asm_parsed) { + for (vector::iterator it = asm_parsed->begin(); + it != asm_parsed->end(); it++) { result_parse_line re_sparse = *(*it); - if (asm_Error.error) - { - // printf("%8x \t %s\t %s\n", re_sparse.address + start_address, re_sparse.debugtxt.c_str(), re_sparse.error.error_message.c_str()); - } - else - { - if (re_sparse.op == opCodeType::label) - { - printf("%8x \t <%s>:\n", (unsigned int)(re_sparse.address + start_address), re_sparse.getText()); + if (asm_Error.error) { + // printf("%8x \t %s\t %s\n", re_sparse.address + start_address, + // re_sparse.debugtxt.c_str(), re_sparse.error.error_message.c_str()); + } else { + if (re_sparse.op == opCodeType::label) { + printf("%8x \t <%s>:\n", + (unsigned int)(re_sparse.address + start_address), + re_sparse.getText()); + } else { +#ifdef __TEST_DEBUG + printf("%8x \t %6x\t %s\n", re_sparse.address + start_address, + re_sparse.bincode, re_sparse.debugtxt.c_str()); +#endif } - else - { - #ifdef __TEST_DEBUG - printf("%8x \t %6x\t %s\n", re_sparse.address + start_address, re_sparse.bincode, re_sparse.debugtxt.c_str()); - #endif - } } } } @@ -1586,9 +1387,11 @@ void flagLabel32aligned(parsedLines *asm_parsed) #endif // uint32_t add = 0; // vector::iterator it = (*asm_parsed).begin(); - for (vector::iterator it = asm_parsed->begin(); it != asm_parsed->end(); it++) + for (vector::iterator it = asm_parsed->begin(); it != +asm_parsed->end(); it++) { - if ((*it)->op == opCodeType::jump_32aligned || (*it)->op == opCodeType::function_declaration) + if ((*it)->op == opCodeType::jump_32aligned || (*it)->op == +opCodeType::function_declaration) { int i = findLabel(string((*it)->getText()), asm_parsed); if (i != -1) @@ -1608,8 +1411,7 @@ void flagLabel32aligned(parsedLines *asm_parsed) } */ -error_message_struct calculateJump(uint8_t *exec, parsedLines *asm_parsed) -{ +error_message_struct calculateJump(uint8_t *exec, parsedLines *asm_parsed) { #ifdef __CONSOLE_ESP32 // LedOS.pushToConsole("alculating jumps 2..."); @@ -1621,31 +1423,31 @@ error_message_struct calculateJump(uint8_t *exec, parsedLines *asm_parsed) error_message_struct error; error.error = 0; error.error_message = ""; - for (vector::iterator it = asm_parsed->begin(); it != asm_parsed->end(); it++) - { + for (vector::iterator it = asm_parsed->begin(); + it != asm_parsed->end(); it++) { result_parse_line *parse_line = *it; - if ((parse_line->op == opCodeType::jump) || (parse_line->op == opCodeType::jump_32aligned)) - { + if ((parse_line->op == opCodeType::jump) || + (parse_line->op == opCodeType::jump_32aligned)) { int index = findLabel(string(parse_line->getText()), asm_parsed); - if (index != -1) - { - if (parse_line->calculateOfssetJump) - { - parse_line->bincode = parse_line->calculateOfssetJump(parse_line->bincode, parse_line->address, getInstrAtPos(index)->address); + if (index != -1) { + if (parse_line->calculateOfssetJump) { + parse_line->bincode = parse_line->calculateOfssetJump( + parse_line->bincode, parse_line->address, + getInstrAtPos(index)->address); // printf("line:%d %d\r\n",parse_line->address,parse_line->size); - memcpy(exec + parse_line->address, &parse_line->bincode, parse_line->size); - } - else - { + memcpy(exec + parse_line->address, &parse_line->bincode, + parse_line->size); + } else { error.error = 1; - // error.error_message += string_format("asm_Error:No method to calcuylate jump offset for %s\n", parse_line->debugtxt.c_str()); + // error.error_message += string_format("asm_Error:No method to + // calcuylate jump offset for %s\n", parse_line->debugtxt.c_str()); } - } - else - { + } else { error.error = 1; // printf("b ou foune\r\n"); - error.error_message += string_format("line : %d label %s not found\n\r", parse_line->line, parse_line->getText()); + error.error_message += + string_format("line : %d label %s not found\n\r", parse_line->line, + parse_line->getText()); } } } @@ -1653,8 +1455,7 @@ error_message_struct calculateJump(uint8_t *exec, parsedLines *asm_parsed) return error; } -uint8_t *createBinaryHeader(parsedLines *asm_parsed) -{ +uint8_t *createBinaryHeader(parsedLines *asm_parsed) { error_message_struct error; uint16_t nb_data = 0; uint16_t tmp_data = 0; @@ -1662,24 +1463,20 @@ uint8_t *createBinaryHeader(parsedLines *asm_parsed) uint8_t type; uint16_t text_size; binary_header_size = 2; - for (vector::iterator it = asm_parsed->begin(); it != asm_parsed->end(); it++) - { - if ((*it)->op == opCodeType::data_label || (*it)->op == opCodeType::number_label) - { + for (vector::iterator it = asm_parsed->begin(); + it != asm_parsed->end(); it++) { + if ((*it)->op == opCodeType::data_label || + (*it)->op == opCodeType::number_label) { nb_objects++; binary_header_size += 1; binary_header_size += 4 + 2; - } - else if ((*it)->op == opCodeType::external_var_label) - { + } else if ((*it)->op == opCodeType::external_var_label) { nb_objects++; binary_header_size += 1; binary_header_size += 2; // text size binary_header_size += strlen((*it)->getText()) + 1; binary_header_size += 2; // nb_data - } - else if ((*it)->op == opCodeType::external_call) - { + } else if ((*it)->op == opCodeType::external_call) { nb_objects++; binary_header_size += 1; binary_header_size += 2; // text size @@ -1687,17 +1484,13 @@ uint8_t *createBinaryHeader(parsedLines *asm_parsed) binary_header_size += 2; // nb_data // binary_header_size += 4; // bincode // binary_header_size += 4; // address - } - else if ((*it)->op == opCodeType::data) - { + } else if ((*it)->op == opCodeType::data) { nb_objects++; binary_header_size += 1; binary_header_size += 4; binary_header_size += 2; // tmp data binary_header_size += 2; // size to copy - } - else if ((*it)->op == opCodeType::function_declaration) - { + } else if ((*it)->op == opCodeType::function_declaration) { nb_objects++; binary_header_size += 1; binary_header_size += 2; // text size @@ -1713,8 +1506,7 @@ uint8_t *createBinaryHeader(parsedLines *asm_parsed) } // on ajoute les json // printf("douiung the jds son\n"); - for (int js = 0; js < jsonVar.size(); js++) - { + for (int js = 0; js < jsonVar.size(); js++) { nb_objects++; binary_header_size += 1; binary_header_size += 2; // text size @@ -1727,10 +1519,10 @@ uint8_t *createBinaryHeader(parsedLines *asm_parsed) binary_header = _binary_header; memcpy(binary_header, &nb_objects, 2); binary_header = binary_header + 2; - for (vector::iterator it = asm_parsed->begin(); it != asm_parsed->end(); it++) - { - if ((*it)->op == opCodeType::data_label || (*it)->op == opCodeType::number_label) - { + for (vector::iterator it = asm_parsed->begin(); + it != asm_parsed->end(); it++) { + if ((*it)->op == opCodeType::data_label || + (*it)->op == opCodeType::number_label) { type = 0; // printf("looking at %s %d\n",(*it)->getText(),(*it)->address); memcpy(binary_header, &type, 1); @@ -1740,9 +1532,7 @@ uint8_t *createBinaryHeader(parsedLines *asm_parsed) memcpy(binary_header, &nb_data, 2); binary_header = binary_header + 2; nb_data++; - } - else if ((*it)->op == opCodeType::external_var_label) - { + } else if ((*it)->op == opCodeType::external_var_label) { type = 1; memcpy(binary_header, &type, 1); binary_header = binary_header + 1; @@ -1755,9 +1545,7 @@ uint8_t *createBinaryHeader(parsedLines *asm_parsed) memcpy(binary_header, &nb_data, 2); binary_header = binary_header + 2; nb_data++; - } - else if ((*it)->op == opCodeType::external_call) - { + } else if ((*it)->op == opCodeType::external_call) { type = 2; memcpy(binary_header, &type, 1); binary_header = binary_header + 1; @@ -1774,9 +1562,7 @@ uint8_t *createBinaryHeader(parsedLines *asm_parsed) memcpy(binary_header, &nb_data, 2); binary_header = binary_header + 2; nb_data++; - } - else if ((*it)->op == opCodeType::data) - { + } else if ((*it)->op == opCodeType::data) { type = 3; // printf("looking at %s %d\n",(*it)->getText(),(*it)->address); memcpy(binary_header, &type, 1); @@ -1788,9 +1574,7 @@ uint8_t *createBinaryHeader(parsedLines *asm_parsed) memcpy(binary_header, &(*it)->size, 2); binary_header = binary_header + 2; tmp_data += (*it)->size; - } - else if ((*it)->op == opCodeType::function_declaration) - { + } else if ((*it)->op == opCodeType::function_declaration) { type = 4; memcpy(binary_header, &type, 1); binary_header = binary_header + 1; @@ -1817,10 +1601,11 @@ uint8_t *createBinaryHeader(parsedLines *asm_parsed) uint32_t bc = getInstrAtPos(index)->address; memcpy(binary_header, &bc, 4); binary_header = binary_header + 4; - if ((*it2)->op == opCodeType::variable) - { + if ((*it2)->op == opCodeType::variable) { string stackstring = string((*it)->getText()).insert(2, "stack_"); - // printf(" qss %s %ld %s\r\n", stackstring.c_str(), getInstrAtPos(findLabel(stackstring, asm_parsed))->bincode, (*it2)->getText()); + // printf(" qss %s %ld %s\r\n", stackstring.c_str(), + // getInstrAtPos(findLabel(stackstring, asm_parsed))->bincode, + // (*it2)->getText()); bc = getInstrAtPos(findLabel(stackstring, asm_parsed))->bincode; memcpy(binary_header, &bc, 4); } @@ -1829,8 +1614,7 @@ uint8_t *createBinaryHeader(parsedLines *asm_parsed) } // on ajoute les json // printf("douiung the json\n"); - for (int js = 0; js < jsonVar.size(); js++) - { + for (int js = 0; js < jsonVar.size(); js++) { type = 5; memcpy(binary_header, &type, 1); binary_header = binary_header + 1; @@ -1840,8 +1624,10 @@ uint8_t *createBinaryHeader(parsedLines *asm_parsed) memcpy(binary_header, jsonVar[js].json.c_str(), text_size - 1); binary_header[text_size - 1] = 0; binary_header = binary_header + text_size; - int index = findLabel(string_format("%s%s", "@_", jsonVar[js].variable.c_str()), asm_parsed); - // printf("ibndexw:%d %s\n",index,string_format("%s%s %s","@_",jsonVar[js].variable.c_str(),jsonVar[js].json.c_str()).c_str()); + int index = findLabel( + string_format("%s%s", "@_", jsonVar[js].variable.c_str()), asm_parsed); + // printf("ibndexw:%d %s\n",index,string_format("%s%s + // %s","@_",jsonVar[js].variable.c_str(),jsonVar[js].json.c_str()).c_str()); uint32_t bc = getInstrAtPos(index)->bincode; // printf("%s%s %d\n","@_",jsonVar[js].variable.c_str(),bc); memcpy(binary_header, &bc, 4); @@ -1853,8 +1639,8 @@ uint8_t *createBinaryHeader(parsedLines *asm_parsed) return _binary_header; } -Binary createBinary(Text *_footer, Text *_header, Text *_content, bool display) -{ +Binary createBinary(Text *_footer, Text *_header, Text *_content, + bool display) { Binary bin; bin.binary_data = NULL; bin.function_data = NULL; @@ -1869,14 +1655,12 @@ Binary createBinary(Text *_footer, Text *_header, Text *_content, bool display) _header->clear(); _content->clear(); _footer->clear(); - if (err.error == 0) - { + if (err.error == 0) { updateMem(); error_message_struct error = calculateJump(tmp_exec, &_asm_parsed); - if (error.error == 1) - { + if (error.error == 1) { bin.error = error; _asm_parsed.clear(); @@ -1892,9 +1676,7 @@ Binary createBinary(Text *_footer, Text *_header, Text *_content, bool display) bin.data_size = _data_size; bin.instruction_size = _instr_size; bin.tmp_instruction_size = tmp_instr_size; - } - else - { + } else { bin.error = err; bin.error.error = 1; @@ -1904,8 +1686,7 @@ Binary createBinary(Text *_footer, Text *_header, Text *_content, bool display) return bin; } #ifndef __TEST_DEBUG -void saveBinary(char *name, fs::FS &fs, Binary *bin) -{ +void saveBinary(char *name, fs::FS &fs, Binary *bin) { File root = fs.open(name, FILE_WRITE); root.write((uint8_t *)"ESPLiveScript1.0.1", 19); root.write((uint8_t *)&bin->tmp_instruction_size, 2); diff --git a/src/asm_parser_LMbin.h b/src/asm_parser_LMbin.h index 0534c95..684bd1f 100644 --- a/src/asm_parser_LMbin.h +++ b/src/asm_parser_LMbin.h @@ -1,593 +1,581 @@ #pragma once #ifndef __ASM_LM #define __ASM_LM -#include #include "asm_struct_enum.h" +#include operandeType op_rsr[2] = {operandeType::registers, operandeType::l0_255}; -uint32_t bin_rsr(uint32_t *values) -{ - return 0x0 + ((values[0] << 4) & 0xF0) + ((values[1] << 8) & 0xFF00) +0x030000; +uint32_t bin_rsr(uint32_t *values) { + return 0x0 + ((values[0] << 4) & 0xF0) + ((values[1] << 8) & 0xFF00) + + 0x030000; } -uint32_t bin_wsr(uint32_t *values) -{ - return 0x0 + ((values[0] << 4) & 0xF0) + ((values[1] << 8) & 0xFF00) +0x130000; +uint32_t bin_wsr(uint32_t *values) { + return 0x0 + ((values[0] << 4) & 0xF0) + ((values[1] << 8) & 0xFF00) + + 0x130000; } -uint32_t bin_mov_n(uint32_t *values) -{ - return 0xd + ((values[0] << 4) & 0xF0) + ((values[1] << 8) & 0xF00); +uint32_t bin_mov_n(uint32_t *values) { + return 0xd + ((values[0] << 4) & 0xF0) + ((values[1] << 8) & 0xF00); } -uint32_t bin_sll(uint32_t *values) -{ - return ((values[0] << 12) & 0xF000) + ((values[1] << 8) & 0xF00) + 0xa10000; +uint32_t bin_sll(uint32_t *values) { + return ((values[0] << 12) & 0xF000) + ((values[1] << 8) & 0xF00) + 0xa10000; } -operandeType op_ssl[1]={operandeType::registers}; -uint32_t bin_ssl(uint32_t *values) -{ - return ((values[0] << 8) & 0xF00) +0x401000; +operandeType op_ssl[1] = {operandeType::registers}; +uint32_t bin_ssl(uint32_t *values) { + return ((values[0] << 8) & 0xF00) + 0x401000; } -uint32_t bin_srl(uint32_t *values) -{ - return ((values[0] << 12) & 0xF000) + ((values[1] << 4) & 0xF0) + 0x910000; +uint32_t bin_srl(uint32_t *values) { + return ((values[0] << 12) & 0xF000) + ((values[1] << 4) & 0xF0) + 0x910000; } -uint32_t bin_or(uint32_t *values) -{ - return ((values[0] << 12) & 0xF000) + ((values[1] << 8) & 0xF00) + ((values[2] << 4) & 0xF0) + 0x200000; +uint32_t bin_or(uint32_t *values) { + return ((values[0] << 12) & 0xF000) + ((values[1] << 8) & 0xF00) + + ((values[2] << 4) & 0xF0) + 0x200000; } -uint32_t bin_mov(uint32_t *values) -{ - return ((values[0] << 12) & 0xF000) + ((values[1] << 8) & 0xF00) + ((values[1] << 4) & 0xF0) + 0x200000; +uint32_t bin_mov(uint32_t *values) { + return ((values[0] << 12) & 0xF000) + ((values[1] << 8) & 0xF00) + + ((values[1] << 4) & 0xF0) + 0x200000; } -uint32_t bin_abs(uint32_t *values) -{ - return ((values[0] << 12) & 0xF000) + ((values[1] << 4) & 0xF0) + 0x600100; +uint32_t bin_abs(uint32_t *values) { + return ((values[0] << 12) & 0xF000) + ((values[1] << 4) & 0xF0) + 0x600100; } -uint32_t bin_neg(uint32_t *values) -{ - return ((values[0] << 12) & 0xF000) + ((values[1] << 4) & 0xF0) + 0x600000; +uint32_t bin_neg(uint32_t *values) { + return ((values[0] << 12) & 0xF000) + ((values[1] << 4) & 0xF0) + 0x600000; } -uint32_t bin_l32i(uint32_t *values) -{ - return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + 0x2000 + ((((values[2] / 4) << 16) & 0xFF0000)); +uint32_t bin_l32i(uint32_t *values) { + return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + 0x2000 + ((((values[2] / 4) << 16) & 0xFF0000)); } - -uint32_t bin_l8ui(uint32_t *values) -{ - return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + ((((values[2]) << 16) & 0xFF0000)); +uint32_t bin_l8ui(uint32_t *values) { + return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + ((((values[2]) << 16) & 0xFF0000)); } - -uint32_t bin_s8i(uint32_t *values) -{ - return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + 0x4000 + ((((values[2]) << 16) & 0xFF0000)); +uint32_t bin_s8i(uint32_t *values) { + return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + 0x4000 + ((((values[2]) << 16) & 0xFF0000)); } - -uint32_t bin_l16si(uint32_t *values) -{ - return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + 0x9000 + ((((values[2] / 2) << 16) & 0xFF0000)); +uint32_t bin_l16si(uint32_t *values) { + return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + 0x9000 + ((((values[2] / 2) << 16) & 0xFF0000)); } -uint32_t bin_l16ui(uint32_t *values) -{ - return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + 0x1000 + ((((values[2] / 2) << 16) & 0xFF0000)); +uint32_t bin_l16ui(uint32_t *values) { + return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + 0x1000 + ((((values[2] / 2) << 16) & 0xFF0000)); } - -uint32_t bin_s16i(uint32_t *values) -{ - return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + 0x5000 + ((((values[2] / 2) << 16) & 0xFF0000)); +uint32_t bin_s16i(uint32_t *values) { + return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + 0x5000 + ((((values[2] / 2) << 16) & 0xFF0000)); } - -uint32_t bin_l32i_n(uint32_t *values) -{ - return 0x8 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + ((((values[2] / 4) << 12) & 0xF000)); +uint32_t bin_l32i_n(uint32_t *values) { + return 0x8 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + ((((values[2] / 4) << 12) & 0xF000)); } - -uint32_t bin_add(uint32_t *values) -{ - return (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + (((values[0] << 12) & 0xF000)) + 0x800000; +uint32_t bin_add(uint32_t *values) { + return (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + (((values[0] << 12) & 0xF000)) + 0x800000; } -uint32_t bin_salt(uint32_t *values) -{ - return (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + (((values[0] << 12) & 0xF000)) + 0x720000; +uint32_t bin_salt(uint32_t *values) { + return (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + (((values[0] << 12) & 0xF000)) + 0x720000; } - - -uint32_t bin_quou(uint32_t *values) -{ - return (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + (((values[0] << 12) & 0xF000)) + 0xC20000; +uint32_t bin_quou(uint32_t *values) { + return (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + (((values[0] << 12) & 0xF000)) + 0xC20000; } -uint32_t bin_quos(uint32_t *values) -{ - return (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + (((values[0] << 12) & 0xF000)) + 0xD20000; +uint32_t bin_quos(uint32_t *values) { + return (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + (((values[0] << 12) & 0xF000)) + 0xD20000; } -uint32_t bin_sub(uint32_t *values) -{ - return (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + (((values[0] << 12) & 0xF000)) + 0xc00000; +uint32_t bin_sub(uint32_t *values) { + return (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + (((values[0] << 12) & 0xF000)) + 0xc00000; } - -uint32_t bin_mull(uint32_t *values) -{ - return (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + (((values[0] << 12) & 0xF000)) + 0x820000; +uint32_t bin_mull(uint32_t *values) { + return (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + (((values[0] << 12) & 0xF000)) + 0x820000; } -uint32_t bin_add_n(uint32_t *values) -{ - return 10 + (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + (((values[0] << 12) & 0xF000)); +uint32_t bin_add_n(uint32_t *values) { + return 10 + (((values[2] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + (((values[0] << 12) & 0xF000)); } -operandeType op_addi[3] = {operandeType::registers, operandeType::registers, operandeType::lm128_127}; -uint32_t bin_addi(uint32_t *values) -{ - return 2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + 0xc000 + (((values[2] << 16) & 0xFF0000)); +operandeType op_addi[3] = {operandeType::registers, operandeType::registers, + operandeType::lm128_127}; +uint32_t bin_addi(uint32_t *values) { + return 2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + 0xc000 + (((values[2] << 16) & 0xFF0000)); } -operandeType op_addi_n[3] = {operandeType::registers, operandeType::registers, operandeType::lm1_15}; +operandeType op_addi_n[3] = {operandeType::registers, operandeType::registers, + operandeType::lm1_15}; ; -uint32_t bin_addi_n(uint32_t *values) -{ - int v = values[2]; - if (values[2] == -1) - v = 0; - return 0xb + (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + ((v << 4) & 0xF0); +uint32_t bin_addi_n(uint32_t *values) { + int v = values[2]; + if (values[2] == -1) + v = 0; + return 0xb + (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + + ((v << 4) & 0xF0); } - -uint32_t bin_s32i(uint32_t *values) -{ - return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + 0x6000 + ((((values[2] / 4) << 16) & 0xFF0000)); +uint32_t bin_s32i(uint32_t *values) { + return 0x2 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + 0x6000 + ((((values[2] / 4) << 16) & 0xFF0000)); } - - -uint32_t bin_s32i_n(uint32_t *values) -{ - return 0x9 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + ((((values[2] / 4) << 12) & 0xF000)); +uint32_t bin_s32i_n(uint32_t *values) { + return 0x9 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + ((((values[2] / 4) << 12) & 0xF000)); } operandeType op_bnez[3] = {operandeType::registers, operandeType::label}; ; -uint32_t bin_bnez(uint32_t *values) -{ +uint32_t bin_bnez(uint32_t *values) { - return 0x56 + (((values[0] << 8) & 0xF00)); - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + return 0x56 + (((values[0] << 8) & 0xF00)); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -uint32_t bin_beqz(uint32_t *values) -{ +uint32_t bin_beqz(uint32_t *values) { - return 0x16 + (((values[0] << 8) & 0xF00)); - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + return 0x16 + (((values[0] << 8) & 0xF00)); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -uint32_t jump_bnez(uint32_t value, uint32_t current_address, uint32_t destination_address) -{ - uint32_t dif = destination_address - current_address - 4; - return value + ((dif << 12) & 0xFFF000); +uint32_t jump_bnez(uint32_t value, uint32_t current_address, + uint32_t destination_address) { + uint32_t dif = destination_address - current_address - 4; + return value + ((dif << 12) & 0xFFF000); } -operandeType op_blti[3] = {operandeType::registers, operandeType::l0_15, operandeType::label}; -uint32_t bin_blti(uint32_t *values) -{ +operandeType op_blti[3] = {operandeType::registers, operandeType::l0_15, + operandeType::label}; +uint32_t bin_blti(uint32_t *values) { - return 0xA6 + (((values[1] << 12) & 0xF000)) + (((values[0] << 8) & 0xF00)) ; + return 0xA6 + (((values[1] << 12) & 0xF000)) + (((values[0] << 8) & 0xF00)); - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -uint32_t bin_bgei(uint32_t *values) -{ +uint32_t bin_bgei(uint32_t *values) { - return 0xE6 + (((values[1] << 12) & 0xF000)) + (((values[0] << 8) & 0xF00)) ; + return 0xE6 + (((values[1] << 12) & 0xF000)) + (((values[0] << 8) & 0xF00)); - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -uint32_t bin_bnei(uint32_t *values) -{ +uint32_t bin_bnei(uint32_t *values) { - return 0x66 + (((values[1] << 12) & 0xF000)) + (((values[0] << 8) & 0xF00)) ; + return 0x66 + (((values[1] << 12) & 0xF000)) + (((values[0] << 8) & 0xF00)); - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -uint32_t bin_beqi(uint32_t *values) -{ +uint32_t bin_beqi(uint32_t *values) { - return 0x26 + (((values[1] << 12) & 0xF000)) + (((values[0] << 8) & 0xF00)) ; + return 0x26 + (((values[1] << 12) & 0xF000)) + (((values[0] << 8) & 0xF00)); - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -operandeType op_blt[3] = {operandeType::registers, operandeType::registers, operandeType::label}; -uint32_t bin_blt(uint32_t *values) -{ +operandeType op_blt[3] = {operandeType::registers, operandeType::registers, + operandeType::label}; +uint32_t bin_blt(uint32_t *values) { - return 0x7 + (((values[1] << 4) & 0xF0)) + (((values[0] << 8) & 0xF00)) + 0x2000; + return 0x7 + (((values[1] << 4) & 0xF0)) + (((values[0] << 8) & 0xF00)) + + 0x2000; - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -uint32_t bin_bltu(uint32_t *values) -{ +uint32_t bin_bltu(uint32_t *values) { - return 0x7 + (((values[1] << 4) & 0xF0)) + (((values[0] << 8) & 0xF00)) + 0x3000; + return 0x7 + (((values[1] << 4) & 0xF0)) + (((values[0] << 8) & 0xF00)) + + 0x3000; - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -uint32_t jump_blt(uint32_t value, uint32_t current_address, uint32_t destination_address) -{ - uint32_t dif = destination_address - current_address - 4; +uint32_t jump_blt(uint32_t value, uint32_t current_address, + uint32_t destination_address) { + uint32_t dif = destination_address - current_address - 4; - return value + ((dif << 16) & 0xFF0000); + return value + ((dif << 16) & 0xFF0000); } -operandeType op_bge[3] = {operandeType::registers, operandeType::registers, operandeType::label}; -uint32_t bin_bge(uint32_t *values) -{ +operandeType op_bge[3] = {operandeType::registers, operandeType::registers, + operandeType::label}; +uint32_t bin_bge(uint32_t *values) { - return 0x7 + (((values[1] << 4) & 0xF0)) + (((values[0] << 8) & 0xF00)) + 0xa000; + return 0x7 + (((values[1] << 4) & 0xF0)) + (((values[0] << 8) & 0xF00)) + + 0xa000; - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -uint32_t bin_bgeu(uint32_t *values) -{ +uint32_t bin_bgeu(uint32_t *values) { - return 0x7 + (((values[1] << 4) & 0xF0)) + (((values[0] << 8) & 0xF00)) + 0xb000; + return 0x7 + (((values[1] << 4) & 0xF0)) + (((values[0] << 8) & 0xF00)) + + 0xb000; - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -uint32_t jump_bge(uint32_t value, uint32_t current_address, uint32_t destination_address) -{ - uint32_t dif = destination_address - current_address - 4; +uint32_t jump_bge(uint32_t value, uint32_t current_address, + uint32_t destination_address) { + uint32_t dif = destination_address - current_address - 4; - return value + ((dif << 16) & 0xFF0000); + return value + ((dif << 16) & 0xFF0000); } -operandeType *op_bne= op_bge; // [3] = {operandeType::registers, operandeType::registers, operandeType::label}; -uint32_t bin_bne(uint32_t *values) -{ +operandeType *op_bne = op_bge; // [3] = {operandeType::registers, + // operandeType::registers, operandeType::label}; +uint32_t bin_bne(uint32_t *values) { - return 0x7 + (((values[1] << 4) & 0xF0)) + (((values[0] << 8) & 0xF00)) + 0x9000; + return 0x7 + (((values[1] << 4) & 0xF0)) + (((values[0] << 8) & 0xF00)) + + 0x9000; - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -uint32_t jump_bne(uint32_t value, uint32_t current_address, uint32_t destination_address) -{ - uint32_t dif = destination_address - current_address - 4; +uint32_t jump_bne(uint32_t value, uint32_t current_address, + uint32_t destination_address) { + uint32_t dif = destination_address - current_address - 4; - return value + ((dif << 16) & 0xFF0000); + return value + ((dif << 16) & 0xFF0000); } -operandeType *op_beq=op_bge; //[3] = {operandeType::registers, operandeType::registers, operandeType::label}; -uint32_t bin_beq(uint32_t *values) -{ +operandeType *op_beq = op_bge; //[3] = {operandeType::registers, + //operandeType::registers, operandeType::label}; +uint32_t bin_beq(uint32_t *values) { - return 0x7 + (((values[1] << 4) & 0xF0)) + (((values[0] << 8) & 0xF00)) + 0x1000; + return 0x7 + (((values[1] << 4) & 0xF0)) + (((values[0] << 8) & 0xF00)) + + 0x1000; - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -uint32_t jump_beq(uint32_t value, uint32_t current_address, uint32_t destination_address) -{ - uint32_t dif = destination_address - current_address - 4; +uint32_t jump_beq(uint32_t value, uint32_t current_address, + uint32_t destination_address) { + uint32_t dif = destination_address - current_address - 4; - return value + ((dif << 16) & 0xFF0000); + return value + ((dif << 16) & 0xFF0000); } operandeType op_j[3] = {operandeType::label}; -uint32_t bin_j(uint32_t *values) -{ +uint32_t bin_j(uint32_t *values) { - return 0x6; + return 0x6; - // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & 0x100000 )); + // (( ((32-values[2]) <<4) & 0xF0 )) + (( (values[1] <<8) & 0xF00 )) + (( + // (values[0]) <<12) & 0x0F000) + 0x10000 + (( ((32-values[2]) <<16) & + // 0x100000 )); } -uint32_t jump_j(uint32_t value, uint32_t current_address, uint32_t destination_address) -{ - uint32_t dif = destination_address - current_address - 4; +uint32_t jump_j(uint32_t value, uint32_t current_address, + uint32_t destination_address) { + uint32_t dif = destination_address - current_address - 4; - return value + ((dif << 6) & 0xFFFFC0); + return value + ((dif << 6) & 0xFFFFC0); } -operandeType op_slli[3] = {operandeType::registers, operandeType::registers, operandeType::l1_31}; +operandeType op_slli[3] = {operandeType::registers, operandeType::registers, + operandeType::l1_31}; ; -uint32_t bin_slli(uint32_t *values) -{ - return ((((32 - values[2]) << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + (((values[0]) << 12) & 0x0F000) + 0x10000 + ((((32 - values[2]) << 16) & 0x100000)); +uint32_t bin_slli(uint32_t *values) { + return ((((32 - values[2]) << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + (((values[0]) << 12) & 0x0F000) + 0x10000 + + ((((32 - values[2]) << 16) & 0x100000)); } operandeType op_movi[3] = {operandeType::registers, operandeType::lm2048_2047}; -uint32_t bin_movi(uint32_t *values) -{ - return 2 + (((values[0] << 4) & 0xF0)) + ((values[1]) & 0xF00) + (0xa << 12) + ((values[1] << 16) & 0xFF0000); +uint32_t bin_movi(uint32_t *values) { + return 2 + (((values[0] << 4) & 0xF0)) + ((values[1]) & 0xF00) + (0xa << 12) + + ((values[1] << 16) & 0xFF0000); - // return 0xc + (( (values[0] <<8) & 0xF00 )) + ((values[1]<<12) &0xF000 ) + ((values[1]) & 0x70 ) ; + // return 0xc + (( (values[0] <<8) & 0xF00 )) + ((values[1]<<12) &0xF000 ) + // + ((values[1]) & 0x70 ) ; } operandeType op_movi_n[2] = {operandeType::registers, operandeType::lm32_95}; -uint32_t bin_movi_n(uint32_t *values) -{ - return 0xc + (((values[0] << 8) & 0xF00)) + ((values[1] << 12) & 0xF000) + ((values[1]) & 0x70); +uint32_t bin_movi_n(uint32_t *values) { + return 0xc + (((values[0] << 8) & 0xF00)) + ((values[1] << 12) & 0xF000) + + ((values[1]) & 0x70); } -operandeType *op_and=op_add; // [3] = {operandeType::registers, operandeType::registers, operandeType::registers}; -uint32_t bin_and(uint32_t *values) -{ - return (((values[2] << 4) & 0xF0)) + ((values[1] << 8) & 0xF00) + ((values[0] << 12) & 0x0F000) + 0x100000; +operandeType *op_and = + op_add; // [3] = {operandeType::registers, operandeType::registers, + // operandeType::registers}; +uint32_t bin_and(uint32_t *values) { + return (((values[2] << 4) & 0xF0)) + ((values[1] << 8) & 0xF00) + + ((values[0] << 12) & 0x0F000) + 0x100000; } -operandeType *op_remu=op_add; //[3] = {operandeType::registers, operandeType::registers, operandeType::registers}; -uint32_t bin_remu(uint32_t *values) -{ - return (((values[2] << 4) & 0xF0)) + ((values[1] << 8) & 0xF00) + ((values[0] << 12) & 0x0F000) + 0xe20000; +operandeType *op_remu = + op_add; //[3] = {operandeType::registers, operandeType::registers, + //operandeType::registers}; +uint32_t bin_remu(uint32_t *values) { + return (((values[2] << 4) & 0xF0)) + ((values[1] << 8) & 0xF00) + + ((values[0] << 12) & 0x0F000) + 0xe20000; } operandeType op_entry[2] = {operandeType::registers, operandeType::l0_32760}; -uint32_t bin_entry(uint32_t *values) -{ - return 0x36 + (((values[0] << 8) & 0xF00)) + (((values[1] / 8) << 12) & 0xFF000); +uint32_t bin_entry(uint32_t *values) { + return 0x36 + (((values[0] << 8) & 0xF00)) + + (((values[1] / 8) << 12) & 0xFF000); } operandeType op_word[2] = {operandeType::l0_FFFFFFFF}; -uint32_t bin_word(uint32_t *values) -{ - return values[0]; -} +uint32_t bin_word(uint32_t *values) { return values[0]; } -operandeType op_extui[4] = {operandeType::registers, operandeType::registers, operandeType::l0_31, operandeType::l0_15}; -uint32_t bin_extui(uint32_t *values) -{ - return (values[1] << 4) + (values[0] << 12) + (2 << 17) + ((values[3] - 1) << 20) + ((values[2] & 0xF) << 8) + ((values[2] & 0x10) << 12); +operandeType op_extui[4] = {operandeType::registers, operandeType::registers, + operandeType::l0_31, operandeType::l0_15}; +uint32_t bin_extui(uint32_t *values) { + return (values[1] << 4) + (values[0] << 12) + (2 << 17) + + ((values[3] - 1) << 20) + ((values[2] & 0xF) << 8) + + ((values[2] & 0x10) << 12); } -uint32_t bin_nop_n(uint32_t *values) -{ - return 0xf03d; - ; -} -uint32_t bin_nop(uint32_t *values) -{ - return 0x0020f0; +uint32_t bin_nop_n(uint32_t *values) { + return 0xf03d; + ; } +uint32_t bin_nop(uint32_t *values) { return 0x0020f0; } operandeType op_l32r[2] = {operandeType::registers, operandeType::label}; -uint32_t bin_l32r(uint32_t *values) -{ - return 1 + ((values[0] << 4) & 0xF0); -} +uint32_t bin_l32r(uint32_t *values) { return 1 + ((values[0] << 4) & 0xF0); } /******float********/ - -uint32_t bin_lsi(uint32_t *values) -{ - return 0x3 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + 0x0000 + ((((values[2] / 4) << 16) & 0xFF0000)); +uint32_t bin_lsi(uint32_t *values) { + return 0x3 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + 0x0000 + ((((values[2] / 4) << 16) & 0xFF0000)); } - -uint32_t bin_ssi(uint32_t *values) -{ - return 0x3 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + 0x4000 + ((((values[2] / 4) << 16) & 0xFF0000)); +uint32_t bin_ssi(uint32_t *values) { + return 0x3 + (((values[0] << 4) & 0xF0)) + (((values[1] << 8) & 0xF00)) + + 0x4000 + ((((values[2] / 4) << 16) & 0xFF0000)); } -uint32_t bin_rfr(uint32_t *values) -{ - return 0x0 + 0x40+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + 0xa0000+0xf00000; +uint32_t bin_rfr(uint32_t *values) { + return 0x0 + 0x40 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)) + 0xa0000 + 0xf00000; } -uint32_t bin_wfr(uint32_t *values) -{ - return 0x0 + 0x50+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + 0xa0000+0xf00000; +uint32_t bin_wfr(uint32_t *values) { + return 0x0 + 0x50 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)) + 0xa0000 + 0xf00000; } -uint32_t bin_adds(uint32_t *values) -{ - return 0x0 + 0xA0000+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + + (((values[2] << 4) & 0xF0)) ; +uint32_t bin_adds(uint32_t *values) { + return 0x0 + 0xA0000 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)) + +(((values[2] << 4) & 0xF0)); } -uint32_t bin_subs(uint32_t *values) -{ - return 0x0 + 0x1A0000+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + + (((values[2] << 4) & 0xF0)) ; +uint32_t bin_subs(uint32_t *values) { + return 0x0 + 0x1A0000 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)) + +(((values[2] << 4) & 0xF0)); } -operandeType op_floats[3] = {operandeType::floatregisters, operandeType::registers, operandeType::l0_15}; -uint32_t bin_floats(uint32_t *values) -{ - return 0x0 + 0xCA0000+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + + (((values[2] << 4) & 0xF0)) ; +operandeType op_floats[3] = {operandeType::floatregisters, + operandeType::registers, operandeType::l0_15}; +uint32_t bin_floats(uint32_t *values) { + return 0x0 + 0xCA0000 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)) + +(((values[2] << 4) & 0xF0)); } -operandeType op_floors[3] = {operandeType::registers, operandeType::floatregisters, operandeType::l0_15}; -uint32_t bin_floors(uint32_t *values) -{ - return 0x0 + 0xAA0000+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + (((values[2] << 4) & 0xF0)) ; +operandeType op_floors[3] = {operandeType::registers, + operandeType::floatregisters, operandeType::l0_15}; +uint32_t bin_floors(uint32_t *values) { + return 0x0 + 0xAA0000 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)) + (((values[2] << 4) & 0xF0)); } -uint32_t bin_rounds(uint32_t *values) -{ - return 0x0 + 0x8A0000+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + (((values[2] << 4) & 0xF0)) ; +uint32_t bin_rounds(uint32_t *values) { + return 0x0 + 0x8A0000 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)) + (((values[2] << 4) & 0xF0)); } -operandeType *op_truncs=op_floors; //[3] = {operandeType::registers, operandeType::floatregisters, operandeType::l0_15}; -uint32_t bin_truncs(uint32_t *values) -{ - return 0x0 + 0x9A0000+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + + (((values[2] << 4) & 0xF0)) ; +operandeType *op_truncs = + op_floors; //[3] = {operandeType::registers, operandeType::floatregisters, + //operandeType::l0_15}; +uint32_t bin_truncs(uint32_t *values) { + return 0x0 + 0x9A0000 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)) + +(((values[2] << 4) & 0xF0)); } -//operandeType op_floors[3] = {operandeType::registers, operandeType::floatregisters, operandeType::l0_15}; -uint32_t bin_muls(uint32_t *values) -{ - return 0x0 + 0x2A0000+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + + (((values[2] << 4) & 0xF0)) ; +// operandeType op_floors[3] = {operandeType::registers, +// operandeType::floatregisters, operandeType::l0_15}; +uint32_t bin_muls(uint32_t *values) { + return 0x0 + 0x2A0000 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)) + +(((values[2] << 4) & 0xF0)); } -operandeType op_div0s[2] = {operandeType::floatregisters, operandeType::floatregisters}; -uint32_t bin_div0s(uint32_t *values) -{ - return (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) +0x70+0xfa0000 ; +operandeType op_div0s[2] = {operandeType::floatregisters, + operandeType::floatregisters}; +uint32_t bin_div0s(uint32_t *values) { + return (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + 0x70 + + 0xfa0000; } -operandeType *op_divns =op_adds; //[3] = {operandeType::floatregisters, operandeType::floatregisters, operandeType::floatregisters}; -uint32_t bin_divns(uint32_t *values) -{ - return 0x0 + 0x7A0000+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + + (((values[2] << 4) & 0xF0)) ; +operandeType *op_divns = + op_adds; //[3] = {operandeType::floatregisters, + //operandeType::floatregisters, operandeType::floatregisters}; +uint32_t bin_divns(uint32_t *values) { + return 0x0 + 0x7A0000 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)) + +(((values[2] << 4) & 0xF0)); } -operandeType *op_maddns=op_divns; -uint32_t bin_maddns(uint32_t *values) -{ - return 0x0 + 0x6A0000+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + + (((values[2] << 4) & 0xF0)) ; +operandeType *op_maddns = op_divns; +uint32_t bin_maddns(uint32_t *values) { + return 0x0 + 0x6A0000 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)) + +(((values[2] << 4) & 0xF0)); } -uint32_t bin_nexp01s(uint32_t *values) -{ - return (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) +0xB0+0xfa0000 ; +uint32_t bin_nexp01s(uint32_t *values) { + return (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + 0xB0 + + 0xfa0000; } - //[2] = {operandeType::floatregisters, operandeType::floatregisters}; -uint32_t bin_negs(uint32_t *values) -{ - return (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) +0x60+0xfa0000 ; +//[2] = {operandeType::floatregisters, operandeType::floatregisters}; +uint32_t bin_negs(uint32_t *values) { + return (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + 0x60 + + 0xfa0000; } -operandeType *op_addexpms=op_negs; -uint32_t bin_addexpms(uint32_t *values) -{ - return (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) +0xF0+0xfa0000 ; +operandeType *op_addexpms = op_negs; +uint32_t bin_addexpms(uint32_t *values) { + return (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + 0xF0 + + 0xfa0000; } -operandeType *op_addexps=op_negs; -uint32_t bin_addexps(uint32_t *values) -{ - return (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) +0xE0+0xfa0000 ; +operandeType *op_addexps = op_negs; +uint32_t bin_addexps(uint32_t *values) { + return (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + 0xE0 + + 0xfa0000; } -operandeType *op_mkdadjs=op_negs; -uint32_t bin_mkdadjs(uint32_t *values) -{ - return (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) +0xD0+0xfa0000 ; +operandeType *op_mkdadjs = op_negs; +uint32_t bin_mkdadjs(uint32_t *values) { + return (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) + 0xD0 + + 0xfa0000; } operandeType op_consts[2] = {operandeType::floatregisters, operandeType::l0_15}; -uint32_t bin_consts(uint32_t *values) -{ - return 0x30 + 0xFA0000+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) ; +uint32_t bin_consts(uint32_t *values) { + return 0x30 + 0xFA0000 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)); } -operandeType *op_movs=op_nexp01s; //[2] = {operandeType::floatregisters, operandeType::floatregisters}; -uint32_t bin_movs(uint32_t *values) -{ - return 0x00 + 0xFA0000+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) ; +operandeType *op_movs = op_nexp01s; //[2] = {operandeType::floatregisters, + //operandeType::floatregisters}; +uint32_t bin_movs(uint32_t *values) { + return 0x00 + 0xFA0000 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)); } -uint32_t bin_abss(uint32_t *values) -{ - return 0xFA0010+ (((values[0] << 12) & 0xF000)) + (((values[1] << 8) & 0xF00)) ; +uint32_t bin_abss(uint32_t *values) { + return 0xFA0010 + (((values[0] << 12) & 0xF000)) + + (((values[1] << 8) & 0xF00)); } - -operandeType op_floatco[3]={operandeType::boolregisters,operandeType::floatregisters,operandeType::floatregisters}; -uint32_t bin_olts(uint32_t *values) -{ - return 0x00 + (((values[2] << 4) & 0x00F0)) +(((values[1] << 8) & 0x00F00))+(((values[0] << 12) & 0x00F000))+0x4B0000 ; +operandeType op_floatco[3] = {operandeType::boolregisters, + operandeType::floatregisters, + operandeType::floatregisters}; +uint32_t bin_olts(uint32_t *values) { + return 0x00 + (((values[2] << 4) & 0x00F0)) + (((values[1] << 8) & 0x00F00)) + + (((values[0] << 12) & 0x00F000)) + 0x4B0000; } -uint32_t bin_oles(uint32_t *values) -{ - return 0x00 + (((values[2] << 4) & 0x00F0)) +(((values[1] << 8) & 0x00F00))+(((values[0] << 12) & 0x00F000))+0x6B0000 ; +uint32_t bin_oles(uint32_t *values) { + return 0x00 + (((values[2] << 4) & 0x00F0)) + (((values[1] << 8) & 0x00F00)) + + (((values[0] << 12) & 0x00F000)) + 0x6B0000; } -uint32_t bin_oeqs(uint32_t *values) -{ - return 0x00 + (((values[2] << 4) & 0x00F0)) +(((values[1] << 8) & 0x00F00))+(((values[0] << 12) & 0x00F000))+0x2B0000 ; +uint32_t bin_oeqs(uint32_t *values) { + return 0x00 + (((values[2] << 4) & 0x00F0)) + (((values[1] << 8) & 0x00F00)) + + (((values[0] << 12) & 0x00F000)) + 0x2B0000; } -operandeType op_jumpfloat[2]={operandeType::boolregisters,operandeType::label}; -uint32_t bin_bt(uint32_t *values) -{ - return 0x1076+(((values[0] << 8) & 0x00F00)); +operandeType op_jumpfloat[2] = {operandeType::boolregisters, + operandeType::label}; +uint32_t bin_bt(uint32_t *values) { + return 0x1076 + (((values[0] << 8) & 0x00F00)); } -uint32_t bin_bf(uint32_t *values) -{ - return 0x0076+(((values[0] << 8) & 0x00F00)); +uint32_t bin_bf(uint32_t *values) { + return 0x0076 + (((values[0] << 8) & 0x00F00)); } operandeType op_call8[2] = {operandeType::label}; -uint32_t bin_call8(uint32_t *values) -{ - return 0x25; -} -uint32_t bin_call12(uint32_t *values) -{ - return 0x35; -} -uint32_t jump_call8(uint32_t value, uint32_t current_address, uint32_t destination_address) -{ - if ((((destination_address) / 4) * 4) != destination_address) - { - printf("impossible de calculer le jump\n"); - return 0; - } - else +uint32_t bin_call8(uint32_t *values) { return 0x25; } +uint32_t bin_call12(uint32_t *values) { return 0x35; } +uint32_t jump_call8(uint32_t value, uint32_t current_address, + uint32_t destination_address) { + if ((((destination_address) / 4) * 4) != destination_address) { + printf("impossible de calculer le jump\n"); + return 0; + } else { + uint32_t dif = + ((destination_address - (current_address & 0xFFFFFFFC)) - 4) & + 0xFFFFFFC; + /* + uint32_t recalculate=(((current_address & 0xFFFF)+dif*4)+3)& 0xFFFc + ; if(recalculate!=(destination_address & 0xFFFF)) { - uint32_t dif = ((destination_address - (current_address & 0xFFFFFFFC)) - 4) & 0xFFFFFFC; - /* - uint32_t recalculate=(((current_address & 0xFFFF)+dif*4)+3)& 0xFFFc ; - if(recalculate!=(destination_address & 0xFFFF)) - { - - recalculate=((current_address & 0xFFFF)+3+4)& 0xFFFFFc ; - if(recalculate==(destination_address & 0xFFFF)) - { - dif++; - } - else - { - printf("not it\n"); - } - }ffd - */ - return value + ((dif << 4) & 0xFFFFC0); - } -} -uint32_t jump_l32r(uint32_t value, uint32_t current_address, uint32_t destination_address) -{ - if ((((destination_address) / 4) * 4) != destination_address) - { - printf("impossible de calculer le jump\n"); - return 0; - } - else - { - uint32_t dif = ((destination_address - current_address) / 4) & 0xFFFFFFFF; - uint32_t recalculate = (((current_address & 0xFFFFFFFF) + dif * 4) + 3) & 0xFFFFFFFc; - if (recalculate != (destination_address & 0xFFFFFFFF)) - { - - recalculate = ((current_address & 0xFFFFFFFF) + 3 + 4) & 0xFFFFFFFc; - if (recalculate == (destination_address & 0xFFFFFFFF)) + recalculate=((current_address & 0xFFFF)+3+4)& 0xFFFFFc ; + if(recalculate==(destination_address & 0xFFFF)) { - printf("calcultate here\n"); - dif++; + dif++; } else { printf("not it\n"); } - } - return value + ((dif << 8) & 0xFFFF00); + }ffd + */ + return value + ((dif << 4) & 0xFFFFC0); + } +} + +uint32_t jump_l32r(uint32_t value, uint32_t current_address, + uint32_t destination_address) { + if ((((destination_address) / 4) * 4) != destination_address) { + printf("impossible de calculer le jump\n"); + return 0; + } else { + uint32_t dif = ((destination_address - current_address) / 4) & 0xFFFFFFFF; + uint32_t recalculate = + (((current_address & 0xFFFFFFFF) + dif * 4) + 3) & 0xFFFFFFFc; + if (recalculate != (destination_address & 0xFFFFFFFF)) { + + recalculate = ((current_address & 0xFFFFFFFF) + 3 + 4) & 0xFFFFFFFc; + if (recalculate == (destination_address & 0xFFFFFFFF)) { + printf("calcultate here\n"); + dif++; + } else { + printf("not it\n"); + } } + return value + ((dif << 8) & 0xFFFF00); + } } operandeType op_callx8[1] = {operandeType::registers}; -uint32_t bin_callx8(uint32_t *values) -{ - return 0xe0 + ((values[0] << 8) & 0xF00); +uint32_t bin_callx8(uint32_t *values) { + return 0xe0 + ((values[0] << 8) & 0xF00); } -uint32_t bin_retw_n(uint32_t *values) -{ - return 0xf01d; -} +uint32_t bin_retw_n(uint32_t *values) { return 0xf01d; } operandeType op_global[1] = {operandeType::label}; -operandeType op_callExt[2] = { operandeType::registers,operandeType::label}; +operandeType op_callExt[2] = {operandeType::registers, operandeType::label}; operandeType op_movExt[2] = {operandeType::registers, operandeType::label}; -uint32_t bin_movExt(uint32_t *values) -{ - - return values[0]; -} - - - - - +uint32_t bin_movExt(uint32_t *values) { return values[0]; } #endif \ No newline at end of file diff --git a/src/asm_struct_enum.h b/src/asm_struct_enum.h index b3305a3..8afae66 100644 --- a/src/asm_struct_enum.h +++ b/src/asm_struct_enum.h @@ -2,472 +2,373 @@ #ifndef _ASM_STRUCT #define _ASM_STRUCT +#include "functionlib.h" +#include "string_function.h" #include -#include #include -#include "string_function.h" -#include "functionlib.h" +#include using namespace std; #define EOF_TEXTARRAY 9999 #define EOF_VARTYPE 255 -int __exe_size=0; +int __exe_size = 0; -template -class Stack -{ +template class Stack { public: - Stack() {} - Stack(T def) - { - _default = def; - } - void push(T a) - { - _stack.push_back(a); - } - T pop() - { - if (_stack.size() < 1) - return _default; - T sav = _stack.back(); - _stack.pop_back(); - return sav; - } - T get() - { + Stack() {} + Stack(T def) { _default = def; } + void push(T a) { _stack.push_back(a); } + T pop() { + if (_stack.size() < 1) + return _default; + T sav = _stack.back(); + _stack.pop_back(); + return sav; + } + T get() { - if (_stack.size() < 1) - return _default; - return _stack.back(); - } - void duplicate() - { - if (_stack.size() > 0) - _stack.push_back(_stack.back()); - else - { - _stack.push_back(_default); - _stack.push_back(_default); - } - } - T front() - { - if (_stack.size() > 0) - return _stack.front(); - else - return _default; + if (_stack.size() < 1) + return _default; + return _stack.back(); + } + void duplicate() { + if (_stack.size() > 0) + _stack.push_back(_stack.back()); + else { + _stack.push_back(_default); + _stack.push_back(_default); } - void swap() - { + } + T front() { + if (_stack.size() > 0) + return _stack.front(); + else + return _default; + } + void swap() { - T sav = pop(); - T sav2 = pop(); - push(sav); - push(sav2); - } - void set(T k) - { - pop(); - push(k); - } - void increase() - { - // if( typeid(T).hash_code()==typeid(int).hash_code()) - // { - int sav = (int)pop(); - push(sav + 1); - // } - } - void decrease() - { - // if( typeid(T).hash_code()==typeid(int).hash_code()) - // { - - int sav = (int)pop(); - push(sav - 1); - // } - } - void clear() - { - _stack.clear(); - _stack.shrink_to_fit(); - } - vector _stack; - T _default; + T sav = pop(); + T sav2 = pop(); + push(sav); + push(sav2); + } + void set(T k) { + pop(); + push(k); + } + void increase() { + // if( typeid(T).hash_code()==typeid(int).hash_code()) + // { + int sav = (int)pop(); + push(sav + 1); + // } + } + void decrease() { + // if( typeid(T).hash_code()==typeid(int).hash_code()) + // { + + int sav = (int)pop(); + push(sav - 1); + // } + } + void clear() { + _stack.clear(); + _stack.shrink_to_fit(); + } + vector _stack; + T _default; }; char *m; -class Text -{ +class Text { public: - Text() - { - //_texts=t; - _texts.clear(); - _texts.shrink_to_fit(); - position = 0; - // _texts.push_back(cc); - _it = _texts.begin(); - } - int findText(char * str) - { - #ifdef __SPEED - return -1; - #endif - // for (int i = 0; i < _texts.size(); i++) - for (int i = _texts.size()-1; i >=0; i--) - { - if (strcmp(str,_texts[i]) == 0) - { - return i; - } - } - return -1; - } - int addText(string str,uint16_t si) - { - m = (char *)malloc(si + 1); - memcpy(m, str.c_str(), si); - m[str.size()] = 0; - _texts.push_back(m); - position++; - str.clear(); - return _texts.size() - 1; - } - int addText(string str) - { - int pos = findText((char *)str.c_str()); - if (pos > -1) - { - #ifdef __TEST_DEBUG - // printf(" find text:%d %s\r\n",pos,str.c_str()); - #endif - - return pos; - } - m = (char *)malloc(str.size() + 1); - memcpy(m, str.c_str(), str.size()); - m[str.size()] = 0; - _texts.push_back(m); - position++; - str.clear(); - #ifdef __TEST_DEBUG - // printf(" addtext:%d %s\r\n",_texts.size(),str.c_str()); - #endif - return _texts.size() - 1; - } - void addAfter(int pos, string s) - { - _it = getChildAtPos(pos); - // printf(" on recupere %d:%s\n",pos,(*__it).c_str()); - // if((*_it).compare(s)!=0) - //{ - addAfter(s); - position--; - _it = getChildAtPos(position); - position++; - // } - } - void addBefore(int pos, string s) - { - _it = getChildAtPos(pos-1); - // printf(" on recupere %d:%s\n",pos,(*__it).c_str()); - // if((*_it).compare(s)!=0) - //{ - addBefore(s); - position--; - _it = getChildAtPos(position); - position++; - // } - } - - void addAfter(string str) - { - int pos = findText((char *)str.c_str()); - // char *tmp; - if (pos > -1) - { - m = _texts[pos]; - } - else - { - m = (char *)malloc(str.size() + 1); - memcpy(m, str.c_str(), str.size()); - m[str.size()] = 0; - } - if (_it == _texts.end()) - { - _texts.push_back(m); - _it = _texts.end(); - _it--; - } - else - { - _it = _texts.insert(next(_it), m); - } - position++; - } - string back() - { - if (_texts.size() > 0) - return string(_texts.back()); - else - return ""; - } - string current() - { - return string(*_it); - } - void blankCurrent() - { - int pos = findText(" "); - if(pos>-1) - { - *_it=_texts[pos]; - } - else - { - string str=" "; - m = (char *)malloc(str.size() + 1); - memcpy(m, str.c_str(), str.size()); - m[str.size()] = 0; - *_it=m; - } + Text() { + //_texts=t; + _texts.clear(); + _texts.shrink_to_fit(); + position = 0; + // _texts.push_back(cc); + _it = _texts.begin(); + } + int findText(char *str) { +#ifdef __SPEED + return -1; +#endif + // for (int i = 0; i < _texts.size(); i++) + for (int i = _texts.size() - 1; i >= 0; i--) { + if (strcmp(str, _texts[i]) == 0) { + return i; + } } - string front() - { - if (_texts.size() > 0) - return string(_texts.front()); - else - return ""; + return -1; + } + int addText(string str, uint16_t si) { + m = (char *)malloc(si + 1); + memcpy(m, str.c_str(), si); + m[str.size()] = 0; + _texts.push_back(m); + position++; + str.clear(); + return _texts.size() - 1; + } + int addText(string str) { + int pos = findText((char *)str.c_str()); + if (pos > -1) { +#ifdef __TEST_DEBUG + // printf(" find text:%d %s\r\n",pos,str.c_str()); +#endif + + return pos; + } + m = (char *)malloc(str.size() + 1); + memcpy(m, str.c_str(), str.size()); + m[str.size()] = 0; + _texts.push_back(m); + position++; + str.clear(); +#ifdef __TEST_DEBUG + // printf(" addtext:%d %s\r\n",_texts.size(),str.c_str()); +#endif + return _texts.size() - 1; + } + void addAfter(int pos, string s) { + _it = getChildAtPos(pos); + // printf(" on recupere %d:%s\n",pos,(*__it).c_str()); + // if((*_it).compare(s)!=0) + //{ + addAfter(s); + position--; + _it = getChildAtPos(position); + position++; + // } + } + void addBefore(int pos, string s) { + _it = getChildAtPos(pos - 1); + // printf(" on recupere %d:%s\n",pos,(*__it).c_str()); + // if((*_it).compare(s)!=0) + //{ + addBefore(s); + position--; + _it = getChildAtPos(position); + position++; + // } + } + + void addAfter(string str) { + int pos = findText((char *)str.c_str()); + // char *tmp; + if (pos > -1) { + m = _texts[pos]; + } else { + m = (char *)malloc(str.size() + 1); + memcpy(m, str.c_str(), str.size()); + m[str.size()] = 0; + } + if (_it == _texts.end()) { + _texts.push_back(m); + _it = _texts.end(); + _it--; + } else { + _it = _texts.insert(next(_it), m); + } + position++; + } + string back() { + if (_texts.size() > 0) + return string(_texts.back()); + else + return ""; + } + string current() { return string(*_it); } + void blankCurrent() { + int pos = findText(" "); + if (pos > -1) { + *_it = _texts[pos]; + } else { + string str = " "; + m = (char *)malloc(str.size() + 1); + memcpy(m, str.c_str(), str.size()); + m[str.size()] = 0; + *_it = m; } - string textAt(int pos) - { - if(pos>0 and pos<_texts.size()) - { - return string(_texts[pos]); - } + } + string front() { + if (_texts.size() > 0) + return string(_texts.front()); + else return ""; + } + string textAt(int pos) { + if (pos > 0 and pos < _texts.size()) { + return string(_texts[pos]); } - void pop_front() - { - if (_texts.size() > 0) - { - if(_texts.front() !=NULL) - { - // printf("we tray to look to delete:|%s|\n",_texts.front()); - if (!isReused(0)) - { - //printf("we tray to delete:|%s|\n",_texts.front()); - free(_texts.front()); - - //printf("we delted the string\n\r"); - } - } - _texts[0]=NULL; - _texts.erase(_texts.begin()); + return ""; + } + void pop_front() { + if (_texts.size() > 0) { + if (_texts.front() != NULL) { + // printf("we tray to look to delete:|%s|\n",_texts.front()); + if (!isReused(0)) { + // printf("we tray to delete:|%s|\n",_texts.front()); + free(_texts.front()); + + // printf("we delted the string\n\r"); } + } + _texts[0] = NULL; + _texts.erase(_texts.begin()); } - void addAfterNoDouble(string s) - { - - // char *str; - if (_it != _texts.end()) - { + } + void addAfterNoDouble(string s) { - if (s.compare(string(*_it)) == 0) - { + // char *str; + if (_it != _texts.end()) { - return; - } - + if (s.compare(string(*_it)) == 0) { - } - - addAfter(s); - } - void addBefore(string s) - { - int pos = findText((char *)s.c_str()); - if (pos > -1) - { - _it = _texts.insert(_it, _texts[pos]); - } - else - { - m = (char *)malloc(s.size() + 1); - memcpy(m, s.c_str(), s.size()); - m[s.size()] = 0; - _it = _texts.insert(_it, m); - } - _it++; - position++; - } - void replaceText(int pos, string str) - { - if (pos >= 0 and pos < size()) - { - // printf("repalce |%s| by |%s|\r\n",_texts[pos],str.c_str()); - if (!isReused(pos)) - { - free(_texts[pos]); - } - int _pos = findText((char *)str.c_str()); - _pos=-1; - if(_pos>-1 && _texts[_pos]!=NULL) - { - // printf("exeist already %d:%d |%s| and |%s|\n\r",_pos,pos,str.c_str(),_texts[_pos]); - - _texts[pos] = _texts[_pos]; - } - else{ - // printf("we add |%s|\n\r",str.c_str()); - m = (char *)malloc(str.size() + 1); - memcpy(m, str.c_str(), str.size()); - m[str.size()] = 0; - _texts[pos] = m; - } - } - } - vector::iterator getChildAtPos(int pos) - { - int i = 0; - if (pos >= _texts.size() || pos < 0) - { - return _texts.end(); - } - for (vector::iterator it = _texts.begin(); it != _texts.end(); it++) - { - if (i == pos) - { - return it; - } - i++; - } - return _texts.end(); - } - void putIteratorAtPos(int pos) - { - _it = getChildAtPos(pos); - //position=pos-1; - } - void end() - { - _it = getChildAtPos(_texts.size() - 1); + return; + } } - void clear() - { -// #ifndef __SPEED - // int kk = 0; - for (int i = 0; i < _texts.size(); i++) - { - char *c1 = _texts[i]; - if (c1 != NULL) - { - if (i < _texts.size() - 2) - { - for (int j = i + 1; j < _texts.size(); j++) - { - if (_texts[j] == c1) - _texts[j] = NULL; - } - } - } - } -//#endif - for (int i = 0; i < _texts.size(); i++) - { - if (_texts[i] != NULL) - { - free(_texts[i]); - // kk++; - } - } - _texts.clear(); - _texts.shrink_to_fit(); - sp.clear(); - position = 0; - _it = _texts.begin(); + + addAfter(s); + } + void addBefore(string s) { + int pos = findText((char *)s.c_str()); + if (pos > -1) { + _it = _texts.insert(_it, _texts[pos]); + } else { + m = (char *)malloc(s.size() + 1); + memcpy(m, s.c_str(), s.size()); + m[s.size()] = 0; + _it = _texts.insert(_it, m); + } + _it++; + position++; + } + void replaceText(int pos, string str) { + if (pos >= 0 and pos < size()) { + // printf("repalce |%s| by |%s|\r\n",_texts[pos],str.c_str()); + if (!isReused(pos)) { + free(_texts[pos]); + } + int _pos = findText((char *)str.c_str()); + _pos = -1; + if (_pos > -1 && _texts[_pos] != NULL) { + // printf("exeist already %d:%d |%s| and + // |%s|\n\r",_pos,pos,str.c_str(),_texts[_pos]); + + _texts[pos] = _texts[_pos]; + } else { + // printf("we add |%s|\n\r",str.c_str()); + m = (char *)malloc(str.size() + 1); + memcpy(m, str.c_str(), str.size()); + m[str.size()] = 0; + _texts[pos] = m; + } } - int size() - { - return _texts.size(); + } + vector::iterator getChildAtPos(int pos) { + int i = 0; + if (pos >= _texts.size() || pos < 0) { + return _texts.end(); + } + for (vector::iterator it = _texts.begin(); it != _texts.end(); + it++) { + if (i == pos) { + return it; + } + i++; } - char *getText(int pos) - { - if (pos >= 0 and pos < _texts.size()) - { - return _texts[pos]; - } - else - { - return cc; + return _texts.end(); + } + void putIteratorAtPos(int pos) { + _it = getChildAtPos(pos); + // position=pos-1; + } + void end() { _it = getChildAtPos(_texts.size() - 1); } + void clear() { + // #ifndef __SPEED + // int kk = 0; + for (int i = 0; i < _texts.size(); i++) { + char *c1 = _texts[i]; + if (c1 != NULL) { + if (i < _texts.size() - 2) { + for (int j = i + 1; j < _texts.size(); j++) { + if (_texts[j] == c1) + _texts[j] = NULL; + } } + } } - bool isReused(int pos) - { - #ifdef __SPEED - return false; - #endif - if (pos < 0 or pos >= _texts.size()) - { - return false; - } - char *c = _texts[pos]; - for (int i = 0; i < _texts.size(); i++) - { - if (i != pos && c== _texts[i]) - { - return true; - } - } - return false; + // #endif + for (int i = 0; i < _texts.size(); i++) { + if (_texts[i] != NULL) { + free(_texts[i]); + // kk++; + } } - void pop() - { - if (size() > 0) - { - if (!isReused(_texts.size() - 1)) - { - free(_texts.back()); - } - _texts.pop_back(); - // _texts.shrink_to_fit(); - position--; - _it = _texts.end(); - _it--; - } + _texts.clear(); + _texts.shrink_to_fit(); + sp.clear(); + position = 0; + _it = _texts.begin(); + } + int size() { return _texts.size(); } + char *getText(int pos) { + if (pos >= 0 and pos < _texts.size()) { + return _texts[pos]; + } else { + return cc; } - int get() - { - - return position - 1; + } + bool isReused(int pos) { +#ifdef __SPEED + return false; +#endif + if (pos < 0 or pos >= _texts.size()) { + return false; + } + char *c = _texts[pos]; + for (int i = 0; i < _texts.size(); i++) { + if (i != pos && c == _texts[i]) { + return true; + } } - void begin() - { - _it = _texts.begin(); - position = 0; + return false; + } + void pop() { + if (size() > 0) { + if (!isReused(_texts.size() - 1)) { + free(_texts.back()); + } + _texts.pop_back(); + // _texts.shrink_to_fit(); + position--; + _it = _texts.end(); + _it--; } - void display() - { - for (char *c : _texts) - { - printf("%s\r\n", c); - } + } + int get() { return position - 1; } + void begin() { + _it = _texts.begin(); + position = 0; + } + void display() { + for (char *c : _texts) { + printf("%s\r\n", c); } - Stack sp; - vector _texts; + } + Stack sp; + vector _texts; private: - char cc[1] = {'\0'}; - int position; - vector::iterator _it; + char cc[1] = {'\0'}; + int position; + vector::iterator _it; }; Text all_text = Text(); -enum class operandeType -{ +enum class operandeType { registers, floatregisters, boolregisters, @@ -492,26 +393,24 @@ enum class operandeType l0_FFFFFFFF, }; -enum varTypeEnum -{ - __none__, - __uint8_t__, - __uint16_t__, - __uint32_t__, - __int__, - __s_int__, - __float__, - __void__, - __CRGB__, - __CRGBW__, - __char__, - __Args__, - __bool__, - __userDefined__, - __unknown__ +enum varTypeEnum { + __none__, + __uint8_t__, + __uint16_t__, + __uint32_t__, + __int__, + __s_int__, + __float__, + __void__, + __CRGB__, + __CRGBW__, + __char__, + __Args__, + __bool__, + __userDefined__, + __unknown__ }; -enum class opCodeType -{ +enum class opCodeType { standard, call, jump, @@ -522,8 +421,8 @@ enum class opCodeType external_var, external_var_label, external_call, - ext_function_declaration, - external_call_label, + ext_function_declaration, + external_call_label, data, number, number_label, @@ -532,32 +431,28 @@ enum class opCodeType not_known }; -enum class externalType -{ +enum class externalType { function, value, }; -typedef struct -{ - void *ptr; +typedef struct { + void *ptr; externalType type; string name; string signature; string shortname; - + int offset; } asm_external; -typedef struct -{ +typedef struct { string opcde; string operandes; int error; } line; -struct error_message_struct -{ +struct error_message_struct { string error_message; int error; uint16_t line; @@ -565,111 +460,80 @@ struct error_message_struct }; error_message_struct asm_Error; -typedef struct -{ +typedef struct { error_message_struct error; int value; string label; uint32_t opcode; } result_parse_operande; -class result_parse_line -{ - public: - result_parse_line(){} +class result_parse_line { +public: + result_parse_line() {} // error_message_struct error; -char *getText() - { - return all_text.getText(nameref); - } - void addText(string t) - { - nameref = all_text.addText(t); - } - void addText(string t,uint16_t si) - { - nameref = all_text.addText(t,si); - } + char *getText() { return all_text.getText(nameref); } + void addText(string t) { nameref = all_text.addText(t); } + void addText(string t, uint16_t si) { nameref = all_text.addText(t, si); } uint32_t bincode; uint16_t size; opCodeType op; - int16_t nameref=EOF_TEXTARRAY; + int16_t nameref = EOF_TEXTARRAY; uint32_t address; bool align; - #ifdef __TEST_DEBUG - string debugtxt; - #endif +#ifdef __TEST_DEBUG + string debugtxt; +#endif int line; - uint32_t (*calculateOfssetJump)(uint32_t value, uint32_t current_address, uint32_t destination_address) = NULL; + uint32_t (*calculateOfssetJump)(uint32_t value, uint32_t current_address, + uint32_t destination_address) = NULL; }; -class parsedLines -{ +class parsedLines { - public: +public: parsedLines(){}; - int size() - { - return parsed_lines.size(); - } - - result_parse_line *getChildAtPos(int pos) - { - if(pos>=0 and pos= 0 and pos < parsed_lines.size()) { return parsed_lines[pos]; - } - else - { + } else { return NULL; } - } - result_parse_line *push_back(result_parse_line res) - { - result_parse_line *tmp=(result_parse_line *)malloc(sizeof(result_parse_line)); - memcpy(tmp,&res,sizeof(result_parse_line)); + result_parse_line *push_back(result_parse_line res) { + result_parse_line *tmp = + (result_parse_line *)malloc(sizeof(result_parse_line)); + memcpy(tmp, &res, sizeof(result_parse_line)); parsed_lines.push_back(tmp); return parsed_lines.back(); } + vector::iterator begin() { return parsed_lines.begin(); } + vector::iterator end() { return parsed_lines.end(); } - vector::iterator begin() - { - return parsed_lines.begin(); - } - vector::iterator end() - { - return parsed_lines.end(); - } - - void clear() - { - for(int i=0;i::iterator insert(vector::iterator t,result_parse_line op) -{ - result_parse_line *tmp=(result_parse_line *)malloc(sizeof(result_parse_line)); - memcpy(tmp,&op,sizeof(result_parse_line)); - return parsed_lines.insert(t,tmp); -} -result_parse_line * last() -{ - return parsed_lines.back(); -} + vector::iterator + insert(vector::iterator t, result_parse_line op) { + result_parse_line *tmp = + (result_parse_line *)malloc(sizeof(result_parse_line)); + memcpy(tmp, &op, sizeof(result_parse_line)); + return parsed_lines.insert(t, tmp); + } + result_parse_line *last() { return parsed_lines.back(); } vector parsed_lines; }; -enum asmInstruction -{ +enum asmInstruction { s8i, l8ui, l16ui, @@ -693,33 +557,14 @@ enum asmInstruction }; -string asmInstructionsName[] = - { - "s8i", - "l8ui", - "l16ui", - "l16si", - "s16i", - "l32i", - "s32i", - "lsi", - "ssi", - "add", - "sub", - "quou", - "quos", - "mull", - "add.s", - "sub.s", - "quou", - "mul.s", - "neg", - "neg.s" +string asmInstructionsName[] = { + "s8i", "l8ui", "l16ui", "l16si", "s16i", "l32i", "s32i", + "lsi", "ssi", "add", "sub", "quou", "quos", "mull", + "add.s", "sub.s", "quou", "mul.s", "neg", "neg.s" }; -typedef struct -{ +typedef struct { string name; uint32_t address; string variables; @@ -728,14 +573,13 @@ typedef struct } globalcall; typedef struct { - - string json; - uint8_t type; - uint32_t address; + + string json; + uint8_t type; + uint32_t address; } jsonVariable; -typedef struct -{ +typedef struct { error_message_struct error; vector functions; vector jsonVars; @@ -748,76 +592,83 @@ typedef struct } executable; -typedef struct -{ - error_message_struct error; - uint8_t *binary_data; - uint8_t *function_data; - uint16_t instruction_size; - uint16_t tmp_instruction_size; - uint16_t function_size; - uint16_t data_size; - +typedef struct { + error_message_struct error; + uint8_t *binary_data; + uint8_t *function_data; + uint16_t instruction_size; + uint16_t tmp_instruction_size; + uint16_t function_size; + uint16_t data_size; } Binary; operandeType op_mov_n[2] = {operandeType::registers, operandeType::registers}; -operandeType *op_mov = op_mov_n; //[3] = {operandeType::registers, operandeType::registers}; -operandeType op_l8ui[3] = {operandeType::registers, operandeType::registers, operandeType::l0_255}; -operandeType *op_s8i = op_l8ui; //[3] = {operandeType::registers, operandeType::registers, operandeType::l0_255}; -operandeType op_l16si[3] = {operandeType::registers, operandeType::registers, operandeType::l0_510}; -operandeType *op_l16ui = op_l16si; //[3] = {operandeType::registers, operandeType::registers, operandeType::l0_510}; -operandeType *op_s16i = op_l16si; //[3] = {operandeType::registers, operandeType::registers, operandeType::l0_510}; -operandeType op_l32i_n[3] = {operandeType::registers, operandeType::registers, operandeType::l0_60}; -operandeType op_s32i[3] = {operandeType::registers, operandeType::registers, operandeType::l0_1020}; -operandeType *op_s32i_n = op_l32i_n; //[3] = {operandeType::registers, operandeType::registers, operandeType::l0_60}; -operandeType *op_l32i = op_s32i; // [3] = {operandeType::registers, operandeType::registers, operandeType::l0_1020}; -operandeType op_lsi[3] = {operandeType::floatregisters, operandeType::registers, operandeType::l0_1020}; -operandeType *op_ssi = op_lsi; //[3] = {operandeType::floatregisters, operandeType::registers, operandeType::l0_1020}; -operandeType op_rfr[2] = {operandeType::registers, operandeType::floatregisters}; -operandeType op_wfr[2] = {operandeType::floatregisters, operandeType::registers}; -operandeType op_add[3] = {operandeType::registers, operandeType::registers, operandeType::registers}; +operandeType *op_mov = + op_mov_n; //[3] = {operandeType::registers, operandeType::registers}; +operandeType op_l8ui[3] = {operandeType::registers, operandeType::registers, + operandeType::l0_255}; +operandeType *op_s8i = + op_l8ui; //[3] = {operandeType::registers, operandeType::registers, + //operandeType::l0_255}; +operandeType op_l16si[3] = {operandeType::registers, operandeType::registers, + operandeType::l0_510}; +operandeType *op_l16ui = + op_l16si; //[3] = {operandeType::registers, operandeType::registers, + //operandeType::l0_510}; +operandeType *op_s16i = + op_l16si; //[3] = {operandeType::registers, operandeType::registers, + //operandeType::l0_510}; +operandeType op_l32i_n[3] = {operandeType::registers, operandeType::registers, + operandeType::l0_60}; +operandeType op_s32i[3] = {operandeType::registers, operandeType::registers, + operandeType::l0_1020}; +operandeType *op_s32i_n = + op_l32i_n; //[3] = {operandeType::registers, operandeType::registers, + //operandeType::l0_60}; +operandeType *op_l32i = + op_s32i; // [3] = {operandeType::registers, operandeType::registers, + // operandeType::l0_1020}; +operandeType op_lsi[3] = {operandeType::floatregisters, operandeType::registers, + operandeType::l0_1020}; +operandeType *op_ssi = + op_lsi; //[3] = {operandeType::floatregisters, operandeType::registers, + //operandeType::l0_1020}; +operandeType op_rfr[2] = {operandeType::registers, + operandeType::floatregisters}; +operandeType op_wfr[2] = {operandeType::floatregisters, + operandeType::registers}; +operandeType op_add[3] = {operandeType::registers, operandeType::registers, + operandeType::registers}; operandeType *op_quou = op_add; operandeType *op_sub = op_add; operandeType *op_mull = op_add; -operandeType op_adds[3] = {operandeType::floatregisters, operandeType::floatregisters, operandeType::floatregisters}; +operandeType op_adds[3] = {operandeType::floatregisters, + operandeType::floatregisters, + operandeType::floatregisters}; operandeType *op_quous = op_adds; operandeType *op_subs = op_adds; operandeType *op_muls = op_adds; operandeType *op_neg = op_mov; -operandeType op_nexp01s[2] = {operandeType::floatregisters, operandeType::floatregisters}; +operandeType op_nexp01s[2] = {operandeType::floatregisters, + operandeType::floatregisters}; operandeType *op_negs = op_nexp01s; -operandeType *asmInstructionOperandes[] = - { - op_s8i, - op_l8ui, - op_l16ui, - op_l16si, - op_s16i, - op_l32i, - op_s32i, - op_lsi, - op_ssi, - op_add, // add - op_sub, // sub - op_quou, // quou - op_quou, - op_mull, // mull - op_adds, - op_subs, - op_quou, - op_muls, - op_neg, - op_negs, +operandeType *asmInstructionOperandes[] = { + op_s8i, op_l8ui, op_l16ui, op_l16si, op_s16i, op_l32i, + op_s32i, op_lsi, op_ssi, + op_add, // add + op_sub, // sub + op_quou, // quou + op_quou, + op_mull, // mull + op_adds, op_subs, op_quou, op_muls, op_neg, op_negs, }; operandeType *__l; operandeType __op; -string getRegType(asmInstruction instr, int pos) -{ - if (instr >= 20) - { +string getRegType(asmInstruction instr, int pos) { + if (instr >= 20) { printf("to hight\r\n"); return " "; } @@ -825,69 +676,44 @@ string getRegType(asmInstruction instr, int pos) __l = asmInstructionOperandes[instr]; __op = __l[pos]; - if (__op == operandeType::registers) - { + if (__op == operandeType::registers) { return "a"; - } - else if (__op == operandeType::floatregisters) - { + } else if (__op == operandeType::floatregisters) { return "f"; - } - else - { + } else { return "unknown"; } } -class _arguments -{ +class _arguments { public: - _arguments() - { - vartype = __unknown__; - } - _arguments(int val) - { - vartype = __int__; - intval = val; - } - _arguments(float val) - { - vartype = __float__; - floatval = val; - } + _arguments() { vartype = __unknown__; } + _arguments(int val) { + vartype = __int__; + intval = val; + } + _arguments(float val) { + vartype = __float__; + floatval = val; + } - varTypeEnum vartype; - int intval; - float floatval; + varTypeEnum vartype; + int intval; + float floatval; }; -class Arguments -{ +class Arguments { public: - Arguments() {} - void add(int val) - { - _args.push_back(_arguments(val)); - } - void add(float val) - { - _args.push_back(_arguments(val)); - } - void clear() - { - _args.clear(); - _args.shrink_to_fit(); - } - void add(_arguments a) - { - _args.push_back(a); - } - int size() - { - return _args.size(); - } - vector<_arguments> _args; + Arguments() {} + void add(int val) { _args.push_back(_arguments(val)); } + void add(float val) { _args.push_back(_arguments(val)); } + void clear() { + _args.clear(); + _args.shrink_to_fit(); + } + void add(_arguments a) { _args.push_back(a); } + int size() { return _args.size(); } + vector<_arguments> _args; }; #endif \ No newline at end of file diff --git a/src/execute.h b/src/execute.h index ab5a373..ba2b6ca 100644 --- a/src/execute.h +++ b/src/execute.h @@ -14,43 +14,38 @@ executable executecmd; volatile bool useOfSync; #ifdef __TEST_DEBUG -typedef struct _TaskHandle_t -{ - int h; +typedef struct _TaskHandle_t { + int h; }; typedef _TaskHandle_t *TaskHandle_t; -typedef struct _xSemaphoreHandle -{ - int h; +typedef struct _xSemaphoreHandle { + int h; }; typedef _xSemaphoreHandle *xSemaphoreHandle; #endif -typedef struct -{ - uint16_t data_size; - uint16_t binary_size; - uint16_t total_size; - bool isRunning; - string name; +typedef struct { + uint16_t data_size; + uint16_t binary_size; + uint16_t total_size; + bool isRunning; + string name; } exe_info; TaskHandle_t __run_handles[_MAX_PROG_AT_ONCE]; volatile xSemaphoreHandle waitEndShow[_MAX_PROG_AT_ONCE]; ; // string strcompile; bool exeExist; -typedef struct -{ - vector args; - string json; - - executable exe; +typedef struct { + vector args; + string json; + + executable exe; } _exe_args; -typedef struct -{ - string name; - bool isRunning; +typedef struct { + string name; + bool isRunning; } exeInfo; #ifndef __TEST_DEBUG @@ -66,1015 +61,845 @@ static void _run_task(void *pvParameters); #include "esp_task_wdt.h" #endif -static void feedTheDog() -{ +static void feedTheDog() { #ifndef __TEST_DEBUG - vTaskDelay(1); + vTaskDelay(1); #endif } class Executable; static void syncExt(int h); -class _executablesClass -{ +class _executablesClass { public: - _executablesClass() - { - for (int i = 0; i < _MAX_PROG_AT_ONCE; i++) - { - __run_handles[i] = NULL; - waitEndShow[i] = NULL; - execPtr[i] = NULL; - } - nb_concurrent_programs = 0; - addExternalFunction("_sync", "void", "uint32_t", (void *)syncExt); - // addExternal("feed", externalType::function, (void *)feedTheDog); + _executablesClass() { + for (int i = 0; i < _MAX_PROG_AT_ONCE; i++) { + __run_handles[i] = NULL; + waitEndShow[i] = NULL; + execPtr[i] = NULL; } - int getHandle(Executable *exec) - { + nb_concurrent_programs = 0; + addExternalFunction("_sync", "void", "uint32_t", (void *)syncExt); + // addExternal("feed", externalType::function, (void *)feedTheDog); + } + int getHandle(Executable *exec) { - if (nb_concurrent_programs < _MAX_PROG_AT_ONCE - 1) - { -#ifndef __TEST_DEBUG - for (int i = 0; i < _MAX_PROG_AT_ONCE; i++) - { - if (__run_handles[i] == NULL) - { - nb_concurrent_programs++; - // printf(" ndb prgh:%d position%d\r\n", nb_concurrent_programs, i); - waitEndShow[i] = xSemaphoreCreateBinary(); - execPtr[i] = exec; - return i; - } - } -#endif - return 9999; - } - else - { - return 9999; - } - } - uint32_t getMask(); - void kill(int handle); - void suspendAll() - { + if (nb_concurrent_programs < _MAX_PROG_AT_ONCE - 1) { #ifndef __TEST_DEBUG - for (int i = 0; i < _MAX_PROG_AT_ONCE; i++) - { - if (__run_handles[i] != NULL) - { - vTaskSuspend(__run_handles[i]); - } + for (int i = 0; i < _MAX_PROG_AT_ONCE; i++) { + if (__run_handles[i] == NULL) { + nb_concurrent_programs++; + // printf(" ndb prgh:%d position%d\r\n", nb_concurrent_programs, i); + waitEndShow[i] = xSemaphoreCreateBinary(); + execPtr[i] = exec; + return i; } + } #endif + return 9999; + } else { + return 9999; } - void restartAll() - { + } + uint32_t getMask(); + void kill(int handle); + void suspendAll() { #ifndef __TEST_DEBUG - for (int i = 0; i < _MAX_PROG_AT_ONCE; i++) - { - if (__run_handles[i] != NULL) - { - vTaskResume(__run_handles[i]); - } - } -#endif + for (int i = 0; i < _MAX_PROG_AT_ONCE; i++) { + if (__run_handles[i] != NULL) { + vTaskSuspend(__run_handles[i]); + } } - void freeSync() - { -#ifndef __TEST_DEBUG - if (nb_concurrent_programs == 0) - return; - uint32_t MASK = getMask(); - // TickType_t xTicksToWait = 2000 / portTICK_PERIOD_MS; - xEventGroupSync(xCreatedEventGroup, - MASK, - MASK, - portMAX_DELAY); - vTaskDelay(10); - xEventGroupSync(xCreatedEventGroup2, - MASK, - MASK, - portMAX_DELAY); #endif + } + void restartAll() { +#ifndef __TEST_DEBUG + for (int i = 0; i < _MAX_PROG_AT_ONCE; i++) { + if (__run_handles[i] != NULL) { + vTaskResume(__run_handles[i]); + } } - TaskHandle_t *getHandleByIndex(int i) - { - if (i >= 0 and i < _MAX_PROG_AT_ONCE) - { - // waitEndShow[i]= xSemaphoreCreateBinary(); - return &__run_handles[i]; - } - else - { - return NULL; - } - } - void removeHandle(int i) - { - nb_concurrent_programs--; - __run_handles[i] = NULL; - waitEndShow[i] = NULL; - execPtr[i] = NULL; - } - void setFunctionToSync(void (*function)()) - { - functionToSync = function; - } - - int getFirstHandle() - { - for (int i = 0; i < _MAX_PROG_AT_ONCE; i++) - { - if (__run_handles[i] != NULL) - { - return i; - } - } - - return _MAX_PROG_AT_ONCE; - } - void setPrekill(void (*function)(), void (*function2)()) - { - _prekill = function; - _postkill = function2; - } - void prekill() - { - if (nb_concurrent_programs == 1) - { - if (_prekill != NULL) - _prekill(); - } - } - void postkill() - { - if (nb_concurrent_programs == 1) - { - if (_postkill != NULL) - _postkill(); - } - } - - // private: - int nb_concurrent_programs; - int nb_concurrent_programs_current; - void (*functionToSync)() = NULL; - Executable *execPtr[_MAX_PROG_AT_ONCE]; - void (*_prekill)() = NULL; - void (*_postkill)() = NULL; -}; -_executablesClass runningPrograms = _executablesClass(); -static void syncExt(int h) -{ +#endif + } + void freeSync() { #ifndef __TEST_DEBUG - isSyncalled = true; - if (resetSync) - return; - // printf("on tente %d\r\n",h); - uint32_t MASK = runningPrograms.getMask(); - EventBits_t uxReturn; + if (nb_concurrent_programs == 0) + return; + uint32_t MASK = getMask(); // TickType_t xTicksToWait = 2000 / portTICK_PERIOD_MS; - uxReturn = xEventGroupSync(xCreatedEventGroup, - 1 << h, - MASK, - portMAX_DELAY); - - // printf("all \r\n"); - - if (h == runningPrograms.getFirstHandle()) - { - if ((uxReturn & MASK) == MASK) - { - xEventGroupClearBits(xCreatedEventGroup, MASK); - // xEventGroupClearBits(xCreatedEventGroup2, MASK); - - if (runningPrograms.functionToSync != NULL) - { - - runningPrograms.functionToSync(); - if (resetSync) - return; - } - - xEventGroupSync(xCreatedEventGroup2, - 1 << h, - MASK, - portMAX_DELAY); - - if (toResetSync) - { - resetSync = true; - toResetSync = false; - } - xEventGroupClearBits(xCreatedEventGroup2, MASK); - } - } - else - { - xEventGroupSync(xCreatedEventGroup2, - 1 << h, - MASK, - portMAX_DELAY); - } - isSyncalled = true; + xEventGroupSync(xCreatedEventGroup, MASK, MASK, portMAX_DELAY); + vTaskDelay(10); + xEventGroupSync(xCreatedEventGroup2, MASK, MASK, portMAX_DELAY); +#endif + } + TaskHandle_t *getHandleByIndex(int i) { + if (i >= 0 and i < _MAX_PROG_AT_ONCE) { + // waitEndShow[i]= xSemaphoreCreateBinary(); + return &__run_handles[i]; + } else { + return NULL; + } + } + void removeHandle(int i) { + nb_concurrent_programs--; + __run_handles[i] = NULL; + waitEndShow[i] = NULL; + execPtr[i] = NULL; + } + void setFunctionToSync(void (*function)()) { functionToSync = function; } + + int getFirstHandle() { + for (int i = 0; i < _MAX_PROG_AT_ONCE; i++) { + if (__run_handles[i] != NULL) { + return i; + } + } + + return _MAX_PROG_AT_ONCE; + } + void setPrekill(void (*function)(), void (*function2)()) { + _prekill = function; + _postkill = function2; + } + void prekill() { + if (nb_concurrent_programs == 1) { + if (_prekill != NULL) + _prekill(); + } + } + void postkill() { + if (nb_concurrent_programs == 1) { + if (_postkill != NULL) + _postkill(); + } + } + + // private: + int nb_concurrent_programs; + int nb_concurrent_programs_current; + void (*functionToSync)() = NULL; + Executable *execPtr[_MAX_PROG_AT_ONCE]; + void (*_prekill)() = NULL; + void (*_postkill)() = NULL; +}; +_executablesClass runningPrograms = _executablesClass(); +static void syncExt(int h) { +#ifndef __TEST_DEBUG + isSyncalled = true; + if (resetSync) + return; + // printf("on tente %d\r\n",h); + uint32_t MASK = runningPrograms.getMask(); + EventBits_t uxReturn; + // TickType_t xTicksToWait = 2000 / portTICK_PERIOD_MS; + uxReturn = xEventGroupSync(xCreatedEventGroup, 1 << h, MASK, portMAX_DELAY); + + // printf("all \r\n"); + + if (h == runningPrograms.getFirstHandle()) { + if ((uxReturn & MASK) == MASK) { + xEventGroupClearBits(xCreatedEventGroup, MASK); + // xEventGroupClearBits(xCreatedEventGroup2, MASK); + + if (runningPrograms.functionToSync != NULL) { + + runningPrograms.functionToSync(); + if (resetSync) + return; + } + + xEventGroupSync(xCreatedEventGroup2, 1 << h, MASK, portMAX_DELAY); + + if (toResetSync) { + resetSync = true; + toResetSync = false; + } + xEventGroupClearBits(xCreatedEventGroup2, MASK); + } + } else { + xEventGroupSync(xCreatedEventGroup2, 1 << h, MASK, portMAX_DELAY); + } + isSyncalled = true; // printf("release %d\r\n",h); #endif } -class Executable -{ +class Executable { public: - int __run_handle_index; - - bool exeExist; - bool _isRunning = false; - bool isHalted = false; - error_message_struct error; - string name = "Unknown"; - Arguments args; - _exe_args df; - Executable() - { - exeExist = false; - __run_handle_index = 9999; - _executecmd.binary_size = 0; - _executecmd.data_size = 0; - // args.clear(); - // args.shrink_to_fit(); - } - Executable(executable _executable) - { - if (_executable.error.error == 0) - { + int __run_handle_index; - exeExist = true; - } - else - { - exeExist = false; - } - _executecmd = _executable; + bool exeExist; + bool _isRunning = false; + bool isHalted = false; + error_message_struct error; + string name = "Unknown"; + Arguments args; + _exe_args df; + Executable() { + exeExist = false; + __run_handle_index = 9999; + _executecmd.binary_size = 0; + _executecmd.data_size = 0; + // args.clear(); + // args.shrink_to_fit(); + } + Executable(executable _executable) { + if (_executable.error.error == 0) { + + exeExist = true; + } else { + exeExist = false; } + _executecmd = _executable; + } #ifndef __TEST_DEBUG - void createExecutableFromBinary(Binary *bin) - { - _executecmd = createExectutable(bin); - if (_executecmd.error.error == 0) - { + void createExecutableFromBinary(Binary *bin) { + _executecmd = createExectutable(bin); + if (_executecmd.error.error == 0) { - exeExist = true; - } - else - { - printf("%s\n\r", _executecmd.error.error_message.c_str()); - exeExist = false; - } + exeExist = true; + } else { + printf("%s\n\r", _executecmd.error.error_message.c_str()); + exeExist = false; } + } #endif - void setExecutable(executable _executable) - { - _executecmd = _executable; - if (_executable.error.error == 0) - { + void setExecutable(executable _executable) { + _executecmd = _executable; + if (_executable.error.error == 0) { - exeExist = true; - } - else - { - exeExist = false; - } + exeExist = true; + } else { + exeExist = false; } - bool isFunctionInExecutable(string name) - { - for (int i = 0; i < _executecmd.functions.size(); i++) - { - if (_executecmd.functions[i].name.compare(name) == 0) - { - return true; - } - } - return false; + } + bool isFunctionInExecutable(string name) { + for (int i = 0; i < _executecmd.functions.size(); i++) { + if (_executecmd.functions[i].name.compare(name) == 0) { + return true; + } } - void createExecutable() - { + return false; + } + void createExecutable() { #ifndef __TEST_DEBUG - if (_isRunning) - { + if (_isRunning) { - pushToConsole("Something Already running kill it first ...", true); + pushToConsole("Something Already running kill it first ...", true); - kill(); - } + kill(); + } - // bool othercore = false; + // bool othercore = false; - if (exeExist) - { - freeExecutable(&_executecmd); - } - exeExist = false; + if (exeExist) { + freeExecutable(&_executecmd); + } + exeExist = false; #endif - } + } - void suspend() - { + void suspend() { #ifndef __TEST_DEBUG #ifndef __TEST_DEBUG - if (_isRunning and !isHalted) - { + if (_isRunning and !isHalted) { - pushToConsole("Halting the program ...", true); + pushToConsole("Halting the program ...", true); - resetSync = true; + resetSync = true; - runningPrograms.freeSync(); - vTaskDelay(10); - if (__run_handle_index != 9999) - { - vTaskSuspend(*runningPrograms.getHandleByIndex(__run_handle_index)); - } + runningPrograms.freeSync(); + vTaskDelay(10); + if (__run_handle_index != 9999) { + vTaskSuspend(*runningPrograms.getHandleByIndex(__run_handle_index)); + } - // _isRunning=false; - isHalted = true; - vTaskDelay(10); + // _isRunning=false; + isHalted = true; + vTaskDelay(10); - // vTaskDelay(20); - pushToConsole("Program Halted.", true); - } + // vTaskDelay(20); + pushToConsole("Program Halted.", true); + } - // freeExecutable(&_executecmd); + // freeExecutable(&_executecmd); #endif #endif - } - void restart() - { + } + void restart() { #ifndef __TEST_DEBUG - if (_isRunning and isHalted) - { - resetSync = true; - - runningPrograms.freeSync(); - vTaskDelay(10); - vTaskResume(*runningPrograms.getHandleByIndex(__run_handle_index)); - isHalted = false; - resetSync = false; - } -#endif + if (_isRunning and isHalted) { + resetSync = true; + + runningPrograms.freeSync(); + vTaskDelay(10); + vTaskResume(*runningPrograms.getHandleByIndex(__run_handle_index)); + isHalted = false; + resetSync = false; } - void kill() - { +#endif + } + void kill() { #ifndef __TEST_DEBUG - if (_isRunning) - { - pushToConsole("Stopping the program ...", true); - - // printf("old mask %d\r\n",runningPrograms.getMask()); - toResetSync = true; - while (!toResetSync) - { - } - vTaskDelay(10); - runningPrograms.freeSync(); - // vTaskDelay(10); - runningPrograms.suspendAll(); - - runningPrograms.prekill(); - vTaskDelay(30); - - if (__run_handle_index != 9999) - { - vTaskDelete(*runningPrograms.getHandleByIndex(__run_handle_index)); - } - - _isRunning = false; - vTaskDelay(30); - - runningPrograms.postkill(); - // vTaskDelay(20); - pushToConsole("Program stopped.", true); - - vTaskDelay(10); - runningPrograms.removeHandle(__run_handle_index); - // printf("new mask %d\r\n",runningPrograms.getMask()); - resetSync = false; - toResetSync = false; - runningPrograms.restartAll(); - isSyncalled = false; - __run_handle_index = 9999; - } + if (_isRunning) { + pushToConsole("Stopping the program ...", true); - // freeExecutable(&_executecmd); -#endif - } + // printf("old mask %d\r\n",runningPrograms.getMask()); + toResetSync = true; + while (!toResetSync) { + } + vTaskDelay(10); + runningPrograms.freeSync(); + // vTaskDelay(10); + runningPrograms.suspendAll(); - int _run(vector args, bool second_core, int core, Arguments arguments,string json) - { - __run_handle_index = 9999; -#ifndef __TEST_DEBUG - if (_isRunning) - { + runningPrograms.prekill(); + vTaskDelay(30); - kill(); - } + if (__run_handle_index != 9999) { + vTaskDelete(*runningPrograms.getHandleByIndex(__run_handle_index)); + } - if (exeExist == true) - { - - //printf("rrrr %d\r\n",args.size()); - df.args = args; - - df.exe = _executecmd; - df.json=json; - // - // we free the sync - - toResetSync = true; - // vTaskDelay(30); - if (isSyncalled) - while (!resetSync) - { - } - // printf("rrrr\r\n"); - // runningPrograms.freeSync(); - vTaskDelay(20); - runningPrograms.suspendAll(); - - __run_handle_index = runningPrograms.getHandle(this); - _isRunning = true; - toResetSync = false; - resetSync = false; - runningPrograms.restartAll(); - // vTaskDelay(10); - - if (__run_handle_index == 9999) - { - pushToConsole("too many programs at once", true); - } - string taskname; - if (name.compare("Unknow") == 0) - taskname = string_format("_run_task_%d", __run_handle_index); - else - taskname = string_format("%s_%d", name.c_str(), __run_handle_index); - xTaskCreateUniversal(_run_task, taskname.c_str(), 4096 * 2, this, 3, (TaskHandle_t *)runningPrograms.getHandleByIndex(__run_handle_index), core); - - pushToConsole("Execution on going CTRL + k to stop", true); - } - else - { - pushToConsole("Nothing to execute.", true); - } -#endif - return __run_handle_index; - } - void free() - { -#ifndef __TEST_DEBUG - if (_isRunning) - { - kill(); - } - if (exeExist) - { - uint32_t memb = esp_get_free_heap_size(); - printf("Free memory before:%ld\r\n", esp_get_free_heap_size()); - freeExecutable(&_executecmd); - _isRunning = false; - uint32_t mema = esp_get_free_heap_size(); - printf("Free memory after:%ld freed:%ld\r\n", mema, mema - memb); - } - exeExist = false; + _isRunning = false; + vTaskDelay(30); -#endif - } + runningPrograms.postkill(); + // vTaskDelay(20); + pushToConsole("Program stopped.", true); - bool isExeExists() - { - return exeExist; + vTaskDelay(10); + runningPrograms.removeHandle(__run_handle_index); + // printf("new mask %d\r\n",runningPrograms.getMask()); + resetSync = false; + toResetSync = false; + runningPrograms.restartAll(); + isSyncalled = false; + __run_handle_index = 9999; } -#ifndef __TEST_DEBUG - void executeOnly(string prog) - { - args.clear(); -#ifndef __TEST_DEBUG - // res = executeBinary("@__footer", _executecmd, 9999, args); - error_message_struct res = executeBinary("@_" + prog, _executecmd, 9999, this, args); - if (res.error) - { - pushToConsole(res.error_message, true); - } + + // freeExecutable(&_executecmd); #endif - } - void execute(string prog) - { - args.clear(); - Arguments d; + } + + int _run(vector args, bool second_core, int core, Arguments arguments, + string json) { + __run_handle_index = 9999; #ifndef __TEST_DEBUG - error_message_struct res = executeBinary("@__footer", _executecmd, 9999, this, d); + if (_isRunning) { - res = executeBinary("@__" + prog, _executecmd, 9999, this, args); - if (res.error) - { - pushToConsole(res.error_message, true); - } -#endif + kill(); } - void execute (string prog, string json) - { - args.clear(); - Arguments d; + if (exeExist == true) { -#ifndef __TEST_DEBUG - error_message_struct res = executeBinary("@__footer", _executecmd, 9999, this, d); + // printf("rrrr %d\r\n",args.size()); + df.args = args; - res = executeBinary("@__" + prog, _executecmd, 9999, this, args,json); - if (res.error) - { - pushToConsole(res.error_message, true); - } -#endif - } + df.exe = _executecmd; + df.json = json; + // + // we free the sync - void execute(string prog, Arguments arguments) - { - args.clear(); - Arguments d; - for (int i = 0; i < arguments._args.size(); i++) - { - args.add(arguments._args[i]); + toResetSync = true; + // vTaskDelay(30); + if (isSyncalled) + while (!resetSync) { } -#ifndef __TEST_DEBUG - error_message_struct res = executeBinary("@__footer", _executecmd, 9999, this, d); + // printf("rrrr\r\n"); + // runningPrograms.freeSync(); + vTaskDelay(20); + runningPrograms.suspendAll(); - res = executeBinary("@__" + prog, _executecmd, 9999, this, args); - if (res.error) - { - pushToConsole(res.error_message, true); - } -#endif - } - bool functionExist(string function) - { - for (int i = 0; i < _executecmd.functions.size(); i++) - { - string ftofind = _executecmd.functions[i].name; - if (_executecmd.functions[i].name.find_first_of("(") != string::npos) - { - ftofind = _executecmd.functions[i].name.substr(0, _executecmd.functions[i].name.find_first_of("(")); - } - // printf("coparing %s\n",ftofind.c_str()); - if (ftofind.compare("@__"+function) == 0) - { - return true; - } - } - return false; -} - void executeAsTask(string prog, int core, Arguments arguments,string json) - { -//printf("herqsd sqdsq\n"); - args.clear(); - for (int i = 0; i < arguments._args.size(); i++) - { - args.add(arguments._args[i]); - } -#ifndef __TEST_DEBUG - if (core == 0 or core == 1) - { - vector __args; - __args.push_back("@__" + prog); - _run(__args, true, core, arguments,json); - } - else - { - pushToConsole(string_format("Wrong core number %d should be 0 or 1", core)); - } -#endif - } + __run_handle_index = runningPrograms.getHandle(this); + _isRunning = true; + toResetSync = false; + resetSync = false; + runningPrograms.restartAll(); + // vTaskDelay(10); + + if (__run_handle_index == 9999) { + pushToConsole("too many programs at once", true); + } + string taskname; + if (name.compare("Unknow") == 0) + taskname = string_format("_run_task_%d", __run_handle_index); + else + taskname = string_format("%s_%d", name.c_str(), __run_handle_index); + xTaskCreateUniversal( + _run_task, taskname.c_str(), 4096 * 2, this, 3, + (TaskHandle_t *)runningPrograms.getHandleByIndex(__run_handle_index), + core); - void executeAsTask(string prog, int core, Arguments arguments) - { - executeAsTask(prog,core,arguments,""); + pushToConsole("Execution on going CTRL + k to stop", true); + } else { + pushToConsole("Nothing to execute.", true); } - void executeAsTask(string prog, Arguments arguments) - { - executeAsTask(prog, __RUN_CORE, arguments); +#endif + return __run_handle_index; + } + void free() { +#ifndef __TEST_DEBUG + if (_isRunning) { + kill(); } - void executeAsTask(string prog) - { - args.clear(); - executeAsTask(prog, __RUN_CORE, args); + if (exeExist) { + uint32_t memb = esp_get_free_heap_size(); + printf("Free memory before:%ld\r\n", esp_get_free_heap_size()); + freeExecutable(&_executecmd); + _isRunning = false; + uint32_t mema = esp_get_free_heap_size(); + printf("Free memory after:%ld freed:%ld\r\n", mema, mema - memb); } + exeExist = false; - void executeAsTask(string prog, int core) - { - args.clear(); - executeAsTask(prog, core, args); - } - void executeAsTask(string prog,string json) - { - // printf("her\n"); - args.clear(); - executeAsTask(prog,__RUN_CORE,args,json); - } #endif - bool isRunning() - { - return _isRunning; + } + + bool isExeExists() { return exeExist; } +#ifndef __TEST_DEBUG + void executeOnly(string prog) { + args.clear(); +#ifndef __TEST_DEBUG + // res = executeBinary("@__footer", _executecmd, 9999, args); + error_message_struct res = + executeBinary("@_" + prog, _executecmd, 9999, this, args); + if (res.error) { + pushToConsole(res.error_message, true); } -error_message_struct updateParam(string json) -{ - #ifdef __JSON__OPTION__ - return updateParameters(_executecmd, json); - #else -error_message_struct res; -res.error=0; -return res; - - #endif -} +#endif + } + void execute(string prog) { + args.clear(); + Arguments d; +#ifndef __TEST_DEBUG + error_message_struct res = + executeBinary("@__footer", _executecmd, 9999, this, d); - void (*prekill)() = NULL; - void (*postkill)() = NULL; - executable _executecmd; + res = executeBinary("@__" + prog, _executecmd, 9999, this, args); + if (res.error) { + pushToConsole(res.error_message, true); + } +#endif + } - // bool exeExist=false; -}; -static void _run_task(void *pvParameters) -{ + void execute(string prog, string json) { + args.clear(); + Arguments d; #ifndef __TEST_DEBUG + error_message_struct res = + executeBinary("@__footer", _executecmd, 9999, this, d); - Executable *exec = ((Executable *)pvParameters); - // esp_task_wdt_delete(NULL); - _exe_args *_fg = &exec->df; - exec->_isRunning = true; - Arguments d; - // printf("as a ttaks:%d\n\r",exec->__run_handle_index); - if (exec->df.args.size() > 0) - { - //printf("as a ttaks args >0:%s %s\n\r",exec->df.args[0].c_str(),exec->df.json.c_str()); - error_message_struct res = executeBinary("@__footer", exec->df.exe, exec->__run_handle_index, exec, d); -//printf("as a ttaks args >0:%s %s\n\r",_fg->args[0].c_str(),_fg->json.c_str()); - res = executeBinary(_fg->args[0], exec->df.exe, exec->__run_handle_index, exec, exec->args,_fg->json); - if (res.error) - { - pushToConsole(res.error_message, true); - } + res = executeBinary("@__" + prog, _executecmd, 9999, this, args, json); + if (res.error) { + pushToConsole(res.error_message, true); } - else - { - // printf("as a ttaks:%s\n\r",exec->df.json); - error_message_struct res = executeBinary("@__footer", exec->df.exe, exec->__run_handle_index, exec, d); - res = executeBinary("@__main", exec->df.exe, exec->__run_handle_index, exec, exec->args,exec->df.json); - if (res.error) - { - pushToConsole(res.error_message, true); - } - } - pushToConsole("Execution done.", true); - // exec->__run_handle= NULL; - exec->_isRunning = false; - runningPrograms.removeHandle(exec->__run_handle_index); - isSyncalled = false; - vTaskDelete(NULL); +#endif + } + + void execute(string prog, Arguments arguments) { + args.clear(); + Arguments d; + for (int i = 0; i < arguments._args.size(); i++) { + args.add(arguments._args[i]); + } +#ifndef __TEST_DEBUG + error_message_struct res = + executeBinary("@__footer", _executecmd, 9999, this, d); + + res = executeBinary("@__" + prog, _executecmd, 9999, this, args); + if (res.error) { + pushToConsole(res.error_message, true); + } +#endif + } + bool functionExist(string function) { + for (int i = 0; i < _executecmd.functions.size(); i++) { + string ftofind = _executecmd.functions[i].name; + if (_executecmd.functions[i].name.find_first_of("(") != string::npos) { + ftofind = _executecmd.functions[i].name.substr( + 0, _executecmd.functions[i].name.find_first_of("(")); + } + // printf("coparing %s\n",ftofind.c_str()); + if (ftofind.compare("@__" + function) == 0) { + return true; + } + } + return false; + } + void executeAsTask(string prog, int core, Arguments arguments, string json) { + // printf("herqsd sqdsq\n"); + args.clear(); + for (int i = 0; i < arguments._args.size(); i++) { + args.add(arguments._args[i]); + } +#ifndef __TEST_DEBUG + if (core == 0 or core == 1) { + vector __args; + __args.push_back("@__" + prog); + _run(__args, true, core, arguments, json); + } else { + pushToConsole( + string_format("Wrong core number %d should be 0 or 1", core)); + } +#endif + } + + void executeAsTask(string prog, int core, Arguments arguments) { + executeAsTask(prog, core, arguments, ""); + } + void executeAsTask(string prog, Arguments arguments) { + executeAsTask(prog, __RUN_CORE, arguments); + } + void executeAsTask(string prog) { + args.clear(); + executeAsTask(prog, __RUN_CORE, args); + } + + void executeAsTask(string prog, int core) { + args.clear(); + executeAsTask(prog, core, args); + } + void executeAsTask(string prog, string json) { + // printf("her\n"); + args.clear(); + executeAsTask(prog, __RUN_CORE, args, json); + } +#endif + bool isRunning() { return _isRunning; } + error_message_struct updateParam(string json) { +#ifdef __JSON__OPTION__ + return updateParameters(_executecmd, json); +#else + error_message_struct res; + res.error = 0; + return res; + +#endif + } + + void (*prekill)() = NULL; + void (*postkill)() = NULL; + executable _executecmd; + + // bool exeExist=false; +}; +static void _run_task(void *pvParameters) { + +#ifndef __TEST_DEBUG + + Executable *exec = ((Executable *)pvParameters); + // esp_task_wdt_delete(NULL); + _exe_args *_fg = &exec->df; + exec->_isRunning = true; + Arguments d; + // printf("as a ttaks:%d\n\r",exec->__run_handle_index); + if (exec->df.args.size() > 0) { + // printf("as a ttaks args >0:%s + // %s\n\r",exec->df.args[0].c_str(),exec->df.json.c_str()); + error_message_struct res = executeBinary("@__footer", exec->df.exe, + exec->__run_handle_index, exec, d); + // printf("as a ttaks args >0:%s + // %s\n\r",_fg->args[0].c_str(),_fg->json.c_str()); + res = executeBinary(_fg->args[0], exec->df.exe, exec->__run_handle_index, + exec, exec->args, _fg->json); + if (res.error) { + pushToConsole(res.error_message, true); + } + } else { + // printf("as a ttaks:%s\n\r",exec->df.json); + error_message_struct res = executeBinary("@__footer", exec->df.exe, + exec->__run_handle_index, exec, d); + res = executeBinary("@__main", exec->df.exe, exec->__run_handle_index, exec, + exec->args, exec->df.json); + if (res.error) { + pushToConsole(res.error_message, true); + } + } + pushToConsole("Execution done.", true); + // exec->__run_handle= NULL; + exec->_isRunning = false; + runningPrograms.removeHandle(exec->__run_handle_index); + isSyncalled = false; + vTaskDelete(NULL); #endif } Executable SCExecutable = Executable(); -void _executablesClass::kill(int handle_number) -{ - if (handle_number >= 0 and handle_number < _MAX_PROG_AT_ONCE) - { - if (execPtr[handle_number] != NULL) - { - execPtr[handle_number]->kill(); - } +void _executablesClass::kill(int handle_number) { + if (handle_number >= 0 and handle_number < _MAX_PROG_AT_ONCE) { + if (execPtr[handle_number] != NULL) { + execPtr[handle_number]->kill(); } + } } -uint32_t _executablesClass::getMask() -{ - - int mask = 0; - for (int i = 0; i < _MAX_PROG_AT_ONCE; i++) - { - if (__run_handles[i] != NULL) - { - if (execPtr[i]->isRunning()) - { - mask = mask | (1 << i); - } - } +uint32_t _executablesClass::getMask() { + + int mask = 0; + for (int i = 0; i < _MAX_PROG_AT_ONCE; i++) { + if (__run_handles[i] != NULL) { + if (execPtr[i]->isRunning()) { + mask = mask | (1 << i); + } } - return mask; + } + return mask; } -class _ScriptRuntime -{ +class _ScriptRuntime { public: - _ScriptRuntime() {} - void addExe(Executable df) - { - - if (df.name.size() > 0) - { - deleteExe(df.name); - _scExecutables.push_back(df); - } - else - { - pushToConsole("please add a name to the executable", true); - } - } - void addExe(Executable df, string name) - { - df.name = name; - addExe(df); + _ScriptRuntime() {} + void addExe(Executable df) { + + if (df.name.size() > 0) { + deleteExe(df.name); + _scExecutables.push_back(df); + } else { + pushToConsole("please add a name to the executable", true); } - Executable *findExecutable(string name) - { - for (int i = 0; i < _scExecutables.size(); i++) - { - if (_scExecutables[i].name.compare(name) == 0) - { - return &_scExecutables[i]; - } - } - pushToConsole(string_format("Executable %s not found", name.c_str()), true); - return NULL; + } + void addExe(Executable df, string name) { + df.name = name; + addExe(df); + } + Executable *findExecutable(string name) { + for (int i = 0; i < _scExecutables.size(); i++) { + if (_scExecutables[i].name.compare(name) == 0) { + return &_scExecutables[i]; + } } - void execute(string name) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + pushToConsole(string_format("Executable %s not found", name.c_str()), true); + return NULL; + } + void execute(string name) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->execute("main"); + exec->execute("main"); #endif - } } - void executeJ(string name, string json) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + } + void executeJ(string name, string json) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->execute("main",json); + exec->execute("main", json); #endif - } } - bool functionExist(string name, string function) - { + } + bool functionExist(string name, string function) { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - return exec->functionExist(function); + return exec->functionExist(function); #endif - } - return false; } - void execute(string name, string function) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + return false; + } + void execute(string name, string function) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->execute(function); + exec->execute(function); #endif - } } - - void execute(string name, string function,string json) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + } + + void execute(string name, string function, string json) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG -Arguments args; - exec->execute(function,json); + Arguments args; + exec->execute(function, json); #endif - } } - - void execute(string name, Arguments arguments) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + } + + void execute(string name, Arguments arguments) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->execute("main", arguments); + exec->execute("main", arguments); #endif - } } - void execute(string name, string function, Arguments arguments) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + } + void execute(string name, string function, Arguments arguments) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->execute(function, arguments); + exec->execute(function, arguments); #endif - } } + } - void executeAsTask(string name, Arguments arguments) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + void executeAsTask(string name, Arguments arguments) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->executeAsTask("main", arguments); + exec->executeAsTask("main", arguments); #endif - } } - void executeAsTask(string name) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + } + void executeAsTask(string name) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->executeAsTask("main"); + exec->executeAsTask("main"); #endif - } } - void executeAsTaskJ(string name,string json) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + } + void executeAsTaskJ(string name, string json) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->executeAsTask("main",json); + exec->executeAsTask("main", json); #endif - } } - void executeAsTask(string name, string function, Arguments arguments) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + } + void executeAsTask(string name, string function, Arguments arguments) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->executeAsTask(function, arguments); + exec->executeAsTask(function, arguments); #endif - } } - - void executeAsTask(string name, string function) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + } + + void executeAsTask(string name, string function) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->executeAsTask(function); + exec->executeAsTask(function); #endif - } - } - void executeAsTask(string name, int core, Arguments args) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + } + } + void executeAsTask(string name, int core, Arguments args) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->executeAsTask("main", core, args); + exec->executeAsTask("main", core, args); #endif - } } - void executeAsTask(string name, int core) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + } + void executeAsTask(string name, int core) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->executeAsTask("main", core); + exec->executeAsTask("main", core); #endif - } } - void kill(string name) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + } + void kill(string name) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - if (exec->isRunning()) - exec->kill(); + if (exec->isRunning()) + exec->kill(); #endif - } } + } - void deleteExe(string name) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { - kill(name); - free(name); - for (vector::iterator it = _scExecutables.begin(); it != _scExecutables.end(); it++) - { - if ((*it).name.compare(name) == 0) - { - // pushToConsole("get it"); - _scExecutables.erase(it); - // pushToConsole("get it2"); - return; - } - } + void deleteExe(string name) { + Executable *exec = findExecutable(name); + if (exec != NULL) { + kill(name); + free(name); + for (vector::iterator it = _scExecutables.begin(); + it != _scExecutables.end(); it++) { + if ((*it).name.compare(name) == 0) { + // pushToConsole("get it"); + _scExecutables.erase(it); + // pushToConsole("get it2"); + return; } + } } - void free(string name) - { - Executable *exec = findExecutable(name); - if (exec != NULL) - { + } + void free(string name) { + Executable *exec = findExecutable(name); + if (exec != NULL) { #ifndef __TEST_DEBUG - exec->free(); + exec->free(); #endif - } } + } - void listExec() - { - for (int i = 0; i < _scExecutables.size(); i++) - { + void listExec() { + for (int i = 0; i < _scExecutables.size(); i++) { - pushToConsole(string_format(" %2d | %20s isRunning:%d| %6d | %6d |%s", i + 1, _scExecutables[i].name.c_str(), _scExecutables[i].isRunning(), _scExecutables[i]._executecmd.binary_size, _scExecutables[i]._executecmd.data_size, _scExecutables[i].error.error_message.c_str()), true); - } + pushToConsole( + string_format(" %2d | %20s isRunning:%d| %6d | %6d |%s", i + 1, + _scExecutables[i].name.c_str(), + _scExecutables[i].isRunning(), + _scExecutables[i]._executecmd.binary_size, + _scExecutables[i]._executecmd.data_size, + _scExecutables[i].error.error_message.c_str()), + true); } + } - vector getListExecutables() - { - vector res; - for (int i = 0; i < _scExecutables.size(); i++) - { - res.push_back(getExecutableInfo(i)); - } - return res; + vector getListExecutables() { + vector res; + for (int i = 0; i < _scExecutables.size(); i++) { + res.push_back(getExecutableInfo(i)); } - void killAndFreeRunningProgram() - { - for (int i = 0; i < _scExecutables.size(); i++) - { - if (_scExecutables[i].isRunning()) - { - kill(_scExecutables[i].name); - free(_scExecutables[i].name); - deleteExe(_scExecutables[i].name); - } - } + return res; + } + void killAndFreeRunningProgram() { + for (int i = 0; i < _scExecutables.size(); i++) { + if (_scExecutables[i].isRunning()) { + kill(_scExecutables[i].name); + free(_scExecutables[i].name); + deleteExe(_scExecutables[i].name); + } } - exe_info getExecutableInfo(string name) - { - Executable *f = findExecutable(name); - - exe_info inf; - if (f != NULL) - { - inf.data_size = f->_executecmd.data_size; - inf.binary_size = f->_executecmd.binary_size; - inf.total_size = f->_executecmd.data_size + f->_executecmd.binary_size; - inf.isRunning = f->_isRunning; - inf.name = f->name; - } - return inf; - } - exe_info getExecutableInfo(int pos) - { - exe_info inf; - if (pos < 0 or pos >= _scExecutables.size()) - { - return inf; - } - Executable *f = &_scExecutables[pos]; - if (f != NULL) - { - inf.data_size = f->_executecmd.data_size; - inf.binary_size = f->_executecmd.binary_size; - inf.total_size = f->_executecmd.data_size + f->_executecmd.binary_size; - inf.isRunning = f->_isRunning; - inf.name = f->name; - } - return inf; + } + exe_info getExecutableInfo(string name) { + Executable *f = findExecutable(name); + + exe_info inf; + if (f != NULL) { + inf.data_size = f->_executecmd.data_size; + inf.binary_size = f->_executecmd.binary_size; + inf.total_size = f->_executecmd.data_size + f->_executecmd.binary_size; + inf.isRunning = f->_isRunning; + inf.name = f->name; } - vector _scExecutables; + return inf; + } + exe_info getExecutableInfo(int pos) { + exe_info inf; + if (pos < 0 or pos >= _scExecutables.size()) { + return inf; + } + Executable *f = &_scExecutables[pos]; + if (f != NULL) { + inf.data_size = f->_executecmd.data_size; + inf.binary_size = f->_executecmd.binary_size; + inf.total_size = f->_executecmd.data_size + f->_executecmd.binary_size; + inf.isRunning = f->_isRunning; + inf.name = f->name; + } + return inf; + } + vector _scExecutables; }; _ScriptRuntime scriptRuntime; diff --git a/src/execute_asm.h b/src/execute_asm.h index 215e5fc..fea2091 100644 --- a/src/execute_asm.h +++ b/src/execute_asm.h @@ -9,14 +9,12 @@ #ifndef __TEST_DEBUG #ifdef __JSON__OPTION__ -JsonVariant getfromJson(JsonDocument obj, string str) -{ +JsonVariant getfromJson(JsonDocument obj, string str) { vector tile; JsonVariant jso = obj; tile = split(str, "."); if (tile.size() >= 1) - for (int i = 0; i < tile.size() - 1; i++) - { + for (int i = 0; i < tile.size() - 1; i++) { // rintf("d:%s\n",tile[i]); jso = jso[tile[i].c_str()]; } @@ -28,8 +26,7 @@ JsonVariant getfromJson(JsonDocument obj, string str) #endif #endif -void freeBinary(Binary *bin) -{ +void freeBinary(Binary *bin) { #ifndef __TEST_DEBUG if (bin->binary_data) heap_caps_free(bin->binary_data); @@ -41,8 +38,9 @@ void freeBinary(Binary *bin) #endif } -error_message_struct decodeBinaryHeader(uint8_t *exec, uint8_t *binary_header, uint8_t *_binary_data, uint32_t _exec, executable *finalexe, int offset) -{ +error_message_struct decodeBinaryHeader(uint8_t *exec, uint8_t *binary_header, + uint8_t *_binary_data, uint32_t _exec, + executable *finalexe, int offset) { error_message_struct error; uint16_t nb_data = 0; uint16_t tmp_data = 0; @@ -63,15 +61,12 @@ error_message_struct decodeBinaryHeader(uint8_t *exec, uint8_t *binary_header, u memcpy(&nb_objects, binary_header, 2); binary_header = binary_header + 2; // printf("nb objecst %u\n", nb_objects); - for (int i = 0; i < nb_objects; i++) - { + for (int i = 0; i < nb_objects; i++) { memcpy(&type, binary_header, 1); binary_header++; // printf("type %d\r\n", type); - switch (type) - { - case 0: - { + switch (type) { + case 0: { memcpy(&bincode, binary_header, 4); binary_header = binary_header + 4; memcpy(&nb_data, binary_header, 2); @@ -80,10 +75,8 @@ error_message_struct decodeBinaryHeader(uint8_t *exec, uint8_t *binary_header, u uint32_t *new_adr = (uint32_t *)exec + nb_data; memcpy(new_adr, &content, 4); - } - break; - case 1: - { + } break; + case 1: { memcpy(&text_size, binary_header, 2); binary_header = binary_header + 2; textptr = (char *)binary_header; @@ -91,8 +84,7 @@ error_message_struct decodeBinaryHeader(uint8_t *exec, uint8_t *binary_header, u memcpy(&nb_data, binary_header, 2); binary_header = binary_header + 2; int index = findLink(string(textptr).substr(6, 100), externalType::value); - if (index > -1) - { + if (index > -1) { uint32_t content; uint32_t *new_adr; #ifndef __TEST_DEBUG @@ -102,17 +94,14 @@ error_message_struct decodeBinaryHeader(uint8_t *exec, uint8_t *binary_header, u memcpy(new_adr, &content, 4); #endif // printf("external var:%s\n\r", textptr); - } - else - { + } else { error.error = 1; // printf("b ou foune\r\n"); - error.error_message += string_format("External variable %s not found\n\r", textptr); + error.error_message += + string_format("External variable %s not found\n\r", textptr); } - } - break; - case 2: - { + } break; + case 2: { memcpy(&text_size, binary_header, 2); binary_header = binary_header + 2; textptr = (char *)binary_header; @@ -123,14 +112,15 @@ error_message_struct decodeBinaryHeader(uint8_t *exec, uint8_t *binary_header, u // binary_header = binary_header + 4; memcpy(&nb_data, binary_header, 2); binary_header = binary_header + 2; - int index = findLink(string(textptr).substr(2, 100), externalType::function); - if (index > -1) - { + int index = + findLink(string(textptr).substr(2, 100), externalType::function); + if (index > -1) { // printf("calculate ext %s\n\r", (*it)->getText()); /* - bincode = jump_call8(bincode, _address + (uint32_t)_exec, (uint32_t)external_links[index].ptr); - memcpy(exec + _address, &bincode, 3); + bincode = jump_call8(bincode, _address + (uint32_t)_exec, + (uint32_t)external_links[index].ptr); memcpy(exec + _address, &bincode, + 3); */ #ifndef __TEST_DEBUG uint32_t content = (uint32_t)((external_links[index].ptr)); @@ -143,17 +133,14 @@ error_message_struct decodeBinaryHeader(uint8_t *exec, uint8_t *binary_header, u memcpy(new_adr, &content, 4); // printf("external func:%s\n\r", textptr); - } - else - { + } else { error.error = 1; // printf("b ou foune\r\n"); - error.error_message += string_format("external function %s not found\n\r", textptr); + error.error_message += + string_format("external function %s not found\n\r", textptr); } - } - break; - case 3: - { + } break; + case 3: { memcpy(&_address, binary_header, 4); binary_header = binary_header + 4; memcpy(&tmp_data, binary_header, 2); @@ -161,11 +148,9 @@ error_message_struct decodeBinaryHeader(uint8_t *exec, uint8_t *binary_header, u memcpy(&size, binary_header, 2); binary_header = binary_header + 2; memcpy(_binary_data + _address, exec + offset + tmp_data, size); - } - break; + } break; - case 4: - { + case 4: { globalcall gc; memcpy(&text_size, binary_header, 2); binary_header = binary_header + 2; @@ -188,11 +173,9 @@ error_message_struct decodeBinaryHeader(uint8_t *exec, uint8_t *binary_header, u gc.variableaddress = _address; // printf("funcrion %s adfrees:%d\n\r",gc.name .c_str(),gc.address); finalexe->functions.push_back(gc); - } - break; - #ifdef __JSON__OPTION__ - case 5: - { + } break; +#ifdef __JSON__OPTION__ + case 5: { jsonVariable jso; memcpy(&text_size, binary_header, 2); binary_header = binary_header + 2; @@ -207,10 +190,10 @@ error_message_struct decodeBinaryHeader(uint8_t *exec, uint8_t *binary_header, u jso.type = type; binary_header = binary_header + 1; finalexe->jsonVars.push_back(jso); - // printf("found %s %d %s\n",jso.json.c_str(),jso.address,varTypeEnumNames[jso.type].c_str()); - } - break; - #endif + // printf("found %s %d + // %s\n",jso.json.c_str(),jso.address,varTypeEnumNames[jso.type].c_str()); + } break; +#endif default: error.error = 1; error.error_message = "Unknown type binary header invalid"; @@ -221,15 +204,16 @@ error_message_struct decodeBinaryHeader(uint8_t *exec, uint8_t *binary_header, u return error; } -executable _createExcutablefromBinary(Binary *bin) -{ +executable _createExcutablefromBinary(Binary *bin) { executable exe; error_message_struct error; exe.error.error = 0; -// printf("on cree un executbale de taiile %d et data %d \n",bin->instruction_size,bin->data_size); +// printf("on cree un executbale de taiile %d et data %d +// \n",bin->instruction_size,bin->data_size); #ifndef __TEST_DEBUG - uint32_t *exec = (uint32_t *)heap_caps_malloc(bin->instruction_size, MALLOC_CAP_EXEC); + uint32_t *exec = + (uint32_t *)heap_caps_malloc(bin->instruction_size, MALLOC_CAP_EXEC); #else uint32_t *exec = (uint32_t *)malloc(bin->instruction_size); #endif @@ -237,19 +221,19 @@ executable _createExcutablefromBinary(Binary *bin) uint8_t *_binary_data = (uint8_t *)malloc(bin->data_size); #ifndef __TEST_DEBUG - error = decodeBinaryHeader(bin->binary_data, bin->function_data, _binary_data, (uint32_t)exec, &exe, bin->instruction_size); + error = decodeBinaryHeader(bin->binary_data, bin->function_data, _binary_data, + (uint32_t)exec, &exe, bin->instruction_size); #else - error = decodeBinaryHeader(bin->binary_data, bin->function_data, _binary_data, 0, &exe, bin->instruction_size); + error = decodeBinaryHeader(bin->binary_data, bin->function_data, _binary_data, + 0, &exe, bin->instruction_size); #endif - if (exe.functions.size() == 0) - { + if (exe.functions.size() == 0) { exe.error.error = 1; exe.error.error_message = "No global start function found"; return exe; } - if (error.error == 1) - { + if (error.error == 1) { exe.error = error; // _asm_parsed.clear(); @@ -261,8 +245,7 @@ executable _createExcutablefromBinary(Binary *bin) // free(tmp_exec); - for (int i = 0; i < exe.functions.size(); i++) - { + for (int i = 0; i < exe.functions.size(); i++) { exe.functions[i].address = (uint32_t)((exe.functions[i].address) / 4); } @@ -273,9 +256,9 @@ executable _createExcutablefromBinary(Binary *bin) return exe; } -void displayBinary(Binary *bin) -{ - printf("char _bin[%d]={\n", 19 + 8 + bin->tmp_instruction_size + bin->function_size); +void displayBinary(Binary *bin) { + printf("char _bin[%d]={\n", + 19 + 8 + bin->tmp_instruction_size + bin->function_size); printf("E,S,P,L,i,v,e,S,c,r,i,p,t,1,.,0,.,1,0x0"); // char g[]="ESPLiveScript1.0.1"; @@ -287,15 +270,13 @@ void displayBinary(Binary *bin) printf(",0x%x", bin->data_size / 256); printf(",0x%x", bin->function_size & 0xff); printf(",0x%x", bin->function_size / 256); - for (int i = 0; i < bin->tmp_instruction_size; i++) - { + for (int i = 0; i < bin->tmp_instruction_size; i++) { if (*(bin->binary_data + i) >= 32 and *(bin->binary_data + i) < 125) printf(",%c", *(bin->binary_data + i)); else printf(",0x%x", *(bin->binary_data + i)); } - for (int i = 0; i < bin->function_size; i++) - { + for (int i = 0; i < bin->function_size; i++) { if (*(bin->function_data + i) >= 32 and *(bin->function_data + i) < 125) printf(",%c", *(bin->function_data + i)); else @@ -304,15 +285,14 @@ void displayBinary(Binary *bin) printf("}\n"); } -void binaryFromArray(char *_array, Binary *bin) -{ +void binaryFromArray(char *_array, Binary *bin) { bin->error.error = 0; char ver[19]; char *arr = _array; - if (strncmp(_array, "ESPLiveScript1.0.1", 18) != 0) - { + if (strncmp(_array, "ESPLiveScript1.0.1", 18) != 0) { bin->error.error = 1; - bin->error.error_message = string_format("wrong format expected ESPLiveScript1.0.1 got %s", (const char *)ver); + bin->error.error_message = string_format( + "wrong format expected ESPLiveScript1.0.1 got %s", (const char *)ver); return; } arr += 19; @@ -333,17 +313,16 @@ void binaryFromArray(char *_array, Binary *bin) memcpy(tmp2, arr, bin->function_size); } #ifndef __TEST_DEBUG -void loadBinary(char *name, fs::FS &fs, Binary *bin) -{ +void loadBinary(char *name, fs::FS &fs, Binary *bin) { File root = fs.open(name); bin->error.error = 0; char ver[19]; root.read((uint8_t *)ver, 19); - if (strcmp(ver, "ESPLiveScript1.0.1") != 0) - { + if (strcmp(ver, "ESPLiveScript1.0.1") != 0) { bin->error.error = 1; - bin->error.error_message = string_format("wrong format expected ESPLiveScript1.0.1 got %s", (const char *)ver); + bin->error.error_message = string_format( + "wrong format expected ESPLiveScript1.0.1 got %s", (const char *)ver); return; } @@ -369,8 +348,7 @@ void loadBinary(char *name, fs::FS &fs, Binary *bin) root.close(); } #endif -executable createExectutable(Binary *bin) -{ +executable createExectutable(Binary *bin) { executable exec; exec.binary_size = 0; @@ -392,28 +370,26 @@ void executeBinaryAsm(uint32_t *j) //, uint32_t *c) asm volatile( //"l32i a10,%1,0\n\t" "l32i a15,%0,0\n\t" "callx8 a15" - : : "r"(j) //, "r"(c) + : + : "r"(j) //, "r"(c) :); #endif } -#ifdef __JSON__OPTION__ +#ifdef __JSON__OPTION__ -error_message_struct updateParameters(executable ex, string json) -{ +error_message_struct updateParameters(executable ex, string json) { // uint32_t toexecute; // printf("execut |%s|\n", json.c_str()); error_message_struct res; - if (json != "") - { + if (json != "") { #ifndef __TEST_DEBUG JsonDocument doc; DeserializationError error = deserializeJson(doc, json); res.error = 0; // Test if parsing succeeds. - if (error) - { + if (error) { Serial.print(F("deserializeJson() failed: ")); Serial.println(error.f_str()); res.error = 1; @@ -422,78 +398,68 @@ error_message_struct updateParameters(executable ex, string json) } uint8_t *var = ex.data; - for (int i = 0; i < ex.jsonVars.size(); i++) - { + for (int i = 0; i < ex.jsonVars.size(); i++) { // printf(" taking:%s\n",ex.jsonVars[i].json.c_str()); JsonVariant p = getfromJson(doc, ex.jsonVars[i].json); - switch (ex.jsonVars[i].type) - { - case __uint32_t__: - { + switch (ex.jsonVars[i].type) { + case __uint32_t__: { uint32_t jk = (uint32_t)p; memcpy(var + ex.jsonVars[i].address, &jk, 4); - } - break; + } break; - case __int__: - { + case __int__: { int jk = (int)p; - // printf(" taking:%s %d %d\n",ex.jsonVars[i].json.c_str(),jk,ex.jsonVars[i].address); + // printf(" taking:%s %d + // %d\n",ex.jsonVars[i].json.c_str(),jk,ex.jsonVars[i].address); memcpy((uint8_t *)(ex.data + ex.jsonVars[i].address), &jk, 4); - } - break; - case __float__: - { + } break; + case __float__: { float jk = (float)p; memcpy(var + ex.jsonVars[i].address, &jk, 4); - } - break; - case __char__: - { + } break; + case __char__: { string jk = p.as(); memcpy(var + ex.jsonVars[i].address, jk.c_str(), jk.size()); - } - break; + } break; default: break; } } #endif - } - else - { - //printf("no json\n"); + } else { + // printf("no json\n"); } return res; } #endif -error_message_struct executeBinary(string function, executable ex, uint32_t handle, void *exePtr, Arguments arguments, string json) -{ +error_message_struct executeBinary(string function, executable ex, + uint32_t handle, void *exePtr, + Arguments arguments, string json) { error_message_struct res; - // printf("executvith %s %s\n",function.c_str(), json.c_str()); - #ifdef __JSON__OPTION__ +// printf("executvith %s %s\n",function.c_str(), json.c_str()); +#ifdef __JSON__OPTION__ res = updateParameters(ex, json); if (res.error == 1) return res; #endif - for (int i = 0; i < ex.functions.size(); i++) - { + for (int i = 0; i < ex.functions.size(); i++) { string ftofind = ex.functions[i].name; - if (ex.functions[i].name.find_first_of("(") != string::npos) - { - ftofind = ex.functions[i].name.substr(0, ex.functions[i].name.find_first_of("(")); + if (ex.functions[i].name.find_first_of("(") != string::npos) { + ftofind = ex.functions[i].name.substr( + 0, ex.functions[i].name.find_first_of("(")); } // printf("coparing %s\n",ftofind.c_str()); - if (ftofind.compare(function) == 0) - { -// printf("address of function %s :%x\n",ex.functions[i].name.c_str(), ex.functions[i].address); + if (ftofind.compare(function) == 0) { +// printf("address of function %s :%x\n",ex.functions[i].name.c_str(), +// ex.functions[i].address); // #ifndef __TEST_DEBUG - ex.functions[i].address = (uint32_t)(ex.start_program + ex.functions[i].address); + ex.functions[i].address = + (uint32_t)(ex.start_program + ex.functions[i].address); #endif uint32_t *t = (uint32_t *)ex.data; // t++; @@ -506,11 +472,9 @@ error_message_struct executeBinary(string function, executable ex, uint32_t hand #endif // printf("exx %x\n",(uint32_t)exePtr); uint8_t *var = (ex.data + ex.functions[i].variableaddress); - if (ex.functions[i].args_num == arguments._args.size()) - { + if (ex.functions[i].args_num == arguments._args.size()) { vector args = split(trim(ex.functions[i].variables), " "); - for (int i = 1; i < args.size(); i++) - { + for (int i = 1; i < args.size(); i++) { int _size = 0; sscanf(args[i].c_str(), "%d", &_size); if (arguments._args[i - 1].vartype == __float__) @@ -519,17 +483,18 @@ error_message_struct executeBinary(string function, executable ex, uint32_t hand memcpy(var, &arguments._args[i - 1].intval, _size); var += _size; } - } - else - { + } else { res.error = 1; - res.error_message = string_format("Expected %d arguments got %d\n", ex.functions[i].args_num, arguments.size()); + res.error_message = + string_format("Expected %d arguments got %d\n", + ex.functions[i].args_num, arguments.size()); return res; } executeBinaryAsm(&ex.functions[i].address); //, &ex.links); - // printf("address of function %s :%x\n",ex.functions[i].name.c_str(), toexecute); + // printf("address of function %s :%x\n",ex.functions[i].name.c_str(), + // toexecute); // executeBinaryAsm(&toexecute, &ex.links); // freeBinary(ex); @@ -537,32 +502,30 @@ error_message_struct executeBinary(string function, executable ex, uint32_t hand } } res.error = 1; - res.error_message = string_format("Immpossible to execute %s: not found\n", function.c_str()); + res.error_message = + string_format("Immpossible to execute %s: not found\n", function.c_str()); return res; } -error_message_struct executeBinary(string function, executable ex, uint32_t handle, void *exePtr, Arguments arguments) -{ +error_message_struct executeBinary(string function, executable ex, + uint32_t handle, void *exePtr, + Arguments arguments) { return executeBinary(function, ex, handle, exePtr, arguments, ""); } -error_message_struct executeBinary(executable ex, uint32_t handle) -{ +error_message_struct executeBinary(executable ex, uint32_t handle) { Arguments args; return executeBinary(ex.functions[0].name, ex, handle, NULL, args); } -void freeExecutable(executable *ex) -{ +void freeExecutable(executable *ex) { #ifndef __TEST_DEBUG - if (ex->start_program != NULL) - { + if (ex->start_program != NULL) { heap_caps_aligned_free(ex->start_program); } ex->start_program = NULL; - if (ex->data != NULL) - { + if (ex->data != NULL) { heap_caps_free(ex->data); } @@ -572,25 +535,23 @@ void freeExecutable(executable *ex) // tmp_exec = NULL; } -void artiPrintf(char const *format, ...) -{ +void artiPrintf(char const *format, ...) { va_list argp; va_start(argp, format); vprintf(format, argp); // printf("\r\n"); va_end(argp); } -void artiPrintfln(char const *format, ...) -{ +void artiPrintfln(char const *format, ...) { va_list argp; va_start(argp, format); vprintf(format, argp); printf("\r\n"); va_end(argp); } -void showError(int line, uint32_t size, uint32_t got) -{ - pushToConsole(string_format("Overflow error max size: %d got %d", size, got), true); +void showError(int line, uint32_t size, uint32_t got) { + pushToConsole(string_format("Overflow error max size: %d got %d", size, got), + true); } #ifdef USE_FASTLED @@ -600,8 +561,7 @@ void showError(int line, uint32_t size, uint32_t got) #define K170 170 #define K85 85 -CRGB hsv(uint8_t hue, uint8_t sat, uint8_t val) -{ +CRGB hsv(uint8_t hue, uint8_t sat, uint8_t val) { // Yellow has a higher inherent brightness than // any other color; 'pure' yellow is perceived to // be 93% as bright as white. In order to make @@ -649,35 +609,28 @@ CRGB hsv(uint8_t hue, uint8_t sat, uint8_t val) uint8_t r, g, b; - if (!(hue & 0x80)) - { + if (!(hue & 0x80)) { // 0XX - if (!(hue & 0x40)) - { + if (!(hue & 0x40)) { // 00X // section 0-1 - if (!(hue & 0x20)) - { + if (!(hue & 0x20)) { // 000 // case 0: // R -> O r = K255 - third; g = third; b = 0; FORCE_REFERENCE(b); - } - else - { + } else { // 001 // case 1: // O -> Y - if (Y1) - { + if (Y1) { r = K171; g = K85 + third; b = 0; FORCE_REFERENCE(b); } - if (Y2) - { + if (Y2) { r = K170 + third; // uint8_t twothirds = (third << 1); uint8_t twothirds = scale8(offset8, ((256 * 2) / 3)); // max=170 @@ -686,17 +639,13 @@ CRGB hsv(uint8_t hue, uint8_t sat, uint8_t val) FORCE_REFERENCE(b); } } - } - else - { + } else { // 01X // section 2-3 - if (!(hue & 0x20)) - { + if (!(hue & 0x20)) { // 010 // case 2: // Y -> G - if (Y1) - { + if (Y1) { // uint8_t twothirds = (third << 1); uint8_t twothirds = scale8(offset8, ((256 * 2) / 3)); // max=170 r = K171 - twothirds; @@ -704,16 +653,13 @@ CRGB hsv(uint8_t hue, uint8_t sat, uint8_t val) b = 0; FORCE_REFERENCE(b); } - if (Y2) - { + if (Y2) { r = K255 - offset8; g = K255; b = 0; FORCE_REFERENCE(b); } - } - else - { + } else { // 011 // case 3: // G -> A r = 0; @@ -722,16 +668,12 @@ CRGB hsv(uint8_t hue, uint8_t sat, uint8_t val) b = third; } } - } - else - { + } else { // section 4-7 // 1XX - if (!(hue & 0x40)) - { + if (!(hue & 0x40)) { // 10X - if (!(hue & 0x20)) - { + if (!(hue & 0x20)) { // 100 // case 4: // A -> B r = 0; @@ -740,9 +682,7 @@ CRGB hsv(uint8_t hue, uint8_t sat, uint8_t val) uint8_t twothirds = scale8(offset8, ((256 * 2) / 3)); // max=170 g = K171 - twothirds; // K170? b = K85 + twothirds; - } - else - { + } else { // 101 // case 5: // B -> P r = third; @@ -750,20 +690,15 @@ CRGB hsv(uint8_t hue, uint8_t sat, uint8_t val) FORCE_REFERENCE(g); b = K255 - third; } - } - else - { - if (!(hue & 0x20)) - { + } else { + if (!(hue & 0x20)) { // 110 // case 6: // P -- K r = K85 + third; g = 0; FORCE_REFERENCE(g); b = K171 - third; - } - else - { + } else { // 111 // case 7: // K -> R r = K170 + third; @@ -783,16 +718,12 @@ CRGB hsv(uint8_t hue, uint8_t sat, uint8_t val) // Scale down colors if we're desaturated at all // and add the brightness_floor to r, g, and b. - if (sat != 255) - { - if (sat == 0) - { + if (sat != 255) { + if (sat == 0) { r = 255; b = 255; g = 255; - } - else - { + } else { uint8_t desat = 255 - sat; desat = scale8_video(desat, desat); @@ -821,18 +752,14 @@ CRGB hsv(uint8_t hue, uint8_t sat, uint8_t val) } // Now scale everything down if we're at value < 255. - if (val != 255) - { + if (val != 255) { val = scale8_video_LEAVING_R1_DIRTY(val, val); - if (val == 0) - { + if (val == 0) { r = 0; g = 0; b = 0; - } - else - { + } else { // nscale8x3_video( r, g, b, val); #if (FASTLED_SCALE8_FIXED == 1) r = scale8_LEAVING_R1_DIRTY(r, val); @@ -860,14 +787,14 @@ CRGB hsv(uint8_t hue, uint8_t sat, uint8_t val) } #endif -class INIT_PARSER -{ +class INIT_PARSER { public: - INIT_PARSER() - { + INIT_PARSER() { addExternalFunction("printf", "void", "char *,Args", (void *)artiPrintf); - addExternalFunction("printfln", "void", "char *,Args", (void *)artiPrintfln); - addExternalFunction("error", "void", "int,uint32_t,uint32_t", (void *)&showError); + addExternalFunction("printfln", "void", "char *,Args", + (void *)artiPrintfln); + addExternalFunction("error", "void", "int,uint32_t,uint32_t", + (void *)&showError); #ifdef USE_FASTLED addExternalFunction("hsv", "CRGB", "int,int,int", (void *)hsv); #endif diff --git a/src/functionlib.h b/src/functionlib.h index 94eb0cd..decd234 100644 --- a/src/functionlib.h +++ b/src/functionlib.h @@ -5,8 +5,8 @@ #define __FUNCTION_LIB // vector add_on; -//string division=""; -string _sync="\ +// string division=""; +string _sync = "\ uint32_t _handle_;\n\ uint32_t _execaddr_;\n\ __ASM__ void sync()\n\ @@ -17,7 +17,7 @@ __ASM__ void sync()\n\ \"callExt a8,@__sync\"\n\ \"retw.n\" \n\ }@"; -string division="\ +string division = "\ __ASM__ float __div(float a,float b)\n\ { \n\ \"entry a1,16\" \n\ @@ -48,40 +48,24 @@ __ASM__ float __div(float a,float b)\n\ \"divn.s f0, f2, f6\"\n\ \"retw.n\"\n\ }@"; -char * _div[]={ -"@___div(d|d):", -"entry a1,16", -"div0.s f3, f2", -"nexp01.s f4, f2", -"const.s f5, 1", -"maddn.s f5, f4, f3", -"mov.s f6, f3", -"mov.s f7, f2", -"nexp01.s f2, f1", -"maddn.s f6, f5, f6", -"const.s f5, 1", -"const.s f0, 0", -"neg.s f8, f2", -"maddn.s f5, f4, f6", -"maddn.s f0, f8, f3", -"mkdadj.s f7, f1", -"maddn.s f6, f5, f6", -"maddn.s f8, f4, f0", -"const.s f3, 1", -"maddn.s f3, f4, f6", -"maddn.s f0, f8, f6", -"neg.s f2, f2", -"maddn.s f6, f3, f6", -"maddn.s f2, f4, f0", -"addexpm.s f0, f7", -"addexp.s f6, f7", -"divn.s f0, f2, f6", -"retw.n" -}; -int _div_size=28; +char *_div[] = {"@___div(d|d):", "entry a1,16", + "div0.s f3, f2", "nexp01.s f4, f2", + "const.s f5, 1", "maddn.s f5, f4, f3", + "mov.s f6, f3", "mov.s f7, f2", + "nexp01.s f2, f1", "maddn.s f6, f5, f6", + "const.s f5, 1", "const.s f0, 0", + "neg.s f8, f2", "maddn.s f5, f4, f6", + "maddn.s f0, f8, f3", "mkdadj.s f7, f1", + "maddn.s f6, f5, f6", "maddn.s f8, f4, f0", + "const.s f3, 1", "maddn.s f3, f4, f6", + "maddn.s f0, f8, f6", "neg.s f2, f2", + "maddn.s f6, f3, f6", "maddn.s f2, f4, f0", + "addexpm.s f0, f7", "addexp.s f6, f7", + "divn.s f0, f2, f6", "retw.n"}; +int _div_size = 28; -#if _TRIGGER ==0 -string _rand="\ +#if _TRIGGER == 0 +string _rand = "\ __ASM__ uint32_t rand(uint32_t mod) \n\ {\n\ \"entry a1,56\" \n\ @@ -100,7 +84,7 @@ __ASM__ uint32_t rand(uint32_t mod) \n\ \"retw.n\" \n\ }@"; #else -string _rand="\__ASM__ uint32_t rand(uint32_t mod) \n\ +string _rand = "\__ASM__ uint32_t rand(uint32_t mod) \n\ {\n\ \"entry a1,56\" \n\ //\"l32r a4,@_stack_rand(d)\" \n\ @@ -118,7 +102,7 @@ string _rand="\__ASM__ uint32_t rand(uint32_t mod) \n\ \"retw.n\" \n\ }@"; #endif -string _copycode="\ +string _copycode = "\ __ASM__ void copy(uint8_t *dest,uint8_t *from,uint16_t size) \n\ { \n\ \"entry a1,80\"\n\ @@ -135,7 +119,7 @@ __ASM__ void copy(uint8_t *dest,uint8_t *from,uint16_t size) \n\ \"bnez a7,loop\" \n\ \"retw.n\" \n\ }@"; -string _memset="\ +string _memset = "\ __ASM__ void memset(uint8_t *obj,uint8_t val, uint16_t size )\n\ {\n\ \"entry a1,80\" \n\ @@ -151,7 +135,7 @@ __ASM__ void memset(uint8_t *obj,uint8_t val, uint16_t size )\n\ \"retw.n\" \n\ }@"; -string _millis="\ +string _millis = "\ __ASM__ uint32_t millis()\n\ {\n\ \"rsr a14,234\" \n\ @@ -159,7 +143,7 @@ __ASM__ uint32_t millis()\n\ }\n\ @"; -string _fill="\ +string _fill = "\ __ASM__ void fill(uint8_t *dest, uint8_t *obj, uint8_t objsize,uint16_t nb_iteration) \n\ {\n\ \"entry a1,80\" \n\ @@ -183,7 +167,7 @@ __ASM__ void fill(uint8_t *dest, uint8_t *obj, uint8_t objsize,uint16_t nb_itera \"retw.n\" \n\ }@"; -string _arduino="\n\ +string _arduino = "\n\ void main(){\n\ setup();\n\ while(2>1)\n\ @@ -192,28 +176,26 @@ loop();\n\ }\n\ }\n\ "; -string base_ext_functions="\n\ +string base_ext_functions = "\n\ #define true 1\n\ #define false 0\n\ @"; -//external void printfln(char * s,Args a);\n -string empty_header=""; -int stdlib_size=5; -string stdlib[]={"sync","rand","copy","memset","fill","arduino"}; - string * _stdlib[]={&_sync,&_rand,&_copycode,&_memset,&_fill,&_arduino}; -string * _stdlib_header[]={&empty_header,&empty_header,&empty_header,&empty_header,&empty_header}; +// external void printfln(char * s,Args a);\n +string empty_header = ""; +int stdlib_size = 5; +string stdlib[] = {"sync", "rand", "copy", "memset", "fill", "arduino"}; +string *_stdlib[] = {&_sync, &_rand, &_copycode, &_memset, &_fill, &_arduino}; +string *_stdlib_header[] = {&empty_header, &empty_header, &empty_header, + &empty_header, &empty_header}; -int findLibFunction(string name) -{ - //int pos=-1; - for(int i=0;i -using namespace std; -#include -#include +using namespace std; #include +#include +#include #include -//#include -//#include -//#include - -vector split(string s, string delimiter) - { - vector res; - int pos = 0 - ; - pos = s.find(delimiter); - while(pos >=0 ){ - pos = s.find(delimiter); - res.push_back(s.substr(0,pos)); - s.erase(0,pos+delimiter.size()); - pos = s.find(delimiter); - } - if(s.size()>0) - { - res.push_back(s); - } - return res; - } - - +// #include +// #include +// #include + +vector split(string s, string delimiter) { + vector res; + int pos = 0; + pos = s.find(delimiter); + while (pos >= 0) { + pos = s.find(delimiter); + res.push_back(s.substr(0, pos)); + s.erase(0, pos + delimiter.size()); + pos = s.find(delimiter); + } + if (s.size() > 0) { + res.push_back(s); + } + return res; +} -template -std::string string_format( const std::string& format, Args ... args ) +template +std::string string_format(const std::string &format, Args... args) { - int size_s = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0' - if( size_s <= 0 ){ return ""; } - auto size = static_cast( size_s ); - std::unique_ptr buf( new char[ size ] ); - std::snprintf( buf.get(), size, format.c_str(), args ... ); - return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside - + int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + + 1; // Extra space for '\0' + if (size_s <= 0) { + return ""; + } + auto size = static_cast(size_s); + std::unique_ptr buf(new char[size]); + std::snprintf(buf.get(), size, format.c_str(), args...); + return std::string(buf.get(), + buf.get() + size - 1); // We don't want the '\0' inside } - - std::string trim(std::string s) { - if(s.find("//")!=-1) - { - s=s.substr(0,s.find("//")).c_str(); + if (s.find("//") != -1) { + s = s.substr(0, s.find("//")).c_str(); } - s.erase(s.find_last_not_of(" \n\r\t")+1); - - return s.replace(0,s.find_first_not_of(" \n\r\t"),""); -} + s.erase(s.find_last_not_of(" \n\r\t") + 1); + return s.replace(0, s.find_first_not_of(" \n\r\t"), ""); +} #endif diff --git a/src/tokenizer.h b/src/tokenizer.h index 37261c8..5bcb06d 100644 --- a/src/tokenizer.h +++ b/src/tokenizer.h @@ -1,15 +1,15 @@ #include #include -//#include -#include +// #include #include "memory.h" +#include #include #pragma once using namespace std; // #include "asm_parser.h" -#include "string_function.h" #include "asm_struct_enum.h" +#include "string_function.h" #define EOF_TEXT 0 @@ -19,43 +19,43 @@ template std::string string_format(const std::string &format, Args... args) { - int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0' - if (size_s <= 0) + int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // +Extra space for '\0' if (size_s <= 0) { throw std::runtime_error("Error during formatting."); } auto size = static_cast(size_s); std::unique_ptr buf(new char[size]); std::snprintf(buf.get(), size, format.c_str(), args...); - return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside + return std::string(buf.get(), buf.get() + size - 1); // We don't want the +'\0' inside } */ #endif -bool inJson=false; -bool insecond=false; - -struct varType -{ - varTypeEnum _varType; - string varName; - uint8_t _varSize; - asmInstruction load[20]; - asmInstruction store[20]; - string membersNames[10]; - uint8_t starts[20]; - - // asmInstruction add; - // asmInstruction mul; - // asmInstruction div; - // string reg_name; - - uint8_t memberSize[20]; - // string vnames[20]; - varTypeEnum types[20]; - uint8_t sizes[20]; - uint8_t size; - uint8_t total_size; +bool inJson = false; +bool insecond = false; + +struct varType { + varTypeEnum _varType; + string varName; + uint8_t _varSize; + asmInstruction load[20]; + asmInstruction store[20]; + string membersNames[10]; + uint8_t starts[20]; + + // asmInstruction add; + // asmInstruction mul; + // asmInstruction div; + // string reg_name; + + uint8_t memberSize[20]; + // string vnames[20]; + varTypeEnum types[20]; + uint8_t sizes[20]; + uint8_t size; + uint8_t total_size; }; varType usded; @@ -63,21 +63,10 @@ vector _userDefinedTypes; varType __v; string varTypeEnumNames[] = { #ifdef __TEST_DEBUG - "__none__", - "__unit8_t__", - "__unit16_t__", - "__unit32_t__", - "__int__", - "__s_int__", - "__float__", - "__void__", - "__CRGB__", - "__CRGBW__", - "__char__", - "__Args__", - "__bool__", - "__userDefined__", - "__unknown__" + "__none__", "__unit8_t__", "__unit16_t__", "__unit32_t__", + "__int__", "__s_int__", "__float__", "__void__", + "__CRGB__", "__CRGBW__", "__char__", "__Args__", + "__bool__", "__userDefined__", "__unknown__" #endif }; @@ -268,174 +257,155 @@ varType _varTypes[] = { string keywordTypeNames[] = { #ifdef __TEST_DEBUG - "KeywordVarType", - "KeywordVarType", - "KeywordVarType", - "KeywordVarType", - "KeywordVarType", - "KeywordVarType", - "KeywordVarType", - "KeywordVarType", - "KeywordVarType", - "KeywordVarType", - "KeywordVarType", - "KeywordVarType", - "KeywordExternalVar", - "KeywordFor", - "KeywordIf", - "KeywordThen", - "KeywordElse", - "KeywordWhile", - "KeywordReturn", - "KeywordImport", - "KeywordFrom", - "KeywordASM", - "KeywordDefine", - "KeywordSafeMode", - "KeywordHeader", - "KeywordContent", + "KeywordVarType", "KeywordVarType", "KeywordVarType", "KeywordVarType", + "KeywordVarType", "KeywordVarType", "KeywordVarType", "KeywordVarType", + "KeywordVarType", "KeywordVarType", "KeywordVarType", "KeywordVarType", + "KeywordExternalVar", "KeywordFor", "KeywordIf", "KeywordThen", + "KeywordElse", "KeywordWhile", "KeywordReturn", "KeywordImport", + "KeywordFrom", "KeywordASM", "KeywordDefine", "KeywordSafeMode", + "KeywordHeader", "KeywordContent", #endif }; #define nb_keywords 39 #define nb_typeVariables 13 -string keyword_array[nb_keywords] = - {"none", "uint8_t", "uint16_t", "uint32_t", "int", "s_int", "float", "void", "CRGB", - "CRGBW", "char", "Args", "bool", "external", "for", "if", "then", "else", "while", "return", - "import", "from", "__ASM__", - "define", "safe_mode", "_header_", "_content_", "and", "or", "continue", - "break", "fabs", "abs", "save_reg", - "save_reg_abs", "struct","override","json","as"}; +string keyword_array[nb_keywords] = { + "none", "uint8_t", "uint16_t", "uint32_t", "int", + "s_int", "float", "void", "CRGB", "CRGBW", + "char", "Args", "bool", "external", "for", + "if", "then", "else", "while", "return", + "import", "from", "__ASM__", "define", "safe_mode", + "_header_", "_content_", "and", "or", "continue", + "break", "fabs", "abs", "save_reg", "save_reg_abs", + "struct", "override", "json", "as"}; bool __isBlockComment = false; -enum tokenType -{ - TokenNumber, - TokenAddition, - TokenStar, - TokenSubstraction, - TokenOpenParenthesis, - TokenCloseParenthesis, - TokenOpenBracket, - TokenCloseBracket, - TokenOpenCurlyBracket, - TokenCloseCurlyBracket, - TokenEqual, - TokenDoubleEqual, - TokenIdentifier, - TokenSemicolon, - TokenUnknown, - TokenSpace, - TokenNewline, - TokenEndOfFile, - TokenSlash, - TokenKeyword, - TokenString, - TokenExternal, - TokenComma, - TokenLessThan, - TokenPlusPlus, - TokenMinusMinus, - TokenModulo, - TokenLessOrEqualThan, - TokenMoreThan, - TokenMoreOrEqualThan, - TokenNotEqual, - TokenNot, - TokenFunction, - TokenUppersand, - TokenDiese, - TokenLineComment, - TokenStartBlockComment, - TokenEndBlockComment, - TokenNegation, - TokenShiftLeft, - TokenShiftRight, - TokenKeywordVarType, - TokenKeywordExternalVar, - TokenKeywordFor, - TokenKeywordIf, - TokenKeywordThen, - TokenKeywordElse, - TokenKeywordWhile, - TokenKeywordReturn, - TokenKeywordImport, - TokenKeywordFrom, - TokenKeywordASM, - TokenKeywordDefine, - TokenKeywordSafeMode, - TokenKeywordHeader, - TokenKeywordContent, - TokenKeywordAnd, - TokenKeywordOr, - TokenPower, - TokenKeywordContinue, - TokenKeywordBreak, - TokenKeywordFabs, - TokenKeywordAbs, - TokenKeywordSaveReg, - TokenKeywordSaveRegAbs, - TokenKeywordStruct, - TokenUserDefinedName, - TokenUserDefinedVariable, - TokenMember, - TokenUserDefinedVariableMember, - TokenUserDefinedVariableMemberFunction, - TokenDoubleUppersand, - TokenDoubleOr, - TokenQuestionMark, - TokenColon, - TokenPlusEqual, - TokenMinusEqual, - TokenStarEqual, - TokenSlashEqual, - TokenOverride, - TokenJson, - TokenAs +enum tokenType { + TokenNumber, + TokenAddition, + TokenStar, + TokenSubstraction, + TokenOpenParenthesis, + TokenCloseParenthesis, + TokenOpenBracket, + TokenCloseBracket, + TokenOpenCurlyBracket, + TokenCloseCurlyBracket, + TokenEqual, + TokenDoubleEqual, + TokenIdentifier, + TokenSemicolon, + TokenUnknown, + TokenSpace, + TokenNewline, + TokenEndOfFile, + TokenSlash, + TokenKeyword, + TokenString, + TokenExternal, + TokenComma, + TokenLessThan, + TokenPlusPlus, + TokenMinusMinus, + TokenModulo, + TokenLessOrEqualThan, + TokenMoreThan, + TokenMoreOrEqualThan, + TokenNotEqual, + TokenNot, + TokenFunction, + TokenUppersand, + TokenDiese, + TokenLineComment, + TokenStartBlockComment, + TokenEndBlockComment, + TokenNegation, + TokenShiftLeft, + TokenShiftRight, + TokenKeywordVarType, + TokenKeywordExternalVar, + TokenKeywordFor, + TokenKeywordIf, + TokenKeywordThen, + TokenKeywordElse, + TokenKeywordWhile, + TokenKeywordReturn, + TokenKeywordImport, + TokenKeywordFrom, + TokenKeywordASM, + TokenKeywordDefine, + TokenKeywordSafeMode, + TokenKeywordHeader, + TokenKeywordContent, + TokenKeywordAnd, + TokenKeywordOr, + TokenPower, + TokenKeywordContinue, + TokenKeywordBreak, + TokenKeywordFabs, + TokenKeywordAbs, + TokenKeywordSaveReg, + TokenKeywordSaveRegAbs, + TokenKeywordStruct, + TokenUserDefinedName, + TokenUserDefinedVariable, + TokenMember, + TokenUserDefinedVariableMember, + TokenUserDefinedVariableMemberFunction, + TokenDoubleUppersand, + TokenDoubleOr, + TokenQuestionMark, + TokenColon, + TokenPlusEqual, + TokenMinusEqual, + TokenStarEqual, + TokenSlashEqual, + TokenOverride, + TokenJson, + TokenAs }; -tokenType __keywordTypes[] = { - TokenKeywordVarType, - TokenKeywordVarType, - TokenKeywordVarType, - TokenKeywordVarType, - TokenKeywordVarType, - TokenKeywordVarType, - TokenKeywordVarType, - TokenKeywordVarType, - TokenKeywordVarType, - TokenKeywordVarType, - TokenKeywordVarType, - TokenKeywordVarType, - TokenKeywordVarType, - TokenKeywordExternalVar, - TokenKeywordFor, - TokenKeywordIf, - TokenKeywordThen, - TokenKeywordElse, - TokenKeywordWhile, - TokenKeywordReturn, - TokenKeywordImport, - TokenKeywordFrom, - TokenKeywordASM, - TokenKeywordDefine, - TokenKeywordSafeMode, - TokenKeywordHeader, - TokenKeywordContent, - TokenKeywordAnd, - TokenKeywordOr, - TokenKeywordContinue, - TokenKeywordBreak, - TokenKeywordFabs, - TokenKeywordAbs, - TokenKeywordSaveReg, - TokenKeywordSaveRegAbs, - TokenKeywordStruct, - TokenOverride, - TokenJson, - TokenAs +tokenType __keywordTypes[] = {TokenKeywordVarType, + TokenKeywordVarType, + TokenKeywordVarType, + TokenKeywordVarType, + TokenKeywordVarType, + TokenKeywordVarType, + TokenKeywordVarType, + TokenKeywordVarType, + TokenKeywordVarType, + TokenKeywordVarType, + TokenKeywordVarType, + TokenKeywordVarType, + TokenKeywordVarType, + TokenKeywordExternalVar, + TokenKeywordFor, + TokenKeywordIf, + TokenKeywordThen, + TokenKeywordElse, + TokenKeywordWhile, + TokenKeywordReturn, + TokenKeywordImport, + TokenKeywordFrom, + TokenKeywordASM, + TokenKeywordDefine, + TokenKeywordSafeMode, + TokenKeywordHeader, + TokenKeywordContent, + TokenKeywordAnd, + TokenKeywordOr, + TokenKeywordContinue, + TokenKeywordBreak, + TokenKeywordFabs, + TokenKeywordAbs, + TokenKeywordSaveReg, + TokenKeywordSaveRegAbs, + TokenKeywordStruct, + TokenOverride, + TokenJson, + TokenAs }; @@ -527,78 +497,65 @@ string tokenNames[] = { #endif }; -typedef struct -{ - string name; - string content; - //string hh; +typedef struct { + string name; + string content; + // string hh; } _define; vector<_define> define_list; vector<_define> external_define_list; -int getExternalDefine(string name) -{ - for (int i=0;icontent=" "+content; - } +void addExternalDefine(string name, string content) { + int index = getExternalDefine(name); + _define t; + t.name = name; + t.content = " " + content; + if (index == -1) { + // printf("one ajoue\n"); + external_define_list.push_back(t); + } else { + (&external_define_list[index])->content = " " + content; + } } -const char *getDefine(string name) -{ - for (vector<_define>::iterator it = define_list.begin(); - it != define_list.end(); ++it) - { - if ((*it).name.compare(name) == 0) - { - // printf("one rrent %s\n",(*it).content.c_str()); - return (*it).content.c_str(); - } +const char *getDefine(string name) { + for (vector<_define>::iterator it = define_list.begin(); + it != define_list.end(); ++it) { + if ((*it).name.compare(name) == 0) { + // printf("one rrent %s\n",(*it).content.c_str()); + return (*it).content.c_str(); } - return NULL; + } + return NULL; } -void deleteDefine() -{ - for (vector<_define>::iterator it = define_list.begin(); - it != define_list.end(); ++it) - { - (*it).content.clear(); - (*it).content.shrink_to_fit(); - (*it).name.clear(); - (*it).name.shrink_to_fit(); - } - define_list.clear(); - define_list.shrink_to_fit(); - //we upload the external define - for(int i=0;i::iterator it = define_list.begin(); + it != define_list.end(); ++it) { + (*it).content.clear(); + (*it).content.shrink_to_fit(); + (*it).name.clear(); + (*it).name.shrink_to_fit(); + } + define_list.clear(); + define_list.shrink_to_fit(); + // we upload the external define + for (int i = 0; i < external_define_list.size(); i++) { + // printf("on ajoute dans la define %s + // %s\n",external_define_list[i].name.c_str(),external_define_list[i].content.c_str()); + define_list.push_back(external_define_list[i]); + // printf("on push exter|%s|\n",external_define_list[i].content.c_str()); + } } #ifdef __CONSOLE_ESP32 @@ -683,10 +640,10 @@ const char *tokenFormat[] = { termColor.BWhite, // TokenMinusEqual termColor.BWhite, // TokenStarEqual termColor.BWhite, // TokenSlashEqual - termColor.LMagenta, // TokenSlashEqual - termColor.LMagenta, // TokenStarEqual - termColor.LMagenta, // TokenSlashEqual - termColor.LMagenta, // TokenSlashEqual + termColor.LMagenta, // TokenSlashEqual + termColor.LMagenta, // TokenStarEqual + termColor.LMagenta, // TokenSlashEqual + termColor.LMagenta, // TokenSlashEqual }; /* @@ -710,248 +667,193 @@ const char *KeywordTypeFormat[] = }; */ #endif -typedef struct -{ - // switch to uin8_t - tokenType type; - varType *_vartype; +typedef struct { + // switch to uin8_t + tokenType type; + varType *_vartype; - // varTypeEnum _varType; + // varTypeEnum _varType; - // needs to find a solution for this maybe a pointer to list string et on ne garde que les identifiers ... + // needs to find a solution for this maybe a pointer to list string et on ne + // garde que les identifiers ... - string text; - uint16_t line; + string text; + uint16_t line; - // switch to uin8_t unn ligne ne fera pas plus de 256 caracteres - uint8_t pos; + // switch to uin8_t unn ligne ne fera pas plus de 256 caracteres + uint8_t pos; - // switch to uin8_t - // KeywordType _keyword; + // switch to uin8_t + // KeywordType _keyword; - // possible sizee 1+4+4+1+1+4=15 au lieu de 44 ... + // possible sizee 1+4+4+1+1+4=15 au lieu de 44 ... } token; vector texts; -class Script -{ +class Script { public: - Script() - { + Script() { position = -1; } + void init() { + it = script.begin(); + position = -1; + } + void addContent(char *str) { script.push_back(str); } + void clear() { + script.clear(); + script.shrink_to_fit(); + position = -1; + } + + char nextChar() { + + if ((*it)[position + 1] != 0) { + position++; + return (*it)[position]; + } else { + + if (next(it) == script.end()) { - position = -1; - } - void init() - { - it = script.begin(); - position = -1; - } - void addContent(char *str) - { - script.push_back(str); - } - void clear() - { - script.clear(); - script.shrink_to_fit(); - position = -1; + position++; + return EOF_TEXT; + } else { + it = next(it); + position = 0; + return (*it)[position]; + } } + } - char nextChar() - { + char currentChar() { - if ((*it)[position + 1] != 0) - { - position++; - return (*it)[position]; - } - else - { + if ((*it)[position] != 0) { - if (next(it) == script.end()) - { + return (*it)[position]; + } else { - position++; - return EOF_TEXT; - } - else - { - it = next(it); - position = 0; - return (*it)[position]; - } - } + return EOF_TEXT; } + } - char currentChar() - { + char previousChar() { - if ((*it)[position] != 0) - { - - return (*it)[position]; - } - else - { - - return EOF_TEXT; - } - } - - char previousChar() - { + if ((position - 1) >= 0) { + position--; + return (*it)[position]; + } else { + if (it != script.begin()) { - if ((position-1) >= 0) - { - position--; - return (*it)[position]; - } - else - { - if (it != script.begin()) - { - - it--; - position = 0; - while ((*it)[position] != 0) - { - position++; - } - position--; - return (*it)[position]; - } - else - { - - position = -1; - - return 0;// (*it)[0]; - } + it--; + position = 0; + while ((*it)[position] != 0) { + position++; } - } - - void insert(char *toInsert) - { + position--; + return (*it)[position]; + } else { - char *_cur = &(*it)[position]; - char *_next = &(*it)[position + 1]; - it++; - if (*_next != 0 and *_cur != 0) - it = script.insert(it, _next); - it = script.insert(it, toInsert); position = -1; - } - void insertAtEnd(char *toInsert) - { - int i = 0; - int res = -1; - for (vector::iterator _it = script.begin(); _it != script.end(); _it++) - { - if (it == _it) - { - res = i; - } - else - { - i++; - } - } - script.insert(script.end(), toInsert); - it = script.begin(); - while (res > 0) - { - it++; - res--; - } - } + + return 0; // (*it)[0]; + } + } + } + + void insert(char *toInsert) { + + char *_cur = &(*it)[position]; + char *_next = &(*it)[position + 1]; + it++; + if (*_next != 0 and *_cur != 0) + it = script.insert(it, _next); + it = script.insert(it, toInsert); + position = -1; + } + void insertAtEnd(char *toInsert) { + int i = 0; + int res = -1; + for (vector::iterator _it = script.begin(); _it != script.end(); + _it++) { + if (it == _it) { + res = i; + } else { + i++; + } + } + script.insert(script.end(), toInsert); + it = script.begin(); + while (res > 0) { + it++; + res--; + } + } private: - // string * script; - int position; + // string * script; + int position; - vector script; - vector::iterator it; + vector script; + vector::iterator it; }; -class Token -{ +class Token { public: - Token() - { - type = (int)TokenUnknown; - _vartype = EOF_VARTYPE; - textref = EOF_TEXTARRAY; - pos= 0; - line = 0; - } - - Token(tokenType h) - { - type = (int)h; - _vartype = EOF_VARTYPE; - textref = EOF_TEXTARRAY; - pos= 0; - line = 0; - } - Token(tokenType _type, int __vartype, int _line) - { - type = (int)_type; - _vartype = __vartype; - line = _line; - textref = EOF_TEXTARRAY; - pos= 0; - - } - Token(tokenType _type, int __vartype) - { - type = (int)_type; - _vartype = __vartype; - - textref = EOF_TEXTARRAY; - pos= 0; - line = 0; - } - tokenType getType() - { - return (tokenType)type; - } - void setType(tokenType _type) - { - type = (int)_type; - } - void clean() - { - line = 0; - type = 0; - pos=0; - _vartype = EOF_VARTYPE; - - textref = EOF_TEXTARRAY; - } - void addText(string t) - { - textref = all_text.addText(t); - } - char *getText() - { - return all_text.getText(textref); - } - varType *getVarType() - { - if (_vartype == EOF_VARTYPE) - - return NULL; - - return &_varTypes[_vartype]; - } - uint16_t line; - uint8_t type; - uint8_t _vartype; - uint8_t pos; - uint16_t textref; + Token() { + type = (int)TokenUnknown; + _vartype = EOF_VARTYPE; + textref = EOF_TEXTARRAY; + pos = 0; + line = 0; + } + + Token(tokenType h) { + type = (int)h; + _vartype = EOF_VARTYPE; + textref = EOF_TEXTARRAY; + pos = 0; + line = 0; + } + Token(tokenType _type, int __vartype, int _line) { + type = (int)_type; + _vartype = __vartype; + line = _line; + textref = EOF_TEXTARRAY; + pos = 0; + } + Token(tokenType _type, int __vartype) { + type = (int)_type; + _vartype = __vartype; + + textref = EOF_TEXTARRAY; + pos = 0; + line = 0; + } + tokenType getType() { return (tokenType)type; } + void setType(tokenType _type) { type = (int)_type; } + void clean() { + line = 0; + type = 0; + pos = 0; + _vartype = EOF_VARTYPE; + + textref = EOF_TEXTARRAY; + } + void addText(string t) { textref = all_text.addText(t); } + char *getText() { return all_text.getText(textref); } + varType *getVarType() { + if (_vartype == EOF_VARTYPE) + + return NULL; + + return &_varTypes[_vartype]; + } + uint16_t line; + uint8_t type; + uint8_t _vartype; + uint8_t pos; + uint16_t textref; }; #ifdef __FULL_TOKEN @@ -961,186 +863,151 @@ class Token #endif int tokenizer(Script *script, bool update, bool increae_line, int nbMaxTokenToRead); -class Tokens -{ +class Tokens { public: - void init() - { + void init() { #ifdef __FULL_TOKEN - position = 0; + position = 0; #endif - } - Tokens() - { - // _tokens = &_list_of_tokens; - clear(); - init(); - } + } + Tokens() { + // _tokens = &_list_of_tokens; + clear(); + init(); + } - void clear() - { - _tokens.clear(); + void clear() { + _tokens.clear(); - _tokens.shrink_to_fit(); + _tokens.shrink_to_fit(); #ifdef __FULL_TOKEN - position = 0; + position = 0; #endif - } - int size() - { - return _tokens.size(); - } - void tokenizelow(Script *script, bool update, bool increae_line, int nbToken) - { - _script = script; - clear(); - tokenizer(script, true, increae_line, nbToken); - // list_of_token.push_back(token()); - // Serial.printf("token read %d\n", tokenizer(script, true, increae_line, nbToken)); - } - void tokenize(Script *script, bool update, bool increae_line, int nbToken) - { - _script = script; - clear(); - - tokenizer(script, update, increae_line, nbToken); - // list_of_token.push_back(token()); - // Serial.printf("token read %d\n", tokenizer(script, true, increae_line, nbToken)); - } - void push(Token t) - { - _tokens.push_back(t); - } - void pop_back() - { - if (_tokens.size() > 0) - { - // Token t = _tokens.back (); - - _tokens.pop_back(); - - // return t; - } - // else - // return end_token; - } - Token *getTokenAtPos(int pos) - { - if (pos >= 0 and pos < _tokens.size()) - { - // printf("%s at %d %d\n",_tokens[pos].line,_tokens[pos].pos); - return &(_tokens[pos]); - } - else - return &end_token; - } - Token *current() - { + } + int size() { return _tokens.size(); } + void tokenizelow(Script *script, bool update, bool increae_line, + int nbToken) { + _script = script; + clear(); + tokenizer(script, true, increae_line, nbToken); + // list_of_token.push_back(token()); + // Serial.printf("token read %d\n", tokenizer(script, true, increae_line, + // nbToken)); + } + void tokenize(Script *script, bool update, bool increae_line, int nbToken) { + _script = script; + clear(); + + tokenizer(script, update, increae_line, nbToken); + // list_of_token.push_back(token()); + // Serial.printf("token read %d\n", tokenizer(script, true, increae_line, + // nbToken)); + } + void push(Token t) { _tokens.push_back(t); } + void pop_back() { + if (_tokens.size() > 0) { + // Token t = _tokens.back (); + + _tokens.pop_back(); + + // return t; + } + // else + // return end_token; + } + Token *getTokenAtPos(int pos) { + if (pos >= 0 and pos < _tokens.size()) { + // printf("%s at %d %d\n",_tokens[pos].line,_tokens[pos].pos); + return &(_tokens[pos]); + } else + return &end_token; + } + Token *current() { #ifdef __FULL_TOKEN - return getTokenAtPos(position); + return getTokenAtPos(position); #else - return getTokenAtPos(__DEPTH); + return getTokenAtPos(__DEPTH); #endif - } - Token *next() - { + } + Token *next() { #ifdef __FULL_TOKEN - // position++; - tokenizer(_script, false, true, 1); - position++; + // position++; + tokenizer(_script, false, true, 1); + position++; - return getTokenAtPos(position); + return getTokenAtPos(position); #else - _tokens.erase(_tokens.begin()); - _tokens.shrink_to_fit(); + _tokens.erase(_tokens.begin()); + _tokens.shrink_to_fit(); - tokenizer(_script, false, true, 1); - return getTokenAtPos(__DEPTH); + tokenizer(_script, false, true, 1); + return getTokenAtPos(__DEPTH); #endif - } - Token *prev() - { + } + Token *prev() { #ifdef __FULL_TOKEN - position--; - return getTokenAtPos(position); + position--; + return getTokenAtPos(position); #else - _tokens.insert(_tokens.begin(), Token()); - return getTokenAtPos(__DEPTH); + _tokens.insert(_tokens.begin(), Token()); + return getTokenAtPos(__DEPTH); #endif - } - Token *peek(int index) - { + } + Token *peek(int index) { #ifdef __FULL_TOKEN - if (index + position < _tokens.size() && index + position >= 0) - { - return getTokenAtPos(index + position); - } - else - { - tokenizer(_script, false, true, - -_tokens.size() + index + position + 1); + if (index + position < _tokens.size() && index + position >= 0) { + return getTokenAtPos(index + position); + } else { + tokenizer(_script, false, true, -_tokens.size() + index + position + 1); - return getTokenAtPos(index + position); - } + return getTokenAtPos(index + position); + } #else - if (index + __DEPTH < _tokens.size() && index + __DEPTH >= 0) - { - return getTokenAtPos(index + __DEPTH); - } - else - { - tokenizer(_script, false, true, - -_tokens.size() + index + __DEPTH + 1); + if (index + __DEPTH < _tokens.size() && index + __DEPTH >= 0) { + return getTokenAtPos(index + __DEPTH); + } else { + tokenizer(_script, false, true, -_tokens.size() + index + __DEPTH + 1); - return getTokenAtPos(index + __DEPTH); - } -#endif - } - Token back() - { - if (_tokens.size() > 0) - return _tokens.back(); - else - return Token(); + return getTokenAtPos(index + __DEPTH); } +#endif + } + Token back() { + if (_tokens.size() > 0) + return _tokens.back(); + else + return Token(); + } - bool Match(tokenType tt) - { - Token *g = current(); - if (g->getType() == tt) - { - return true; - } - else - { - return false; - } + bool Match(tokenType tt) { + Token *g = current(); + if (g->getType() == tt) { + return true; + } else { + return false; } + } - bool Match(tokenType tt, int index) - { - Token *g = peek(index); - if (g->getType() == tt) - { - return true; - } - else - { - return false; - } + bool Match(tokenType tt, int index) { + Token *g = peek(index); + if (g->getType() == tt) { + return true; + } else { + return false; } + } private: - vector _tokens; - Token end_token = Token(TokenEndOfFile); - Script *_script; + vector _tokens; + Token end_token = Token(TokenEndOfFile); + Script *_script; #ifdef __FULL_TOKEN - int position; + int position; #endif }; @@ -1148,154 +1015,127 @@ Tokens __tks = Tokens(); Tokens _extra_tks = Tokens(); Tokens *_tks; -void displaytoken(token t) -{ - // printf("%s\t%d\t%f\t%s%d %d\t%s\n", tokenNames[t.type].c_str(), t.int_value, t.float_value, t.text.c_str(), t.line, //t.pos, keywordTypeNames[t._keyword].c_str()); +void displaytoken(token t) { + // printf("%s\t%d\t%f\t%s%d %d\t%s\n", tokenNames[t.type].c_str(), + // t.int_value, t.float_value, t.text.c_str(), t.line, //t.pos, + // keywordTypeNames[t._keyword].c_str()); } -Token transNumber(string str) -{ - // t; - // t.float_value=0; - // t.int_value=0; - if (_tks->size() > 1) - { - if (_tks->back().type == TokenSubstraction) - { - tokenType subtype = (tokenType)_tks->getTokenAtPos(_tks->size() - 2)->type; - if (subtype == TokenComma || subtype == TokenEqual || subtype == TokenDoubleEqual || subtype == TokenLessOrEqualThan || subtype == TokenMoreThan || subtype == TokenMoreOrEqualThan || subtype == TokenNotEqual || subtype == TokenStarEqual || subtype == TokenPlusEqual || subtype == TokenOpenParenthesis) - { - str = "-" + str; - _tks->pop_back(); - } - } - } - if (str.find(".") != string::npos) - { - - Token t = Token(TokenNumber, (int)__float__); - t.addText(str); - return t; - } - else - { - - Token t = Token(TokenNumber, (int)__int__); - t.addText(str); - return t; - } +Token transNumber(string str) { + // t; + // t.float_value=0; + // t.int_value=0; + if (_tks->size() > 1) { + if (_tks->back().type == TokenSubstraction) { + tokenType subtype = + (tokenType)_tks->getTokenAtPos(_tks->size() - 2)->type; + if (subtype == TokenComma || subtype == TokenEqual || + subtype == TokenDoubleEqual || subtype == TokenLessOrEqualThan || + subtype == TokenMoreThan || subtype == TokenMoreOrEqualThan || + subtype == TokenNotEqual || subtype == TokenStarEqual || + subtype == TokenPlusEqual || subtype == TokenOpenParenthesis) { + str = "-" + str; + _tks->pop_back(); + } + } + } + if (str.find(".") != string::npos) { + + Token t = Token(TokenNumber, (int)__float__); + t.addText(str); + return t; + } else { + + Token t = Token(TokenNumber, (int)__int__); + t.addText(str); + return t; + } } -int isKeyword(string str) -{ - for (int i = 0; i < nb_keywords; i++) - { - if (keyword_array[i].compare(str) == 0) - { - return i; - } +int isKeyword(string str) { + for (int i = 0; i < nb_keywords; i++) { + if (keyword_array[i].compare(str) == 0) { + return i; } + } - return -1; + return -1; } -int isUserDefined(string str) -{ - for (int i = 0; i < userDefinedVarTypeNames.size(); i++) - { - if (userDefinedVarTypeNames[i].compare(str) == 0) - { - return i; - } +int isUserDefined(string str) { + for (int i = 0; i < userDefinedVarTypeNames.size(); i++) { + if (userDefinedVarTypeNames[i].compare(str) == 0) { + return i; } + } - return -1; + return -1; } -bool isIna_zA_Z_(unsigned char c) -{ - - if (c >= 97 && c <= 122) - { - return true; - } - if (c >= 65 && c <= 90) - { - return true; - } - if (c == '_') - { - return true; - } - if( inJson == true && c =='.') - { - return true; - } - return false; +bool isIna_zA_Z_(unsigned char c) { + + if (c >= 97 && c <= 122) { + return true; + } + if (c >= 65 && c <= 90) { + return true; + } + if (c == '_') { + return true; + } + if (inJson == true && c == '.') { + return true; + } + return false; } -bool isIna_zA_Z_0_9(unsigned char c) -{ - - if (c >= 97 && c <= 122) - { - return true; - } - if (c >= 65 && c <= 90) - { - return true; - } - if (c == '_') - { - return true; - } - if( inJson == true && c =='.') - { - return true; - } - if (c >= 48 && c <= 57) - { - return true; - } - return false; +bool isIna_zA_Z_0_9(unsigned char c) { + + if (c >= 97 && c <= 122) { + return true; + } + if (c >= 65 && c <= 90) { + return true; + } + if (c == '_') { + return true; + } + if (inJson == true && c == '.') { + return true; + } + if (c >= 48 && c <= 57) { + return true; + } + return false; } -bool isIn0_9(unsigned char c) -{ - if (c >= 48 && c <= 57) - { - return true; - } - return false; +bool isIn0_9(unsigned char c) { + if (c >= 48 && c <= 57) { + return true; + } + return false; } -bool isIn0_9_x_b(unsigned char c) -{ - if (c >= 48 && c <= 57) - { - return true; - } - if (c == 'b') - { - return true; - } - if (c >= 'A' && c <= 'F') - { - return true; - } - if (c >= 'a' && c <= 'f') - { - return true; - } - if (c == 'x') - { - return true; - } - if (c == '.') - { - return true; - } - return false; +bool isIn0_9_x_b(unsigned char c) { + if (c >= 48 && c <= 57) { + return true; + } + if (c == 'b') { + return true; + } + if (c >= 'A' && c <= 'F') { + return true; + } + if (c >= 'a' && c <= 'f') { + return true; + } + if (c == 'x') { + return true; + } + if (c == '.') { + return true; + } + return false; } bool _for_display = false; @@ -1303,1090 +1143,969 @@ bool _for_display = false; int _token_line; int _sav_token_line = 0; int pos_in_line = 0; -//list::iterator _index_token; +// list::iterator _index_token; string vchar; int tokenizer(Script *script, bool update, bool increae_line, - int nbMaxTokenToRead) -{ - - // list list_of_token; - // int line = 1; -Token t; - // int pos = 0; - char c; - char c2; - - _define newdef; + int nbMaxTokenToRead) { + + // list list_of_token; + // int line = 1; + Token t; + // int pos = 0; + char c; + char c2; + + _define newdef; + vchar.clear(); + if (update) { + // inJson=false; + _tks->clear(); + for (int i = 0; i < __DEPTH; i++) { + _tks->push(Token()); + } + _token_line = 1; + pos_in_line = 0; + if (!insecond) { + userDefinedVarTypeNames.clear(); + userDefinedVarTypeNames.shrink_to_fit(); + // all_text.clear(); + + deleteDefine(); + + __isBlockComment = false; + } + } + // _for_display= true; + + else { + script->previousChar(); + } + int nbReadToken = 0; + while (script->nextChar() != EOF_TEXT and nbReadToken < nbMaxTokenToRead) { + // printf(" nb read :%c:\n",script->currentChar()); + t.clean(); vchar.clear(); - if (update) - { - // inJson=false; - _tks->clear(); - for (int i = 0; i < __DEPTH; i++) - { - _tks->push(Token()); - } - _token_line = 1; - pos_in_line = 0; - if(!insecond) - { - userDefinedVarTypeNames.clear(); - userDefinedVarTypeNames.shrink_to_fit(); - //all_text.clear(); - - deleteDefine(); - - __isBlockComment = false; - } - } - // _for_display= true; - - else - { + pos_in_line++; + + c = script->currentChar(); + // printf("line : %d pos:%d char:%c token size:%d + // %d\n",line,pos,c,list_of_token.size(),heap_caps_get_free_size(MALLOC_CAP_8BIT)); + if (c == '=') { + c2 = script->nextChar(); + if (c2 == '=') { + t = Token(TokenDoubleEqual, EOF_VARTYPE); + // t._vartype = NULL; + // t.type = TokenDoubleEqual; + if (_for_display) + t.addText("=="); + t.line = _token_line; + t.pos = pos_in_line; + _tks->push(t); + // nbReadToken++; + + continue; + } else { script->previousChar(); - } - int nbReadToken = 0; - while (script->nextChar() != EOF_TEXT and nbReadToken < nbMaxTokenToRead) - { - // printf(" nb read :%c:\n",script->currentChar()); - t.clean(); - vchar.clear(); - pos_in_line++; - - c = script->currentChar(); - // printf("line : %d pos:%d char:%c token size:%d %d\n",line,pos,c,list_of_token.size(),heap_caps_get_free_size(MALLOC_CAP_8BIT)); - if (c == '=') - { - c2 = script->nextChar(); - if (c2 == '=') - { - t = Token(TokenDoubleEqual, EOF_VARTYPE); - // t._vartype = NULL; - // t.type = TokenDoubleEqual; - if (_for_display) - t.addText("=="); - t.line = _token_line; - t.pos = pos_in_line; - _tks->push(t); - // nbReadToken++; - - continue; - } - else - { - script->previousChar(); - // token t; - // t._vartype = NULL; - // t.type = TokenEqual; - // t.line = _token_line; - t = Token(TokenEqual, EOF_VARTYPE, _token_line); - t.pos = pos_in_line; - if (_for_display) - t.addText("="); - _tks->push(t); - // nbReadToken++; - continue; - } - } - if (c == '<') - { - c2 = script->nextChar(); - if (c2 == '=') - { - // token t; - // t._vartype = NULL; - // t.type = TokenLessOrEqualThan; - - t = Token(TokenLessOrEqualThan, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("<="); - // t.line = _token_line; - t.pos = pos_in_line; - _tks->push(t); - // nbReadToken++; - continue; - } - else if (c2 == '<') - { - // token t; - // t._vartype = NULL; - // t.type = TokenShiftLeft; - t = Token(TokenShiftLeft, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("<<"); - // t.line = _token_line; - // t.pos = pos_in_line; - // _tks->push(t); - _tks->push(t); - nbReadToken++; - continue; - } - else - { - script->previousChar(); - // token t; - // t._vartype = NULL; - // t.type = TokenLessThan; - // t.line = _token_line;// - // TokenLessThan - t = Token(TokenLessThan, EOF_VARTYPE, _token_line); - t.pos = pos_in_line; - if (_for_display) - t.addText("<"); - _tks->push(t); - nbReadToken++; - continue; - } - } - if (c == '>') - { - c2 = script->nextChar(); - if (c2 == '=') - { - // token t; - // t._vartype = NULL; - // t.type = TokenMoreOrEqualThan; - t = Token(TokenMoreOrEqualThan, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText(">="); - // t.line = _token_line; - t.pos = pos_in_line; - //_tks->push(t); - _tks->push(t); - // nbReadToken++; - continue; - } - else if (c2 == '>') - { - // token t; - // t._vartype = NULL; - // t.type = TokenShiftRight; - t = Token(TokenShiftRight, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText(">>"); - // t.line = _token_line; - t.pos = pos_in_line; - // _tks->push(t); - _tks->push(t); - nbReadToken++; - continue; - } - else - { - script->previousChar(); - // token t; - // t._vartype = NULL; - // t.type = TokenMoreThan; - // t.line = _token_line; - t = Token(TokenMoreThan, EOF_VARTYPE, _token_line); - t.pos = pos_in_line; - if (_for_display) - t.addText(">"); - // _tks->push(t); - _tks->push(t); - // nbReadToken++; - continue; + // token t; + // t._vartype = NULL; + // t.type = TokenEqual; + // t.line = _token_line; + t = Token(TokenEqual, EOF_VARTYPE, _token_line); + t.pos = pos_in_line; + if (_for_display) + t.addText("="); + _tks->push(t); + // nbReadToken++; + continue; + } + } + if (c == '<') { + c2 = script->nextChar(); + if (c2 == '=') { + // token t; + // t._vartype = NULL; + // t.type = TokenLessOrEqualThan; + + t = Token(TokenLessOrEqualThan, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("<="); + // t.line = _token_line; + t.pos = pos_in_line; + _tks->push(t); + // nbReadToken++; + continue; + } else if (c2 == '<') { + // token t; + // t._vartype = NULL; + // t.type = TokenShiftLeft; + t = Token(TokenShiftLeft, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("<<"); + // t.line = _token_line; + // t.pos = pos_in_line; + // _tks->push(t); + _tks->push(t); + nbReadToken++; + continue; + } else { + script->previousChar(); + // token t; + // t._vartype = NULL; + // t.type = TokenLessThan; + // t.line = _token_line;// + // TokenLessThan + t = Token(TokenLessThan, EOF_VARTYPE, _token_line); + t.pos = pos_in_line; + if (_for_display) + t.addText("<"); + _tks->push(t); + nbReadToken++; + continue; + } + } + if (c == '>') { + c2 = script->nextChar(); + if (c2 == '=') { + // token t; + // t._vartype = NULL; + // t.type = TokenMoreOrEqualThan; + t = Token(TokenMoreOrEqualThan, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText(">="); + // t.line = _token_line; + t.pos = pos_in_line; + //_tks->push(t); + _tks->push(t); + // nbReadToken++; + continue; + } else if (c2 == '>') { + // token t; + // t._vartype = NULL; + // t.type = TokenShiftRight; + t = Token(TokenShiftRight, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText(">>"); + // t.line = _token_line; + t.pos = pos_in_line; + // _tks->push(t); + _tks->push(t); + nbReadToken++; + continue; + } else { + script->previousChar(); + // token t; + // t._vartype = NULL; + // t.type = TokenMoreThan; + // t.line = _token_line; + t = Token(TokenMoreThan, EOF_VARTYPE, _token_line); + t.pos = pos_in_line; + if (_for_display) + t.addText(">"); + // _tks->push(t); + _tks->push(t); + // nbReadToken++; + continue; + } + } + if (c == '!') { + c2 = script->nextChar(); + if (c2 == '=') { + + // token t; + // t._vartype = NULL; + // t.type = TokenNotEqual; + t = Token(TokenNotEqual, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("!="); + // t.line = _token_line; + t.pos = pos_in_line; + //_tks->push(t); + _tks->push(t); + /// nbReadToken++; + continue; + } else { + script->previousChar(); + // token t; + // t._vartype = NULL; + // t.type = TokenNot; + // t.line = _token_line; + t = Token(TokenNot, EOF_VARTYPE, _token_line); + t.pos = pos_in_line; + if (_for_display) + t.addText("!"); + // _tks->push(t); + _tks->push(t); + nbReadToken++; + continue; + } + } + if (c == '+') { + c2 = script->nextChar(); + if (c2 == '+') { + // token t; + // t._vartype = NULL; + // t.type = TokenPlusPlus; + t = Token(TokenPlusPlus, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("++"); + // t.line = _token_line; + t.pos = pos_in_line; + // _tks->push(t); + // nbReadToken++; + _tks->push(t); + continue; + } else if (c2 == '=') { + t = Token(TokenPlusEqual, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("+="); + // t.line = _token_line; + t.pos = pos_in_line; + // _tks->push(t); + // nbReadToken++; + _tks->push(t); + continue; + } else { + script->previousChar(); + // token t; + // t._vartype = NULL; + // t.type = TokenAddition; + t = Token(TokenAddition, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("+"); + // t.line = _token_line; + t.pos = pos_in_line; + // _tks->push(t); + _tks->push(t); + nbReadToken++; + continue; + } + } + + if (isIna_zA_Z_(c)) { + + int newpos = pos_in_line; + while (isIna_zA_Z_0_9(c)) { + vchar += c; + newpos++; + c = script->nextChar(); + } + script->previousChar(); // on revient un caractere en arriere + // token t; + // Token t; + // t._vartype=NULL; + t.line = _token_line; + t.pos = pos_in_line; + if (isKeyword(vchar) > -1) { + // printf("keyword;%s\n",v.c_str()); + // t.type = TokenKeyword; + t.type = (int)__keywordTypes[isKeyword(vchar)]; + if (isKeyword(vchar) < nb_typeVariables) + t._vartype = isKeyword(vchar); + if (t.getType() == TokenKeywordExternalVar) { + t.type = (int)TokenExternal; + // printf("ereeeeeeee\n"); + } else if (t.getType() == TokenKeywordDefine) { + // printf("on est ici\n"); + if (!_for_display) { + if ((_tks->back()).getType() == TokenDiese) { + // printf("on est ici"); + _tks->pop_back(); + } else { + t.type = TokenUnknown; } + } } - if (c == '!') - { - c2 = script->nextChar(); - if (c2 == '=') - { - - // token t; - // t._vartype = NULL; - // t.type = TokenNotEqual; - t = Token(TokenNotEqual, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("!="); - // t.line = _token_line; - t.pos = pos_in_line; - //_tks->push(t); - _tks->push(t); - /// nbReadToken++; - continue; - } - else - { - script->previousChar(); - // token t; - // t._vartype = NULL; - // t.type = TokenNot; - // t.line = _token_line; - t = Token(TokenNot, EOF_VARTYPE, _token_line); - t.pos = pos_in_line; - if (_for_display) - t.addText("!"); - // _tks->push(t); - _tks->push(t); - nbReadToken++; - continue; - } + if (t.getType() == TokenJson) { + + inJson = true; } - if (c == '+') - { - c2 = script->nextChar(); - if (c2 == '+') - { - // token t; - // t._vartype = NULL; - // t.type = TokenPlusPlus; - t = Token(TokenPlusPlus, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("++"); - // t.line = _token_line; - t.pos = pos_in_line; - // _tks->push(t); - // nbReadToken++; - _tks->push(t); - continue; - } - else if (c2 == '=') - { - t = Token(TokenPlusEqual, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("+="); - // t.line = _token_line; - t.pos = pos_in_line; - // _tks->push(t); - // nbReadToken++; - _tks->push(t); - continue; - } - else - { - script->previousChar(); - // token t; - // t._vartype = NULL; - // t.type = TokenAddition; - t = Token(TokenAddition, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("+"); - // t.line = _token_line; - t.pos = pos_in_line; - // _tks->push(t); - _tks->push(t); - nbReadToken++; - continue; - } + if ((t.getType() == TokenKeywordImport or + t.getType() == TokenKeywordDefine) && + !_for_display) { + + nbReadToken--; } + } else if (isUserDefined(vchar) > -1) { + t.type = (int)TokenUserDefinedVariable; + t._vartype = (int)__userDefined__; + } else { + t.type = (int)TokenIdentifier; + inJson = false; + if (_tks->size() >= __DEPTH) { - if (isIna_zA_Z_(c)) - { + Token prev = _tks->back(); + if (prev.getType() == TokenKeywordImport && !_for_display) { - int newpos = pos_in_line; - while (isIna_zA_Z_0_9(c)) - { - vchar += c; - newpos++; - c = script->nextChar(); - } - script->previousChar(); // on revient un caractere en arriere - // token t; - // Token t; - // t._vartype=NULL; - t.line = _token_line; - t.pos = pos_in_line; - if (isKeyword(vchar) > -1) - { - // printf("keyword;%s\n",v.c_str()); - // t.type = TokenKeyword; - t.type = (int)__keywordTypes[isKeyword(vchar)]; - if (isKeyword(vchar) < nb_typeVariables) - t._vartype = isKeyword(vchar); - if (t.getType() == TokenKeywordExternalVar) - { - t.type = (int)TokenExternal; - // printf("ereeeeeeee\n"); - } - else if(t.getType()==TokenKeywordDefine) - { - //printf("on est ici\n"); - if(!_for_display) - { - if((_tks->back()).getType()==TokenDiese) - { - //printf("on est ici"); - _tks->pop_back(); - } - else - { - t.type=TokenUnknown; - } - } - } - if(t.getType()==TokenJson) - { - - inJson=true; - } - if ((t.getType() == TokenKeywordImport or t.getType() == TokenKeywordDefine) && !_for_display) - { - - nbReadToken--; - } - } - else if (isUserDefined(vchar) > -1) - { - t.type = (int)TokenUserDefinedVariable; - t._vartype = (int)__userDefined__; - } - else - { - t.type = (int)TokenIdentifier; - inJson=false; - if (_tks->size() >= __DEPTH) - { - - Token prev = _tks->back(); - if (prev.getType() == TokenKeywordImport && !_for_display) - { - - // script->insert(import); - - _sav_token_line = _token_line; - nbReadToken--; - if (findLibFunction(vchar) > -1) - { - - _tks->pop_back(); - - all_text.pop(); - - // list_of_token.pop_back(); - // add_on.push_back(findLibFunction(v)); - script->insert((char *)((*_stdlib[findLibFunction(vchar)]).c_str())); - - // script->previousChar (); - - continue; - } - } - else if (prev.getType() == TokenDiese && !_for_display) - { - nbReadToken--; - if (findLibFunction(vchar) > -1) - { - - _tks->pop_back(); - // printf("token %d\n",_tks->back().type); - all_text.pop(); - - // list_of_token.pop_back(); - // add_on.push_back(findLibFunction(v)); - script->insertAtEnd((char *)((*_stdlib[findLibFunction(vchar)]).c_str())); - // printf("ll%d %s\n",findLibFunction(v),(*_stdlib[findLibFunction(v)]).c_str()); - script->nextChar(); - // script->previousChar (); - continue; - } - } - else if (prev.getType() == TokenKeywordDefine && !_for_display) - { - _tks->pop_back(); - all_text.pop(); - // nbReadToken--; - - newdef.name = vchar; - newdef.content = ""; - - c2 = script->nextChar(); - // c2 = script->nextChar(); - while (c2 != '\n' and c2 != EOF_TEXT) - { - newdef.content = newdef.content + c2; - c2 = script->nextChar(); - } - // printf("on push |%s|\n",newdef.content.c_str()); - define_list.push_back(newdef); - if (increae_line) - _token_line++; - // script->previousChar(); - continue; - } - else if (prev.getType() == TokenKeywordStruct && !_for_display) - { - userDefinedVarTypeNames.push_back(vchar); - t.type = (int)TokenUserDefinedName; - // continue; - } - } - if (!_for_display) // on ne remplace pas lorsque l'on display - { - if (getDefine(vchar) != NULL) - { - - script->insert((char *)(getDefine(vchar))); - script->nextChar(); - // nbReadToken--; - continue; - } - } - } - pos_in_line = newpos - 1; + // script->insert(import); - t.addText(vchar); - //_tks->push(t); - _tks->push(t); - nbReadToken++; - continue; - } + _sav_token_line = _token_line; + nbReadToken--; + if (findLibFunction(vchar) > -1) { - if (isIn0_9(c)) - { - // //printf("on a %c\n",c); - vchar = ""; - int newpos = pos_in_line; - while (isIn0_9_x_b(c)) - { - vchar += c; - c = script->nextChar(); - newpos++; + _tks->pop_back(); + + all_text.pop(); + + // list_of_token.pop_back(); + // add_on.push_back(findLibFunction(v)); + script->insert( + (char *)((*_stdlib[findLibFunction(vchar)]).c_str())); + + // script->previousChar (); + + continue; } - script->previousChar(); // on revient un caractere en arriere - t = transNumber(vchar); - // t._vartype=NULL; - t.line = _token_line; - t.pos = pos_in_line; - //_tks->push(t); - - _tks->push(t); - nbReadToken++; - pos_in_line = newpos - 1; - continue; - } - if (c == ';') - { - // token t; - // t.type = TokenSemicolon; - // t._vartype = NULL; - t = Token(TokenSemicolon, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText(";"); - // t.line = _token_line; - t.pos = pos_in_line; - // _tks->push(t); - _tks->push(t); - nbReadToken++; - continue; - } - if (c == '\t') - { - // token t; - // t.type = TokenSpace; - // t._vartype = NULL; - t = Token(TokenSpace, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("\t"); - // t.line = _token_line; - // t.pos = pos_in_line; - if (_for_display) - { - //_tks->push(t); - // nbReadToken++; - _tks->push(t); + } else if (prev.getType() == TokenDiese && !_for_display) { + nbReadToken--; + if (findLibFunction(vchar) > -1) { + + _tks->pop_back(); + // printf("token %d\n",_tks->back().type); + all_text.pop(); + + // list_of_token.pop_back(); + // add_on.push_back(findLibFunction(v)); + script->insertAtEnd( + (char *)((*_stdlib[findLibFunction(vchar)]).c_str())); + // printf("ll%d + // %s\n",findLibFunction(v),(*_stdlib[findLibFunction(v)]).c_str()); + script->nextChar(); + // script->previousChar (); + continue; } - continue; - } - if (c == '&') - { + } else if (prev.getType() == TokenKeywordDefine && !_for_display) { + _tks->pop_back(); + all_text.pop(); + // nbReadToken--; + + newdef.name = vchar; + newdef.content = ""; + c2 = script->nextChar(); - if (c2 == '&') - { - t = Token(TokenDoubleUppersand, EOF_VARTYPE); - // t._vartype = NULL; - // t.type = TokenDoubleEqual; - if (_for_display) - t.addText("&&"); - t.line = _token_line; - t.pos = pos_in_line; - _tks->push(t); - // nbReadToken++; - - continue; - } - else - { - script->previousChar(); - // token t; - // t._vartype = NULL; - // t.type = TokenEqual; - // t.line = _token_line; - t = Token(TokenUppersand, EOF_VARTYPE, _token_line); - t.pos = pos_in_line; - if (_for_display) - t.addText("&"); - _tks->push(t); - nbReadToken++; - continue; + // c2 = script->nextChar(); + while (c2 != '\n' and c2 != EOF_TEXT) { + newdef.content = newdef.content + c2; + c2 = script->nextChar(); } - } - if (c == '#') - { - // token t; - // t.type = TokenDiese; - // t._vartype = NULL; - t = Token(TokenDiese, EOF_VARTYPE, _token_line); - if (_for_display) - { - t.addText("#"); - // _tks->push(t); - } - // t.line = _token_line; - // t.pos = pos_in_line; - _tks->push(t); - // nbReadToken++; - continue; - } - if (c == '(') - { - // token t; - // t.type = TokenOpenParenthesis; - // t._vartype = NULL; - t = Token(TokenOpenParenthesis, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("("); - // t.line = _token_line; - t.pos = pos_in_line; - //_tks->push(t); - _tks->push(t); - // nbReadToken++; - continue; - } - if (c == '%') - { - // token t; - // t.type = TokenModulo; - // t._vartype = NULL; - t = Token(TokenModulo, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("%"); - // t.line = _token_line; - t.pos = pos_in_line; - //_tks->push(t); - _tks->push(t); - nbReadToken++; - continue; - } - if (c == ')') - { - // token t; - // t.type = TokenCloseParenthesis; - // t._vartype = NULL; - t = Token(TokenCloseParenthesis, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText(")"); - // t.line = _token_line; - t.pos = pos_in_line; - //_tks->push(t); - _tks->push(t); - nbReadToken++; - continue; - } - if (c == '{') - { - // token t; - // t.type = TokenOpenCurlyBracket; - // t._vartype = NULL; - t = Token(TokenOpenCurlyBracket, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("{"); - // t.line = _token_line; - // t.pos = pos_in_line; - //_tks->push(t); - _tks->push(t); - nbReadToken++; + // printf("on push |%s|\n",newdef.content.c_str()); + define_list.push_back(newdef); + if (increae_line) + _token_line++; + // script->previousChar(); continue; + } else if (prev.getType() == TokenKeywordStruct && !_for_display) { + userDefinedVarTypeNames.push_back(vchar); + t.type = (int)TokenUserDefinedName; + // continue; + } } - if (c == '}') + if (!_for_display) // on ne remplace pas lorsque l'on display { - // token t; - // t.type = TokenCloseCurlyBracket; - // t._vartype = NULL; - t = Token(TokenCloseCurlyBracket, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("}"); - // t.line = _token_line; - t.pos = pos_in_line; - // _tks->push(t); - _tks->push(t); - nbReadToken++; + if (getDefine(vchar) != NULL) { + + script->insert((char *)(getDefine(vchar))); + script->nextChar(); + // nbReadToken--; continue; + } } - if (c == '[') - { - // token t; - // t.type = TokenOpenBracket; - // t._vartype = NULL; - t = Token(TokenOpenBracket, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("["); - // t.line = _token_line; - t.pos = pos_in_line; - //_tks->push(t); - _tks->push(t); - nbReadToken++; - continue; + } + pos_in_line = newpos - 1; + + t.addText(vchar); + //_tks->push(t); + _tks->push(t); + nbReadToken++; + continue; + } + + if (isIn0_9(c)) { + // //printf("on a %c\n",c); + vchar = ""; + int newpos = pos_in_line; + while (isIn0_9_x_b(c)) { + vchar += c; + c = script->nextChar(); + newpos++; + } + script->previousChar(); // on revient un caractere en arriere + t = transNumber(vchar); + // t._vartype=NULL; + t.line = _token_line; + t.pos = pos_in_line; + //_tks->push(t); + + _tks->push(t); + nbReadToken++; + pos_in_line = newpos - 1; + continue; + } + if (c == ';') { + // token t; + // t.type = TokenSemicolon; + // t._vartype = NULL; + t = Token(TokenSemicolon, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText(";"); + // t.line = _token_line; + t.pos = pos_in_line; + // _tks->push(t); + _tks->push(t); + nbReadToken++; + continue; + } + if (c == '\t') { + // token t; + // t.type = TokenSpace; + // t._vartype = NULL; + t = Token(TokenSpace, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("\t"); + // t.line = _token_line; + // t.pos = pos_in_line; + if (_for_display) { + //_tks->push(t); + // nbReadToken++; + _tks->push(t); + } + continue; + } + if (c == '&') { + c2 = script->nextChar(); + if (c2 == '&') { + t = Token(TokenDoubleUppersand, EOF_VARTYPE); + // t._vartype = NULL; + // t.type = TokenDoubleEqual; + if (_for_display) + t.addText("&&"); + t.line = _token_line; + t.pos = pos_in_line; + _tks->push(t); + // nbReadToken++; + + continue; + } else { + script->previousChar(); + // token t; + // t._vartype = NULL; + // t.type = TokenEqual; + // t.line = _token_line; + t = Token(TokenUppersand, EOF_VARTYPE, _token_line); + t.pos = pos_in_line; + if (_for_display) + t.addText("&"); + _tks->push(t); + nbReadToken++; + continue; + } + } + if (c == '#') { + // token t; + // t.type = TokenDiese; + // t._vartype = NULL; + t = Token(TokenDiese, EOF_VARTYPE, _token_line); + if (_for_display) { + t.addText("#"); + // _tks->push(t); + } + // t.line = _token_line; + // t.pos = pos_in_line; + _tks->push(t); + // nbReadToken++; + continue; + } + if (c == '(') { + // token t; + // t.type = TokenOpenParenthesis; + // t._vartype = NULL; + t = Token(TokenOpenParenthesis, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("("); + // t.line = _token_line; + t.pos = pos_in_line; + //_tks->push(t); + _tks->push(t); + // nbReadToken++; + continue; + } + if (c == '%') { + // token t; + // t.type = TokenModulo; + // t._vartype = NULL; + t = Token(TokenModulo, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("%"); + // t.line = _token_line; + t.pos = pos_in_line; + //_tks->push(t); + _tks->push(t); + nbReadToken++; + continue; + } + if (c == ')') { + // token t; + // t.type = TokenCloseParenthesis; + // t._vartype = NULL; + t = Token(TokenCloseParenthesis, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText(")"); + // t.line = _token_line; + t.pos = pos_in_line; + //_tks->push(t); + _tks->push(t); + nbReadToken++; + continue; + } + if (c == '{') { + // token t; + // t.type = TokenOpenCurlyBracket; + // t._vartype = NULL; + t = Token(TokenOpenCurlyBracket, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("{"); + // t.line = _token_line; + // t.pos = pos_in_line; + //_tks->push(t); + _tks->push(t); + nbReadToken++; + continue; + } + if (c == '}') { + // token t; + // t.type = TokenCloseCurlyBracket; + // t._vartype = NULL; + t = Token(TokenCloseCurlyBracket, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("}"); + // t.line = _token_line; + t.pos = pos_in_line; + // _tks->push(t); + _tks->push(t); + nbReadToken++; + continue; + } + if (c == '[') { + // token t; + // t.type = TokenOpenBracket; + // t._vartype = NULL; + t = Token(TokenOpenBracket, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("["); + // t.line = _token_line; + t.pos = pos_in_line; + //_tks->push(t); + _tks->push(t); + nbReadToken++; + continue; + } + if (c == ']') { + // token t; + // t.type = TokenCloseBracket; + // t._vartype = NULL; + c2 = script->nextChar(); + if (c2 == '[') { + t = Token(TokenComma, EOF_VARTYPE); + // t._vartype = NULL; + // t.type = TokenDoubleEqual; + if (_for_display) + t.addText("]["); + t.line = _token_line; + t.pos = pos_in_line; + _tks->push(t); + nbReadToken++; + + continue; + } else { + script->previousChar(); + // token t; + // t._vartype = NULL; + // t.type = TokenEqual; + // t.line = _token_line; + t = Token(TokenCloseBracket, EOF_VARTYPE, _token_line); + t.pos = pos_in_line; + if (_for_display) + t.addText("]"); + _tks->push(t); + nbReadToken++; + continue; + } + } + if (c == '/') { + char c2 = script->nextChar(); + if (c2 == '/') { + // Token t; + t._vartype = EOF_VARTYPE; + t.type = (int)TokenLineComment; + string str = "//"; + + c2 = script->nextChar(); + while (c2 != '\n' and c2 != EOF_TEXT) { + str = str + c2; // string_format("%s%c", t.getText(), c2); + c2 = script->nextChar(); } - if (c == ']') - { - // token t; - // t.type = TokenCloseBracket; - // t._vartype = NULL; - c2 = script->nextChar(); - if (c2 == '[') - { - t = Token(TokenComma, EOF_VARTYPE); - // t._vartype = NULL; - // t.type = TokenDoubleEqual; - if (_for_display) - t.addText("]["); - t.line = _token_line; - t.pos = pos_in_line; - _tks->push(t); - nbReadToken++; - - continue; - } - else - { - script->previousChar(); - // token t; - // t._vartype = NULL; - // t.type = TokenEqual; - // t.line = _token_line; - t = Token(TokenCloseBracket, EOF_VARTYPE, _token_line); - t.pos = pos_in_line; - if (_for_display) - t.addText("]"); - _tks->push(t); - nbReadToken++; - continue; - } + str = str + '\0'; + // c2 = script->previousChar(); + if (_for_display) { + c2 = script->previousChar(); + t.addText(str); } - if (c == '/') - { - char c2 = script->nextChar(); - if (c2 == '/') - { - // Token t; - t._vartype = EOF_VARTYPE; - t.type = (int)TokenLineComment; - string str = "//"; - - c2 = script->nextChar(); - while (c2 != '\n' and c2 != EOF_TEXT) - { - str = str + c2; // string_format("%s%c", t.getText(), c2); - c2 = script->nextChar(); - } - str = str + '\0'; - // c2 = script->previousChar(); - if (_for_display) - { - c2 = script->previousChar(); - t.addText(str); - } - t.line = _token_line; - t.pos = pos_in_line; - if (increae_line) - _token_line++; - if (_for_display) - { - // script->previousChar(); - _tks->push(t); - nbReadToken++; - } - continue; - } - else if (c2 == '=') - { - t = Token(TokenSlashEqual, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("/="); - // t.line = _token_line; - t.pos = pos_in_line; - // _tks->push(t); - // nbReadToken++; - _tks->push(t); - continue; - } - else if (c2 == '*') - { - // Token t; - t._vartype = EOF_VARTYPE; - t.type = (int)TokenLineComment; - - string str = "/*"; - c = script->nextChar(); - c2 = script->nextChar(); - while ((c != '*' or c2 != '/') and c2 != EOF_TEXT and c != EOF_TEXT) // stop when (c=* and c2=/) or c=0 or c2=0 - { - if (_for_display) - // str = string_format("%s%c", t.getText(), c); - str = str + c2; - c = c2; - c2 = script->nextChar(); - } - if (_for_display) - t.addText(str); - t.line = _token_line; - t.pos = pos_in_line; - if (_for_display) - { - _tks->push(t); - nbReadToken++; - } - continue; - } - else - { - script->previousChar(); - // Token t; - t.type = (int)TokenSlash; - t._vartype = EOF_VARTYPE; - if (_for_display) - t.addText("/"); - t.line = _token_line; - t.pos = pos_in_line; - _tks->push(t); - nbReadToken++; - continue; - } + t.line = _token_line; + t.pos = pos_in_line; + if (increae_line) + _token_line++; + if (_for_display) { + // script->previousChar(); + _tks->push(t); + nbReadToken++; } - if (c == '-') + continue; + } else if (c2 == '=') { + t = Token(TokenSlashEqual, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("/="); + // t.line = _token_line; + t.pos = pos_in_line; + // _tks->push(t); + // nbReadToken++; + _tks->push(t); + continue; + } else if (c2 == '*') { + // Token t; + t._vartype = EOF_VARTYPE; + t.type = (int)TokenLineComment; + + string str = "/*"; + c = script->nextChar(); + c2 = script->nextChar(); + while ((c != '*' or c2 != '/') and c2 != EOF_TEXT and + c != EOF_TEXT) // stop when (c=* and c2=/) or c=0 or c2=0 { - c2 = script->nextChar(); - if (c2 == '-') - { - // token t; - // t._vartype = NULL; - // t.type = TokenPlusPlus; - t = Token(TokenMinusMinus, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("--"); - // t.line = _token_line; - t.pos = pos_in_line; - // _tks->push(t); - // nbReadToken++; - _tks->push(t); - continue; - } - if (c2 == '=') - { - // token t; - // t._vartype = NULL; - // t.type = TokenPlusPlus; - t = Token(TokenMinusEqual, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("-="); - // t.line = _token_line; - t.pos = pos_in_line; - // _tks->push(t); - // nbReadToken++; - _tks->push(t); - continue; - } - else - { - script->previousChar(); - // token t; - // t._vartype = NULL; - // t.type = TokenAddition; - t = Token(TokenSubstraction, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("-"); - // t.line = _token_line; - t.pos = pos_in_line; - // _tks->push(t); - _tks->push(t); - // nbReadToken++; - continue; - } - // Token t; + if (_for_display) + // str = string_format("%s%c", t.getText(), c); + str = str + c2; + c = c2; + c2 = script->nextChar(); } - if (c == ' ') - { - // Token t; - t.line = _token_line; - t.pos = pos_in_line; - string str = ""; - - while (c == ' ') - { - c = script->nextChar(); - pos_in_line++; - str = str + " "; - } - if (_for_display) - t.addText(str); - script->previousChar(); // on revient un caractere en arriere - pos_in_line--; - t.type = TokenSpace; - // t.addText(" "; - if (_for_display) - { - _tks->push(t); - nbReadToken++; - } - continue; + if (_for_display) + t.addText(str); + t.line = _token_line; + t.pos = pos_in_line; + if (_for_display) { + _tks->push(t); + nbReadToken++; } - if (c == '"') - { - vchar = ""; - // Token t; - t._vartype = EOF_VARTYPE; - t.line = _token_line; - t.pos = pos_in_line; + continue; + } else { + script->previousChar(); + // Token t; + t.type = (int)TokenSlash; + t._vartype = EOF_VARTYPE; + if (_for_display) + t.addText("/"); + t.line = _token_line; + t.pos = pos_in_line; + _tks->push(t); + nbReadToken++; + continue; + } + } + if (c == '-') { + c2 = script->nextChar(); + if (c2 == '-') { + // token t; + // t._vartype = NULL; + // t.type = TokenPlusPlus; + t = Token(TokenMinusMinus, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("--"); + // t.line = _token_line; + t.pos = pos_in_line; + // _tks->push(t); + // nbReadToken++; + _tks->push(t); + continue; + } + if (c2 == '=') { + // token t; + // t._vartype = NULL; + // t.type = TokenPlusPlus; + t = Token(TokenMinusEqual, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("-="); + // t.line = _token_line; + t.pos = pos_in_line; + // _tks->push(t); + // nbReadToken++; + _tks->push(t); + continue; + } else { + script->previousChar(); + // token t; + // t._vartype = NULL; + // t.type = TokenAddition; + t = Token(TokenSubstraction, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("-"); + // t.line = _token_line; + t.pos = pos_in_line; + // _tks->push(t); + _tks->push(t); + // nbReadToken++; + continue; + } + // Token t; + } + if (c == ' ') { + // Token t; + t.line = _token_line; + t.pos = pos_in_line; + string str = ""; + + while (c == ' ') { + c = script->nextChar(); + pos_in_line++; + str = str + " "; + } + if (_for_display) + t.addText(str); + script->previousChar(); // on revient un caractere en arriere + pos_in_line--; + t.type = TokenSpace; + // t.addText(" "; + if (_for_display) { + _tks->push(t); + nbReadToken++; + } + continue; + } + if (c == '"') { + vchar = ""; + // Token t; + t._vartype = EOF_VARTYPE; + t.line = _token_line; + t.pos = pos_in_line; + vchar += c; + c = script->nextChar(); + pos_in_line++; + while (c != '"' && c != EOF_TEXT) { + if (!_for_display) { + char c2 = script->nextChar(); + if (c == '\\' and c2 == 'n') { + c = '\x0d'; + vchar += c; + c = '\x0a'; vchar += c; c = script->nextChar(); - pos_in_line++; - while (c != '"' && c != EOF_TEXT) - { - if (!_for_display) - { - char c2 = script->nextChar(); - if (c == '\\' and c2 == 'n') - { - c = '\x0d'; - vchar += c; - c = '\x0a'; - vchar += c; - c = script->nextChar(); - } - else - { - vchar += c; - c = c2; - } - - pos_in_line++; - } - else - { - vchar += c; - c = script->nextChar(); - pos_in_line++; - } - } - // script->previousChar(); //on revient un caractere en arriere - // pos--; + } else { vchar += c; - t.type = (int)TokenString; - t.addText(vchar); - _tks->push(t); - nbReadToken++; - continue; - } - if (c == '\n') - { - // Token t; - t.type = (int)TokenNewline; - if (_for_display) - t.addText("\r\n"); - t.line = _token_line; - t.pos = pos_in_line; - if (increae_line) - _token_line++; - pos_in_line = 0; - if (_for_display) - _tks->push(t); - #ifdef PARSER_DEBUG - if (!_for_display) - { - - printf("line;%d\n\r", _token_line); - } - #endif - continue; - } - if (c == '?') - { - // Token t; - t = Token(TokenQuestionMark, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("?"); - t.line = _token_line; - t.pos = pos_in_line; - //_token_line++; - // pos = 0; - // if (_for_display) - _tks->push(t); - continue; - } - if (c == '.') - { - // Token t; - t.type = (int)TokenMember; - if (_for_display) - t.addText("."); - t.line = _token_line; - t.pos = pos_in_line; - //_token_line++; - // pos = 0; - // if (_for_display) - _tks->push(t); - // nbReadToken++; - continue; - } - if (c == '^') - { - // Token t; - t.type = (int)TokenPower; - if (_for_display) - t.addText("^"); - t.line = _token_line; - t.pos = pos_in_line; - //_token_line++; - // pos = 0; - // if (_for_display) - _tks->push(t); - // nbReadToken++; - continue; - } - if (c == '@') - { - // Token t; - t.type = (int)TokenUnknown; - if (_for_display) - t.addText("@"); - _token_line = _sav_token_line; - if (_for_display) - _tks->push(t); - continue; - } - if (c == '\'') - { - // Token t; - t.type = (int)TokenUnknown; - if (_for_display) - t.addText("\'"); - t.line = _token_line; - t.pos = pos_in_line; - // _token_line++; - // pos = 0; - if (_for_display) - _tks->push(t); - continue; - } - if (c == ':') - { - // Token t; - t = Token(TokenColon, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText(":"); - t.line = _token_line; - t.pos = pos_in_line; - //_token_line++; - // pos = 0; - // if (_for_display) - _tks->push(t); - continue; - } - if (c == '*') - { - c2 = script->nextChar(); - if (c2 == '=') - { - // token t; - // t._vartype = NULL; - // t.type = TokenPlusPlus; - t = Token(TokenStarEqual, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("*="); - // t.line = _token_line; - t.pos = pos_in_line; - // _tks->push(t); - // nbReadToken++; - _tks->push(t); - continue; - } - else - { - script->previousChar(); - // token t; - // t._vartype = NULL; - // t.type = TokenAddition; - t = Token(TokenStar, EOF_VARTYPE, _token_line); - if (_for_display) - t.addText("*"); - // t.line = _token_line; - t.pos = pos_in_line; - // _tks->push(t); - _tks->push(t); - nbReadToken++; - continue; - } - // Token t; - } - if (c == '|') - { - c2 = script->nextChar(); - if (c2 == '|') - { - t = Token(TokenDoubleOr, EOF_VARTYPE); - // t._vartype = NULL; - // t.type = TokenDoubleEqual; - if (_for_display) - t.addText("||"); - t.line = _token_line; - t.pos = pos_in_line; - _tks->push(t); - nbReadToken++; - - continue; - } - else - { - script->previousChar(); - // token t; - // t._vartype = NULL; - // t.type = TokenEqual; - // t.line = _token_line; - t = Token(TokenKeywordOr, EOF_VARTYPE, _token_line); - t.pos = pos_in_line; - if (_for_display) - t.addText("|"); - _tks->push(t); - nbReadToken++; - continue; - } - } - if (c == ',') - { - // Token t; - t._vartype = EOF_VARTYPE; - t.type = (int)TokenComma; - if (_for_display) - t.addText(","); - t.line = _token_line; - t.pos = pos_in_line; - _tks->push(t); - // nbReadToken++; - continue; - } - if (c == 27) - { - c2 = script->nextChar(); - continue; - } - //if (!_for_display) - // printf("Error invalid character |%d| line :%d pos: %d\n", c, _token_line, pos); - } - - if (script->currentChar() == EOF_TEXT) - { - if (_tks->back().getType() != TokenEndOfFile) - { - t = Token(TokenEndOfFile, EOF_VARTYPE, _token_line); - - _tks->push(t); + c = c2; + } + + pos_in_line++; + } else { + vchar += c; + c = script->nextChar(); + pos_in_line++; } - } - // return list_of_token; - return nbReadToken - 1; + } + // script->previousChar(); //on revient un caractere en arriere + // pos--; + vchar += c; + t.type = (int)TokenString; + t.addText(vchar); + _tks->push(t); + nbReadToken++; + continue; + } + if (c == '\n') { + // Token t; + t.type = (int)TokenNewline; + if (_for_display) + t.addText("\r\n"); + t.line = _token_line; + t.pos = pos_in_line; + if (increae_line) + _token_line++; + pos_in_line = 0; + if (_for_display) + _tks->push(t); +#ifdef PARSER_DEBUG + if (!_for_display) { + + printf("line;%d\n\r", _token_line); + } +#endif + continue; + } + if (c == '?') { + // Token t; + t = Token(TokenQuestionMark, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("?"); + t.line = _token_line; + t.pos = pos_in_line; + //_token_line++; + // pos = 0; + // if (_for_display) + _tks->push(t); + continue; + } + if (c == '.') { + // Token t; + t.type = (int)TokenMember; + if (_for_display) + t.addText("."); + t.line = _token_line; + t.pos = pos_in_line; + //_token_line++; + // pos = 0; + // if (_for_display) + _tks->push(t); + // nbReadToken++; + continue; + } + if (c == '^') { + // Token t; + t.type = (int)TokenPower; + if (_for_display) + t.addText("^"); + t.line = _token_line; + t.pos = pos_in_line; + //_token_line++; + // pos = 0; + // if (_for_display) + _tks->push(t); + // nbReadToken++; + continue; + } + if (c == '@') { + // Token t; + t.type = (int)TokenUnknown; + if (_for_display) + t.addText("@"); + _token_line = _sav_token_line; + if (_for_display) + _tks->push(t); + continue; + } + if (c == '\'') { + // Token t; + t.type = (int)TokenUnknown; + if (_for_display) + t.addText("\'"); + t.line = _token_line; + t.pos = pos_in_line; + // _token_line++; + // pos = 0; + if (_for_display) + _tks->push(t); + continue; + } + if (c == ':') { + // Token t; + t = Token(TokenColon, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText(":"); + t.line = _token_line; + t.pos = pos_in_line; + //_token_line++; + // pos = 0; + // if (_for_display) + _tks->push(t); + continue; + } + if (c == '*') { + c2 = script->nextChar(); + if (c2 == '=') { + // token t; + // t._vartype = NULL; + // t.type = TokenPlusPlus; + t = Token(TokenStarEqual, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("*="); + // t.line = _token_line; + t.pos = pos_in_line; + // _tks->push(t); + // nbReadToken++; + _tks->push(t); + continue; + } else { + script->previousChar(); + // token t; + // t._vartype = NULL; + // t.type = TokenAddition; + t = Token(TokenStar, EOF_VARTYPE, _token_line); + if (_for_display) + t.addText("*"); + // t.line = _token_line; + t.pos = pos_in_line; + // _tks->push(t); + _tks->push(t); + nbReadToken++; + continue; + } + // Token t; + } + if (c == '|') { + c2 = script->nextChar(); + if (c2 == '|') { + t = Token(TokenDoubleOr, EOF_VARTYPE); + // t._vartype = NULL; + // t.type = TokenDoubleEqual; + if (_for_display) + t.addText("||"); + t.line = _token_line; + t.pos = pos_in_line; + _tks->push(t); + nbReadToken++; + + continue; + } else { + script->previousChar(); + // token t; + // t._vartype = NULL; + // t.type = TokenEqual; + // t.line = _token_line; + t = Token(TokenKeywordOr, EOF_VARTYPE, _token_line); + t.pos = pos_in_line; + if (_for_display) + t.addText("|"); + _tks->push(t); + nbReadToken++; + continue; + } + } + if (c == ',') { + // Token t; + t._vartype = EOF_VARTYPE; + t.type = (int)TokenComma; + if (_for_display) + t.addText(","); + t.line = _token_line; + t.pos = pos_in_line; + _tks->push(t); + // nbReadToken++; + continue; + } + if (c == 27) { + c2 = script->nextChar(); + continue; + } + // if (!_for_display) + // printf("Error invalid character |%d| line :%d pos: %d\n", c, + // _token_line, pos); + } + + if (script->currentChar() == EOF_TEXT) { + if (_tks->back().getType() != TokenEndOfFile) { + t = Token(TokenEndOfFile, EOF_VARTYPE, _token_line); + + _tks->push(t); + } + } + // return list_of_token; + return nbReadToken - 1; } #ifdef __CONSOLE_ESP32