Skip to content

Instantly share code, notes, and snippets.

@melix

melix/hodorlang.groovy

Last active Aug 29, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.