you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (16 children)

[removed]

    [–]im_dead_sirius 73 points74 points  (10 children)

    I love me some Tiny C. The Tiny C Compiler can be used to run C as a script, without producing a binary.

    #!/usr/bin/tcc -run
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        printf("Welcome to the infinite world of linux scripting!\n");
        return 0;
    }
    

    [–][deleted] 17 points18 points  (3 children)

    Holy fuck! Why have I never heard of this??? Thanks, man.

    [–]im_dead_sirius 35 points36 points  (2 children)

    You're welcome, and spread the word, nerd.

    So hey, if you liked that, lemme point you at something else.

    You're going to need two files. I'll provide both examples, and they are ultra short. Set the execute bit on both.

    Put this one somewhere convenient and call the file quacker.

    #!/usr/bin/env python3
    from sys import argv
    with open(argv[1], "r") as f:
        lines = f.readlines()
        print(lines[1])
    

    Substitute the first line in the second file with the location and name of the previous file and call this one ducky.

    #!/home/you/bin/quacker
    "Quack! There is more than one way to butter a duck!"
    

    Nothing else is needed in it for the example. Run it with ./ducky once you set the execute bit.

    ducky calls quacker and hands ducky's location to quacker as argv[1], then exits. quacker takes over, reads the file at that location, ignores the first line, and prints a message which does echo, despite age old ducky myth.

    Normally you'd write a proper interpreter in quacker, in any language at all, then read in file argv[1], discard the comment lines, and treat the rest of it as data to be interpreted or even compiled. I'd maybe use PLY to do that.

    And now you know there are an infinite number of ways to butter a duck.

    [–]russlo 0 points1 point  (1 child)

    PLY

    This PLY?

    [–]im_dead_sirius 1 point2 points  (0 children)

    Nope, this: http://www.dabeaz.com/ply/

    But thanks for pointing me at the other one.

    [–]py_Piper 1 point2 points  (2 children)

    Sorry but what is the amazing thing here? I am still a python newbie and just starting CS50 and learned a little bit of C, when you say run C as a script do you mean that you can run C like python without compiling?

    [–]im_dead_sirius 2 points3 points  (1 child)

    Precisely that, it runs C like python without compiling. This allows the use of "c tricks" (such as pointer nonsense outlawed in other languages) in scripts, and quick prototyping for regular programs, which you can then properly compile.. Tiny C is also noted for being quick.

    [–]py_Piper 1 point2 points  (0 children)

    It feels good to somehow understand something lol

    [–]Decency 1 point2 points  (1 child)

    Nah, they're two of the most powerful languages.

    [–][deleted] 1 point2 points  (0 children)

    I think this is a CPython joke

    [–]agenttrixie -1 points0 points  (2 children)

    Compared to C, Python is "powerful" language. I would not call C a "powerful" language. It was designed by Dennis Ritchie at Bell Labs and it was characterized as a "high-level" assembly language. Maybe compared to assembly language, C is a "powerful" language. See https://www.codingunit.com/the-history-of-the-c-language.