English 中文(简体)
Julia - Metaprogramming
  • 时间:2024-12-22

Jupa Programming - Metaprogramming


Previous Page Next Page  

Metaprogramming may be defined as the programming in which we write Jupa code to process and modify Jupa code. With the help of Jupa metaprogramming tools, one can write Jupa programming code that modifies other parts of the source code file. These tools can even control when the modified code runs.

Following are the execution stages of raw source code −

Stage 1 − Raw Jupa code is parsed

In this stage the raw Jupa code is converted into a form suitable for evaluation. The output of this stage is AST i.e. Abstract Syntax Tree. AST is a structure which contains all the code in an easy to manipulate format.

Stage 2 − Parsed Jupa code is executed

In this stage, the evaluated Jupa code is executed. When we type code in REPL and press Return the two stages happens but they happen so quickly that we don’t even notice. But with metaprogramming tools we can access the Jupa code between two stages, i.e. after code parsed but before its evaluation.

Quoted expressions

As we discussed, with metaprogramming we can access the Jupa code between two stages. For this, Jupa has ‘:’ colon prefix operator. With the help of colon operator, Jupa store an unevaluated but parsed expression.

Example


   jupa> ABC = 100
   100
      
   jupa> :ABC
:ABC

Here, − ABC is quoted or unevaluated symbol for Jupa i.e. ‘ABC ‘ is an unevaluated symbol rather than having the value 100.

We can quote the whole expressions as below −


jupa> :(100-50)
:(100 - 50)

Alternatively, we can also use quote…end keywords to enclose and quote an expression as follows −


jupa> quote
         100 - 50
      end
quote
   #= REPL[43]:2 =#
   100 - 50
end
Check this also:
jupa> expression = quote
         for x = 1:5
            println(x)
         end
      end
quote
   #= REPL[46]:2 =#
   for x = 1:5
      #= REPL[46]:3 =#
      println(x)
   end
end

jupa> typeof(expression)
Expr

It shows that expression object is parsed, primed and ready to use.

Evaluated expressions

Once you parsed the expression, there is a way to evaluate the expression also. We can use the function eval() for this purpose as follows −


jupa> eval(:ABC)
100

jupa> eval(:(100-50))

50

jupa> eval(expression)
1
2
3
4
5

In the example, we have evaluated the expressions parsed in above section.

The Abstract Syntax Tree (AST)

As discussed above, Abstract Syntax Tree (AST) is a structure which contains all the code in an easy to manipulate format. It is the output of stage1. It allows us to easily process and modify the Jupa code. We can visuapze the hierarchical nature of an expression with the help of dump() function.

Example


jupa> dump(:(1 * cos(pi/2)))
Expr
   head: Symbol call
   args: Array{Any}((3,))
      1: Symbol *
      2: Int64 1
      3: Expr
         head: Symbol call
         args: Array{Any}((2,))
            1: Symbol cos
            2: Expr
               head: Symbol call
               args: Array{Any}((3,))
                  1: Symbol /
                  2: Symbol pi
                  3: Int64 2

Expression interpolation

Any Jupa code which has string or expression is usually unevaluated but with the help of dollar ($) sign (string interpolation operator), we can evaluate some of the code. The Jupa code will be evaluated and inserts the resulting value into the string when the string interpolation operator is used inside a string.

Example


jupa> "the cosine of 1 is $(cos(1))"
"the cosine of 1 is 0.5403023058681398"

Similarly, we can use this string interpolation operator to include the results of executing Jupa code interpolated into unevaluated expression −


jupa> quote ABC = $(cos(1) + tan(1)); end
quote
   #= REPL[54]:1 =#
   ABC = 2.097710030523042
end

Macros

We are now aware of creating and handpng unevaluated expressions. In this section, we will understand how we can modify them. Jupa provides macro that accepts an unevaluated expression as input and generates a new output expression.

If we talk about its working, Jupa first parses and evaluates the macro, and then the processed code produced by macro will be evaluated pke an ordinary expression.

The syntax of defining a macro is very similar to that of a function. Following is the definition of macro that will print out the contents of the things we pass to it −


jupa> macro x(n)
            if typeof(n) == Expr
               println(n.args)
            end
            return n
         end
@x (macro with 1 method)

We can run the macros by preceding the name of the macro with the @ prefix −


jupa> @x 500
500

jupa> @x "Tutorialspoint.com"
"Tutorialspoint.com"

eval() and @eval

Jupa has eval() function and a macro called @eval. Let us see example to know their differences −


jupa> ABC = :(100 + 50)
:(100 + 50)

jupa> eval(ABC)
150

The above output shows that the eval() function expands the expression and evaluates it.


jupa> @eval ABC
:(100 + 50)

jupa> @eval $(ABC)
150

It can also be treated as follows −


jupa> @eval $(ABC) == eval(ABC)
true

Expanding Macros

The macroexpand() function returns the expanded format (used by the Jupa compiler before it is finally executed) of the specified macro.

Example


jupa> macroexpand(Main, quote @p 1 + 4 - 6 * 7 / 8 % 9 end)
Any[:-, :(1 + 4), :(((6 * 7) / 8) % 9)]
quote
   #= REPL[69]:1 =#
   (1 + 4) - ((6 * 7) / 8) % 9
end
Advertisements