-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler_lexer.p6
executable file
·92 lines (76 loc) · 2.64 KB
/
compiler_lexer.p6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env perl6
#---------------------------.
# Compiler/lexical analyzer |
#---------------------------'
# For the task: https://rosettacode.org/wiki/Compiler/lexical_analyzer
# Language: Perl 6
# Author: Sam S <[email protected]>
# Not sumbitted, because someone else had already submitted a Perl 6 solution
# in the meantime.
role Cish::Tokens {
token OP_MULTIPLY { '*' }
token OP_DIVIDE { '/' }
token OP_ADD { '+' }
token OP_SUBTRACT { '-' }
token OP_LESS { '<' }
token OP_LESSEQUAL { '<=' }
token OP_GREATER { '>' }
token OP_NOTEQUAL { '!=' }
token OP_ASSIGN { '=' }
token OP_AND { '&&' }
token LEFTPAREN { '(' }
token RIGHTPAREN { ')' }
token LEFTBRACE { '{' }
token RIGHTBRACE { '}' }
token SEMICOLON { ';' }
token COMMA { ',' }
token KEYWORD_IF { 'if' <|w> }
token KEYWORD_WHILE { 'while' <|w> }
token KEYWORD_PRINT { 'print' <|w> }
token KEYWORD_PUTC { 'putc' <|w> }
token IDENTIFIER {
<[_a..z]><[_a..z0..9]>* { make ~$/ }
}
token INTEGER {
| <[0..9]>+ <|w> { make +$/ }
# | "'" (<-[']>*) [ "'" | $ { error "" } ]
} # '
token STRING {
\" [
| <-["\s]>
| \\n
| \\(.) { die "Invalid escape sequence \\$0" }
| $ { die "End-of-file" }
| $$ { die "End-of-line" }
]+
# \" (<-["]>+) \"
# a
} # "
token END_OF_INPUT { $ }
token ws {
| <?after \w> [| [\s | <comment>]+ <?before \w>
| [\s | <comment>]* <?before \W> ]
| <?after \W> [| [\s | <comment>]* <?before \w>
| [\s | <comment>]* <?before \w> ]
}
token ws { [\s | <comment>]* } # ||
token comment { '/*' .+? ['*/' | $ { die "End-of-file in comment\n"} ] }
sub error ($a) { die }
}
grammar Cish::Lexer does Cish::Tokens {
rule TOP {
<.ws>
[
| <OP_MULTIPLY> | <OP_DIVIDE> | <OP_ADD> | <OP_SUBTRACT>
| <OP_LESS> | <OP_LESSEQUAL> | <OP_GREATER> | <OP_NOTEQUAL>
| <OP_ASSIGN> | <OP_AND> | <LEFTPAREN> | <RIGHTPAREN>
| <LEFTBRACE> | <RIGHTBRACE> | <SEMICOLON> | <COMMA>
| <KEYWORD_IF> | <KEYWORD_WHILE> | <KEYWORD_PRINT> | <KEYWORD_PUTC>
| <IDENTIFIER> | <INTEGER> | <STRING> | <END_OF_INPUT>
]*
[ $ | (\w+ | .) { die "Invalid token '$0'" } ]
{ make $/.caps.map({ .key => .value.made }) }
}
}
Cish::Lexer.subparse(slurp);
dd $/.ast;