Programming Language Fun

Found some code I made a while ago, it was an attempt at writing a programming language (albeit a very simple one). I hadn’t gotten it working (maybe I had written it while I was tired), and decided I wanted to.

Here’s the source for a program using the most awesome language ever 🙂 :

The Books Of Totally Awesome Awesomeness

	Your Commander Is Tyler Church

	Execute All I Say

	Book 1: Printing Stuff

		Show the Deciples What I Am Capable Of

	You Will Obey My Will

	Book 2: Inspired By LOLCATS

		Show the Deciples I Can Haz Teh Cheezeburger

	You Will Obey My Will

This is The Word of The Programmer

Which should give us:

What I Am Capable Of
I Can Haz Teh Cheezeburger

I decided it should compile to a Ruby program, and wrote a very short, very unflexible converter:

# Compiler Variables
@indent = ""
@flags = {}
@flags[:exec_all] = false
@methods = []
@result = "# This code was generated by the BOOKS compiler, which was written by Tyler Church\n\n"

# Start Converting Code
text = File.read(ARGV[0])
text.split("\n")
text.each do |line|
	{
	"The Books Of" => lambda { |text| "# Program Name: " + text[13..-1] + "\n" },
	"Your Commander Is" => lambda { |text| "# Original Code Written By: " + text[18..-1] + "\n" },
	"Execute All I Say" => lambda { |text| @flags[:exec_all] = true ; "" },
	"Book" => lambda { |text| if text[0..3] == "Book" ; @indent += "\t" ; name = text.split(":")[1].strip.gsub(" ", "_").downcase ; @methods += [name] ; "def " + name + "\n" ; else ; "" ; end },
	"You Will Obey My Will" => lambda { |text| @indent.sub!("\t", "") ; "end\n" },
	"Show the Deciples" => lambda { |text| "puts \"" + text[18..-1] + "\"\n" },
	}.each do |text, code|
		@result += @indent + code.call(line.strip) if line.index(text) != nil
	end
end

# Process Flags
@flags.each do |flag, value|
	{
	:exec_all => lambda { |value| if value ; @methods .each { |name| @result += name + "\n"} ; end },
	}.each do |flag2, code|
		code.call(value)
	end
end

# Output Ruby File
File.open ARGV[0] + '.rb', 'w' do |f|
	f.puts @result
end

puts @result

# Run the program!
puts "\n-----------Running Program-----------"
exec("ruby " + ARGV[0] + '.rb')

(its a lot more readable if you view it in the plain source window)

That gives us this code:

# This code was generated by the BOOKS compiler, which was written by Tyler Church

# Program Name: Totally Awesome Awesomeness
# Original Code Written By: Tyler Church
def printing_stuff
	puts "What I Am Capable Of"
end
def inspired_by_lolcats
	puts "I Can Haz Teh Cheezeburger"
end
printing_stuff
inspired_by_lolcats

Which outputs this:

What I Am Capable Of
I Can Haz Teh Cheezeburger

It’s not really related to game development, but it was lots of fun making it, so I figured I’d put it up here. 🙂

Advertisements

~ by Tyler Church on September 26, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: