Navigation Menu

Skip to content

Commit

Permalink
booleans rules for [expr]; use in TclString. make '0' not conflict wi…
Browse files Browse the repository at this point in the history
…th octals.
  • Loading branch information
coke committed Dec 14, 2009
1 parent f14e892 commit c1106c9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
4 changes: 1 addition & 3 deletions TODO
Expand Up @@ -14,8 +14,6 @@ Tasks/Bugs:
result in a tcl error, not a parrot error.
{t/tcl_misc.t; t/tcl_catch.t}

4. [expr true] and other literal boolean values {t/cmd_expr.t}

5. User-defined procs should catch continue/break and complain about them, as
should the :main PIR sub. {t/cmd_continue.t}

Expand All @@ -29,7 +27,7 @@ Tasks/Bugs:

9. regsub -all <exp> <string> <replace>

10. [expr !<token>]
10. [expr !<token>] {t/cmd_expr.t}

11. add a glob compiler. (http://www.tcl.tk/man/tcl8.5/TclCmd/string.htm#M40)
*, ?, [], \x (prior art: Tcl::Glob in core.) Needed for [string match]
Expand Down
10 changes: 6 additions & 4 deletions src/Partcl/Actions.pm
Expand Up @@ -169,10 +169,12 @@ method integer($/) {
}
}
method int:sym<zed>($/) { make 0 };
method int:sym<oct>($/) { make HLL::Actions::string_to_int(~$<digits>, 8) };
method int:sym<dec>($/) { make HLL::Actions::string_to_int(~$<digits>, 10) };
method int:sym<hex>($/) { make HLL::Actions::string_to_int(~$<digits>, 16) };
method int:sym<oct>($/) { make HLL::Actions::string_to_int(~$<digits>, 8) }
method int:sym<dec>($/) { make HLL::Actions::string_to_int(~$<digits>, 10) }
method int:sym<hex>($/) { make HLL::Actions::string_to_int(~$<digits>, 16) }
method term:sym<true>($/) { make $/.Str }
method term:sym<false>($/) { make $/.Str }
method term:sym<variable>($/) { make $<variable>.ast; }
method term:sym<integer>($/) { make $<integer>.ast; }
Expand Down
18 changes: 16 additions & 2 deletions src/Partcl/Grammar.pm
Expand Up @@ -88,10 +88,9 @@ token variable { '$' <identifier> }
rule integer { $<sign>=(<[+\-]>?)<int> }

proto token int { <...> }
token int:sym<dec> { $<digits>=[<[1..9]><[0..9]>*] }
token int:sym<dec> { $<digits>=[<[1..9]><[0..9]>* | 0] }
token int:sym<hex> { 0<[Xx]> $<digits>=(<[0..9A..Fa..f]>+) }
token int:sym<oct> { 0<[Oo]>? $<digits>=(<[0..7]>+) }
token int:sym<zed> { 0 }

# expression parsing

Expand All @@ -108,6 +107,21 @@ token ws { \h* }

token term:sym<integer> { <integer> }
token term:sym<variable> { <variable> }

token term:sym<true> {
(:i true | tru | tr | t
| yes | ye | y
| on
)
}

token term:sym<false> {
(:i false | fals | fal | fa | f
| no | n
| off | of
)
}

token term:sym<[ ]> { '[' ~ ']' <script> }
token term:sym<" "> { '"' <quoted_atom>* '"' }

Expand Down
55 changes: 21 additions & 34 deletions src/TclString.pm
Expand Up @@ -8,60 +8,47 @@ INIT {
}

module TclString {

method getInteger() { ## :is vtable

my $parse := Partcl::Grammar.parse(
self,
:rule('integer'),
self, :rule('integer'),
:actions(Partcl::Actions)
);

if ?$parse && $parse.chars() == pir::length__is(self) {
return $parse.ast(); # Will constant fold
return $parse.ast();
} else {
error('expected integer but got "' ~ self ~ '"');
}
}

method getBoolean() { ## :is vtable
my $check := pir::downcase__ss(self);

my $parse := Partcl::Grammar.parse(
self, :rule('term:sym<true>')
);

if $check eq 'true' || $check eq 'tru' || $check eq 'tr' || $check eq 't' {
return 1;
}
if $check eq 'yes' || $check eq 'ye' || $check eq 'y' {
return 1;
}
if $check eq 'on' {
return 1;
}
if $check eq 'false' || $check eq 'fals' || $check eq 'fal' || $check eq 'fa' || $check eq 'f' {
return 0;
}
if $check eq 'no' || $check eq 'n' {
return 0;
}
if $check eq 'off' || $check eq 'of' {
return 0;
if ?$parse && $parse.chars() == pir::length__is(self) {
return 1;
}

my $bool := -1;
$parse := Partcl::Grammar.parse(
self, :rule('term:sym<false>')
);

try {
$bool := ?self.getInteger();
CONTROL {}
};
if ?$parse && $parse.chars() == pir::length__is(self) {
return 0;
}

$parse := Partcl::Grammar.parse(
self, :rule('integer'),
:actions(Partcl::Actions)
);

if $bool != -1 {
return $bool;
} else {
error('expected boolean value but got "' ~ self ~ '"');
if ?$parse && $parse.chars() == pir::length__is(self) {
return ?$parse.ast();
}
}

error('expected boolean value but got "' ~ self ~ '"');
}
}

# vim: filetype=perl6:

0 comments on commit c1106c9

Please sign in to comment.