Skip to content

Instantly share code, notes, and snippets.

@colomon
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colomon/2bb128f3b40df3ffaee8 to your computer and use it in GitHub Desktop.
Save colomon/2bb128f3b40df3ffaee8 to your computer and use it in GitHub Desktop.
use v6;
sub simple-intepreter($input) {
my %defined-words;
my @stack;
my $defining = False;
sub run-words(@words is copy) {
while $_ = @words.shift {
if $defining {
when ";" { $defining = False }
default { %defined-words{$defining}.push($_) }
}
when /^ \d+ $/ { @stack.push($_.Int) }
when "." { say @stack.pop }
when "dup" { @stack.push(@stack[*-1]) }
when "drop" { @stack.pop }
when "+" { @stack.push(@stack.pop + @stack.pop) }
when "-" { @stack.push(@stack.pop R- @stack.pop) }
when "*" { @stack.push(@stack.pop * @stack.pop) }
when "/" { @stack.push(@stack.pop Rdiv @stack.pop) }
when "mod" { @stack.push(@stack.pop Rmod @stack.pop) }
when "max" { @stack.push(@stack.pop max @stack.pop) }
when "min" { @stack.push(@stack.pop min @stack.pop) }
when "negate" { @stack[*-1] = -@stack[*-1] }
when ":" { $defining = @words.shift }
when %defined-words { run-words(%defined-words{$_}) }
default { die "Unrecognized word $_" }
}
}
run-words($input.words);
@stack
}
multi sub MAIN() {
my @results = simple-intepreter(slurp());
say "Ending stack " ~ @results.join(" ");
}
multi sub MAIN("test") {
use Test;
is simple-intepreter("1").join(", "), "1", "Just putting a number on the stack works";
is simple-intepreter("1 2 +").join(", "), "3", "Addition works";
is simple-intepreter("10 4 -").join(", "), "6", "Subtraction works";
is simple-intepreter("10 4 *").join(", "), "40", "Multiplication works";
is simple-intepreter("10 4 /").join(", "), "2", "Division works";
is simple-intepreter("10 3 mod").join(", "), "1", "Mod works";
is simple-intepreter("10 3 dup").join(", "), "10, 3, 3", "dup works";
is simple-intepreter("10 3 drop").join(", "), "10", "drop works";
is simple-intepreter("10 3 min").join(", "), "3", "min works";
is simple-intepreter("10 3 max").join(", "), "10", "max works";
is simple-intepreter("10 3 negate").join(", "), "10, -3", "negate works";
is simple-intepreter(": 2* 2 * ; 10 2* 23 2*").join(", "), "20, 46", "can define words";
done;
}
@raiph
Copy link

raiph commented Oct 26, 2014

I think this is a nice P6 example, independent of JaDogg_Coder's spec and P5 solution.

But I'm also curious about the pros and cons of this P6 solution compared to JaDogg_Coder's spec and solution, and what it would take to satisfy the spec as listed below (my quick assessment of how I think your gist'd solution compares are in italics in parens):

  • A reverse polish notation based compiler.
  • Very small subset of forth (Looks like your solution goes beyond JaDogg_Coder's spec and solution)
  • This is a proof of concept level compiler, no optimizations or over/underflow checking (Looks like your solution goes beyond JaDogg_Coder's spec and solution)
  • see the embedded POD for more information. (Your solution has no Pod.)
  • NASM is used as assembler. (Your solution uses NQP toolchain and backend specific codegen.)
  • gcc is used to link with glibc. (Your solution uses NQP toolchain and backend specific codegen.)
  • 32bit ELF Binary is generated (Your solution uses NQP toolchain and backend specific codegen.)

Can the NQP toolchain and backend specific codegen generate a 32bit ELF Binary? (Fwiw I think I've read that the MoarVM JIT, which is now on by default, generates 64 bit code only at the moment, and maybe forever.)

@colomon
Copy link
Author

colomon commented Oct 29, 2014

This "solution" is purely an interpreter, it just runs the "Forth" code in Perl 6. It doesn't even bother to tokenize, really; if you create a new word, it just stores the list of words in the new word as a list of strings.

I've thought about implementing a Forth in NQP, but haven't gotten around to studying jnthn's presentation on NQP work from last year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment