(var labelNo 64) (define-function getNewLabel () (var c labelNo) (set! labelNo (+ c 1)) (return (. String fromCharCode labelNo))) (define-macro-with-comment vars "arguments are list of variable definitions" (fn (args) (map (function (list) (concat (quote (var)) list)) args))) (define-macro-with-comment define-hrm-code "~0 name. ~1 vars (list of pair) ~2- main codes." (syntax-template ((function () (set! labelNo 64) (return (+ "-- HUMAN RESOURCE MACHINE PROGRAM --\n\n" "-- " (symbol->string (quote ~0)) "\n\n\n" ~1-)))))) (// " -- HUMAN RESOURCE MACHINE PROGRAM -- INBOX OUTBOX COPYFROM 0 COPYTO 0 ADD 0 SUB 0 BUMPUP 0 BUMPDN 0 a: JUMP a b: JUMPZ b c: JUMPN c ") (define-function moveFromInputToAcc () (return (+ " INBOX \n"))) (define-function moveFromAccToOutput () (return (+ " OUTBOX \n"))) (define-function copyFromAccToMem (mem) (return (+ " COPYTO " mem "\n"))) (define-function copyFromMemToAcc (mem) (return (+ " COPYFROM " mem "\n"))) (define-function readMemAndAddToAcc (mem) (return (+ " ADD " mem "\n"))) (define-function readMemAndSubFromAcc (mem) (return (+ " SUB " mem "\n"))) (define-function readMemAndIncrementAndCopyToMem (mem) (return (+ " BUMPUP " mem "\n"))) (define-function readMemAndDecrimentAndCopyToMem (mem) (return (+ " BUMPDN " mem "\n"))) (define-function jumpToLabel (label) (return (+ " JUMP " label "\n"))) (define-function jumpToLabelIfAccIsZero (label) (return (+ " JUMPZ " label "\n"))) (define-function jumpToLabelIfAccIsNegative (label) (return (+ " JUMPN " label "\n"))) (define-function label (label) (return (+ label ":\n"))) (define-function infiniteLoop (exps) (var l (getNewLabel)) (return (+ (label l) (exps) (jumpToLabel l)))) (define-macro-with-comment infinite-loop "~0- content" (syntax-template (infiniteLoop (function () (return (+ ~0-)))))) (define-function whileAccIsNotZero (exps) (var b (getNewLabel)) (var e (getNewLabel)) (return (+ (label b) (jumpToLabelIfAccIsZero e) (exps) (jumpToLabel b) (label e)))) (define-macro-with-comment while-acc-is-not-zero "~0- content" (syntax-template (whileAccIsNotZero (function () (return (+ ~0-)))))) (define-function memcpy (from to) (return (+ (copyFromMemToAcc from) (copyFromAccToMem to))))
(define-hrm-code q1 (moveFromInputToAcc) (moveFromAccToOutput) (moveFromInputToAcc) (moveFromAccToOutput) (moveFromInputToAcc) (moveFromAccToOutput)) (define-hrm-code q2 (infinite-loop (moveFromInputToAcc) (moveFromAccToOutput))) (var c 0) (var sum 1) (var zero 5) (define-hrm-code q25 (infinite-loop (memcpy zero sum) (moveFromInputToAcc) (copyFromAccToMem c) (while-acc-is-not-zero (readMemAndAddToAcc sum) (copyFromAccToMem sum) (readMemAndDecrimentAndCopyToMem c)) (copyFromMemToAcc sum) (moveFromAccToOutput))) (var p 10) (var pp "[10]") (define-hrm-code q29 (infinite-loop (moveFromInputToAcc) (copyFromAccToMem p) (copyFromMemToAcc pp) (moveFromAccToOutput))) (var p 24) (var pp "[24]") (define-hrm-code q30 (infinite-loop (moveFromInputToAcc) (copyFromAccToMem p) (copyFromMemToAcc pp) (while-acc-is-not-zero (moveFromAccToOutput) (readMemAndIncrementAndCopyToMem p) (copyFromMemToAcc pp))))