Skip to content

Commit

Permalink
fixed generated code comments
Browse files Browse the repository at this point in the history
alien movemement
  • Loading branch information
bill committed Jun 6, 2011
1 parent a3dbf16 commit f31f06c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 18 deletions.
4 changes: 2 additions & 2 deletions evaluator.html
Expand Up @@ -118,8 +118,8 @@
function runReduce(expr) {
var last

formatExpr(" <a style='border: solid black 1px;font-family: monospace' onclick='toggleSteps(this)'>-</a> Reduce", expr, result)
results.push("<div class='hidden'></div>")
formatExpr(" <a style='border: solid black 1px;font-family: monospace' onclick='toggleSteps(this)'>+</a> Reduce", expr, result)
results.push("<div></div>")
displayResults()
last = expr = expr.globalSub()
var trace = result.lastElementChild
Expand Down
36 changes: 21 additions & 15 deletions invaders.html
Expand Up @@ -117,8 +117,8 @@
next()
}
function display() {
updateGrid(state(LC.code['1of3']))
updateShip(state(LC.code['2of3']))
updateGrid(state(LC.code['first']))
updateShip(state(LC.code['second']))
}
function loaded() {
wrap = LC.wrap
Expand Down Expand Up @@ -151,6 +151,7 @@
# Booleans
true = \x y . x
false = \x y . y
not = \a . a false true

# Y combinator
Y = \g . (\x . g (x x)) \x . g (x x)
Expand Down Expand Up @@ -198,12 +199,13 @@
# row functions
row = \1 2 3 4 5 6 7 8 9 g . g 1 2 3 4 5 6 7 8 9
cell1 = \1 2 3 4 5 6 7 8 9 . cell 1
cell9 = \1 2 3 4 5 6 7 8 9 . cell 9
get1 = \1 2 3 4 5 6 7 8 9 . 1
left = \1 2 3 4 5 6 7 8 9 . row 2 3 4 5 6 7 8 9 1
right = \1 2 3 4 5 6 7 8 9 . row 9 1 2 3 4 5 6 7 8
empty = row e e e e e e e e e
arow1 = row e a1 e a2 e a1 e a2 e
arow2 = row e a2 e a1 e a2 e a1 e
arow1 = row e e a1 e a2 e a1 e e
arow2 = row e e a2 e a1 e a2 e e
gridStart = row arow1 arow2 arow1 arow2 empty empty empty empty empty
shipStart = row e e e e s e e e e

Expand All @@ -212,26 +214,30 @@
ignore = \x . row
onFirstEmpty = \1 2 3 4 5 6 7 8 9 . 1 identity ignore ignore ignore ignore
# compare a cell with the first row element. If they're both e, return e otherwise return one of the nonE values
check1 = \n r . r \m1 m2 m3 m4 m5 m6 m7 m8 m9 . cell (n m1 n n n n)
ifAllFirstEmpty = \1 2 3 4 5 6 7 8 9 . 1 cell1 check1 2 check1 3 check1 4 check1 5 check1 6 check1 7 check1 8 check1 9 isE
check1 = \n r . r \m1 m2 m3 m4 m5 m6 m7 m8 m9 . cell (n m1 n n n n)
check9 = \n r . r \m1 m2 m3 m4 m5 m6 m7 m8 m9 . cell (n m9 n n n n)
isAllFirstEmpty = \1 2 3 4 5 6 7 8 9 . 1 cell1 check1 2 check1 3 check1 4 check1 5 check1 6 check1 7 check1 8 check1 9 isE
isAllLastEmpty = \1 2 3 4 5 6 7 8 9 . 1 cell9 check9 2 check9 3 check9 4 check9 5 check9 6 check9 7 check9 8 check9 9 isE
allLeft = \1 2 3 4 5 6 7 8 9 . row (1 left) (2 left) (3 left) (4 left) (5 left) (6 left) (7 left) (8 left) (9 left)
allRight = \1 2 3 4 5 6 7 8 9 . row (1 right) (2 right) (3 right) (4 right) (5 right) (6 right) (7 right) (8 right) (9 right)

# game functions
start = \statef . statef gridStart shipStart dir
next = \grid ship dir statef . statef grid ship dir
start = \statef . statef gridStart shipStart true (row true false false false false false false false false)
checkDir = \grid left? . left? (grid isAllFirstEmpty) (not (grid isAllLastEmpty))
next = \grid ship left? counter statef . (\dir . statef (counter get1 (grid (dir allLeft allRight)) grid) ship dir (counter left)) (checkDir grid left?)

# in progress
# moveThem = (\moveLeft moveRight . pair moveLeft moveRight) rec \moveThem

#events
moveLeft = \grid ship dir . next grid (ship (ship onFirstEmpty left)) dir
moveRight = \grid ship dir . next grid (ship (ship right onFirstEmpty right)) dir
stay = \grid ship dir . next grid ship dir
fire = \grid ship dir . next (missile grid ship) ship dir
moveLeft = \grid ship left? counter . next grid (ship (ship onFirstEmpty left)) left? counter
moveRight = \grid ship left? counter . next grid (ship (ship right onFirstEmpty right)) left? counter
stay = \grid ship left? counter . next grid ship left? counter
fire = \grid ship left? counter . next (missile grid ship) ship left? counter

# accessors
1of3 = \a b c . a
2of3 = \a b c . b
3of3 = \a b c . c
first = \a b c d . a
second = \a b c d . b
</pre>
</body>
</html>
4 changes: 3 additions & 1 deletion lc.js
Expand Up @@ -147,9 +147,11 @@ function constructEnv(src) {
var env = ['(function(){\n']

for (var i = 0; i < order.length; i++) {
env.push('LC.code[order[' + i + '].name] = order[' + i + '].code = ' + order[i].src)
if (order[i].name != "") {
env.push('\n// ' + order[i].name + ' = ' + order[i].expr.format(true, true))
}
env.push('LC.code[order[' + i + '].name] = order[' + i + '].code = ' + order[i].src)
if (order[i].name != "") {
env.push("var " + order[i].cname + ' = ' + 'order[' + i + '].code')
env.push("L." + order[i].cname + " = " + order[i].cname)
}
Expand Down
75 changes: 75 additions & 0 deletions lcvm.js
@@ -0,0 +1,75 @@
LC = (function(){
var code = []
var labels = {} // label -> code offset
var stack = []
var vars = []
var pc = 0
var sp = 0
var ct = null
//OPCODES
var APPLY = 0 // APPLY: TMP = bind(stack[sp], stack[sp - 1]), stack[sp - 1] = ct, stack[sp] = pc + 1, pc = TMP[0], ct = TMP ([func, arg] get replaced with [ct, pc])
var VAR = 1 // VAR, offset: stack.push(ct[offset])
var GVAR = 2 // VAR, offset: stack.push(vars[offset])
var LAMBDA = 3 // LAMBDA, addr: context(addr)
var PRIMITIVE = 4 // PRIMITIVE, function
var RETURN = 5 // RETURN: ct = stack[sp-2], stack[sp-2] = stack[sp], pc = stack[sp-1], sp -= 2

function label(name) {labels[name] = code.length}

function closure(addr, indices) {
var cl = [addr]

for (var i = 1; i <= indices.length; i++) {
cl.push(ct[indices[i]])
}
return cl
}

function execute(label) {
var len = code.length

stack = []
stack.push(nil, -1)
sp = 1
pc = labels[label]
while (pc > -1) {
switch (code[pc++]) {
case APPLY: // APPLY: TMP = EXTEND(stack[sp], stack[sp - 1]), stack[sp - 1] = ct, stack[sp] = pc + 1, pc = TMP[0], ct = TMP ([func, arg] get replaced with [ct, pc])
var con = stack[sp]
var tmp = con[con.length - 1] = stack[sp - 1]

stack[sp] = pc
stack[sp - 1] = ct
pc = tmp[0]
ct = tmp
break
case VAR: // VAR, offset: S.push(ct[offset])
s[++sp] = ct[pc++]
break
case GVAR: // VAR, offset: S.push(vars[offset])
s[++sp] = vars[pc++]
break
case LAMBDA:
var cl = [code[pc++]]

for (var i = 1; i <= indices.length; i++) {
cl.push(ct[indices[i]])
}
s[++sp] = cl
break
case PRIMITIVE:
s[++sp] = code[pc++]()
break
case RETURN: // RETURN: ct = stack[sp-3], stack[sp-2] = stack[sp], pc = stack[sp-1], sp -= 2
pc = stack[sp - 1]
ct = stack[sp - 2]
stack[sp - 2] = stack[sp]
sp -= 2
break
}
}
}

return {
}
})()

0 comments on commit f31f06c

Please sign in to comment.