Skip to content

Commit

Permalink
minor rubyish fixes to variables, signatures and precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Nov 9, 2013
1 parent dd3fde1 commit 40d723f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
6 changes: 3 additions & 3 deletions examples/rubyish/README.md
Expand Up @@ -15,17 +15,17 @@ Implemented:
- simple strings 'Hello World!' %q{...}
- interpolating strings: "number #{37+5}" %Q{Hello #{planet}!}
- quoted words: `%w[aa bb cc]`
- scoping, including $globals, @class_instance and @@package variables
- basic scoping, including $globals, @class_instance and @@package variables
- conditional blocks: `if ... then ... elsif ... else ... endif`, `unless..end`
- nqp opcode calls: `nqp::sleep(5)`
- a few built-ins: `abort`, `print`, `puts`, `sleep`
- a couple of methods: `.call` and `.nil?`
- infixish assigments: `+=` `-=` `*=` ...
- very simple classes and objects with attributes. no inheritence yet
- very simple classes and objects with attributes. no inheritence
- `while` and `until` loops
- statement modifiers `if` `unless`, `while`, `until` e.g.: `puts 42 if true`
- basic arrays and hashes
- for loops on arrays: `for val in [10,20,30] do puts val end`
- for loops on arrays: `for val in [10, 20, 30] do puts val end`
- for loops on hash keys: `h = {'a'=>10, 'b'=>20}; for k in h do puts h{k} end`
- lambda blocks/closures: `def make_counter(n,incr) ; n-=incr; lambda { n += incr }; end`
- lightweight eRuby like templating, see [template.rbi](examples-rubyish/template.rbi)
Expand Down
37 changes: 20 additions & 17 deletions examples/rubyish/rubyish.nqp
Expand Up @@ -74,15 +74,17 @@ grammar Rubyish::Grammar is HLL::Grammar {

rule defbody {
:my $*CUR_BLOCK := QAST::Block.new(QAST::Stmts.new());
<operation> ['(' ~ ')' <signature>]? <separator>?
<operation> ['(' ~ ')' <signature>?]? <separator>?
<stmtlist>
}

rule comma { [','|'=>'] }

rule signature {
:my $*IN_PARENS := 1;
[ <param> | '*' <slurpy=.param> <!before ','>]* % ','
[ <param> ]+ % ',' [ ',' '*' <slurpy=.param> ]?
|
'*' <slurpy=.param>
}

token param { <ident> [:s<hs> '=' <EXPR>]?}
Expand Down Expand Up @@ -135,8 +137,9 @@ grammar Rubyish::Grammar is HLL::Grammar {

token var {
:my $*MAYBE_DECL := 0;
[$<sigil>=[\$|\@\@?]|<!keyword>]
\+?<ident>
\+?
$<sigil>=[ \$ | \@\@? | <!keyword> ]
<ident>
[ <?before \h* '=' [\w | \h+ || <.EXPR>] { $*MAYBE_DECL := 1 }> || <?> ]
}

Expand Down Expand Up @@ -208,10 +211,10 @@ grammar Rubyish::Grammar is HLL::Grammar {
INIT {
# Operator precedence levels
# see http://www.tutorialspoint.com/ruby/ruby_operators.htm
# x: **
Rubyish::Grammar.O(':prec<x=>, :assoc<left>', '%exponentiation');
# y: ! ~ + - (unary)
Rubyish::Grammar.O(':prec<y=>, :assoc<unary>', '%unary');
# y: **
Rubyish::Grammar.O(':prec<y=>, :assoc<left>', '%exponentiation');
# x: ! ~ + - (unary)
Rubyish::Grammar.O(':prec<x=>, :assoc<unary>', '%unary');
# w: * / %
Rubyish::Grammar.O(':prec<w=>, :assoc<left>', '%multiplicative');
# u: + -
Expand Down Expand Up @@ -343,8 +346,8 @@ grammar Rubyish::Grammar is HLL::Grammar {
}

token term:sym<lambda> {:s
'lambda' ['{' ['|' ~ '|' <signature>]? ] ~ '}' <stmtlist>
| '->' ['(' ~ ')' <signature> ]? '{' ~ '}' <stmtlist>
'lambda' ['{' ['|' ~ '|' <signature>?]? ] ~ '}' <stmtlist>
| '->' ['(' ~ ')' <signature>? ]? '{' ~ '}' <stmtlist>
}

method builtin-init() {
Expand Down Expand Up @@ -510,8 +513,6 @@ class Rubyish::Actions is HLL::Actions {
method var($/) {
my $sigil := ~$<sigil> // '';
my $name := $sigil ~ $<ident>;
my $block;
my $decl := 'var';

if $sigil eq '@' && $*IN_CLASS {
# instance variable, bound to self
Expand All @@ -523,8 +524,11 @@ class Rubyish::Actions is HLL::Actions {
else {
if $*MAYBE_DECL {

my $block;
my $decl := 'var';

if !$sigil {
$block := $*CUR_BLOCK;
$block := $*CUR_BLOCK;
}
elsif $sigil eq '$' {
$block := $*TOP_BLOCK;
Expand All @@ -534,7 +538,7 @@ class Rubyish::Actions is HLL::Actions {
}
elsif $sigil eq '@@' {
$block := $*CLASS_BLOCK;
$decl := 'static';
$decl := 'static';
}
else {
nqp::die("unhandled sigil: $sigil");
Expand Down Expand Up @@ -610,9 +614,8 @@ class Rubyish::Actions is HLL::Actions {
}

if $<slurpy> {
@params.push(QAST::Var.new(
:name(~$<slurpy>[0]), :scope('lexical'), :decl('param'), :slurpy<1>
));
@params.push($<slurpy>.ast);
@params[-1].slurpy(1);
}

make @params;
Expand Down

0 comments on commit 40d723f

Please sign in to comment.