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 Oct 29 2009, 02:18 PM
Post #2


Senior Member
******

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



Boring day at work, wrote a Python version of the Buttfuck VM:

CODE
#!/usr/bin/env python

import os
import sys


class Buttf*ckToken(object):
    TYPES = ("STRING", "LOCATION", "EOF", "ERROR")
    
    def __init__(self, type, line, col, val=None):
        if type not in Buttf*ckToken.TYPES:
            raise ValueError("type must be one of %s" %
                             ", ".join(Buttf*ckToken.TYPES))
        self.type = type
        self.line = line
        self.col = col
        self.val = val
    
    def __str__(self):
        if self.val:
            return "%s(%s)" % (self.type, self.val)
        else:
            return "%s [%d]" % (self.type, self.line)


class Buttf*ckTokenizer(object):
    
    def __init__(self, stream):
        self.stream = stream
        self.line = 0
        self.col = 0
        self.buffer = []
    
    def getc(self):
        if self.buffer:
            ch = self.buffer[0]
            self.buffer = self.buffer[1:]
        else:
            ch = self.stream.read(1)
            self.col += 1
            if not ch:
                ch = '\'
            elif ch == '\t':
                ch = ' '
                self.col += 7
            elif ch == '\n':
                ch = ' '
                self.line += 1
        return ch
    
    def ungetc(self, ch):
        self.buffer.append(ch)
    
    def next(self):
        state = "START"
        buf = ""
        bitpos = 0
        masked = 0
        tkntype = None
        while state != "DONE":
            ch = self.getc()
            if state == "START":
                if ch == '\':
                    state = "DONE"
                    tkn = Buttf*ckToken("EOF", self.line, self.col)
                elif ch == '.':
                    state = "START"
                elif ch == '[':
                    state = "STRING"
                elif ch == '~':
                    state = "TILDE"
                else:
                    state = "ERROR"
                    buf += ch
            elif state == "DATA":
                if ch == '[':
                    state = "DATA"
                elif ch == '>':
                    bitpos += 1
                    if bitpos > 7:
                        buf += chr(masked)
                        bitpos = 0
                        masked = 0
                    bitmask = 2 ** (7 - bitpos)
                elif ch == '+':
                    state = "DATA"
                    masked |= (0xffffffff & bitmask)
                elif ch == '-':
                    state = "DATA"
                    masked |= (0x0 & bitmask)
                elif ch == ']':
                    state = "DONE"
                    buf += chr(masked)
                    tkn = Buttf*ckToken(tkntype, self.line, self.col)
                else:
                    state = "ERROR"
                    buf += ch
            elif state == "TILDE":
                if ch == '[':
                    state = "LOCATION"
                else:
                    state = "ERROR"
                    buf += ch
            elif state == "STRING":
                state = "DATA"
                tkntype = "STRING"
            elif state == "LOCATION":
                state = "DATA"
                tkntype = "LOCATION"
            elif state == "ERROR":
                if ch == '\':
                    state = "DONE"
                    tkn = Buttf*ckToken("ERROR", self.line, self.col)
                else:
                    buf += ch
            else:
                assert False, "Invalid state: " + state
        tkn.val = buf
        return tkn


class Buttf*ckParser(object):
    
    def __init__(self, tokenizer):
        self.tokenizer = tokenizer
        self.path = None
        self.str = None
    
    def parse(self):
        self._parse_string()
        self._parse_location()
        
        perror("Path:   " + self.path)
        perror("String: " + self.str)
        
        self.path = os.path.expanduser(self.path)
        self.path = os.path.realpath(self.path)
        with open(self.path, "w") as fh:
            fh.write(self.str)
        return True
    
    def _parse_string(self):
        tkn = self.tokenizer.next()
        if tkn.type == "STRING":
            self.str = tkn.val
        else:
            perror("Error: Line %d, Col %d" % (tkn.line, tkn.col))
    
    def _parse_location(self):
        tkn = self.tokenizer.next()
        if tkn.type == "LOCATION":
            self.path = tkn.val
        elif tkn.type == "EOF":
            pass
        else:
            perror("Error: Line %d, Col %d" % (tkn.line, tkn.col))


def perror(msg):
    print >>sys.stderr, "buttf*ck: " + msg


def main(argv):
    if len(argv) > 0:
        try:
            stream = open(argv[0])
        except IOError as e:
            perror(str(e))
            return 1
    else:
        stream = sys.stdin
    parser = Buttf*ckParser(Buttf*ckTokenizer(stream))
    if parser.parse():
        stream.close()
        return 0
    else:
        stream.close()
        return 2


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))


Source: buttuck.py (usual problem with the URL applies)

This post has been edited by mipadi: Oct 29 2009, 11:29 PM
Reason for edit: use bit.ly for the urls
 
tokyo-rose
post Oct 29 2009, 08:24 PM
Post #3


Senior Member
********

Group: Head Staff
Posts: 18,173
Joined: Mar 2005
Member No: 108,478



QUOTE(mipadi @ Oct 29 2009, 03:18 PM) *
Source: buttuck.py (usual problem with the URL applies)

Use a URL shortener, like bit.ly?
 
mipadi
post Oct 29 2009, 10:36 PM
Post #4


Senior Member
******

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



QUOTE(Cristy @ Oct 29 2009, 09:24 PM) *
Use a URL shortener, like bit.ly?

I thought those were only used to disguise really nasty porn links.
 

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: