Skip to content

Instantly share code, notes, and snippets.

@c-spencer
Last active December 16, 2015 19:29
Show Gist options
  • Save c-spencer/5484973 to your computer and use it in GitHub Desktop.
Save c-spencer/5484973 to your computer and use it in GitHub Desktop.
(defn Buffer [string]
(set! this.string string)
(set! this.pos 0)
undefined)
(defn Buffer.prototype.read1 []
(cond
(= this.pos this.string.length)
(do
(set! this.pos (+ this.pos 1))
" ")
(> this.pos this.string.length)
(throw "EOF while reading")
:else
(let [ch (this.string[this.pos])]
(set! this.pos (+ this.pos 1))
ch)))
(defn Buffer.prototype.lookahead [n]
(this.string.substring this.pos (+ this.pos n)))
(defn Buffer.prototype.unread [s]
(set! this.pos (- this.pos s.length)))
; JS Output
$env.Buffer = function (string) {
this.string = string;
this.pos = 0;
return undefined;
};
$env.Buffer.prototype.read1 = function () {
return function () {
if (this.pos === this.string.length)
return function () {
this.pos = this.pos + 1;
return ' ';
}.call(this);
else if (this.pos > this.string.length)
return function () {
throw 'EOF while reading';
}.call(this);
else
return function () {
var ch = this.string[this.pos]();
this.pos = this.pos + 1;
return ch;
}.call(this);
}.call(this);
};
$env.Buffer.prototype.lookahead = function (n) {
return this.string.substring(this.pos, this.pos + n);
};
$env.Buffer.prototype.unread = function (s) {
return this.pos = this.pos - s.length;
};
class Buffer
constructor: (@string) ->
@pos = 0
read1: ->
if @pos == @string.length
++@pos
return " "
else if @pos > @string.length
throw 'EOF while reading'
ch = @string[@pos]
++@pos
ch
lookahead: (n) ->
@string.substring(@pos, @pos + n)
unread: (str) ->
@pos -= str.length
# JS Output
var Buffer;
Buffer = (function() {
function Buffer(string) {
this.string = string;
this.pos = 0;
}
Buffer.prototype.read1 = function() {
var ch;
if (this.pos === this.string.length) {
++this.pos;
return " ";
} else if (this.pos > this.string.length) {
throw 'EOF while reading';
}
ch = this.string[this.pos];
++this.pos;
return ch;
};
Buffer.prototype.lookahead = function(n) {
return this.string.substring(this.pos, this.pos + n);
};
Buffer.prototype.unread = function(str) {
return this.pos -= str.length;
};
return Buffer;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment