String Templates in E-Language

Learning programming languages is like learning a foreign language. It has words, syntax, grammar and meaning. If you don’t use a foreign language for a long time it might become difficult to remember some of the words. The same happens with programming languages. Keywords or specific language constructs might be difficult to recall if you have not used that language lately or if it is a rarely used language construct that you try to remember. When this happens I usually go and search on the internet when it comes to foreign languages or I go and search inside the language reference manual (LRM) when it comes to programming language constructs.

But sometimes LRM does not help because not everything is well documented. That’s the case with what I call string templates in e-language or Inline Text Expansion construct according to Specman LRM.

Here is how this string template looks like:

<<#:
//multiline string
end #

The interesting thing is that I learned this construct 12 years ago not from the LRM as anyone would expect, but from my colleague, Cristian Slav. He was using it extensively inside the define as computed e-language macros. Thus, in order not to forget this language construct again, I decided to write this blog post and tell its story :).

So, what’s that interesting about this construct? Well, it allows you to reference variable names inside a multiline string. Like this:

<'
extend sys {
  var n : int=10;
  var myLoop : list of string = <<#:
  for i from 1 to <(n)> do;
      out(i);
  end #
}
'>

The variable myLoop will replace the <(n)> with the number 10, like bellow:

for i from 1 to 10 do;
   out(i);

The whole construct is built to return a list of string. This is what I can think you could do with a list of string:

  1. Generating content for various files (html, vsif, csv, etc.)
  2. E.g. Create HTML code from e-language.

    <'
    createHtmlLinks(bookCollection : list of bookStructure):list of string is {
      var htmlCode: list of string;
      
      for i from 0 to bookCollection.size()-1 {
        var singleBook := bookCollection[i];
        Var linkData : list of string = <<#:
        <a href = “<(singleBook.url)>” title=”<(singleBook.title)>”><(singleBook.linkText)></a>
        End #;
        htmlCode.add(linkData)
      };
      
      return htmlCode;
    };
    '>

    I used this even for creating e-language files to be used in the debug process.

  3. Create define as computed macros
  4. Below we define a macro taking a variable number of arguments in the form of a list and it defines fields of a particular type.

    <'
    define <fieldDefinition'statement> "declare <unitName'name> fields \[<item'name>,...\] as <t'name>"  as computed {
      var code : list of string;
      var unitName : string = <unitName'name>;
      var tName : string = <t'name>;
      var fields : list of string = <item'names>;
      for each(field) in fields {
        code.add(<<#:
          extend <(unitName)> {
            
            (field)> : <(tName)>;
          };
        end #);
      };
      return str_join(code, "");
    };
    '>

    The macro is called as

    <'
    declare sys fields [has_coverage,has_data,check_field] as bool;
    declare sys fields [n1,n2] as int;
    '>

    Which expands to

    <'
    extend sys {
        has_coverage : bool;
    };
    extend sys {
        has_data : bool;
    };
    extend sys {
        check_field : bool;
    };
    extend sys {
        n1 : int;
    };
    extend sys {
        n2 : int;
    };
    '>

While writing this post I actually found that I’m not the only one who felt the need of documenting this interesting construct :). Cristian Slav himself wrote an article about the same thing: Specman Secrets – Inline Text Expansion

If you want to play with the code from this article you can find it on AMIQ GitHub.

Have you ever used this secret language construct?


Comments

Orit Kirshenberg December 13th, 2020 09:28:52

Very nice article!


    Aurelian Ionel Munteanu December 13th, 2020 21:19:52

    Thank you. I’m glad you like it!


Leave a Reply to Orit Kirshenberg Cancel reply

Your comment will be visible after approval.

(will not be published)

This site uses Akismet to reduce spam. Learn how your comment data is processed.