Skip to content

Commit

Permalink
Convert all builtins to methods.
Browse files Browse the repository at this point in the history
class methods are much more amenable to introspection in nqp
than subs (which were our goto in parrot-nqp)
  • Loading branch information
coke committed Oct 1, 2012
1 parent 7507362 commit 8156479
Show file tree
Hide file tree
Showing 64 changed files with 69 additions and 94 deletions.
4 changes: 3 additions & 1 deletion build/Makefile.in
Expand Up @@ -116,7 +116,9 @@ all: $(PARTCL_EXE) .revision

src/Partcl.pir: $(GEN_SOURCES)
src/Partcl/commands.pm: $(COMMANDS) src/init.pm
cat $(COMMANDS) > src/Partcl/commands.pm
echo "class Builtins {" > src/Partcl/commands.pm
cat $(COMMANDS) >> src/Partcl/commands.pm
echo "}" >> src/Partcl/commands.pm

src/Internals.pm: src/Partcl/commands.pir src/init.pm
src/init.pm: src/TclLexPad.pir src/TclArray.pir
Expand Down
34 changes: 4 additions & 30 deletions src/Internals.pm
Expand Up @@ -5,39 +5,13 @@ class Internals {

## wrapper method for invoking tcl builtins - deals with unknown
## handling and namespace desugaring


my $Builtins := Builtins.new();

method dispatch($command, *@args) {

## Call a tcl method just to prove it works here.
puts("Called our invoke dispatcher with [$command" ~
(+@args ?? " " !! "") ~
nqp::join(" ", @args) ~ "]");

1;
=for later
## Get our caller's namespace, do the lookup from there.
my $ns := Q:PIR {
$P1 = getinterp
%r = $P1['sub'; 1]
}.get_namespace();
my &command := $ns{$command};

## if that didn't work, check in the root ns.
if pir::typeof__SP(&command) eq "Undef" {
$ns := pir::get_hll_namespace__P();
&command := $ns{$command};
}

if pir::typeof__SP(&command) eq "Undef" {
error("invalid command name \"$command\"");
}

&command(|@args);

=end later
$Builtins."$command"(|@args);
}
}
# vim: expandtab shiftwidth=4 ft=perl6:
2 changes: 1 addition & 1 deletion src/Partcl/commands/after.pm
@@ -1,4 +1,4 @@
sub after(*@args) is export {
method after(*@args) {
if +@args < 1 {
error('wrong # args: should be "after option ?arg arg ...?"')
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/append.pm
@@ -1,4 +1,4 @@
sub append(*@args) is export {
method append(*@args) {
if +@args < 1 {
error('wrong # args: should be "append varName ?value value ...?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/apply.pm
@@ -1,4 +1,4 @@
sub apply(*@args) is export {
method apply(*@args) {
if +@args == 0 {
error('wrong # args: should be "apply lambdaExpr ?arg1 arg2 ...?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/array.pm
@@ -1,4 +1,4 @@
sub array(*@args) is export {
method array(*@args) {
Array::dispatch_command(|@args);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/binary.pm
@@ -1,4 +1,4 @@
sub binary(*@args) is export {
method binary(*@args) {
error('wrong # args: should be "binary option ?arg arg ...?"')
if !+@args;

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/cd.pm
@@ -1,5 +1,5 @@
# TODO: implement ~user syntax
sub cd(*@args) is export {
method cd(*@args) {
if +@args > 1 {
error('wrong # args: should be "cd ?dirName?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/concat.pm
@@ -1,4 +1,4 @@
sub concat(*@args) is export {
method concat(*@args) {
my $result := @args ?? String::trim(@args.shift) !! '';
while @args {
$result := $result ~ ' ' ~ String::trim(@args.shift);
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/dict.pm
@@ -1,4 +1,4 @@
sub dict(*@args) is export {
method dict(*@args) {
Dict::dispatch_command(|@args);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/encoding.pm
@@ -1,4 +1,4 @@
sub encoding(*@args) is export {
method encoding(*@args) {
'';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/eof.pm
@@ -1,4 +1,4 @@
sub eof(*@args) is export {
method eof(*@args) {
if +@args != 1 {
error('wrong # args: should be "eof channelId"')
}
Expand Down
3 changes: 1 addition & 2 deletions src/Partcl/commands/error.pm
@@ -1,7 +1,6 @@

#placeholder error

sub error(*@args) is export {
method error(*@args) {
say(nqp::join("\n",@args));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/eval.pm
@@ -1,4 +1,4 @@
sub eval(*@args) is export {
method eval(*@args) {
if +@args < 1 {
error('wrong # args: should be "eval arg ?arg ...?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/exit.pm
@@ -1,4 +1,4 @@
sub exit(*@args) is export {
method exit(*@args) {
if +@args > 1 {
error('wrong # args: should be "exit ?returnCode?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/expr.pm
@@ -1,4 +1,4 @@
sub expr(*@args) is export {
method expr(*@args) {
my $code := nqp::join(' ', @args);
error("empty expression\nin expression \"\"")
if $code eq '';
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/file.pm
@@ -1,4 +1,4 @@
sub file(*@args) is export {
method file(*@args) {
File::dispatch_command(|@args);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/fileevent.pm
@@ -1,4 +1,4 @@
sub fileevent(*@args) is export {
method fileevent(*@args) {
if +@args < 2 || +@args > 3 {
error('wrong # args: should be "fileevent channelId event ?script?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/flush.pm
@@ -1,4 +1,4 @@
sub flush(*@args) is export {
method flush(*@args) {
if +@args != 1 {
error('wrong # args: should be "flush channelId"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/for.pm
@@ -1,4 +1,4 @@
sub for(*@args) is export {
method for(*@args) {
if +@args != 4 {
error('wrong # args: should be "for start test next command"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/foreach.pm
@@ -1,4 +1,4 @@
sub foreach(*@args) is export {
method foreach(*@args) {
if +@args < 2 || +@args % 2 == 0 {
error('wrong # args: should be "foreach varList list ?varList list ...? command"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/format.pm
@@ -1,4 +1,4 @@
sub format(*@args) is export {
method format(*@args) {
unless +@args {
error('wrong # args: should be "format formatString ?arg arg ...?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/gets.pm
@@ -1,4 +1,4 @@
sub gets(*@args) is export {
method gets(*@args) {
our %CHANNELS;

if +@args < 1 || +@args > 2 {
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/glob.pm
@@ -1,4 +1,4 @@
sub glob(*@args) is export {
method glob(*@args) {
my $dir := ".";
while @args[0] ne '--' && nqp::substr(@args[0],0,1) eq '-' {
my $opt := @args.shift;
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/global.pm
@@ -1,4 +1,4 @@
sub global (*@args) is export {
method global (*@args) {
our %GLOBALS;

## my %CUR_LEXPAD := DYNAMIC::<%LEXPAD>
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/if.pm
@@ -1,4 +1,4 @@
sub if(*@args) is export {
method if(*@args) {
while @args {
my $expr := @args.shift;
my $body;
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/incr.pm
@@ -1,4 +1,4 @@
sub incr (*@args) is export {
method incr (*@args) {
if +@args < 1 || +@args > 2 {
error('wrong # args: should be "incr varName ?increment?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/info.pm
@@ -1,4 +1,4 @@
sub info(*@args) is export {
method info(*@args) {
Info::dispatch_command(|@args);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/interp.pm
@@ -1,4 +1,4 @@
sub interp(*@args) is export {
method interp(*@args) {
if +@args < 1 {
error('wrong # args: should be "interp subcommand ?argument ...?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/join.pm
@@ -1,4 +1,4 @@
sub join(*@args) is export {
method join(*@args) {
if +@args < 1 || +@args > 2 {
error('wrong # args: should be "join list ?joinString?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/lappend.pm
@@ -1,4 +1,4 @@
sub lappend(*@args) is export {
method lappend(*@args) {
if +@args < 1 {
error('wrong # args: should be "lappend varName ?value value ...?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/lassign.pm
@@ -1,4 +1,4 @@
sub lassign(*@args) is export {
method lassign(*@args) {
if +@args < 2 {
error('wrong # args: should be "lassign list varName ?varName ...?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/lindex.pm
@@ -1,4 +1,4 @@
sub lindex(*@args) is export {
method lindex(*@args) {
if +@args < 1 {
error('wrong # args: should be "lindex list ?index...?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/linsert.pm
@@ -1,4 +1,4 @@
sub linsert(*@args) is export {
method linsert(*@args) {
if +@args < 2 {
error('wrong # args: should be "linsert list index element ?element ...?"')
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/list.pm
@@ -1,4 +1,4 @@
sub list(*@args) is export {
method list(*@args) {
return @args;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/llength.pm
@@ -1,4 +1,4 @@
sub llength(*@args) is export {
method llength(*@args) {
if +@args != 1 {
error('wrong # args: should be "llength list"')
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/lrange.pm
@@ -1,4 +1,4 @@
sub lrange(*@args) is export {
method lrange(*@args) {
if +@args != 3 {
error('wrong # args: should be "lrange list first last"')
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/lrepeat.pm
@@ -1,4 +1,4 @@
sub lrepeat(*@args) is export {
method lrepeat(*@args) {
if +@args < 2 {
error('wrong # args: should be "lrepeat positiveCount value ?value ...?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/lreplace.pm
@@ -1,4 +1,4 @@
sub lreplace(*@args) is export {
method lreplace(*@args) {
if +@args < 3 {
error('wrong # args: should be "lreplace list first last ?element element ...?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/lreverse.pm
@@ -1,4 +1,4 @@
sub lreverse(*@args) is export {
method lreverse(*@args) {
if +@args != 1 {
error('wrong # args: should be "lreverse list"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/lset.pm
@@ -1,4 +1,4 @@
sub lset(*@args) is export {
method lset(*@args) {
if +@args < 2 {
error('wrong # args: should be "lset listVar index ?index...? value"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/lsort.pm
@@ -1,4 +1,4 @@
sub lsort(*@args) is export {
method lsort(*@args) {

error('wrong # args: should be "lsort ?options? list"')
unless +@args;
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/namespace.pm
@@ -1,4 +1,4 @@
sub namespace(*@args) is export {
method namespace(*@args) {
Namespace::dispatch_command(|@args);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/package.pm
@@ -1,4 +1,4 @@
sub package(*@args) is export {
method package(*@args) {
if +@args < 1 {
error('wrong # args: should be "package option ?argument ...?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/proc.pm
@@ -1,4 +1,4 @@
sub proc(*@args) is export {
method proc(*@args) {
if +@args != 3 {
error('wrong # args: should be "proc name args body"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/puts.pm
@@ -1,6 +1,6 @@
use src::init;

sub puts(*@args) is export {
method puts(*@args) {
my $nl := 1;
if @args[0] eq '-nonewline' {
@args.shift; $nl := 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/pwd.pm
@@ -1,4 +1,4 @@
sub pwd () is export {
method pwd() {
pir::new__Ps('OS').cwd();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/regexp.pm
@@ -1,4 +1,4 @@
sub regexp(*@args) is export {
method regexp(*@args) {
error('wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"')
if +@args < 2;

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/rename.pm
@@ -1,4 +1,4 @@
sub rename(*@args) is export {
method rename(*@args) {
if +@args != 2 {
error('wrong # args: should be "rename oldName newName"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/set.pm
@@ -1,4 +1,4 @@
sub set(*@args) is export {
method set(*@args) {
if +@args < 1 || +@args > 2 {
error('wrong # args: should be "set varName ?newValue?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/socket.pm
@@ -1,4 +1,4 @@
sub socket(*@args) is export {
method socket(*@args) {
'';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/source.pm
@@ -1,4 +1,4 @@
sub source($filename) is export {
method source($filename) {
Partcl::Compiler.evalfiles($filename);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/split.pm
@@ -1,4 +1,4 @@
sub split(*@args) is export {
method split(*@args) {
if +@args < 1 || +@args > 2 {
error('wrong # args: should be "split string ?splitChars?"')
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/string.pm
@@ -1,4 +1,4 @@
sub string(*@args) is export {
method string(*@args) {
String::dispatch_command(|@args);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/subst.pm
@@ -1,4 +1,4 @@
sub subst(*@args) is export {
method subst(*@args) {
'';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/switch.pm
@@ -1,4 +1,4 @@
sub switch(*@args) is export {
method switch(*@args) {
if +@args < 2 {
error('wrong # args: should be "switch ?switches? string pattern body ... ?default body?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/time.pm
@@ -1,4 +1,4 @@
sub time(*@args) is export {
method time(*@args) {
if +@args < 1 || +@args > 2 {
error('wrong # args: should be "time command ?count?"');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Partcl/commands/trace.pm
@@ -1,4 +1,4 @@
sub trace(*@args) is export {
method trace(*@args) {
if +@args < 1 {
error('wrong # args: should be "trace subcommand ?argument ...?"');
}
Expand Down

0 comments on commit 8156479

Please sign in to comment.