Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
7f24bb2
...
LavirtheWhiolet Sep 19, 2015
3e955e3
...
LavirtheWhiolet Sep 19, 2015
152e135
...
LavirtheWhiolet Sep 19, 2015
70c80c6
...
LavirtheWhiolet Sep 19, 2015
674ebad
...
LavirtheWhiolet Sep 19, 2015
6955608
...
LavirtheWhiolet Sep 19, 2015
063e370
...
LavirtheWhiolet Sep 19, 2015
5268d39
...
LavirtheWhiolet Sep 19, 2015
66462a2
...
LavirtheWhiolet Sep 19, 2015
9f46f1c
...
LavirtheWhiolet Sep 19, 2015
2959e03
...
LavirtheWhiolet Sep 19, 2015
10e1d12
...
LavirtheWhiolet Sep 19, 2015
e571491
...
LavirtheWhiolet Sep 19, 2015
f4cc05d
...
LavirtheWhiolet Sep 19, 2015
fb2e53d
...
LavirtheWhiolet Sep 19, 2015
60809d5
...
LavirtheWhiolet Sep 19, 2015
f316f60
...
LavirtheWhiolet Sep 19, 2015
f4addf8
...
LavirtheWhiolet Sep 19, 2015
f4e58f5
...
LavirtheWhiolet Sep 19, 2015
12d1141
...
LavirtheWhiolet Sep 19, 2015
39883d7
...
LavirtheWhiolet Sep 19, 2015
ae6e0d1
...
LavirtheWhiolet Sep 19, 2015
8f3e198
...
LavirtheWhiolet Sep 19, 2015
c6ffcb1
...
LavirtheWhiolet Sep 19, 2015
88a1af5
...
LavirtheWhiolet Sep 19, 2015
4f40479
...
LavirtheWhiolet Sep 19, 2015
949d48a
...
LavirtheWhiolet Sep 19, 2015
92805eb
...
LavirtheWhiolet Sep 19, 2015
4ad0dd4
...
LavirtheWhiolet Sep 19, 2015
99685c3
...
LavirtheWhiolet Sep 19, 2015
bd81aae
...
LavirtheWhiolet Sep 19, 2015
83d2988
...
LavirtheWhiolet Sep 19, 2015
e522ac2
...
LavirtheWhiolet Sep 19, 2015
801d8d1
...
LavirtheWhiolet Sep 19, 2015
118d4d0
...
LavirtheWhiolet Sep 19, 2015
1819837
...
LavirtheWhiolet Sep 19, 2015
d5bc506
...
LavirtheWhiolet Sep 19, 2015
06b5903
...
LavirtheWhiolet Sep 19, 2015
854150a
...
LavirtheWhiolet Sep 19, 2015
657910e
expand_standard_macros()
LavirtheWhiolet Sep 19, 2015
15b02e8
...
LavirtheWhiolet Sep 19, 2015
be450d3
...
LavirtheWhiolet Sep 19, 2015
d7a88cd
Update "README.md".
LavirtheWhiolet Sep 19, 2015
91fe38e
Bump version.
LavirtheWhiolet Sep 19, 2015
6528ef3
Fix bug: `rake clean` removed standalone script "petooh.rb" which is …
LavirtheWhiolet Sep 19, 2015
e9ec96b
README.md: Fix bug: Lists were not translated to <ul>/<ol>.
LavirtheWhiolet Sep 19, 2015
2f1b6da
.gitignore: Add "doc/*".
LavirtheWhiolet Sep 19, 2015
a321633
Fix doc. bugs.
LavirtheWhiolet Sep 19, 2015
520ec16
...
LavirtheWhiolet Sep 19, 2015
90ef33a
...
LavirtheWhiolet Sep 19, 2015
7214cd5
...
LavirtheWhiolet Sep 19, 2015
de0d7ff
...
LavirtheWhiolet Sep 19, 2015
ee2eb22
...
LavirtheWhiolet Sep 19, 2015
304b6b2
...
LavirtheWhiolet Sep 19, 2015
a28ad84
...
LavirtheWhiolet Sep 19, 2015
8ac2644
...
LavirtheWhiolet Sep 19, 2015
3ee518a
Bump version.
LavirtheWhiolet Sep 19, 2015
7951781
...
LavirtheWhiolet Sep 19, 2015
88f0d31
Major docs fix.
LavirtheWhiolet Sep 19, 2015
507576b
...
LavirtheWhiolet Sep 20, 2015
e96e968
...
LavirtheWhiolet Sep 20, 2015
64cf2e4
Bump version.
LavirtheWhiolet Sep 20, 2015
4ccf99b
...
LavirtheWhiolet Sep 20, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Ruby/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
peg2rb.rb
gen/*
*.gem
doc/*
4 changes: 4 additions & 0 deletions Ruby/.yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lib/**/*.rb
gen/lib/**/*.rb
README.md
gen/LICENSE
43 changes: 37 additions & 6 deletions Ruby/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
### DESCRIPTION

PETOOH interpreter written in [Ruby](http://ruby-lang.org).
[PETOOH](https://github.com/Ky6uk/PETOOH) interpreter written in [Ruby](http://ruby-lang.org) and [GPL](https://github.com/LavirtheWhiolet/self-bootstrap).

### DEPENDS

[Ruby](http://ruby-lang.org) 1.9.3 or higer.

### BUILD

First, you need to install:

- [Rake](http://docs.seattlerb.org/rake/). Usually it comes with Ruby, but if it isn't then you may install it with the command `gem install rake`.
- [GPL](https://github.com/LavirtheWhiolet/self-bootstrap). Just download ["peg2rb.rb"](https://github.com/LavirtheWhiolet/self-bootstrap/blob/master/peg2rb.rb) into this directory and you are ready.

To (re-)build PETOOH interpreter as a standalone Ruby script you give the command:

rake petooh.rb

To build a Ruby gem you give the command:

rake gem

To build everything in this package you just give the command:

rake

To clean up your directory give the command:

rake clean

### INSTALL

Copy "petooh.rb" to some directory.

Or install the corresponding Ruby gem by giving the command:

gem install petooh

### USAGE

To run:

~$ ruby petooh.rb file
~$ ruby petooh.rb [options] file

To get some help:
Or, if you have installed the interpreter as a Ruby gem:

~$ ruby petooh.rb -h
~$ petooh [options] file

### VERSION
Options:

It is compiled from [petooh-gpl](https://github.com/LavirtheWhiolet/petooh-gpl), commit [384cd996a2c7246d2a9905b3e9f24c5eb941539d on Wed Dec 10 02:02:00 2014 +0500](https://github.com/LavirtheWhiolet/petooh-gpl/commit/384cd996a2c7246d2a9905b3e9f24c5eb941539d).
- `-h` - Get some help.
115 changes: 115 additions & 0 deletions Ruby/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
require 'open-uri'

# ---------------
# :section: Tasks
# ---------------

file "gen/lib/petooh.rb" => "lib/petooh.peg" do |task|
peg2rb task.source, prepare_dir_for(task.name)
end

file "petooh.rb" => ["gen/lib/petooh.rb", "bin/petooh"] do |task|
gen_file(task.sources[1], prepare_dir_for(task.name)) do |content|
content.gsub(/require\s+['"]petooh['"]/) do |match|
"# #{match}\n"\
"#{File.read(task.sources[0])}"
end
end
end

file "gen/petooh.gemspec" => ["petooh.gemspec.erb", "../LICENSE"] do |task|
erb task.source, prepare_dir_for(task.name)
end

file "gen/LICENSE" => "../LICENSE" do |task|
cp task.source, prepare_dir_for(task.name)
end

file "gen/README" => "README.md" do |task|
cp task.source, prepare_dir_for(task.name)
end

GEM_FILES = <<LIST.lines.map(&:strip)
gen/lib/petooh.rb
bin/petooh
gen/petooh.gemspec
README.md
gen/README
gen/LICENSE
.yardopts
LIST

task gem: GEM_FILES do
sh "gem build gen/petooh.gemspec"
end

task all: ["petooh.rb", :gem]

task default: :all

task :clean do
rm_rf "gen"
FileList["*.gem"].each { |f| rm f }
end

# ---------------
# :section: Utils
# ---------------

#
# It performs following replacements in +file+:
# * "$Generated$" is replaced with
# <tt>"Generated from "{source_file}" on {current date}"</tt>.
#
def expand_standard_macros(file, source_file)
log "expand_standard_macros #{file}"
File.write(
file,
File.read(file).
gsub("$Generated$", %(Generated from "#{source_file}" on #{Time.now}))
)
end

def log(msg)
STDERR.puts msg
end

def erb src_file, dest_file
sh "erb -T 2 #{src_file} > #{dest_file}"
expand_standard_macros(dest_file, src_file)
end

def gen_file source_file, dest_file, &content_f
log "gen #{dest_file}"
File.write(dest_file, content_f.(File.read(source_file)))
expand_standard_macros(dest_file, source_file)
end

#
# Creates directory for +file+ if it does not exist.
#
# It returns +file+.
#
def prepare_dir_for(file)
mkdir_p File.dirname(file)
return file
end

def peg2rb src_file, dest_file
peg2rb = "peg2rb.rb"
# Ensure gpl is present.
if not File.file? peg2rb then
begin
STDERR.puts %(It looks like "#{peg2rb}" is not present. I will try to download it for you.)
open("https://raw.githubusercontent.com/LavirtheWhiolet/self-bootstrap/master/peg2rb.rb") do |io|
File.write(peg2rb, io.read)
end
rescue
raise %("#{peg2rb}" is not present; download it from "https://github.com/LavirtheWhiolet/self-bootstrap/blob/master/peg2rb.rb")
end
end
#
sh "ruby #{peg2rb} #{src_file} > #{dest_file}"
#
expand_standard_macros(dest_file, src_file)
end
19 changes: 19 additions & 0 deletions Ruby/bin/petooh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env ruby
# encoding: UTF-8
require 'petooh'

# Parse args.
case ARGV
when [], ["-h"], ["--help"]
puts <<-HELP
Usage: ruby #{File.basename(__FILE__)} file.koko
HELP
exit
end
file = ARGV[0]
# Run!
include PETOOH
File.open(file) do |io|
program = parse(io)
run program
end
138 changes: 138 additions & 0 deletions Ruby/lib/petooh.peg
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
{...
# encoding: UTF-8

# $Generated$

#
module PETOOH

#
# +program+ is an Array of PETOOH keywords (in the form of String-s).
#
# +debug+ - if true then PETOOH VM state is dumped on each execution step.
#
def run(program, debug = false)
# Init virtual machine.
mem = [0] * 16
data_ptr = 0
pc = 0 # Index of current instruction in program.
# Run!
while pc < program.size
# Log (if needed).
if debug
STDERR.puts "MEM: #{mem.join(" ")}"
STDERR.puts "PC: #{pc} (#{program[pc]})"
STDERR.puts "DP: #{data_ptr} (#{mem[data_ptr]})"
end
# Analyze current program instruction.
case program[pc]
when "Kudah"
#
data_ptr += 1
# Expand memory if needed.
if data_ptr >= mem.size
mem.concat([0] * 16)
end
#
pc += 1
when "kudah"
#
data_ptr -= 1
# Convert the machine's state from the form of
# { data_ptr = -1, mem = { -1:x, 0:y, 1:z, … } }
# to the form of
# { data_ptr = 0, mem = { 0:x, 1:y, 2:z, … } }
if data_ptr < 0
mem.unshift(0)
data_ptr = 0
end
#
pc += 1
when "Ko"
mem[data_ptr] += 1
pc += 1
when "kO"
mem[data_ptr] -= 1
pc += 1
when "Kukarek"
print mem[data_ptr].chr
pc += 1
when "Kud"
if mem[data_ptr] == 0
# Increment PC until corresponding "kud" is encountered
# (taking nested "Kud"/"kud" into account).
nest_level = 1
pc += 1
while pc < program.size and nest_level > 0
case program[pc]
when "Kud"
nest_level += 1
when "kud"
nest_level -= 1
end
pc += 1
end
else
# Just skip the instruction.
pc += 1
end
when "kud"
if mem[data_ptr] != 0
# Decrement PC until corresponding "Kud" is encountered
# (taking nested "Kud"/"kud" into account).
nest_level = 1
pc -= 1
while pc > 0 and nest_level > 0
case program[pc]
when "Kud"
nest_level -= 1
when "kud"
nest_level += 1
end
pc -= 1
end
# Position PC right to the corresponding "Kud".
pc += 1
else
# Just skip the instruction.
pc += 1
end
end
end
end

#
# reads PETOOH program from +io+ and parses it into Array of PETOOH
# keywords (in the form of String-s).
#
def parse(io)
yy_parse(io)
end

private

}...

program =
{ val = [] }
(instruction :>> val / comment)* ;

instruction =
<
/ "Kudah"
/ "kudah"
/ "Ko"
/ "kO"
/ "Kukarek"
/ "Kud"
/ "kud"
>:val
;

comment = char ;

{...

end

}...
50 changes: 50 additions & 0 deletions Ruby/petooh.gemspec.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<%
require 'rake'

class Object

def to_rb
self.inspect
end

end

class String

def lchomp(prefix)
if self.start_with? prefix then self[prefix.length..-1]
else self
end
end

end

%>

Gem::Specification.new do |s|
s.name = "petooh"
s.version = "6.0.4"
s.required_ruby_version = ">= 1.9.3"
s.license = <%= File.open("../LICENSE") { |io| io.gets.chomp }.to_rb %>
s.summary = "PETOOH interpreter"
s.description = <<-TEXT
Interpreter of PETOOH - the fundamentally new programming language.
TEXT
s.authors = ["Roman Nuritdinov", "Lavir the Whiolet"] # TODO: Get names from Git repository's history.
s.email = ["ky6uk.kun@gmail.com", "Lavir.th.Whiolet@gmail.com"] # TODO: Get e-mails from Git repository's history.
s.files = <%=
FileList[
"lib/**/*.rb", "gen/lib/**/*.rb",
"README.md",
"gen/README",
"gen/LICENSE",
".yardopts"
].to_a.to_rb
%>
s.require_paths = ["lib", "gen/lib"]
s.bindir = "bin"
s.executables = <%= FileList["bin/*"].map { |f| f.lchomp("bin/") }.to_rb %>
s.extra_rdoc_files = ["gen/README", "gen/LICENSE"]
s.rdoc_options << '--main' << 'gen/README'
s.homepage = "https://github.com/Ky6uk/PETOOH"
end
Loading