Skip to content

2.4 Tamgu as a scripting language

Claude Roux edited this page Oct 19, 2022 · 2 revisions

Tamgu as a scripting language

Version française

Tamgu is a programming language that offers a combination of imperative, functional and logical paradigms...

But above all, Tamgu can also be used as a scripting language in the same way as "Awk" or "Perl".

Standard Input: stdin

After all, to use a programming language as a scripting language, you only need to intercept the "standard input": stdin. In other words, you must be able to place your code in a series of pipes to apply the desired transformation to the output of the previous program.

We would like to have something that looks like this:

ls -1 | tamgu ... | ...

In fact, that's exactly what Tamgu gives you and even more...

Different modes

Tamgu offers two different modes to allow you to use it as a scripting language:

  • A dynamic mode: each input is intercepted and modified on the fly
  • An exploratory mode: we retrieve all the entries and write a script to manipulate them

The dynamic mode: -p, -pb, -pe.

  • The "-p" option allows you to provide Tamgu instructions on the command line.
  • The "-pb" option allows you to provide initialization instructions
  • The "-pe" option allows you to provide final instructions.

The option "-p" in addition offers you the following pre-declared variables free of charge:

    bool a,b,b,c;
    int i,j,k;
    float f,g,h;
    string s,t,u;
    map m;
    vector v;

These variables can be used anywhere in your code. As for the current line read from stdin, it is directly accessible via another variable generously offered by Tamgu: "l".

So here's what a filter on file names might look like:

ls -1 | tamgu -p" if ('.txt' in l) println(l);"

In other words, we only choose files that have ".txt" as extension.

test23.txt
test123.txt
...

You can also initialize some variables beforehand:

ls -1 | tamgu -pb "i=1;" -p" if ('.txt' in l) println(i,l); i++;"

In the example above, we initialize "i", a pre-declared variable, to 1. This way, we can display the list of filtered files by numbering them starting from 1.

1 test23.txt
2 test123.txt
...

You can also display your solution as a vector at the very end of the execution, thanks to "-pe".

ls -1 | tamgu -p "if ('.txt' in l) v. push(l);" -pe "println(v);"

At each iteration, "l" is saved in a vector and at the end of the execution this vector is displayed. "v" is of course a pre-declared variable.

["test123.txt", "test23.txt", ...]

Be careful, if you declare a variable in the "-p" section, it will be reset at each iteration. If you want a variable that survives through, you must declare it in the "-pb" section.

Reading a file

Tamgu offers a particular method associated with the "string" type: read. The "read" method opens a file and reads its contents, which allows some very fast operations to be performed.

ls -1 | tamgu -p "if ('.txt' in l) {s.read(l); \\
        if ("TOTO" in s and "TITI" in s) println(s["TITI":100]);}"

Thus, in one operation, the file name extension is tested, the file content is read into the variable "s" and we check whether the content contains the strings "TOTO" and "TITI". The 100 characters starting from the "TITI" position in the file are then displayed. This is a way of making a "grep" that is certainly a little complicated, but which offers the advantage of combining several tests together with Boolean operators.

Exploratory: -a

Obviously, the method above is only valid if the code following "-p" is small. A code that is too long can quickly become very complex to read or correct. Tamgu proposes another way to manage stdin: "-a".

"-a" reads the entire stdin and places each line in the vector " _args " which in Tamgu is used to record arguments on the command line.

There are two ways to use this vector.

  • In the same way as "-p" by providing code after. In this case, your code will itself have to loop on "_args".

  • If no code is provided, then Tamgu opens a console in the terminal, in which you can start playing with "_args".

ls -1 | tamgu -a

which results in:

Tamgu 1.2022.10.11.18(탐구)
Copyright 2019-present NAVER Corp.
64 bits

help: display available commands

◀▶2> _args
['LICENSE','Makefile','Makefile.in','NOTICE','README.md','Tamgu_license.txt',...]
◀▶2> edit

This is what your environment looks like when you input this command. As you can see, you can easily display the content of _args in the console.

You can run as many times as you want, _args will always contain the same data from stdin. No need to launch and restart the initial command.

Internal editor

Tamgu contains its own internal editor with which it is possible not only to write code but also to debug. To launch it, simply type the command: "edit".

1> bool a,b,c; int i,j,k; float f,g,h; string s,t,u; map m; vector v; self x,y,z;
작2> 
작3> for (s in _args)
작4> println(s);
작5> 

As is the case with the "-p" option, Tamgu pre-declares some variables that appear in the editor.

While these variables will be reset at each execution, "_args" will conversely always contain the same data. In this way, it becomes possible to perfect the script so that it gives the expected result.

_sys: system library

Tamgu also includes a _sys variable that allows you to perform a number of system operations:

 _sys.ls(path): returns the list of files and directories in "path".
 _sys.mkdir(path): builds a directory
 _sys.isdirectory(path): returns "true" if path is a directory
 _sys.realpath(path): returns the expanded path of the path
 _sys.fileinfo(path): returns the information of the file pointed to by path
 _sys.env(var, val): modifies or returns the value of the var environment variable

But above all

 _sys.command(command): executes a system command.

Thanks to "_sys.command(...)", it is possible to execute system commands directly from a Tamgu script.

1> bool a,b,c;int i,j,k;float f,g,h;string s,t,u;map m;vector v;self x,y,z;
작2> 
작3> for (s in _args)
작4> sys.command("cp" + s +".../toto");
작5> 

Variables "through"

Tamgu finally offers a very unique last type (I think): "through" variables. A "through" variable is a variable that survives several successive executions, just like _args. In this way, it becomes possible to keep track of successive executions.

The type of the variable is indicated by the first letter with the keyword "through".

    ithrough: is an integer
    sthrough: is a string
    vthrough: is a string vector
    mthrough: is a map whose keys and values are strings

Here is for example, a script that calculates at each iteration the time spent applying a modification to the arguments.

1> bool a,b,c;int i,j,j,k;float f,g,h;string s,t,u;map m;vector v;self x,y,z;
작 2> vthrough vv;
작 3> 
작 4> time t1;
작 5> for (s in _args)
작 6> _sys.command(...);
작 7> time t2;
작 8> float d = t2-t1;
작 9> 
작10> vv.push(d);
작11> 
작12> 

At each execution, the time required to complete the instructions is kept in vv. After ten executions, vv will contain ten different time values.

Attention: This variable loses its value as soon as you leave Tamgu... The values are only kept for a given session.

Combine with a file name

You can finally define a file name when you launch your Tamgu command and switch to "editor" mode immediately. The only constraint is to put the option "-a" at the end of the command line.

ls -1 | tamgu -e monfichier.tmg -a

If "myfile.tmg" exists, it will be automatically loaded, otherwise it will be created.

You can now run this file directly on your data.

ls -1 | tamgu myfile.tmg -a

The only difference is that the name is given directly....

Clone this wiki locally