Skip to content

Commit

Permalink
Add more grammar. 'Inspired' by NQP and Squaak
Browse files Browse the repository at this point in the history
  • Loading branch information
bacek committed Feb 21, 2011
1 parent 987367a commit b3d8b5e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
51 changes: 47 additions & 4 deletions compilers/opsc/src/Ops/Compiler/Grammar.pm
Expand Up @@ -149,20 +149,51 @@ rule statement_list {
}

token statement {
<EXPR> <.ws>
| <statement_control>
| <blockoid>
| <EXPR> <.ws>
}


proto rule statement_control { <...> }

rule statement_control:sym<if> {
<sym> '(' <EXPR> ')'
<then=.statement>
[ 'else' <else=.statement> ]?
}


token term:sym<name> { <identifier> }
token term:sym<int> { <integer> }
token term:sym<str> { <quote> }
token term:sym<float_constant_long> { # longer to work-around lack of LTM
[
| \d+ '.' \d*
| \d* '.' \d+
]
}

# Assignment
token infix:sym<=> { <sym> <O('%assignment')> }

token postcircumfix:sym<( )> {
'(' <.ws> <EXPR>? ')'
'(' <.ws> <arglist> ')'
<O('%methodop')>
}
token postcircumfix:sym<[ ]> {
'[' <.ws> <EXPR>? ']'
<O('%methodop')>
}

token arglist {
<.ws>
[
| <EXPR('f=')>
| <?>
]
}


rule blockoid {
'{'
Expand Down Expand Up @@ -205,8 +236,20 @@ token ws {

INIT {
Ops::Compiler::Grammar.O(':prec<y=>, :assoc<unary>', '%methodop');
Ops::Compiler::Grammar.O(':prec<p=>, :assoc<left>', '%multiplicative');
Ops::Compiler::Grammar.O(':prec<o=>, :assoc<left>', '%additive');
Ops::Compiler::Grammar.O(':prec<x=>, :assoc<unary>', '%autoincrement');
Ops::Compiler::Grammar.O(':prec<w=>, :assoc<left>', '%exponentiation');
Ops::Compiler::Grammar.O(':prec<v=>, :assoc<unary>', '%symbolic_unary');
Ops::Compiler::Grammar.O(':prec<u=>, :assoc<left>', '%multiplicative');
Ops::Compiler::Grammar.O(':prec<t=>, :assoc<left>', '%additive');
Ops::Compiler::Grammar.O(':prec<r=>, :assoc<left>', '%concatenation');
Ops::Compiler::Grammar.O(':prec<m=>, :assoc<left>', '%relational');
Ops::Compiler::Grammar.O(':prec<l=>, :assoc<left>', '%tight_and');
Ops::Compiler::Grammar.O(':prec<k=>, :assoc<left>', '%tight_or');
Ops::Compiler::Grammar.O(':prec<j=>, :assoc<right>', '%conditional');
Ops::Compiler::Grammar.O(':prec<i=>, :assoc<right>', '%assignment');
Ops::Compiler::Grammar.O(':prec<g=>, :assoc<list>, :nextterm<nulltermish>', '%comma');
Ops::Compiler::Grammar.O(':prec<f=>, :assoc<list>', '%list_infix');
Ops::Compiler::Grammar.O(':prec<e=>, :assoc<unary>', '%list_prefix');
}


Expand Down
21 changes: 21 additions & 0 deletions t/compilers/opsc/01-parse-body.t
Expand Up @@ -26,6 +26,27 @@ inline op noop() {
|);
ok(1, "Single call op parsed");

_parse($c, q|
inline op noop() {
char bar;
}
|);
ok(1, "Simple declaration parsed");

_parse($c, q|
inline op noop() {
Interp * const new_interp;
}
|);
ok(1, "Complex declaration parsed");

_parse($c, q|
inline op noop() {
Interp * const new_interp = foo();
}
|);
ok(1, "More complex declaration parsed");


};

Expand Down

0 comments on commit b3d8b5e

Please sign in to comment.