I will show how to create an interpreter for a simple programming language using Perl 6 grammars.
This talk is not an introduction to Perl 6 regexes and grammars, so we'll use them straight on, but I will add comments so that you can understand what's going on even if you never tried Perl 6 grammars.
There will not be enough time to write the whole compiler, of course, but I will show how you can do that at home.
28. Lingua: create array
my a[];
a = 3, 4, 5;
say a; ## 3, 4, 5
my b[] = 7, 8, 9;
say b; ## 7, 8, 9
29. Lingua: array elements
my arr[];
my brr[] = 3, 4, 5;
say brr; ## 3, 4, 5
my x = brr[0];
say x; ## 3
say brr[0]; ## 3
say brr[1]; ## 4
say brr[2]; ## 5
31. Lingua: keys and values
my h{};
my g{} = "a": "b", "c": "d";
say g; ## a: b, c: d
say g{"a"}; ## b
my x = g{"a"};
say x; ## b
32. Lingua: numbers 1
my int = 42;
say int; ## 42
my float = 3.14;
say float; ## 3.14
my sci = 3E14;
say sci; ## 300000000000000
my negative = -1.2;
say negative; ## -1.2
my zero = 0;
say zero; ## 0
33. Lingua: numbers 2
my half = .5;
say half; ## 0.5
my minus_n = -.3;
say minus_n; ## -0.3
my x = +7;
say x; ## 7
my y = 4.43E-1;
say y; ## 0.443
34. Lingua: strings
my s1;
s1 = "Hello, World!";
say s1; ## Hello, World!
my s2 = "Another string";
say s2; ## Another string
my s3 = ""; # Empty string
say s3; ##
37. Lingua: string interpolation
my i = 10;
my f = -1.2;
my c = 1E-2;
my s = "word";
my str = "i=$i, f=$f, c=$c, s=$s";
say str; ## i=10, f=-1.2, c=0.01, s=word
39. Lingua: variables
as indices and keys
my a[] = 2, 4, 6, 8, 10;
my i = 3;
say a[i]; ## 8
my b{} = "a": 1, "b": 2;
my j = "b";
say b{j}; ## 2
40. Lingua: expressions 1
my x;
x = 3 + 4;
say x; ## 7
x = 3 - 4;
say x; ## -1
x = 7;
say x; ## 7
x = 1 + 2 + 3;
say x; ## 6
41. Lingua: expressions 2
x = 1 + 3 + 5 + 7;
say x; ## 16
x = 7 + 8 - 3;
say x; ## 12
x = 14 - 4 - 3;
say x; ## 7
x = 100 - 200 + 300 + 1 - 2;
say x; ## 199
x = 3 * 4;
say x; ## 12
x = 100 / 25;
say x; ## 4
x = 1 + 2 * 3;
say x; ## 7
42. Lingua: expressions 3
x = 2 ** 3 ** 4;
say x; ## 4096
x = 10 * (20 - 30);
say x; ## -100
x = 10 * 20 - 30;
say x; ## 170
x = (5 * 6);
say x; ## 30
x = (10);
say x; ## 10
x = 1 - (5 * (3 + 4)) / 2;
say x; ## -16.5
43. Lingua: if
my flag = 1;
if flag say "Printed"; ## Printed
flag = 0;
if flag say "Ignored";
say "Done"; ## Done
44. Lingua: inline if-else
if 1 say "A" else say "B"; ## A
if 0 say "A" else say "B"; ## B
my x;
if 1 x = 10 else x = 20;
say x; ## 10
if 0 x = 30 else x = 40;
say x; ## 40
45. Lingua: if-else blocks
my c = 0;
if c {
say "Not printed";
}
else {
say "c = $c";
say "ELSE block";
}
## c = 0
## ELSE block
c = 1;
if c {
say "c = $c";
say "IF block";
}
else {
say "Not printed either";
}
46. Lingua: comparisons
my x = 10;
my y = 20;
if x > y say ">" else say "<"; ## <
if x < y say ">" else say "<"; ## >
if x != y say "!=" else say "=="; ## !=
if x != x say "!=" else say "=="; ## ==
if x == y say "==" else say "!="; ## !=
if x == x say "==" else say "!="; ## ==
if 5 <= 5 say "5 <= 5"; ## 5 <= 5
if 5 <= 6 say "5 <= 6"; ## 5 <= 6
82. my condition = 0;
if condition say "Passed";
rule function-call {
['if' <variable-name>]? <function-name> <value>
}
83. my condition = 0;
if condition say "Passed";
method statement($/) {
if $<condition> {
my $condition = $<condition>.made;
fail unless $condition;
}
}
84. class ASTNode {
}
class AST::ScalarDeclaration is ASTNode {
has Str $.variable-name;
has $.value;
}
88. $ grep class LinguaAST.pm
class ASTNode {
class AST::TOP is ASTNode {
class AST::ScalarDeclaration is ASTNode {
class AST::NumberValue is ASTNode {
class AST::StringValue is ASTNode {
class AST::Null is ASTNode {
class AST::Variable is ASTNode {
class AST::ArrayDeclaration is ASTNode {
class AST::HashDeclaration is ASTNode {
class AST::ScalarAssignment is ASTNode {
class AST::ArrayItemAssignment is ASTNode {
class AST::HashItemAssignment is ASTNode {
class AST::ArrayAssignment is ASTNode {
class AST::HashAssignment is ASTNode {
class AST::MathOperations is ASTNode {
class AST::ArrayItem is ASTNode {
class AST::HashItem is ASTNode {
class AST::FunctionCall is ASTNode {
class AST::Condition is ASTNode {
class AST::Loop is ASTNode {
class AST::While is ASTNode {