Personal Perl 6 compiler
-
Upload
andysh -
Category
Technology
-
view
1.331 -
download
0
description
Transcript of Personal Perl 6 compiler
![Page 1: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/1.jpg)
Perlsonal Perl 6 compiler
![Page 2: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/2.jpg)
or
![Page 3: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/3.jpg)
How to speed upyour Perl 6 app
![Page 4: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/4.jpg)
How to speed upyour Perl 6 app
and make it 10 000 times faster
![Page 5: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/5.jpg)
-O fun
![Page 6: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/6.jpg)
![Page 7: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/7.jpg)
Let’s take a simple
programme
![Page 8: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/8.jpg)
Let’s take a simplebut CPU consuming
programme
![Page 9: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/9.jpg)
sayis_prime(@*ARGS[0]);
subis_prime($n){for2..$n‐1‐>$d{return0unless$n%$d;}return1;}
![Page 10: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/10.jpg)
$perl6
![Page 11: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/11.jpg)
$perl6is_prime.p6
![Page 12: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/12.jpg)
$perl6is_prime.p637
![Page 13: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/13.jpg)
$perl6is_prime.p6371
![Page 14: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/14.jpg)
$perl6is_prime.p6380
![Page 15: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/15.jpg)
$perl6is_prime.p615511
![Page 16: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/16.jpg)
$perl6is_prime.p6155111
20 seconds with Rakudo :-(
![Page 17: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/17.jpg)
Perl 6 can be fast!
![Page 18: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/18.jpg)
Perl 6 can be fast!
![Page 19: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/19.jpg)
Perl 6 can be fast!
![Page 20: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/20.jpg)
Boost it!
![Page 21: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/21.jpg)
Boost it!
OK, go to boost.org :-)
![Page 22: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/22.jpg)
structp6grammar:publicgrammar<p6grammar>{template<typenameScannerT>structdefinition{definition(p6grammarconst&self){
![Page 23: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/23.jpg)
structp6grammar:publicgrammar<p6grammar>{template<typenameScannerT>structdefinition{definition(p6grammarconst&self){program=statement_list>>end_p;
![Page 24: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/24.jpg)
structp6grammar:publicgrammar<p6grammar>{template<typenameScannerT>structdefinition{definition(p6grammarconst&self){program=statement_list>>end_p;statement_list=*statement;
![Page 25: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/25.jpg)
structp6grammar:publicgrammar<p6grammar>{template<typenameScannerT>structdefinition{definition(p6grammarconst&self){program=statement_list>>end_p;statement_list=*statement;statement=statement_code>>!statement_separator;
![Page 26: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/26.jpg)
statement_code=comment|statement_control|sub_def|sub_call|expression;
![Page 27: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/27.jpg)
statement_code=comment|statement_control|sub_def|sub_call|expression;comment=ch_p('#')>>lexeme_d[*~ch_p('\n')];
![Page 28: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/28.jpg)
statement_code=comment|statement_control|sub_def|sub_call|expression;comment=ch_p('#')>>lexeme_d[*~ch_p('\n')];sub_call=sub_name>>!sub_arguments;
![Page 29: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/29.jpg)
structp6grammar:publicgrammar<p6grammar>{template<typenameScannerT>structdefinition{definition(p6grammarconst&self){program=statement_list>>end_p;statement_list=*statement;statement=statement_code>>!statement_separator[&a_statement];statement_code=comment|statement_control|sub_def//|(sub_call[&a_sub_call_start]>>!statement_modifier)[&a_sub_call_end]|sub_call|expression;comment=ch_p('#')>>lexeme_d[*~ch_p('\n')];statement_separator=ch_p(';');sub_call=(sub_name>>!sub_arguments)[&a_sub_called];sub_name=builtin_sub_name|identifier[&a_sub_name][&a_sub_calling];builtin_sub_name=str_p("say")[&a_builtin_say]|str_p("print")[&a_builtin_print]|str_p("return")[&a_builtin_return];identifier=lexeme_d[+(alpha_p|'_')>>*(alnum_p|'_')];sub_arguments=ch_p('(')>>!argument_list>>ch_p(')')|argument_list;argument_list=list_p(argument,ch_p(',')[&a_argument_list_sep]);
argument=expression|variable;variable=array|scalar;scalar=constant|ch_p('$')>>identifier[&a_scalar]|array_element;array=simple_array|global_array;simple_array=ch_p('@')>>(identifier>>~eps_p('['))[&a_array];
global_array=str_p("@*")>>(identifier>>~eps_p('['))[&a_global_array];array_element=simple_array_element|global_array_element;simple_array_element=(ch_p('@')>>array_element_name_and_index)[&a_array_element];
global_array_element=(str_p("@*")>>array_element_name_and_index)[&a_global_array_element];array_element_name_and_index=identifier[assign_a(array_element_name)]>>ch_p('[')>>array_index>>ch_p(']');array_index=int_p[assign_a(array_element_index)];constant=real_p[&a_constant];sub_def=str_p("sub")[&a_sub_def_start]>>sub_name>>!sub_arguments[&a_sub_def_arguments_end]>>sub_body[&a_sub_def_end];
sub_body=ch_p('{')[&a_sub_def_body_start]>>!statement_list>>ch_p('}');statement_control=for_cycle|conditional_block;conditional_block=(str_p("if")|str_p("unless"))[&a_condition_start]>>expression[&a_condition_end]>>code_block[&a_conditional_block_end];code_block=ch_p('{')>>statement_list>>ch_p('}');for_cycle=str_p("for")[&a_for_cycle_start]>>(range>>!(str_p("‐>")>>ch_p('$')>>identifier[&a_for_cycle_variable]))[&a_for_cycle_range]>>for_cycle_body[&a_for_cycle_body];range=ch_p('(')>>range_content>>ch_p(')')|range_content;range_content=expression>>str_p("..")[&a_range_sep]>>expression;expression=ch_p('(')[&a_paren]>>expression_content>>ch_p(')')[&a_paren]|expression_content;expression_content=scalar>>*(math_op[&a_math_op]>>expression)|sub_call;math_op=ch_p('+')|ch_p('‐')>>eps_p(~ch_p('>'))|ch_p('*')|ch_p('/')|ch_p('%');for_cycle_body=sub_body;statement_modifier=(str_p("unless")|str_p("if"))>>expression;
![Page 30: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/30.jpg)
sayis_prime(@*ARGS[0]);
subis_prime($n){for2..$n‐1‐>$d{return0unless$n%$d;}return1;}
![Page 31: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/31.jpg)
sayis_prime(@*ARGS[0]);
subis_prime($n){for2..$n‐1‐>$d{return0unless$n%$d;}return1;}
![Page 32: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/32.jpg)
sayis_prime(@*ARGS[0]);
subis_prime($n){for2..$n‐1‐>$d{unless$n%$d{return0;}}return1;}
![Page 33: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/33.jpg)
Programme 1
Checks if the argument is a prime
![Page 34: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/34.jpg)
sayis_prime(@*ARGS[0]);
subis_prime($n){for2..$n‐1‐>$d{unless$n%$d{return0;}}return1;}
![Page 35: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/35.jpg)
./p++<t/is_prime.p6
![Page 36: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/36.jpg)
#include<iostream>#include<string>
usingnamespacestd;
typedefintvariable_t;typedefintscalar_t;typedefintrange_boundary_t;
variable_tsub_is_prime(scalar_tvar_scalar_n){for(range_boundary_tvar_scalar_d=2;var_scalar_d<=var_scalar_n‐1;var_scalar_d++){if(!(var_scalar_n%var_scalar_d)){return0;}}return1;}
intmain(intargn,char**var_global_array_ARGS){cout<<sub_is_prime(atoi(var_global_array_ARGS[1+0]))<<"\n";return0;}
![Page 37: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/37.jpg)
sayis_prime(@*ARGS[0]);
cout<<sub_is_prime(atoi(var_global_array_ARGS[1+0]))
<<"\n";
![Page 38: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/38.jpg)
subis_prime($n){
variable_tsub_is_prime(scalar_tvar_scalar_n){
![Page 39: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/39.jpg)
for2..$n‐1‐>$d{
for(range_boundary_tvar_scalar_d=2;var_scalar_d<=var_scalar_n‐1;var_scalar_d++){
![Page 40: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/40.jpg)
$time./is_prime155111
![Page 41: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/41.jpg)
$time./is_prime155111
real0m0.002s
![Page 42: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/42.jpg)
Programme 2
Checks if the argument is a primeand displays the first denominator
![Page 43: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/43.jpg)
sayis_prime(@*ARGS[0]);
subis_prime($n){for2..$n‐1‐>$d{unless$n%$dsay$d;return0;}}return1;}
![Page 44: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/44.jpg)
Programme 3
Generates prime numbers below 1 000 000
![Page 45: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/45.jpg)
@primes=grep*.prime,2..*;
S2 recommends
![Page 46: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/46.jpg)
for2..1000000‐>$n{ifis_prime($n){say$n;}}
subis_prime($n){for2..$n‐1‐>$d{unless$n%$d{return0;}}return1;}
![Page 47: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/47.jpg)
Sparse ideas behind p6c
![Page 48: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/48.jpg)
1. C++‘s type casts
![Page 49: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/49.jpg)
my$var=100;say$var;
$var="string";say$var;
![Page 50: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/50.jpg)
my$var=100;say$var;
$var="string";say$var;
variable_var.set(100);cout<<variable_var;
variable_var.set("string");cout<<variable_var;
![Page 51: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/51.jpg)
classPerlVariable{...public:operatorint();operatorstring();}
![Page 52: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/52.jpg)
2. Intensive use of C++ compiler
![Page 53: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/53.jpg)
classMyClass{has$member;methodmeth(){...}
}
![Page 54: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/54.jpg)
classMyClass{has$member;methodmeth(){...}
}
classMyClass{public:PerlVariablemember_member;
voidmeth_method();}
![Page 55: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/55.jpg)
3. What to implement(base principle)
![Page 56: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/56.jpg)
a) Write Perl 6 programmeI’d like to have in production
![Page 57: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/57.jpg)
a) Write Perl 6 programmeI’d like to have in production
b) Implement that part of the compiler
![Page 58: Personal Perl 6 compiler](https://reader034.fdocuments.us/reader034/viewer/2022051816/546ce8c3b4af9f702c8b520e/html5/thumbnails/58.jpg)
a) Write Perl 6 programmeI’d like to have in production
b) Implement that part of the compiler (following STD.pm)