From 74515bd11b6ab24237dd1bd598300cdf07b5a68b Mon Sep 17 00:00:00 2001 From: mcgirjau Date: Tue, 30 Jun 2020 00:08:48 -0400 Subject: [PATCH] Add output instructions --- .../push/instructions/input_output.clj | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/propeller/push/instructions/input_output.clj b/src/propeller/push/instructions/input_output.clj index a761642..6b4b111 100644 --- a/src/propeller/push/instructions/input_output.clj +++ b/src/propeller/push/instructions/input_output.clj @@ -1,6 +1,7 @@ (ns propeller.push.instructions.input-output (:require [propeller.push.state :as state] - [propeller.push.utils :refer [def-instruction]])) + [propeller.push.utils :refer [def-instruction + generate-instructions]])) ;; ============================================================================= ;; INPUT Instructions @@ -17,5 +18,34 @@ (throw (Exception. (str "Undefined input instruction " instruction))))) ;; ============================================================================= -;; OUTPUT and PRINT Instructions +;; OUTPUT Instructions ;; ============================================================================= + +(def-instruction + :print_newline + ^{:stacks [:print]} + (fn [state] + (let [current-output (state/peek-stack state :output) + popped-state (state/pop-stack state :output)] + (state/push-to-stack popped-state :output (str current-output \newline))))) + +(def _print + ^{:stacks [:print]} + (fn [stack state] + (if (state/empty-stack? state stack) + state + (let [top-item (state/peek-stack state stack) + top-item-str (if (or (string? top-item) + (char? top-item)) + top-item + (pr-str top-item)) + current-output (state/peek-stack state :output) + popped-state (state/pop-stack (state/pop-stack state stack) :output)] + (state/push-to-stack popped-state + :output + (str current-output top-item-str)))))) + +(generate-instructions + [:boolean :char :code :exec :float :integer :string + :vector_boolean :vector_float :vector_integer :vector_string] + [_print])