Log In · Register

 
scripting challenge, easy mode
heyo-captain-jac...
post Aug 22 2009, 04:59 PM
Post #1


/人◕‿‿◕人\
*******

Group: Official Member
Posts: 8,283
Joined: Dec 2007
Member No: 602,927



1. think up your own esoteric language.
2. write a hello world script with it.
3. explain the code.
4. show the output.
5. ???
6. profit!


Mine: a brainfuck-esque language that uses four commands and seven characters. Useful for learning binary in an extreme way.
CODE
.[->+>+>->+>->->->->+>+>->->+>->+>->+>+>->+>+>->->->+>+>->+>+>->->->+>+>->+>+>+>+>->->+>->->->->->->+>+>+>->+>+>->+>+>->+>+>+>+>->+>+>+>->->+>->->+>+>->+>+>->->->+>+>->->+>->->->->+>->->->->+]~[->+>+>+>+>+>+>->->->+>->+>+>+>+>->+>+>->+>->->->->+>+>->->+>->+>->+>+>->+>+>->->->+>+>->+>+>->->->+>+>->+>+>+>+>->+>+>+>->+>+>+>->+>+>->+>+>+>+>->+>+>+>->->+>->->+>+>->+>+>->->->+>+>->->+>->->->->+>->+>+>+>->->+>+>+>->+>->->->+>+>+>+>->->->->+>+>+>->+>->-]

This code creates "~/helloworld.txt" and writes "hello world!" to it.

.[(string)]~[(location)] = write (string) to (location)
- = turn bit off
+ = turn bit on
> = next bit





You can make a compiler if you really want to.
 
 
Start new topic
Replies
mipadi
post Nov 8 2009, 01:50 AM
Post #2


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



Wrote one for Ruby. Thought I'd have more fun with the Ruby version, so I used Treetop to generate a grammar.

Here's the grammar (buttf*ck.tt):

CODE
grammar Buttf*ck
  rule program
    '.[' string ']~[' location ']'
  end
  
  rule string
    bit+
  end
  
  rule location
    bit+
  end
  
  rule bit
    [+-] '>'?
  end
end


And here's the program that uses that grammar to parse a file (bf.rb):

CODE
#!/usr/bin/env ruby

require 'rubygems'
require 'treetop'
require 'buttf*ck'


class String
  def from_buttf*ck
    data = {:bitpos => 0, :masked => 0, :bitmask => 0}
    buf = ''
    each_char do |ch|
      case ch
      when '>'
        data[:bitpos] += 1
        if data[:bitpos] > 7
          buf << data[:masked]
          data[:bitpos] = 0
          data[:masked] = 0
        end
        data[:bitmask] = 2 ** (7 - data[:bitpos])
      when '+'
        data[:masked] |= (0xffffffff & data[:bitmask])
      when '-'
        data[:masked] |= (0x0 & data[:bitmask])
      end
    end
    buf
  end
  
  def to_path
    File.expand_path self
  end
end


if $0 == __FILE__
  if $*.length < 1
    $stderr.puts 'Usage: ruby bf.rb file.bf'
    exit 1
  end
  
  string = nil
  location = nil
  open($*[0], 'r') do |fh|
    nodes = Buttf*ckParser.new.parse fh.read.strip
    string = nodes.string.text_value.from_buttf*ck
    location = nodes.location.text_value.from_buttf*ck
  end
  open(location.to_path, 'w') { |fh| fh.print string }
end


Grammar: buttf*ck.tt
Source: bf.rb
 

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: