aboutsummaryrefslogtreecommitdiff
path: root/src/Assembly/AlmostQhasm.v
diff options
context:
space:
mode:
authorGravatar Robert Sloan <varomodt@gmail.com>2016-03-29 20:58:39 -0400
committerGravatar Robert Sloan <varomodt@gmail.com>2016-06-22 13:42:37 -0400
commit066875fdbd323d7190750a24f17b0ab6dc599578 (patch)
treea24e67ac880d9f6b16a33086606850fb916516f6 /src/Assembly/AlmostQhasm.v
parent3d33bd49cb80f513dcbb38c77238cbe9a8d2ff3b (diff)
Module-based reorganization of Qhasm code
Diffstat (limited to 'src/Assembly/AlmostQhasm.v')
-rw-r--r--src/Assembly/AlmostQhasm.v57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/Assembly/AlmostQhasm.v b/src/Assembly/AlmostQhasm.v
new file mode 100644
index 000000000..31128fbfb
--- /dev/null
+++ b/src/Assembly/AlmostQhasm.v
@@ -0,0 +1,57 @@
+Require Import QhasmEvalCommon.
+Require Import Language.
+Require Import List.
+
+Module AlmostQhasm <: Language.
+ Import ListNotations.
+
+ (* A constant upper-bound on the number of operations we run *)
+ Definition maxOps: nat := 1000.
+
+ (* Program Types *)
+ Definition State := State.
+
+ Inductive AlmostProgram: Set :=
+ | ASkip: AlmostProgram
+ | ASeq: AlmostProgram -> AlmostProgram -> AlmostProgram
+ | AAssign: Assignment -> AlmostProgram
+ | AOp: Operation -> AlmostProgram
+ | ACond: Conditional -> AlmostProgram -> AlmostProgram -> AlmostProgram
+ | AWhile: Conditional -> AlmostProgram -> AlmostProgram.
+
+ Hint Constructors AlmostProgram.
+
+ Definition Program := AlmostProgram.
+
+ (* Only execute while loops a fixed number of times.
+ TODO (rsloan): can we do any better? *)
+
+ (* TODO: make this inductive *)
+ Fixpoint almostEvalWhile (cond: Conditional) (prog: Program) (state: State) (horizon: nat): option State :=
+ match horizon with
+ | (S m) =>
+ if (evalCond cond state)
+ then almostEvalWhile cond prog state m
+ else Some state
+ | O => None
+ end.
+
+ Fixpoint eval (prog: Program) (state: State): option State :=
+ match prog with
+ | ASkip => Some state
+ | ASeq a b =>
+ match (eval a state) with
+ | (Some st') => eval b st'
+ | _ => None
+ end
+ | AAssign a => Some (evalAssignment a state)
+ | AOp a => Some (evalOperation a state)
+ | ACond c a b =>
+ if (evalCond c state)
+ then eval a state
+ else eval b state
+ | AWhile c a => almostEvalWhile c a state maxOps
+ end.
+
+ (* world peace *)
+End AlmostQhasm.