Skip to content

Instantly share code, notes, and snippets.

@melix
Last active August 29, 2015 14:01
Show Gist options
  • Save melix/8580bdc709cdddd05277 to your computer and use it in GitHub Desktop.
Save melix/8580bdc709cdddd05277 to your computer and use it in GitHub Desktop.
The Hodor DSL
import org.codehaus.groovy.control.CompilerConfiguration
/*
* Copyright 2003-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// The "Hodor" DSL for Groovy
// freely inspired from the Brainfuck programming lanchage
def config = new CompilerConfiguration()
config.scriptBaseClass = DelegatingScript.name
def shell = new GroovyShell(config)
def script = shell.parse '''
hodor.hodor.hodor.hodor.hodor.hodor.hodor.hodor.hodor.hodor
hodor {
hodor Hodor
hodor.hodor.hodor.hodor.hodor.hodor.hodor
hodor Hodor
hodor.hodor.hodor.hodor.hodor.hodor.hodor.hodor.hodor.hodor
hodor Hodor
hodor
hodor.hodor
hodor Hodor
hodor
hodor 'hodor'
hodor 'hodor'
hodor 'hodor'
hodor 'hodor'
HODOR
}
hodor Hodor
hodor.hodor
hodor()
hodor Hodor
hodor.hodor.hodor
hodor.hodor.hodor
hodor
hodor.hodor
hodor.hodor
hodor()
HODOR.HODOR.HODOR.HODOR
HODOR.HODOR.HODOR.HODOR
HODOR.HODOR
HODOR
hodor()
hodor.hodor
hodor.hodor
hodor.hodor.hodor
hodor.hodor.hodor.hodor
hodor()
hodor
hodor.hodor
hodor()
hodor Hodor
hodor.hodor.hodor
hodor()
'''
script.setDelegate(new HodorEnvironment())
script.run() // prints "Hodor!"
class HodorEnvironment {
private char[] buffer = new char[128]
private int ptr = 0
public static final int Hodor = 1
public static final String Hodorr = 'hodor'
private void resize() {
int len = 2*buffer.length
char[] copy = new char[len]
System.arraycopy(buffer, 0, copy, 0, buffer.length)
buffer = copy
}
HodorEnvironment getHodor() {
buffer[ptr]++
this
}
HodorEnvironment getHODOR() {
buffer[ptr]--
this
}
HodorEnvironment hodor(Closure cl) {
def clone = cl.rehydrate(this, this, this)
while (buffer[ptr]!=0) {
clone()
}
this
}
HodorEnvironment hodor(int h) {
if (h==Hodor) {
ptr++
if (ptr==buffer.length) {
resize()
}
}
this
}
HodorEnvironment hodor(String h) {
if (h==Hodorr) {
ptr--
}
this
}
void hodor() {
print buffer[ptr]
}
public String toString() {
StringBuilder sb = new StringBuilder()
sb.append(buffer, 0, ptr+1)
sb
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment