🔬41. Converting Num to Rat in Perl 6

In Perl 6, you can easily coerce numerical values from one type to another. One of the interesting conversions is Num to Rat. For example, take the value of π and convert it to a fraction: $ perl6 -e’say pi.Rat.perl’ <355/113> Indeed, 355/113 is 3.14159292035398, which is close to π. The default maximum error is built-in in … Continue reading “🔬41. Converting Num to Rat in Perl 6”

🔬40. Substitution and balanced brackets in Perl 6 regexes

I was randomly looking at different Rakudo Perl 6 source files and found a syntax construction that I could not recognise. I hope you were reading the documentation more carefully (or you did not forget after you have read it) than me, but nevertheless let me devote today’s post to that feature. So, this was … Continue reading “🔬40. Substitution and balanced brackets in Perl 6 regexes”

🔬39. Experimenting with Rats and Nums in Perl 6

Today, it will be a post with some small things about the two data types, Rat and Num. First of all, Zoffix Znet added some insights on Twitter regarding my previous posts. Let me just quote them here. Some useful information about DIVIDE_NUMBERS and DON’T_DIVIDE_NUMBERS: FWIW, these will be gone some time these year, together … Continue reading “🔬39. Experimenting with Rats and Nums in Perl 6”

🔬38. To divide or not to divide

We have seen the mysterious DIVIDE_NUMBERS function a couple of times already. Let us keep our focus on it today. The function is a part of the Rat data type. It lives in src/core/Rat.pm together with its sister, DON’T_DIVIDE_NUMBERS. (An apostrophe is a valid character for identifiers in Perl 6; it is not a namespace separator … Continue reading “🔬38. To divide or not to divide”

🔬35. Statement in the Grammar of Perl 6

At the beginning of the year, we began reading Perl 6’s Grammar. That exercise is very interesting but not that easy. Today, let us take one more step and look at the statement. The statement token is relatively compact. It is reproduced here with some simplifications (refer to src/Perl6/Grammar.nqp for the full code): token statement($*LABEL = … Continue reading “🔬35. Statement in the Grammar of Perl 6”

🔬34. Delimiters of embedded comments in Perl 6

In Perl 6, you can leave a comment inside the line of code using the #`(…) syntax: my Int #`(Integer variable) $i = #`(initialising the variable) 42; say #`( Printing it ) $i; This program is equivalent to the following: my Int $i = 42; say $i; The body of the embedded comment is located … Continue reading “🔬34. Delimiters of embedded comments in Perl 6”

🔬33. The cmp infix in Perl 6

In Perl 6, there is an infix operator called cmp. Despite its simple name and some connotations with its counter partner in Perl 5, its semantic is not trivial. From the documentation, we read: Generic, “smart” three-way comparator. Compares strings with string semantics, numbers with number semantics, Pair objects first by key and then by value … Continue reading “🔬33. The cmp infix in Perl 6”

💡32. It’s time for optimism

Today, we will not talk about the internals of Rakudo, NQP, or MoarVM; let me pause for a bit and talk about some random things related to Perl 6 and its future. Efficiency If you follow my blog, I have an update to the post about optimising MoarVM, and it appears that when making calculations … Continue reading “💡32. It’s time for optimism”

🔬31. The opcode dispatching loop in MoarVM

Let me make some correction work following the previous post. I felt that something escaped from my view because changing the code did not bring any advances in speed. Jonathan Worthington left a brilliant comment saying that I was trying to optimise the JIT compiler. Bang! Indeed, I thought that JIT should have had some … Continue reading “🔬31. The opcode dispatching loop in MoarVM”

🔬30. How I was optimising MoarVM

A Friday story for your pleasure. An attentive reader could notice yesterday that MoarVM is using many switch/case choices when it processes the bytecode. In src/jit/graph.c, there is code which returns the address of the function corresponding to the given opcode: static void * op_to_func(MVMThreadContext *tc, MVMint16 opcode) { switch(opcode) { case MVM_OP_checkarity: return MVM_args_checkarity; … Continue reading “🔬30. How I was optimising MoarVM”

🔬29. Exploring the Int type in Perl 6, part 2

Today, the journey to the internals of Int continues and first let’s make a deep dive into one on of the fascinating methods, is-prime. If you never used it, this is a great method that implements the logic, which is usually not built-in in a programming language. It is also great because primality is an … Continue reading “🔬29. Exploring the Int type in Perl 6, part 2”

🔬28. Exploring the Int type in Perl 6, part 1

Actually, we already started looking at the internals of the Int data type two days ago, but today we’ll start from the very beginning. So, the Int data type. It is a great data type for practical programming and it is also widely used in Rakudo itself. For example, the Rational object is an object … Continue reading “🔬28. Exploring the Int type in Perl 6, part 1”

🔬27. Obsolete syntax error messages in Perl 6, part 4

So far, we covered a lot of different error messages that Rakudo Perl 6 generates when you accidentally use the Perl 5 syntax. This is a really nice feature for easy migration to the new language. Let us continue and cover another couple of errors. new X It was one of the hottest topics in … Continue reading “🔬27. Obsolete syntax error messages in Perl 6, part 4”

🔬26. Native integers and UInt in Perl 6

As soon as we touched some native integers yesterday, let us look a bit closer at them. The topic is deep, so we limit ourselves with a brief understanding of interconnections between the different integer data types in Perl 6. UInt and UInt64 The simplest is UInt. This data type is defined in src/core/Int.pm and … Continue reading “🔬26. Native integers and UInt in Perl 6”

🔬25. The Range method

Today, I started looking into the internals of the Int class (src/core/Int.pm) and faced a strangely looking method, Range. The Range method returns an object of the Range type showing the minimum and the maximum values that the object can hold. For example, this is how the method is defined for the Num class: method … Continue reading “🔬25. The Range method”

🔬24. Obsolete syntax error messages in Perl 6, part 3

A couple of weeks ago, we looked at some error messages that Perl 6 generates when it sees the Perl 5 constructions. Let us continue and go through another portion of the messages that are there in today’s Rakudo. \x[] We start with a simple error message that informs you to use new syntax when … Continue reading “🔬24. Obsolete syntax error messages in Perl 6, part 3”

🔬23. The internals of the ternary operator in Perl 6

Yesterday, we saw that the ternary operator is treated as an infix in the Perl 6 Grammar. The code between the two parts of the operator is caught by the <EXPR> method: token infix:sym<?? !!> { :my $*GOAL := ‘!!’; $<sym>=’??’ <.ws> <EXPR(‘i=’)> [ ‘!!’ . . . ] <O(|%conditional, :reducecheck<ternary>, :pasttype<if>)> } Now, our attraction … Continue reading “🔬23. The internals of the ternary operator in Perl 6”

🔬22. The infix nature of the ternary operator in Perl 6

The ternary operator ?? !! takes three operands, obviously. Although, it is said in the documentation that the operator is an infix. Let us figure out why. Here is the fragment from the Grammar that handles the ternary operator: token infix:sym<?? !!> { :my $*GOAL := ‘!!’; $<sym>=’??’ <.ws> <EXPR(‘i=’)> [ ‘!!’ || <?before ‘::’ <.-[=]>> … Continue reading “🔬22. The infix nature of the ternary operator in Perl 6”