# HG changeset patch # User Michael Pavone # Date 1406528363 25200 # Node ID 8a0f1447c034330bb0d9fc04c6e5c33980138208 # Parent f4f403c83e8040029ecd24dc5cf0153daf805e4d Implement plain if in gqc and use it in ghost0. This provides a small improvement in code size diff -r f4f403c83e80 -r 8a0f1447c034 code/ghost0.gq --- a/code/ghost0.gq Sun Jul 27 23:11:46 2014 -0700 +++ b/code/ghost0.gq Sun Jul 27 23:19:23 2014 -0700 @@ -10,7 +10,7 @@ } else: { if: dir = left { startX <- startX - 1 - } else: {} + } } startX } @@ -21,7 +21,7 @@ } else: { if: dir = down { startY <- startY + 1 - } else: {} + } } startY } @@ -99,7 +99,7 @@ //currently in fright mode, try to run away firstChoice <- opDir: firstChoice secondChoice <- opDir: secondChoice - } else: {} + } diff -r f4f403c83e80 -r 8a0f1447c034 code/gqc.tp --- a/code/gqc.tp Sun Jul 27 23:11:46 2014 -0700 +++ b/code/gqc.tp Sun Jul 27 23:19:23 2014 -0700 @@ -520,6 +520,108 @@ } } + _funHandlers set: "if" :args syms { + cond <- (args value) + trueBody <- ((args tail) value) expressions + + if: (cond nodeType) = (ast binary) { + trueLbl <- prog makeLabel: "true" + endLbl <- prog makeLabel: "end" + + l <- 0 + r <- preserveTemps: { + l <- compileExpr: (cond left) syms: syms + compileExpr: (cond right) syms: syms + } + + ok <- true + + if: (cond op) = ">=" { + prog add: (inst: "JLT" #[ + endLbl + l + r + ]) + } else: { + if: (cond op) = "<=" { + prog add: (inst: "JGT" #[ + endLbl + l + r + ]) + } else: { + if: (cond op) = "!=" { + prog add: (inst: "JEQ" #[ + endLbl + l + r + ]) + } else: { + if: (cond op) = ">" { + prog add: (inst: "JGT" #[ + trueLbl + l + r + ]) + prog add: (inst: "MOV" #[ + reg: 8 + endLbl + ]) + } else: { + if: (cond op) = "<" { + prog add: (inst: "JLT" #[ + trueLbl + l + r + ]) + prog add: (inst: "MOV" #[ + reg: 8 + endLbl + ]) + } else: { + bodyLbl <- prog makeLabel: "loop_body" + if: (cond op) = "=" { + prog add: (inst: "JEQ" #[ + trueLbl + l + r + ]) + prog add: (inst: "MOV" #[ + reg: 8 + endLbl + ]) + } else: { + ok <- false + } + } + } + } + } + } + if: ok { + prog setLabel: trueLbl + //TODO: do 2 passes for labels to allow forward references + foreach: trueBody :idx expr { + if: (expr nodeType) = (ast sym) { + //allow using bare symbols to define labels + lbl <- prog makeLabel: (expr name) + prog setLabel: lbl + syms define: (expr name) lbl + } else: { + v <- preserveTemps: { + compileExpr: expr syms: syms + } + } + } + prog setLabel: endLbl + } else: { + error: "Condition parameter to if must be a comparison operator expression" + } + } else: { + error: "Condition parameter to if must be a comparison operator expression" + } + } + _exprHandlers set: (ast call) :expr syms { tc <- (expr tocall) if: (tc nodeType) = (ast sym) {