Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all...

As you know I like making code backwards compatible for as many platforms as possible.

This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam on a stock A1200 only to expansions to 68060, FPU, MMU and serious memory for the day.
This is designed purely for my stock A1200 with HDD and 4MB of extra FastRam and Python 1.4.0, the only version that does not require MMU and FPU.
It requires ADE the *NIX emulator and gcc 2.95.3 to work and the result gives a simple calculator for ADE as bc, dc and others do not exist.
AND before anyone mentions the 'r' prefix on the 'docstring' variable = r""" Some raw text... """ , Pythons 1.4.0 to 1.6.x do NOT have that facility. So a basic docstring is required. Therefore ALL escaped text MUST have the escqpe escaped, for example, \n becomes \\n .

I uploaded it on 30-04-2019 and it reached 179 dls in 14 days, it is now at 197 dls as of 20-05-2019.
Aminet - dev/gcc/C_Source_In_Python.py.txt

( @ Corona688, your awk DFT is now at 734 dls, Aminet - dev/gcc/DFT-FFT.awk.txt and I expect it to be over 1000 by the end of the year. )

It is not easy working out all the permutations and combinations of a language where each major version is near entirely different from its predecessor but this is the result, note my MBP booting into Linux Mint 19:

import sys
import os

EMBED_C="""
/* Simple Calculator in C. */
/* Compiled througth ADE, the *NIX emulator, using:- */
/* gcc -Wall -pedantic -ansi -o calc mycalc.c -lm */

/* This version is purely for any AMIGA Python from 1.4.0 upwards. */
/* Also tested on Python 2.7.10 and 3.5.2, on OSX 10.14.3 and Linux Mint 19. */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double SOLUTION;
double FLOAT_1;
double FLOAT_2;
char OPERATOR;

double calc(double FLOAT_1, double FLOAT_2, char OPERATOR)
{
    double SOLUTION = 0.0;
    switch(OPERATOR)
    {
        case '+':
            SOLUTION = FLOAT_1 + FLOAT_2;
            break;
        case '-':
            SOLUTION = FLOAT_1 - FLOAT_2;
            break;
        case 'x':
            SOLUTION = FLOAT_1 * FLOAT_2;
            break;
        case '/':
            SOLUTION = FLOAT_1 / FLOAT_2;
            break;
        case '^':
            SOLUTION = pow(FLOAT_1, FLOAT_2);
            break;
        case 'r':
            FLOAT_2 = 1.0 / FLOAT_2;
            SOLUTION = pow(FLOAT_1, FLOAT_2);
            break;
    }
    return SOLUTION;
}

int main(int argc, char *argv[])
{
    char OPERATOR;
    double FLOAT_1;
    double FLOAT_2;
    double SOLUTION;

    if (argc <= 3)
    {
        printf("\\nERROR!\\n\\n");
        printf("Usage: calc FLOAT_1 <|+|-|x|/|^|r|> FLOAT_2<CR>\\n");
        printf("Where: '+'=ADD; '-'=SUBTRACT; 'x'=MULTIPLY; '/'=DIVIDE;\\n");
        printf("       '^'=POWER; and 'r'=NTHROOT.\\n\\n");
        printf("Example: calc 781.37 r 3.3<CR>, (NOTE: spaces), where <CR> is the ENTER key!\\n\\n");
        /* Google result: 7.52702316103. */
        exit(1);
    }

    SOLUTION = 0.0;
    OPERATOR = *argv[2];
    FLOAT_1 = strtod(argv[1], NULL);
    FLOAT_2 = strtod(argv[3], NULL);

    SOLUTION = calc(FLOAT_1, FLOAT_2, OPERATOR);
    printf("%.11f\\n", SOLUTION);
    return(0);
}
"""

# Create the C file.
myfile = open("mycalc.c", "w")
myfile.write(EMBED_C)
myfile.close()

# Print the whole C file to stdout.
print(EMBED_C)

# Compile the C file.
print("Compile mycalc.c to calc using:-")
print("os.system(\"gcc -Wall -pedantic -ansi -o calc mycalc.c -lm\")\n")
os.system("gcc -Wall -pedantic -ansi -o calc mycalc.c -lm")

# Now run it...
print("Run calc to calculate 781.37 to root 3.3 and save in a _variable_:-")
# The next few lines takes the calculation and places it into 'result'.
if sys.platform == 'amiga':
    calc_result = os.popen("calc 781.37 r 3.3")
else:
    calc_result = os.popen("./calc 781.37 r 3.3")
result = calc_result.read()
calc_result.close()

# This is the result.
print(result)
print("Google value using 781.37**(1.0/3.3) is:-\n7.52702316103")
sys.exit()

Results on Linux Mint 19, default bash terminal, Python 2.7.x, gcc 7.3.0.

bazza@amiga-MacBookPro:~$ cd Desktop/Code/Python
bazza@amiga-MacBookPro:~/Desktop/Code/Python$ python embed_C.py

/* Simple Calculator in C. */
/* Compiled througth ADE, the *NIX emulator, using:- */
/* gcc -Wall -pedantic -ansi -o calc mycalc.c -lm */

/* This version is purely for any AMIGA Python from 1.4.0 upwards. */
/* Also tested on Python 2.7.10 and 3.5.2, on OSX 10.14.3 and Linux Mint 19. */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double SOLUTION;
double FLOAT_1;
double FLOAT_2;
char OPERATOR;

double calc(double FLOAT_1, double FLOAT_2, char OPERATOR)
{
    double SOLUTION = 0.0;
    switch(OPERATOR)
    {
        case '+':
            SOLUTION = FLOAT_1 + FLOAT_2;
            break;
        case '-':
            SOLUTION = FLOAT_1 - FLOAT_2;
            break;
        case 'x':
            SOLUTION = FLOAT_1 * FLOAT_2;
            break;
        case '/':
            SOLUTION = FLOAT_1 / FLOAT_2;
            break;
        case '^':
            SOLUTION = pow(FLOAT_1, FLOAT_2);
            break;
        case 'r':
            FLOAT_2 = 1.0 / FLOAT_2;
            SOLUTION = pow(FLOAT_1, FLOAT_2);
            break;
    }
    return SOLUTION;
}

int main(int argc, char *argv[])
{
    char OPERATOR;
    double FLOAT_1;
    double FLOAT_2;
    double SOLUTION;

    if (argc <= 3)
    {
        printf("\nERROR!\n\n");
        printf("Usage: calc FLOAT_1 <|+|-|x|/|^|r|> FLOAT_2<CR>\n");
        printf("Where: '+'=ADD; '-'=SUBTRACT; 'x'=MULTIPLY; '/'=DIVIDE;\n");
        printf("       '^'=POWER; and 'r'=NTHROOT.\n\n");
        printf("Example: calc 781.37 r 3.3<CR>, (NOTE: spaces), where <CR> is the ENTER key!\n\n");
        /* Google result: 7.52702316103. */
        exit(1);
    }

    SOLUTION = 0.0;
    OPERATOR = *argv[2];
    FLOAT_1 = strtod(argv[1], NULL);
    FLOAT_2 = strtod(argv[3], NULL);

    SOLUTION = calc(FLOAT_1, FLOAT_2, OPERATOR);
    printf("%.11f\n", SOLUTION);
    return(0);
}

Compile mycalc.c to calc using:-
os.system("gcc -Wall -pedantic -ansi -o calc mycalc.c -lm")

Run calc to calculate 781.37 to root 3.3 and save in a _variable_:-
7.52702316103

Google value using 781.37**(1.0/3.3) is:-
7.52702316103
bazza@amiga-MacBookPro:~/Desktop/Code/Python$ _

So even though these Python versions are worlds apart, if the minimum is found between them all then everyone can share fully usable code for, now esoteric, platforms.
( Long live the AMIGA... <wink> )

EDIT:
Now tested on Python 3.6.7 and 3.7.1 on Linux Mint 19...

1 Like

Note that your four global variables (SOLUTION, FLOAT_1, FLOAT_2, and OPERATOR) are never referenced in your code because both of your subroutines (main() and calc()) have local variables with the same names.

If you get rid of the global variables, you should get exactly the same results from a binary that might be a few bytes smaller.

1 Like